@awayjs/scene 0.13.265 → 0.13.267

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.
Files changed (268) hide show
  1. package/.eslintrc +100 -100
  2. package/README.md +61 -61
  3. package/bundle/awayjs-scene.umd.js +2 -2
  4. package/bundle/awayjs-scene.umd.js.map +1 -1
  5. package/copyVersionToIndex.js +23 -23
  6. package/dist/index.d.ts +76 -75
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +67 -66
  9. package/dist/lib/Scene.d.ts +84 -84
  10. package/dist/lib/Scene.js +288 -288
  11. package/dist/lib/Settings.d.ts +16 -16
  12. package/dist/lib/Settings.js +60 -60
  13. package/dist/lib/adapters/IDisplayObjectAdapter.d.ts +13 -13
  14. package/dist/lib/adapters/IDisplayObjectAdapter.js +1 -1
  15. package/dist/lib/adapters/IFilter.d.ts +24 -24
  16. package/dist/lib/adapters/IFilter.js +1 -1
  17. package/dist/lib/adapters/IMovieClipAdapter.d.ts +14 -14
  18. package/dist/lib/adapters/IMovieClipAdapter.js +1 -1
  19. package/dist/lib/adapters/ITextfieldAdapter.d.ts +4 -4
  20. package/dist/lib/adapters/ITextfieldAdapter.js +1 -1
  21. package/dist/lib/base/IBitmapDrawable.d.ts +11 -11
  22. package/dist/lib/base/IBitmapDrawable.js +1 -1
  23. package/dist/lib/base/IFrameLabel.d.ts +4 -4
  24. package/dist/lib/base/IFrameLabel.js +1 -1
  25. package/dist/lib/base/IFrameScript.d.ts +5 -5
  26. package/dist/lib/base/IFrameScript.js +1 -1
  27. package/dist/lib/base/MouseButtons.d.ts +26 -22
  28. package/dist/lib/base/MouseButtons.d.ts.map +1 -1
  29. package/dist/lib/base/MouseButtons.js +1 -1
  30. package/dist/lib/base/Timeline.d.ts +148 -148
  31. package/dist/lib/base/Timeline.js +535 -535
  32. package/dist/lib/base/TimelineActionType.d.ts +18 -18
  33. package/dist/lib/base/TimelineActionType.js +1 -1
  34. package/dist/lib/controllers/ControllerBase.d.ts +14 -14
  35. package/dist/lib/controllers/ControllerBase.js +60 -60
  36. package/dist/lib/controllers/FirstPersonController.d.ts +78 -78
  37. package/dist/lib/controllers/FirstPersonController.js +228 -228
  38. package/dist/lib/controllers/FollowController.d.ts +12 -12
  39. package/dist/lib/controllers/FollowController.js +27 -27
  40. package/dist/lib/controllers/HoverController.d.ts +105 -105
  41. package/dist/lib/controllers/HoverController.js +302 -302
  42. package/dist/lib/controllers/LookAtController.d.ts +14 -14
  43. package/dist/lib/controllers/LookAtController.js +68 -68
  44. package/dist/lib/controllers/SpringController.d.ts +38 -38
  45. package/dist/lib/controllers/SpringController.js +59 -59
  46. package/dist/lib/display/Billboard.d.ts +147 -147
  47. package/dist/lib/display/Billboard.js +426 -426
  48. package/dist/lib/display/Camera.d.ts +40 -40
  49. package/dist/lib/display/Camera.js +90 -90
  50. package/dist/lib/display/DisplayObject.d.ts +938 -938
  51. package/dist/lib/display/DisplayObject.js +1641 -1641
  52. package/dist/lib/display/DisplayObjectContainer.d.ts +325 -325
  53. package/dist/lib/display/DisplayObjectContainer.js +480 -480
  54. package/dist/lib/display/LineSegment.d.ts +84 -84
  55. package/dist/lib/display/LineSegment.js +215 -215
  56. package/dist/lib/display/LoaderContainer.d.ts +504 -504
  57. package/dist/lib/display/LoaderContainer.js +603 -603
  58. package/dist/lib/display/MorphSprite.d.ts +20 -20
  59. package/dist/lib/display/MorphSprite.js +254 -254
  60. package/dist/lib/display/MovieClip.d.ts +152 -152
  61. package/dist/lib/display/MovieClip.js +735 -735
  62. package/dist/lib/display/SimpleButton.d.ts +158 -158
  63. package/dist/lib/display/SimpleButton.js +228 -228
  64. package/dist/lib/display/Skybox.d.ts +156 -156
  65. package/dist/lib/display/Skybox.js +309 -309
  66. package/dist/lib/display/Sprite.d.ts +70 -70
  67. package/dist/lib/display/Sprite.js +144 -144
  68. package/dist/lib/display/TextField.d.ts +1109 -1109
  69. package/dist/lib/display/TextField.js +3109 -3109
  70. package/dist/lib/display/TextSprite.d.ts +10 -10
  71. package/dist/lib/display/TextSprite.js +25 -25
  72. package/dist/lib/elements/SkyboxElements.d.ts +29 -29
  73. package/dist/lib/elements/SkyboxElements.js +127 -127
  74. package/dist/lib/errors/CastError.d.ts +4 -4
  75. package/dist/lib/errors/CastError.js +10 -10
  76. package/dist/lib/events/CameraEvent.d.ts +16 -16
  77. package/dist/lib/events/CameraEvent.js +30 -30
  78. package/dist/lib/events/FocusEvent.d.ts +42 -42
  79. package/dist/lib/events/FocusEvent.js +43 -43
  80. package/dist/lib/events/KeyboardEvent.d.ts +21 -21
  81. package/dist/lib/events/KeyboardEvent.js +43 -43
  82. package/dist/lib/events/MouseEvent.d.ts +91 -184
  83. package/dist/lib/events/MouseEvent.d.ts.map +1 -1
  84. package/dist/lib/events/MouseEvent.js +122 -191
  85. package/dist/lib/events/PointerEvent.d.ts +94 -0
  86. package/dist/lib/events/PointerEvent.d.ts.map +1 -0
  87. package/dist/lib/events/PointerEvent.js +73 -0
  88. package/dist/lib/events/ResizeEvent.d.ts +15 -15
  89. package/dist/lib/events/ResizeEvent.js +38 -38
  90. package/dist/lib/events/TextfieldEvent.d.ts +19 -19
  91. package/dist/lib/events/TextfieldEvent.js +28 -28
  92. package/dist/lib/events/TouchEvent.d.ts +55 -0
  93. package/dist/lib/events/TouchEvent.d.ts.map +1 -0
  94. package/dist/lib/events/TouchEvent.js +89 -0
  95. package/dist/lib/factories/DefaultSceneGraphFactory.d.ts +26 -26
  96. package/dist/lib/factories/DefaultSceneGraphFactory.js +54 -54
  97. package/dist/lib/factories/ISceneGraphFactory.d.ts +20 -20
  98. package/dist/lib/factories/ISceneGraphFactory.js +1 -1
  99. package/dist/lib/image/SceneImage2D.d.ts +236 -236
  100. package/dist/lib/image/SceneImage2D.js +687 -687
  101. package/dist/lib/managers/DefaultFontManager.d.ts +14 -14
  102. package/dist/lib/managers/DefaultFontManager.js +101 -101
  103. package/dist/lib/managers/DeviceFontManager.d.ts +8 -8
  104. package/dist/lib/managers/DeviceFontManager.js +44 -44
  105. package/dist/lib/managers/FrameScriptManager.d.ts +36 -36
  106. package/dist/lib/managers/FrameScriptManager.js +377 -377
  107. package/dist/lib/managers/IInputRecorder.d.ts +3 -3
  108. package/dist/lib/managers/IInputRecorder.js +1 -1
  109. package/dist/lib/managers/MouseManager.d.ts +76 -89
  110. package/dist/lib/managers/MouseManager.d.ts.map +1 -1
  111. package/dist/lib/managers/MouseManager.js +812 -761
  112. package/dist/lib/prefabs/PrefabBase.d.ts +19 -19
  113. package/dist/lib/prefabs/PrefabBase.js +42 -42
  114. package/dist/lib/prefabs/PrimitiveCapsulePrefab.d.ts +56 -56
  115. package/dist/lib/prefabs/PrimitiveCapsulePrefab.js +269 -269
  116. package/dist/lib/prefabs/PrimitiveConePrefab.d.ts +21 -21
  117. package/dist/lib/prefabs/PrimitiveConePrefab.js +43 -43
  118. package/dist/lib/prefabs/PrimitiveCubePrefab.d.ts +74 -74
  119. package/dist/lib/prefabs/PrimitiveCubePrefab.js +545 -545
  120. package/dist/lib/prefabs/PrimitiveCylinderPrefab.d.ts +79 -79
  121. package/dist/lib/prefabs/PrimitiveCylinderPrefab.js +584 -584
  122. package/dist/lib/prefabs/PrimitivePlanePrefab.d.ts +65 -65
  123. package/dist/lib/prefabs/PrimitivePlanePrefab.js +300 -300
  124. package/dist/lib/prefabs/PrimitivePolygonPrefab.d.ts +29 -29
  125. package/dist/lib/prefabs/PrimitivePolygonPrefab.js +64 -64
  126. package/dist/lib/prefabs/PrimitivePrefabBase.d.ts +67 -67
  127. package/dist/lib/prefabs/PrimitivePrefabBase.js +158 -158
  128. package/dist/lib/prefabs/PrimitiveSpherePrefab.d.ts +48 -48
  129. package/dist/lib/prefabs/PrimitiveSpherePrefab.js +294 -294
  130. package/dist/lib/prefabs/PrimitiveTorusPrefab.d.ts +55 -55
  131. package/dist/lib/prefabs/PrimitiveTorusPrefab.js +260 -260
  132. package/dist/lib/text/AntiAliasType.d.ts +24 -24
  133. package/dist/lib/text/AntiAliasType.js +28 -28
  134. package/dist/lib/text/BitmapFontChar.d.ts +39 -39
  135. package/dist/lib/text/BitmapFontChar.js +42 -42
  136. package/dist/lib/text/BitmapFontTable.d.ts +80 -80
  137. package/dist/lib/text/BitmapFontTable.js +203 -203
  138. package/dist/lib/text/FNTGenerator.d.ts +10 -10
  139. package/dist/lib/text/FNTGenerator.js +74 -74
  140. package/dist/lib/text/FNTGeneratorCanvas.d.ts +22 -22
  141. package/dist/lib/text/FNTGeneratorCanvas.js +205 -205
  142. package/dist/lib/text/FNTGeneratrorBase.d.ts +7 -7
  143. package/dist/lib/text/FNTGeneratrorBase.js +7 -7
  144. package/dist/lib/text/Font.d.ts +38 -38
  145. package/dist/lib/text/Font.js +109 -109
  146. package/dist/lib/text/FontLookUpMode.d.ts +5 -5
  147. package/dist/lib/text/FontLookUpMode.js +7 -7
  148. package/dist/lib/text/FontStyleName.d.ts +6 -6
  149. package/dist/lib/text/FontStyleName.js +7 -7
  150. package/dist/lib/text/GridFitType.d.ts +31 -31
  151. package/dist/lib/text/GridFitType.js +35 -35
  152. package/dist/lib/text/HTMLTextProcessor.d.ts +8 -8
  153. package/dist/lib/text/HTMLTextProcessor.js +423 -423
  154. package/dist/lib/text/IFontTable.d.ts +16 -16
  155. package/dist/lib/text/IFontTable.js +1 -1
  156. package/dist/lib/text/TesselatedFontChar.d.ts +58 -58
  157. package/dist/lib/text/TesselatedFontChar.js +66 -66
  158. package/dist/lib/text/TesselatedFontTable.d.ts +95 -95
  159. package/dist/lib/text/TesselatedFontTable.js +992 -992
  160. package/dist/lib/text/TextFieldAutoSize.d.ts +28 -28
  161. package/dist/lib/text/TextFieldAutoSize.js +32 -32
  162. package/dist/lib/text/TextFieldType.d.ts +20 -20
  163. package/dist/lib/text/TextFieldType.js +24 -24
  164. package/dist/lib/text/TextFormat.d.ts +273 -273
  165. package/dist/lib/text/TextFormat.js +440 -440
  166. package/dist/lib/text/TextFormatAlign.d.ts +26 -26
  167. package/dist/lib/text/TextFormatAlign.js +30 -30
  168. package/dist/lib/text/TextInteractionMode.d.ts +21 -21
  169. package/dist/lib/text/TextInteractionMode.js +25 -25
  170. package/dist/lib/text/TextLineMetrics.d.ts +61 -61
  171. package/dist/lib/text/TextLineMetrics.js +42 -42
  172. package/dist/lib/text/TextShape.d.ts +20 -20
  173. package/dist/lib/text/TextShape.js +75 -75
  174. package/dist/lib/tools/Merge.d.ts +81 -81
  175. package/dist/lib/tools/Merge.js +266 -266
  176. package/dist/lib/utils/Cast.d.ts +20 -20
  177. package/dist/lib/utils/Cast.js +271 -271
  178. package/index.ts +92 -91
  179. package/lib/Scene.ts +343 -343
  180. package/lib/Settings.ts +83 -83
  181. package/lib/adapters/IDisplayObjectAdapter.ts +23 -23
  182. package/lib/adapters/IFilter.ts +25 -25
  183. package/lib/adapters/IMovieClipAdapter.ts +24 -24
  184. package/lib/adapters/ITextfieldAdapter.ts +6 -6
  185. package/lib/base/IBitmapDrawable.ts +13 -13
  186. package/lib/base/IFrameLabel.ts +3 -3
  187. package/lib/base/IFrameScript.ts +4 -4
  188. package/lib/base/MouseButtons.ts +31 -26
  189. package/lib/base/Timeline.ts +727 -727
  190. package/lib/base/TimelineActionType.ts +18 -18
  191. package/lib/controllers/ControllerBase.ts +66 -66
  192. package/lib/controllers/FirstPersonController.ts +241 -241
  193. package/lib/controllers/FollowController.ts +22 -22
  194. package/lib/controllers/HoverController.ts +304 -304
  195. package/lib/controllers/LookAtController.ts +75 -75
  196. package/lib/controllers/SpringController.ts +91 -91
  197. package/lib/display/Billboard.ts +504 -504
  198. package/lib/display/Camera.ts +89 -89
  199. package/lib/display/DisplayObject.ts +1893 -1893
  200. package/lib/display/DisplayObjectContainer.ts +538 -538
  201. package/lib/display/LineSegment.ts +248 -248
  202. package/lib/display/LoaderContainer.ts +651 -651
  203. package/lib/display/MorphSprite.ts +347 -347
  204. package/lib/display/MovieClip.ts +879 -879
  205. package/lib/display/SimpleButton.ts +224 -224
  206. package/lib/display/Skybox.ts +379 -379
  207. package/lib/display/Sprite.ts +165 -165
  208. package/lib/display/TextField.ts +3958 -3958
  209. package/lib/display/TextSprite.ts +22 -22
  210. package/lib/elements/SkyboxElements.ts +141 -141
  211. package/lib/errors/CastError.ts +6 -6
  212. package/lib/events/CameraEvent.ts +29 -29
  213. package/lib/events/FocusEvent.ts +69 -69
  214. package/lib/events/KeyboardEvent.ts +40 -40
  215. package/lib/events/MouseEvent.ts +147 -287
  216. package/lib/events/PointerEvent.ts +146 -0
  217. package/lib/events/ResizeEvent.ts +31 -31
  218. package/lib/events/TextfieldEvent.ts +28 -28
  219. package/lib/events/TouchEvent.ts +107 -0
  220. package/lib/factories/DefaultSceneGraphFactory.ts +56 -56
  221. package/lib/factories/ISceneGraphFactory.ts +28 -28
  222. package/lib/image/SceneImage2D.ts +1020 -1020
  223. package/lib/managers/DefaultFontManager.ts +124 -124
  224. package/lib/managers/DeviceFontManager.ts +55 -55
  225. package/lib/managers/FrameScriptManager.ts +431 -431
  226. package/lib/managers/IInputRecorder.ts +2 -2
  227. package/lib/managers/MouseManager.ts +948 -884
  228. package/lib/prefabs/PrefabBase.ts +47 -47
  229. package/lib/prefabs/PrimitiveCapsulePrefab.ts +286 -286
  230. package/lib/prefabs/PrimitiveConePrefab.ts +37 -37
  231. package/lib/prefabs/PrimitiveCubePrefab.ts +630 -630
  232. package/lib/prefabs/PrimitiveCylinderPrefab.ts +657 -657
  233. package/lib/prefabs/PrimitivePlanePrefab.ts +352 -352
  234. package/lib/prefabs/PrimitivePolygonPrefab.ts +53 -53
  235. package/lib/prefabs/PrimitivePrefabBase.ts +177 -177
  236. package/lib/prefabs/PrimitiveSpherePrefab.ts +336 -336
  237. package/lib/prefabs/PrimitiveTorusPrefab.ts +282 -282
  238. package/lib/text/AntiAliasType.ts +24 -24
  239. package/lib/text/BitmapFontChar.ts +53 -53
  240. package/lib/text/BitmapFontTable.ts +232 -232
  241. package/lib/text/FNTGenerator.ts +100 -100
  242. package/lib/text/FNTGeneratorCanvas.ts +287 -287
  243. package/lib/text/FNTGeneratrorBase.ts +10 -10
  244. package/lib/text/Font.ts +114 -114
  245. package/lib/text/FontLookUpMode.ts +5 -5
  246. package/lib/text/FontStyleName.ts +6 -6
  247. package/lib/text/GridFitType.ts +32 -32
  248. package/lib/text/HTMLTextProcessor.ts +418 -418
  249. package/lib/text/IFontTable.ts +17 -17
  250. package/lib/text/TesselatedFontChar.ts +80 -80
  251. package/lib/text/TesselatedFontTable.ts +1221 -1221
  252. package/lib/text/TextFieldAutoSize.ts +30 -30
  253. package/lib/text/TextFieldType.ts +21 -21
  254. package/lib/text/TextFormat.ts +601 -601
  255. package/lib/text/TextFormatAlign.ts +28 -28
  256. package/lib/text/TextInteractionMode.ts +21 -21
  257. package/lib/text/TextLineMetrics.ts +74 -74
  258. package/lib/text/TextShape.ts +90 -90
  259. package/lib/tools/Merge.ts +311 -311
  260. package/lib/utils/Cast.ts +304 -304
  261. package/package.json +74 -74
  262. package/rollup.config.js +33 -33
  263. package/scripts/build.sh +2 -2
  264. package/scripts/gendocs.sh +14 -14
  265. package/scripts/genexamples.sh +14 -14
  266. package/scripts/genfull.sh +14 -14
  267. package/system.config.js +51 -51
  268. package/tsconfig.json +18 -18
