@awayjs/scene 0.13.223 → 0.13.232
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 +879 -417
- 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/Settings.d.ts +3 -0
- package/dist/lib/Settings.d.ts.map +1 -1
- package/dist/lib/Settings.js +14 -0
- package/dist/lib/display/DisplayObjectContainer.d.ts +1 -0
- package/dist/lib/display/DisplayObjectContainer.d.ts.map +1 -1
- package/dist/lib/display/DisplayObjectContainer.js +4 -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.map +1 -1
- package/dist/lib/image/SceneImage2D.js +35 -8
- package/dist/lib/managers/MouseManager.d.ts +1 -1
- package/dist/lib/managers/MouseManager.d.ts.map +1 -1
- package/dist/lib/managers/MouseManager.js +9 -29
- 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 +15 -11
- 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/Settings.ts +20 -1
- package/lib/display/DisplayObjectContainer.ts +4 -2
- package/lib/display/TextField.ts +317 -145
- package/lib/image/SceneImage2D.ts +84 -9
- package/lib/managers/MouseManager.ts +11 -30
- package/lib/text/FNTGenerator.ts +5 -7
- 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
|
@@ -288,6 +288,7 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
288
288
|
//setup the projection
|
|
289
289
|
SceneImage2D._renderer.disableClear = true;
|
|
290
290
|
SceneImage2D._renderer.view.backgroundAlpha = 0;
|
|
291
|
+
SceneImage2D._renderer.view.backgroundColor = 0x0;
|
|
291
292
|
SceneImage2D._renderer.view.projection = projection;
|
|
292
293
|
SceneImage2D._renderer.view.projection.transform = new Transform();
|
|
293
294
|
SceneImage2D._renderer.view.projection.transform.scaleTo(1, -1, 1);
|
|
@@ -316,6 +317,7 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
316
317
|
//setup the projection
|
|
317
318
|
SceneImage2D._billboardRenderer.disableClear = true;
|
|
318
319
|
SceneImage2D._billboardRenderer.view.backgroundAlpha = 0;
|
|
320
|
+
SceneImage2D._billboardRenderer.view.backgroundColor = 0x0;
|
|
319
321
|
SceneImage2D._billboardRenderer.view.projection = projection;
|
|
320
322
|
SceneImage2D._billboardRenderer.view.projection.transform = new Transform();
|
|
321
323
|
SceneImage2D._billboardRenderer.view.projection.transform.moveTo(0, 0, -1000);
|
|
@@ -505,13 +507,60 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
505
507
|
this._initalFillColor = null;
|
|
506
508
|
}
|
|
507
509
|
|
|
508
|
-
|
|
510
|
+
let compositeSource: Image2D = source;
|
|
511
|
+
|
|
512
|
+
// merge alpha with source
|
|
513
|
+
if (alphaBitmapData) {
|
|
514
|
+
compositeSource = this._stage.filterManager.popTemp(source.width, source.height);
|
|
515
|
+
|
|
516
|
+
//this._stage.filterManager.copyPixels(source, compositeSource, source.rect, source.rect.topLeft);
|
|
517
|
+
this._stage.filterManager.copyPixels(
|
|
518
|
+
alphaBitmapData,
|
|
519
|
+
compositeSource,
|
|
520
|
+
new Rectangle(
|
|
521
|
+
alphaPoint.x,
|
|
522
|
+
alphaPoint.y,
|
|
523
|
+
sourceRect.width,
|
|
524
|
+
sourceRect.height
|
|
525
|
+
),
|
|
526
|
+
new Point(0,0)
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
this._stage.filterManager.copyPixels(
|
|
530
|
+
source,
|
|
531
|
+
compositeSource,
|
|
532
|
+
source.rect,
|
|
533
|
+
new Point(0,0),
|
|
534
|
+
true,
|
|
535
|
+
'alpha_back'
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
this._stage.filterManager.copyPixels(
|
|
540
|
+
compositeSource,
|
|
541
|
+
this,
|
|
542
|
+
sourceRect,
|
|
543
|
+
destPoint,
|
|
544
|
+
mergeAlpha
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
if (alphaBitmapData) {
|
|
548
|
+
this._stage.filterManager.pushTemp(compositeSource);
|
|
549
|
+
}
|
|
550
|
+
|
|
509
551
|
this._imageDataDirty = true;
|
|
510
552
|
}
|
|
511
553
|
|
|
512
554
|
public threshold(
|
|
513
|
-
source: BitmapImage2D,
|
|
514
|
-
|
|
555
|
+
source: BitmapImage2D,
|
|
556
|
+
sourceRect: Rectangle,
|
|
557
|
+
destPoint: Point,
|
|
558
|
+
operation: string,
|
|
559
|
+
threshold: number,
|
|
560
|
+
color: number,
|
|
561
|
+
mask: number,
|
|
562
|
+
copySource: boolean
|
|
563
|
+
): void {
|
|
515
564
|
|
|
516
565
|
this._lastUsedFill = null;
|
|
517
566
|
this.dropAllReferences();
|
|
@@ -746,20 +795,28 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
746
795
|
this._initalFillColor = null;
|
|
747
796
|
}
|
|
748
797
|
|
|
798
|
+
const stage = this._stage;
|
|
799
|
+
const mappedBlend = SceneImage2D._mapSupportedBlendMode(blendMode);
|
|
800
|
+
const supportNativeBlend = !blendMode || mappedBlend !== BlendMode.LAYER || blendMode == BlendMode.LAYER;
|
|
801
|
+
const useTmp = (!supportNativeBlend || this === source);
|
|
802
|
+
const target = useTmp ?
|
|
803
|
+
stage.filterManager.popTemp(this.width, this.height, false)
|
|
804
|
+
: this;
|
|
805
|
+
|
|
749
806
|
const renderer = SceneImage2D._billboardRenderer;
|
|
750
807
|
const root = SceneImage2D._billboardRoot;
|
|
751
808
|
const billboard = SceneImage2D._billboard;
|
|
752
809
|
|
|
753
810
|
billboard.sampler.smooth = smoothing;
|
|
754
811
|
|
|
755
|
-
renderer.disableClear =
|
|
756
|
-
renderer.view.target =
|
|
757
|
-
renderer.view.projection.scale = 1000 /
|
|
812
|
+
renderer.disableClear = !useTmp;
|
|
813
|
+
renderer.view.target = target;
|
|
814
|
+
renderer.view.projection.scale = 1000 / target.height;
|
|
758
815
|
|
|
759
816
|
billboard.material.style.image = source;
|
|
760
817
|
|
|
761
818
|
// not all blend modes can be used for rendering
|
|
762
|
-
billboard.material.blendMode = SceneImage2D._mapSupportedBlendMode(blendMode);
|
|
819
|
+
billboard.material.blendMode = !useTmp ? SceneImage2D._mapSupportedBlendMode(blendMode) : BlendMode.LAYER;
|
|
763
820
|
|
|
764
821
|
(<MaterialBase> billboard.material).useColorTransform = !!colorTransform;
|
|
765
822
|
|
|
@@ -773,20 +830,35 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
773
830
|
m._rawData[4] = matrix.c;
|
|
774
831
|
m._rawData[5] = -matrix.d;
|
|
775
832
|
m._rawData[12] = matrix.tx;
|
|
776
|
-
m._rawData[13] =
|
|
833
|
+
m._rawData[13] = target.height - matrix.ty;
|
|
777
834
|
|
|
778
835
|
root.transform.invalidateComponents();
|
|
779
836
|
} else {
|
|
780
837
|
|
|
781
838
|
root.transform.rotateTo(0,0,0);
|
|
782
839
|
root.transform.scaleTo(1, -1, 1);
|
|
783
|
-
root.transform.moveTo(0,
|
|
840
|
+
root.transform.moveTo(0, target.height,0);
|
|
784
841
|
}
|
|
785
842
|
|
|
786
843
|
root.transform.colorTransform = colorTransform;
|
|
787
844
|
|
|
788
845
|
//render
|
|
789
846
|
renderer.render();
|
|
847
|
+
|
|
848
|
+
//
|
|
849
|
+
if (useTmp) {
|
|
850
|
+
stage.filterManager.copyPixels(
|
|
851
|
+
target,
|
|
852
|
+
this,
|
|
853
|
+
this.rect,
|
|
854
|
+
TMP_POINT,
|
|
855
|
+
true,
|
|
856
|
+
blendMode
|
|
857
|
+
);
|
|
858
|
+
|
|
859
|
+
stage.filterManager.pushTemp(target);
|
|
860
|
+
}
|
|
861
|
+
|
|
790
862
|
}
|
|
791
863
|
|
|
792
864
|
private _drawAsDisplay(
|
|
@@ -884,7 +956,10 @@ export class SceneImage2D extends BitmapImage2D {
|
|
|
884
956
|
renderer.view.height = this.height;
|
|
885
957
|
|
|
886
958
|
const sourceNode: ContainerNode = rootNode.addChildAt(source, 0);
|
|
959
|
+
|
|
887
960
|
sourceNode.transformDisabled = true;
|
|
961
|
+
// color transform should be enabled!
|
|
962
|
+
sourceNode.colorTransformDisabled = false;
|
|
888
963
|
|
|
889
964
|
root.transform.colorTransform = colorTransform;
|
|
890
965
|
// anyway we not support this =))
|
|
@@ -5,7 +5,7 @@ import { PickingCollision, RaycastPicker, ContainerNode, IPartitionContainer, En
|
|
|
5
5
|
|
|
6
6
|
import { KeyboardEvent } from '../events/KeyboardEvent';
|
|
7
7
|
import { MouseEvent as AwayMouseEvent } from '../events/MouseEvent';
|
|
8
|
-
import { FrameScriptManager } from '
|
|
8
|
+
import { FrameScriptManager } from './FrameScriptManager';
|
|
9
9
|
|
|
10
10
|
import { IInputRecorder } from './IInputRecorder';
|
|
11
11
|
|
|
@@ -66,9 +66,11 @@ export class MouseManager {
|
|
|
66
66
|
private _isTouch: Boolean;
|
|
67
67
|
private _isAVM1Dragging: Boolean = false;
|
|
68
68
|
|
|
69
|
-
public startDragObject(collision
|
|
69
|
+
public startDragObject(collision?: PickingCollision) {
|
|
70
70
|
this._isAVM1Dragging = true;
|
|
71
|
-
|
|
71
|
+
|
|
72
|
+
// we MUST overrider collision target if present, because draggable !== drag emitter
|
|
73
|
+
if (collision)
|
|
72
74
|
this._mouseDragCollision = collision;
|
|
73
75
|
}
|
|
74
76
|
|
|
@@ -292,14 +294,9 @@ export class MouseManager {
|
|
|
292
294
|
|
|
293
295
|
let event: AwayMouseEvent;
|
|
294
296
|
let dispatcher: ContainerNode;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
for (var i: number = 0; i < len; ++i) {
|
|
299
|
-
logEvents+= " / "+this._queuedEvents[i].type;
|
|
300
|
-
}
|
|
301
|
-
console.log(logEvents);*/
|
|
302
|
-
for (let i: number = 0; i < len; ++i) {
|
|
297
|
+
|
|
298
|
+
const len = this._queuedEvents.length;
|
|
299
|
+
for (let i = 0; i < len; ++i) {
|
|
303
300
|
event = this._queuedEvents[i];
|
|
304
301
|
dispatcher = collision?.rootNode;
|
|
305
302
|
|
|
@@ -333,7 +330,7 @@ export class MouseManager {
|
|
|
333
330
|
if (this._focusContainer)
|
|
334
331
|
this._focusContainer.setFocus(false, true);
|
|
335
332
|
|
|
336
|
-
this._focusContainer = this._mouseDragCollision?.rootNode
|
|
333
|
+
this._focusContainer = this._mouseDragCollision?.rootNode?.container;
|
|
337
334
|
|
|
338
335
|
if (this._focusContainer)
|
|
339
336
|
this._focusContainer.setFocus(true, true);
|
|
@@ -515,7 +512,7 @@ export class MouseManager {
|
|
|
515
512
|
this._collisionIsEnabledButton = isActiveButton;
|
|
516
513
|
}
|
|
517
514
|
|
|
518
|
-
// set cursor if not dragging mouse
|
|
515
|
+
// set cursor if not dragging mouse ???
|
|
519
516
|
if (!this._mouseDragging) {
|
|
520
517
|
this.cursorType = collisionNode ? <any>collisionNode.container.getMouseCursor() : 'auto';
|
|
521
518
|
}
|
|
@@ -523,22 +520,6 @@ export class MouseManager {
|
|
|
523
520
|
this._updateDirty = false;
|
|
524
521
|
}
|
|
525
522
|
|
|
526
|
-
// public addSceneLayer(scene:Scene)
|
|
527
|
-
// {
|
|
528
|
-
// var stg:Stage = scene.stage;
|
|
529
|
-
//
|
|
530
|
-
// // Add instance to mouse3dmanager to fire mouse events for multiple scenes
|
|
531
|
-
// if (!scene.stageGL.mouse3DManager)
|
|
532
|
-
// scene.stageGL.mouse3DManager = this;
|
|
533
|
-
//
|
|
534
|
-
// if (!hasKey(scene))
|
|
535
|
-
// _scene3Ds[scene] = 0;
|
|
536
|
-
//
|
|
537
|
-
// _childDepth = 0;
|
|
538
|
-
// traverseDisplayObjects(stg);
|
|
539
|
-
// _sceneCount = _childDepth;
|
|
540
|
-
// }
|
|
541
|
-
|
|
542
523
|
public registerPicker(picker: RaycastPicker): void {
|
|
543
524
|
this._pickerLookup.push(picker);
|
|
544
525
|
}
|
|
@@ -737,7 +718,7 @@ export class MouseManager {
|
|
|
737
718
|
|
|
738
719
|
private queueDispatch(event: AwayMouseEvent, sourceEvent: MouseEvent | TouchEvent | WheelEvent): void {
|
|
739
720
|
// Store event to be dispatched later.
|
|
740
|
-
event.delta = (sourceEvent instanceof WheelEvent) ? sourceEvent.deltaY : 0;
|
|
721
|
+
event.delta = (sourceEvent instanceof WheelEvent) ? -3 * sourceEvent.deltaY / 100 : 0;
|
|
741
722
|
event.ctrlKey = sourceEvent.ctrlKey;
|
|
742
723
|
event.altKey = sourceEvent.altKey;
|
|
743
724
|
event.shiftKey = sourceEvent.shiftKey;
|
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
10
|
import { BasicPartition, NodePool, 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();
|
|
@@ -39,10 +38,9 @@ export class FNTGenerator {
|
|
|
39
38
|
this._renderer.view.projection.transform.lookAt(new Vector3D());
|
|
40
39
|
|
|
41
40
|
this._renderer.renderableSorter = null;//new RenderableSort2D();
|
|
42
|
-
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
public generate(font: Font, maxSize: number, fontSize: number, padding: number):
|
|
43
|
+
public generate(font: Font, maxSize: number, fontSize: number, padding: number): BitmapImage2D[] {
|
|
46
44
|
let outputBitmap: BitmapImage2D;
|
|
47
45
|
const outputBitmaps: BitmapImage2D[] = [];
|
|
48
46
|
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() {
|