@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
@@ -5,6 +5,8 @@ import { Style, TriangleElements } from '@awayjs/renderer';
5
5
  import { GraphicsPath, Shape, GraphicsFactoryFills, GraphicsFactoryHelper, MaterialManager } from '@awayjs/graphics';
6
6
  import { MethodMaterial } from '@awayjs/materials';
7
7
  import { TesselatedFontChar } from './TesselatedFontChar';
8
+ import { Settings } from '../Settings';
9
+ import { FNTGeneratorCanvas } from './FNTGeneratorCanvas';
8
10
  var ONCE_EMIT_TABLE = Object.create(null);
9
11
  function once(obj, key) {
10
12
  if (key === void 0) { key = ''; }
@@ -75,59 +77,19 @@ var TesselatedFontTable = /** @class */ (function (_super) {
75
77
  TesselatedFontTable.prototype.getGlyphCount = function () {
76
78
  return this._font_chars.length;
77
79
  };
78
- TesselatedFontTable.prototype._drawDebugRect = function (glyph_verts, minx, maxx, miny, maxy) {
79
- // left
80
- glyph_verts[glyph_verts.length] = minx;
81
- glyph_verts[glyph_verts.length] = miny;
82
- glyph_verts[glyph_verts.length] = minx + 1;
83
- glyph_verts[glyph_verts.length] = maxy;
84
- glyph_verts[glyph_verts.length] = minx;
85
- glyph_verts[glyph_verts.length] = maxy;
86
- glyph_verts[glyph_verts.length] = minx;
87
- glyph_verts[glyph_verts.length] = miny;
88
- glyph_verts[glyph_verts.length] = minx + 1;
89
- glyph_verts[glyph_verts.length] = miny;
90
- glyph_verts[glyph_verts.length] = minx + 1;
91
- glyph_verts[glyph_verts.length] = maxy;
92
- // right
93
- glyph_verts[glyph_verts.length] = maxx;
94
- glyph_verts[glyph_verts.length] = miny;
95
- glyph_verts[glyph_verts.length] = maxx - 1;
96
- glyph_verts[glyph_verts.length] = maxy;
97
- glyph_verts[glyph_verts.length] = maxx;
98
- glyph_verts[glyph_verts.length] = maxy;
99
- glyph_verts[glyph_verts.length] = maxx;
100
- glyph_verts[glyph_verts.length] = miny;
101
- glyph_verts[glyph_verts.length] = maxx - 1;
102
- glyph_verts[glyph_verts.length] = miny;
103
- glyph_verts[glyph_verts.length] = maxx - 1;
104
- glyph_verts[glyph_verts.length] = maxy;
105
- // top
106
- glyph_verts[glyph_verts.length] = minx + 1;
107
- glyph_verts[glyph_verts.length] = miny;
108
- glyph_verts[glyph_verts.length] = minx + 1;
109
- glyph_verts[glyph_verts.length] = miny + 1;
110
- glyph_verts[glyph_verts.length] = maxx - 1;
111
- glyph_verts[glyph_verts.length] = miny;
112
- glyph_verts[glyph_verts.length] = minx + 1;
113
- glyph_verts[glyph_verts.length] = miny + 1;
114
- glyph_verts[glyph_verts.length] = maxx - 1;
115
- glyph_verts[glyph_verts.length] = miny + 1;
116
- glyph_verts[glyph_verts.length] = maxx - 1;
117
- glyph_verts[glyph_verts.length] = miny;
118
- // bottom
119
- glyph_verts[glyph_verts.length] = minx + 1;
120
- glyph_verts[glyph_verts.length] = maxy;
121
- glyph_verts[glyph_verts.length] = minx + 1;
122
- glyph_verts[glyph_verts.length] = maxy - 1;
123
- glyph_verts[glyph_verts.length] = maxx - 1;
124
- glyph_verts[glyph_verts.length] = maxy;
125
- glyph_verts[glyph_verts.length] = minx + 1;
126
- glyph_verts[glyph_verts.length] = maxy - 1;
127
- glyph_verts[glyph_verts.length] = maxx - 1;
128
- glyph_verts[glyph_verts.length] = maxy - 1;
129
- glyph_verts[glyph_verts.length] = maxx - 1;
130
- glyph_verts[glyph_verts.length] = maxy;
80
+ TesselatedFontTable.prototype.generateFNTData = function (stage) {
81
+ // already exist
82
+ if (this._fntSizeLimit > 0) {
83
+ return;
84
+ }
85
+ var generator = new FNTGeneratorCanvas(stage, false);
86
+ var bitmaps = generator.generate(this.font, this._font_em_size, this._current_size, 4);
87
+ if (bitmaps && bitmaps.length > 0) {
88
+ /**
89
+ * Random value, anyway we render real font dimension ;)
90
+ */
91
+ this._fntSizeLimit = this._current_size * 4;
92
+ }
131
93
  };
132
94
  TesselatedFontTable.prototype.generateFNTTextures = function (padding, fontSize, texSize) {
133
95
  console.log('generateFNTTextures');
@@ -414,15 +376,13 @@ var TesselatedFontTable = /** @class */ (function (_super) {
414
376
  // need to implement textFormat tabstop
415
377
  return (space * 2 * 20) / 20;
416
378
  }
417
- var t_font_char = this.getChar(char_code);
379
+ var t_font_char = this.getChar(char_code, false);
418
380
  if (t_font_char) {
419
- return (Math.floor(t_font_char.char_width * this._size_multiply * 20)) / 20;
381
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
420
382
  }
421
- else {
422
- if (char_code == '9679') {
423
- t_font_char = this.createPointGlyph_9679();
424
- return (Math.floor(t_font_char.char_width * this._size_multiply * 20)) / 20;
425
- }
383
+ else if (char_code == '9679') {
384
+ t_font_char = this.createPointGlyph_9679();
385
+ return ((t_font_char.char_width * this._size_multiply * 20) | 0) / 20;
426
386
  }
427
387
  return 0;
428
388
  };
@@ -565,28 +525,14 @@ var TesselatedFontTable = /** @class */ (function (_super) {
565
525
  return new Point(x - origin_x, this.getLineHeight());
566
526
  };
567
527
  TesselatedFontTable.prototype.fillTextRun = function (tf, format, startWord, wordCnt) {
568
- var useFNT = this._fntSizeLimit >= 0 &&
569
- (this._fntSizeLimit == 0 || this._fntSizeLimit >= format.size);
570
- var textShape = tf.getTextShapeForIdentifierAndFormat(format.color.toString() + useFNT.toString() + '0', format);
571
- var fntTextShapesByChannel = {};
572
- var fntSelectedTextShapesByChannel = {};
573
- //var currentFNTTextShapeMap:any;
574
- var argb;
575
- var mat;
528
+ var useFNT = this._fntSizeLimit >= 0 && this._fntSizeLimit >= format.size;
576
529
  if (useFNT) {
577
- argb = ColorUtils.float32ColorToARGB(format.color);
578
- mat = new MethodMaterial(this._fnt_channels[0]);
579
- mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
580
- mat.bothSides = true;
581
- mat.alphaBlending = true;
582
- mat.useColorTransform = true;
583
- mat.style.sampler = new ImageSampler(false, true, true);
530
+ return this.fillTextRunFNT(tf, format, startWord, wordCnt);
584
531
  }
585
- textShape.fntMaterial = useFNT ? mat : null;
586
- fntTextShapesByChannel[format.color.toString() + useFNT.toString() + '0'] = textShape;
532
+ var textShape = this._queryShape(tf, format);
587
533
  var textShapeSelected;
588
534
  var newFormat;
589
- var wordsCount = startWord + wordCnt * 5;
535
+ var wordsCount = startWord + wordCnt;
590
536
  var size_multiply = this._size_multiply;
591
537
  var select_start = tf.selectionBeginIndex;
592
538
  var select_end = tf.selectionEndIndex;
@@ -594,16 +540,9 @@ var TesselatedFontTable = /** @class */ (function (_super) {
594
540
  if (tf.selectable) {
595
541
  newFormat = format.clone();
596
542
  newFormat.color = 0xffffff;
597
- textShapeSelected = tf.getTextShapeForIdentifierAndFormat(useFNT.toString() + '0', newFormat);
598
- fntSelectedTextShapesByChannel[useFNT.toString() + '0'] = textShapeSelected;
599
- mat = new MethodMaterial(this._fnt_channels[0]);
600
- mat.bothSides = true;
601
- mat.alphaBlending = true;
602
- mat.useColorTransform = true;
603
- mat.style.sampler = new ImageSampler(false, true, true);
604
- textShapeSelected.fntMaterial = useFNT ? mat : null;
543
+ textShapeSelected = this._queryShape(tf, newFormat);
605
544
  if (newFormat.underline && (startWord + 1) < tf.words.length) {
606
- start_x = tf.words[startWord + 1];
545
+ start_x = tf.words.get(startWord).x;
607
546
  }
608
547
  if (tf.selectionEndIndex < tf.selectionBeginIndex) {
609
548
  select_start = tf.selectionEndIndex;
@@ -617,66 +556,39 @@ var TesselatedFontTable = /** @class */ (function (_super) {
617
556
  var underlines = [];
618
557
  // loop over all the words and create the text data for it
619
558
  // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
620
- for (var w = startWord; w < wordsCount; w += 5) {
621
- var x = tf.words[w + 1];
622
- var y = tf.words[w + 2];
623
- var startIdx = tf.words[w];
624
- var charsCount = startIdx + tf.words[w + 4];
559
+ for (var w = startWord; w < wordsCount; w += 1) {
560
+ var word = tf.words.get(w);
561
+ var x = word.x;
562
+ var y = word.y;
563
+ var startIdx = word.start;
564
+ var charsCount = startIdx + word.len;
625
565
  for (var c = startIdx; c < charsCount; c++) {
626
566
  var curTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
627
567
  textShapeSelected : textShape;
628
- if (tf.chars_codes[c] !== 32 && tf.chars_codes[c] !== 9) {
629
- var charGlyph = this.getChar(tf.chars_codes[c].toString());
630
- if (!charGlyph) {
631
- if (once(this, 'miss' + tf.chars_codes[c])) {
632
- console.debug('[TesselatedFontTable] Error: char not found in fontTable', tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
633
- }
634
- continue;
635
- }
636
- if (useFNT) {
637
- // the font should use fnt
638
- var curFormat = curTShape.format;
639
- var ctmpTShape = (tf.isInFocus && c >= select_start && c < select_end) ?
640
- fntSelectedTextShapesByChannel[useFNT.toString() + charGlyph.fnt_channel] :
641
- fntTextShapesByChannel[curFormat.color.toString() +
642
- useFNT.toString() + charGlyph.fnt_channel];
643
- if (!ctmpTShape) {
644
- var newtextShape = tf.getTextShapeForIdentifierAndFormat(curFormat.color.toString()
645
- + useFNT.toString() + charGlyph.fnt_channel.toString(), curFormat);
646
- var argb_1 = ColorUtils.float32ColorToARGB(curFormat.color);
647
- var mat_1 = new MethodMaterial(this._fnt_channels[charGlyph.fnt_channel]);
648
- mat_1.colorTransform = new ColorTransform(argb_1[1] / 255, argb_1[2] / 255, argb_1[3] / 255);
649
- mat_1.bothSides = true;
650
- mat_1.alphaBlending = true;
651
- mat_1.useColorTransform = true;
652
- mat_1.style.sampler = new ImageSampler(false, true, true);
653
- newtextShape.fntMaterial = mat_1;
654
- fntTextShapesByChannel[curFormat.color.toString() +
655
- useFNT.toString() + charGlyph.fnt_channel] = newtextShape;
656
- ctmpTShape = newtextShape;
657
- }
658
- var x1 = x + ((charGlyph.char_width * size_multiply) * charGlyph.fnt_rect.x);
659
- var x2 = x1 + ((charGlyph.char_width * size_multiply) * charGlyph.fnt_rect.width);
660
- var y1 = y + ((this._font_em_size * size_multiply) * charGlyph.fnt_rect.y);
661
- var y2 = y1 + ((this._font_em_size * size_multiply) * charGlyph.fnt_rect.height);
662
- throw 'Invalid implementation, verts not raw array';
663
- }
664
- else {
665
- if (curTShape === textShapeSelected) {
666
- selectedCharEntries.push({
667
- char: charGlyph, x: x, y: y, selected: true
668
- });
669
- selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
670
- }
671
- else {
672
- charEntries.push({
673
- char: charGlyph, x: x, y: y, selected: false
674
- });
675
- textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
676
- }
568
+ var code = tf.chars_codes[c];
569
+ if (code === 32 || code === 9) {
570
+ continue;
571
+ }
572
+ var charGlyph = this.getChar(code, true);
573
+ if (!charGlyph) {
574
+ if (once(this, 'miss' + tf.chars_codes[c])) {
575
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable', tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
677
576
  }
678
- x += charGlyph.char_width * size_multiply;
577
+ continue;
679
578
  }
579
+ if (curTShape === textShapeSelected) {
580
+ selectedCharEntries.push({
581
+ char: charGlyph, x: x, y: y, selected: true
582
+ });
583
+ selectedBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
584
+ }
585
+ else {
586
+ charEntries.push({
587
+ char: charGlyph, x: x, y: y, selected: false
588
+ });
589
+ textBuffSize += charGlyph.fill_data.buffer.byteLength / 4;
590
+ }
591
+ x += charGlyph.char_width * size_multiply;
680
592
  }
681
593
  var half_thickness = 0.25 * tf.internalScale.y;
682
594
  var topY = y + this.getUnderLineHeight() + half_thickness;
@@ -716,6 +628,173 @@ var TesselatedFontTable = /** @class */ (function (_super) {
716
628
  textShapeSelected.addChunk(buff);
717
629
  }
718
630
  };
631
+ TesselatedFontTable.prototype._cacheKey = function (color, channel) {
632
+ if (color === void 0) { color = 0xffffff; }
633
+ if (channel === void 0) { channel = -1; }
634
+ var fnt = channel >= 0;
635
+ return "" + color + fnt + (fnt ? channel : 0);
636
+ };
637
+ /**
638
+ * Query shape from TextField cache
639
+ * @param tf
640
+ * @param format
641
+ * @param channel - FNT channel, -1 for regular font
642
+ * @private
643
+ */
644
+ TesselatedFontTable.prototype._queryShape = function (tf, format, channel) {
645
+ if (channel === void 0) { channel = -1; }
646
+ var textShape = tf.getTextShapeForIdentifierAndFormat(this._cacheKey(format.color, channel), format);
647
+ if (channel >= 0 && !textShape.fntMaterial) {
648
+ var argb = ColorUtils.float32ColorToARGB(format.color);
649
+ var mat = new MethodMaterial(this._fnt_channels[channel]);
650
+ mat.colorTransform = new ColorTransform(argb[1] / 255, argb[2] / 255, argb[3] / 255);
651
+ mat.bothSides = true;
652
+ mat.alphaBlending = true;
653
+ mat.useColorTransform = true;
654
+ mat.style.sampler = new ImageSampler(false, true, true);
655
+ textShape.fntMaterial = mat;
656
+ }
657
+ return textShape;
658
+ };
659
+ /**
660
+ * Fill text run but use FNT Texture.
661
+ */
662
+ TesselatedFontTable.prototype.fillTextRunFNT = function (tf, format, startWord, wordCnt) {
663
+ var textShape = this._queryShape(tf, format, 0);
664
+ var charShapesCache = {};
665
+ var charShapesSelectedCache = {};
666
+ var wordsCount = startWord + wordCnt;
667
+ var size_multiply = this._size_multiply;
668
+ var textShapeSelected;
669
+ var select_start = tf.selectionBeginIndex;
670
+ var select_end = tf.selectionEndIndex;
671
+ var start_x = 0;
672
+ if (tf.selectable) {
673
+ var newFormat = format.clone();
674
+ newFormat.color = 0xffffff;
675
+ textShapeSelected = this._queryShape(tf, newFormat, 0);
676
+ charShapesSelectedCache[textShapeSelected.cacheId] = textShapeSelected;
677
+ if (newFormat.underline && (startWord + 1) < tf.words.length) {
678
+ start_x = tf.words.get(startWord).x;
679
+ }
680
+ if (tf.selectionEndIndex < tf.selectionBeginIndex) {
681
+ select_start = tf.selectionEndIndex;
682
+ select_end = tf.selectionBeginIndex;
683
+ }
684
+ }
685
+ charShapesCache[textShape.cacheId] = textShape;
686
+ var shapeArray = new Map();
687
+ var underlines = [];
688
+ // loop over all the words and create the text data for it
689
+ // each word provides its own start-x and start-y values, so we can just ignore whitespace-here
690
+ for (var w = startWord; w < wordsCount; w += 1) {
691
+ var word = tf.words.get(w);
692
+ var x = word.x;
693
+ var y = word.y;
694
+ var startIdx = word.start;
695
+ var charsCount = startIdx + word.len;
696
+ var preFilledShape = null;
697
+ var target = null;
698
+ var color = 0;
699
+ var fntID = -1;
700
+ for (var c = startIdx; c < charsCount; c++) {
701
+ // space codes
702
+ if (tf.chars_codes[c] === 32 || tf.chars_codes[c] === 9) {
703
+ continue;
704
+ }
705
+ var charGlyph = this.getChar(tf.chars_codes[c], false);
706
+ if (!charGlyph) {
707
+ if (once(this, 'miss' + tf.chars_codes[c])) {
708
+ console.debug('[TesselatedFontTable] Error: char not found in fontTable', tf.chars_codes[c], String.fromCharCode(tf.chars_codes[c]));
709
+ }
710
+ continue;
711
+ }
712
+ var selected = (tf.isInFocus && c >= select_start && c < select_end);
713
+ var baseShape = textShape;
714
+ var cacheStore = charShapesCache;
715
+ if (selected) {
716
+ baseShape = textShapeSelected;
717
+ cacheStore = charShapesSelectedCache;
718
+ }
719
+ var curFormat = baseShape.format;
720
+ var cacheId = preFilledShape ? preFilledShape.cacheId : '';
721
+ if (!cacheId || color !== curFormat.color || charGlyph.fnt_channel !== fntID) {
722
+ cacheId = this._cacheKey(curFormat.color, charGlyph.fnt_channel);
723
+ color = curFormat.color;
724
+ fntID = charGlyph.fnt_channel;
725
+ }
726
+ var fntFilledShape = preFilledShape && preFilledShape.cacheId !== cacheId
727
+ ? cacheStore[cacheId]
728
+ : preFilledShape;
729
+ if (!fntFilledShape) {
730
+ fntFilledShape = this._queryShape(tf, curFormat, charGlyph.fnt_channel);
731
+ cacheStore[fntFilledShape.cacheId] = fntFilledShape;
732
+ }
733
+ if (preFilledShape !== fntFilledShape || !target) {
734
+ // FNT is very little, has 6 vertices per shape (instead of 300 ++)
735
+ // fill it directly
736
+ target = shapeArray.get(fntFilledShape) || {
737
+ uv: [],
738
+ pos: []
739
+ };
740
+ shapeArray.set(fntFilledShape, target);
741
+ preFilledShape = fntFilledShape;
742
+ }
743
+ this._emitCharFNT(target, charGlyph, x, y);
744
+ x += charGlyph.char_width * size_multiply;
745
+ }
746
+ /*
747
+ todo Generate underline for regular shape
748
+
749
+ const half_thickness: number = 0.25 * tf.internalScale.y;
750
+ const topY: number = y + this.getUnderLineHeight() + half_thickness;
751
+ const bottomY: number = y + this.getUnderLineHeight() - half_thickness;
752
+
753
+ if (tf.selectable && newFormat.underline && (startWord + 1) < tf.words.length) {
754
+ const underBuff = new Float32Array(12);
755
+
756
+ underBuff[0] = start_x;
757
+ underBuff[1] = bottomY;
758
+ underBuff[2] = start_x;
759
+ underBuff[3] = topY;
760
+ underBuff[4] = x;
761
+ underBuff[5] = topY;
762
+ underBuff[6] = x;
763
+ underBuff[7] = topY;
764
+ underBuff[8] = x;
765
+ underBuff[9] = bottomY;
766
+ underBuff[10] = start_x;
767
+ underBuff[11] = bottomY;
768
+
769
+ underlines.push(underBuff);
770
+ }
771
+ */
772
+ } // for word
773
+ // oh, es5... iterating over map won't working
774
+ var shapes = Array.from(shapeArray.keys());
775
+ for (var _i = 0, shapes_1 = shapes; _i < shapes_1.length; _i++) {
776
+ var shape = shapes_1[_i];
777
+ var values = shapeArray.get(shape);
778
+ shape.addChunk(new Float32Array(values.pos), new Float32Array(values.uv));
779
+ }
780
+ };
781
+ TesselatedFontTable.prototype._emitCharFNT = function (target, charGlyph, x, y) {
782
+ var scale = this._size_multiply;
783
+ var size = this._font_em_size * scale;
784
+ var rect = charGlyph.fnt_rect;
785
+ var uv = charGlyph.fnt_uv;
786
+ var width = charGlyph.char_width * scale;
787
+ if (!rect) {
788
+ // empty chars not have data
789
+ return;
790
+ }
791
+ var x1 = x + width * rect.x * 20;
792
+ var x2 = x1 + width * rect.width * 20;
793
+ var y1 = y + size * rect.y * 20;
794
+ var y2 = y1 + size * rect.height * 20;
795
+ target.pos.push(x1, y1, x1, y2, x2, y1, x2, y1, x1, y2, x2, y2);
796
+ target.uv.push(uv.x, uv.y, uv.x, uv.bottom, uv.right, uv.y, uv.right, uv.y, uv.x, uv.bottom, uv.right, uv.bottom);
797
+ };
719
798
  TesselatedFontTable.prototype.createPointGlyph_9679 = function () {
720
799
  var verts = [];
721
800
  GraphicsFactoryHelper.drawElipse(this._font_em_size / 2, this._font_em_size / 2, this._font_em_size / 8, this._font_em_size / 8, verts, 0, 360, 5, false);
@@ -729,92 +808,122 @@ var TesselatedFontTable = /** @class */ (function (_super) {
729
808
  this._font_chars_dic['9679'] = t_font_char;
730
809
  return t_font_char;
731
810
  };
732
- TesselatedFontTable.prototype.getChar = function (name) {
733
- var t_font_char = this._font_chars_dic[name];
734
- if (!t_font_char) {
811
+ TesselatedFontTable.prototype.getCharOpenType = function (name) {
812
+ if (!this._opentype_font) {
813
+ return null;
814
+ }
815
+ var thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
816
+ if (!thisGlyph) {
817
+ return null;
818
+ }
819
+ //console.log("got the glyph from opentype");
820
+ var thisPath = thisGlyph.getPath();
821
+ var awayPath = new GraphicsPath();
822
+ var i = 0;
823
+ var len = thisPath.commands.length;
824
+ //awayPath.lineTo(0, 0);
825
+ //awayPath.moveTo(0,0);//-100);
826
+ //awayPath.curveTo(100, 250, 200,0);
827
+ //awayPath.lineTo(150, 100);
828
+ //awayPath.moveTo(0,20);
829
+ //awayPath.curveTo(100, 270, 200,20);
830
+ //awayPath.moveTo(0,-20);
831
+ //awayPath.moveTo(0,-10);
832
+ //awayPath.curveTo(100, -110, 200,-10);
833
+ var startx = 0;
834
+ var starty = 0;
835
+ var y_offset = this._ascent;
836
+ //40 = 66
837
+ var scale = (this._opentype_font.unitsPerEm / 72);
838
+ for (i = 0; i < len; i++) {
839
+ var cmd = thisPath.commands[i];
840
+ //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
841
+ if (cmd.type === 'M') {
842
+ awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
843
+ startx = scale * cmd.x;
844
+ starty = scale * cmd.y + y_offset;
845
+ }
846
+ else if (cmd.type === 'L') {
847
+ awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
848
+ }
849
+ else if (cmd.type === 'Q') {
850
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset, scale * cmd.x, scale * cmd.y + y_offset);
851
+ }
852
+ else if (cmd.type === 'C') {
853
+ var mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
854
+ var mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
855
+ awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset, scale * mergedX, scale * mergedY + y_offset);
856
+ awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset, scale * cmd.x, scale * cmd.y + y_offset);
857
+ }
858
+ else if (cmd.type === 'Z') {
859
+ awayPath.lineTo(startx, starty);
860
+ }
861
+ }
862
+ var t_font_char = new TesselatedFontChar(null, null, awayPath);
863
+ t_font_char.char_width = thisGlyph.advanceWidth; //(1 / thisGlyph.path.unitsPerEm * 72);
864
+ t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(awayPath, true, null, scale * Settings.FONT_TESSELATION_QUALITY);
865
+ t_font_char.lastTesselatedScale = scale;
866
+ if (!t_font_char.fill_data) {
867
+ if (once(this, 'tess' + name)) {
868
+ console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:', name.charCodeAt(0));
869
+ }
870
+ return null;
871
+ }
872
+ this._font_chars.push(t_font_char);
873
+ this._font_chars_dic[name] = t_font_char;
874
+ };
875
+ TesselatedFontTable.prototype.getChar = function (name, tesselation) {
876
+ if (tesselation === void 0) { tesselation = true; }
877
+ name = '' + name;
878
+ var fontChar = this._font_chars_dic[name];
879
+ if (!tesselation && fontChar) {
880
+ return fontChar;
881
+ }
882
+ if (!fontChar) {
735
883
  if (this._opentype_font) {
736
- var thisGlyph = this._opentype_font.charToGlyph(String.fromCharCode(parseInt(name)));
737
- if (thisGlyph) {
738
- //console.log("got the glyph from opentype");
739
- var thisPath = thisGlyph.getPath();
740
- var awayPath = new GraphicsPath();
741
- var i = 0;
742
- var len = thisPath.commands.length;
743
- //awayPath.lineTo(0, 0);
744
- //awayPath.moveTo(0,0);//-100);
745
- //awayPath.curveTo(100, 250, 200,0);
746
- //awayPath.lineTo(150, 100);
747
- //awayPath.moveTo(0,20);
748
- //awayPath.curveTo(100, 270, 200,20);
749
- //awayPath.moveTo(0,-20);
750
- //awayPath.moveTo(0,-10);
751
- //awayPath.curveTo(100, -110, 200,-10);
752
- var startx = 0;
753
- var starty = 0;
754
- var y_offset = this._ascent;
755
- //40 = 66
756
- var scale = (this._opentype_font.unitsPerEm / 72);
757
- for (i = 0; i < len; i++) {
758
- var cmd = thisPath.commands[i];
759
- //console.log("cmd", cmd.type, cmd.x, cmd.y, cmd.x1, cmd.y1, cmd.x2, cmd.y2);
760
- if (cmd.type === 'M') {
761
- awayPath.moveTo(scale * cmd.x, scale * cmd.y + y_offset);
762
- startx = scale * cmd.x;
763
- starty = scale * cmd.y + y_offset;
764
- }
765
- else if (cmd.type === 'L') {
766
- awayPath.lineTo(scale * cmd.x, scale * cmd.y + y_offset);
767
- }
768
- else if (cmd.type === 'Q') {
769
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset, scale * cmd.x, scale * cmd.y + y_offset);
770
- }
771
- else if (cmd.type === 'C') {
772
- var mergedX = cmd.x1 + (cmd.x2 - cmd.x1) / 2;
773
- var mergedY = cmd.y1 + (cmd.y2 - cmd.y1) / 2;
774
- awayPath.curveTo(scale * cmd.x1, scale * cmd.y1 + y_offset, scale * mergedX, scale * mergedY + y_offset);
775
- awayPath.curveTo(scale * cmd.x2, scale * cmd.y2 + y_offset, scale * cmd.x, scale * cmd.y + y_offset);
776
- }
777
- else if (cmd.type === 'Z') {
778
- awayPath.lineTo(startx, starty);
779
- }
780
- }
781
- t_font_char = new TesselatedFontChar(null, null, awayPath);
782
- t_font_char.char_width = thisGlyph.advanceWidth; //(1 / thisGlyph.path.unitsPerEm * 72);
783
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(awayPath, true);
784
- if (!t_font_char.fill_data) {
785
- if (once(this, 'tess' + name)) {
786
- console.debug('[TesselatedFontTable] Error:tesselating opentype glyph:', name.charCodeAt(0));
787
- }
788
- return null;
789
- }
790
- this._font_chars.push(t_font_char);
791
- this._font_chars_dic[name] = t_font_char;
792
- }
884
+ return this.getCharOpenType(name);
793
885
  }
794
886
  if (name == '9679') {
795
- t_font_char = this.createPointGlyph_9679();
796
- }
797
- }
798
- else if (t_font_char.fill_data == null &&
799
- t_font_char.stroke_data == null && t_font_char.fill_data_path != null) {
800
- // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
801
- // change the new first command to moveTo
802
- if (t_font_char.fill_data_path.commands[0][0] == 1 &&
803
- t_font_char.fill_data_path.data[0][0] == 0 && t_font_char.fill_data_path.data[0][1] == 0) {
804
- t_font_char.fill_data_path.data[0].shift();
805
- t_font_char.fill_data_path.data[0].shift();
806
- t_font_char.fill_data_path.commands[0].shift();
807
- t_font_char.fill_data_path.commands[0][0] = 2;
808
- }
809
- t_font_char.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(t_font_char.fill_data_path, true);
810
- if (!t_font_char.fill_data) {
811
- if (once(this, 'tess' + name)) {
812
- console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
813
- }
814
- return null;
887
+ return this.createPointGlyph_9679();
815
888
  }
889
+ return null;
816
890
  }
817
- return t_font_char;
891
+ // tesselation pass
892
+ var scale = this._size_multiply;
893
+ var qualityStepScale = Math.max(0.01, Math.round(scale * 100) / 100);
894
+ if (fontChar.fill_data_path === null) {
895
+ return null;
896
+ }
897
+ // we already has buffer with tesselation scale ratio
898
+ if (!(fontChar.fill_data == null && fontChar.stroke_data == null)
899
+ && fontChar.lastTesselatedScale >= qualityStepScale) {
900
+ return fontChar;
901
+ }
902
+ var path = fontChar.fill_data_path;
903
+ // hack for messed up "X": remove the first command if it is moveTo that points to 0,0
904
+ // change the new first command to moveTo
905
+ if (path.commands[0][0] == 1 && path.data[0][0] == 0 && path.data[0][1] == 0) {
906
+ path.data[0].shift();
907
+ path.data[0].shift();
908
+ path.commands[0].shift();
909
+ path.commands[0][0] = 2;
910
+ }
911
+ if (fontChar.fill_data) {
912
+ // it not have dispose, but clear will drop abstraction if it exist
913
+ fontChar.fill_data.clear();
914
+ }
915
+ fontChar.fill_data = GraphicsFactoryFills.pathToAttributesBuffer(fontChar.fill_data_path, true, null, qualityStepScale * Settings.FONT_TESSELATION_QUALITY);
916
+ if (fontChar.lastTesselatedScale > 0) {
917
+ console.debug('[TesselatedFontTable] Retesselate char from scale:', fontChar.lastTesselatedScale, qualityStepScale, String.fromCharCode(+name));
918
+ }
919
+ fontChar.lastTesselatedScale = qualityStepScale;
920
+ if (!fontChar.fill_data) {
921
+ if (once(this, 'tess' + name)) {
922
+ console.debug('[TesselatedFontTable] Error:tesselating glyph:', name.charCodeAt(0));
923
+ }
924
+ return null;
925
+ }
926
+ return fontChar;
818
927
  };
819
928
  TesselatedFontTable.prototype.setChar = function (name, char_width, fills_data, stroke_data, uses_curves, glyph_idx, fill_data_path) {
820
929
  if (fills_data === void 0) { fills_data = null; }
@@ -842,6 +951,11 @@ var TesselatedFontTable = /** @class */ (function (_super) {
842
951
  this._font_chars.push(t_font_char);
843
952
  this._font_chars_dic[name] = t_font_char;
844
953
  };
954
+ TesselatedFontTable.prototype.roundTo = function (val) {
955
+ var p = Settings.TEXT_SHAPE_ROUND_PRECISION;
956
+ var invP = 1 / (p || 1);
957
+ return Math.floor(val * invP) * p;
958
+ };
845
959
  TesselatedFontTable.prototype._fillBuffer = function (buffer, chars, selection) {
846
960
  if (selection === void 0) { selection = false; }
847
961
  var scale = this._size_multiply;
@@ -855,8 +969,8 @@ var TesselatedFontTable = /** @class */ (function (_super) {
855
969
  var view = new Float32Array(char.fill_data.buffer);
856
970
  var count = view.length;
857
971
  for (var v = 0; v < count; v += 2) {
858
- buffer[offset + v + 0] = view[v + 0] * scale + x;
859
- buffer[offset + v + 1] = view[v + 1] * scale + y;
972
+ buffer[offset + v + 0] = this.roundTo(view[v + 0] * scale + x);
973
+ buffer[offset + v + 1] = this.roundTo(view[v + 1] * scale + y);
860
974
  }
861
975
  offset += view.length;
862
976
  }
@@ -3,16 +3,19 @@ import { Shape } from '@awayjs/graphics';
3
3
  import { MaterialBase } from '@awayjs/materials';
4
4
  import { TextFormat } from './TextFormat';
5
5
  export declare class TextShape {
6
+ readonly format: TextFormat;
7
+ readonly cacheId: string;
6
8
  verts: Array<Float32Array>;
7
- format: TextFormat;
9
+ uvs: Array<Float32Array>;
8
10
  shape: Shape;
9
11
  fntMaterial: MaterialBase;
10
12
  elements: TriangleElements;
11
13
  private _length;
12
14
  get length(): number;
13
15
  set length(val: number);
14
- constructor();
15
- addChunk(buffer: Float32Array): void;
16
+ get hasUV(): boolean;
17
+ constructor(format: TextFormat, cacheId: string);
18
+ addChunk(buffer: Float32Array, uvs?: Float32Array): void;
16
19
  get tall(): Float32Array;
17
20
  }
18
21
  //# sourceMappingURL=TextShape.d.ts.map