Skip to content

Commit

Permalink
Prefere unicode FontName
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Sep 13, 2021
1 parent 8d133f3 commit 4607273
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions pdf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix text justify with multiple paragraphs
- Apply Flutter 2.5 coding style
- Prefere unicode FontName

## 3.5.0

Expand Down
49 changes: 39 additions & 10 deletions pdf/lib/src/pdf/ttf_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ import 'package:meta/meta.dart';

import 'font_metrics.dart';

enum TtfParserName {
copyright,
fontFamily,
fontSubfamily,
uniqueID,
fullName,
version,
postScriptName,
trademark,
manufacturer,
designer,
description,
manufacturerURL,
designerURL,
license,
licenseURL,
reserved,
preferredFamily,
preferredSubfamily,
compatibleFullName,
sampleText,
postScriptFindFontName,
wwsFamily,
wwsSubfamily,
}

@immutable
class TtfGlyphInfo {
const TtfGlyphInfo(this.index, this.data, this.compounds);
Expand Down Expand Up @@ -71,7 +97,6 @@ class TtfParser {
assert(tableOffsets.containsKey(glyf_table),
'Unable to find the `glyf` table. This file is not a supported TTF font');

_parseFontName();
_parseCMap();
_parseIndexes();
_parseGlyphs();
Expand All @@ -89,7 +114,7 @@ class TtfParser {
final UnmodifiableByteDataView bytes;
final Map<String, int> tableOffsets = <String, int>{};
final Map<String, int> tableSize = <String, int>{};
String? _fontName;

final Map<int, int> charToGlyphIndexMap = <int, int>{};
final List<int> glyphOffsets = <int>[];
final Map<int, PdfFontMetrics> glyphInfoMap = <int, PdfFontMetrics>{};
Expand All @@ -115,42 +140,46 @@ class TtfParser {

int get numGlyphs => bytes.getUint16(tableOffsets[maxp_table]! + 4);

String? get fontName => _fontName;
String get fontName =>
getNameID(TtfParserName.postScriptName) ?? hashCode.toString();

bool get unicode => bytes.getUint32(0) == 0x10000;

// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
void _parseFontName() {
String? getNameID(TtfParserName fontNameID) {
final basePosition = tableOffsets[name_table]!;
// final format = bytes.getUint16(basePosition);
final count = bytes.getUint16(basePosition + 2);
final stringOffset = bytes.getUint16(basePosition + 4);
var pos = basePosition + 6;
String? _fontName;

for (var i = 0; i < count; i++) {
final platformID = bytes.getUint16(pos);
final nameID = bytes.getUint16(pos + 6);
final length = bytes.getUint16(pos + 8);
final offset = bytes.getUint16(pos + 10);
pos += 12;
if (platformID == 1 && nameID == 6) {

if (platformID == 1 && nameID == fontNameID.index) {
try {
_fontName = utf8.decode(bytes.buffer
.asUint8List(basePosition + stringOffset + offset, length));
return;
} catch (a) {
print('Error: $platformID $nameID $a');
}
}
if (platformID == 3 && nameID == 6) {

if (platformID == 3 && nameID == fontNameID.index) {
try {
_fontName = _decodeUtf16(bytes.buffer
return _decodeUtf16(bytes.buffer
.asUint8List(basePosition + stringOffset + offset, length));
return;
} catch (a) {
print('Error: $platformID $nameID $a');
}
}
}
_fontName = hashCode.toString();
return _fontName;
}

void _parseCMap() {
Expand Down
2 changes: 1 addition & 1 deletion pdf/lib/src/pdf/ttffont.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PdfTtfFont extends PdfFont {
final TtfParser font;

@override
String get fontName => font.fontName!;
String get fontName => font.fontName;

@override
double get ascent => font.ascent.toDouble() / font.unitsPerEm;
Expand Down
16 changes: 14 additions & 2 deletions pdf/lib/src/widgets/font.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Font {
Type1Fonts.zapfDingbats: 'ZapfDingbats'
};

String? get fontName => _type1Map[font];
String get fontName => _type1Map[font]!;

@protected
PdfFont buildFont(PdfDocument pdfDocument) {
Expand Down Expand Up @@ -153,7 +153,7 @@ class TtfFont extends Font {
}

@override
String? get fontName {
String get fontName {
if (_pdfFont != null) {
return _pdfFont!.fontName;
}
Expand All @@ -162,6 +162,18 @@ class TtfFont extends Font {
return font.fontName;
}

String? fontNameID(TtfParserName nameID) {
final pdfFont = _pdfFont;
if (pdfFont != null) {
if (pdfFont is PdfTtfFont) {
return pdfFont.font.getNameID(nameID);
}
}

final font = TtfParser(data);
return font.getNameID(nameID);
}

@override
String toString() {
final font = TtfParser(data);
Expand Down
2 changes: 1 addition & 1 deletion pdf/test/widget_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void main() {
build: (Context context) => ListView(
children: <Widget>[
Text(
style.font!.fontName!,
style.font!.fontName,
style: style,
),
],
Expand Down

0 comments on commit 4607273

Please sign in to comment.