@next2d/text 3.8.2 → 3.9.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/text",
3
- "version": "3.8.2",
3
+ "version": "3.9.0",
4
4
  "description": "Next2D Text Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -23,11 +23,12 @@
23
23
  "type": "git",
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
+ "sideEffects": false,
26
27
  "dependencies": {
27
- "@next2d/display": "3.8.2",
28
- "@next2d/geom": "3.8.2",
29
- "@next2d/events": "3.8.2",
30
- "@next2d/ui": "3.8.2",
31
- "@next2d/cache": "3.8.2"
28
+ "@next2d/display": "3.9.0",
29
+ "@next2d/geom": "3.9.0",
30
+ "@next2d/events": "3.9.0",
31
+ "@next2d/ui": "3.9.0",
32
+ "@next2d/cache": "3.9.0"
32
33
  }
33
34
  }
@@ -1,5 +1,5 @@
1
1
  import { execute as textFormatGenerateFontStyleService } from "../../TextFormat/service/TextFormatGenerateFontStyleService";
2
- import { $context, $setCurrentWidth } from "../../TextUtil";
2
+ import { $context, $setContextFont, $setCurrentWidth } from "../../TextUtil";
3
3
  /**
4
4
  * @description 新しい行を作成
5
5
  * Create a new line
@@ -13,7 +13,7 @@ import { $context, $setCurrentWidth } from "../../TextUtil";
13
13
  export const execute = (text_data, text_format) => {
14
14
  $setCurrentWidth(0);
15
15
  const line = text_data.lineTable.length;
16
- $context.font = textFormatGenerateFontStyleService(text_format);
16
+ $setContextFont(textFormatGenerateFontStyleService(text_format));
17
17
  const mesure = $context.measureText("");
18
18
  const object = {
19
19
  "mode": "break",
@@ -1,6 +1,6 @@
1
1
  import { execute as textFormatGetWidthMarginService } from "../../TextFormat/service/TextFormatGetWidthMarginService";
2
2
  import { execute as textFormatGenerateFontStyleService } from "../../TextFormat/service/TextFormatGenerateFontStyleService";
3
- import { $context, $setCurrentWidth, $getCurrentWidth } from "../../TextUtil";
3
+ import { $context, $setContextFont, $setCurrentWidth, $getCurrentWidth } from "../../TextUtil";
4
4
  /**
5
5
  * @description テキストを解析
6
6
  * Analyze text
@@ -16,6 +16,9 @@ import { $context, $setCurrentWidth, $getCurrentWidth } from "../../TextUtil";
16
16
  export const execute = (texts, text_format, text_data, options) => {
17
17
  let line = text_data.lineTable.length - 1;
18
18
  const maxWidth = options.width - textFormatGetWidthMarginService(text_format) - 4;
19
+ // 同一TextFormatが連続する間はフォントスタイル文字列の再生成をスキップする
20
+ let cachedTextFormat = null;
21
+ let cachedFontStyle = "";
19
22
  for (let idx = 0; idx < texts.length; ++idx) {
20
23
  const textFormat = options.textFormats
21
24
  ? options.textFormats.shift()
@@ -31,7 +34,11 @@ export const execute = (texts, text_format, text_data, options) => {
31
34
  "line": line,
32
35
  "textFormat": textFormat.toObject()
33
36
  };
34
- $context.font = textFormatGenerateFontStyleService(textFormat);
37
+ if (cachedTextFormat !== textFormat) {
38
+ cachedTextFormat = textFormat;
39
+ cachedFontStyle = textFormatGenerateFontStyleService(textFormat);
40
+ }
41
+ $setContextFont(cachedFontStyle);
35
42
  const mesure = $context.measureText(text || "");
36
43
  const width = textFormat.letterSpacing
37
44
  ? mesure.width + textFormat.letterSpacing
package/src/TextUtil.d.ts CHANGED
@@ -46,6 +46,21 @@ export declare const $getCurrentWidth: () => number;
46
46
  * @protected
47
47
  */
48
48
  export declare const $setCurrentWidth: (width: number) => void;
49
+ /**
50
+ * @description $context.fontの設定をラップし、同一スタイルの再代入をスキップする。
51
+ * font代入はブラウザ内部でフォント文字列のパースを伴う重い操作のため、
52
+ * 1文字ごとのレイアウト処理で同一フォーマットが続く場合の負荷を除去する。
53
+ * Wraps assignment to $context.font and skips redundant assignment
54
+ * of the same style. Assigning font triggers font-string parsing
55
+ * inside the browser, so this removes the cost for consecutive
56
+ * characters that share the same format.
57
+ *
58
+ * @param {string} font_style
59
+ * @return {void}
60
+ * @method
61
+ * @protected
62
+ */
63
+ export declare const $setContextFont: (font_style: string) => void;
49
64
  /**
50
65
  * @description カラー文字列を数値に変換
51
66
  * Convert color string to number
package/src/TextUtil.js CHANGED
@@ -82,6 +82,44 @@ export const $getCurrentWidth = () => {
82
82
  export const $setCurrentWidth = (width) => {
83
83
  $currentWidth = width;
84
84
  };
85
+ /**
86
+ * @description 最後に$contextへ設定したフォントスタイル文字列
87
+ * The font style string last assigned to $context
88
+ *
89
+ * @type {string}
90
+ * @private
91
+ */
92
+ let $currentFontStyle = "";
93
+ /**
94
+ * @description $context.fontの設定をラップし、同一スタイルの再代入をスキップする。
95
+ * font代入はブラウザ内部でフォント文字列のパースを伴う重い操作のため、
96
+ * 1文字ごとのレイアウト処理で同一フォーマットが続く場合の負荷を除去する。
97
+ * Wraps assignment to $context.font and skips redundant assignment
98
+ * of the same style. Assigning font triggers font-string parsing
99
+ * inside the browser, so this removes the cost for consecutive
100
+ * characters that share the same format.
101
+ *
102
+ * @param {string} font_style
103
+ * @return {void}
104
+ * @method
105
+ * @protected
106
+ */
107
+ export const $setContextFont = (font_style) => {
108
+ if ($currentFontStyle !== font_style) {
109
+ $currentFontStyle = font_style;
110
+ $context.font = font_style;
111
+ }
112
+ };
113
+ /**
114
+ * @description カラー文字列→数値変換のキャッシュ。
115
+ * canvasのfillStyle往復はコストが高いため、一意なカラー文字列ごとに1度だけ変換する。
116
+ * Cache for color-string-to-number conversion. The canvas fillStyle
117
+ * round trip is expensive, so convert each unique color string once.
118
+ *
119
+ * @type {Map<string, number>}
120
+ * @private
121
+ */
122
+ const $colorStringCache = new Map();
85
123
  /**
86
124
  * @description カラー文字列を数値に変換
87
125
  * Convert color string to number
@@ -95,8 +133,16 @@ export const $convertColorStringToNumber = (value) => {
95
133
  if (!$context) {
96
134
  return 0;
97
135
  }
136
+ const cachedColor = $colorStringCache.get(value);
137
+ if (cachedColor !== undefined) {
138
+ return cachedColor;
139
+ }
98
140
  $context.fillStyle = value;
99
- return +`0x${$context.fillStyle.slice(1)}`;
141
+ const color = +`0x${$context.fillStyle.slice(1)}`;
142
+ if (256 > $colorStringCache.size) {
143
+ $colorStringCache.set(value, color);
144
+ }
145
+ return color;
100
146
  };
101
147
  /**
102
148
  * @description カラー文字列を数値に変換