@awayjs/scene 0.13.226 → 0.13.235
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/bundle/awayjs-scene.umd.js +885 -399
- package/bundle/awayjs-scene.umd.js.map +1 -1
- package/bundle/awayjs-scene.umd.min.js.map +1 -1
- package/bundle/awayjs-scene.umd.min.js.map.map +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/Scene.d.ts.map +1 -1
- package/dist/lib/Scene.js +6 -6
- package/dist/lib/Settings.d.ts +3 -0
- package/dist/lib/Settings.d.ts.map +1 -1
- package/dist/lib/Settings.js +17 -2
- package/dist/lib/display/TextField.d.ts +36 -5
- package/dist/lib/display/TextField.d.ts.map +1 -1
- package/dist/lib/display/TextField.js +227 -138
- package/dist/lib/image/SceneImage2D.d.ts +1 -0
- package/dist/lib/image/SceneImage2D.d.ts.map +1 -1
- package/dist/lib/image/SceneImage2D.js +45 -13
- package/dist/lib/text/FNTGenerator.d.ts +5 -4
- package/dist/lib/text/FNTGenerator.d.ts.map +1 -1
- package/dist/lib/text/FNTGenerator.js +18 -13
- package/dist/lib/text/FNTGeneratorCanvas.d.ts +23 -0
- package/dist/lib/text/FNTGeneratorCanvas.d.ts.map +1 -0
- package/dist/lib/text/FNTGeneratorCanvas.js +205 -0
- package/dist/lib/text/FNTGeneratrorBase.d.ts +8 -0
- package/dist/lib/text/FNTGeneratrorBase.d.ts.map +1 -0
- package/dist/lib/text/FNTGeneratrorBase.js +7 -0
- package/dist/lib/text/TesselatedFontChar.d.ts +6 -4
- package/dist/lib/text/TesselatedFontChar.d.ts.map +1 -1
- package/dist/lib/text/TesselatedFontChar.js +8 -3
- package/dist/lib/text/TesselatedFontTable.d.ts +19 -3
- package/dist/lib/text/TesselatedFontTable.d.ts.map +1 -1
- package/dist/lib/text/TesselatedFontTable.js +339 -225
- package/dist/lib/text/TextShape.d.ts +6 -3
- package/dist/lib/text/TextShape.d.ts.map +1 -1
- package/dist/lib/text/TextShape.js +27 -2
- package/index.ts +1 -1
- package/lib/Scene.ts +5 -6
- package/lib/Settings.ts +23 -3
- package/lib/display/TextField.ts +317 -145
- package/lib/image/SceneImage2D.ts +103 -14
- package/lib/text/FNTGenerator.ts +10 -11
- package/lib/text/FNTGeneratorCanvas.ts +288 -0
- package/lib/text/FNTGeneratrorBase.ts +11 -0
- package/lib/text/TesselatedFontChar.ts +8 -12
- package/lib/text/TesselatedFontTable.ts +438 -255
- package/lib/text/TextShape.ts +30 -3
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { BasicPartition, ContainerNode,
|
|
2
|
+
import { BasicPartition, ContainerNode, View } from '@awayjs/view';
|
|
3
3
|
import { MaterialBase, MethodMaterial } from '@awayjs/materials';
|
|
4
4
|
import { DisplayObjectContainer } from '../display/DisplayObjectContainer';
|
|
5
5
|
import { DisplayObject } from '../display/DisplayObject';
|
|
@@ -275,11 +275,12 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
275
275
|
projection.originY = 1;
|
|
276
276
|
|
|
277
277
|
//create the view
|
|
278
|
+
const view = new View(projection, this._stage, null, null, null, true);
|
|
278
279
|
SceneImage2D._root = new DisplayObjectContainer();
|
|
279
|
-
SceneImage2D._rootNode =
|
|
280
|
+
SceneImage2D._rootNode = view.getNode(SceneImage2D._root);
|
|
280
281
|
SceneImage2D._renderer =
|
|
281
282
|
<DefaultRenderer> RenderGroup
|
|
282
|
-
.getInstance(
|
|
283
|
+
.getInstance(DefaultRenderer)
|
|
283
284
|
.getRenderer(SceneImage2D._rootNode.partition);
|
|
284
285
|
|
|
285
286
|
//SceneImage2D._root.partition = SceneImage2D._renderer.partition;
|
|
@@ -288,6 +289,7 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
288
289
|
//setup the projection
|
|
289
290
|
SceneImage2D._renderer.disableClear = true;
|
|
290
291
|
SceneImage2D._renderer.view.backgroundAlpha = 0;
|
|
292
|
+
SceneImage2D._renderer.view.backgroundColor = 0x0;
|
|
291
293
|
SceneImage2D._renderer.view.projection = projection;
|
|
292
294
|
SceneImage2D._renderer.view.projection.transform = new Transform();
|
|
293
295
|
SceneImage2D._renderer.view.projection.transform.scaleTo(1, -1, 1);
|
|
@@ -305,10 +307,11 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
305
307
|
projection.originY = 1;
|
|
306
308
|
|
|
307
309
|
//create the view
|
|
310
|
+
const view = new View(projection, this._stage, null, null, null, true);
|
|
308
311
|
SceneImage2D._billboardRoot = new DisplayObjectContainer();
|
|
309
312
|
SceneImage2D._billboardRenderer = <DefaultRenderer> RenderGroup
|
|
310
|
-
.getInstance(
|
|
311
|
-
.getRenderer(
|
|
313
|
+
.getInstance(DefaultRenderer)
|
|
314
|
+
.getRenderer(view.getNode(SceneImage2D._billboardRoot).partition);
|
|
312
315
|
|
|
313
316
|
//SceneImage2D._billboardRoot.partition = SceneImage2D._billboardRenderer.partition;
|
|
314
317
|
|
|
@@ -316,6 +319,7 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
316
319
|
//setup the projection
|
|
317
320
|
SceneImage2D._billboardRenderer.disableClear = true;
|
|
318
321
|
SceneImage2D._billboardRenderer.view.backgroundAlpha = 0;
|
|
322
|
+
SceneImage2D._billboardRenderer.view.backgroundColor = 0x0;
|
|
319
323
|
SceneImage2D._billboardRenderer.view.projection = projection;
|
|
320
324
|
SceneImage2D._billboardRenderer.view.projection.transform = new Transform();
|
|
321
325
|
SceneImage2D._billboardRenderer.view.projection.transform.moveTo(0, 0, -1000);
|
|
@@ -505,13 +509,60 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
505
509
|
this._initalFillColor = null;
|
|
506
510
|
}
|
|
507
511
|
|
|
508
|
-
|
|
512
|
+
let compositeSource: Image2D = source;
|
|
513
|
+
|
|
514
|
+
// merge alpha with source
|
|
515
|
+
if (alphaBitmapData) {
|
|
516
|
+
compositeSource = this._stage.filterManager.popTemp(source.width, source.height);
|
|
517
|
+
|
|
518
|
+
//this._stage.filterManager.copyPixels(source, compositeSource, source.rect, source.rect.topLeft);
|
|
519
|
+
this._stage.filterManager.copyPixels(
|
|
520
|
+
alphaBitmapData,
|
|
521
|
+
compositeSource,
|
|
522
|
+
new Rectangle(
|
|
523
|
+
alphaPoint.x,
|
|
524
|
+
alphaPoint.y,
|
|
525
|
+
sourceRect.width,
|
|
526
|
+
sourceRect.height
|
|
527
|
+
),
|
|
528
|
+
new Point(0,0)
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
this._stage.filterManager.copyPixels(
|
|
532
|
+
source,
|
|
533
|
+
compositeSource,
|
|
534
|
+
source.rect,
|
|
535
|
+
new Point(0,0),
|
|
536
|
+
true,
|
|
537
|
+
'alpha_back'
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
this._stage.filterManager.copyPixels(
|
|
542
|
+
compositeSource,
|
|
543
|
+
this,
|
|
544
|
+
sourceRect,
|
|
545
|
+
destPoint,
|
|
546
|
+
mergeAlpha
|
|
547
|
+
);
|
|
548
|
+
|
|
549
|
+
if (alphaBitmapData) {
|
|
550
|
+
this._stage.filterManager.pushTemp(compositeSource);
|
|
551
|
+
}
|
|
552
|
+
|
|
509
553
|
this._imageDataDirty = true;
|
|
510
554
|
}
|
|
511
555
|
|
|
512
556
|
public threshold(
|
|
513
|
-
source: BitmapImage2D,
|
|
514
|
-
|
|
557
|
+
source: BitmapImage2D,
|
|
558
|
+
sourceRect: Rectangle,
|
|
559
|
+
destPoint: Point,
|
|
560
|
+
operation: string,
|
|
561
|
+
threshold: number,
|
|
562
|
+
color: number,
|
|
563
|
+
mask: number,
|
|
564
|
+
copySource: boolean
|
|
565
|
+
): void {
|
|
515
566
|
|
|
516
567
|
this._lastUsedFill = null;
|
|
517
568
|
this.dropAllReferences();
|
|
@@ -746,20 +797,28 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
746
797
|
this._initalFillColor = null;
|
|
747
798
|
}
|
|
748
799
|
|
|
800
|
+
const stage = this._stage;
|
|
801
|
+
const mappedBlend = SceneImage2D._mapSupportedBlendMode(blendMode);
|
|
802
|
+
const supportNativeBlend = !blendMode || mappedBlend !== BlendMode.LAYER || blendMode == BlendMode.LAYER;
|
|
803
|
+
const useTmp = (!supportNativeBlend || this === source);
|
|
804
|
+
const target = useTmp ?
|
|
805
|
+
stage.filterManager.popTemp(this.width, this.height, false)
|
|
806
|
+
: this;
|
|
807
|
+
|
|
749
808
|
const renderer = SceneImage2D._billboardRenderer;
|
|
750
809
|
const root = SceneImage2D._billboardRoot;
|
|
751
810
|
const billboard = SceneImage2D._billboard;
|
|
752
811
|
|
|
753
812
|
billboard.sampler.smooth = smoothing;
|
|
754
813
|
|
|
755
|
-
renderer.disableClear =
|
|
756
|
-
renderer.view.target =
|
|
757
|
-
renderer.view.projection.scale = 1000 /
|
|
814
|
+
renderer.disableClear = !useTmp;
|
|
815
|
+
renderer.view.target = target;
|
|
816
|
+
renderer.view.projection.scale = 1000 / target.height;
|
|
758
817
|
|
|
759
818
|
billboard.material.style.image = source;
|
|
760
819
|
|
|
761
820
|
// not all blend modes can be used for rendering
|
|
762
|
-
billboard.material.blendMode = SceneImage2D._mapSupportedBlendMode(blendMode);
|
|
821
|
+
billboard.material.blendMode = !useTmp ? SceneImage2D._mapSupportedBlendMode(blendMode) : BlendMode.LAYER;
|
|
763
822
|
|
|
764
823
|
(<MaterialBase> billboard.material).useColorTransform = !!colorTransform;
|
|
765
824
|
|
|
@@ -773,20 +832,35 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
773
832
|
m._rawData[4] = matrix.c;
|
|
774
833
|
m._rawData[5] = -matrix.d;
|
|
775
834
|
m._rawData[12] = matrix.tx;
|
|
776
|
-
m._rawData[13] =
|
|
835
|
+
m._rawData[13] = target.height - matrix.ty;
|
|
777
836
|
|
|
778
837
|
root.transform.invalidateComponents();
|
|
779
838
|
} else {
|
|
780
839
|
|
|
781
840
|
root.transform.rotateTo(0,0,0);
|
|
782
841
|
root.transform.scaleTo(1, -1, 1);
|
|
783
|
-
root.transform.moveTo(0,
|
|
842
|
+
root.transform.moveTo(0, target.height,0);
|
|
784
843
|
}
|
|
785
844
|
|
|
786
845
|
root.transform.colorTransform = colorTransform;
|
|
787
846
|
|
|
788
847
|
//render
|
|
789
848
|
renderer.render();
|
|
849
|
+
|
|
850
|
+
//
|
|
851
|
+
if (useTmp) {
|
|
852
|
+
stage.filterManager.copyPixels(
|
|
853
|
+
target,
|
|
854
|
+
this,
|
|
855
|
+
this.rect,
|
|
856
|
+
TMP_POINT,
|
|
857
|
+
true,
|
|
858
|
+
blendMode
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
stage.filterManager.pushTemp(target);
|
|
862
|
+
}
|
|
863
|
+
|
|
790
864
|
}
|
|
791
865
|
|
|
792
866
|
private _drawAsDisplay(
|
|
@@ -922,6 +996,21 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
922
996
|
}
|
|
923
997
|
|
|
924
998
|
public reset() {}
|
|
999
|
+
|
|
1000
|
+
public clone(): SceneImage2D {
|
|
1001
|
+
const image = SceneImage2D.getImage(
|
|
1002
|
+
this.width,
|
|
1003
|
+
this.height,
|
|
1004
|
+
this.transparent,
|
|
1005
|
+
null,
|
|
1006
|
+
false,
|
|
1007
|
+
this._stage,
|
|
1008
|
+
false
|
|
1009
|
+
);
|
|
1010
|
+
|
|
1011
|
+
image.deepClone(this);
|
|
1012
|
+
return image;
|
|
1013
|
+
}
|
|
925
1014
|
}
|
|
926
1015
|
|
|
927
1016
|
Stage.registerAbstraction(_Stage_BitmapImage2D, SceneImage2D);
|
package/lib/text/FNTGenerator.ts
CHANGED
|
@@ -7,18 +7,17 @@ import { Stage, BitmapImage2D } from '@awayjs/stage';
|
|
|
7
7
|
import { DefaultRenderer, RenderGroup } from '@awayjs/renderer';
|
|
8
8
|
|
|
9
9
|
import { DisplayObjectContainer } from '../display/DisplayObjectContainer';
|
|
10
|
-
|
|
11
|
-
import { BasicPartition, NodePool, View } from '@awayjs/view';
|
|
10
|
+
import { View } from '@awayjs/view';
|
|
12
11
|
import { Sprite } from '../display/Sprite';
|
|
13
12
|
import { Shape } from '@awayjs/graphics';
|
|
13
|
+
import { FNTGeneratorBase } from './FNTGeneratrorBase';
|
|
14
14
|
|
|
15
|
-
export class FNTGenerator {
|
|
15
|
+
export class FNTGenerator extends FNTGeneratorBase {
|
|
16
16
|
private _root: DisplayObjectContainer;
|
|
17
17
|
private _renderer: DefaultRenderer;
|
|
18
|
-
private _stage: Stage;
|
|
19
18
|
|
|
20
|
-
constructor(stage) {
|
|
21
|
-
|
|
19
|
+
constructor(stage: Stage) {
|
|
20
|
+
super (stage);
|
|
22
21
|
|
|
23
22
|
//create the projection
|
|
24
23
|
const projection = new PerspectiveProjection();
|
|
@@ -28,9 +27,10 @@ export class FNTGenerator {
|
|
|
28
27
|
|
|
29
28
|
//create the view
|
|
30
29
|
this._root = new DisplayObjectContainer();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.
|
|
30
|
+
const view = new View(null, stage, null, null, null, true);
|
|
31
|
+
this._renderer = <DefaultRenderer> RenderGroup
|
|
32
|
+
.getInstance(DefaultRenderer)
|
|
33
|
+
.getRenderer(view.getNode(this._root).partition);
|
|
34
34
|
|
|
35
35
|
//setup the projection
|
|
36
36
|
this._renderer.view.projection = projection;
|
|
@@ -39,10 +39,9 @@ export class FNTGenerator {
|
|
|
39
39
|
this._renderer.view.projection.transform.lookAt(new Vector3D());
|
|
40
40
|
|
|
41
41
|
this._renderer.renderableSorter = null;//new RenderableSort2D();
|
|
42
|
-
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
public generate(font: Font, maxSize: number, fontSize: number, padding: number):
|
|
44
|
+
public generate(font: Font, maxSize: number, fontSize: number, padding: number): BitmapImage2D[] {
|
|
46
45
|
let outputBitmap: BitmapImage2D;
|
|
47
46
|
const outputBitmaps: BitmapImage2D[] = [];
|
|
48
47
|
let mipSize: number;
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { FNTGeneratorBase } from './FNTGeneratrorBase';
|
|
2
|
+
import { Font } from './Font';
|
|
3
|
+
import { BitmapImage2D, Stage } from '@awayjs/stage';
|
|
4
|
+
import { Rectangle } from '@awayjs/core';
|
|
5
|
+
import { GraphicsPathCommand } from '@awayjs/graphics';
|
|
6
|
+
import { TesselatedFontTable } from './TesselatedFontTable';
|
|
7
|
+
import { TesselatedFontChar } from './TesselatedFontChar';
|
|
8
|
+
|
|
9
|
+
class Bounds {
|
|
10
|
+
left: number = Number.MAX_VALUE;
|
|
11
|
+
right: number = Number.MIN_VALUE;
|
|
12
|
+
top: number = Number.MAX_VALUE;
|
|
13
|
+
bottom: number = Number.MIN_VALUE;
|
|
14
|
+
|
|
15
|
+
grown (x: number, y: number) {
|
|
16
|
+
if (x < this.left)
|
|
17
|
+
this.left = x;
|
|
18
|
+
|
|
19
|
+
if (x > this.right)
|
|
20
|
+
this.right = x;
|
|
21
|
+
|
|
22
|
+
if (y < this.top)
|
|
23
|
+
this.top = y;
|
|
24
|
+
|
|
25
|
+
if (y > this.bottom)
|
|
26
|
+
this.bottom = y;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
toRect(): Rectangle {
|
|
30
|
+
return new Rectangle(this.left, this.top, this.right - this.left, this.bottom - this.top);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface IPathContainer {
|
|
35
|
+
path: Path2D;
|
|
36
|
+
rect: Rectangle;
|
|
37
|
+
char?: TesselatedFontChar;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class FNTGeneratorCanvas extends FNTGeneratorBase {
|
|
41
|
+
private static getCanvasContext (
|
|
42
|
+
width: number = 100,
|
|
43
|
+
height: number = 100,
|
|
44
|
+
debug: boolean = false
|
|
45
|
+
): CanvasRenderingContext2D {
|
|
46
|
+
const canvas = ('OffscreenCanvas' in self && !debug)
|
|
47
|
+
? new self.OffscreenCanvas(width, height)
|
|
48
|
+
: document.createElement('canvas');
|
|
49
|
+
|
|
50
|
+
canvas.width = width;
|
|
51
|
+
canvas.height = height;
|
|
52
|
+
|
|
53
|
+
return canvas.getContext('2d', { desynchronized: true, alpha: true }) as CanvasRenderingContext2D;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private static freeCanvasContext (ctx: CanvasRenderingContext2D) {
|
|
57
|
+
if (ctx.canvas instanceof self.OffscreenCanvas) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// safari bug with canvas leaks
|
|
62
|
+
ctx.canvas.width = 0;
|
|
63
|
+
ctx.canvas.height = 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private static transformChar (char: TesselatedFontChar): IPathContainer {
|
|
67
|
+
const fillData = char.fill_data_path;
|
|
68
|
+
|
|
69
|
+
// empty command
|
|
70
|
+
if (fillData.commands[0].length === 0 && fillData.commands.length === 1) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const path = new Path2D();
|
|
75
|
+
const box = new Bounds();
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < fillData.commands.length; i++) {
|
|
78
|
+
const data = fillData.data[i];
|
|
79
|
+
const commands = fillData.commands[i];
|
|
80
|
+
|
|
81
|
+
let pointer = 0;
|
|
82
|
+
for (const command of commands) {
|
|
83
|
+
switch (command) {
|
|
84
|
+
case GraphicsPathCommand.MOVE_TO: {
|
|
85
|
+
// flash path in twips, transform it
|
|
86
|
+
const px = data[pointer++] / 20;
|
|
87
|
+
const py = data[pointer++] / 20;
|
|
88
|
+
|
|
89
|
+
box.grown(px, py);
|
|
90
|
+
path.moveTo(px, py);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
case GraphicsPathCommand.LINE_TO: {
|
|
95
|
+
const px = data[pointer++] / 20;
|
|
96
|
+
const py = data[pointer++] / 20;
|
|
97
|
+
|
|
98
|
+
box.grown(px, py);
|
|
99
|
+
path.lineTo(px, py);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case GraphicsPathCommand.CURVE_TO: {
|
|
104
|
+
const cx = data[pointer++] / 20;
|
|
105
|
+
const cy = data[pointer++] / 20;
|
|
106
|
+
const dx = data[pointer++] / 20;
|
|
107
|
+
const dy = data[pointer++] / 20;
|
|
108
|
+
|
|
109
|
+
box.grown(cx, cy);
|
|
110
|
+
box.grown(dx, dy);
|
|
111
|
+
|
|
112
|
+
path.quadraticCurveTo(
|
|
113
|
+
cx, cy,
|
|
114
|
+
dx, dy
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//path.closePath();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
path,
|
|
124
|
+
char,
|
|
125
|
+
rect: box.toRect(),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private static sortPatches (a: IPathContainer, b: IPathContainer): number {
|
|
130
|
+
return a.rect.height - b.rect.height;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private static render(
|
|
134
|
+
table: TesselatedFontTable,
|
|
135
|
+
context: CanvasRenderingContext2D,
|
|
136
|
+
size: number,
|
|
137
|
+
fontSize: number = 0,
|
|
138
|
+
padding: number = 0
|
|
139
|
+
): void {
|
|
140
|
+
const chars = table.get_font_chars();
|
|
141
|
+
const patches: IPathContainer[] = [];
|
|
142
|
+
const doublePad = padding * 2;
|
|
143
|
+
|
|
144
|
+
let totalArea: number = 0;
|
|
145
|
+
let scale: number = 1;
|
|
146
|
+
|
|
147
|
+
for (const char of chars) {
|
|
148
|
+
const path = FNTGeneratorCanvas.transformChar(char);
|
|
149
|
+
|
|
150
|
+
if (!path) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
totalArea += (doublePad + path.rect.width) * (doublePad + path.rect.height);
|
|
155
|
+
|
|
156
|
+
patches.push(path);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
patches.sort(FNTGeneratorCanvas.sortPatches);
|
|
160
|
+
|
|
161
|
+
// TODO compute valid scale when fontSize is 0
|
|
162
|
+
if (fontSize) {
|
|
163
|
+
table.initFontSize(fontSize);
|
|
164
|
+
scale = /* 20 * table._size_multiply **/ size / table.get_font_em_size();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Render glyphs onto canvas
|
|
169
|
+
* We will render glyphs as white and background as black transparent
|
|
170
|
+
*/
|
|
171
|
+
context.save();
|
|
172
|
+
context.clearRect(0, 0, size, size);
|
|
173
|
+
context.fillStyle = '#fff';
|
|
174
|
+
|
|
175
|
+
let tx = padding;
|
|
176
|
+
let ty = padding;
|
|
177
|
+
let maxHeight = 0;
|
|
178
|
+
|
|
179
|
+
// pack glyph based on size
|
|
180
|
+
for (const path of patches) {
|
|
181
|
+
if (!path.path) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// if overflow - jump next location
|
|
186
|
+
if (tx + path.rect.width + doublePad >= size / scale) {
|
|
187
|
+
tx = padding;
|
|
188
|
+
ty += maxHeight + doublePad;
|
|
189
|
+
maxHeight = 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @todo Support multiple channel table data
|
|
194
|
+
*/
|
|
195
|
+
if (ty + path.rect.height > size / scale) {
|
|
196
|
+
console.warn('[FNTGeneratorCanvas] Font image overflow, font will corrupted!');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
maxHeight = Math.max(path.rect.height, maxHeight);
|
|
200
|
+
|
|
201
|
+
context.setTransform(
|
|
202
|
+
scale, 0, 0, scale,
|
|
203
|
+
(tx - path.rect.x) * scale,
|
|
204
|
+
(ty - path.rect.y) * scale
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
context.fill(path.path);
|
|
208
|
+
//context.strokeRect(path.rect.x,path.rect.y, path.rect.width, path.rect.height);
|
|
209
|
+
|
|
210
|
+
path.char.fnt_rect = new Rectangle(
|
|
211
|
+
(path.rect.x) / path.char.char_width,
|
|
212
|
+
(path.rect.y) / table.get_font_em_size(),
|
|
213
|
+
path.rect.width / path.char.char_width,
|
|
214
|
+
path.rect.height / table.get_font_em_size()
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
path.char.fnt_uv = new Rectangle(
|
|
218
|
+
tx * scale / size,
|
|
219
|
+
ty * scale / size,
|
|
220
|
+
path.rect.width * scale / size,
|
|
221
|
+
path.rect.height * scale / size);
|
|
222
|
+
/*
|
|
223
|
+
path.char.fnt_uv = new Rectangle(
|
|
224
|
+
(tx - path.rect.x) * scale / size,
|
|
225
|
+
1 - (ty - path.rect.y) * scale / scale,
|
|
226
|
+
path.rect.width * scale / size,
|
|
227
|
+
path.rect.height * scale / size
|
|
228
|
+
);*/
|
|
229
|
+
|
|
230
|
+
path.char.fnt_channel = 0;
|
|
231
|
+
|
|
232
|
+
tx += path.rect.width + doublePad;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
context.restore();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Debug mode.
|
|
240
|
+
* Prevent OffscreenCanvas and prevent canvas clear.
|
|
241
|
+
* Store canvas
|
|
242
|
+
*/
|
|
243
|
+
public debug: boolean = false;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Store canvas used for caching. Only in debug mode
|
|
247
|
+
*/
|
|
248
|
+
public canvas: HTMLCanvasElement;
|
|
249
|
+
|
|
250
|
+
constructor(stage: Stage, debug = false) {
|
|
251
|
+
super(stage);
|
|
252
|
+
|
|
253
|
+
this.debug = debug;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public generate(font: Font, size: number, fontSize: number, padding: number): BitmapImage2D[] {
|
|
257
|
+
const context = FNTGeneratorCanvas.getCanvasContext(size, size, this.debug);
|
|
258
|
+
const bitmaps = [];
|
|
259
|
+
|
|
260
|
+
for (const key in font.font_styles) {
|
|
261
|
+
const table = <TesselatedFontTable> font.font_styles[key];
|
|
262
|
+
// render table in context
|
|
263
|
+
FNTGeneratorCanvas.render(
|
|
264
|
+
table,
|
|
265
|
+
context,
|
|
266
|
+
size,
|
|
267
|
+
fontSize,
|
|
268
|
+
padding
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const data = context.getImageData(0, 0, size, size);
|
|
272
|
+
const image = new BitmapImage2D(size, size, true, 0, true, this._stage);
|
|
273
|
+
|
|
274
|
+
image.setPixels(new Rectangle(0, 0, size, size), data.data);
|
|
275
|
+
|
|
276
|
+
bitmaps.push(image);
|
|
277
|
+
table.addFNTChannel(image);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (!this.debug) {
|
|
281
|
+
FNTGeneratorCanvas.freeCanvasContext(context);
|
|
282
|
+
} else {
|
|
283
|
+
this.canvas = context.canvas;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return bitmaps;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Stage, BitmapImage2D } from '@awayjs/stage';
|
|
2
|
+
import { Font } from './Font';
|
|
3
|
+
|
|
4
|
+
export abstract class FNTGeneratorBase {
|
|
5
|
+
constructor (
|
|
6
|
+
protected _stage: Stage
|
|
7
|
+
) {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public abstract generate(font: Font, maxSize: number, fontSize: number, padding: number): BitmapImage2D[];
|
|
11
|
+
}
|
|
@@ -39,14 +39,13 @@ export class TesselatedFontChar {
|
|
|
39
39
|
|
|
40
40
|
public fnt_rect: Rectangle;
|
|
41
41
|
public fnt_uv: Rectangle;
|
|
42
|
-
public fnt_channel: number=0;
|
|
42
|
+
public fnt_channel: number = 0;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* What is scale was used for generating last tesselation
|
|
46
|
+
* Used for reduce a vertices count
|
|
46
47
|
*/
|
|
47
|
-
public
|
|
48
|
-
public fill_data: AttributesBuffer;
|
|
49
|
-
public fill_data_path: GraphicsPath;
|
|
48
|
+
public lastTesselatedScale: number = 0;
|
|
50
49
|
|
|
51
50
|
/**
|
|
52
51
|
* the char_codes that this geom has kerning set for
|
|
@@ -58,13 +57,10 @@ export class TesselatedFontChar {
|
|
|
58
57
|
public kerningValues: Array<number>=[];
|
|
59
58
|
|
|
60
59
|
constructor(
|
|
61
|
-
fill_data: AttributesBuffer = null,
|
|
62
|
-
stroke_data: AttributesBuffer = null,
|
|
63
|
-
fill_data_path: GraphicsPath = null
|
|
64
|
-
|
|
65
|
-
this.fill_data = fill_data;
|
|
66
|
-
this.stroke_data = stroke_data;
|
|
67
|
-
this.fill_data_path = fill_data_path;
|
|
60
|
+
public fill_data: AttributesBuffer = null,
|
|
61
|
+
public stroke_data: AttributesBuffer = null,
|
|
62
|
+
public fill_data_path: GraphicsPath = null
|
|
63
|
+
) {
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
public dispose() {
|