David PHAM-VAN

Remove PdfSecString

... ... @@ -24,25 +24,41 @@ import 'stream.dart';
enum PdfStringFormat { binary, literal }
class PdfString extends PdfDataType {
const PdfString(this.value, [this.format = PdfStringFormat.literal]);
factory PdfString.fromString(String value) {
return PdfString(_string(value), PdfStringFormat.literal);
const PdfString(
this.value, {
this.format = PdfStringFormat.literal,
this.encrypted = true,
});
factory PdfString.fromString(
String value, {
bool encrypted = true,
}) {
return PdfString(_string(value),
format: PdfStringFormat.literal, encrypted: encrypted);
}
factory PdfString.fromStream(PdfStream value,
[PdfStringFormat format = PdfStringFormat.literal]) {
return PdfString(value.output(), format);
factory PdfString.fromStream(
PdfStream value, {
PdfStringFormat format = PdfStringFormat.literal,
bool encrypted = true,
}) {
return PdfString(value.output(), format: format, encrypted: encrypted);
}
factory PdfString.fromDate(DateTime date) {
return PdfString(_date(date));
factory PdfString.fromDate(
DateTime date, {
bool encrypted = true,
}) {
return PdfString(_date(date), encrypted: encrypted);
}
final Uint8List value;
final PdfStringFormat format;
final bool encrypted;
static Uint8List _string(String value) {
try {
return latin1.encode(value);
... ... @@ -168,7 +184,12 @@ class PdfString extends PdfDataType {
@override
void output(PdfObjectBase o, PdfStream s, [int? indent]) {
_output(s, value);
if (!encrypted || o.encryptCallback == null) {
return _output(s, value);
}
final enc = o.encryptCallback!(value, o);
_output(s, enc);
}
@override
... ... @@ -183,46 +204,3 @@ class PdfString extends PdfDataType {
@override
int get hashCode => value.hashCode;
}
class PdfSecString extends PdfString {
const PdfSecString(Uint8List value,
[PdfStringFormat format = PdfStringFormat.binary])
: super(value, format);
factory PdfSecString.fromString(
String value, [
PdfStringFormat format = PdfStringFormat.literal,
]) {
return PdfSecString(
PdfString._string(value),
format,
);
}
factory PdfSecString.fromStream(
PdfStream value, [
PdfStringFormat format = PdfStringFormat.literal,
]) {
return PdfSecString(
value.output(),
format,
);
}
factory PdfSecString.fromDate(DateTime date) {
return PdfSecString(
PdfString._date(date),
PdfStringFormat.literal,
);
}
@override
void output(PdfObjectBase o, PdfStream s, [int? indent]) {
if (o.encryptCallback == null) {
return super.output(o, s, indent);
}
final enc = o.encryptCallback!(value, o);
_output(s, enc);
}
}
... ...
... ... @@ -87,7 +87,7 @@ class PdfChoiceField extends PdfAnnotWidget {
g.setFillColor(textColor);
g.setFont(font, fontSize);
params['/DA'] = PdfSecString.fromStream(buf);
params['/DA'] = PdfString.fromStream(buf);
// What is /TU? Tooltip?
//params['/TU'] = PdfString.fromString('Select from list');
... ... @@ -285,11 +285,11 @@ abstract class PdfAnnotBase {
}
if (content != null) {
params['/Contents'] = PdfSecString.fromString(content!);
params['/Contents'] = PdfString.fromString(content!);
}
if (name != null) {
params['/NM'] = PdfSecString.fromString(name!);
params['/NM'] = PdfString.fromString(name!);
}
if (flags != null && flags!.isNotEmpty) {
... ... @@ -297,7 +297,7 @@ abstract class PdfAnnotBase {
}
if (date != null) {
params['/M'] = PdfSecString.fromDate(date!);
params['/M'] = PdfString.fromDate(date!);
}
if (color != null) {
... ... @@ -305,11 +305,11 @@ abstract class PdfAnnotBase {
}
if (subject != null) {
params['/Subj'] = PdfSecString.fromString(subject!);
params['/Subj'] = PdfString.fromString(subject!);
}
if (author != null) {
params['/T'] = PdfSecString.fromString(author!);
params['/T'] = PdfString.fromString(author!);
}
if (_appearances.isNotEmpty) {
... ... @@ -377,7 +377,7 @@ class PdfAnnotNamedLink extends PdfAnnotBase {
params['/A'] = PdfDict(
{
'/S': const PdfName('/GoTo'),
'/D': PdfSecString.fromString(dest),
'/D': PdfString.fromString(dest),
},
);
}
... ... @@ -413,7 +413,7 @@ class PdfAnnotUrlLink extends PdfAnnotBase {
params['/A'] = PdfDict(
{
'/S': const PdfName('/URI'),
'/URI': PdfSecString.fromString(url),
'/URI': PdfString.fromString(url),
},
);
}
... ... @@ -632,7 +632,7 @@ abstract class PdfAnnotWidget extends PdfAnnotBase {
params['/FT'] = PdfName(fieldType);
if (fieldName != null) {
params['/T'] = PdfSecString.fromString(fieldName!);
params['/T'] = PdfString.fromString(fieldName!);
}
final mk = PdfDict();
... ... @@ -847,10 +847,10 @@ class PdfFormField extends PdfAnnotWidget {
void build(PdfPage page, PdfObject object, PdfDict params) {
super.build(page, object, params);
if (alternateName != null) {
params['/TU'] = PdfSecString.fromString(alternateName!);
params['/TU'] = PdfString.fromString(alternateName!);
}
if (mappingName != null) {
params['/TM'] = PdfSecString.fromString(mappingName!);
params['/TM'] = PdfString.fromString(mappingName!);
}
params['/Ff'] = PdfNum(fieldFlagsValue);
... ... @@ -923,13 +923,13 @@ class PdfTextField extends PdfFormField {
final g = PdfGraphics(page, buf);
g.setFillColor(textColor);
g.setFont(font, fontSize);
params['/DA'] = PdfSecString.fromStream(buf);
params['/DA'] = PdfString.fromStream(buf);
if (value != null) {
params['/V'] = PdfSecString.fromString(value!);
params['/V'] = PdfString.fromString(value!);
}
if (defaultValue != null) {
params['/DV'] = PdfSecString.fromString(defaultValue!);
params['/DV'] = PdfString.fromString(defaultValue!);
}
if (textAlign != null) {
params['/Q'] = PdfNum(textAlign!.index);
... ...
... ... @@ -195,7 +195,8 @@ See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
/// Draw some text
void putText(PdfStream stream, String text) {
try {
PdfString(latin1.encode(text), PdfStringFormat.literal)
PdfString(latin1.encode(text),
format: PdfStringFormat.literal, encrypted: false)
.output(this, stream);
} catch (_) {
assert(() {
... ...
... ... @@ -30,28 +30,27 @@ class PdfInfo extends PdfObjectDict {
this.producer})
: super(pdfDocument) {
if (author != null) {
params['/Author'] = PdfSecString.fromString(author!);
params['/Author'] = PdfString.fromString(author!);
}
if (creator != null) {
params['/Creator'] = PdfSecString.fromString(creator!);
params['/Creator'] = PdfString.fromString(creator!);
}
if (title != null) {
params['/Title'] = PdfSecString.fromString(title!);
params['/Title'] = PdfString.fromString(title!);
}
if (subject != null) {
params['/Subject'] = PdfSecString.fromString(subject!);
params['/Subject'] = PdfString.fromString(subject!);
}
if (keywords != null) {
params['/Keywords'] = PdfSecString.fromString(keywords!);
params['/Keywords'] = PdfString.fromString(keywords!);
}
if (producer != null) {
params['/Producer'] =
PdfSecString.fromString('$producer ($_libraryName)');
params['/Producer'] = PdfString.fromString('$producer ($_libraryName)');
} else {
params['/Producer'] = PdfSecString.fromString(_libraryName);
params['/Producer'] = PdfString.fromString(_libraryName);
}
params['/CreationDate'] = PdfSecString.fromDate(DateTime.now());
params['/CreationDate'] = PdfString.fromDate(DateTime.now());
}
static const String _libraryName = 'https://github.com/DavBfr/dart_pdf';
... ...
... ... @@ -62,7 +62,7 @@ class PdfNames extends PdfObjectDict {
final keys = _dests.keys.toList()..sort();
for (final name in keys) {
dests.add(PdfSecString.fromString(name));
dests.add(PdfString.fromString(name));
dests.add(_dests[name]!);
}
... ... @@ -70,8 +70,8 @@ class PdfNames extends PdfObjectDict {
if (dests.values.isNotEmpty) {
dict['/Names'] = dests;
dict['/Limits'] = PdfArray([
PdfSecString.fromString(keys.first),
PdfSecString.fromString(keys.last),
PdfString.fromString(keys.first),
PdfString.fromString(keys.last),
]);
}
params['/Dests'] = dict;
... ...
... ... @@ -125,7 +125,7 @@ class PdfOutline extends PdfObjectDict {
// These are for kids only
if (parent != null) {
params['/Title'] = PdfSecString.fromString(title!);
params['/Title'] = PdfString.fromString(title!);
if (color != null) {
params['/C'] = PdfArray.fromColor(color!);
... ... @@ -136,7 +136,7 @@ class PdfOutline extends PdfObjectDict {
}
if (anchor != null) {
params['/Dest'] = PdfSecString.fromString(anchor!);
params['/Dest'] = PdfString.fromString(anchor!);
} else {
final dests = PdfArray();
dests.add(dest!.ref());
... ...
... ... @@ -76,7 +76,7 @@ class PdfPageLabel {
return PdfDict({
if (s != null) '/S': s,
if (prefix != null && prefix!.isNotEmpty)
'/P': PdfSecString.fromString(prefix!),
'/P': PdfString.fromString(prefix!),
if (subsequent != null) '/St': PdfNum(subsequent!)
});
}
... ...
... ... @@ -129,8 +129,8 @@ class PdfTtfFont extends PdfFont {
'/Subtype': const PdfName('/CIDFontType2'),
'/CIDSystemInfo': PdfDict({
'/Supplement': const PdfNum(0),
'/Registry': PdfSecString.fromString('Adobe'),
'/Ordering': PdfSecString.fromString('Identity-H'),
'/Registry': PdfString.fromString('Adobe'),
'/Ordering': PdfString.fromString('Identity-H'),
})
});
... ...
... ... @@ -130,8 +130,8 @@ class PdfOutput with PdfDiagnostic {
// the /Root catalog indirect reference (REQUIRED)
params['/Root'] = rootID!.ref();
final id =
PdfString(rootID!.pdfDocument.documentID, PdfStringFormat.binary);
final id = PdfString(rootID!.pdfDocument.documentID,
format: PdfStringFormat.binary, encrypted: false);
params['/ID'] = PdfArray([id, id]);
// the /Info reference (OPTIONAL)
... ...
... ... @@ -59,7 +59,7 @@ void main() {
expect(
PdfString(
Uint8List.fromList(const <int>[0, 1, 2, 3, 4, 5, 6]),
PdfStringFormat.binary,
format: PdfStringFormat.binary,
).toString(),
'<00010203040506>',
);
... ...