widget_text_test.dart
5.73 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
/*
* 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.
*/
import 'dart:io';
import 'dart:math' as math;
import 'dart:typed_data';
import 'package:test/test.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
Document pdf;
Font ttf;
Font ttfBold;
void main() {
setUpAll(() {
Document.debug = true;
RichText.debug = true;
final Uint8List fontData = File('open-sans.ttf').readAsBytesSync();
ttf = Font.ttf(fontData.buffer.asByteData());
final Uint8List fontDataBold = File('open-sans-bold.ttf').readAsBytesSync();
ttfBold = Font.ttf(fontDataBold.buffer.asByteData());
pdf = Document();
});
test('Text Widgets Quotes', () {
pdf.addPage(Page(
build: (Context context) => Text('Text containing \' or " works!')));
});
test('Text Widgets Unicode Quotes', () {
pdf.addPage(Page(
build: (Context context) => Text('Text containing ’ and ” works!',
style: TextStyle(font: ttf))));
});
test('Text Widgets softWrap', () {
pdf.addPage(MultiPage(
build: (Context context) => <Widget>[
Text(
'Text with\nsoft wrap\nenabled',
softWrap: true,
),
Text(
'Text with\nsoft wrap\ndisabled',
softWrap: false,
),
]));
});
test('Text Widgets Alignement', () {
final String para = LoremText().paragraph(40);
final List<Widget> widgets = <Widget>[];
for (TextAlign align in TextAlign.values) {
widgets.add(
Text(
'$align:\n' + para,
textAlign: align,
softWrap: true,
),
);
}
pdf.addPage(MultiPage(build: (Context context) => widgets));
});
test('Text Widgets lineSpacing', () {
final String para = LoremText().paragraph(40);
final List<Widget> widgets = <Widget>[];
for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
widgets.add(
Text(para, style: TextStyle(font: ttf, lineSpacing: spacing)),
);
widgets.add(
SizedBox(height: 30),
);
}
pdf.addPage(MultiPage(build: (Context context) => widgets));
});
test('Text Widgets wordSpacing', () {
final String para = LoremText().paragraph(40);
final List<Widget> widgets = <Widget>[];
for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
widgets.add(
Text(para, style: TextStyle(font: ttf, wordSpacing: spacing)),
);
widgets.add(
SizedBox(height: 30),
);
}
pdf.addPage(MultiPage(build: (Context context) => widgets));
});
test('Text Widgets letterSpacing', () {
final String para = LoremText().paragraph(40);
final List<Widget> widgets = <Widget>[];
for (double spacing = 0.0; spacing < 10.0; spacing += 2.0) {
widgets.add(
Text(para, style: TextStyle(font: ttf, letterSpacing: spacing)),
);
widgets.add(
SizedBox(height: 30),
);
}
pdf.addPage(MultiPage(build: (Context context) => widgets));
});
test('Text Widgets background', () {
final String para = LoremText().paragraph(40);
pdf.addPage(MultiPage(
build: (Context context) => <Widget>[
Text(
para,
style: TextStyle(
font: ttf,
background: PdfColors.purple50,
),
),
]));
});
test('Text Widgets RichText', () {
final math.Random rnd = math.Random(42);
final String para = LoremText(random: rnd).paragraph(40);
final List<TextSpan> spans = <TextSpan>[];
for (String word in para.split(' ')) {
spans.add(
TextSpan(
text: '$word',
style: TextStyle(
font: ttf,
fontSize: rnd.nextDouble() * 20 + 20,
color:
PdfColors.primaries[rnd.nextInt(PdfColors.primaries.length)]),
),
);
spans.add(const TextSpan(text: ' '));
}
pdf.addPage(MultiPage(
build: (Context context) => <Widget>[
RichText(
text: TextSpan(
text: 'Hello ',
style: TextStyle(
font: ttf,
fontSize: 20,
),
children: <InlineSpan>[
TextSpan(
text: 'bold',
style: TextStyle(
font: ttfBold, fontSize: 40, color: PdfColors.blue),
children: <InlineSpan>[
const TextSpan(text: '*', baseline: 20),
WidgetSpan(child: PdfLogo(), baseline: -10),
]),
TextSpan(
text: ' world!\n',
children: spans,
),
WidgetSpan(
child: PdfLogo(),
annotation: AnnotationUrl(
'https://github.com/DavBfr/dart_pdf',
)),
],
),
),
]));
});
tearDownAll(() {
final File file = File('widgets-text.pdf');
file.writeAsBytesSync(pdf.save());
});
}