David PHAM-VAN

Improve TTF writer with multi-compound characters

1 # Changelog 1 # Changelog
2 2
  3 +## 3.10.5
  4 +
  5 +- Improve TTF writer with multi-compound characters
  6 +
3 ## 3.10.4 7 ## 3.10.4
4 8
5 - Fix Deprecation warning message 9 - Fix Deprecation warning message
@@ -67,43 +67,56 @@ class TtfWriter { @@ -67,43 +67,56 @@ class TtfWriter {
67 final tablesLength = <String, int>{}; 67 final tablesLength = <String, int>{};
68 68
69 // Create the glyphs table 69 // Create the glyphs table
70 - final glyphsInfo = <TtfGlyphInfo>[]; 70 + final glyphsMap = <int, TtfGlyphInfo>{};
  71 + final charMap = <int, int>{};
  72 + final overflow = <int>{};
71 final compounds = <int, int>{}; 73 final compounds = <int, int>{};
72 74
73 - for (var index = 0; index < chars.length; index++) {  
74 - if (chars[index] == 32) { 75 + for (final char in chars) {
  76 + if (char == 32) {
75 final glyph = TtfGlyphInfo(32, Uint8List(0), const <int>[]); 77 final glyph = TtfGlyphInfo(32, Uint8List(0), const <int>[]);
76 - glyphsInfo.add(glyph); 78 + glyphsMap[glyph.index] = glyph;
  79 + charMap[char] = glyph.index;
77 continue; 80 continue;
78 } 81 }
79 82
80 - final glyphIndex = ttf.charToGlyphIndexMap[chars[index]] ?? 0; 83 + final glyphIndex = ttf.charToGlyphIndexMap[char] ?? 0;
81 if (glyphIndex >= ttf.glyphOffsets.length) { 84 if (glyphIndex >= ttf.glyphOffsets.length) {
  85 + assert(true, '$glyphIndex not in the font');
82 continue; 86 continue;
83 } 87 }
84 88
85 - final glyph = ttf.readGlyph(glyphIndex).copy();  
86 - for (final g in glyph.compounds) {  
87 - compounds[g] = -1; 89 + void addGlyph(glyphIndex) {
  90 + final glyph = ttf.readGlyph(glyphIndex).copy();
  91 + for (final g in glyph.compounds) {
  92 + compounds[g] = -1;
  93 + overflow.add(g);
  94 + addGlyph(g);
  95 + }
  96 + glyphsMap[glyph.index] = glyph;
88 } 97 }
89 - glyphsInfo.add(glyph); 98 +
  99 + charMap[char] = glyphIndex;
  100 + addGlyph(glyphIndex);
90 } 101 }
91 102
92 - // Add compound glyphs  
93 - for (final compound in compounds.keys) {  
94 - final index = glyphsInfo.firstWhere(  
95 - (TtfGlyphInfo glyph) => glyph.index == compound,  
96 - orElse: () {  
97 - final glyph = ttf.readGlyph(compound);  
98 - assert(glyph.compounds.isEmpty, 'This is not a simple glyph');  
99 - glyphsInfo.add(glyph); 103 + final glyphsInfo = <TtfGlyphInfo>[];
  104 +
  105 + for (final char in chars) {
  106 + final glyphsIndex = charMap[char]!;
  107 + print('$char $glyphsIndex $glyphsMap $charMap ${ttf.fontName}');
  108 + glyphsInfo.add(glyphsMap[glyphsIndex] ?? glyphsMap.values.first);
  109 + glyphsMap.remove(glyphsIndex);
  110 + }
100 111
101 - return glyph;  
102 - },  
103 - ); 112 + glyphsInfo.addAll(glyphsMap.values);
104 113
  114 + // Add compound glyphs
  115 + for (final compound in compounds.keys) {
  116 + final index = glyphsInfo
  117 + .firstWhere((TtfGlyphInfo glyph) => glyph.index == compound);
105 compounds[compound] = glyphsInfo.indexOf(index); 118 compounds[compound] = glyphsInfo.indexOf(index);
106 - assert(compounds[compound]! >= 0, 'Unable to find the glyph'); 119 + assert((compounds[compound] ?? 0) >= 0, 'Unable to find the glyph');
107 } 120 }
108 121
109 // update compound indices 122 // update compound indices
@@ -6,7 +6,7 @@ issue_tracker: https://github.com/DavBfr/dart_pdf/issues @@ -6,7 +6,7 @@ issue_tracker: https://github.com/DavBfr/dart_pdf/issues
6 screenshots: 6 screenshots:
7 - description: 'Example of a generated document' 7 - description: 'Example of a generated document'
8 path: example.jpg 8 path: example.jpg
9 -version: 3.10.4 9 +version: 3.10.5
10 10
11 environment: 11 environment:
12 sdk: ">=2.18.0 <4.0.0" 12 sdk: ">=2.18.0 <4.0.0"