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