@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.
Files changed (46) hide show
  1. package/bundle/awayjs-scene.umd.js +885 -399
  2. package/bundle/awayjs-scene.umd.js.map +1 -1
  3. package/bundle/awayjs-scene.umd.min.js.map +1 -1
  4. package/bundle/awayjs-scene.umd.min.js.map.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/lib/Scene.d.ts.map +1 -1
  7. package/dist/lib/Scene.js +6 -6
  8. package/dist/lib/Settings.d.ts +3 -0
  9. package/dist/lib/Settings.d.ts.map +1 -1
  10. package/dist/lib/Settings.js +17 -2
  11. package/dist/lib/display/TextField.d.ts +36 -5
  12. package/dist/lib/display/TextField.d.ts.map +1 -1
  13. package/dist/lib/display/TextField.js +227 -138
  14. package/dist/lib/image/SceneImage2D.d.ts +1 -0
  15. package/dist/lib/image/SceneImage2D.d.ts.map +1 -1
  16. package/dist/lib/image/SceneImage2D.js +45 -13
  17. package/dist/lib/text/FNTGenerator.d.ts +5 -4
  18. package/dist/lib/text/FNTGenerator.d.ts.map +1 -1
  19. package/dist/lib/text/FNTGenerator.js +18 -13
  20. package/dist/lib/text/FNTGeneratorCanvas.d.ts +23 -0
  21. package/dist/lib/text/FNTGeneratorCanvas.d.ts.map +1 -0
  22. package/dist/lib/text/FNTGeneratorCanvas.js +205 -0
  23. package/dist/lib/text/FNTGeneratrorBase.d.ts +8 -0
  24. package/dist/lib/text/FNTGeneratrorBase.d.ts.map +1 -0
  25. package/dist/lib/text/FNTGeneratrorBase.js +7 -0
  26. package/dist/lib/text/TesselatedFontChar.d.ts +6 -4
  27. package/dist/lib/text/TesselatedFontChar.d.ts.map +1 -1
  28. package/dist/lib/text/TesselatedFontChar.js +8 -3
  29. package/dist/lib/text/TesselatedFontTable.d.ts +19 -3
  30. package/dist/lib/text/TesselatedFontTable.d.ts.map +1 -1
  31. package/dist/lib/text/TesselatedFontTable.js +339 -225
  32. package/dist/lib/text/TextShape.d.ts +6 -3
  33. package/dist/lib/text/TextShape.d.ts.map +1 -1
  34. package/dist/lib/text/TextShape.js +27 -2
  35. package/index.ts +1 -1
  36. package/lib/Scene.ts +5 -6
  37. package/lib/Settings.ts +23 -3
  38. package/lib/display/TextField.ts +317 -145
  39. package/lib/image/SceneImage2D.ts +103 -14
  40. package/lib/text/FNTGenerator.ts +10 -11
  41. package/lib/text/FNTGeneratorCanvas.ts +288 -0
  42. package/lib/text/FNTGeneratrorBase.ts +11 -0
  43. package/lib/text/TesselatedFontChar.ts +8 -12
  44. package/lib/text/TesselatedFontTable.ts +438 -255
  45. package/lib/text/TextShape.ts +30 -3
  46. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import { Matrix, AssetBase, Point, Rectangle, ColorTransform, ColorUtils } from '@awayjs/core';
2
2
 
3
- import { ImageSampler, AttributesBuffer, Float2Attributes, AttributesView, BitmapImage2D } from '@awayjs/stage';
3
+ import { ImageSampler, AttributesBuffer, Float2Attributes, AttributesView, BitmapImage2D, Stage } from '@awayjs/stage';
4
4
 
5
5
  import { Style, TriangleElements } from '@awayjs/renderer';
6
6
 
@@ -14,6 +14,8 @@ import { TextFormat } from './TextFormat';
14
14
  import { TextShape } from './TextShape';
15
15
 
16
16
  import { TextField } from '../display/TextField';
17
+ import { Settings } from '../Settings';
18
+ import { FNTGeneratorCanvas } from './FNTGeneratorCanvas';
17
19
 
18
20
  const ONCE_EMIT_TABLE: StringMap<boolean> = Object.create(null);
