@awayjs/scene 0.13.324 → 0.13.326

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