David PHAM-VAN

Allow Annotations in TextSpan

# 1.3.8
* Add jpeg image loading function
* Add Theme::copyFrom() method
* Allow Annotations in TextSpan
# 1.3.7
* Add Pdf Creation date
... ...
... ... @@ -42,69 +42,76 @@ class Anchor extends SingleChildWidget {
}
}
class _Annotation extends SingleChildWidget {
_Annotation({Widget child}) : super(child: child);
@override
void debugPaint(Context context) {
context.canvas
..setFillColor(PdfColors.pink)
..drawRect(box.x, box.y, box.width, box.height)
..fillPath();
abstract class AnnotationBuilder {
PdfRect localToGlobal(Context context, PdfRect box) {
final Matrix4 mat = context.canvas.getTransform();
final Vector3 lt = mat.transform3(Vector3(box.left, box.bottom, 0));
final Vector3 rb = mat.transform3(Vector3(box.right, box.top, 0));
return PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
}
@override
void paint(Context context) {
super.paint(context);
paintChild(context);
}
void build(Context context, PdfRect box);
}
class Link extends _Annotation {
Link({@required Widget child, this.destination})
: assert(child != null),
super(child: child);
class AnnotationLink extends AnnotationBuilder {
AnnotationLink(this.destination) : assert(destination != null);
final String destination;
@override
void paint(Context context) {
super.paint(context);
if (destination != null) {
final Matrix4 mat = context.canvas.getTransform();
final Vector3 lt = mat.transform3(Vector3(box.left, box.bottom, 0));
final Vector3 rb = mat.transform3(Vector3(box.right, box.top, 0));
final PdfRect ibox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
void build(Context context, PdfRect box) {
PdfAnnot.namedLink(
context.page,
rect: ibox,
rect: localToGlobal(context, box),
dest: destination,
);
}
}
}
class UrlLink extends _Annotation {
UrlLink({@required Widget child, @required this.destination})
: assert(child != null),
assert(destination != null),
super(child: child);
class AnnotationUrl extends AnnotationBuilder {
AnnotationUrl(this.destination) : assert(destination != null);
final String destination;
@override
void paint(Context context) {
super.paint(context);
final Matrix4 mat = context.canvas.getTransform();
final Vector3 lt = mat.transform3(Vector3(box.left, box.bottom, 0));
final Vector3 rb = mat.transform3(Vector3(box.right, box.top, 0));
final PdfRect ibox = PdfRect.fromLTRB(lt.x, lt.y, rb.x, rb.y);
void build(Context context, PdfRect box) {
PdfAnnot.urlLink(
context.page,
rect: ibox,
rect: localToGlobal(context, box),
dest: destination,
);
}
}
class Annotation extends SingleChildWidget {
Annotation({Widget child, this.builder}) : super(child: child);
final AnnotationBuilder builder;
@override
void debugPaint(Context context) {
context.canvas
..setFillColor(PdfColors.pink)
..drawRect(box.x, box.y, box.width, box.height)
..fillPath();
}
@override
void paint(Context context) {
super.paint(context);
paintChild(context);
builder?.build(context, box);
}
}
class Link extends Annotation {
Link({@required Widget child, String destination})
: assert(child != null),
super(child: child, builder: AnnotationLink(destination));
}
class UrlLink extends Annotation {
UrlLink({@required Widget child, String destination})
: assert(child != null),
super(child: child, builder: AnnotationUrl(destination));
}
... ...
... ... @@ -19,7 +19,7 @@ part of widget;
enum TextAlign { left, right, center, justify }
class _Word {
_Word(this.text, this.style, this.metrics);
_Word(this.text, this.style, this.metrics, this.annotation);
final String text;
... ... @@ -29,6 +29,8 @@ class _Word {
PdfPoint offset = PdfPoint.zero;
final AnnotationBuilder annotation;
@override
String toString() {
return 'Word "$text" offset:$offset metrics:$metrics style:$style';
... ... @@ -53,7 +55,7 @@ class _Word {
}
class TextSpan {
const TextSpan({this.style, this.text, this.children});
const TextSpan({this.style, this.text, this.children, this.annotation});
final TextStyle style;
... ... @@ -61,6 +63,8 @@ class TextSpan {
final List<TextSpan> children;
final AnnotationBuilder annotation;
String toPlainText() {
final StringBuffer buffer = StringBuffer();
visitTextSpan((TextSpan span) {
... ... @@ -210,7 +214,7 @@ class RichText extends Widget {
top = math.min(top ?? metrics.top, metrics.top);
bottom = math.max(bottom ?? metrics.bottom, metrics.bottom);
final _Word wd = _Word(word, style, metrics);
final _Word wd = _Word(word, style, metrics, span.annotation);
wd.offset = PdfPoint(offsetX, -offsetY);
_words.add(wd);
... ... @@ -264,6 +268,15 @@ class RichText extends Widget {
}
}
if (word.annotation != null) {
final PdfRect wordBox = PdfRect(
box.x + word.offset.x + word.metrics.left,
box.top + word.offset.y + word.metrics.top,
word.metrics.width,
word.metrics.height);
word.annotation.build(context, wordBox);
}
context.canvas.drawString(
currentStyle.font.getFont(context),
currentStyle.fontSize * textScaleFactor,
... ...