@@ -1,1222 +1,1222 @@
1
- import { Matrix, AssetBase, Point, Rectangle, ColorTransform, ColorUtils } from '@awayjs/core';
2
-
3
- import { ImageSampler, AttributesBuffer, Float2Attributes, AttributesView, BitmapImage2D, Stage } from '@awayjs/stage';
4
-
5
- import { Style, TriangleElements } from '@awayjs/renderer';
6
-
7
- import { GraphicsPath, Shape, GraphicsFactoryFills, GraphicsFactoryHelper, MaterialManager } from '@awayjs/graphics';
8
-
9
- import { MaterialBase, MethodMaterial } from '@awayjs/materials';
10
-
11
- import { TesselatedFontChar } from './TesselatedFontChar';
12
- import { IFontTable } from './IFontTable';
13
- import { TextFormat } from './TextFormat';
14
- import { TextShape } from './TextShape';
15
-
16
- import { TextField } from '../display/TextField';
17
- import { Settings } from '../Settings';
18
- import { FNTGeneratorCanvas } from './FNTGeneratorCanvas';
19
-
20
- const ONCE_EMIT_TABLE: StringMap<boolean> = Object.create(null);
21
- function once(obj: any, key = '') {
22
- const has = ONCE_EMIT_TABLE[obj._id + key];
23
-
24
- ONCE_EMIT_TABLE[obj._id + key] = true;
25
- return !has;
26
- }
27
-
28
- interface ICharEntry {
29
- x: number;
30
- y: number;
31
- char: TesselatedFontChar;
32
- selected?: boolean;
33
- shape?: TextShape;
34
- }
35
-
36
- /**
37
- * GraphicBase wraps a TriangleElements as a scene graph instantiation. A GraphicBase is owned by a Sprite object.
38
- *
39
- *
40
- * @see away.base.TriangleElements
41
- * @see away.entities.Sprite
42
- *
43
- * @class away.base.GraphicBase
44
- */
45
- export class TesselatedFontTable extends AssetBase implements IFontTable {
46
- public static assetType: string = '[asset TesselatedFontTable]';
47
- public static DEFAULT_SPACE = 14;
48
-
49
- public font: any;
50
-
51
- /*internal*/ _font_chars_dic: StringMap<TesselatedFontChar>;
52
- /*internal*/ _current_size: number;
53
- /*internal*/ _size_multiply: number;
54
-
55
- private _font_chars: Array<TesselatedFontChar>;
56
- private _font_em_size: number;
57
-
58
- private _whitespace_width: number;
59
-
60
- private _offset_x: number;
61
- private _offset_y: number;
62
- private _ascent: number;
63
- private _descent: number;
64
- private _charDictDirty: Boolean;
65
- private _usesCurves: boolean;
66
- private _opentype_font: any;
67
- private _glyphIdxToChar: any;
68
-
69
- private _fntSizeLimit: number=-1;
70
- private _fnt_channels: BitmapImage2D[];
71
-
72
- /**
73
- * Creates a new TesselatedFont object
74
- * If a opentype_font object is passed, the chars will get tessellated whenever requested.
75
- * If no opentype font object is passed, it is expected that tesselated chars
76
- */
77
- constructor(opentype_font: any = null) {
78
- super();
79
- this._font_chars = [];
80
- this._font_chars_dic = Object.create(null);
81
- this._current_size = 0;
82
- this._size_multiply = 0;
83
- this._ascent = 0;
84
- this._descent = 0;
85
- this._usesCurves = false;
86
- this._glyphIdxToChar = {};
87
- this._fnt_channels = [];
88
-
89
- // default size
90
- this._font_em_size = 32;
91
-
92
- if (opentype_font) {
93
- this._opentype_font = opentype_font;
94
- /*
95
- console.log("head.yMax ",opentype_font);
96
-
97
- console.log("head.yMax "+opentype_font.tables.head.yMax);
98
- console.log("head.yMin "+opentype_font.tables.head.yMin);
99
- console.log("font.numGlyphs "+opentype_font.numGlyphs);
100
- console.log('Ascender', opentype_font.tables.hhea.ascender);
101
- console.log('Descender', opentype_font.tables.hhea.descender);
102
- console.log('Typo Ascender', opentype_font.tables.os2.sTypoAscender);
103
- console.log('Typo Descender', opentype_font.tables.os2.sTypoDescender);
104
- */
105
- this._font_em_size = 72;//2048;
106
- this._ascent = opentype_font.tables.hhea.ascender / (this._opentype_font.unitsPerEm / 72);
107
- this._descent = -2 + opentype_font.tables.hhea.descender / (this._opentype_font.unitsPerEm / 72);
108
- this._current_size = 0;
109
- this._size_multiply = 0;
110
-
111
- const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(32));
112
- if (thisGlyph) {
113
- //this._whitespace_width = thisGlyph.advanceWidth / (this._opentype_font.unitsPerEm / 72);
114
- }
115
- return;
116
- }
117
- }
118
-
119
- public addFNTChannel(mat: BitmapImage2D) {
120
- this._fnt_channels.push(mat);
121
- }
122
-
123
- public getGlyphCount(): number {
124
- return this._font_chars.length;
125
- }
126
-
127
- public generateFNTData (stage: Stage): void {
128
- // already exist
129
- if (this._fntSizeLimit > 0) {
130
- return;
131
- }
132
-
133
- const generator = new FNTGeneratorCanvas(stage, false);
134
- const bitmaps = generator.generate(this.font, this._font_em_size, this._current_size, 4);
135
-
136
- if (bitmaps && bitmaps.length > 0) {
137
- /**
138
- * Random value, anyway we render real font dimension ;)
139
- */
140
- this._fntSizeLimit = this._current_size * 4;
141
- }
142
- }
143
-
144
- public generateFNTTextures(padding: number, fontSize: number, texSize: number): Shape[] {
145
- console.log('generateFNTTextures');
146
- if (fontSize) {
147
- this._fntSizeLimit = fontSize * 0.5; //why half?
148
- this.initFontSize(fontSize);
149
-
150
- }
151
-
152
- if (this._opentype_font) {
153
- // if this was loaded from opentype,
154
- // we make sure all glyphs are tesselated first
155
- for (let g: number = 0; g < this._opentype_font.glyphs.length;g++) {
156
- const thisGlyph = this._opentype_font.charToGlyph(
157
- String.fromCharCode(this._opentype_font.glyphs.glyphs[g].unicode));
158
- if (thisGlyph) {
159
- const thisPath = thisGlyph.getPath();
160
- const awayPath: GraphicsPath = new GraphicsPath();
161
- let i = 0;
162
- const len = thisPath.commands.length;
163
- let startx: number = 0;
164
- let starty: number = 0;
165
- const y_offset = this._ascent;
166
- for (i = 0;i < len;i++) {
167
- const cmd = thisPath.commands[i];
168
- //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
169
- if (cmd.type === 'M') {
170
- awayPath.moveTo(cmd.x, cmd.y + y_offset);
171
- startx = cmd.x;
172
- starty = cmd.y + y_offset;
173
- } else if (cmd.type === 'L') {
174
- awayPath.lineTo(cmd.x, cmd.y + y_offset);
175
- } else if (cmd.type === 'Q') {
176
- awayPath.curveTo(cmd.x1, cmd.y1 + y_offset, cmd.x, cmd.y + y_offset);
177
- } else if (cmd.type === 'C') {
178
- awayPath.cubicCurveTo(cmd.x1, cmd.y1 + y_offset, cmd.x2,
179
- cmd.y2 + y_offset, cmd.x, cmd.y + y_offset);
180
- } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
181
- }
182
- const t_font_char: TesselatedFontChar = new TesselatedFontChar(null, null, awayPath);
183
- t_font_char.char_width = (thisGlyph.advanceWidth * (1 / thisGlyph.path.unitsPerEm * 72));
184
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(awayPath, true);
185
- if (!t_font_char.fill_data) {
186
- console.log('error tesselating opentype glyph', this._opentype_font.glyphs.glyphs[g]);
187
- //return null;
188
- } else {
189
- this._font_chars.push(t_font_char);
190
- this._font_chars_dic[this._opentype_font.glyphs.glyphs[g].unicode] = t_font_char;
191
-
192
- }
193
- //console.log("unicode:",this._opentype_font.glyphs.glyphs[g].unicode);
194
- //console.log("name:",this._opentype_font.glyphs.glyphs[g].name);
195
-
196
- } else {
197
- console.log('no char found for', this._opentype_font.glyphs.glyphs[g]);
198
- }
199
- }
200
- }
201
- let char_vertices: AttributesBuffer;
202
- let x: number = 0;
203
- let y: number = 0;
204
- let buffer: Float32Array;
205
- let v: number;
206
- const glyph_verts: number[][] = [[]];
207
- let channel_idx: number = 0;
208
- let maxSize = 0;
209
- const allChars: any[] = [];
210
-
211
- let maxy: number = Number.MIN_VALUE;
212
- let maxx: number = Number.MIN_VALUE;
213
- let miny: number = Number.MAX_VALUE;
214
- let minx: number = Number.MAX_VALUE;
215
- let tmpx: number = 0;
216
- let tmpy: number = 0;
217
-
218
- // step1: loop over all chars, get their bounds and sort them by there height
219
- // done without applying any font-scale to the glyphdata
220
- let allAreas: number = 0;
221
- for (let i: number = 0; i < this._font_chars.length;i++) {
222
- char_vertices = this._font_chars[i].fill_data;
223
- //console.log("got the glyph from opentype", this._opentype_font.glyphs.glyphs[g]);
224
- buffer = new Float32Array(char_vertices.buffer);
225
- maxy = Number.MIN_VALUE;
226
- maxx = Number.MIN_VALUE;
227
- miny = Number.MAX_VALUE;
228
- minx = Number.MAX_VALUE;
229
- tmpx = 0;
230
- tmpy = 0;
231
- for (v = 0; v < char_vertices.count; v++) {
232
- tmpx = (buffer[v * 2]);
233
- tmpy = (buffer[v * 2 + 1]);
234
- if (tmpx < minx) minx = tmpx;
235
- if (tmpx > maxx) maxx = tmpx;
236
- if (tmpy < miny) miny = tmpy;
237
- if (tmpy > maxy) maxy = tmpy;
238
- }
239
- this._font_chars[i].fnt_rect = new Rectangle(
240
- (minx) / (this._font_chars[i].char_width),
241
- (miny) / (this._font_em_size),
242
- (maxx - minx) / (this._font_chars[i].char_width),
243
- (maxy - miny) / (this._font_em_size));
244
-
245
- allAreas += (maxx - minx + padding + padding) * (maxy - miny + padding + padding);
246
- allChars[allChars.length] = {
247
- idx:i,
248
- minx:minx,
249
- miny:miny,
250
- width:maxx - minx,
251
- height:maxy - miny
252
- };
253
- }
254
- allChars.sort(function(a,b) {
255
- return a.height > b.height ? -1 : 1;
256
- });
257
-
258
- // now allChars contains all glyphs sorted by height
259
-
260
- let curHeight: number = 0;
261
- maxSize = texSize;
262
- let size_multiply: number = 1;
263
- if (fontSize) {
264
- this.initFontSize(fontSize);
265
- size_multiply = this._size_multiply;
266
- } else {
267
- // figure out the optiomal fontSize so that the glyphs fill the texture
268
-
269
- // imagine the spaces needed by all glyphs would be a perfect quat
270
- // compare the width of that quat with the available width to have a first guess at fontSize
271
-
272
- // make the size slightly bigger as the estimate and check if all glyphs will fit
273
- // if they do not fit, slightly reduce the scale.
274
- // repeat until all glyphs fit
275
-
276
- size_multiply = maxSize / (Math.sqrt(allAreas)) * 1.2;
277
- let invalid: boolean = true;
278
- let curCharHeight: number = 0;
279
- while (invalid) {
280
- invalid = false;
281
- x = 0;
282
- y = padding;
283
- for (let i: number = 0; i < allChars.length;i++) {
284
- curCharHeight = allChars[i].height * size_multiply;
285
- tmpx = 0;
286
- tmpy = 0;
287
- x += padding;
288
- if (x + allChars[i].width * size_multiply + padding >= maxSize) {
289
- x = padding;
290
- y += curHeight + padding * 2;
291
- curHeight = 0;
292
- }
293
- if (curCharHeight > curHeight) {
294
- curHeight = curCharHeight;
295
- }
296
- if (y + curCharHeight + padding * 2 >= maxSize) {
297
- invalid = true;
298
- size_multiply = size_multiply * 0.995;
299
- break;
300
- }
301
- x += (allChars[i].width) * size_multiply + padding;
302
- }
303
- }
304
- }
305
-
306
- this._fntSizeLimit = size_multiply * this._font_em_size * 0.5; //why half?
307
-
308
- // collect the glyph-data:
309
-
310
- let font_char: TesselatedFontChar;
311
- x = 0;
312
- y = padding;
313
- for (let i: number = 0; i < allChars.length;i++) {
314
-
315
- font_char = this._font_chars[allChars[i].idx];
316
- char_vertices = font_char.fill_data;
317
- buffer = new Float32Array(char_vertices.buffer);
318
- minx = allChars[i].minx;
319
- maxx = Number.MIN_VALUE;
320
- miny = allChars[i].miny;
321
- maxy = Number.MIN_VALUE;
322
- tmpx = 0;
323
- tmpy = 0;
324
- x += padding;
325
-
326
- if (x + allChars[i].width * size_multiply + padding >= maxSize) {
327
- x = padding;
328
- y += curHeight + padding * 2;
329
- curHeight = 0;
330
- }
331
- allChars[i].height *= size_multiply;
332
- if (allChars[i].height > curHeight) {
333
- curHeight = allChars[i].height;
334
- }
335
- if (y + curHeight + padding * 2 >= maxSize) {
336
- x = padding;
337
- y = padding;
338
- curHeight = 0;
339
- channel_idx++;
340
- glyph_verts[channel_idx] = [];
341
- }
342
- font_char.fnt_channel = channel_idx;
343
- for (v = 0; v < char_vertices.count; v++) {
344
- tmpx = ((buffer[v * 2] - minx) * size_multiply) + x;
345
- tmpy = ((buffer[v * 2 + 1] - miny) * size_multiply) + y;
346
- glyph_verts[channel_idx][glyph_verts[channel_idx].length] = tmpx;
347
- glyph_verts[channel_idx][glyph_verts[channel_idx].length] = tmpy;
348
- if (tmpx > maxx) {
349
- maxx = tmpx;
350
- }
351
- if (tmpy > maxy) {
352
- maxy = tmpy;
353
- }
354
- }
355
- font_char.fnt_uv = new Rectangle(
356
- x / maxSize, 1 - (y / maxSize),
357
- (maxx - x) / maxSize, ((maxy - y) / maxSize));
358
-
359
- //this._drawDebugRect(glyph_verts[channel_idx], x, maxx, y, maxy);
360
-
361
- x += (allChars[i].width) * size_multiply + padding;
362
-
363
- }
364
- const shapes: Shape[] = [];
365
- for (let i: number = 0; i < glyph_verts.length;i++) {
366
- const attr_length: number = 2;//(tess_fontTable.usesCurves)?3:2;
367
- const attributesView: AttributesView = new AttributesView(Float32Array, attr_length);
368
- attributesView.set(glyph_verts[i]);
369
- const vertexBuffer: AttributesBuffer = attributesView.attributesBuffer.cloneBufferView();
370
- attributesView.dispose();
371
- const elements = new TriangleElements(vertexBuffer);
372
- elements.setPositions(new Float2Attributes(vertexBuffer));
373
- const shape = Shape.getShape(elements);
374
-
375
- const sampler: ImageSampler = new ImageSampler();
376
- shape.style = new Style();
377
-
378
- const color = 0xFFFFFF;
379
- const alpha = 1;
380
- const obj = MaterialManager.get_material_for_color(color, alpha);
381
-
382
- shape.material = obj.material;
383
- if (obj.colorPos) {
384
- shape.style.addSamplerAt(sampler, shape.material.getTextureAt(0));
385
- (<MaterialBase> shape.material).animateUVs = true;
386
- shape.style.uvMatrix = new Matrix(0, 0, 0, 0, obj.colorPos.x, obj.colorPos.y);
387
- }
388
- shapes[i] = shape;
389
- }
390
- return shapes;
391
-
392
- }
393
-
394
- public getRatio(size: number): number {
395
- return this._size_multiply;
396
- }
397
-
398
- public hasChar(char_code: string): boolean {
399
- let has = !!this._font_chars_dic[char_code];
400
-
401
- if (!has && this._opentype_font) {
402
- has = !!this._opentype_font.charToGlyph(String.fromCharCode(parseInt(char_code)));
403
- }
404
-
405
- return has;
406
- }
407
-
408
- public changeOpenTypeFont(newOpenTypeFont: any, tesselateAllOld: boolean = true) {
409
- if ((tesselateAllOld) && (this._opentype_font)) {
410
- //todo: make sure all available chars are tesselated
411
- }
412
- // todo: when updating a font we must take care that they are compatible in terms of em_size
413
- this._opentype_font = newOpenTypeFont;
414
- this._ascent = newOpenTypeFont.ascender;// * (1024 / newOpenTypeFont.unitsPerEm);
415
- this._descent = newOpenTypeFont.descender ;//* (1024 / newOpenTypeFont.unitsPerEm);
416
- this._font_em_size = newOpenTypeFont.unitsPerEm;
417
-
418
- const space = this.getChar('32');
419
- if (space) {
420
- this._whitespace_width = space.char_width;
421
- }
422
- //console.log('changeOpenTypeFont', this._ascent, this._descent, this._font_em_size);
423
- }
424
-
425
- public initFontSize(font_size: number) {
426
- if (this._current_size === font_size) {
427
- return;
428
- }
429
-
430
- this._current_size = font_size;
431
- this._size_multiply = font_size / this._font_em_size;
432
-
433
- if (!this._size_multiply) {
434
- debugger;
435
- }
436
- }
437
-
438
- public getCharVertCnt(char_code: string): number {
439
-
440
- const t_font_char: TesselatedFontChar = this._font_chars_dic[char_code];
441
- if (t_font_char) {
442
- return t_font_char.fill_data.length;
443
- }
444
-
445
- return 0;
446
- }
447
-
448
- public getCharWidth(char_code: string): number {
449
- const space = this._whitespace_width * this._size_multiply || TesselatedFontTable.DEFAULT_SPACE;
450
-
451
- // SPACE
452
- if (char_code == '32') {
453
- return (space * 20 | 0) / 20;
454
- }
455
-
456
- // TAB
457
- if (char_code == '9') {
458
- // todo: temporary change this to 2.
459
- // need to implement textFormat tabstop
460
- return (space * 2 * 20) / 20;
461
- }
462
-
463
- let t_font_char = this.getChar(char_code, false);
464
-
465
- if (t_font_char) {
466
- return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
467
- } else if (char_code == '9679') {
468
- t_font_char = this.createPointGlyph_9679();
469
- return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
470
- }
471
-
472
- return 0;
473
- }
474
-
475
- public get usesCurves(): boolean {
476
- return this._usesCurves;
477
- }
478
-
479
- public getLineHeight(): number {
480
- // @todo: root evil for wrong linespace :(
481
- // i am still not sure if we use the correct version of this 3:
482
- return this._size_multiply * (this._ascent - this.descent); // used for sf
483
- //return this._size_multiply * this._font_em_size; // used a long time for poki etc
484
- //return (this._ascent+this._descent)*this._size_multiply; // used long ago for icycle
485
- }
486
-
487
- public getUnderLineHeight(): number {
488
- return this._size_multiply * (this._ascent - this.descent / 2);
489
- }
490
-
491
- public get assetType(): string {
492
- return TesselatedFontTable.assetType;
493
- }
494
-
495
- public dispose(): void {
496
- for (let i: number = 0; i < this._font_chars.length; i++) {
497
- this._font_chars[i].dispose();
498
- }
499
- this._font_chars.length = 0;
500
- this._font_chars_dic = null;
501
- }
502
-
503
- get fntSizeLimit(): number {
504
- return this._fntSizeLimit;
505
- }
506
-
507
- set fntSizeLimit(value: number) {
508
- this._fntSizeLimit = value;
509
- }
510
-
511
- get ascent(): number {
512
- return this._ascent;
513
- }
514
-
515
- set ascent(value: number) {
516
- this._ascent = value;
517
- }
518
-
519
- get descent(): number {
520
- return this._descent;
521
- }
522
-
523
- set descent(value: number) {
524
- this._descent = value;
525
- }
526
-
527
- get offset_x(): number {
528
- return this._offset_x;
529
- }
530
-
531
- set offset_x(value: number) {
532
- this._offset_x = value;
533
- }
534
-
535
- get offset_y(): number {
536
- return this._offset_y;
537
- }
538
-
539
- set offset_y(value: number) {
540
- this._offset_y = value;
541
- }
542
-
543
- public get_font_chars(): Array<TesselatedFontChar> {
544
- return this._font_chars;
545
- }
546
-
547
- public get_font_em_size(): number {
548
- return this._font_em_size;
549
- }
550
-
551
- public set_whitespace_width(value: number): void {
552
- this._whitespace_width = value;
553
- }
554
-
555
- public get_whitespace_width(): number {
556
- return this._whitespace_width;
557
- }
558
-
559
- public set_font_em_size(font_em_size: number): void {
560
- this._font_em_size = font_em_size;
561
- }
562
-
563
- public buildTextLineFromIndices(
564
- field: TextField,
565
- format: TextFormat,
566
- x: number, y: number,
567
- indices: number[],
568
- advance: number[]): Point {
569
-
570
- const textShape = field.getTextShapeForIdentifierAndFormat(format.color.toString(), format);
571
- const origin_x = x;
572
- const indicesCount = indices.length;
573
- const charEntries: ICharEntry[] = [];
574
- let textBuffSize: number = 0;
575
-
576
- y -= this._ascent * this._size_multiply;//this.getLineHeight();
577
-
578
- // loop over all the words and create the text data for it
579
- // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
580
- for (let i = 0; i < indicesCount; i++) {
581
- const idx = indices[i];
582
- const charGlyph = this._glyphIdxToChar[idx];
583
-
584
- if (!charGlyph) {
585
- x += advance[i];
586
- //console.log("no glyph found at idx", idx, "todo: support fallback fonts");
587
- continue;
588
- }
589
-
590
- if (charGlyph.fill_data === null) {
591
-
592
- if (charGlyph.fill_data_path.commands[0][0] == 1
593
- && charGlyph.fill_data_path.data[0][0] == 0
594
- && charGlyph.fill_data_path.data[0][1] == 0) {
595
-
596
- charGlyph.fill_data_path.data[0].shift();
597
- charGlyph.fill_data_path.data[0].shift();
598
- charGlyph.fill_data_path.commands[0].shift();
599
- charGlyph.fill_data_path.commands[0][0] = 2;
600
- }
601
-
602
- charGlyph.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(charGlyph.fill_data_path, true);
603
- }
604
-
605
- const charVertices = charGlyph.fill_data;
606
-
607
- if (charVertices) {
608
- charEntries.push({
609
- char: charGlyph, x, y, selected: false
610
- });
611
-
612
- textBuffSize += charVertices.buffer.byteLength / 4;
613
- }
614
-
615
- x += advance[i];// * size_multiply;
616
- }
617
-
618
- const buff = new Float32Array(textBuffSize);
619
- this._fillBuffer(buff, charEntries, false);
620
- textShape.addChunk(buff);
621
-
622
- return new Point(x - origin_x, this.getLineHeight());
623
- }
624
-
625
- public fillTextRun (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
626
- const useFNT: boolean = this._fntSizeLimit >= 0 && this._fntSizeLimit >= format.size;
627
-
628
- if (useFNT) {
629
- return this.fillTextRunFNT(tf, format, startWord, wordCnt);
630
- }
631
-
632
- const textShape = this._queryShape(tf, format);
633
-
634
- let textShapeSelected: TextShape;
635
- let newFormat: TextFormat;
636
-
637
- const wordsCount = startWord + wordCnt;
638
- const size_multiply = this._size_multiply;
639
-
640
- let select_start = tf.selectionBeginIndex;
641
- let select_end = tf.selectionEndIndex;
642
- let start_x: number = 0;
643
-
644
- if (tf.selectable) {
645
- newFormat = format.clone();
646
- newFormat.color = 0xffffff;
647
-
648
- textShapeSelected = this._queryShape(tf, newFormat);
649
-
650
- if (newFormat.underline && (startWord + 1) < tf.words.length) {
651
- start_x = tf.words.get(startWord).x;
652
- }
653
-
654
- if (tf.selectionEndIndex < tf.selectionBeginIndex) {
655
- select_start = tf.selectionEndIndex;
656
- select_end = tf.selectionBeginIndex;
657
- }
658
- }
659
-
660
- let selectedBuffSize = 0;
661
- let textBuffSize = 0;
662
-
663
- const charEntries: ICharEntry[] = [];
664
- const selectedCharEntries: ICharEntry[] = [];
665
- const underlines: Float32Array[] = [];
666
-
667
- // loop over all the words and create the text data for it
668
- // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
669
- for (let w = startWord; w < wordsCount; w += 1) {
670
- const word = tf.words.get(w);
671
-
672
- let x = word.x;
673
- const y = word.y;
674
-
675
- const startIdx = word.start;
676
- const charsCount = startIdx + word.len;
677
-
678
- for (let c = startIdx; c < charsCount; c++) {
679
- const curTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
680
- textShapeSelected : textShape;
681
-
682
- const code = tf.chars_codes[c];
683
-
684
- if (code === 32 || code === 9) {
685
- continue;
686
- }
687
-
688
- const charGlyph = this.getChar(code, true);
689
-
690
- if (!charGlyph) {
691
- if (once(this, 'miss' + tf.chars_codes[c])) {
692
- console.debug('[TesselatedFontTable] Error: char not found in fontTable',
693
- tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
694
- }
695
- continue;
696
- }
697
-
698
- if (curTShape === textShapeSelected) {
699
- selectedCharEntries.push({
700
- char: charGlyph, x, y, selected: true
701
- });
702
-
703
- selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
704
- } else {
705
- charEntries.push({
706
- char: charGlyph, x, y, selected: false
707
- });
708
- textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
709
- }
710
-
711
- x += charGlyph.char_width * size_multiply;
712
-
713
- }
714
-
715
- const half_thickness: number = 0.25 * tf.internalScale.y;
716
- const topY: number = y + this.getUnderLineHeight() + half_thickness;
717
- const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
718
-
719
- if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
720
- const underBuff = new Float32Array(12);
721
-
722
- underBuff[0] = start_x;
723
- underBuff[1] = bottomY;
724
- underBuff[2] = start_x;
725
- underBuff[3] = topY;
726
- underBuff[4] = x;
727
- underBuff[5] = topY;
728
- underBuff[6] = x;
729
- underBuff[7] = topY;
730
- underBuff[8] = x;
731
- underBuff[9] = bottomY;
732
- underBuff[10] = start_x;
733
- underBuff[11] = bottomY;
734
-
735
- underlines.push(underBuff);
736
- }
737
-
738
- } // for word
739
-
740
- // reallock buffers and fill
741
- if (textBuffSize > 0) {
742
- textBuffSize += underlines.length * 12;
743
-
744
- const buff = new Float32Array(textBuffSize);
745
- let offset = this._fillBuffer(buff, charEntries, false);
746
-
747
- for (const under of underlines) {
748
- buff.set(under, offset);
749
- offset += under.length;
750
- }
751
-
752
- textShape.addChunk(buff);
753
- }
754
-
755
- if (selectedBuffSize > 0) {
756
- const buff = new Float32Array(selectedBuffSize);
757
-
758
- this._fillBuffer(buff, selectedCharEntries, true);
759
-
760
- textShapeSelected.addChunk(buff);
761
- }
762
-
763
- }
764
-
765
- private _cacheKey (color: number = 0xffffff, channel: number = -1) {
766
- const fnt = channel >= 0;
767
- return `${color}${fnt}${fnt ? channel : 0}`;
768
- }
769
-
770
- /**
771
- * Query shape from TextField cache
772
- * @param tf
773
- * @param format
774
- * @param channel - FNT channel, -1 for regular font
775
- * @private
776
- */
777
- private _queryShape (tf: TextField, format: TextFormat, channel = -1): TextShape {
778
- const textShape = tf.getTextShapeForIdentifierAndFormat(this._cacheKey(format.color, channel), format);
779
-
780
- if (channel >= 0 && !textShape.fntMaterial) {
781
- const argb = ColorUtils.float32ColorToARGB(format.color);
782
- const mat = new MethodMaterial(this._fnt_channels[channel]);
783
-
784
- mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
785
- mat.bothSides = true;
786
- mat.alphaBlending = true;
787
- mat.useColorTransform = true;
788
- mat.style.sampler = new ImageSampler(false, true, true);
789
-
790
- textShape.fntMaterial = mat;
791
- }
792
-
793
- return textShape;
794
- }
795
-
796
- /**
797
- * Fill text run but use FNT Texture.
798
- */
799
- public fillTextRunFNT (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
800
- const textShape: TextShape = this._queryShape(tf, format, 0);
801
- const charShapesCache: Record<string, TextShape> = {};
802
- const charShapesSelectedCache: Record<string, TextShape> = {};
803
- const wordsCount = startWord + wordCnt;
804
- const size_multiply = this._size_multiply;
805
-
806
- let textShapeSelected: TextShape;
807
-
808
- let select_start = tf.selectionBeginIndex;
809
- let select_end = tf.selectionEndIndex;
810
- let start_x: number = 0;
811
-
812
- if (tf.selectable) {
813
- const newFormat = format.clone();
814
- newFormat.color = 0xffffff;
815
-
816
- textShapeSelected = this._queryShape(tf, newFormat, 0);
817
- charShapesSelectedCache[textShapeSelected.cacheId] = textShapeSelected;
818
-
819
- if (newFormat.underline && (startWord + 1) < tf.words.length) {
820
- start_x = tf.words.get(startWord).x;
821
- }
822
-
823
- if (tf.selectionEndIndex < tf.selectionBeginIndex) {
824
- select_start = tf.selectionEndIndex;
825
- select_end = tf.selectionBeginIndex;
826
- }
827
- }
828
-
829
- charShapesCache[textShape.cacheId] = textShape;
830
-
831
- const shapeArray = new Map<TextShape, {
832
- uv: number [],
833
- pos: number []
834
- }>();
835
-
836
- const underlines: Float32Array[] = [];
837
-
838
- // loop over all the words and create the text data for it
839
- // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
840
- for (let w = startWord; w < wordsCount; w += 1) {
841
- const word = tf.words.get(w);
842
-
843
- let x = word.x;
844
- const y = word.y;
845
-
846
- const startIdx = word.start;
847
- const charsCount = startIdx + word.len;
848
-
849
- let preFilledShape: TextShape = null;
850
- let target: any = null;
851
- let color: number = 0;
852
- let fntID: number = -1;
853
-
854
- for (let c = startIdx; c < charsCount; c++) {
855
- // space codes
856
- if (tf.chars_codes[c] === 32 || tf.chars_codes[c] === 9) {
857
- continue;
858
- }
859
-
860
- const charGlyph = this.getChar(tf.chars_codes[c], false);
861
-
862
- if (!charGlyph) {
863
- if (once(this, 'miss' + tf.chars_codes[c])) {
864
- console.debug('[TesselatedFontTable] Error: char not found in fontTable',
865
- tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
866
- }
867
- continue;
868
- }
869
-
870
- const selected = (tf.isInFocus && c >= select_start && c < select_end);
871
- let baseShape = textShape;
872
- let cacheStore = charShapesCache;
873
-
874
- if (selected) {
875
- baseShape = textShapeSelected;
876
- cacheStore = charShapesSelectedCache;
877
- }
878
-
879
- const curFormat = baseShape.format;
880
- let cacheId = preFilledShape ? preFilledShape.cacheId : '';
881
-
882
- if (!cacheId || color !== curFormat.color || charGlyph.fnt_channel !== fntID) {
883
- cacheId = this._cacheKey(curFormat.color, charGlyph.fnt_channel);
884
- color = curFormat.color;
885
- fntID = charGlyph.fnt_channel;
886
- }
887
-
888
- let fntFilledShape = preFilledShape && preFilledShape.cacheId !== cacheId
889
- ? cacheStore[cacheId]
890
- : preFilledShape;
891
-
892
- if (!fntFilledShape) {
893
- fntFilledShape = this._queryShape(tf, curFormat, charGlyph.fnt_channel);
894
- cacheStore[fntFilledShape.cacheId] = fntFilledShape;
895
- }
896
-
897
- if (preFilledShape !== fntFilledShape || !target) {
898
- // FNT is very little, has 6 vertices per shape (instead of 300 ++)
899
- // fill it directly
900
- target = shapeArray.get(fntFilledShape) || {
901
- uv: [],
902
- pos: []
903
- };
904
-
905
- shapeArray.set(fntFilledShape, target);
906
- preFilledShape = fntFilledShape;
907
- }
908
-
909
- this._emitCharFNT (target, charGlyph, x, y);
910
-
911
- x += charGlyph.char_width * size_multiply;
912
- }
913
-
914
- /*
915
- todo Generate underline for regular shape
916
-
917
- const half_thickness: number = 0.25 * tf.internalScale.y;
918
- const topY: number = y + this.getUnderLineHeight() + half_thickness;
919
- const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
920
-
921
- if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
922
- const underBuff = new Float32Array(12);
923
-
924
- underBuff[0] = start_x;
925
- underBuff[1] = bottomY;
926
- underBuff[2] = start_x;
927
- underBuff[3] = topY;
928
- underBuff[4] = x;
929
- underBuff[5] = topY;
930
- underBuff[6] = x;
931
- underBuff[7] = topY;
932
- underBuff[8] = x;
933
- underBuff[9] = bottomY;
934
- underBuff[10] = start_x;
935
- underBuff[11] = bottomY;
936
-
937
- underlines.push(underBuff);
938
- }
939
- */
940
-
941
- } // for word
942
-
943
- // oh, es5... iterating over map won't working
944
- const shapes = Array.from(shapeArray.keys());
945
-
946
- for (const shape of shapes) {
947
- const values = shapeArray.get(shape);
948
-
949
- shape.addChunk(
950
- new Float32Array(values.pos),
951
- new Float32Array(values.uv)
952
- );
953
- }
954
- }
955
-
956
- private _emitCharFNT (
957
- target: {uv: number[], pos: number[]},
958
- charGlyph: TesselatedFontChar,
959
- x: number,
960
- y: number
961
- ) {
962
- const scale = this._size_multiply;
963
- const size = this._font_em_size * scale;
964
- const rect = charGlyph.fnt_rect;
965
- const uv = charGlyph.fnt_uv;
966
- const width = charGlyph.char_width * scale;
967
-
968
- if (!rect) {
969
- // empty chars not have data
970
- return;
971
- }
972
-
973
- const x1 = x + width * rect.x * 20;
974
- const x2 = x1 + width * rect.width * 20;
975
- const y1 = y + size * rect.y * 20;
976
- const y2 = y1 + size * rect.height * 20;
977
-
978
- target.pos.push(
979
- x1, y1, x1, y2, x2, y1,
980
- x2, y1, x1, y2, x2, y2
981
- );
982
-
983
- target.uv.push(
984
- uv.x, uv.y, uv.x, uv.bottom, uv.right, uv.y,
985
- uv.right, uv.y, uv.x, uv.bottom, uv.right, uv.bottom
986
- );
987
- }
988
-
989
- public createPointGlyph_9679(): TesselatedFontChar {
990
- const verts: number[] = [];
991
- GraphicsFactoryHelper.drawElipse(
992
- this._font_em_size / 2, this._font_em_size / 2,
993
- this._font_em_size / 8, this._font_em_size / 8,
994
- verts, 0, 360, 5, false);
995
-
996
- const attributesView: AttributesView = new AttributesView(Float32Array, 2);
997
- attributesView.set(verts);
998
- const attributesBuffer: AttributesBuffer = attributesView.attributesBuffer.cloneBufferView();
999
- attributesView.dispose();
1000
- const t_font_char: TesselatedFontChar = new TesselatedFontChar(attributesBuffer, null, null);
1001
- t_font_char.char_width = this._font_em_size;
1002
- this._font_chars.push(t_font_char);
1003
- this._font_chars_dic['9679'] = t_font_char;
1004
- return t_font_char;
1005
- }
1006
-
1007
- private getCharOpenType (name: string): TesselatedFontChar {
1008
- if (!this._opentype_font) {
1009
- return null;
1010
- }
1011
- const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
1012
-
1013
- if (!thisGlyph) {
1014
- return null;
1015
- }
1016
- //console.log("got the glyph from opentype");
1017
- const thisPath = thisGlyph.getPath();
1018
- const awayPath: GraphicsPath = new GraphicsPath();
1019
- let i = 0;
1020
- const len = thisPath.commands.length;
1021
-
1022
- //awayPath.lineTo(0, 0);
1023
- //awayPath.moveTo(0,0);//-100);
1024
- //awayPath.curveTo(100, 250, 200,0);
1025
- //awayPath.lineTo(150, 100);
1026
- //awayPath.moveTo(0,20);
1027
- //awayPath.curveTo(100, 270, 200,20);
1028
- //awayPath.moveTo(0,-20);
1029
- //awayPath.moveTo(0,-10);
1030
- //awayPath.curveTo(100, -110, 200,-10);
1031
-
1032
- let startx: number = 0;
1033
- let starty: number = 0;
1034
- const y_offset = this._ascent;
1035
- //40 = 66
1036
- const scale = (this._opentype_font.unitsPerEm / 72);
1037
- for (i = 0; i < len; i++) {
1038
- const cmd = thisPath.commands[i];
1039
- //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
1040
- if (cmd.type === 'M') {
1041
- awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
1042
- startx = scale * cmd.x;
1043
- starty = scale * cmd.y + y_offset;
1044
- } else if (cmd.type === 'L') {
1045
- awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
1046
- } else if (cmd.type === 'Q') {
1047
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1048
- scale * cmd.x, scale * cmd.y + y_offset);
1049
- } else if (cmd.type === 'C') {
1050
- const mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
1051
- const mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
1052
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1053
- scale * mergedX, scale * mergedY + y_offset);
1054
- awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset,
1055
- scale * cmd.x, scale * cmd.y + y_offset);
1056
- } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
1057
- }
1058
-
1059
- const t_font_char = new TesselatedFontChar(null, null, awayPath);
1060
- t_font_char.char_width = thisGlyph.advanceWidth;//(1 / thisGlyph.path.unitsPerEm * 72);
1061
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1062
- awayPath,
1063
- true,
1064
- null,
1065
- scale * Settings.FONT_TESSELATION_QUALITY
1066
- );
1067
- t_font_char.lastTesselatedScale = scale;
1068
-
1069
- if (!t_font_char.fill_data) {
1070
- if (once(this, 'tess' + name)) {
1071
- console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:',
1072
- name.charCodeAt(0));
1073
- }
1074
- return null;
1075
- }
1076
-
1077
- this._font_chars.push(t_font_char);
1078
- this._font_chars_dic[name] = t_font_char;
1079
- }
1080
-
1081
- public getChar(name: string | number, tesselation = true): TesselatedFontChar {
1082
- name = '' + name;
1083
-
1084
- const fontChar: TesselatedFontChar = this._font_chars_dic[name];
1085
-
1086
- if (!tesselation && fontChar) {
1087
- return fontChar;
1088
- }
1089
-
1090
- if (!fontChar) {
1091
- if (this._opentype_font) {
1092
- return this.getCharOpenType(name);
1093
- }
1094
-
1095
- if (name == '9679') {
1096
- return this.createPointGlyph_9679();
1097
- }
1098
-
1099
- return null;
1100
- }
1101
-
1102
- // tesselation pass
1103
- const scale = this._size_multiply;
1104
- const qualityStepScale = Math.max(0.01, Math.round(scale * 100) / 100);
1105
-
1106
- if (fontChar.fill_data_path === null) {
1107
- return null;
1108
- }
1109
-
1110
- // we already has buffer with tesselation scale ratio
1111
- if (!(fontChar.fill_data == null && fontChar.stroke_data == null)
1112
- && fontChar.lastTesselatedScale >= qualityStepScale
1113
- ) {
1114
- return fontChar;
1115
- }
1116
-
1117
- const path = fontChar.fill_data_path;
1118
-
1119
- // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
1120
- // change the new first command to moveTo
1121
- if (path.commands[0][0] == 1 && path.data[0][0] == 0 && path.data[0][1] == 0) {
1122
- path.data[0].shift();
1123
- path.data[0].shift();
1124
- path.commands[0].shift();
1125
- path.commands[0][0] = 2;
1126
- }
1127
-
1128
- if (fontChar.fill_data) {
1129
- // it not have dispose, but clear will drop abstraction if it exist
1130
- fontChar.fill_data.clear();
1131
- }
1132
-
1133
- fontChar.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1134
- fontChar.fill_data_path,
1135
- true,
1136
- null,
1137
- qualityStepScale * Settings.FONT_TESSELATION_QUALITY
1138
- );
1139
-
1140
- if (fontChar.lastTesselatedScale > 0) {
1141
- console.debug(
1142
- '[TesselatedFontTable] Retesselate char from scale:',
1143
- fontChar.lastTesselatedScale, qualityStepScale, String.fromCharCode(+name)
1144
- );
1145
- }
1146
-
1147
- fontChar.lastTesselatedScale = qualityStepScale;
1148
-
1149
- if (!fontChar.fill_data) {
1150
- if (once(this, 'tess' + name)) {
1151
- console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
1152
- }
1153
- return null;
1154
- }
1155
-
1156
- return fontChar;
1157
- }
1158
-
1159
- public setChar(
1160
- name: string, char_width: number, fills_data: AttributesBuffer = null,
1161
- stroke_data: AttributesBuffer = null, uses_curves: boolean = false,
1162
- glyph_idx: number = 0, fill_data_path: GraphicsPath = null): void {
1163
-
1164
- char_width = Math.floor(char_width * 20) / 20;
1165
- //console.log("adding char", name, String.fromCharCode(parseInt(name)));
1166
- if ((fills_data == null) && (stroke_data == null) && (fill_data_path == null))
1167
- throw (`TesselatedFontTable: trying to create a TesselatedFontChar with no data
1168
- (fills_data, stroke_data and fill_data_path is null)`);
1169
- if (this._font_chars.length > 0) {
1170
- if (uses_curves != this._usesCurves) {
1171
- throw (`TesselatedFontTable: Can not set different types of graphic-glyphs
1172
- (curves vs non-cuves) on the same FontTable!`);
1173
- }
1174
- } else {
1175
- this._usesCurves = uses_curves;
1176
- }
1177
- const t_font_char: TesselatedFontChar = new TesselatedFontChar(fills_data, stroke_data, fill_data_path);
1178
- t_font_char.char_width = char_width;
1179
- t_font_char.glyph_idx = glyph_idx;
1180
- t_font_char.name = name;
1181
- this._glyphIdxToChar[glyph_idx] = t_font_char;
1182
-
1183
- this._font_chars.push(t_font_char);
1184
- this._font_chars_dic[name] = t_font_char;
1185
- }
1186
-
1187
- private roundTo(val: number) {
1188
- const p = Settings.TEXT_SHAPE_ROUND_PRECISION;
1189
- const invP = 1 / (p || 1);
1190
-
1191
- return Math.floor(val * invP) * p;
1192
- }
1193
-
1194
- private _fillBuffer(
1195
- buffer: Float32Array,
1196
- chars: ICharEntry[],
1197
- selection: boolean = false
1198
- ): number {
1199
-
1200
- const scale = this._size_multiply;
1201
-
1202
- let offset = 0;
1203
- for (const entry of chars) {
1204
- if (entry.selected !== selection) {
1205
- continue;
1206
- }
1207
-
1208
- const { x, y, char } = entry;
1209
- const view = new Float32Array(char.fill_data.buffer);
1210
- const count = view.length;
1211
-
1212
- for (let v = 0; v < count; v += 2) {
1213
- buffer[offset + v + 0] = this.roundTo(view[v + 0] * scale + x);
1214
- buffer[offset + v + 1] = this.roundTo(view[v + 1] * scale + y);
1215
- }
1216
-
1217
- offset += view.length;
1218
- }
1219
-
1220
- return offset;
1221
- }
1
+ import { Matrix, AssetBase, Point, Rectangle, ColorTransform, ColorUtils } from '@awayjs/core';
2
+
3
+ import { ImageSampler, AttributesBuffer, Float2Attributes, AttributesView, BitmapImage2D, Stage } from '@awayjs/stage';
4
+
5
+ import { Style, TriangleElements } from '@awayjs/renderer';
6
+
7
+ import { GraphicsPath, Shape, GraphicsFactoryFills, GraphicsFactoryHelper, MaterialManager } from '@awayjs/graphics';
8
+
9
+ import { MaterialBase, MethodMaterial } from '@awayjs/materials';
10
+
11
+ import { TesselatedFontChar } from './TesselatedFontChar';
12
+ import { IFontTable } from './IFontTable';
13
+ import { TextFormat } from './TextFormat';
14
+ import { TextShape } from './TextShape';
15
+
16
+ import { TextField } from '../display/TextField';
17
+ import { Settings } from '../Settings';
18
+ import { FNTGeneratorCanvas } from './FNTGeneratorCanvas';
19
+
20
+ const ONCE_EMIT_TABLE: StringMap<boolean> = Object.create(null);
21
+ function once(obj: any, key = '') {
22
+ const has = ONCE_EMIT_TABLE[obj._id + key];
23
+
24
+ ONCE_EMIT_TABLE[obj._id + key] = true;
25
+ return !has;
26
+ }
27
+
28
+ interface ICharEntry {
29
+ x: number;
30
+ y: number;
31
+ char: TesselatedFontChar;
32
+ selected?: boolean;
33
+ shape?: TextShape;
34
+ }
35
+
36
+ /**
37
+ * GraphicBase wraps a TriangleElements as a scene graph instantiation. A GraphicBase is owned by a Sprite object.
38
+ *
39
+ *
40
+ * @see away.base.TriangleElements
41
+ * @see away.entities.Sprite
42
+ *
43
+ * @class away.base.GraphicBase
44
+ */
45
+ export class TesselatedFontTable extends AssetBase implements IFontTable {
46
+ public static assetType: string = '[asset TesselatedFontTable]';
47
+ public static DEFAULT_SPACE = 14;
48
+
49
+ public font: any;
50
+
51
+ /*internal*/ _font_chars_dic: StringMap<TesselatedFontChar>;
52
+ /*internal*/ _current_size: number;
53
+ /*internal*/ _size_multiply: number;
54
+
55
+ private _font_chars: Array<TesselatedFontChar>;
56
+ private _font_em_size: number;
57
+
58
+ private _whitespace_width: number;
59
+
60
+ private _offset_x: number;
61
+ private _offset_y: number;
62
+ private _ascent: number;
63
+ private _descent: number;
64
+ private _charDictDirty: Boolean;
65
+ private _usesCurves: boolean;
66
+ private _opentype_font: any;
67
+ private _glyphIdxToChar: any;
68
+
69
+ private _fntSizeLimit: number=-1;
70
+ private _fnt_channels: BitmapImage2D[];
71
+
72
+ /**
73
+ * Creates a new TesselatedFont object
74
+ * If a opentype_font object is passed, the chars will get tessellated whenever requested.
75
+ * If no opentype font object is passed, it is expected that tesselated chars
76
+ */
77
+ constructor(opentype_font: any = null) {
78
+ super();
79
+ this._font_chars = [];
80
+ this._font_chars_dic = Object.create(null);
81
+ this._current_size = 0;
82
+ this._size_multiply = 0;
83
+ this._ascent = 0;
84
+ this._descent = 0;
85
+ this._usesCurves = false;
86
+ this._glyphIdxToChar = {};
87
+ this._fnt_channels = [];
88
+
89
+ // default size
90
+ this._font_em_size = 32;
91
+
92
+ if (opentype_font) {
93
+ this._opentype_font = opentype_font;
94
+ /*
95
+ console.log("head.yMax ",opentype_font);
96
+
97
+ console.log("head.yMax "+opentype_font.tables.head.yMax);
98
+ console.log("head.yMin "+opentype_font.tables.head.yMin);
99
+ console.log("font.numGlyphs "+opentype_font.numGlyphs);
100
+ console.log('Ascender', opentype_font.tables.hhea.ascender);
101
+ console.log('Descender', opentype_font.tables.hhea.descender);
102
+ console.log('Typo Ascender', opentype_font.tables.os2.sTypoAscender);
103
+ console.log('Typo Descender', opentype_font.tables.os2.sTypoDescender);
104
+ */
105
+ this._font_em_size = 72;//2048;
106
+ this._ascent = opentype_font.tables.hhea.ascender / (this._opentype_font.unitsPerEm / 72);
107
+ this._descent = -2 + opentype_font.tables.hhea.descender / (this._opentype_font.unitsPerEm / 72);
108
+ this._current_size = 0;
109
+ this._size_multiply = 0;
110
+
111
+ const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(32));
112
+ if (thisGlyph) {
113
+ //this._whitespace_width = thisGlyph.advanceWidth / (this._opentype_font.unitsPerEm / 72);
114
+ }
115
+ return;
116
+ }
117
+ }
118
+
119
+ public addFNTChannel(mat: BitmapImage2D) {
120
+ this._fnt_channels.push(mat);
121
+ }
122
+
123
+ public getGlyphCount(): number {
124
+ return this._font_chars.length;
125
+ }
126
+
127
+ public generateFNTData (stage: Stage): void {
128
+ // already exist
129
+ if (this._fntSizeLimit > 0) {
130
+ return;
131
+ }
132
+
133
+ const generator = new FNTGeneratorCanvas(stage, false);
134
+ const bitmaps = generator.generate(this.font, this._font_em_size, this._current_size, 4);
135
+
136
+ if (bitmaps && bitmaps.length > 0) {
137
+ /**
138
+ * Random value, anyway we render real font dimension ;)
139
+ */
140
+ this._fntSizeLimit = this._current_size * 4;
141
+ }
142
+ }
143
+
144
+ public generateFNTTextures(padding: number, fontSize: number, texSize: number): Shape[] {
145
+ console.log('generateFNTTextures');
146
+ if (fontSize) {
147
+ this._fntSizeLimit = fontSize * 0.5; //why half?
148
+ this.initFontSize(fontSize);
149
+
150
+ }
151
+
152
+ if (this._opentype_font) {
153
+ // if this was loaded from opentype,
154
+ // we make sure all glyphs are tesselated first
155
+ for (let g: number = 0; g < this._opentype_font.glyphs.length;g++) {
156
+ const thisGlyph = this._opentype_font.charToGlyph(
157
+ String.fromCharCode(this._opentype_font.glyphs.glyphs[g].unicode));
158
+ if (thisGlyph) {
159
+ const thisPath = thisGlyph.getPath();
160
+ const awayPath: GraphicsPath = new GraphicsPath();
161
+ let i = 0;
162
+ const len = thisPath.commands.length;
163
+ let startx: number = 0;
164
+ let starty: number = 0;
165
+ const y_offset = this._ascent;
166
+ for (i = 0;i < len;i++) {
167
+ const cmd = thisPath.commands[i];
168
+ //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
169
+ if (cmd.type === 'M') {
170
+ awayPath.moveTo(cmd.x, cmd.y + y_offset);
171
+ startx = cmd.x;
172
+ starty = cmd.y + y_offset;
173
+ } else if (cmd.type === 'L') {
174
+ awayPath.lineTo(cmd.x, cmd.y + y_offset);
175
+ } else if (cmd.type === 'Q') {
176
+ awayPath.curveTo(cmd.x1, cmd.y1 + y_offset, cmd.x, cmd.y + y_offset);
177
+ } else if (cmd.type === 'C') {
178
+ awayPath.cubicCurveTo(cmd.x1, cmd.y1 + y_offset, cmd.x2,
179
+ cmd.y2 + y_offset, cmd.x, cmd.y + y_offset);
180
+ } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
181
+ }
182
+ const t_font_char: TesselatedFontChar = new TesselatedFontChar(null, null, awayPath);
183
+ t_font_char.char_width = (thisGlyph.advanceWidth * (1 / thisGlyph.path.unitsPerEm * 72));
184
+ t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(awayPath, true);
185
+ if (!t_font_char.fill_data) {
186
+ console.log('error tesselating opentype glyph', this._opentype_font.glyphs.glyphs[g]);
187
+ //return null;
188
+ } else {
189
+ this._font_chars.push(t_font_char);
190
+ this._font_chars_dic[this._opentype_font.glyphs.glyphs[g].unicode] = t_font_char;
191
+
192
+ }
193
+ //console.log("unicode:",this._opentype_font.glyphs.glyphs[g].unicode);
194
+ //console.log("name:",this._opentype_font.glyphs.glyphs[g].name);
195
+
196
+ } else {
197
+ console.log('no char found for', this._opentype_font.glyphs.glyphs[g]);
198
+ }
199
+ }
200
+ }
201
+ let char_vertices: AttributesBuffer;
202
+ let x: number = 0;
203
+ let y: number = 0;
204
+ let buffer: Float32Array;
205
+ let v: number;
206
+ const glyph_verts: number[][] = [[]];
207
+ let channel_idx: number = 0;
208
+ let maxSize = 0;
209
+ const allChars: any[] = [];
210
+
211
+ let maxy: number = Number.MIN_VALUE;
212
+ let maxx: number = Number.MIN_VALUE;
213
+ let miny: number = Number.MAX_VALUE;
214
+ let minx: number = Number.MAX_VALUE;
215
+ let tmpx: number = 0;
216
+ let tmpy: number = 0;
217
+
218
+ // step1: loop over all chars, get their bounds and sort them by there height
219
+ // done without applying any font-scale to the glyphdata
220
+ let allAreas: number = 0;
221
+ for (let i: number = 0; i < this._font_chars.length;i++) {
222
+ char_vertices = this._font_chars[i].fill_data;
223
+ //console.log("got the glyph from opentype", this._opentype_font.glyphs.glyphs[g]);
224
+ buffer = new Float32Array(char_vertices.buffer);
225
+ maxy = Number.MIN_VALUE;
226
+ maxx = Number.MIN_VALUE;
227
+ miny = Number.MAX_VALUE;
228
+ minx = Number.MAX_VALUE;
229
+ tmpx = 0;
230
+ tmpy = 0;
231
+ for (v = 0; v < char_vertices.count; v++) {
232
+ tmpx = (buffer[v * 2]);
233
+ tmpy = (buffer[v * 2 + 1]);
234
+ if (tmpx < minx) minx = tmpx;
235
+ if (tmpx > maxx) maxx = tmpx;
236
+ if (tmpy < miny) miny = tmpy;
237
+ if (tmpy > maxy) maxy = tmpy;
238
+ }
239
+ this._font_chars[i].fnt_rect = new Rectangle(
240
+ (minx) / (this._font_chars[i].char_width),
241
+ (miny) / (this._font_em_size),
242
+ (maxx - minx) / (this._font_chars[i].char_width),
243
+ (maxy - miny) / (this._font_em_size));
244
+
245
+ allAreas += (maxx - minx + padding + padding) * (maxy - miny + padding + padding);
246
+ allChars[allChars.length] = {
247
+ idx:i,
248
+ minx:minx,
249
+ miny:miny,
250
+ width:maxx - minx,
251
+ height:maxy - miny
252
+ };
253
+ }
254
+ allChars.sort(function(a,b) {
255
+ return a.height > b.height ? -1 : 1;
256
+ });
257
+
258
+ // now allChars contains all glyphs sorted by height
259
+
260
+ let curHeight: number = 0;
261
+ maxSize = texSize;
262
+ let size_multiply: number = 1;
263
+ if (fontSize) {
264
+ this.initFontSize(fontSize);
265
+ size_multiply = this._size_multiply;
266
+ } else {
267
+ // figure out the optiomal fontSize so that the glyphs fill the texture
268
+
269
+ // imagine the spaces needed by all glyphs would be a perfect quat
270
+ // compare the width of that quat with the available width to have a first guess at fontSize
271
+
272
+ // make the size slightly bigger as the estimate and check if all glyphs will fit
273
+ // if they do not fit, slightly reduce the scale.
274
+ // repeat until all glyphs fit
275
+
276
+ size_multiply = maxSize / (Math.sqrt(allAreas)) * 1.2;
277
+ let invalid: boolean = true;
278
+ let curCharHeight: number = 0;
279
+ while (invalid) {
280
+ invalid = false;
281
+ x = 0;
282
+ y = padding;
283
+ for (let i: number = 0; i < allChars.length;i++) {
284
+ curCharHeight = allChars[i].height * size_multiply;
285
+ tmpx = 0;
286
+ tmpy = 0;
287
+ x += padding;
288
+ if (x + allChars[i].width * size_multiply + padding >= maxSize) {
289
+ x = padding;
290
+ y += curHeight + padding * 2;
291
+ curHeight = 0;
292
+ }
293
+ if (curCharHeight > curHeight) {
294
+ curHeight = curCharHeight;
295
+ }
296
+ if (y + curCharHeight + padding * 2 >= maxSize) {
297
+ invalid = true;
298
+ size_multiply = size_multiply * 0.995;
299
+ break;
300
+ }
301
+ x += (allChars[i].width) * size_multiply + padding;
302
+ }
303
+ }
304
+ }
305
+
306
+ this._fntSizeLimit = size_multiply * this._font_em_size * 0.5; //why half?
307
+
308
+ // collect the glyph-data:
309
+
310
+ let font_char: TesselatedFontChar;
311
+ x = 0;
312
+ y = padding;
313
+ for (let i: number = 0; i < allChars.length;i++) {
314
+
315
+ font_char = this._font_chars[allChars[i].idx];
316
+ char_vertices = font_char.fill_data;
317
+ buffer = new Float32Array(char_vertices.buffer);
318
+ minx = allChars[i].minx;
319
+ maxx = Number.MIN_VALUE;
320
+ miny = allChars[i].miny;
321
+ maxy = Number.MIN_VALUE;
322
+ tmpx = 0;
323
+ tmpy = 0;
324
+ x += padding;
325
+
326
+ if (x + allChars[i].width * size_multiply + padding >= maxSize) {
327
+ x = padding;
328
+ y += curHeight + padding * 2;
329
+ curHeight = 0;
330
+ }
331
+ allChars[i].height *= size_multiply;
332
+ if (allChars[i].height > curHeight) {
333
+ curHeight = allChars[i].height;
334
+ }
335
+ if (y + curHeight + padding * 2 >= maxSize) {
336
+ x = padding;
337
+ y = padding;
338
+ curHeight = 0;
339
+ channel_idx++;
340
+ glyph_verts[channel_idx] = [];
341
+ }
342
+ font_char.fnt_channel = channel_idx;
343
+ for (v = 0; v < char_vertices.count; v++) {
344
+ tmpx = ((buffer[v * 2] - minx) * size_multiply) + x;
345
+ tmpy = ((buffer[v * 2 + 1] - miny) * size_multiply) + y;
346
+ glyph_verts[channel_idx][glyph_verts[channel_idx].length] = tmpx;
347
+ glyph_verts[channel_idx][glyph_verts[channel_idx].length] = tmpy;
348
+ if (tmpx > maxx) {
349
+ maxx = tmpx;
350
+ }
351
+ if (tmpy > maxy) {
352
+ maxy = tmpy;
353
+ }
354
+ }
355
+ font_char.fnt_uv = new Rectangle(
356
+ x / maxSize, 1 - (y / maxSize),
357
+ (maxx - x) / maxSize, ((maxy - y) / maxSize));
358
+
359
+ //this._drawDebugRect(glyph_verts[channel_idx], x, maxx, y, maxy);
360
+
361
+ x += (allChars[i].width) * size_multiply + padding;
362
+
363
+ }
364
+ const shapes: Shape[] = [];
365
+ for (let i: number = 0; i < glyph_verts.length;i++) {
366
+ const attr_length: number = 2;//(tess_fontTable.usesCurves)?3:2;
367
+ const attributesView: AttributesView = new AttributesView(Float32Array, attr_length);
368
+ attributesView.set(glyph_verts[i]);
369
+ const vertexBuffer: AttributesBuffer = attributesView.attributesBuffer.cloneBufferView();
370
+ attributesView.dispose();
371
+ const elements = new TriangleElements(vertexBuffer);
372
+ elements.setPositions(new Float2Attributes(vertexBuffer));
373
+ const shape = Shape.getShape(elements);
374
+
375
+ const sampler: ImageSampler = new ImageSampler();
376
+ shape.style = new Style();
377
+
378
+ const color = 0xFFFFFF;
379
+ const alpha = 1;
380
+ const obj = MaterialManager.get_material_for_color(color, alpha);
381
+
382
+ shape.material = obj.material;
383
+ if (obj.colorPos) {
384
+ shape.style.addSamplerAt(sampler, shape.material.getTextureAt(0));
385
+ (<MaterialBase> shape.material).animateUVs = true;
386
+ shape.style.uvMatrix = new Matrix(0, 0, 0, 0, obj.colorPos.x, obj.colorPos.y);
387
+ }
388
+ shapes[i] = shape;
389
+ }
390
+ return shapes;
391
+
392
+ }
393
+
394
+ public getRatio(size: number): number {
395
+ return this._size_multiply;
396
+ }
397
+
398
+ public hasChar(char_code: string): boolean {
399
+ let has = !!this._font_chars_dic[char_code];
400
+
401
+ if (!has && this._opentype_font) {
402
+ has = !!this._opentype_font.charToGlyph(String.fromCharCode(parseInt(char_code)));
403
+ }
404
+
405
+ return has;
406
+ }
407
+
408
+ public changeOpenTypeFont(newOpenTypeFont: any, tesselateAllOld: boolean = true) {
409
+ if ((tesselateAllOld) && (this._opentype_font)) {
410
+ //todo: make sure all available chars are tesselated
411
+ }
412
+ // todo: when updating a font we must take care that they are compatible in terms of em_size
413
+ this._opentype_font = newOpenTypeFont;
414
+ this._ascent = newOpenTypeFont.ascender;// * (1024 / newOpenTypeFont.unitsPerEm);
415
+ this._descent = newOpenTypeFont.descender ;//* (1024 / newOpenTypeFont.unitsPerEm);
416
+ this._font_em_size = newOpenTypeFont.unitsPerEm;
417
+
418
+ const space = this.getChar('32');
419
+ if (space) {
420
+ this._whitespace_width = space.char_width;
421
+ }
422
+ //console.log('changeOpenTypeFont', this._ascent, this._descent, this._font_em_size);
423
+ }
424
+
425
+ public initFontSize(font_size: number) {
426
+ if (this._current_size === font_size) {
427
+ return;
428
+ }
429
+
430
+ this._current_size = font_size;
431
+ this._size_multiply = font_size / this._font_em_size;
432
+
433
+ if (!this._size_multiply) {
434
+ debugger;
435
+ }
436
+ }
437
+
438
+ public getCharVertCnt(char_code: string): number {
439
+
440
+ const t_font_char: TesselatedFontChar = this._font_chars_dic[char_code];
441
+ if (t_font_char) {
442
+ return t_font_char.fill_data.length;
443
+ }
444
+
445
+ return 0;
446
+ }
447
+
448
+ public getCharWidth(char_code: string): number {
449
+ const space = this._whitespace_width * this._size_multiply || TesselatedFontTable.DEFAULT_SPACE;
450
+
451
+ // SPACE
452
+ if (char_code == '32') {
453
+ return (space * 20 | 0) / 20;
454
+ }
455
+
456
+ // TAB
457
+ if (char_code == '9') {
458
+ // todo: temporary change this to 2.
459
+ // need to implement textFormat tabstop
460
+ return (space * 2 * 20) / 20;
461
+ }
462
+
463
+ let t_font_char = this.getChar(char_code, false);
464
+
465
+ if (t_font_char) {
466
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
467
+ } else if (char_code == '9679') {
468
+ t_font_char = this.createPointGlyph_9679();
469
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
470
+ }
471
+
472
+ return 0;
473
+ }
474
+
475
+ public get usesCurves(): boolean {
476
+ return this._usesCurves;
477
+ }
478
+
479
+ public getLineHeight(): number {
480
+ // @todo: root evil for wrong linespace :(
481
+ // i am still not sure if we use the correct version of this 3:
482
+ return this._size_multiply * (this._ascent - this.descent); // used for sf
483
+ //return this._size_multiply * this._font_em_size; // used a long time for poki etc
484
+ //return (this._ascent+this._descent)*this._size_multiply; // used long ago for icycle
485
+ }
486
+
487
+ public getUnderLineHeight(): number {
488
+ return this._size_multiply * (this._ascent - this.descent / 2);
489
+ }
490
+
491
+ public get assetType(): string {
492
+ return TesselatedFontTable.assetType;
493
+ }
494
+
495
+ public dispose(): void {
496
+ for (let i: number = 0; i < this._font_chars.length; i++) {
497
+ this._font_chars[i].dispose();
498
+ }
499
+ this._font_chars.length = 0;
500
+ this._font_chars_dic = null;
501
+ }
502
+
503
+ get fntSizeLimit(): number {
504
+ return this._fntSizeLimit;
505
+ }
506
+
507
+ set fntSizeLimit(value: number) {
508
+ this._fntSizeLimit = value;
509
+ }
510
+
511
+ get ascent(): number {
512
+ return this._ascent;
513
+ }
514
+
515
+ set ascent(value: number) {
516
+ this._ascent = value;
517
+ }
518
+
519
+ get descent(): number {
520
+ return this._descent;
521
+ }
522
+
523
+ set descent(value: number) {
524
+ this._descent = value;
525
+ }
526
+
527
+ get offset_x(): number {
528
+ return this._offset_x;
529
+ }
530
+
531
+ set offset_x(value: number) {
532
+ this._offset_x = value;
533
+ }
534
+
535
+ get offset_y(): number {
536
+ return this._offset_y;
537
+ }
538
+
539
+ set offset_y(value: number) {
540
+ this._offset_y = value;
541
+ }
542
+
543
+ public get_font_chars(): Array<TesselatedFontChar> {
544
+ return this._font_chars;
545
+ }
546
+
547
+ public get_font_em_size(): number {
548
+ return this._font_em_size;
549
+ }
550
+
551
+ public set_whitespace_width(value: number): void {
552
+ this._whitespace_width = value;
553
+ }
554
+
555
+ public get_whitespace_width(): number {
556
+ return this._whitespace_width;
557
+ }
558
+
559
+ public set_font_em_size(font_em_size: number): void {
560
+ this._font_em_size = font_em_size;
561
+ }
562
+
563
+ public buildTextLineFromIndices(
564
+ field: TextField,
565
+ format: TextFormat,
566
+ x: number, y: number,
567
+ indices: number[],
568
+ advance: number[]): Point {
569
+
570
+ const textShape = field.getTextShapeForIdentifierAndFormat(format.color.toString(), format);
571
+ const origin_x = x;
572
+ const indicesCount = indices.length;
573
+ const charEntries: ICharEntry[] = [];
574
+ let textBuffSize: number = 0;
575
+
576
+ y -= this._ascent * this._size_multiply;//this.getLineHeight();
577
+
578
+ // loop over all the words and create the text data for it
579
+ // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
580
+ for (let i = 0; i < indicesCount; i++) {
581
+ const idx = indices[i];
582
+ const charGlyph = this._glyphIdxToChar[idx];
583
+
584
+ if (!charGlyph) {
585
+ x += advance[i];
586
+ //console.log("no glyph found at idx", idx, "todo: support fallback fonts");
587
+ continue;
588
+ }
589
+
590
+ if (charGlyph.fill_data === null) {
591
+
592
+ if (charGlyph.fill_data_path.commands[0][0] == 1
593
+ && charGlyph.fill_data_path.data[0][0] == 0
594
+ && charGlyph.fill_data_path.data[0][1] == 0) {
595
+
596
+ charGlyph.fill_data_path.data[0].shift();
597
+ charGlyph.fill_data_path.data[0].shift();
598
+ charGlyph.fill_data_path.commands[0].shift();
599
+ charGlyph.fill_data_path.commands[0][0] = 2;
600
+ }
601
+
602
+ charGlyph.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(charGlyph.fill_data_path, true);
603
+ }
604
+
605
+ const charVertices = charGlyph.fill_data;
606
+
607
+ if (charVertices) {
608
+ charEntries.push({
609
+ char: charGlyph, x, y, selected: false
610
+ });
611
+
612
+ textBuffSize += charVertices.buffer.byteLength / 4;
613
+ }
614
+
615
+ x += advance[i];// * size_multiply;
616
+ }
617
+
618
+ const buff = new Float32Array(textBuffSize);
619
+ this._fillBuffer(buff, charEntries, false);
620
+ textShape.addChunk(buff);
621
+
622
+ return new Point(x - origin_x, this.getLineHeight());
623
+ }
624
+
625
+ public fillTextRun (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
626
+ const useFNT: boolean = this._fntSizeLimit >= 0 && this._fntSizeLimit >= format.size;
627
+
628
+ if (useFNT) {
629
+ return this.fillTextRunFNT(tf, format, startWord, wordCnt);
630
+ }
631
+
632
+ const textShape = this._queryShape(tf, format);
633
+
634
+ let textShapeSelected: TextShape;
635
+ let newFormat: TextFormat;
636
+
637
+ const wordsCount = startWord + wordCnt;
638
+ const size_multiply = this._size_multiply;
639
+
640
+ let select_start = tf.selectionBeginIndex;
641
+ let select_end = tf.selectionEndIndex;
642
+ let start_x: number = 0;
643
+
644
+ if (tf.selectable) {
645
+ newFormat = format.clone();
646
+ newFormat.color = 0xffffff;
647
+
648
+ textShapeSelected = this._queryShape(tf, newFormat);
649
+
650
+ if (newFormat.underline && (startWord + 1) < tf.words.length) {
651
+ start_x = tf.words.get(startWord).x;
652
+ }
653
+
654
+ if (tf.selectionEndIndex < tf.selectionBeginIndex) {
655
+ select_start = tf.selectionEndIndex;
656
+ select_end = tf.selectionBeginIndex;
657
+ }
658
+ }
659
+
660
+ let selectedBuffSize = 0;
661
+ let textBuffSize = 0;
662
+
663
+ const charEntries: ICharEntry[] = [];
664
+ const selectedCharEntries: ICharEntry[] = [];
665
+ const underlines: Float32Array[] = [];
666
+
667
+ // loop over all the words and create the text data for it
668
+ // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
669
+ for (let w = startWord; w < wordsCount; w += 1) {
670
+ const word = tf.words.get(w);
671
+
672
+ let x = word.x;
673
+ const y = word.y;
674
+
675
+ const startIdx = word.start;
676
+ const charsCount = startIdx + word.len;
677
+
678
+ for (let c = startIdx; c < charsCount; c++) {
679
+ const curTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
680
+ textShapeSelected : textShape;
681
+
682
+ const code = tf.chars_codes[c];
683
+
684
+ if (code === 32 || code === 9) {
685
+ continue;
686
+ }
687
+
688
+ const charGlyph = this.getChar(code, true);
689
+
690
+ if (!charGlyph) {
691
+ if (once(this, 'miss' + tf.chars_codes[c])) {
692
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable',
693
+ tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
694
+ }
695
+ continue;
696
+ }
697
+
698
+ if (curTShape === textShapeSelected) {
699
+ selectedCharEntries.push({
700
+ char: charGlyph, x, y, selected: true
701
+ });
702
+
703
+ selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
704
+ } else {
705
+ charEntries.push({
706
+ char: charGlyph, x, y, selected: false
707
+ });
708
+ textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
709
+ }
710
+
711
+ x += charGlyph.char_width * size_multiply;
712
+
713
+ }
714
+
715
+ const half_thickness: number = 0.25 * tf.internalScale.y;
716
+ const topY: number = y + this.getUnderLineHeight() + half_thickness;
717
+ const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
718
+
719
+ if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
720
+ const underBuff = new Float32Array(12);
721
+
722
+ underBuff[0] = start_x;
723
+ underBuff[1] = bottomY;
724
+ underBuff[2] = start_x;
725
+ underBuff[3] = topY;
726
+ underBuff[4] = x;
727
+ underBuff[5] = topY;
728
+ underBuff[6] = x;
729
+ underBuff[7] = topY;
730
+ underBuff[8] = x;
731
+ underBuff[9] = bottomY;
732
+ underBuff[10] = start_x;
733
+ underBuff[11] = bottomY;
734
+
735
+ underlines.push(underBuff);
736
+ }
737
+
738
+ } // for word
739
+
740
+ // reallock buffers and fill
741
+ if (textBuffSize > 0) {
742
+ textBuffSize += underlines.length * 12;
743
+
744
+ const buff = new Float32Array(textBuffSize);
745
+ let offset = this._fillBuffer(buff, charEntries, false);
746
+
747
+ for (const under of underlines) {
748
+ buff.set(under, offset);
749
+ offset += under.length;
750
+ }
751
+
752
+ textShape.addChunk(buff);
753
+ }
754
+
755
+ if (selectedBuffSize > 0) {
756
+ const buff = new Float32Array(selectedBuffSize);
757
+
758
+ this._fillBuffer(buff, selectedCharEntries, true);
759
+
760
+ textShapeSelected.addChunk(buff);
761
+ }
762
+
763
+ }
764
+
765
+ private _cacheKey (color: number = 0xffffff, channel: number = -1) {
766
+ const fnt = channel >= 0;
767
+ return `${color}${fnt}${fnt ? channel : 0}`;
768
+ }
769
+
770
+ /**
771
+ * Query shape from TextField cache
772
+ * @param tf
773
+ * @param format
774
+ * @param channel - FNT channel, -1 for regular font
775
+ * @private
776
+ */
777
+ private _queryShape (tf: TextField, format: TextFormat, channel = -1): TextShape {
778
+ const textShape = tf.getTextShapeForIdentifierAndFormat(this._cacheKey(format.color, channel), format);
779
+
780
+ if (channel >= 0 && !textShape.fntMaterial) {
781
+ const argb = ColorUtils.float32ColorToARGB(format.color);
782
+ const mat = new MethodMaterial(this._fnt_channels[channel]);
783
+
784
+ mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
785
+ mat.bothSides = true;
786
+ mat.alphaBlending = true;
787
+ mat.useColorTransform = true;
788
+ mat.style.sampler = new ImageSampler(false, true, true);
789
+
790
+ textShape.fntMaterial = mat;
791
+ }
792
+
793
+ return textShape;
794
+ }
795
+
796
+ /**
797
+ * Fill text run but use FNT Texture.
798
+ */
799
+ public fillTextRunFNT (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
800
+ const textShape: TextShape = this._queryShape(tf, format, 0);
801
+ const charShapesCache: Record<string, TextShape> = {};
802
+ const charShapesSelectedCache: Record<string, TextShape> = {};
803
+ const wordsCount = startWord + wordCnt;
804
+ const size_multiply = this._size_multiply;
805
+
806
+ let textShapeSelected: TextShape;
807
+
808
+ let select_start = tf.selectionBeginIndex;
809
+ let select_end = tf.selectionEndIndex;
810
+ let start_x: number = 0;
811
+
812
+ if (tf.selectable) {
813
+ const newFormat = format.clone();
814
+ newFormat.color = 0xffffff;
815
+
816
+ textShapeSelected = this._queryShape(tf, newFormat, 0);
817
+ charShapesSelectedCache[textShapeSelected.cacheId] = textShapeSelected;
818
+
819
+ if (newFormat.underline && (startWord + 1) < tf.words.length) {
820
+ start_x = tf.words.get(startWord).x;
821
+ }
822
+
823
+ if (tf.selectionEndIndex < tf.selectionBeginIndex) {
824
+ select_start = tf.selectionEndIndex;
825
+ select_end = tf.selectionBeginIndex;
826
+ }
827
+ }
828
+
829
+ charShapesCache[textShape.cacheId] = textShape;
830
+
831
+ const shapeArray = new Map<TextShape, {
832
+ uv: number [],
833
+ pos: number []
834
+ }>();
835
+
836
+ const underlines: Float32Array[] = [];
837
+
838
+ // loop over all the words and create the text data for it
839
+ // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
840
+ for (let w = startWord; w < wordsCount; w += 1) {
841
+ const word = tf.words.get(w);
842
+
843
+ let x = word.x;
844
+ const y = word.y;
845
+
846
+ const startIdx = word.start;
847
+ const charsCount = startIdx + word.len;
848
+
849
+ let preFilledShape: TextShape = null;
850
+ let target: any = null;
851
+ let color: number = 0;
852
+ let fntID: number = -1;
853
+
854
+ for (let c = startIdx; c < charsCount; c++) {
855
+ // space codes
856
+ if (tf.chars_codes[c] === 32 || tf.chars_codes[c] === 9) {
857
+ continue;
858
+ }
859
+
860
+ const charGlyph = this.getChar(tf.chars_codes[c], false);
861
+
862
+ if (!charGlyph) {
863
+ if (once(this, 'miss' + tf.chars_codes[c])) {
864
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable',
865
+ tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
866
+ }
867
+ continue;
868
+ }
869
+
870
+ const selected = (tf.isInFocus && c >= select_start && c < select_end);
871
+ let baseShape = textShape;
872
+ let cacheStore = charShapesCache;
873
+
874
+ if (selected) {
875
+ baseShape = textShapeSelected;
876
+ cacheStore = charShapesSelectedCache;
877
+ }
878
+
879
+ const curFormat = baseShape.format;
880
+ let cacheId = preFilledShape ? preFilledShape.cacheId : '';
881
+
882
+ if (!cacheId || color !== curFormat.color || charGlyph.fnt_channel !== fntID) {
883
+ cacheId = this._cacheKey(curFormat.color, charGlyph.fnt_channel);
884
+ color = curFormat.color;
885
+ fntID = charGlyph.fnt_channel;
886
+ }
887
+
888
+ let fntFilledShape = preFilledShape && preFilledShape.cacheId !== cacheId
889
+ ? cacheStore[cacheId]
890
+ : preFilledShape;
891
+
892
+ if (!fntFilledShape) {
893
+ fntFilledShape = this._queryShape(tf, curFormat, charGlyph.fnt_channel);
894
+ cacheStore[fntFilledShape.cacheId] = fntFilledShape;
895
+ }
896
+
897
+ if (preFilledShape !== fntFilledShape || !target) {
898
+ // FNT is very little, has 6 vertices per shape (instead of 300 ++)
899
+ // fill it directly
900
+ target = shapeArray.get(fntFilledShape) || {
901
+ uv: [],
902
+ pos: []
903
+ };
904
+
905
+ shapeArray.set(fntFilledShape, target);
906
+ preFilledShape = fntFilledShape;
907
+ }
908
+
909
+ this._emitCharFNT (target, charGlyph, x, y);
910
+
911
+ x += charGlyph.char_width * size_multiply;
912
+ }
913
+
914
+ /*
915
+ todo Generate underline for regular shape
916
+
917
+ const half_thickness: number = 0.25 * tf.internalScale.y;
918
+ const topY: number = y + this.getUnderLineHeight() + half_thickness;
919
+ const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
920
+
921
+ if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
922
+ const underBuff = new Float32Array(12);
923
+
924
+ underBuff[0] = start_x;
925
+ underBuff[1] = bottomY;
926
+ underBuff[2] = start_x;
927
+ underBuff[3] = topY;
928
+ underBuff[4] = x;
929
+ underBuff[5] = topY;
930
+ underBuff[6] = x;
931
+ underBuff[7] = topY;
932
+ underBuff[8] = x;
933
+ underBuff[9] = bottomY;
934
+ underBuff[10] = start_x;
935
+ underBuff[11] = bottomY;
936
+
937
+ underlines.push(underBuff);
938
+ }
939
+ */
940
+
941
+ } // for word
942
+
943
+ // oh, es5... iterating over map won't working
944
+ const shapes = Array.from(shapeArray.keys());
945
+
946
+ for (const shape of shapes) {
947
+ const values = shapeArray.get(shape);
948
+
949
+ shape.addChunk(
950
+ new Float32Array(values.pos),
951
+ new Float32Array(values.uv)
952
+ );
953
+ }
954
+ }
955
+
956
+ private _emitCharFNT (
957
+ target: {uv: number[], pos: number[]},
958
+ charGlyph: TesselatedFontChar,
959
+ x: number,
960
+ y: number
961
+ ) {
962
+ const scale = this._size_multiply;
963
+ const size = this._font_em_size * scale;
964
+ const rect = charGlyph.fnt_rect;
965
+ const uv = charGlyph.fnt_uv;
966
+ const width = charGlyph.char_width * scale;
967
+
968
+ if (!rect) {
969
+ // empty chars not have data
970
+ return;
971
+ }
972
+
973
+ const x1 = x + width * rect.x * 20;
974
+ const x2 = x1 + width * rect.width * 20;
975
+ const y1 = y + size * rect.y * 20;
976
+ const y2 = y1 + size * rect.height * 20;
977
+
978
+ target.pos.push(
979
+ x1, y1, x1, y2, x2, y1,
980
+ x2, y1, x1, y2, x2, y2
981
+ );
982
+
983
+ target.uv.push(
984
+ uv.x, uv.y, uv.x, uv.bottom, uv.right, uv.y,
985
+ uv.right, uv.y, uv.x, uv.bottom, uv.right, uv.bottom
986
+ );
987
+ }
988
+
989
+ public createPointGlyph_9679(): TesselatedFontChar {
990
+ const verts: number[] = [];
991
+ GraphicsFactoryHelper.drawElipse(
992
+ this._font_em_size / 2, this._font_em_size / 2,
993
+ this._font_em_size / 8, this._font_em_size / 8,
994
+ verts, 0, 360, 5, false);
995
+
996
+ const attributesView: AttributesView = new AttributesView(Float32Array, 2);
997
+ attributesView.set(verts);
998
+ const attributesBuffer: AttributesBuffer = attributesView.attributesBuffer.cloneBufferView();
999
+ attributesView.dispose();
1000
+ const t_font_char: TesselatedFontChar = new TesselatedFontChar(attributesBuffer, null, null);
1001
+ t_font_char.char_width = this._font_em_size;
1002
+ this._font_chars.push(t_font_char);
1003
+ this._font_chars_dic['9679'] = t_font_char;
1004
+ return t_font_char;
1005
+ }
1006
+
1007
+ private getCharOpenType (name: string): TesselatedFontChar {
1008
+ if (!this._opentype_font) {
1009
+ return null;
1010
+ }
1011
+ const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
1012
+
1013
+ if (!thisGlyph) {
1014
+ return null;
1015
+ }
1016
+ //console.log("got the glyph from opentype");
1017
+ const thisPath = thisGlyph.getPath();
1018
+ const awayPath: GraphicsPath = new GraphicsPath();
1019
+ let i = 0;
1020
+ const len = thisPath.commands.length;
1021
+
1022
+ //awayPath.lineTo(0, 0);
1023
+ //awayPath.moveTo(0,0);//-100);
1024
+ //awayPath.curveTo(100, 250, 200,0);
1025
+ //awayPath.lineTo(150, 100);
1026
+ //awayPath.moveTo(0,20);
1027
+ //awayPath.curveTo(100, 270, 200,20);
1028
+ //awayPath.moveTo(0,-20);
1029
+ //awayPath.moveTo(0,-10);
1030
+ //awayPath.curveTo(100, -110, 200,-10);
1031
+
1032
+ let startx: number = 0;
1033
+ let starty: number = 0;
1034
+ const y_offset = this._ascent;
1035
+ //40 = 66
1036
+ const scale = (this._opentype_font.unitsPerEm / 72);
1037
+ for (i = 0; i < len; i++) {
1038
+ const cmd = thisPath.commands[i];
1039
+ //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
1040
+ if (cmd.type === 'M') {
1041
+ awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
1042
+ startx = scale * cmd.x;
1043
+ starty = scale * cmd.y + y_offset;
1044
+ } else if (cmd.type === 'L') {
1045
+ awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
1046
+ } else if (cmd.type === 'Q') {
1047
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1048
+ scale * cmd.x, scale * cmd.y + y_offset);
1049
+ } else if (cmd.type === 'C') {
1050
+ const mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
1051
+ const mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
1052
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1053
+ scale * mergedX, scale * mergedY + y_offset);
1054
+ awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset,
1055
+ scale * cmd.x, scale * cmd.y + y_offset);
1056
+ } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
1057
+ }
1058
+
1059
+ const t_font_char = new TesselatedFontChar(null, null, awayPath);
1060
+ t_font_char.char_width = thisGlyph.advanceWidth;//(1 / thisGlyph.path.unitsPerEm * 72);
1061
+ t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1062
+ awayPath,
1063
+ true,
1064
+ null,
1065
+ scale * Settings.FONT_TESSELATION_QUALITY
1066
+ );
1067
+ t_font_char.lastTesselatedScale = scale;
1068
+
1069
+ if (!t_font_char.fill_data) {
1070
+ if (once(this, 'tess' + name)) {
1071
+ console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:',
1072
+ name.charCodeAt(0));
1073
+ }
1074
+ return null;
1075
+ }
1076
+
1077
+ this._font_chars.push(t_font_char);
1078
+ this._font_chars_dic[name] = t_font_char;
1079
+ }
1080
+
1081
+ public getChar(name: string | number, tesselation = true): TesselatedFontChar {
1082
+ name = '' + name;
1083
+
1084
+ const fontChar: TesselatedFontChar = this._font_chars_dic[name];
1085
+
1086
+ if (!tesselation && fontChar) {
1087
+ return fontChar;
1088
+ }
1089
+
1090
+ if (!fontChar) {
1091
+ if (this._opentype_font) {
1092
+ return this.getCharOpenType(name);
1093
+ }
1094
+
1095
+ if (name == '9679') {
1096
+ return this.createPointGlyph_9679();
1097
+ }
1098
+
1099
+ return null;
1100
+ }
1101
+
1102
+ // tesselation pass
1103
+ const scale = this._size_multiply;
1104
+ const qualityStepScale = Math.max(0.01, Math.round(scale * 100) / 100);
1105
+
1106
+ if (fontChar.fill_data_path === null) {
1107
+ return null;
1108
+ }
1109
+
1110
+ // we already has buffer with tesselation scale ratio
1111
+ if (!(fontChar.fill_data == null && fontChar.stroke_data == null)
1112
+ && fontChar.lastTesselatedScale >= qualityStepScale
1113
+ ) {
1114
+ return fontChar;
1115
+ }
1116
+
1117
+ const path = fontChar.fill_data_path;
1118
+
1119
+ // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
1120
+ // change the new first command to moveTo
1121
+ if (path.commands[0][0] == 1 && path.data[0][0] == 0 && path.data[0][1] == 0) {
1122
+ path.data[0].shift();
1123
+ path.data[0].shift();
1124
+ path.commands[0].shift();
1125
+ path.commands[0][0] = 2;
1126
+ }
1127
+
1128
+ if (fontChar.fill_data) {
1129
+ // it not have dispose, but clear will drop abstraction if it exist
1130
+ fontChar.fill_data.clear();
1131
+ }
1132
+
1133
+ fontChar.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1134
+ fontChar.fill_data_path,
1135
+ true,
1136
+ null,
1137
+ qualityStepScale * Settings.FONT_TESSELATION_QUALITY
1138
+ );
1139
+
1140
+ if (fontChar.lastTesselatedScale > 0) {
1141
+ console.debug(
1142
+ '[TesselatedFontTable] Retesselate char from scale:',
1143
+ fontChar.lastTesselatedScale, qualityStepScale, String.fromCharCode(+name)
1144
+ );
1145
+ }
1146
+
1147
+ fontChar.lastTesselatedScale = qualityStepScale;
1148
+
1149
+ if (!fontChar.fill_data) {
1150
+ if (once(this, 'tess' + name)) {
1151
+ console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
1152
+ }
1153
+ return null;
1154
+ }
1155
+
1156
+ return fontChar;
1157
+ }
1158
+
1159
+ public setChar(
1160
+ name: string, char_width: number, fills_data: AttributesBuffer = null,
1161
+ stroke_data: AttributesBuffer = null, uses_curves: boolean = false,
1162
+ glyph_idx: number = 0, fill_data_path: GraphicsPath = null): void {
1163
+
1164
+ char_width = Math.floor(char_width * 20) / 20;
1165
+ //console.log("adding char", name, String.fromCharCode(parseInt(name)));
1166
+ if ((fills_data == null) && (stroke_data == null) && (fill_data_path == null))
1167
+ throw (`TesselatedFontTable: trying to create a TesselatedFontChar with no data
1168
+ (fills_data, stroke_data and fill_data_path is null)`);
1169
+ if (this._font_chars.length > 0) {
1170
+ if (uses_curves != this._usesCurves) {
1171
+ throw (`TesselatedFontTable: Can not set different types of graphic-glyphs
1172
+ (curves vs non-cuves) on the same FontTable!`);
1173
+ }
1174
+ } else {
1175
+ this._usesCurves = uses_curves;
1176
+ }
1177
+ const t_font_char: TesselatedFontChar = new TesselatedFontChar(fills_data, stroke_data, fill_data_path);
1178
+ t_font_char.char_width = char_width;
1179
+ t_font_char.glyph_idx = glyph_idx;
1180
+ t_font_char.name = name;
1181
+ this._glyphIdxToChar[glyph_idx] = t_font_char;
1182
+
1183
+ this._font_chars.push(t_font_char);
1184
+ this._font_chars_dic[name] = t_font_char;
1185
+ }
1186
+
1187
+ private roundTo(val: number) {
1188
+ const p = Settings.TEXT_SHAPE_ROUND_PRECISION;
1189
+ const invP = 1 / (p || 1);
1190
+
1191
+ return Math.floor(val * invP) * p;
1192
+ }
1193
+
1194
+ private _fillBuffer(
1195
+ buffer: Float32Array,
1196
+ chars: ICharEntry[],
1197
+ selection: boolean = false
1198
+ ): number {
1199
+
1200
+ const scale = this._size_multiply;
1201
+
1202
+ let offset = 0;
1203
+ for (const entry of chars) {
1204
+ if (entry.selected !== selection) {
1205
+ continue;
1206
+ }
1207
+
1208
+ const { x, y, char } = entry;
1209
+ const view = new Float32Array(char.fill_data.buffer);
1210
+ const count = view.length;
1211
+
1212
+ for (let v = 0; v < count; v += 2) {
1213
+ buffer[offset + v + 0] = this.roundTo(view[v + 0] * scale + x);
1214
+ buffer[offset + v + 1] = this.roundTo(view[v + 1] * scale + y);
1215
+ }
1216
+
1217
+ offset += view.length;
1218
+ }
1219
+
1220
+ return offset;
1221
+ }
1222
1222
  }