@next2d/text 3.8.0 → 3.8.2

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.0",
3
+ "version": "3.8.2",
4
4
  "description": "Next2D Text Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,10 +24,10 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/display": "3.8.0",
28
- "@next2d/geom": "3.8.0",
29
- "@next2d/events": "3.8.0",
30
- "@next2d/ui": "3.8.0",
31
- "@next2d/cache": "3.8.0"
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"
32
32
  }
33
33
  }
@@ -1,7 +1,11 @@
1
1
  import type { TextField } from "../../TextField";
2
2
  /**
3
3
  * @description フォーカスしているテキストの位置にテキストエリアを移動します。
4
+ * 入力中の文字の真裏に input(textarea) を配置し、高さ・フォントサイズを
5
+ * canvas のテキストサイズに合わせます。
4
6
  * Move the text area to the position of the text that is focusing.
7
+ * The input(textarea) is placed directly behind the character being typed,
8
+ * and its height/font-size are matched to the canvas text size.
5
9
  *
6
10
  * @param {TextField} text_field
7
11
  * @return {void}
@@ -6,9 +6,96 @@ import { $textArea, $mainCanvasPosition } from "../../TextUtil";
6
6
  * @private
7
7
  */
8
8
  const $devicePixelRatio = window.devicePixelRatio;
9
+ /**
10
+ * @description フォーカスしているテキストのカーソル位置(ローカル座標)を算出します。
11
+ * 入力中の文字の「真裏」に input(textarea) を配置するため、カーソルのある行の
12
+ * 上端を基準に x/y を返却します。これにより IME の変換文字が canvas 側の文字に
13
+ * ちょうど重なり、他の文字に被らないようにします。カーソルが文末にある場合
14
+ * (textTable[focusIndex] が存在しない場合)も直前の文字から行と x を求めます。
15
+ * Calculate the caret position (local coordinates) of the focused text.
16
+ * Returns x/y based on the top of the caret line so that the input(textarea)
17
+ * is placed directly behind the character being typed, making the IME
18
+ * composition overlap the canvas character (and not other characters).
19
+ * Handles the end-of-text case (textTable[focusIndex] is undefined) by using
20
+ * the previous character.
21
+ *
22
+ * あわせて、input(textarea) の高さ・フォントサイズを canvas のテキストサイズへ
23
+ * 合わせるため、カーソル行の行高(lineHeight)とフォントサイズ(fontSize)も返却します。
24
+ * Also returns the caret line height and font size so that the input(textarea)
25
+ * height/font-size can match the canvas text size.
26
+ *
27
+ * @param {TextField} text_field
28
+ * @return {{ x: number, y: number, lineHeight: number, fontSize: number }}
29
+ * @method
30
+ * @private
31
+ */
32
+ const calcCaretLocalPosition = (text_field) => {
33
+ const textData = text_field.$textData;
34
+ if (!textData) {
35
+ return { "x": 0, "y": 0, "lineHeight": 0, "fontSize": 0 };
36
+ }
37
+ const textTable = textData.textTable;
38
+ // カーソル行の決定
39
+ let caretLine = 0;
40
+ const focusIndex = text_field.focusIndex;
41
+ const focusTextObject = textTable[focusIndex];
42
+ if (focusTextObject) {
43
+ // 改行・折り返しの直前にカーソルがある場合は前の行の末尾を指す
44
+ caretLine = focusTextObject.mode === "break" || focusTextObject.mode === "wrap"
45
+ ? focusTextObject.line - 1
46
+ : focusTextObject.line;
47
+ }
48
+ else {
49
+ // 文末(textTable[focusIndex] が存在しない)は直前の文字の行を採用
50
+ const lastTextObject = textTable[textTable.length - 1];
51
+ caretLine = lastTextObject ? lastTextObject.line : 0;
52
+ }
53
+ if (caretLine < 0) {
54
+ caretLine = 0;
55
+ }
56
+ // カーソル行で、カーソルより前にある文字幅の合計(= カーソルの x 位置)
57
+ let x = 0;
58
+ for (let idx = 1; idx < textTable.length; ++idx) {
59
+ if (idx >= focusIndex) {
60
+ break;
61
+ }
62
+ const textObject = textTable[idx];
63
+ if (!textObject || textObject.mode !== "text" || textObject.line !== caretLine) {
64
+ continue;
65
+ }
66
+ x += textObject.w;
67
+ }
68
+ // カーソル行の上端(= 入力文字の真裏)
69
+ let y = 0;
70
+ for (let idx = 0; idx < caretLine; ++idx) {
71
+ y += textData.heightTable[idx] || 0;
72
+ }
73
+ // カーソル行の行高(= input の高さに使用)
74
+ const lineHeight = textData.heightTable[caretLine] || 0;
75
+ // カーソル行のフォントサイズ(= input の font-size に使用)。
76
+ // カーソル位置までの最後の文字サイズを優先し、無ければ defaultTextFormat を採用する。
77
+ let fontSize = text_field.defaultTextFormat?.size || 0;
78
+ for (let idx = 1; idx < textTable.length; ++idx) {
79
+ const textObject = textTable[idx];
80
+ if (!textObject || textObject.mode !== "text" || textObject.line !== caretLine) {
81
+ continue;
82
+ }
83
+ if (textObject.textFormat?.size) {
84
+ fontSize = textObject.textFormat.size;
85
+ }
86
+ if (idx >= focusIndex) {
87
+ break;
88
+ }
89
+ }
90
+ return { x, y, lineHeight, fontSize };
91
+ };
9
92
  /**
10
93
  * @description フォーカスしているテキストの位置にテキストエリアを移動します。
94
+ * 入力中の文字の真裏に input(textarea) を配置し、高さ・フォントサイズを
95
+ * canvas のテキストサイズに合わせます。
11
96
  * Move the text area to the position of the text that is focusing.
97
+ * The input(textarea) is placed directly behind the character being typed,
98
+ * and its height/font-size are matched to the canvas text size.
12
99
  *
13
100
  * @param {TextField} text_field
14
101
  * @return {void}
@@ -16,27 +103,33 @@ const $devicePixelRatio = window.devicePixelRatio;
16
103
  * @protected
17
104
  */
