modern-text 1.3.8 → 1.3.10

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/dist/index.cjs CHANGED
@@ -453,20 +453,7 @@ function parseValueNumber(value, ctx) {
453
453
  }
454
454
  }
455
455
  function parseColormap(colormap) {
456
- const _colormap = isNone(colormap) ? {} : colormap;
457
- return Object.keys(_colormap).reduce((obj, key) => {
458
- let value = _colormap[key];
459
- const keyRgb = hexToRgb(key);
460
- const valueRgb = hexToRgb(value);
461
- if (keyRgb) {
462
- key = keyRgb;
463
- }
464
- if (valueRgb) {
465
- value = valueRgb;
466
- }
467
- obj[key] = value;
468
- return obj;
469
- }, {});
456
+ return isNone(colormap) ? {} : colormap;
470
457
  }
471
458
  function isNone(val) {
472
459
  return !val || val === "none";
@@ -947,7 +934,11 @@ function background() {
947
934
  update: (text) => {
948
935
  pathSet.paths.length = 0;
949
936
  const { style, lineBox, isVertical } = text;
950
- const { backgroundImage, backgroundSize } = style;
937
+ const {
938
+ backgroundImage,
939
+ backgroundSize,
940
+ backgroundColormap = "none"
941
+ } = style;
951
942
  if (isNone(backgroundImage))
952
943
  return;
953
944
  const { pathSet: imagePathSet } = parser.parse(backgroundImage);
@@ -958,7 +949,17 @@ function background() {
958
949
  } else {
959
950
  ({ x, y, width, height } = lineBox);
960
951
  }
961
- const paths = imagePathSet.paths.map((p) => p.clone());
952
+ const colormap = parseColormap(backgroundColormap);
953
+ const paths = imagePathSet.paths.map((p) => {
954
+ const cloned = p.clone();
955
+ if (cloned.style.fill && cloned.style.fill in colormap) {
956
+ cloned.style.fill = colormap[cloned.style.fill];
957
+ }
958
+ if (cloned.style.stroke && cloned.style.stroke in colormap) {
959
+ cloned.style.stroke = colormap[cloned.style.stroke];
960
+ }
961
+ return cloned;
962
+ });
962
963
  let scaleX;
963
964
  let scaleY;
964
965
  if (backgroundSize === "rigid") {
@@ -1352,41 +1353,37 @@ function listStyle() {
1352
1353
  }
1353
1354
  const imagePathSet = modernPath2d.svgToPath2DSet(image);
1354
1355
  const imageBox = imagePathSet.getBoundingBox();
1355
- let prevChar;
1356
- paragraph.fragments.forEach((f) => {
1357
- f.characters.forEach((c) => {
1358
- const { inlineBox } = c;
1359
- if (isVertical ? prevChar?.inlineBox.left !== inlineBox.left : prevChar?.inlineBox.top !== inlineBox.top) {
1360
- prevChar = c;
1361
- const scale = size === "cover" ? 1 : parseValueNumber(size, { total: fontSize, fontSize }) / fontSize;
1362
- const m = new modernPath2d.Matrix3();
1363
- if (isVertical) {
1364
- const _scale = fontSize / imageBox.height * scale;
1365
- m.translate(-imageBox.left, -imageBox.top).rotate(Math.PI / 2).scale(_scale, _scale).translate(
1366
- inlineBox.left + (inlineBox.width - imageBox.height * _scale) / 2,
1367
- inlineBox.top - padding
1368
- );
1369
- } else {
1370
- const _scale = fontSize / imageBox.height * scale;
1371
- m.translate(-imageBox.left, -imageBox.top).scale(_scale, _scale).translate(
1372
- inlineBox.left - imageBox.width * _scale - padding,
1373
- inlineBox.top + (inlineBox.height - imageBox.height * _scale) / 2
1374
- );
1375
- }
1376
- pathSet.paths.push(...imagePathSet.paths.map((p) => {
1377
- const path = p.clone();
1378
- path.applyTransform(m);
1379
- if (path.style.fill && path.style.fill in colormap) {
1380
- path.style.fill = colormap[path.style.fill];
1381
- }
1382
- if (path.style.stroke && path.style.stroke in colormap) {
1383
- path.style.stroke = colormap[path.style.stroke];
1384
- }
1385
- return path;
1386
- }));
1387
- }
1388
- });
1389
- });
1356
+ const char = paragraph.fragments[0]?.characters[0];
1357
+ if (!char) {
1358
+ return;
1359
+ }
1360
+ const { inlineBox } = char;
1361
+ const scale = size === "cover" ? 1 : parseValueNumber(size, { total: fontSize, fontSize }) / fontSize;
1362
+ const m = new modernPath2d.Matrix3();
1363
+ if (isVertical) {
1364
+ const _scale = fontSize / imageBox.height * scale;
1365
+ m.translate(-imageBox.left, -imageBox.top).rotate(Math.PI / 2).scale(_scale, _scale).translate(
1366
+ inlineBox.left + (inlineBox.width - imageBox.height * _scale) / 2,
1367
+ inlineBox.top - padding
1368
+ );
1369
+ } else {
1370
+ const _scale = fontSize / imageBox.height * scale;
1371
+ m.translate(-imageBox.left, -imageBox.top).scale(_scale, _scale).translate(
1372
+ inlineBox.left - imageBox.width * _scale - padding,
1373
+ inlineBox.top + (inlineBox.height - imageBox.height * _scale) / 2
1374
+ );
1375
+ }
1376
+ pathSet.paths.push(...imagePathSet.paths.map((p) => {
1377
+ const path = p.clone();
1378
+ path.applyTransform(m);
1379
+ if (path.style.fill && path.style.fill in colormap) {
1380
+ path.style.fill = colormap[path.style.fill];
1381
+ }
1382
+ if (path.style.stroke && path.style.stroke in colormap) {
1383
+ path.style.stroke = colormap[path.style.stroke];
1384
+ }
1385
+ return path;
1386
+ }));
1390
1387
  });
1391
1388
  }
1392
1389
  });