19
21
  function once(obj: any, key = '') {
@@ -28,6 +30,7 @@ interface ICharEntry {
28
30
  y: number;
29
31
  char: TesselatedFontChar;
30
32
  selected?: boolean;
33
+ shape?: TextShape;
31
34
  }
32
35
 
33
36
  /**
@@ -121,84 +124,21 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
121
124
  return this._font_chars.length;
122
125
  }
123
126
 
124
- private _drawDebugRect(glyph_verts: number[], minx: number, maxx: number,miny: number,maxy: number,) {
125
-
126
- // left
127
- glyph_verts[glyph_verts.length] = minx;
128
- glyph_verts[glyph_verts.length] = miny;
129
-
130
- glyph_verts[glyph_verts.length] = minx + 1;
131
- glyph_verts[glyph_verts.length] = maxy;
132
-
133
- glyph_verts[glyph_verts.length] = minx;
134
- glyph_verts[glyph_verts.length] = maxy;
135
-
136
- glyph_verts[glyph_verts.length] = minx;
137
- glyph_verts[glyph_verts.length] = miny;
138
-
139
- glyph_verts[glyph_verts.length] = minx + 1;
140
- glyph_verts[glyph_verts.length] = miny;
141
-
142
- glyph_verts[glyph_verts.length] = minx + 1;
143
- glyph_verts[glyph_verts.length] = maxy;
144
-
145
- // right
146
-
147
- glyph_verts[glyph_verts.length] = maxx;
148
- glyph_verts[glyph_verts.length] = miny;
149
-
150
- glyph_verts[glyph_verts.length] = maxx - 1;
151
- glyph_verts[glyph_verts.length] = maxy;
152
-
153
- glyph_verts[glyph_verts.length] = maxx;
154
- glyph_verts[glyph_verts.length] = maxy;
155
-
156
- glyph_verts[glyph_verts.length] = maxx;
157
- glyph_verts[glyph_verts.length] = miny;
158
-
159
- glyph_verts[glyph_verts.length] = maxx - 1;
160
- glyph_verts[glyph_verts.length] = miny;
161
-
162
- glyph_verts[glyph_verts.length] = maxx - 1;
163
- glyph_verts[glyph_verts.length] = maxy;
164
-
165
- // top
166
- glyph_verts[glyph_verts.length] = minx + 1;
167
- glyph_verts[glyph_verts.length] = miny;
168
-
169
- glyph_verts[glyph_verts.length] = minx + 1;
170
- glyph_verts[glyph_verts.length] = miny + 1;
171
-
172
- glyph_verts[glyph_verts.length] = maxx - 1;
173
- glyph_verts[glyph_verts.length] = miny;
174
-
175
- glyph_verts[glyph_verts.length] = minx + 1;
176
- glyph_verts[glyph_verts.length] = miny + 1;
177
-
178
- glyph_verts[glyph_verts.length] = maxx - 1;
179
- glyph_verts[glyph_verts.length] = miny + 1;
180
-
181
- glyph_verts[glyph_verts.length] = maxx - 1;
182
- glyph_verts[glyph_verts.length] = miny;
183
-
184
- // bottom
185
- glyph_verts[glyph_verts.length] = minx + 1;
186
- glyph_verts[glyph_verts.length] = maxy;
187
-
188
- glyph_verts[glyph_verts.length] = minx + 1;
189
- glyph_verts[glyph_verts.length] = maxy - 1;
190
-
191
- glyph_verts[glyph_verts.length] = maxx - 1;
192
- glyph_verts[glyph_verts.length] = maxy;
193
-
194
- glyph_verts[glyph_verts.length] = minx + 1;
195
- glyph_verts[glyph_verts.length] = maxy - 1;
127
+ public generateFNTData (stage: Stage): void {
128
+ // already exist
129
+ if (this._fntSizeLimit > 0) {
130
+ return;
131
+ }
196
132
 
197
- glyph_verts[glyph_verts.length] = maxx - 1;
198
- glyph_verts[glyph_verts.length] = maxy - 1;
133
+ const generator = new FNTGeneratorCanvas(stage, false);
134
+ const bitmaps = generator.generate(this.font, this._font_em_size, this._current_size, 4);
199
135
 
200
- glyph_verts[glyph_verts.length] = maxx - 1;
201
- glyph_verts[glyph_verts.length] = maxy;
136
+ if (bitmaps && bitmaps.length > 0) {
137
+ /**
138
+ * Random value, anyway we render real font dimension ;)
139
+ */
140
+ this._fntSizeLimit = this._current_size * 4;
141
+ }
202
142
  }
203
143
 
204
144
  public generateFNTTextures(padding: number, fontSize: number, texSize: number): Shape[] {
@@ -520,14 +460,13 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
520
460
  return (space * 2 * 20) / 20;
521
461
  }
522
462
 