18
105
  export const execute = (text_field) => {
19
- const point = text_field.localToGlobal(new Point());
20
- const textData = text_field.$textData;
21
- if (textData) {
22
- const focusTextObject = textData.textTable[text_field.focusIndex];
23
- if (focusTextObject) {
24
- for (let idx = text_field.focusIndex - 1; idx > -1; --idx) {
25
- const textObject = textData.textTable[idx];
26
- if (!textObject || textObject.line !== focusTextObject.line) {
27
- break;
28
- }
29
- point.x += textObject.w;
30
- }
31
- const line = focusTextObject.mode === "break"
32
- ? focusTextObject.line - 1
33
- : focusTextObject.line;
34
- for (let idx = 0; idx < line; ++idx) {
35
- point.y += textData.heightTable[idx];
36
- }
37
- }
106
+ const caret = calcCaretLocalPosition(text_field);
107
+ // スクロール量を反映して、表示されているカーソル位置に補正する
108
+ let localX = caret.x;
109
+ if (text_field.scrollX > 0) {
110
+ const width = text_field.width;
111
+ localX -= text_field.scrollX * (text_field.textWidth - width) / width;
112
+ }
113
+ let localY = caret.y;
114
+ if (text_field.scrollY > 0) {
115
+ const height = text_field.height;
116
+ localY -= text_field.scrollY * (text_field.textHeight - height) / height;
38
117
  }
118
+ // ローカル座標をステージ座標へ変換(TextField のスケール・回転・配置を考慮)
119
+ const point = text_field.localToGlobal(new Point(localX, localY));
120
+ // ステージ座標 → 画面(CSS)座標へ変換。
121
+ // PlayerSetCurrentMousePointService の逆変換に対応する。
39
122
  const scale = stage.rendererScale / $devicePixelRatio;
40
- $textArea.style.left = `${$mainCanvasPosition.x + point.x * scale}px`;
41
- $textArea.style.top = `${$mainCanvasPosition.y + point.y * scale}px`;
123
+ const tx = (stage.rendererWidth - stage.stageWidth * stage.rendererScale) / 2;
124
+ const ty = (stage.rendererHeight - stage.stageHeight * stage.rendererScale) / 2;
125
+ $textArea.style.left = `${$mainCanvasPosition.x + tx / $devicePixelRatio + point.x * scale}px`;
126
+ $textArea.style.top = `${$mainCanvasPosition.y + ty / $devicePixelRatio + point.y * scale}px`;
127
+ // canvas のテキストサイズに合わせて input の高さ・フォントサイズを設定する
128
+ const lineHeight = caret.lineHeight;
129
+ if (lineHeight > 0) {
130
+ const fontSize = caret.fontSize || lineHeight;
131
+ $textArea.style.height = `${lineHeight * scale}px`;
132
+ $textArea.style.fontSize = `${fontSize * scale}px`;
133
+ $textArea.style.lineHeight = `${lineHeight * scale}px`;
134
+ }
42
135
  };