fabric 5.1.0 → 5.2.2-browser

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util/misc.js CHANGED
@@ -651,6 +651,9 @@
651
651
  groupSVGElements: function(elements, options, path) {
652
652
  var object;
653
653
  if (elements && elements.length === 1) {
654
+ if (typeof path !== 'undefined') {
655
+ elements[0].sourcePath = path;
656
+ }
654
657
  return elements[0];
655
658
  }
656
659
  if (options) {
@@ -681,7 +684,7 @@
681
684
  * @return {Array} properties Properties names to include
682
685
  */
683
686
  populateWithProperties: function(source, destination, properties) {
684
- if (properties && Object.prototype.toString.call(properties) === '[object Array]') {
687
+ if (properties && Array.isArray(properties)) {
685
688
  for (var i = 0, len = properties.length; i < len; i++) {
686
689
  if (properties[i] in source) {
687
690
  destination[properties[i]] = source[properties[i]];
@@ -1215,5 +1218,112 @@
1215
1218
  }
1216
1219
  return new fabric.Group([a], { clipPath: b, inverted: inverted });
1217
1220
  },
1221
+
1222
+ /**
1223
+ * @memberOf fabric.util
1224
+ * @param {Object} prevStyle first style to compare
1225
+ * @param {Object} thisStyle second style to compare
1226
+ * @param {boolean} forTextSpans whether to check overline, underline, and line-through properties
1227
+ * @return {boolean} true if the style changed
1228
+ */
1229
+ hasStyleChanged: function(prevStyle, thisStyle, forTextSpans) {
1230
+ forTextSpans = forTextSpans || false;
1231
+ return (prevStyle.fill !== thisStyle.fill ||
1232
+ prevStyle.stroke !== thisStyle.stroke ||
1233
+ prevStyle.strokeWidth !== thisStyle.strokeWidth ||
1234
+ prevStyle.fontSize !== thisStyle.fontSize ||
1235
+ prevStyle.fontFamily !== thisStyle.fontFamily ||
1236
+ prevStyle.fontWeight !== thisStyle.fontWeight ||
1237
+ prevStyle.fontStyle !== thisStyle.fontStyle ||
1238
+ prevStyle.deltaY !== thisStyle.deltaY) ||
1239
+ (forTextSpans &&
1240
+ (prevStyle.overline !== thisStyle.overline ||
1241
+ prevStyle.underline !== thisStyle.underline ||
1242
+ prevStyle.linethrough !== thisStyle.linethrough));
1243
+ },
1244
+
1245
+ /**
1246
+ * Returns the array form of a text object's inline styles property with styles grouped in ranges
1247
+ * rather than per character. This format is less verbose, and is better suited for storage
1248
+ * so it is used in serialization (not during runtime).
1249
+ * @memberOf fabric.util
1250
+ * @param {object} styles per character styles for a text object
1251
+ * @param {String} text the text string that the styles are applied to
1252
+ * @return {{start: number, end: number, style: object}[]}
1253
+ */
1254
+ stylesToArray: function(styles, text) {
1255
+ // clone style structure to prevent mutation
1256
+ var styles = fabric.util.object.clone(styles, true),
1257
+ textLines = text.split('\n'),
1258
+ charIndex = -1, prevStyle = {}, stylesArray = [];
1259
+ //loop through each textLine
1260
+ for (var i = 0; i < textLines.length; i++) {
1261
+ if (!styles[i]) {
1262
+ //no styles exist for this line, so add the line's length to the charIndex total
1263
+ charIndex += textLines[i].length;
1264
+ continue;
1265
+ }
1266
+ //loop through each character of the current line
1267
+ for (var c = 0; c < textLines[i].length; c++) {
1268
+ charIndex++;
1269
+ var thisStyle = styles[i][c];
1270
+ //check if style exists for this character
1271
+ if (thisStyle) {
1272
+ var styleChanged = fabric.util.hasStyleChanged(prevStyle, thisStyle, true);
1273
+ if (styleChanged) {
1274
+ stylesArray.push({
1275
+ start: charIndex,
1276
+ end: charIndex + 1,
1277
+ style: thisStyle
1278
+ });
1279
+ }
1280
+ else {
1281
+ //if style is the same as previous character, increase end index
1282
+ stylesArray[stylesArray.length - 1].end++;
1283
+ }
1284
+ }
1285
+ prevStyle = thisStyle || {};
1286
+ }
1287
+ }
1288
+ return stylesArray;
1289
+ },
1290
+
1291
+ /**
1292
+ * Returns the object form of the styles property with styles that are assigned per
1293
+ * character rather than grouped by range. This format is more verbose, and is
1294
+ * only used during runtime (not for serialization/storage)
1295
+ * @memberOf fabric.util
1296
+ * @param {Array} styles the serialized form of a text object's styles
1297
+ * @param {String} text the text string that the styles are applied to
1298
+ * @return {Object}
1299
+ */
1300
+ stylesFromArray: function(styles, text) {
1301
+ if (!Array.isArray(styles)) {
1302
+ return styles;
1303
+ }
1304
+ var textLines = text.split('\n'),
1305
+ charIndex = -1, styleIndex = 0, stylesObject = {};
1306
+ //loop through each textLine
1307
+ for (var i = 0; i < textLines.length; i++) {
1308
+ //loop through each character of the current line
1309
+ for (var c = 0; c < textLines[i].length; c++) {
1310
+ charIndex++;
1311
+ //check if there's a style collection that includes the current character
1312
+ if (styles[styleIndex]
1313
+ && styles[styleIndex].start <= charIndex
1314
+ && charIndex < styles[styleIndex].end) {
1315
+ //create object for line index if it doesn't exist
1316
+ stylesObject[i] = stylesObject[i] || {};
1317
+ //assign a style at this character's index
1318
+ stylesObject[i][c] = Object.assign({}, styles[styleIndex].style);
1319
+ //if character is at the end of the current style collection, move to the next
1320
+ if (charIndex === styles[styleIndex].end - 1) {
1321
+ styleIndex++;
1322
+ }
1323
+ }
1324
+ }
1325
+ }
1326
+ return stylesObject;
1327
+ }
1218
1328
  };
1219
1329
  })(typeof exports !== 'undefined' ? exports : this);