@awayjs/scene 0.13.325 → 0.13.327

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