523
- let t_font_char: TesselatedFontChar = this.getChar(char_code);
463
+ let t_font_char = this.getChar(char_code, false);
464
+
524
465
  if (t_font_char) {
525
- return (Math.floor(t_font_char.char_width * this._size_multiply * 20)) / 20;
526
- } else {
527
- if (char_code == '9679') {
528
- t_font_char = this.createPointGlyph_9679();
529
- return (Math.floor(t_font_char.char_width * this._size_multiply * 20)) / 20;
530
- }
466
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
467
+ } else if (char_code == '9679') {
468
+ t_font_char = this.createPointGlyph_9679();
469
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
531
470
  }
532
471
 
533
472
  return 0;
@@ -683,35 +622,21 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
683
622
  return new Point(x - origin_x, this.getLineHeight());
684
623
  }
685
624
 
686
- public fillTextRun(tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
687
-
688
- const useFNT: boolean = this._fntSizeLimit >= 0 &&
689
- (this._fntSizeLimit == 0 || this._fntSizeLimit >= format.size);
690
- const textShape: TextShape =
691
- tf.getTextShapeForIdentifierAndFormat(format.color.toString() + useFNT.toString() + '0', format);
692
-
693
- const fntTextShapesByChannel: any = {};
694
- const fntSelectedTextShapesByChannel: any = {};
695
- //var currentFNTTextShapeMap:any;
625
+ public fillTextRun (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
626
+ const useFNT: boolean = this._fntSizeLimit >= 0 && this._fntSizeLimit >= format.size;
696
627
 
697
- let argb: number[];
698
- let mat: MethodMaterial;
699
628
  if (useFNT) {
700
- argb = ColorUtils.float32ColorToARGB(format.color);
701
- mat = new MethodMaterial(this._fnt_channels[0]);
702
- mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
703
- mat.bothSides = true;
704
- mat.alphaBlending = true;
705
- mat.useColorTransform = true;
706
- mat.style.sampler = new ImageSampler(false, true, true);
629
+ return this.fillTextRunFNT(tf, format, startWord, wordCnt);
707
630
  }
708
- textShape.fntMaterial = useFNT ? mat : null;
709
- fntTextShapesByChannel[format.color.toString() + useFNT.toString() + '0'] = textShape;
631
+
632
+ const textShape = this._queryShape(tf, format);
633
+
710
634
  let textShapeSelected: TextShape;
711
635
  let newFormat: TextFormat;
712
636
 
713
- const wordsCount = startWord + wordCnt * 5;
637
+ const wordsCount = startWord + wordCnt;
714
638
  const size_multiply = this._size_multiply;
639
+
715
640
  let select_start = tf.selectionBeginIndex;
716
641
  let select_end = tf.selectionEndIndex;
717
642
  let start_x: number = 0;
@@ -720,17 +645,10 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
720
645
  newFormat = format.clone();
721
646
  newFormat.color = 0xffffff;
722
647
 
723
- textShapeSelected = tf.getTextShapeForIdentifierAndFormat(useFNT.toString() + '0', newFormat);
724
- fntSelectedTextShapesByChannel[useFNT.toString() + '0'] = textShapeSelected;
725
- mat = new MethodMaterial(this._fnt_channels[0]);
726
- mat.bothSides = true;
727
- mat.alphaBlending = true;
728
- mat.useColorTransform = true;
729
- mat.style.sampler = new ImageSampler(false, true, true);
730
- textShapeSelected.fntMaterial = useFNT ? mat : null;
648
+ textShapeSelected = this._queryShape(tf, newFormat);
731
649
 
732
650
  if (newFormat.underline && (startWord + 1) < tf.words.length) {
733
- start_x = tf.words[startWord + 1];
651
+ start_x = tf.words.get(startWord).x;
734
652
  }
735
653
 
736
654
  if (tf.selectionEndIndex < tf.selectionBeginIndex) {
@@ -748,78 +666,50 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
748
666
 
749
667
  // loop over all the words and create the text data for it
750
668
  // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
751
- for (let w = startWord; w < wordsCount; w += 5) {
669
+ for (let w = startWord; w < wordsCount; w += 1) {
670
+ const word = tf.words.get(w);
752
671
 
753
- let x = tf.words[w + 1];
754
- const y = tf.words[w + 2];
672
+ let x = word.x;
673
+ const y = word.y;
755
674
 
756
- const startIdx = tf.words[w];
757
- const charsCount = startIdx + tf.words[w + 4];
675
+ const startIdx = word.start;
676
+ const charsCount = startIdx + word.len;
758
677
 
759
678
  for (let c = startIdx; c < charsCount; c++) {
760
679
  const curTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
761
680
  textShapeSelected : textShape;
762
681
 
763
- if (tf.chars_codes[c] !== 32 && tf.chars_codes[c] !== 9) {
764
- const charGlyph = this.getChar(tf.chars_codes[c].toString());
682
+ const code = tf.chars_codes[c];
765
683
 
766
- if (!charGlyph) {
767
- if (once(this, 'miss' + tf.chars_codes[c])) {
768
- console.debug('[TesselatedFontTable] Error: char not found in fontTable',
769
- tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
770
- }
771
- continue;
772
- }
684
+ if (code === 32 || code === 9) {
685
+ continue;
686
+ }
773
687
 
774
- if (useFNT) {
775
- // the font should use fnt
776
- const curFormat = curTShape.format;
777
- let ctmpTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
778
- fntSelectedTextShapesByChannel[useFNT.toString() + charGlyph.fnt_channel] :
779
- fntTextShapesByChannel[curFormat.color.toString() +
780
- useFNT.toString() + charGlyph.fnt_channel];
781
- if (!ctmpTShape) {
782
- const newtextShape: TextShape =
783
- tf.getTextShapeForIdentifierAndFormat(
784
- curFormat.color.toString()
785
- + useFNT.toString() + charGlyph.fnt_channel.toString(),
786
- curFormat);
787
- const argb: number[] = ColorUtils.float32ColorToARGB(curFormat.color);
788
- const mat: MethodMaterial = new MethodMaterial(
789
- this._fnt_channels[charGlyph.fnt_channel]);
790
- mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
791
- mat.bothSides = true;
792
- mat.alphaBlending = true;
793
- mat.useColorTransform = true;
794
- mat.style.sampler = new ImageSampler(false, true, true);
795
- newtextShape.fntMaterial = mat;
796
- fntTextShapesByChannel[curFormat.color.toString() +
797
- useFNT.toString() + charGlyph.fnt_channel] = newtextShape;
798
- ctmpTShape = newtextShape;
799
- }
800
- const x1: number = x + ((charGlyph.char_width * size_multiply) * charGlyph.fnt_rect.x);
801
- const x2: number = x1 + ((charGlyph.char_width * size_multiply) * charGlyph.fnt_rect.width);
802
- const y1: number = y + ((this._font_em_size * size_multiply) * charGlyph.fnt_rect.y);
803
- const y2: number = y1 + ((this._font_em_size * size_multiply) * charGlyph.fnt_rect.height);
804
-
805
- throw 'Invalid implementation, verts not raw array';
688
+ const charGlyph = this.getChar(code, true);
806
689
 
807
- } else {
808
- if (curTShape === textShapeSelected) {
809
- selectedCharEntries.push({
810
- char: charGlyph, x, y, selected: true
811
- });
812
-
813
- selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
814
- } else {
815
- charEntries.push({
816
- char: charGlyph, x, y, selected: false
817
- });
818
- textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
819
- }
690
+ if (!charGlyph) {
691
+ if (once(this, 'miss' + tf.chars_codes[c])) {
692
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable',
693
+ tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
820
694
  }
821
- x += charGlyph.char_width * size_multiply;
695
+ continue;
696
+ }
697
+
698
+ if (curTShape === textShapeSelected) {
699
+ selectedCharEntries.push({
700
+ char: charGlyph, x, y, selected: true
701
+ });
702
+
703
+ selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
704
+ } else {
705
+ charEntries.push({
706
+ char: charGlyph, x, y, selected: false
707
+ });
708
+ textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
822
709
  }
710
+
711
+ x += charGlyph.char_width * size_multiply;
712
+
823
713
  }
824
714
 
825
715
  const half_thickness: number = 0.25 * tf.internalScale.y;
@@ -872,6 +762,230 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
872
762
 
873
763
  }
874
764
 
765
+ private _cacheKey (color: number = 0xffffff, channel: number = -1) {
766
+ const fnt = channel >= 0;
767
+ return `${color}${fnt}${fnt ? channel : 0}`;
768
+ }
769
+
770
+ /**
771
+ * Query shape from TextField cache
772
+ * @param tf
773
+ * @param format
774
+ * @param channel - FNT channel, -1 for regular font
775
+ * @private
776
+ */
777
+ private _queryShape (tf: TextField, format: TextFormat, channel = -1): TextShape {
778
+ const textShape = tf.getTextShapeForIdentifierAndFormat(this._cacheKey(format.color, channel), format);
779
+
780
+ if (channel >= 0 && !textShape.fntMaterial) {
781
+ const argb = ColorUtils.float32ColorToARGB(format.color);
782
+ const mat = new MethodMaterial(this._fnt_channels[channel]);
783
+
784
+ mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
785
+ mat.bothSides = true;
786
+ mat.alphaBlending = true;
787
+ mat.useColorTransform = true;
788
+ mat.style.sampler = new ImageSampler(false, true, true);
789
+
790
+ textShape.fntMaterial = mat;
791
+ }
792
+
793
+ return textShape;
794
+ }
795
+
796
+ /**
797
+ * Fill text run but use FNT Texture.
798
+ */
799
+ public fillTextRunFNT (tf: TextField, format: TextFormat, startWord: number, wordCnt: number) {
800
+ const textShape: TextShape = this._queryShape(tf, format, 0);
801
+ const charShapesCache: Record<string, TextShape> = {};
802
+ const charShapesSelectedCache: Record<string, TextShape> = {};
803
+ const wordsCount = startWord + wordCnt;
804
+ const size_multiply = this._size_multiply;
805
+
806
+ let textShapeSelected: TextShape;
807
+
808
+ let select_start = tf.selectionBeginIndex;
809
+ let select_end = tf.selectionEndIndex;
810
+ let start_x: number = 0;
811
+
812
+ if (tf.selectable) {
813
+ const newFormat = format.clone();
814
+ newFormat.color = 0xffffff;
815
+
816
+ textShapeSelected = this._queryShape(tf, newFormat, 0);
817
+ charShapesSelectedCache[textShapeSelected.cacheId] = textShapeSelected;
818
+
819
+ if (newFormat.underline && (startWord + 1) < tf.words.length) {
820
+ start_x = tf.words.get(startWord).x;
821
+ }
822
+
823
+ if (tf.selectionEndIndex < tf.selectionBeginIndex) {
824
+ select_start = tf.selectionEndIndex;
825
+ select_end = tf.selectionBeginIndex;
826
+ }
827
+ }
828
+
829
+ charShapesCache[textShape.cacheId] = textShape;
830
+
831
+ const shapeArray = new Map<TextShape, {
832
+ uv: number [],
833
+ pos: number []
834
+ }>();
835
+
836
+ const underlines: Float32Array[] = [];
837
+
838
+ // loop over all the words and create the text data for it
839
+ // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
840
+ for (let w = startWord; w < wordsCount; w += 1) {
841
+ const word = tf.words.get(w);
842
+
843
+ let x = word.x;
844
+ const y = word.y;
845
+
846
+ const startIdx = word.start;
847
+ const charsCount = startIdx + word.len;
848
+
849
+ let preFilledShape: TextShape = null;
850
+ let target: any = null;
851
+ let color: number = 0;
852
+ let fntID: number = -1;
853
+
854
+ for (let c = startIdx; c < charsCount; c++) {
855
+ // space codes
856
+ if (tf.chars_codes[c] === 32 || tf.chars_codes[c] === 9) {
857
+ continue;
858
+ }
859
+
860
+ const charGlyph = this.getChar(tf.chars_codes[c], false);
861
+
862
+ if (!charGlyph) {
863
+ if (once(this, 'miss' + tf.chars_codes[c])) {
864
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable',
865
+ tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
866
+ }
867
+ continue;
868
+ }
869
+
870
+ const selected = (tf.isInFocus && c >= select_start && c < select_end);
871
+ let baseShape = textShape;
872
+ let cacheStore = charShapesCache;
873
+
874
+ if (selected) {
875
+ baseShape = textShapeSelected;
876
+ cacheStore = charShapesSelectedCache;
877
+ }
878
+
879
+ const curFormat = baseShape.format;
880
+ let cacheId = preFilledShape ? preFilledShape.cacheId : '';
881
+
882
+ if (!cacheId || color !== curFormat.color || charGlyph.fnt_channel !== fntID) {
883
+ cacheId = this._cacheKey(curFormat.color, charGlyph.fnt_channel);
884
+ color = curFormat.color;
885
+ fntID = charGlyph.fnt_channel;
886
+ }
887
+
888
+ let fntFilledShape = preFilledShape && preFilledShape.cacheId !== cacheId
889
+ ? cacheStore[cacheId]
890
+ : preFilledShape;
891
+
892
+ if (!fntFilledShape) {
893
+ fntFilledShape = this._queryShape(tf, curFormat, charGlyph.fnt_channel);
894
+ cacheStore[fntFilledShape.cacheId] = fntFilledShape;
895
+ }
896
+
897
+ if (preFilledShape !== fntFilledShape || !target) {
898
+ // FNT is very little, has 6 vertices per shape (instead of 300 ++)
899
+ // fill it directly
900
+ target = shapeArray.get(fntFilledShape) || {
901
+ uv: [],
902
+ pos: []
903
+ };
904
+
905
+ shapeArray.set(fntFilledShape, target);
906
+ preFilledShape = fntFilledShape;
907
+ }
908
+
909
+ this._emitCharFNT (target, charGlyph, x, y);
910
+
911
+ x += charGlyph.char_width * size_multiply;
912
+ }
913
+
914
+ /*
915
+ todo Generate underline for regular shape
916
+
917
+ const half_thickness: number = 0.25 * tf.internalScale.y;
918
+ const topY: number = y + this.getUnderLineHeight() + half_thickness;
919
+ const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
920
+
921
+ if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
922
+ const underBuff = new Float32Array(12);
923
+
924
+ underBuff[0] = start_x;
925
+ underBuff[1] = bottomY;
926
+ underBuff[2] = start_x;
927
+ underBuff[3] = topY;
928
+ underBuff[4] = x;
929
+ underBuff[5] = topY;
930
+ underBuff[6] = x;
931
+ underBuff[7] = topY;
932
+ underBuff[8] = x;
933
+ underBuff[9] = bottomY;
934
+ underBuff[10] = start_x;
935
+ underBuff[11] = bottomY;
936
+
937
+ underlines.push(underBuff);
938
+ }
939
+ */
940
+
941
+ } // for word
942
+
943
+ // oh, es5... iterating over map won't working
944
+ const shapes = Array.from(shapeArray.keys());
945
+
946
+ for (const shape of shapes) {
947
+ const values = shapeArray.get(shape);
948
+
949
+ shape.addChunk(
950
+ new Float32Array(values.pos),
951
+ new Float32Array(values.uv)
952
+ );
953
+ }
954
+ }
955
+
956
+ private _emitCharFNT (
957
+ target: {uv: number[], pos: number[]},
958
+ charGlyph: TesselatedFontChar,
959
+ x: number,
960
+ y: number
961
+ ) {
962
+ const scale = this._size_multiply;
963
+ const size = this._font_em_size * scale;
964
+ const rect = charGlyph.fnt_rect;
965
+ const uv = charGlyph.fnt_uv;
966
+ const width = charGlyph.char_width * scale;
967
+
968
+ if (!rect) {
969
+ // empty chars not have data
970
+ return;
971
+ }
972
+
973
+ const x1 = x + width * rect.x * 20;
974
+ const x2 = x1 + width * rect.width * 20;
975
+ const y1 = y + size * rect.y * 20;
976
+ const y2 = y1 + size * rect.height * 20;
977
+
978
+ target.pos.push(
979
+ x1, y1, x1, y2, x2, y1,
980
+ x2, y1, x1, y2, x2, y2
981
+ );
982
+
983
+ target.uv.push(
984
+ uv.x, uv.y, uv.x, uv.bottom, uv.right, uv.y,
985
+ uv.right, uv.y, uv.x, uv.bottom, uv.right, uv.bottom
986
+ );
987
+ }
988
+
875
989
  public createPointGlyph_9679(): TesselatedFontChar {
876
990
  const verts: number[] = [];
877
991
  GraphicsFactoryHelper.drawElipse(
@@ -890,94 +1004,156 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
890
1004
  return t_font_char;
891
1005
  }
892
1006
 
893
- public getChar(name: string): TesselatedFontChar {
894
- let t_font_char: TesselatedFontChar = this._font_chars_dic[name];
895
- if (!t_font_char) {
896
- if (this._opentype_font) {
897
- const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
898
- if (thisGlyph) {
899
- //console.log("got the glyph from opentype");
900
- const thisPath = thisGlyph.getPath();
901
- const awayPath: GraphicsPath = new GraphicsPath();
902
- let i = 0;
903
- const len = thisPath.commands.length;
1007
+ private getCharOpenType (name: string): TesselatedFontChar {
1008
+ if (!this._opentype_font) {
1009
+ return null;
1010
+ }
1011
+ const thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
904
1012
 
905
- //awayPath.lineTo(0, 0);
906
- //awayPath.moveTo(0,0);//-100);
907
- //awayPath.curveTo(100, 250, 200,0);
908
- //awayPath.lineTo(150, 100);
909
- //awayPath.moveTo(0,20);
910
- //awayPath.curveTo(100, 270, 200,20);
911
- //awayPath.moveTo(0,-20);
912
- //awayPath.moveTo(0,-10);
913
- //awayPath.curveTo(100, -110, 200,-10);
1013
+ if (!thisGlyph) {
1014
+ return null;
1015
+ }
1016
+ //console.log("got the glyph from opentype");
1017
+ const thisPath = thisGlyph.getPath();
1018
+ const awayPath: GraphicsPath = new GraphicsPath();
1019
+ let i = 0;
1020
+ const len = thisPath.commands.length;
1021
+
1022
+ //awayPath.lineTo(0, 0);
1023
+ //awayPath.moveTo(0,0);//-100);
1024
+ //awayPath.curveTo(100, 250, 200,0);
1025
+ //awayPath.lineTo(150, 100);
1026
+ //awayPath.moveTo(0,20);
1027
+ //awayPath.curveTo(100, 270, 200,20);
1028
+ //awayPath.moveTo(0,-20);
1029
+ //awayPath.moveTo(0,-10);
1030
+ //awayPath.curveTo(100, -110, 200,-10);
1031
+
1032
+ let startx: number = 0;
1033
+ let starty: number = 0;
1034
+ const y_offset = this._ascent;
1035
+ //40 = 66
1036
+ const scale = (this._opentype_font.unitsPerEm / 72);
1037
+ for (i = 0; i < len; i++) {
1038
+ const cmd = thisPath.commands[i];
1039
+ //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
1040
+ if (cmd.type === 'M') {
1041
+ awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
1042
+ startx = scale * cmd.x;
1043
+ starty = scale * cmd.y + y_offset;
1044
+ } else if (cmd.type === 'L') {
1045
+ awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
1046
+ } else if (cmd.type === 'Q') {
1047
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1048
+ scale * cmd.x, scale * cmd.y + y_offset);
1049
+ } else if (cmd.type === 'C') {
1050
+ const mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
1051
+ const mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
1052
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
1053
+ scale * mergedX, scale * mergedY + y_offset);
1054
+ awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset,
1055
+ scale * cmd.x, scale * cmd.y + y_offset);
1056
+ } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
1057
+ }
914
1058
 
915
- let startx: number = 0;
916
- let starty: number = 0;
917
- const y_offset = this._ascent;
918
- //40 = 66
919
- const scale = (this._opentype_font.unitsPerEm / 72);
920
- for (i = 0; i < len; i++) {
921
- const cmd = thisPath.commands[i];
922
- //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
923
- if (cmd.type === 'M') {
924
- awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
925
- startx = scale * cmd.x;
926
- starty = scale * cmd.y + y_offset;
927
- } else if (cmd.type === 'L') {
928
- awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
929
- } else if (cmd.type === 'Q') {
930
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
931
- scale * cmd.x, scale * cmd.y + y_offset);
932
- } else if (cmd.type === 'C') {
933
- const mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
934
- const mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
935
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset,
936
- scale * mergedX, scale * mergedY + y_offset);
937
- awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset,
938
- scale * cmd.x, scale * cmd.y + y_offset);
939
- } else if (cmd.type === 'Z') { awayPath.lineTo(startx, starty);}
940
- }
1059
+ const t_font_char = new TesselatedFontChar(null, null, awayPath);
1060
+ t_font_char.char_width = thisGlyph.advanceWidth;//(1 / thisGlyph.path.unitsPerEm * 72);
1061
+ t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1062
+ awayPath,
1063
+ true,
1064
+ null,
1065
+ scale * Settings.FONT_TESSELATION_QUALITY
1066
+ );
1067
+ t_font_char.lastTesselatedScale = scale;
1068
+
1069
+ if (!t_font_char.fill_data) {
1070
+ if (once(this, 'tess' + name)) {
1071
+ console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:',
1072
+ name.charCodeAt(0));
1073
+ }
1074
+ return null;
1075
+ }
941
1076
 
942
- t_font_char = new TesselatedFontChar(null, null, awayPath);
943
- t_font_char.char_width = thisGlyph.advanceWidth;//(1 / thisGlyph.path.unitsPerEm * 72);
944
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(awayPath, true);
1077
+ this._font_chars.push(t_font_char);
1078
+ this._font_chars_dic[name] = t_font_char;
1079
+ }
945
1080
 
946
- if (!t_font_char.fill_data) {
947
- if (once(this, 'tess' + name)) {
948
- console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:',
949
- name.charCodeAt(0));
950
- }
951
- return null;
952
- }
1081
+ public getChar(name: string | number, tesselation = true): TesselatedFontChar {
1082
+ name = '' + name;
953
1083
 
954
- this._font_chars.push(t_font_char);
955
- this._font_chars_dic[name] = t_font_char;
956
- }
1084
+ const fontChar: TesselatedFontChar = this._font_chars_dic[name];
1085
+
1086
+ if (!tesselation && fontChar) {
1087
+ return fontChar;
1088
+ }
1089
+
1090
+ if (!fontChar) {
1091
+ if (this._opentype_font) {
1092
+ return this.getCharOpenType(name);
957
1093
  }
1094
+
958
1095
  if (name == '9679') {
959
- t_font_char = this.createPointGlyph_9679();
960
- }
961
- } else if (t_font_char.fill_data == null &&
962
- t_font_char.stroke_data == null && t_font_char.fill_data_path != null) {
963
- // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
964
- // change the new first command to moveTo
965
- if (t_font_char.fill_data_path.commands[0][0] == 1 &&
966
- t_font_char.fill_data_path.data[0][0] == 0 && t_font_char.fill_data_path.data[0][1] == 0) {
967
- t_font_char.fill_data_path.data[0].shift();
968
- t_font_char.fill_data_path.data[0].shift();
969
- t_font_char.fill_data_path.commands[0].shift();
970
- t_font_char.fill_data_path.commands[0][0] = 2;
1096
+ return this.createPointGlyph_9679();
971
1097
  }
972
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(t_font_char.fill_data_path, true);
973
- if (!t_font_char.fill_data) {
974
- if (once(this, 'tess' + name)) {
975
- console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
976
- }
977
- return null;
1098
+
1099
+ return null;
1100
+ }
1101
+
1102
+ // tesselation pass
1103
+ const scale = this._size_multiply;
1104
+ const qualityStepScale = Math.max(0.01, Math.round(scale * 100) / 100);
1105
+
1106
+ if (fontChar.fill_data_path === null) {
1107
+ return null;
1108
+ }
1109
+
1110
+ // we already has buffer with tesselation scale ratio
1111
+ if (!(fontChar.fill_data == null && fontChar.stroke_data == null)
1112
+ && fontChar.lastTesselatedScale >= qualityStepScale
1113
+ ) {
1114
+ return fontChar;
1115
+ }
1116
+
1117
+ const path = fontChar.fill_data_path;
1118
+
1119
+ // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
1120
+ // change the new first command to moveTo
1121
+ if (path.commands[0][0] == 1 && path.data[0][0] == 0 && path.data[0][1] == 0) {
1122
+ path.data[0].shift();
1123
+ path.data[0].shift();
1124
+ path.commands[0].shift();
1125
+ path.commands[0][0] = 2;
1126
+ }
1127
+
1128
+ if (fontChar.fill_data) {
1129
+ // it not have dispose, but clear will drop abstraction if it exist
1130
+ fontChar.fill_data.clear();
1131
+ }
1132
+
1133
+ fontChar.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(
1134
+ fontChar.fill_data_path,
1135
+ true,
1136
+ null,
1137
+ qualityStepScale * Settings.FONT_TESSELATION_QUALITY
1138
+ );
1139
+
1140
+ if (fontChar.lastTesselatedScale > 0) {
1141
+ console.debug(
1142
+ '[TesselatedFontTable] Retesselate char from scale:',
1143
+ fontChar.lastTesselatedScale, qualityStepScale, String.fromCharCode(+name)
1144
+ );
1145
+ }
1146
+
1147
+ fontChar.lastTesselatedScale = qualityStepScale;
1148
+
1149
+ if (!fontChar.fill_data) {
1150
+ if (once(this, 'tess' + name)) {
1151
+ console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
978
1152
  }
1153
+ return null;
979
1154
  }
980
- return t_font_char;
1155
+
1156
+ return fontChar;
981
1157
  }
982
1158
 
983
1159
  public setChar(
@@ -1008,6 +1184,13 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
1008
1184
  this._font_chars_dic[name] = t_font_char;
1009
1185
  }
1010
1186
 
1187
+ private roundTo(val: number) {
1188
+ const p = Settings.TEXT_SHAPE_ROUND_PRECISION;
1189
+ const invP = 1 / (p || 1);
1190
+
1191
+ return Math.floor(val * invP) * p;
1192
+ }
1193
+
1011
1194
  private _fillBuffer(
1012
1195
  buffer: Float32Array,
1013
1196
  chars: ICharEntry[],
@@ -1027,8 +1210,8 @@ export class TesselatedFontTable extends AssetBase implements IFontTable {
1027
1210
  const count = view.length;
1028
1211
 
1029
1212
  for (let v = 0; v < count; v += 2) {
1030
- buffer[offset + v + 0] = view[v + 0] * scale + x;
1031
- buffer[offset + v + 1] = view[v + 1] * scale + y;
1213
+ buffer[offset + v + 0] = this.roundTo(view[v + 0] * scale + x);
1214
+ buffer[offset + v + 1] = this.roundTo(view[v + 1] * scale + y);
1032
1215
  }
1033
1216
 
1034
1217
  offset += view.length;