Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify setSubscript, setSuperscript #4190

Merged
merged 4 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 14 additions & 89 deletions src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,6 @@
* @return {Number} fontSize of the character
*/
getHeightOfChar: function(line, char) {
var size = this.getValueOfPropertyAt(line, char, '*fontSize');
if (typeof size === 'number') {
return size;
}

return this.getValueOfPropertyAt(line, char, 'fontSize');
},

Expand Down Expand Up @@ -1143,7 +1138,7 @@
* @returns {Object} this
*/
setSuperscript: function(line, char) {
return this.superscript.apply(this, line, char);
return this._setScript(line, char, this.superscript);
},

/**
Expand All @@ -1153,7 +1148,14 @@
* @returns {Object} this
*/
setSubscript: function(line, char) {
return this.subscript.apply(this, line, char);
return this._setScript(line, char, this.subscript);
},

_setScript: function(line, char, schema) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some checks are missing here

var fontSize = this.getValueOfPropertyAt(line, char, 'fontSize'),
baseline = this.getValueOfPropertyAt(line, char, 'deltaY') + fontSize * schema.baseline;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's supposed to be ... + fontSize * -schema.baseline (follows the baseline-shift attribute from SVG)

this.setPropertyAt(line, char, 'deltaY', baseline);
this.setPropertyAt(line, char, 'fontSize', fontSize * schema.size);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return this;

},

/**
Expand All @@ -1169,7 +1171,7 @@
prevStyle.fontFamily !== thisStyle.fontFamily ||
prevStyle.fontWeight !== thisStyle.fontWeight ||
prevStyle.fontStyle !== thisStyle.fontStyle ||
prevStyle.deltaY != thisStyle.deltaY
prevStyle.deltaY !== thisStyle.deltaY
);
},

Expand Down Expand Up @@ -1259,6 +1261,10 @@
if (charStyle && typeof charStyle[property] !== 'undefined') {
return charStyle[property];
}
if (property === 'deltaY') {
// if did not find deltaY in style, return 0
return 0;
}

return this[property];
},
Expand Down Expand Up @@ -1441,87 +1447,6 @@
complexity: function() {
return 1;
}
}, function(prototype, Text) {
prototype.superscript.apply = prototype.subscript.apply =
/**
* Mutates the style at given position in 'self' based on values from 'this'
* @param {Object} self the object to be mutated
* @param {Number} line the line number or the starting position
* @param {Number} char the character number or the final position
* @returns {Object} self
*/
function(self, line, char) {
var schema = this;
var apply = function(line, char) {
var l = self._textLines[line];
if (l == null || typeof l[char] !== 'string') {
return self;
}

var size = self.getValueOfPropertyAt(line, char, 'fontSize');
if (typeof self.getValueOfPropertyAt(line, char, '*fontSize') !== 'number') {
self.setPropertyAt(line, char, '*fontSize', size);
}

var dy = self.getValueOfPropertyAt(line, char, 'deltaY') || 0;
self.setPropertyAt(line, char, 'fontSize', size * schema.size);
self.setPropertyAt(line, char, 'deltaY', dy + size * -schema.baseline);
return self;
};

if (typeof line === 'number') {
if (typeof char === 'number') {
return apply(line, char);
}

var c1 = char[0], c2 = char[1];
if (c1 >= c2) {
return self;
}

for (var c = c1; c <= c2; c++) {
apply(line, c, this);
}
return self;
}

var l1 = line[0], C1 = line[1];
if (typeof char !== 'object') {
return apply(l1, C1);
}

var l2 = char[0], C2 = char[1];
if (l1 > l2) {
return self;
}

var lines = self._textLines;
for (var l = l1; l <= l2; l++) { // lines
line = lines[l];
if (line == null) {
break;
}

var c1 = 0, c2 = line.length - 1;
switch (l) {
case l1:
c1 = C1;
break;

case l2:
c2 = C2;
}

if (l1 == l2 && c1 == c2 || c1 > c2) {
break;
}
for (var c = c1; c <= c2; c++) { // characters
apply(l, c);
}
}

return self;
};
});

/* _FROM_SVG_START_ */
Expand Down
4 changes: 0 additions & 4 deletions src/util/lang_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

/** @ignore */
addMethods = function(klass, source, parent) {
if (typeof source === 'function') {
return source(klass.prototype, klass, parent);
}

for (var property in source) {

if (property in klass.prototype &&
Expand Down