David PHAM-VAN

Move TextStyle class to its own file

@@ -43,6 +43,7 @@ part 'widgets/progress.dart'; @@ -43,6 +43,7 @@ part 'widgets/progress.dart';
43 part 'widgets/stack.dart'; 43 part 'widgets/stack.dart';
44 part 'widgets/table.dart'; 44 part 'widgets/table.dart';
45 part 'widgets/text.dart'; 45 part 'widgets/text.dart';
  46 +part 'widgets/text_style.dart';
46 part 'widgets/theme.dart'; 47 part 'widgets/theme.dart';
47 part 'widgets/widget.dart'; 48 part 'widgets/widget.dart';
48 part 'widgets/wrap.dart'; 49 part 'widgets/wrap.dart';
  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 +part of widget;
  18 +
  19 +enum FontWeight { normal, bold }
  20 +
  21 +enum FontStyle { normal, italic }
  22 +
  23 +@immutable
  24 +class TextStyle {
  25 + const TextStyle({
  26 + this.inherit = true,
  27 + this.color,
  28 + Font font,
  29 + Font fontNormal,
  30 + Font fontBold,
  31 + Font fontItalic,
  32 + Font fontBoldItalic,
  33 + this.fontSize,
  34 + this.fontWeight,
  35 + this.fontStyle,
  36 + this.letterSpacing,
  37 + this.wordSpacing,
  38 + this.lineSpacing,
  39 + this.height,
  40 + this.background,
  41 + }) : assert(inherit || color != null),
  42 + assert(inherit || font != null),
  43 + assert(inherit || fontSize != null),
  44 + assert(inherit || fontWeight != null),
  45 + assert(inherit || fontStyle != null),
  46 + assert(inherit || letterSpacing != null),
  47 + assert(inherit || wordSpacing != null),
  48 + assert(inherit || lineSpacing != 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);
  66 +
  67 + factory TextStyle.defaultStyle() {
  68 + return TextStyle(
  69 + color: PdfColors.black,
  70 + fontNormal: Font.helvetica(),
  71 + fontBold: Font.helveticaBold(),
  72 + fontItalic: Font.helveticaOblique(),
  73 + fontBoldItalic: Font.helveticaBoldOblique(),
  74 + fontSize: _defaultFontSize,
  75 + fontWeight: FontWeight.normal,
  76 + fontStyle: FontStyle.normal,
  77 + letterSpacing: 1.0,
  78 + wordSpacing: 1.0,
  79 + lineSpacing: 0.0,
  80 + height: 1.0,
  81 + );
  82 + }
  83 +
  84 + final bool inherit;
  85 +
  86 + final PdfColor color;
  87 +
  88 + final Font fontNormal;
  89 +
  90 + final Font fontBold;
  91 +
  92 + final Font fontItalic;
  93 +
  94 + final Font fontBoldItalic;
  95 +
  96 + // font height, in pdf unit
  97 + final double fontSize;
  98 +
  99 + /// The typeface thickness to use when painting the text (e.g., bold).
  100 + final FontWeight fontWeight;
  101 +
  102 + /// The typeface variant to use when drawing the letters (e.g., italics).
  103 + final FontStyle fontStyle;
  104 +
  105 + static const double _defaultFontSize = 12.0 * PdfPageFormat.point;
  106 +
  107 + // spacing between letters, 1.0 being natural spacing
  108 + final double letterSpacing;
  109 +
  110 + // spacing between lines, in pdf unit
  111 + final double lineSpacing;
  112 +
  113 + // spacing between words, 1.0 being natural spacing
  114 + final double wordSpacing;
  115 +
  116 + final double height;
  117 +
  118 + final PdfColor background;
  119 +
  120 + TextStyle copyWith({
  121 + PdfColor color,
  122 + Font font,
  123 + Font fontNormal,
  124 + Font fontBold,
  125 + Font fontItalic,
  126 + Font fontBoldItalic,
  127 + double fontSize,
  128 + FontWeight fontWeight,
  129 + FontStyle fontStyle,
  130 + double letterSpacing,
  131 + double wordSpacing,
  132 + double lineSpacing,
  133 + double height,
  134 + PdfColor background,
  135 + }) {
  136 + return TextStyle(
  137 + inherit: inherit,
  138 + color: color ?? this.color,
  139 + font: font ?? this.font,
  140 + fontNormal: fontNormal ?? this.fontNormal,
  141 + fontBold: fontBold ?? this.fontBold,
  142 + fontItalic: fontItalic ?? this.fontItalic,
  143 + fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,
  144 + fontSize: fontSize ?? this.fontSize,
  145 + fontWeight: fontWeight ?? this.fontWeight,
  146 + fontStyle: fontStyle ?? this.fontStyle,
  147 + letterSpacing: letterSpacing ?? this.letterSpacing,
  148 + wordSpacing: wordSpacing ?? this.wordSpacing,
  149 + lineSpacing: lineSpacing ?? this.lineSpacing,
  150 + height: height ?? this.height,
  151 + background: background ?? this.background,
  152 + );
  153 + }
  154 +
  155 + /// Creates a copy of this text style replacing or altering the specified
  156 + /// properties.
  157 + TextStyle apply({
  158 + PdfColor color,
  159 + Font font,
  160 + Font fontNormal,
  161 + Font fontBold,
  162 + Font fontItalic,
  163 + Font fontBoldItalic,
  164 + double fontSizeFactor = 1.0,
  165 + double fontSizeDelta = 0.0,
  166 + double letterSpacingFactor = 1.0,
  167 + double letterSpacingDelta = 0.0,
  168 + double wordSpacingFactor = 1.0,
  169 + double wordSpacingDelta = 0.0,
  170 + double heightFactor = 1.0,
  171 + double heightDelta = 0.0,
  172 + }) {
  173 + assert(fontSizeFactor != null);
  174 + assert(fontSizeDelta != null);
  175 + assert(fontSize != null || (fontSizeFactor == 1.0 && fontSizeDelta == 0.0));
  176 + assert(letterSpacingFactor != null);
  177 + assert(letterSpacingDelta != null);
  178 + assert(letterSpacing != null ||
  179 + (letterSpacingFactor == 1.0 && letterSpacingDelta == 0.0));
  180 + assert(wordSpacingFactor != null);
  181 + assert(wordSpacingDelta != null);
  182 + assert(wordSpacing != null ||
  183 + (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0));
  184 + assert(heightFactor != null);
  185 + assert(heightDelta != null);
  186 + assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0));
  187 +
  188 + return TextStyle(
  189 + inherit: inherit,
  190 + color: color ?? this.color,
  191 + font: font ?? this.font,
  192 + fontNormal: fontNormal ?? this.fontNormal,
  193 + fontBold: fontBold ?? this.fontBold,
  194 + fontItalic: fontItalic ?? this.fontItalic,
  195 + fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,
  196 + fontSize:
  197 + fontSize == null ? null : fontSize * fontSizeFactor + fontSizeDelta,
  198 + fontWeight: fontWeight,
  199 + fontStyle: fontStyle,
  200 + letterSpacing: letterSpacing == null
  201 + ? null
  202 + : letterSpacing * letterSpacingFactor + letterSpacingDelta,
  203 + wordSpacing: wordSpacing == null
  204 + ? null
  205 + : wordSpacing * wordSpacingFactor + wordSpacingDelta,
  206 + height: height == null ? null : height * heightFactor + heightDelta,
  207 + background: background,
  208 + );
  209 + }
  210 +
  211 + /// Returns a new text style that is a combination of this style and the given
  212 + /// [other] style.
  213 + TextStyle merge(TextStyle other) {
  214 + if (other == null) {
  215 + return this;
  216 + }
  217 +
  218 + if (!other.inherit) {
  219 + return other;
  220 + }
  221 +
  222 + return copyWith(
  223 + color: other.color,
  224 + font: other.font,
  225 + fontNormal: other.fontNormal,
  226 + fontBold: other.fontBold,
  227 + fontItalic: other.fontItalic,
  228 + fontBoldItalic: other.fontBoldItalic,
  229 + fontSize: other.fontSize,
  230 + fontWeight: other.fontWeight,
  231 + fontStyle: other.fontStyle,
  232 + letterSpacing: other.letterSpacing,
  233 + wordSpacing: other.wordSpacing,
  234 + lineSpacing: other.lineSpacing,
  235 + height: other.height,
  236 + background: other.background,
  237 + );
  238 + }
  239 +
  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;
  248 + } else {
  249 + // italic
  250 + return fontItalic ?? fontNormal ?? fontBold ?? fontBoldItalic;
  251 + }
  252 + } else {
  253 + if (fontStyle != FontStyle.italic) {
  254 + // bold
  255 + return fontBold ?? fontNormal ?? fontItalic ?? fontBoldItalic;
  256 + } else {
  257 + // bold + italic
  258 + return fontBoldItalic ?? fontBold ?? fontItalic ?? fontNormal;
  259 + }
  260 + }
  261 + }
  262 +
  263 + @override
  264 + String toString() =>
  265 + 'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)';
  266 +}
