text.dart
8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of widget;
@immutable
class TextStyle {
const TextStyle({
this.color = PdfColor.black,
@required this.font,
this.fontSize = _defaultFontSize,
this.letterSpacing = 1.0,
this.wordSpacing = 1.0,
this.lineSpacing = 0.0,
this.height = 1.0,
this.background,
}) : assert(font != null),
assert(color != null);
final PdfColor color;
final PdfFont font;
// font height, in pdf unit
final double fontSize;
static const double _defaultFontSize = 12.0 * PdfPageFormat.point;
// spacing between letters, 1.0 being natural spacing
final double letterSpacing;
// spacing between lines, in pdf unit
final double lineSpacing;
// spacing between words, 1.0 being natural spacing
final double wordSpacing;
final double height;
final PdfColor background;
TextStyle copyWith({
PdfColor color,
PdfFont font,
double fontSize,
double letterSpacing,
double wordSpacing,
double lineSpacing,
double height,
PdfColor background,
}) {
return TextStyle(
color: color ?? this.color,
font: font ?? this.font,
fontSize: fontSize ?? this.fontSize,
letterSpacing: letterSpacing ?? this.letterSpacing,
wordSpacing: wordSpacing ?? this.wordSpacing,
lineSpacing: lineSpacing ?? this.lineSpacing,
height: height ?? this.height,
background: background ?? this.background,
);
}
@override
String toString() =>
'TextStyle(color:$color font:$font letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)';
}
enum TextAlign { left, right, center, justify }
class _Word {
_Word(this.text, this.style, this.metrics);
final String text;
final TextStyle style;
final PdfFontMetrics metrics;
PdfPoint offset = PdfPoint.zero;
@override
String toString() {
return 'Word "$text" offset:$offset metrics:$metrics style:$style';
}
void debugPaint(Context context, double textScaleFactor, PdfRect globalBox) {
const double deb = 5.0;
context.canvas
..drawRect(globalBox.x + offset.x + metrics.left,
globalBox.top + offset.y + metrics.top, metrics.width, metrics.height)
..setStrokeColor(PdfColor.orange)
..strokePath()
..drawLine(
globalBox.x + offset.x - deb,
globalBox.top + offset.y,
globalBox.x + offset.x + metrics.right + deb,
globalBox.top + offset.y)
..setStrokeColor(PdfColor.deepPurple)
..strokePath();
}
}
class TextSpan {
const TextSpan({this.style, this.text, this.children});
final TextStyle style;
final String text;
final List<TextSpan> children;
String toPlainText() {
final StringBuffer buffer = StringBuffer();
visitTextSpan((TextSpan span) {
buffer.write(span.text);
return true;
});
return buffer.toString();
}
bool visitTextSpan(bool visitor(TextSpan span)) {
if (text != null) {
if (!visitor(this)) {
return false;
}
}
if (children != null) {
for (TextSpan child in children) {
if (!child.visitTextSpan(visitor)) {
return false;
}
}
}
return true;
}
}
class RichText extends Widget {
RichText(
{@required this.text,
this.textAlign = TextAlign.left,
bool softWrap = true,
this.textScaleFactor = 1.0,
int maxLines})
: maxLines = !softWrap ? 1 : maxLines,
assert(text != null);
static const bool debug = false;
final TextSpan text;
final TextAlign textAlign;
final double textScaleFactor;
final int maxLines;
final List<_Word> _words = <_Word>[];
double _realignLine(List<_Word> words, double totalWidth, double wordsWidth,
bool last, double baseline) {
double delta = 0.0;
switch (textAlign) {
case TextAlign.left:
totalWidth = wordsWidth;
break;
case TextAlign.right:
delta = totalWidth - wordsWidth;
break;
case TextAlign.center:
delta = (totalWidth - wordsWidth) / 2.0;
break;
case TextAlign.justify:
if (last) {
totalWidth = wordsWidth;
break;
}
delta = (totalWidth - wordsWidth) / (words.length - 1);
double x = 0.0;
for (_Word word in words) {
word.offset = word.offset.translate(x, -baseline);
x += delta;
}
return totalWidth;
}
for (_Word word in words) {
word.offset = word.offset.translate(delta, -baseline);
}
return totalWidth;
}
@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
_words.clear();
final TextStyle defaultstyle = Theme.of(context).defaultTextStyle;
final double constraintWidth = constraints.hasBoundedWidth
? constraints.maxWidth
: constraints.constrainWidth();
final double constraintHeight = constraints.hasBoundedHeight
? constraints.maxHeight
: constraints.constrainHeight();
double offsetX = 0.0;
double offsetY = 0.0;
double width = 0.0;
double top;
double bottom;
int lines = 1;
int wCount = 0;
int lineStart = 0;
text.visitTextSpan((TextSpan span) {
if (span.text == null) {
return true;
}
final TextStyle style = span.style ?? defaultstyle;
final PdfFontMetrics space =
style.font.stringMetrics(' ') * (style.fontSize * textScaleFactor);
for (String word in span.text.split(' ')) {
if (word.isEmpty) {
offsetX += space.width;
continue;
}
final PdfFontMetrics metrics =
style.font.stringMetrics(word) * (style.fontSize * textScaleFactor);
if (offsetX + metrics.width > constraintWidth) {
if (wCount == 0) {
break;
}
width = math.max(
width,
_realignLine(_words.sublist(lineStart), constraintWidth,
offsetX - space.width, false, bottom));
lineStart += wCount;
if (maxLines != null && ++lines > maxLines) {
break;
}
offsetX = 0.0;
offsetY += bottom - top + style.lineSpacing;
top = null;
bottom = null;
if (offsetY > constraintHeight) {
return false;
}
wCount = 0;
}
top = math.min(top ?? metrics.top, metrics.top);
bottom = math.max(bottom ?? metrics.bottom, metrics.bottom);
final _Word wd = _Word(word, style, metrics);
wd.offset = PdfPoint(offsetX, -offsetY);
_words.add(wd);
wCount++;
offsetX += metrics.width + space.advanceWidth;
}
offsetX -= space.width;
return true;
});
width = math.max(
width,
_realignLine(
_words.sublist(lineStart), constraintWidth, offsetX, true, bottom));
box = PdfRect(0.0, 0.0, constraints.constrainWidth(width),
constraints.constrainHeight(offsetY + bottom - top));
}
@override
void debugPaint(Context context) {
context.canvas
..setStrokeColor(PdfColor.blue)
..drawRect(box.x, box.y, box.width, box.height)
..strokePath();
}
@override
void paint(Context context) {
super.paint(context);
TextStyle currentStyle;
PdfColor currentColor;
for (_Word word in _words) {
assert(() {
if (Document.debug && RichText.debug) {
word.debugPaint(context, textScaleFactor, box);
}
return true;
}());
if (word.style != currentStyle) {
currentStyle = word.style;
if (currentStyle.color != currentColor) {
currentColor = currentStyle.color;
context.canvas.setFillColor(currentColor);
}
}
context.canvas.drawString(
currentStyle.font,
currentStyle.fontSize * textScaleFactor,
word.text,
box.x + word.offset.x,
box.top + word.offset.y);
}
}
}
class Text extends RichText {
Text(
String text, {
TextStyle style,
TextAlign textAlign = TextAlign.left,
bool softWrap = true,
double textScaleFactor = 1.0,
int maxLines,
}) : assert(text != null),
super(
text: TextSpan(text: text, style: style),
textAlign: textAlign,
softWrap: softWrap,
textScaleFactor: textScaleFactor,
maxLines: maxLines);
}