David PHAM-VAN

Add unicode support for annotations and info block

... ... @@ -8,6 +8,7 @@
* Implement drawShape
* Add support for Jpeg images
* Fix numeric conversions in graphic operations
* Add unicode support for annotations and info block
# 1.0.8
* Fix monospace TTF font loading
... ...
... ... @@ -22,6 +22,7 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:utf/utf.dart';
import 'package:vector_math/vector_math_64.dart';
part 'src/annotation.dart';
... ...
... ... @@ -97,7 +97,7 @@ class PdfAnnot extends PdfObject {
// Now the annotation subtypes
if (subtype == "/Text") {
params["/Contents"] = PdfStream.text(content);
params["/Contents"] = PdfStream()..putLiteral(content);
} else if (subtype == "/Link") {
var dests = List<PdfStream>();
dests.add(dest.ref());
... ...
... ... @@ -96,7 +96,6 @@ class PdfDocument {
// Now create some standard objects
pdfPageList = PdfPageList(this);
catalog = PdfCatalog(this, pdfPageList, pageMode);
info = PdfInfo(this);
}
/// This returns a specific page. It's used mainly when using a
... ...
... ... @@ -19,28 +19,43 @@
part of pdf;
class PdfInfo extends PdfObject {
String author;
String creator;
String title;
String subject;
String keywords;
static const String _libraryName = "https://github.com/DavBfr/dart_pdf";
final String author;
final String creator;
final String title;
final String subject;
final String keywords;
final String producer;
/// @param title Title of this document
PdfInfo(PdfDocument pdfDocument,
{this.title, this.author, this.creator, this.subject, this.keywords})
{this.title,
this.author,
this.creator,
this.subject,
this.keywords,
this.producer})
: super(pdfDocument, null) {
params["/Producer"] = PdfStream.text("dpdf - David PHAM-VAN");
}
/// @param os OutputStream to send the object to
@override
void _prepare() {
super._prepare();
if (author != null) params["/Author"] = PdfStream.text(author);
if (creator != null) params["/Creator"] = PdfStream.text(creator);
if (title != null) params["/Title"] = PdfStream.text(title);
if (subject != null) params["/Subject"] = PdfStream.text(subject);
if (keywords != null) params["/Keywords"] = PdfStream.text(keywords);
if (author != null) {
params["/Author"] = PdfStream()..putLiteral(author);
}
if (creator != null) {
params["/Creator"] = PdfStream()..putLiteral(creator);
}
if (title != null) {
params["/Title"] = PdfStream()..putLiteral(title);
}
if (subject != null) {
params["/Subject"] = PdfStream()..putLiteral(subject);
}
if (keywords != null) {
params["/Keywords"] = PdfStream()..putLiteral(keywords);
}
if (producer != null) {
params["/Producer"] = PdfStream()
..putLiteral("$producer ($_libraryName)");
} else {
params["/Producer"] = PdfStream()..putLiteral(_libraryName);
}
}
}
... ...
... ... @@ -60,31 +60,61 @@ class PdfStream {
static PdfStream num(double d) => PdfStream()..putNum(d);
static PdfStream intNum(int i) => PdfStream()..putString(i.toString());
/// Escape special characters
/// \ddd Character code ddd (octal)
void putTextBytes(List<int> s) {
for (var c in s) {
switch (c) {
case 0x0a: // \n Line feed (LF)
_stream.add(0x5c);
_stream.add(0x6e);
break;
case 0x0d: // \r Carriage return (CR)
_stream.add(0x5c);
_stream.add(0x72);
break;
case 0x09: // \t Horizontal tab (HT)
_stream.add(0x5c);
_stream.add(0x74);
break;
case 0x08: // \b Backspace (BS)
_stream.add(0x5c);
_stream.add(0x62);
break;
case 0x0c: // \f Form feed (FF)
_stream.add(0x5c);
_stream.add(0x66);
break;
case 0x28: // \( Left parenthesis
_stream.add(0x5c);
_stream.add(0x28);
break;
case 0x29: // \) Right parenthesis
_stream.add(0x5c);
_stream.add(0x29);
break;
case 0x5c: // \\ Backslash
_stream.add(0x5c);
_stream.add(0x5c);
break;
default:
_stream.add(c);
}
}
}
void putText(String s) {
// Escape special characters
// \n Line feed (LF)
// \r Carriage return (CR)
// \t Horizontal tab (HT)
// \b Backspace (BS)
// \f Form feed (FF)
// \( Left parenthesis
// \) Right parenthesis
// \\ Backslash
// \ddd Character code ddd (octal)
s = s
.replaceAll('\\', '\\\\')
.replaceAll('(', '\\(')
.replaceAll(')', '\\)')
.replaceAll('\n', '\\n')
.replaceAll('\t', '\\t')
.replaceAll('\b', '\\b')
.replaceAll('\f', '\\f')
.replaceAll('\r', '\\r');
putBytes(latin1.encode('(' + s + ')'));
putBytes(latin1.encode('('));
putTextBytes(latin1.encode(s));
putBytes(latin1.encode(')'));
}
static PdfStream text(String s) => PdfStream()..putText(s);
void putLiteral(String s) {
putBytes(latin1.encode('('));
putBytes([0xfe, 0xff]);
putTextBytes(encodeUtf16be(s));
putBytes(latin1.encode(')'));
}
void putBool(bool value) {
putString(value ? "true" : "false");
... ...
... ... @@ -10,6 +10,7 @@ environment:
dependencies:
meta: "^1.1.5"
vector_math: "^2.0.0"
utf: "^0.9.0"
dev_dependencies:
test: any
... ...
... ... @@ -12,11 +12,11 @@ void main() {
img.fillRange(0, img.length - 1, 0x12345678);
var pdf = PdfDocument(deflate: zlib.encode);
var i = pdf.info;
i.author = "David PHAM-VAN";
i.creator = i.author;
i.title = "My Title";
i.subject = "My Subject";
pdf.info = PdfInfo(pdf,
author: "David PHAM-VAN",
creator: "David PHAM-VAN",
title: "My Title",
subject: "My Subject");
var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var g = page.getGraphics();
... ...
... ... @@ -7,11 +7,6 @@ import 'package:test/test.dart';
void main() {
test('Pdf', () {
var pdf = PdfDocument();
var i = pdf.info;
i.author = "David PHAM-VAN";
i.creator = i.author;
i.title = "My Title";
i.subject = "My Subject";
var page = PdfPage(pdf, pageFormat: const PdfPageFormat(500.0, 300.0));
var g = page.getGraphics();
... ...