David PHAM-VAN

Fix Text decoration placements

... ... @@ -10,6 +10,7 @@
- Add PdfColor.shade
- Uniformize examples
- Fix context painting empty Table
- Fix Text decoration placements
## 1.5.0
... ...
... ... @@ -52,7 +52,9 @@ abstract class _Span {
}
class _TextDecoration {
_TextDecoration(this.style, this.annotation) : assert(style != null);
_TextDecoration(this.style, this.annotation, this.startSpan, this.endSpan)
: assert(startSpan <= endSpan),
assert(style != null);
static const double _space = -0.15;
... ... @@ -60,13 +62,44 @@ class _TextDecoration {
final AnnotationBuilder annotation;
PdfRect box = PdfRect.zero;
final int startSpan;
final int endSpan;
PdfRect _box;
PdfRect _getBox(List<_Span> spans) {
if (_box != null) {
return _box;
}
final double x1 = spans[startSpan].offset.x + spans[startSpan].left;
final double x2 =
spans[endSpan].offset.x + spans[endSpan].left + spans[endSpan].width;
double y1 = spans[startSpan].offset.y + spans[startSpan].top;
double y2 = y1 + spans[startSpan].height;
for (int n = startSpan + 1; n <= endSpan; n++) {
final double ny1 = spans[n].offset.y + spans[n].top;
final double ny2 = ny1 + spans[n].height;
y1 = math.min(y1, ny1);
y2 = math.max(y2, ny2);
}
_box = PdfRect.fromLTRB(x1, y1, x2, y2);
return _box;
}
_TextDecoration copyWith({int endSpan}) =>
_TextDecoration(style, annotation, startSpan, endSpan ?? this.endSpan);
void backgroundPaint(
Context context,
double textScaleFactor,
PdfRect globalBox,
List<_Span> spans,
) {
final PdfRect box = _getBox(spans);
if (annotation != null) {
final PdfRect spanBox = PdfRect(
globalBox.x + box.left,
... ... @@ -93,78 +126,80 @@ class _TextDecoration {
Context context,
double textScaleFactor,
PdfRect globalBox,
List<_Span> spans,
) {
if (style.decoration != null) {
final PdfFont font = style.font.getFont(context);
final double space =
_space * style.fontSize * textScaleFactor * style.decorationThickness;
context.canvas
..setStrokeColor(style.decorationColor ?? style.color)
..setLineWidth(style.decorationThickness *
style.fontSize *
textScaleFactor *
0.05);
if (style.decoration.contains(TextDecoration.underline)) {
final double base =
-font.descent * style.fontSize * textScaleFactor / 2;
if (style.decoration == null) {
return;
}
final PdfRect box = _getBox(spans);
final PdfFont font = style.font.getFont(context);
final double space =
_space * style.fontSize * textScaleFactor * style.decorationThickness;
context.canvas
..setStrokeColor(style.decorationColor ?? style.color)
..setLineWidth(
style.decorationThickness * style.fontSize * textScaleFactor * 0.05);
if (style.decoration.contains(TextDecoration.underline)) {
final double base = -font.descent * style.fontSize * textScaleFactor / 2;
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base + space,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base + space,
);
}
context.canvas.strokePath();
}
context.canvas.strokePath();
}
if (style.decoration.contains(TextDecoration.overline)) {
final double base = style.fontSize * textScaleFactor;
if (style.decoration.contains(TextDecoration.overline)) {
final double base = style.fontSize * textScaleFactor;
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base - space,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base - space,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base - space,
globalBox.x + box.right,
globalBox.top + box.bottom + base - space,
);
}
context.canvas.strokePath();
}
context.canvas.strokePath();
}
if (style.decoration.contains(TextDecoration.lineThrough)) {
final double base =
(1 - font.descent) * style.fontSize * textScaleFactor / 2;
if (style.decoration.contains(TextDecoration.lineThrough)) {
final double base =
(1 - font.descent) * style.fontSize * textScaleFactor / 2;
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base,
globalBox.top + box.bottom + base + space,
);
if (style.decorationStyle == TextDecorationStyle.double) {
context.canvas.drawLine(
globalBox.x + box.left,
globalBox.top + box.bottom + base + space,
globalBox.x + box.right,
globalBox.top + box.bottom + base + space,
);
}
context.canvas.strokePath();
}
context.canvas.strokePath();
}
}
... ... @@ -172,7 +207,10 @@ class _TextDecoration {
Context context,
double textScaleFactor,
PdfRect globalBox,
List<_Span> spans,
) {
final PdfRect box = _getBox(spans);
context.canvas
..setLineWidth(.5)
..drawRect(
... ... @@ -197,13 +235,13 @@ class _Word extends _Span {
double get left => metrics.left;
@override
double get top => metrics.top;
double get top => metrics.descent;
@override
double get width => metrics.width;
@override
double get height => metrics.height;
double get height => metrics.maxHeight;
@override
String toString() {
... ... @@ -464,14 +502,20 @@ class RichText extends Widget {
span.offset = span.offset.translate(delta, -baseline);
}
for (_TextDecoration decoration in decorations) {
decoration.box = PdfRect.fromPoints(
decoration.box.offset.translate(delta, -baseline),
decoration.box.size,
);
return totalWidth;
}
void _appendDecoration(bool append, _TextDecoration td) {
if (append && _decorations.isNotEmpty) {
final _TextDecoration last = _decorations.last;
if (last.style == td.style && last.annotation == td.annotation) {
_decorations[_decorations.length - 1] =
last.copyWith(endSpan: td.endSpan);
return;
}
}
return totalWidth;
_decorations.add(td);
}
@override
... ... @@ -568,16 +612,15 @@ class RichText extends Widget {
_spans.add(wd);
spanCount++;
final _TextDecoration td = _TextDecoration(
style,
span.annotation,
_appendDecoration(
spanCount > 1,
_TextDecoration(
style,
span.annotation,
_spans.length - 1,
_spans.length - 1,
),
);
td.box = PdfRect(
offsetX,
-offsetY + metrics.descent + baseline,
metrics.maxWidth + space.advanceWidth * style.wordSpacing,
metrics.maxHeight);
_decorations.add(td);
offsetX +=
metrics.advanceWidth + space.advanceWidth * style.wordSpacing;
... ... @@ -598,15 +641,6 @@ class RichText extends Widget {
spanStart += spanCount;
decorationStart = _decorations.length;
if (_decorations.isNotEmpty) {
// remove the last space
_decorations.last.box = PdfRect.fromPoints(
_decorations.last.box.offset,
_decorations.last.box.size
.translate(-space.advanceWidth * style.wordSpacing, 0),
);
}
lines++;
if (maxLines != null && lines > maxLines) {
break;
... ... @@ -683,12 +717,15 @@ class RichText extends Widget {
_spans.add(ws);
spanCount++;
final _TextDecoration td = _TextDecoration(
style,
span.annotation,
_appendDecoration(
spanCount > 1,
_TextDecoration(
style,
span.annotation,
_spans.length - 1,
_spans.length - 1,
),
);
td.box = PdfRect(offsetX, -offsetY + baseline, ws.width, ws.height);
_decorations.add(td);
offsetX += ws.left + ws.width;
}
... ... @@ -732,7 +769,7 @@ class RichText extends Widget {
for (_TextDecoration decoration in _decorations) {
assert(() {
if (Document.debug && RichText.debug) {
decoration.debugPaint(context, textScaleFactor, box);
decoration.debugPaint(context, textScaleFactor, box, _spans);
}
return true;
}());
... ... @@ -741,6 +778,7 @@ class RichText extends Widget {
context,
textScaleFactor,
box,
_spans,
);
}
... ... @@ -773,6 +811,7 @@ class RichText extends Widget {
context,
textScaleFactor,
box,
_spans,
);
}
}
... ...
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type