@@ -16,255 +16,6 @@ @@ -16,255 +16,6 @@
16 16
17 part of widget; 17 part of widget;
18 18
19 -enum FontWeight { normal, bold }  
20 -  
21 -enum FontStyle { normal, italic }  
22 -  
23 -@immutable  
24 -class TextStyle {  
25 - const TextStyle({  
26 - this.inherit = true,  
27 - this.color,  
28 - Font font,  
29 - Font fontNormal,  
30 - Font fontBold,  
31 - Font fontItalic,  
32 - Font fontBoldItalic,  
33 - this.fontSize,  
34 - this.fontWeight,  
35 - this.fontStyle,  
36 - this.letterSpacing,  
37 - this.wordSpacing,  
38 - this.lineSpacing,  
39 - this.height,  
40 - this.background,  
41 - }) : assert(inherit || color != null),  
42 - assert(inherit || font != null),  
43 - assert(inherit || fontSize != null),  
44 - assert(inherit || fontWeight != null),  
45 - assert(inherit || fontStyle != null),  
46 - assert(inherit || letterSpacing != null),  
47 - assert(inherit || wordSpacing != null),  
48 - assert(inherit || lineSpacing != 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);  
66 -  
67 - factory TextStyle.defaultStyle() {  
68 - return TextStyle(  
69 - color: PdfColors.black,  
70 - fontNormal: Font.helvetica(),  
71 - fontBold: Font.helveticaBold(),  
72 - fontItalic: Font.helveticaOblique(),  
73 - fontBoldItalic: Font.helveticaBoldOblique(),  
74 - fontSize: _defaultFontSize,  
75 - fontWeight: FontWeight.normal,  
76 - fontStyle: FontStyle.normal,  
77 - letterSpacing: 1.0,  
78 - wordSpacing: 1.0,  
79 - lineSpacing: 0.0,  
80 - height: 1.0,  
81 - );  
82 - }  
83 -  
84 - final bool inherit;  
85 -  
86 - final PdfColor color;  
87 -  
88 - final Font fontNormal;  
89 -  
90 - final Font fontBold;  
91 -  
92 - final Font fontItalic;  
93 -  
94 - final Font fontBoldItalic;  
95 -  
96 - // font height, in pdf unit  
97 - final double fontSize;  
98 -  
99 - /// The typeface thickness to use when painting the text (e.g., bold).  
100 - final FontWeight fontWeight;  
101 -  
102 - /// The typeface variant to use when drawing the letters (e.g., italics).  
103 - final FontStyle fontStyle;  
104 -  
105 - static const double _defaultFontSize = 12.0 * PdfPageFormat.point;  
106 -  
107 - // spacing between letters, 1.0 being natural spacing  
108 - final double letterSpacing;  
109 -  
110 - // spacing between lines, in pdf unit  
111 - final double lineSpacing;  
112 -  
113 - // spacing between words, 1.0 being natural spacing  
114 - final double wordSpacing;  
115 -  
116 - final double height;  
117 -  
118 - final PdfColor background;  
119 -  
120 - TextStyle copyWith({  
121 - PdfColor color,  
122 - Font font,  
123 - Font fontNormal,  
124 - Font fontBold,  
125 - Font fontItalic,  
126 - Font fontBoldItalic,  
127 - double fontSize,  
128 - FontWeight fontWeight,  
129 - FontStyle fontStyle,  
130 - double letterSpacing,  
131 - double wordSpacing,  
132 - double lineSpacing,  
133 - double height,  
134 - PdfColor background,  
135 - }) {  
136 - return TextStyle(  
137 - inherit: inherit,  
138 - color: color ?? this.color,  
139 - font: font ?? this.font,  
140 - fontNormal: fontNormal ?? this.fontNormal,  
141 - fontBold: fontBold ?? this.fontBold,  
142 - fontItalic: fontItalic ?? this.fontItalic,  
143 - fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,  
144 - fontSize: fontSize ?? this.fontSize,  
145 - fontWeight: fontWeight ?? this.fontWeight,  
146 - fontStyle: fontStyle ?? this.fontStyle,  
147 - letterSpacing: letterSpacing ?? this.letterSpacing,  
148 - wordSpacing: wordSpacing ?? this.wordSpacing,  
149 - lineSpacing: lineSpacing ?? this.lineSpacing,  
150 - height: height ?? this.height,  
151 - background: background ?? this.background,  
152 - );  
153 - }  
154 -  
155 - /// Creates a copy of this text style replacing or altering the specified  
156 - /// properties.  
157 - TextStyle apply({  
158 - PdfColor color,  
159 - Font font,  
160 - Font fontNormal,  
161 - Font fontBold,  
162 - Font fontItalic,  
163 - Font fontBoldItalic,  
164 - double fontSizeFactor = 1.0,  
165 - double fontSizeDelta = 0.0,  
166 - double letterSpacingFactor = 1.0,  
167 - double letterSpacingDelta = 0.0,  
168 - double wordSpacingFactor = 1.0,  
169 - double wordSpacingDelta = 0.0,  
170 - double heightFactor = 1.0,  
171 - double heightDelta = 0.0,  
172 - }) {  
173 - assert(fontSizeFactor != null);  
174 - assert(fontSizeDelta != null);  
175 - assert(fontSize != null || (fontSizeFactor == 1.0 && fontSizeDelta == 0.0));  
176 - assert(letterSpacingFactor != null);  
177 - assert(letterSpacingDelta != null);  
178 - assert(letterSpacing != null ||  
179 - (letterSpacingFactor == 1.0 && letterSpacingDelta == 0.0));  
180 - assert(wordSpacingFactor != null);  
181 - assert(wordSpacingDelta != null);  
182 - assert(wordSpacing != null ||  
183 - (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0));  
184 - assert(heightFactor != null);  
185 - assert(heightDelta != null);  
186 - assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0));  
187 -  
188 - return TextStyle(  
189 - inherit: inherit,  
190 - color: color ?? this.color,  
191 - font: font ?? this.font,  
192 - fontNormal: fontNormal ?? this.fontNormal,  
193 - fontBold: fontBold ?? this.fontBold,  
194 - fontItalic: fontItalic ?? this.fontItalic,  
195 - fontBoldItalic: fontBoldItalic ?? this.fontBoldItalic,  
196 - fontSize:  
197 - fontSize == null ? null : fontSize * fontSizeFactor + fontSizeDelta,  
198 - fontWeight: fontWeight,  
199 - fontStyle: fontStyle,  
200 - letterSpacing: letterSpacing == null  
201 - ? null  
202 - : letterSpacing * letterSpacingFactor + letterSpacingDelta,  
203 - wordSpacing: wordSpacing == null  
204 - ? null  
205 - : wordSpacing * wordSpacingFactor + wordSpacingDelta,  
206 - height: height == null ? null : height * heightFactor + heightDelta,  
207 - background: background,  
208 - );  
209 - }  
210 -  
211 - /// Returns a new text style that is a combination of this style and the given  
212 - /// [other] style.  
213 - TextStyle merge(TextStyle other) {  
214 - if (other == null) {  
215 - return this;  
216 - }  
217 -  
218 - if (!other.inherit) {  
219 - return other;  
220 - }  
221 -  
222 - return copyWith(  
223 - color: other.color,  
224 - font: other.font,  
225 - fontNormal: other.fontNormal,  
226 - fontBold: other.fontBold,  
227 - fontItalic: other.fontItalic,  
228 - fontBoldItalic: other.fontBoldItalic,  
229 - fontSize: other.fontSize,  
230 - fontWeight: other.fontWeight,  
231 - fontStyle: other.fontStyle,  
232 - letterSpacing: other.letterSpacing,  
233 - wordSpacing: other.wordSpacing,  
234 - lineSpacing: other.lineSpacing,  
235 - height: other.height,  
236 - background: other.background,  
237 - );  
238 - }  
239 -  
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;  
248 - } else {  
249 - // italic  
250 - return fontItalic ?? fontNormal ?? fontBold ?? fontBoldItalic;  
251 - }  
252 - } else {  
253 - if (fontStyle != FontStyle.italic) {  
254 - // bold  
255 - return fontBold ?? fontNormal ?? fontItalic ?? fontBoldItalic;  
256 - } else {  
257 - // bold + italic  
258 - return fontBoldItalic ?? fontBold ?? fontItalic ?? fontNormal;  
259 - }  
260 - }  
261 - }  
262 -  
263 - @override  
264 - String toString() =>  
265 - 'TextStyle(color:$color font:$font size:$fontSize weight:$fontWeight style:$fontStyle letterSpacing:$letterSpacing wordSpacing:$wordSpacing lineSpacing:$lineSpacing height:$height background:$background)';  
266 -}  
267 -  
268 @immutable 19 @immutable
269 class Theme extends Inherited { 20 class Theme extends Inherited {
270 const Theme({ 21 const Theme({