Showing
10 changed files
with
68 additions
and
90 deletions
| @@ -24,25 +24,41 @@ import 'stream.dart'; | @@ -24,25 +24,41 @@ import 'stream.dart'; | ||
| 24 | enum PdfStringFormat { binary, literal } | 24 | enum PdfStringFormat { binary, literal } |
| 25 | 25 | ||
| 26 | class PdfString extends PdfDataType { | 26 | class PdfString extends PdfDataType { |
| 27 | - const PdfString(this.value, [this.format = PdfStringFormat.literal]); | ||
| 28 | - | ||
| 29 | - factory PdfString.fromString(String value) { | ||
| 30 | - return PdfString(_string(value), PdfStringFormat.literal); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - factory PdfString.fromStream(PdfStream value, | ||
| 34 | - [PdfStringFormat format = PdfStringFormat.literal]) { | ||
| 35 | - return PdfString(value.output(), format); | 27 | + const PdfString( |
| 28 | + this.value, { | ||
| 29 | + this.format = PdfStringFormat.literal, | ||
| 30 | + this.encrypted = true, | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + factory PdfString.fromString( | ||
| 34 | + String value, { | ||
| 35 | + bool encrypted = true, | ||
| 36 | + }) { | ||
| 37 | + return PdfString(_string(value), | ||
| 38 | + format: PdfStringFormat.literal, encrypted: encrypted); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + factory PdfString.fromStream( | ||
| 42 | + PdfStream value, { | ||
| 43 | + PdfStringFormat format = PdfStringFormat.literal, | ||
| 44 | + bool encrypted = true, | ||
| 45 | + }) { | ||
| 46 | + return PdfString(value.output(), format: format, encrypted: encrypted); | ||
| 36 | } | 47 | } |
| 37 | 48 | ||
| 38 | - factory PdfString.fromDate(DateTime date) { | ||
| 39 | - return PdfString(_date(date)); | 49 | + factory PdfString.fromDate( |
| 50 | + DateTime date, { | ||
| 51 | + bool encrypted = true, | ||
| 52 | + }) { | ||
| 53 | + return PdfString(_date(date), encrypted: encrypted); | ||
| 40 | } | 54 | } |
| 41 | 55 | ||
| 42 | final Uint8List value; | 56 | final Uint8List value; |
| 43 | 57 | ||
| 44 | final PdfStringFormat format; | 58 | final PdfStringFormat format; |
| 45 | 59 | ||
| 60 | + final bool encrypted; | ||
| 61 | + | ||
| 46 | static Uint8List _string(String value) { | 62 | static Uint8List _string(String value) { |
| 47 | try { | 63 | try { |
| 48 | return latin1.encode(value); | 64 | return latin1.encode(value); |
| @@ -168,7 +184,12 @@ class PdfString extends PdfDataType { | @@ -168,7 +184,12 @@ class PdfString extends PdfDataType { | ||
| 168 | 184 | ||
| 169 | @override | 185 | @override |
| 170 | void output(PdfObjectBase o, PdfStream s, [int? indent]) { | 186 | void output(PdfObjectBase o, PdfStream s, [int? indent]) { |
| 171 | - _output(s, value); | 187 | + if (!encrypted || o.encryptCallback == null) { |
| 188 | + return _output(s, value); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + final enc = o.encryptCallback!(value, o); | ||
| 192 | + _output(s, enc); | ||
| 172 | } | 193 | } |
| 173 | 194 | ||
| 174 | @override | 195 | @override |
| @@ -183,46 +204,3 @@ class PdfString extends PdfDataType { | @@ -183,46 +204,3 @@ class PdfString extends PdfDataType { | ||
| 183 | @override | 204 | @override |
| 184 | int get hashCode => value.hashCode; | 205 | int get hashCode => value.hashCode; |
| 185 | } | 206 | } |
| 186 | - | ||
| 187 | -class PdfSecString extends PdfString { | ||
| 188 | - const PdfSecString(Uint8List value, | ||
| 189 | - [PdfStringFormat format = PdfStringFormat.binary]) | ||
| 190 | - : super(value, format); | ||
| 191 | - | ||
| 192 | - factory PdfSecString.fromString( | ||
| 193 | - String value, [ | ||
| 194 | - PdfStringFormat format = PdfStringFormat.literal, | ||
| 195 | - ]) { | ||
| 196 | - return PdfSecString( | ||
| 197 | - PdfString._string(value), | ||
| 198 | - format, | ||
| 199 | - ); | ||
| 200 | - } | ||
| 201 | - | ||
| 202 | - factory PdfSecString.fromStream( | ||
| 203 | - PdfStream value, [ | ||
| 204 | - PdfStringFormat format = PdfStringFormat.literal, | ||
| 205 | - ]) { | ||
| 206 | - return PdfSecString( | ||
| 207 | - value.output(), | ||
| 208 | - format, | ||
| 209 | - ); | ||
| 210 | - } | ||
| 211 | - | ||
| 212 | - factory PdfSecString.fromDate(DateTime date) { | ||
| 213 | - return PdfSecString( | ||
| 214 | - PdfString._date(date), | ||
| 215 | - PdfStringFormat.literal, | ||
| 216 | - ); | ||
| 217 | - } | ||
| 218 | - | ||
| 219 | - @override | ||
| 220 | - void output(PdfObjectBase o, PdfStream s, [int? indent]) { | ||
| 221 | - if (o.encryptCallback == null) { | ||
| 222 | - return super.output(o, s, indent); | ||
| 223 | - } | ||
| 224 | - | ||
| 225 | - final enc = o.encryptCallback!(value, o); | ||
| 226 | - _output(s, enc); | ||
| 227 | - } | ||
| 228 | -} |
| @@ -87,7 +87,7 @@ class PdfChoiceField extends PdfAnnotWidget { | @@ -87,7 +87,7 @@ class PdfChoiceField extends PdfAnnotWidget { | ||
| 87 | g.setFillColor(textColor); | 87 | g.setFillColor(textColor); |
| 88 | g.setFont(font, fontSize); | 88 | g.setFont(font, fontSize); |
| 89 | 89 | ||
| 90 | - params['/DA'] = PdfSecString.fromStream(buf); | 90 | + params['/DA'] = PdfString.fromStream(buf); |
| 91 | 91 | ||
| 92 | // What is /TU? Tooltip? | 92 | // What is /TU? Tooltip? |
| 93 | //params['/TU'] = PdfString.fromString('Select from list'); | 93 | //params['/TU'] = PdfString.fromString('Select from list'); |
| @@ -285,11 +285,11 @@ abstract class PdfAnnotBase { | @@ -285,11 +285,11 @@ abstract class PdfAnnotBase { | ||
| 285 | } | 285 | } |
| 286 | 286 | ||
| 287 | if (content != null) { | 287 | if (content != null) { |
| 288 | - params['/Contents'] = PdfSecString.fromString(content!); | 288 | + params['/Contents'] = PdfString.fromString(content!); |
| 289 | } | 289 | } |
| 290 | 290 | ||
| 291 | if (name != null) { | 291 | if (name != null) { |
| 292 | - params['/NM'] = PdfSecString.fromString(name!); | 292 | + params['/NM'] = PdfString.fromString(name!); |
| 293 | } | 293 | } |
| 294 | 294 | ||
| 295 | if (flags != null && flags!.isNotEmpty) { | 295 | if (flags != null && flags!.isNotEmpty) { |
| @@ -297,7 +297,7 @@ abstract class PdfAnnotBase { | @@ -297,7 +297,7 @@ abstract class PdfAnnotBase { | ||
| 297 | } | 297 | } |
| 298 | 298 | ||
| 299 | if (date != null) { | 299 | if (date != null) { |
| 300 | - params['/M'] = PdfSecString.fromDate(date!); | 300 | + params['/M'] = PdfString.fromDate(date!); |
| 301 | } | 301 | } |
| 302 | 302 | ||
| 303 | if (color != null) { | 303 | if (color != null) { |
| @@ -305,11 +305,11 @@ abstract class PdfAnnotBase { | @@ -305,11 +305,11 @@ abstract class PdfAnnotBase { | ||
| 305 | } | 305 | } |
| 306 | 306 | ||
| 307 | if (subject != null) { | 307 | if (subject != null) { |
| 308 | - params['/Subj'] = PdfSecString.fromString(subject!); | 308 | + params['/Subj'] = PdfString.fromString(subject!); |
| 309 | } | 309 | } |
| 310 | 310 | ||
| 311 | if (author != null) { | 311 | if (author != null) { |
| 312 | - params['/T'] = PdfSecString.fromString(author!); | 312 | + params['/T'] = PdfString.fromString(author!); |
| 313 | } | 313 | } |
| 314 | 314 | ||
| 315 | if (_appearances.isNotEmpty) { | 315 | if (_appearances.isNotEmpty) { |
| @@ -377,7 +377,7 @@ class PdfAnnotNamedLink extends PdfAnnotBase { | @@ -377,7 +377,7 @@ class PdfAnnotNamedLink extends PdfAnnotBase { | ||
| 377 | params['/A'] = PdfDict( | 377 | params['/A'] = PdfDict( |
| 378 | { | 378 | { |
| 379 | '/S': const PdfName('/GoTo'), | 379 | '/S': const PdfName('/GoTo'), |
| 380 | - '/D': PdfSecString.fromString(dest), | 380 | + '/D': PdfString.fromString(dest), |
| 381 | }, | 381 | }, |
| 382 | ); | 382 | ); |
| 383 | } | 383 | } |
| @@ -413,7 +413,7 @@ class PdfAnnotUrlLink extends PdfAnnotBase { | @@ -413,7 +413,7 @@ class PdfAnnotUrlLink extends PdfAnnotBase { | ||
| 413 | params['/A'] = PdfDict( | 413 | params['/A'] = PdfDict( |
| 414 | { | 414 | { |
| 415 | '/S': const PdfName('/URI'), | 415 | '/S': const PdfName('/URI'), |
| 416 | - '/URI': PdfSecString.fromString(url), | 416 | + '/URI': PdfString.fromString(url), |
| 417 | }, | 417 | }, |
| 418 | ); | 418 | ); |
| 419 | } | 419 | } |
| @@ -632,7 +632,7 @@ abstract class PdfAnnotWidget extends PdfAnnotBase { | @@ -632,7 +632,7 @@ abstract class PdfAnnotWidget extends PdfAnnotBase { | ||
| 632 | params['/FT'] = PdfName(fieldType); | 632 | params['/FT'] = PdfName(fieldType); |
| 633 | 633 | ||
| 634 | if (fieldName != null) { | 634 | if (fieldName != null) { |
| 635 | - params['/T'] = PdfSecString.fromString(fieldName!); | 635 | + params['/T'] = PdfString.fromString(fieldName!); |
| 636 | } | 636 | } |
| 637 | 637 | ||
| 638 | final mk = PdfDict(); | 638 | final mk = PdfDict(); |
| @@ -847,10 +847,10 @@ class PdfFormField extends PdfAnnotWidget { | @@ -847,10 +847,10 @@ class PdfFormField extends PdfAnnotWidget { | ||
| 847 | void build(PdfPage page, PdfObject object, PdfDict params) { | 847 | void build(PdfPage page, PdfObject object, PdfDict params) { |
| 848 | super.build(page, object, params); | 848 | super.build(page, object, params); |
| 849 | if (alternateName != null) { | 849 | if (alternateName != null) { |
| 850 | - params['/TU'] = PdfSecString.fromString(alternateName!); | 850 | + params['/TU'] = PdfString.fromString(alternateName!); |
| 851 | } | 851 | } |
| 852 | if (mappingName != null) { | 852 | if (mappingName != null) { |
| 853 | - params['/TM'] = PdfSecString.fromString(mappingName!); | 853 | + params['/TM'] = PdfString.fromString(mappingName!); |
| 854 | } | 854 | } |
| 855 | 855 | ||
| 856 | params['/Ff'] = PdfNum(fieldFlagsValue); | 856 | params['/Ff'] = PdfNum(fieldFlagsValue); |
| @@ -923,13 +923,13 @@ class PdfTextField extends PdfFormField { | @@ -923,13 +923,13 @@ class PdfTextField extends PdfFormField { | ||
| 923 | final g = PdfGraphics(page, buf); | 923 | final g = PdfGraphics(page, buf); |
| 924 | g.setFillColor(textColor); | 924 | g.setFillColor(textColor); |
| 925 | g.setFont(font, fontSize); | 925 | g.setFont(font, fontSize); |
| 926 | - params['/DA'] = PdfSecString.fromStream(buf); | 926 | + params['/DA'] = PdfString.fromStream(buf); |
| 927 | 927 | ||
| 928 | if (value != null) { | 928 | if (value != null) { |
| 929 | - params['/V'] = PdfSecString.fromString(value!); | 929 | + params['/V'] = PdfString.fromString(value!); |
| 930 | } | 930 | } |
| 931 | if (defaultValue != null) { | 931 | if (defaultValue != null) { |
| 932 | - params['/DV'] = PdfSecString.fromString(defaultValue!); | 932 | + params['/DV'] = PdfString.fromString(defaultValue!); |
| 933 | } | 933 | } |
| 934 | if (textAlign != null) { | 934 | if (textAlign != null) { |
| 935 | params['/Q'] = PdfNum(textAlign!.index); | 935 | params['/Q'] = PdfNum(textAlign!.index); |
| @@ -195,7 +195,8 @@ See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management | @@ -195,7 +195,8 @@ See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management | ||
| 195 | /// Draw some text | 195 | /// Draw some text |
| 196 | void putText(PdfStream stream, String text) { | 196 | void putText(PdfStream stream, String text) { |
| 197 | try { | 197 | try { |
| 198 | - PdfString(latin1.encode(text), PdfStringFormat.literal) | 198 | + PdfString(latin1.encode(text), |
| 199 | + format: PdfStringFormat.literal, encrypted: false) | ||
| 199 | .output(this, stream); | 200 | .output(this, stream); |
| 200 | } catch (_) { | 201 | } catch (_) { |
| 201 | assert(() { | 202 | assert(() { |
| @@ -30,28 +30,27 @@ class PdfInfo extends PdfObjectDict { | @@ -30,28 +30,27 @@ class PdfInfo extends PdfObjectDict { | ||
| 30 | this.producer}) | 30 | this.producer}) |
| 31 | : super(pdfDocument) { | 31 | : super(pdfDocument) { |
| 32 | if (author != null) { | 32 | if (author != null) { |
| 33 | - params['/Author'] = PdfSecString.fromString(author!); | 33 | + params['/Author'] = PdfString.fromString(author!); |
| 34 | } | 34 | } |
| 35 | if (creator != null) { | 35 | if (creator != null) { |
| 36 | - params['/Creator'] = PdfSecString.fromString(creator!); | 36 | + params['/Creator'] = PdfString.fromString(creator!); |
| 37 | } | 37 | } |
| 38 | if (title != null) { | 38 | if (title != null) { |
| 39 | - params['/Title'] = PdfSecString.fromString(title!); | 39 | + params['/Title'] = PdfString.fromString(title!); |
| 40 | } | 40 | } |
| 41 | if (subject != null) { | 41 | if (subject != null) { |
| 42 | - params['/Subject'] = PdfSecString.fromString(subject!); | 42 | + params['/Subject'] = PdfString.fromString(subject!); |
| 43 | } | 43 | } |
| 44 | if (keywords != null) { | 44 | if (keywords != null) { |
| 45 | - params['/Keywords'] = PdfSecString.fromString(keywords!); | 45 | + params['/Keywords'] = PdfString.fromString(keywords!); |
| 46 | } | 46 | } |
| 47 | if (producer != null) { | 47 | if (producer != null) { |
| 48 | - params['/Producer'] = | ||
| 49 | - PdfSecString.fromString('$producer ($_libraryName)'); | 48 | + params['/Producer'] = PdfString.fromString('$producer ($_libraryName)'); |
| 50 | } else { | 49 | } else { |
| 51 | - params['/Producer'] = PdfSecString.fromString(_libraryName); | 50 | + params['/Producer'] = PdfString.fromString(_libraryName); |
| 52 | } | 51 | } |
| 53 | 52 | ||
| 54 | - params['/CreationDate'] = PdfSecString.fromDate(DateTime.now()); | 53 | + params['/CreationDate'] = PdfString.fromDate(DateTime.now()); |
| 55 | } | 54 | } |
| 56 | 55 | ||
| 57 | static const String _libraryName = 'https://github.com/DavBfr/dart_pdf'; | 56 | static const String _libraryName = 'https://github.com/DavBfr/dart_pdf'; |
| @@ -62,7 +62,7 @@ class PdfNames extends PdfObjectDict { | @@ -62,7 +62,7 @@ class PdfNames extends PdfObjectDict { | ||
| 62 | final keys = _dests.keys.toList()..sort(); | 62 | final keys = _dests.keys.toList()..sort(); |
| 63 | 63 | ||
| 64 | for (final name in keys) { | 64 | for (final name in keys) { |
| 65 | - dests.add(PdfSecString.fromString(name)); | 65 | + dests.add(PdfString.fromString(name)); |
| 66 | dests.add(_dests[name]!); | 66 | dests.add(_dests[name]!); |
| 67 | } | 67 | } |
| 68 | 68 | ||
| @@ -70,8 +70,8 @@ class PdfNames extends PdfObjectDict { | @@ -70,8 +70,8 @@ class PdfNames extends PdfObjectDict { | ||
| 70 | if (dests.values.isNotEmpty) { | 70 | if (dests.values.isNotEmpty) { |
| 71 | dict['/Names'] = dests; | 71 | dict['/Names'] = dests; |
| 72 | dict['/Limits'] = PdfArray([ | 72 | dict['/Limits'] = PdfArray([ |
| 73 | - PdfSecString.fromString(keys.first), | ||
| 74 | - PdfSecString.fromString(keys.last), | 73 | + PdfString.fromString(keys.first), |
| 74 | + PdfString.fromString(keys.last), | ||
| 75 | ]); | 75 | ]); |
| 76 | } | 76 | } |
| 77 | params['/Dests'] = dict; | 77 | params['/Dests'] = dict; |
| @@ -125,7 +125,7 @@ class PdfOutline extends PdfObjectDict { | @@ -125,7 +125,7 @@ class PdfOutline extends PdfObjectDict { | ||
| 125 | 125 | ||
| 126 | // These are for kids only | 126 | // These are for kids only |
| 127 | if (parent != null) { | 127 | if (parent != null) { |
| 128 | - params['/Title'] = PdfSecString.fromString(title!); | 128 | + params['/Title'] = PdfString.fromString(title!); |
| 129 | 129 | ||
| 130 | if (color != null) { | 130 | if (color != null) { |
| 131 | params['/C'] = PdfArray.fromColor(color!); | 131 | params['/C'] = PdfArray.fromColor(color!); |
| @@ -136,7 +136,7 @@ class PdfOutline extends PdfObjectDict { | @@ -136,7 +136,7 @@ class PdfOutline extends PdfObjectDict { | ||
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | if (anchor != null) { | 138 | if (anchor != null) { |
| 139 | - params['/Dest'] = PdfSecString.fromString(anchor!); | 139 | + params['/Dest'] = PdfString.fromString(anchor!); |
| 140 | } else { | 140 | } else { |
| 141 | final dests = PdfArray(); | 141 | final dests = PdfArray(); |
| 142 | dests.add(dest!.ref()); | 142 | dests.add(dest!.ref()); |
| @@ -76,7 +76,7 @@ class PdfPageLabel { | @@ -76,7 +76,7 @@ class PdfPageLabel { | ||
| 76 | return PdfDict({ | 76 | return PdfDict({ |
| 77 | if (s != null) '/S': s, | 77 | if (s != null) '/S': s, |
| 78 | if (prefix != null && prefix!.isNotEmpty) | 78 | if (prefix != null && prefix!.isNotEmpty) |
| 79 | - '/P': PdfSecString.fromString(prefix!), | 79 | + '/P': PdfString.fromString(prefix!), |
| 80 | if (subsequent != null) '/St': PdfNum(subsequent!) | 80 | if (subsequent != null) '/St': PdfNum(subsequent!) |
| 81 | }); | 81 | }); |
| 82 | } | 82 | } |
| @@ -129,8 +129,8 @@ class PdfTtfFont extends PdfFont { | @@ -129,8 +129,8 @@ class PdfTtfFont extends PdfFont { | ||
| 129 | '/Subtype': const PdfName('/CIDFontType2'), | 129 | '/Subtype': const PdfName('/CIDFontType2'), |
| 130 | '/CIDSystemInfo': PdfDict({ | 130 | '/CIDSystemInfo': PdfDict({ |
| 131 | '/Supplement': const PdfNum(0), | 131 | '/Supplement': const PdfNum(0), |
| 132 | - '/Registry': PdfSecString.fromString('Adobe'), | ||
| 133 | - '/Ordering': PdfSecString.fromString('Identity-H'), | 132 | + '/Registry': PdfString.fromString('Adobe'), |
| 133 | + '/Ordering': PdfString.fromString('Identity-H'), | ||
| 134 | }) | 134 | }) |
| 135 | }); | 135 | }); |
| 136 | 136 |
| @@ -130,8 +130,8 @@ class PdfOutput with PdfDiagnostic { | @@ -130,8 +130,8 @@ class PdfOutput with PdfDiagnostic { | ||
| 130 | 130 | ||
| 131 | // the /Root catalog indirect reference (REQUIRED) | 131 | // the /Root catalog indirect reference (REQUIRED) |
| 132 | params['/Root'] = rootID!.ref(); | 132 | params['/Root'] = rootID!.ref(); |
| 133 | - final id = | ||
| 134 | - PdfString(rootID!.pdfDocument.documentID, PdfStringFormat.binary); | 133 | + final id = PdfString(rootID!.pdfDocument.documentID, |
| 134 | + format: PdfStringFormat.binary, encrypted: false); | ||
| 135 | params['/ID'] = PdfArray([id, id]); | 135 | params['/ID'] = PdfArray([id, id]); |
| 136 | 136 | ||
| 137 | // the /Info reference (OPTIONAL) | 137 | // the /Info reference (OPTIONAL) |
| @@ -59,7 +59,7 @@ void main() { | @@ -59,7 +59,7 @@ void main() { | ||
| 59 | expect( | 59 | expect( |
| 60 | PdfString( | 60 | PdfString( |
| 61 | Uint8List.fromList(const <int>[0, 1, 2, 3, 4, 5, 6]), | 61 | Uint8List.fromList(const <int>[0, 1, 2, 3, 4, 5, 6]), |
| 62 | - PdfStringFormat.binary, | 62 | + format: PdfStringFormat.binary, |
| 63 | ).toString(), | 63 | ).toString(), |
| 64 | '<00010203040506>', | 64 | '<00010203040506>', |
| 65 | ); | 65 | ); |
-
Please register or login to post a comment