David PHAM-VAN

Remove num functions from stream object

... ... @@ -24,7 +24,7 @@ abstract class PdfDataType {
void output(PdfStream s);
PdfStream toStream() {
PdfStream _toStream() {
final PdfStream s = PdfStream();
output(s);
return s;
... ... @@ -32,11 +32,12 @@ abstract class PdfDataType {
@override
String toString() {
return String.fromCharCodes(toStream().output());
return String.fromCharCodes(_toStream().output());
}
@visibleForTesting
Uint8List toList() {
return toStream().output();
return _toStream().output();
}
}
... ... @@ -52,7 +53,13 @@ class PdfBool extends PdfDataType {
}
class PdfNum extends PdfDataType {
const PdfNum(this.value);
const PdfNum(this.value)
: assert(value != null),
assert(value != double.infinity),
assert(value != double.nan),
assert(value != double.negativeInfinity);
static const int precision = 5;
final num value;
... ... @@ -61,7 +68,23 @@ class PdfNum extends PdfDataType {
if (value is int) {
s.putString(value.toInt().toString());
} else {
s.putNum(value);
s.putString(value.toStringAsFixed(precision));
}
}
}
class PdfNumList extends PdfDataType {
PdfNumList(this.values) : assert(values != null);
final List<num> values;
@override
void output(PdfStream s) {
for (int n = 0; n < values.length; n++) {
if (n > 0) {
s.putByte(0x20);
}
PdfNum(values[n]).output(s);
}
}
}
... ... @@ -102,6 +125,49 @@ class PdfString extends PdfDataType {
return _string('D:$year$month$day$hour$minute${second}Z');
}
/// Escape special characters
/// \ddd Character code ddd (octal)
void _putTextBytes(PdfStream s, List<int> b) {
for (int c in b) {
switch (c) {
case 0x0a: // \n Line feed (LF)
s.putByte(0x5c);
s.putByte(0x6e);
break;
case 0x0d: // \r Carriage return (CR)
s.putByte(0x5c);
s.putByte(0x72);
break;
case 0x09: // \t Horizontal tab (HT)
s.putByte(0x5c);
s.putByte(0x74);
break;
case 0x08: // \b Backspace (BS)
s.putByte(0x5c);
s.putByte(0x62);
break;
case 0x0c: // \f Form feed (FF)
s.putByte(0x5c);
s.putByte(0x66);
break;
case 0x28: // \( Left parenthesis
s.putByte(0x5c);
s.putByte(0x28);
break;
case 0x29: // \) Right parenthesis
s.putByte(0x5c);
s.putByte(0x29);
break;
case 0x5c: // \\ Backslash
s.putByte(0x5c);
s.putByte(0x5c);
break;
default:
s.putByte(c);
}
}
}
/// Returns the ASCII/Unicode code unit corresponding to the hexadecimal digit
/// [digit].
int _codeUnitForDigit(int digit) =>
... ... @@ -116,11 +182,10 @@ class PdfString extends PdfDataType {
s.putByte(_codeUnitForDigit(byte & 0x0F));
}
s.putByte(0x3e);
break;
case PdfStringFormat.litteral:
s.putByte(40);
s.putTextBytes(value);
_putTextBytes(s, value);
s.putByte(41);
break;
}
... ...
... ... @@ -172,10 +172,7 @@ See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
void putText(PdfStream stream, String text) {
try {
stream
..putByte(40)
..putTextBytes(latin1.encode(text))
..putByte(41);
PdfString(latin1.encode(text), PdfStringFormat.litteral).output(stream);
} catch (_) {
assert(() {
print(_cannotDecodeMessage);
... ...
... ... @@ -138,28 +138,28 @@ class PdfGraphics {
buf.putString('q ');
switch (img.orientation) {
case PdfImageOrientation.topLeft:
buf.putNumList(<double>[w, 0, 0, h, x, y]);
PdfNumList(<double>[w, 0, 0, h, x, y]).output(buf);
break;
case PdfImageOrientation.topRight:
buf.putNumList(<double>[-w, 0, 0, h, w + x, y]);
PdfNumList(<double>[-w, 0, 0, h, w + x, y]).output(buf);
break;
case PdfImageOrientation.bottomRight:
buf.putNumList(<double>[-w, 0, 0, -h, w + x, h + y]);
PdfNumList(<double>[-w, 0, 0, -h, w + x, h + y]).output(buf);
break;
case PdfImageOrientation.bottomLeft:
buf.putNumList(<double>[w, 0, 0, -h, x, h + y]);
PdfNumList(<double>[w, 0, 0, -h, x, h + y]).output(buf);
break;
case PdfImageOrientation.leftTop:
buf.putNumList(<double>[0, -h, -w, 0, w + x, h + y]);
PdfNumList(<double>[0, -h, -w, 0, w + x, h + y]).output(buf);
break;
case PdfImageOrientation.rightTop:
buf.putNumList(<double>[0, -h, w, 0, x, h + y]);
PdfNumList(<double>[0, -h, w, 0, x, h + y]).output(buf);
break;
case PdfImageOrientation.rightBottom:
buf.putNumList(<double>[0, h, w, 0, x, y]);
PdfNumList(<double>[0, h, w, 0, x, y]).output(buf);
break;
case PdfImageOrientation.leftBottom:
buf.putNumList(<double>[0, h, -w, 0, w + x, y]);
PdfNumList(<double>[0, h, -w, 0, w + x, y]).output(buf);
break;
}
... ... @@ -200,7 +200,7 @@ class PdfGraphics {
double w,
double h,
) {
buf.putNumList(<double>[x, y, w, h]);
PdfNumList(<double>[x, y, w, h]).output(buf);
buf.putString(' re\n');
}
... ... @@ -247,24 +247,24 @@ class PdfGraphics {
}
buf.putString('BT ');
buf.putNumList(<double>[x, y]);
PdfNumList(<double>[x, y]).output(buf);
buf.putString(' Td ${font.name} ');
buf.putNum(size);
PdfNum(size).output(buf);
buf.putString(' Tf ');
if (charSpace != 0) {
buf.putNum(charSpace);
PdfNum(charSpace).output(buf);
buf.putString(' Tc ');
}
if (wordSpace != 0) {
buf.putNum(wordSpace);
PdfNum(wordSpace).output(buf);
buf.putString(' Tw ');
}
if (scale != 1) {
buf.putNum(scale * 100);
PdfNum(scale * 100).output(buf);
buf.putString(' Tz ');
}
if (rise != 0) {
buf.putNum(rise);
PdfNum(rise).output(buf);
buf.putString(' Ts ');
}
if (mode != PdfTextRenderingMode.fill) {
... ... @@ -288,11 +288,11 @@ class PdfGraphics {
/// @param c Color to use
void setFillColor(PdfColor color) {
if (color is PdfColorCmyk) {
buf.putNumList(
<double>[color.cyan, color.magenta, color.yellow, color.black]);
PdfNumList(<double>[color.cyan, color.magenta, color.yellow, color.black])
.output(buf);
buf.putString(' k\n');
} else {
buf.putNumList(<double>[color.red, color.green, color.blue]);
PdfNumList(<double>[color.red, color.green, color.blue]).output(buf);
buf.putString(' rg\n');
}
}
... ... @@ -302,11 +302,11 @@ class PdfGraphics {
/// @param c Color to use
void setStrokeColor(PdfColor color) {
if (color is PdfColorCmyk) {
buf.putNumList(
<double>[color.cyan, color.magenta, color.yellow, color.black]);
PdfNumList(<double>[color.cyan, color.magenta, color.yellow, color.black])
.output(buf);
buf.putString(' K\n');
} else {
buf.putNumList(<double>[color.red, color.green, color.blue]);
PdfNumList(<double>[color.red, color.green, color.blue]).output(buf);
buf.putString(' RG\n');
}
}
... ... @@ -320,7 +320,7 @@ class PdfGraphics {
/// Set the transformation Matrix
void setTransform(Matrix4 t) {
final Float64List s = t.storage;
buf.putNumList(<double>[s[0], s[1], s[4], s[5], s[12], s[13]]);
PdfNumList(<double>[s[0], s[1], s[4], s[5], s[12], s[13]]).output(buf);
buf.putString(' cm\n');
_context.ctm.multiply(t);
}
... ... @@ -335,7 +335,7 @@ class PdfGraphics {
/// @param x coordinate
/// @param y coordinate
void lineTo(double x, double y) {
buf.putNumList(<double>[x, y]);
PdfNumList(<double>[x, y]).output(buf);
buf.putString(' l\n');
}
... ... @@ -344,7 +344,7 @@ class PdfGraphics {
/// @param x coordinate
/// @param y coordinate
void moveTo(double x, double y) {
buf.putNumList(<double>[x, y]);
PdfNumList(<double>[x, y]).output(buf);
buf.putString(' m\n');
}
... ... @@ -360,7 +360,7 @@ class PdfGraphics {
/// @param y3 end point
void curveTo(
double x1, double y1, double x2, double y2, double x3, double y3) {
buf.putNumList(<double>[x1, y1, x2, y2, x3, y3]);
PdfNumList(<double>[x1, y1, x2, y2, x3, y3]).output(buf);
buf.putString(' c\n');
}
... ... @@ -685,12 +685,12 @@ class PdfGraphics {
}
void setLineWidth(double width) {
buf.putNum(width);
PdfNum(width).output(buf);
buf.putString(' w\n');
}
void setMiterLimit(double limit) {
buf.putNum(limit);
PdfNum(limit).output(buf);
buf.putString(' M\n');
}
}
... ...
... ... @@ -19,8 +19,6 @@
part of pdf;
class PdfStream {
static const int precision = 5;
static const int _grow = 65536;
Uint8List _stream = Uint8List(_grow);
... ... @@ -62,67 +60,14 @@ class PdfStream {
Uint8List output() => _stream.sublist(0, _offset);
void putString(String s) {
for (int codeUnit in s.codeUnits) {
if (codeUnit <= 0x7f) {
putByte(codeUnit);
} else {
putByte(0x20);
}
}
}
void putNum(double d) {
assert(d != double.infinity);
putString(d.toStringAsFixed(precision));
}
void putNumList(List<double> d) {
putString(d.map((double v) {
assert(v != double.infinity);
return v.toStringAsFixed(precision);
}).join(' '));
}
/// Escape special characters
/// \ddd Character code ddd (octal)
void putTextBytes(List<int> s) {
for (int c in s) {
switch (c) {
case 0x0a: // \n Line feed (LF)
putByte(0x5c);
putByte(0x6e);
break;
case 0x0d: // \r Carriage return (CR)
putByte(0x5c);
putByte(0x72);
break;
case 0x09: // \t Horizontal tab (HT)
putByte(0x5c);
putByte(0x74);
break;
case 0x08: // \b Backspace (BS)
putByte(0x5c);
putByte(0x62);
break;
case 0x0c: // \f Form feed (FF)
putByte(0x5c);
putByte(0x66);
break;
case 0x28: // \( Left parenthesis
putByte(0x5c);
putByte(0x28);
break;
case 0x29: // \) Right parenthesis
putByte(0x5c);
putByte(0x29);
break;
case 0x5c: // \\ Backslash
putByte(0x5c);
putByte(0x5c);
break;
default:
putByte(c);
assert(() {
for (final int codeUnit in s.codeUnits) {
if (codeUnit > 0x7f) {
return false;
}
}
return true;
}());
putBytes(s.codeUnits);
}
}
... ...