Showing
7 changed files
with
133 additions
and
21 deletions
@@ -179,7 +179,7 @@ class RichText extends Widget { | @@ -179,7 +179,7 @@ class RichText extends Widget { | ||
179 | return true; | 179 | return true; |
180 | } | 180 | } |
181 | 181 | ||
182 | - final PdfFont font = style.paintFont.getFont(context); | 182 | + final PdfFont font = style.font.getFont(context); |
183 | 183 | ||
184 | final PdfFontMetrics space = | 184 | final PdfFontMetrics space = |
185 | font.stringMetrics(' ') * (style.fontSize * textScaleFactor); | 185 | font.stringMetrics(' ') * (style.fontSize * textScaleFactor); |
@@ -322,7 +322,7 @@ class RichText extends Widget { | @@ -322,7 +322,7 @@ class RichText extends Widget { | ||
322 | } | 322 | } |
323 | 323 | ||
324 | context.canvas.drawString( | 324 | context.canvas.drawString( |
325 | - currentStyle.paintFont.getFont(context), | 325 | + currentStyle.font.getFont(context), |
326 | currentStyle.fontSize * textScaleFactor, | 326 | currentStyle.fontSize * textScaleFactor, |
327 | word.text, | 327 | word.text, |
328 | box.x + word.offset.x, | 328 | box.x + word.offset.x, |
@@ -25,10 +25,11 @@ class TextStyle { | @@ -25,10 +25,11 @@ class TextStyle { | ||
25 | const TextStyle({ | 25 | const TextStyle({ |
26 | this.inherit = true, | 26 | this.inherit = true, |
27 | this.color, | 27 | this.color, |
28 | - this.font, | ||
29 | - this.fontBold, | ||
30 | - this.fontItalic, | ||
31 | - this.fontBoldItalic, | 28 | + Font font, |
29 | + Font fontNormal, | ||
30 | + Font fontBold, | ||
31 | + Font fontItalic, | ||
32 | + Font fontBoldItalic, | ||
32 | this.fontSize, | 33 | this.fontSize, |
33 | this.fontWeight, | 34 | this.fontWeight, |
34 | this.fontStyle, | 35 | this.fontStyle, |
@@ -45,12 +46,28 @@ class TextStyle { | @@ -45,12 +46,28 @@ class TextStyle { | ||
45 | assert(inherit || letterSpacing != null), | 46 | assert(inherit || letterSpacing != null), |
46 | assert(inherit || wordSpacing != null), | 47 | assert(inherit || wordSpacing != null), |
47 | assert(inherit || lineSpacing != null), | 48 | assert(inherit || lineSpacing != null), |
48 | - assert(inherit || height != null); | 49 | + assert(inherit || height != null), |
50 | + fontNormal = fontNormal ?? | ||
51 | + (fontStyle != FontStyle.italic && fontWeight != FontWeight.bold | ||
52 | + ? font | ||
53 | + : null), | ||
54 | + fontBold = fontBold ?? | ||
55 | + (fontStyle != FontStyle.italic && fontWeight == FontWeight.bold | ||
56 | + ? font | ||
57 | + : null), | ||
58 | + fontItalic = fontItalic ?? | ||
59 | + (fontStyle == FontStyle.italic && fontWeight != FontWeight.bold | ||
60 | + ? font | ||
61 | + : null), | ||
62 | + fontBoldItalic = fontBoldItalic ?? | ||
63 | + (fontStyle == FontStyle.italic && fontWeight == FontWeight.bold | ||
64 | + ? font | ||
65 | + : null); | ||
49 | 66 | ||
50 | factory TextStyle.defaultStyle() { | 67 | factory TextStyle.defaultStyle() { |
51 | return TextStyle( | 68 | return TextStyle( |
52 | color: PdfColors.black, | 69 | color: PdfColors.black, |
53 | - font: Font.helvetica(), | 70 | + fontNormal: Font.helvetica(), |
54 | fontBold: Font.helveticaBold(), | 71 | fontBold: Font.helveticaBold(), |
55 | fontItalic: Font.helveticaOblique(), | 72 | fontItalic: Font.helveticaOblique(), |
56 | fontBoldItalic: Font.helveticaBoldOblique(), | 73 | fontBoldItalic: Font.helveticaBoldOblique(), |
@@ -68,7 +85,7 @@ class TextStyle { | @@ -68,7 +85,7 @@ class TextStyle { | ||
68 | 85 | ||
69 | final PdfColor color; | 86 | final PdfColor color; |
70 | 87 | ||
71 | - final Font font; | 88 | + final Font fontNormal; |
72 | 89 | ||
73 | final Font fontBold; | 90 | final Font fontBold; |
74 | 91 | ||
@@ -103,6 +120,7 @@ class TextStyle { | @@ -103,6 +120,7 @@ class TextStyle { | ||
103 | TextStyle copyWith({ | 120 | TextStyle copyWith({ |
104 | PdfColor color, | 121 | PdfColor color, |
105 | Font font, | 122 | Font font, |
123 | + Font fontNormal, | ||
106 | Font fontBold, | 124 | Font fontBold, |
107 | Font fontItalic, | 125 | Font fontItalic, |
108 | Font fontBoldItalic, | 126 | Font fontBoldItalic, |
@@ -119,6 +137,7 @@ class TextStyle { | @@ -119,6 +137,7 @@ class TextStyle { | ||
119 | inherit: inherit, | 137 | inherit: inherit, |
120 | color: color ?? this.color, | 138 | color: color ?? this.color, |
121 | font: font ?? this.font, | 139 | font: font ?? this.font, |
140 | + fontNormal: fontNormal ?? this.fontNormal, | ||
122 | fontBold: fontBold ?? this.fontBold, | 141 | fontBold: fontBold ?? this.fontBold, |
123 | fontItalic: fontItalic ?? this.fontItalic, | 142 | fontItalic: fontItalic ?? this.fontItalic, |
124 | fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic, | 143 | fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic, |
@@ -138,6 +157,7 @@ class TextStyle { | @@ -138,6 +157,7 @@ class TextStyle { | ||
138 | TextStyle apply({ | 157 | TextStyle apply({ |
139 | PdfColor color, | 158 | PdfColor color, |
140 | Font font, | 159 | Font font, |
160 | + Font fontNormal, | ||
141 | Font fontBold, | 161 | Font fontBold, |
142 | Font fontItalic, | 162 | Font fontItalic, |
143 | Font fontBoldItalic, | 163 | Font fontBoldItalic, |
@@ -169,6 +189,7 @@ class TextStyle { | @@ -169,6 +189,7 @@ class TextStyle { | ||
169 | inherit: inherit, | 189 | inherit: inherit, |
170 | color: color ?? this.color, | 190 | color: color ?? this.color, |
171 | font: font ?? this.font, | 191 | font: font ?? this.font, |
192 | + fontNormal: fontNormal ?? this.fontNormal, | ||
172 | fontBold: fontBold ?? this.fontBold, | 193 | fontBold: fontBold ?? this.fontBold, |
173 | fontItalic: fontItalic ?? this.fontItalic, | 194 | fontItalic: fontItalic ?? this.fontItalic, |
174 | fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic, | 195 | fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic, |
@@ -201,6 +222,8 @@ class TextStyle { | @@ -201,6 +222,8 @@ class TextStyle { | ||
201 | return copyWith( | 222 | return copyWith( |
202 | color: other.color, | 223 | color: other.color, |
203 | font: other.font, | 224 | font: other.font, |
225 | + fontNormal: other.fontNormal, | ||
226 | + fontBold: other.fontBold, | ||
204 | fontItalic: other.fontItalic, | 227 | fontItalic: other.fontItalic, |
205 | fontBoldItalic: other.fontBoldItalic, | 228 | fontBoldItalic: other.fontBoldItalic, |
206 | fontSize: other.fontSize, | 229 | fontSize: other.fontSize, |
@@ -214,25 +237,32 @@ class TextStyle { | @@ -214,25 +237,32 @@ class TextStyle { | ||
214 | ); | 237 | ); |
215 | } | 238 | } |
216 | 239 | ||
217 | - Font get paintFont { | ||
218 | - if (fontWeight == FontWeight.normal) { | ||
219 | - if (fontStyle == FontStyle.normal) { | ||
220 | - return font; | 240 | + @Deprecated('use font instead') |
241 | + Font get paintFont => font; | ||
242 | + | ||
243 | + Font get font { | ||
244 | + if (fontWeight != FontWeight.bold) { | ||
245 | + if (fontStyle != FontStyle.italic) { | ||
246 | + // normal | ||
247 | + return fontNormal ?? fontBold ?? fontItalic ?? fontBoldItalic; | ||
221 | } else { | 248 | } else { |
222 | - return fontItalic ?? font; | 249 | + // italic |
250 | + return fontItalic ?? fontNormal ?? fontBold ?? fontBoldItalic; | ||
223 | } | 251 | } |
224 | } else { | 252 | } else { |
225 | - if (fontStyle == FontStyle.normal) { | ||
226 | - return fontBold ?? font; | 253 | + if (fontStyle != FontStyle.italic) { |
254 | + // bold | ||
255 | + return fontBold ?? fontNormal ?? fontItalic ?? fontBoldItalic; | ||
227 | } else { | 256 | } else { |
228 | - return fontBoldItalic ?? fontBold ?? fontItalic ?? font; | 257 | + // bold + italic |
258 | + return fontBoldItalic ?? fontBold ?? fontItalic ?? fontNormal; | ||
229 | } | 259 | } |
230 | } | 260 | } |
231 | } | 261 | } |
232 | 262 | ||
233 | @override | 263 | @override |
234 | String toString() => | 264 | String toString() => |
235 | - 'TextStyle(color:$color font:$paintFont size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)'; | 265 | + 'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)'; |
236 | } | 266 | } |
237 | 267 | ||
238 | @immutable | 268 | @immutable |
@@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl | ||
4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf |
5 | repository: https://github.com/DavBfr/dart_pdf | 5 | repository: https://github.com/DavBfr/dart_pdf |
6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues | 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues |
7 | -version: 1.3.11 | 7 | +version: 1.3.12 |
8 | 8 | ||
9 | environment: | 9 | environment: |
10 | sdk: ">=2.1.0 <3.0.0" | 10 | sdk: ">=2.1.0 <3.0.0" |
@@ -31,11 +31,13 @@ import 'widget_container_test.dart' as widget_container; | @@ -31,11 +31,13 @@ import 'widget_container_test.dart' as widget_container; | ||
31 | import 'widget_table_test.dart' as widget_table; | 31 | import 'widget_table_test.dart' as widget_table; |
32 | import 'widget_test.dart' as widget; | 32 | import 'widget_test.dart' as widget; |
33 | import 'widget_text_test.dart' as widget_text; | 33 | import 'widget_text_test.dart' as widget_text; |
34 | +import 'widget_theme_test.dart' as widget_theme; | ||
34 | 35 | ||
35 | void main() { | 36 | void main() { |
36 | annotations.main(); | 37 | annotations.main(); |
37 | colors.main(); | 38 | colors.main(); |
38 | complex.main(); | 39 | complex.main(); |
40 | + example.main(); | ||
39 | jpeg.main(); | 41 | jpeg.main(); |
40 | metrics.main(); | 42 | metrics.main(); |
41 | minimal.main(); | 43 | minimal.main(); |
@@ -46,6 +48,6 @@ void main() { | @@ -46,6 +48,6 @@ void main() { | ||
46 | widget_container.main(); | 48 | widget_container.main(); |
47 | widget_table.main(); | 49 | widget_table.main(); |
48 | widget_text.main(); | 50 | widget_text.main(); |
51 | + widget_theme.main(); | ||
49 | widget.main(); | 52 | widget.main(); |
50 | - example.main(); | ||
51 | } | 53 | } |
@@ -175,7 +175,7 @@ void main() { | @@ -175,7 +175,7 @@ void main() { | ||
175 | text: 'Hello ', | 175 | text: 'Hello ', |
176 | style: Theme.of(context).defaultTextStyle, | 176 | style: Theme.of(context).defaultTextStyle, |
177 | children: <TextSpan>[ | 177 | children: <TextSpan>[ |
178 | - const TextSpan( | 178 | + TextSpan( |
179 | text: 'bold', | 179 | text: 'bold', |
180 | style: TextStyle( | 180 | style: TextStyle( |
181 | fontWeight: FontWeight.bold, | 181 | fontWeight: FontWeight.bold, |
pdf/test/widget_theme_test.dart
0 → 100644
1 | +/* | ||
2 | + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com> | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +import 'dart:io'; | ||
18 | +import 'dart:typed_data'; | ||
19 | + | ||
20 | +import 'package:pdf/pdf.dart'; | ||
21 | +import 'package:test/test.dart'; | ||
22 | +import 'package:pdf/widgets.dart'; | ||
23 | + | ||
24 | +Document pdf; | ||
25 | +Font openSans; | ||
26 | +Font openSansBold; | ||
27 | +Font roboto; | ||
28 | +Font notoSans; | ||
29 | +Font genyomintw; | ||
30 | + | ||
31 | +Font loadFont(String filename) { | ||
32 | + final Uint8List data = File(filename).readAsBytesSync(); | ||
33 | + return Font.ttf(data.buffer.asByteData()); | ||
34 | +} | ||
35 | + | ||
36 | +void main() { | ||
37 | + setUpAll(() { | ||
38 | + Document.debug = true; | ||
39 | + RichText.debug = true; | ||
40 | + | ||
41 | + openSans = loadFont('open-sans.ttf'); | ||
42 | + openSansBold = loadFont('open-sans-bold.ttf'); | ||
43 | + roboto = loadFont('roboto.ttf'); | ||
44 | + notoSans = loadFont('noto-sans.ttf'); | ||
45 | + genyomintw = loadFont('genyomintw.ttf'); | ||
46 | + | ||
47 | + pdf = Document(); | ||
48 | + }); | ||
49 | + | ||
50 | + test('Theme FontStyle', () { | ||
51 | + final TextStyle style = TextStyle( | ||
52 | + font: roboto, | ||
53 | + fontBold: openSansBold, | ||
54 | + fontNormal: openSans, | ||
55 | + fontItalic: notoSans, | ||
56 | + fontBoldItalic: genyomintw, | ||
57 | + fontWeight: FontWeight.bold, | ||
58 | + fontSize: 20, | ||
59 | + color: PdfColors.blue); | ||
60 | + | ||
61 | + pdf.addPage(Page( | ||
62 | + build: (Context context) => ListView( | ||
63 | + children: <Widget>[ | ||
64 | + Text( | ||
65 | + style.font.fontName, | ||
66 | + style: style, | ||
67 | + ), | ||
68 | + ], | ||
69 | + ), | ||
70 | + )); | ||
71 | + }); | ||
72 | + | ||
73 | + tearDownAll(() { | ||
74 | + final File file = File('widgets-theme.pdf'); | ||
75 | + file.writeAsBytesSync(pdf.save()); | ||
76 | + }); | ||
77 | +} |
-
Please register or login to post a comment