@awayjs/scene 0.13.265 → 0.13.266

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