David PHAM-VAN

Add more font drawing options

... ... @@ -8,6 +8,7 @@
- Fix Bullet widget styling
- Fix HSV and HSL Color constructors
- Add PageTheme.copyWith
- Add more font drawing options
## 1.4.1
... ...
... ... @@ -20,6 +20,32 @@ part of pdf;
enum PdfLineCap { joinMiter, joinRound, joinBevel }
enum PdfTextRenderingMode {
/// Fill text
fill,
/// Stroke text
stroke,
/// Fill, then stroke text
fillAndStroke,
/// Neither fill nor stroke text (invisible)
invisible,
/// Fill text and add to path for clipping
fillAndClip,
/// Stroke text and add to path for clipping
strokeAndClip,
/// Fill, then stroke text and add to path for clipping
fillStrokeAndClip,
/// Add text to path for clipping
clip
}
@immutable
class _PdfGraphicsContext {
const _PdfGraphicsContext({@required this.ctm}) : assert(ctm != null);
... ... @@ -213,7 +239,18 @@ class PdfGraphics {
/// @param x coordinate
/// @param y coordinate
/// @param s String to draw
void drawString(PdfFont font, double size, String s, double x, double y) {
void drawString(
PdfFont font,
double size,
String s,
double x,
double y, {
double charSpace = 0,
double wordSpace = 0,
double scale = 1,
PdfTextRenderingMode mode = PdfTextRenderingMode.fill,
double rise = 0,
}) {
if (!page.fonts.containsKey(font.name)) {
page.fonts[font.name] = font;
}
... ... @@ -223,8 +260,28 @@ class PdfGraphics {
buf.putString(' Td ${font.name} ');
buf.putNum(size);
buf.putString(' Tf ');
if (charSpace != 0) {
buf.putNum(charSpace);
buf.putString(' Tc ');
}
if (wordSpace != 0) {
buf.putNum(wordSpace);
buf.putString(' Tw ');
}
if (scale != 1) {
buf.putNum(scale * 100);
buf.putString(' Tz ');
}
if (rise != 0) {
buf.putNum(rise);
buf.putString(' Ts ');
}
if (mode != PdfTextRenderingMode.fill) {
buf.putString('${mode.index} Tr ');
}
buf.putString('[');
buf.putStream(font.putText(s));
buf.putString(' Tj ET\n');
buf.putString(']TJ ET\n');
}
/// Sets the color for drawing
... ...
... ... @@ -20,11 +20,11 @@ part of pdf;
class PdfTtfFont extends PdfFont {
/// Constructs a [PdfTtfFont]
PdfTtfFont(PdfDocument pdfDocument, ByteData bytes)
PdfTtfFont(PdfDocument pdfDocument, ByteData bytes, {bool protect = false})
: font = TtfParser(bytes),
super._create(pdfDocument, subtype: '/TrueType') {
file = PdfObjectStream(pdfDocument, isBinary: true);
unicodeCMap = PdfUnicodeCmap(pdfDocument);
unicodeCMap = PdfUnicodeCmap(pdfDocument, protect);
descriptor = PdfFontDescriptor(this, file);
widthsObject = PdfArrayObject(pdfDocument, <String>[]);
}
... ...
... ... @@ -19,12 +19,18 @@
part of pdf;
class PdfUnicodeCmap extends PdfObjectStream {
PdfUnicodeCmap(PdfDocument pdfDocument) : super(pdfDocument);
PdfUnicodeCmap(PdfDocument pdfDocument, this.protect) : super(pdfDocument);
final List<int> cmap = <int>[0];
final bool protect;
@override
void _prepare() {
if (protect) {
cmap.fillRange(1, cmap.length, 0x20);
}
buf.putString('/CIDInit/ProcSet findresource begin\n'
'12 dict begin\n'
'begincmap\n'
... ...
... ... @@ -144,13 +144,15 @@ class Font {
}
class TtfFont extends Font {
TtfFont(this.data);
TtfFont(this.data, {this.protect = false});
final ByteData data;
final bool protect;
@override
PdfFont buildFont(PdfDocument pdfDocument) {
return PdfTtfFont(pdfDocument, data);
return PdfTtfFont(pdfDocument, data, protect: protect);
}
@override
... ...
... ... @@ -222,7 +222,9 @@ class _Word extends _Span {
style.fontSize * textScaleFactor,
text,
point.x + offset.x,
point.y + offset.y);
point.y + offset.y,
mode: style.renderingMode,
);
}
@override
... ...
... ... @@ -112,6 +112,7 @@ class TextStyle {
this.decorationColor,
this.decorationStyle,
this.decorationThickness,
this.renderingMode,
}) : assert(inherit || color != null),
assert(inherit || fontNormal != null),
assert(inherit || fontBold != null),
... ... @@ -127,6 +128,7 @@ class TextStyle {
assert(inherit || decoration != null),
assert(inherit || decorationStyle != null),
assert(inherit || decorationThickness != null),
assert(inherit || renderingMode != null),
fontNormal = fontNormal ??
(fontStyle != FontStyle.italic && fontWeight != FontWeight.bold
? font
... ... @@ -163,6 +165,7 @@ class TextStyle {
decorationColor: null,
decorationStyle: TextDecorationStyle.solid,
decorationThickness: 1,
renderingMode: PdfTextRenderingMode.fill,
);
}
... ... @@ -210,6 +213,8 @@ class TextStyle {
final double decorationThickness;
final PdfTextRenderingMode renderingMode;
TextStyle copyWith({
PdfColor color,
Font font,
... ... @@ -229,6 +234,7 @@ class TextStyle {
PdfColor decorationColor,
TextDecorationStyle decorationStyle,
double decorationThickness,
PdfTextRenderingMode renderingMode,
}) {
return TextStyle(
inherit: inherit,
... ... @@ -250,6 +256,7 @@ class TextStyle {
decorationColor: decorationColor ?? this.decorationColor,
decorationStyle: decorationStyle ?? this.decorationStyle,
decorationThickness: decorationThickness ?? this.decorationThickness,
renderingMode: renderingMode ?? this.renderingMode,
);
}
... ... @@ -342,6 +349,7 @@ class TextStyle {
decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle,
decorationThickness: other.decorationThickness,
renderingMode: other.renderingMode,
);
}
... ... @@ -370,5 +378,5 @@ class TextStyle {
@override
String toString() =>
'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background decoration:$decoration decorationColor:$decorationColor decorationStyle:$decorationStyle decorationThickness:$decorationThickness)';
'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background decoration:$decoration decorationColor:$decorationColor decorationStyle:$decorationStyle decorationThickness:$decorationThickness, renderingMode:$renderingMode)';
}
... ...