David PHAM-VAN

Improve PdfName conformance

... ... @@ -315,7 +315,27 @@ class PdfName extends PdfDataType {
@override
void output(PdfStream s) {
assert(value[0] == '/');
s.putString(value);
final bytes = <int>[];
for (final c in value.codeUnits) {
assert(c < 0xff && c > 0x00);
if (c < 0x21 ||
c > 0x7E ||
c == 0x23 ||
(c == 0x2f && bytes.isNotEmpty) ||
c == 0x5b ||
c == 0x5d ||
c == 0x28 ||
c == 0x3c ||
c == 0x3e) {
bytes.add(0x23);
final x = c.toRadixString(16).padLeft(2, '0');
bytes.addAll(x.codeUnits);
} else {
bytes.add(c);
}
}
s.putBytes(bytes);
}
}
... ...
... ... @@ -25,6 +25,12 @@ void main() {
expect(const PdfBool(false).toString(), 'false');
});
test('PdfDataTypes Name ', () {
expect(const PdfName('/Test').toString(), '/Test');
expect(const PdfName('/Type 1').toString(), '/Type#201');
expect(const PdfName('/Num#1').toString(), '/Num#231');
});
test('PdfDataTypes Num', () {
expect(const PdfNum(0).toString(), '0');
expect(const PdfNum(.5).toString(), '0.5');
... ...