David PHAM-VAN

Fix TextStyle constructor

# 1.3.12
* Fix TextStyle constructor
# 1.3.11
* Update Readme
... ...
... ... @@ -179,7 +179,7 @@ class RichText extends Widget {
return true;
}
final PdfFont font = style.paintFont.getFont(context);
final PdfFont font = style.font.getFont(context);
final PdfFontMetrics space =
font.stringMetrics(' ') * (style.fontSize * textScaleFactor);
... ... @@ -322,7 +322,7 @@ class RichText extends Widget {
}
context.canvas.drawString(
currentStyle.paintFont.getFont(context),
currentStyle.font.getFont(context),
currentStyle.fontSize * textScaleFactor,
word.text,
box.x + word.offset.x,
... ...
... ... @@ -25,10 +25,11 @@ class TextStyle {
const TextStyle({
this.inherit = true,
this.color,
this.font,
this.fontBold,
this.fontItalic,
this.fontBoldItalic,
Font font,
Font fontNormal,
Font fontBold,
Font fontItalic,
Font fontBoldItalic,
this.fontSize,
this.fontWeight,
this.fontStyle,
... ... @@ -45,12 +46,28 @@ class TextStyle {
assert(inherit || letterSpacing != null),
assert(inherit || wordSpacing != null),
assert(inherit || lineSpacing != null),
assert(inherit || height != null);
assert(inherit || height != null),
fontNormal = fontNormal ??
(fontStyle != FontStyle.italic && fontWeight != FontWeight.bold
? font
: null),
fontBold = fontBold ??
(fontStyle != FontStyle.italic && fontWeight == FontWeight.bold
? font
: null),
fontItalic = fontItalic ??
(fontStyle == FontStyle.italic && fontWeight != FontWeight.bold
? font
: null),
fontBoldItalic = fontBoldItalic ??
(fontStyle == FontStyle.italic && fontWeight == FontWeight.bold
? font
: null);
factory TextStyle.defaultStyle() {
return TextStyle(
color: PdfColors.black,
font: Font.helvetica(),
fontNormal: Font.helvetica(),
fontBold: Font.helveticaBold(),
fontItalic: Font.helveticaOblique(),
fontBoldItalic: Font.helveticaBoldOblique(),
... ... @@ -68,7 +85,7 @@ class TextStyle {
final PdfColor color;
final Font font;
final Font fontNormal;
final Font fontBold;
... ... @@ -103,6 +120,7 @@ class TextStyle {
TextStyle copyWith({
PdfColor color,
Font font,
Font fontNormal,
Font fontBold,
Font fontItalic,
Font fontBoldItalic,
... ... @@ -119,6 +137,7 @@ class TextStyle {
inherit: inherit,
color: color ?? this.color,
font: font ?? this.font,
fontNormal: fontNormal ?? this.fontNormal,
fontBold: fontBold ?? this.fontBold,
fontItalic: fontItalic ?? this.fontItalic,
fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,
... ... @@ -138,6 +157,7 @@ class TextStyle {
TextStyle apply({
PdfColor color,
Font font,
Font fontNormal,
Font fontBold,
Font fontItalic,
Font fontBoldItalic,
... ... @@ -169,6 +189,7 @@ class TextStyle {
inherit: inherit,
color: color ?? this.color,
font: font ?? this.font,
fontNormal: fontNormal ?? this.fontNormal,
fontBold: fontBold ?? this.fontBold,
fontItalic: fontItalic ?? this.fontItalic,
fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,
... ... @@ -201,6 +222,8 @@ class TextStyle {
return copyWith(
color: other.color,
font: other.font,
fontNormal: other.fontNormal,
fontBold: other.fontBold,
fontItalic: other.fontItalic,
fontBoldItalic: other.fontBoldItalic,
fontSize: other.fontSize,
... ... @@ -214,25 +237,32 @@ class TextStyle {
);
}
Font get paintFont {
if (fontWeight == FontWeight.normal) {
if (fontStyle == FontStyle.normal) {
return font;
@Deprecated('use font instead')
Font get paintFont => font;
Font get font {
if (fontWeight != FontWeight.bold) {
if (fontStyle != FontStyle.italic) {
// normal
return fontNormal ?? fontBold ?? fontItalic ?? fontBoldItalic;
} else {
return fontItalic ?? font;
// italic
return fontItalic ?? fontNormal ?? fontBold ?? fontBoldItalic;
}
} else {
if (fontStyle == FontStyle.normal) {
return fontBold ?? font;
if (fontStyle != FontStyle.italic) {
// bold
return fontBold ?? fontNormal ?? fontItalic ?? fontBoldItalic;
} else {
return fontBoldItalic ?? fontBold ?? fontItalic ?? font;
// bold + italic
return fontBoldItalic ?? fontBold ?? fontItalic ?? fontNormal;
}
}
}
@override
String toString() =>
'TextStyle(color:$color font:$paintFont size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)';
'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)';
}
@immutable
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 1.3.11
version: 1.3.12
environment:
sdk: ">=2.1.0 <3.0.0"
... ...
... ... @@ -31,11 +31,13 @@ import 'widget_container_test.dart' as widget_container;
import 'widget_table_test.dart' as widget_table;
import 'widget_test.dart' as widget;
import 'widget_text_test.dart' as widget_text;
import 'widget_theme_test.dart' as widget_theme;
void main() {
annotations.main();
colors.main();
complex.main();
example.main();
jpeg.main();
metrics.main();
minimal.main();
... ... @@ -46,6 +48,6 @@ void main() {
widget_container.main();
widget_table.main();
widget_text.main();
widget_theme.main();
widget.main();
example.main();
}
... ...
... ... @@ -175,7 +175,7 @@ void main() {
text: 'Hello ',
style: Theme.of(context).defaultTextStyle,
children: <TextSpan>[
const TextSpan(
TextSpan(
text: 'bold',
style: TextStyle(
fontWeight: FontWeight.bold,
... ...
/*
* 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:typed_data';
import 'package:pdf/pdf.dart';
import 'package:test/test.dart';
import 'package:pdf/widgets.dart';
Document pdf;
Font openSans;
Font openSansBold;
Font roboto;
Font notoSans;
Font genyomintw;
Font loadFont(String filename) {
final Uint8List data = File(filename).readAsBytesSync();
return Font.ttf(data.buffer.asByteData());
}
void main() {
setUpAll(() {
Document.debug = true;
RichText.debug = true;
openSans = loadFont('open-sans.ttf');
openSansBold = loadFont('open-sans-bold.ttf');
roboto = loadFont('roboto.ttf');
notoSans = loadFont('noto-sans.ttf');
genyomintw = loadFont('genyomintw.ttf');
pdf = Document();
});
test('Theme FontStyle', () {
final TextStyle style = TextStyle(
font: roboto,
fontBold: openSansBold,
fontNormal: openSans,
fontItalic: notoSans,
fontBoldItalic: genyomintw,
fontWeight: FontWeight.bold,
fontSize: 20,
color: PdfColors.blue);
pdf.addPage(Page(
build: (Context context) => ListView(
children: <Widget>[
Text(
style.font.fontName,
style: style,
),
],
),
));
});
tearDownAll(() {
final File file = File('widgets-theme.pdf');
file.writeAsBytesSync(pdf.save());
});
}
... ...