| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:sky'; | 5 import 'dart:sky'; |
| 6 | 6 |
| 7 enum FontWeight { | 7 enum FontWeight { |
| 8 w100, | 8 w100, |
| 9 w200, | 9 w200, |
| 10 w300, | 10 w300, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 center | 32 center |
| 33 } | 33 } |
| 34 | 34 |
| 35 enum TextDecoration { | 35 enum TextDecoration { |
| 36 none, | 36 none, |
| 37 underline, | 37 underline, |
| 38 overline, | 38 overline, |
| 39 lineThrough | 39 lineThrough |
| 40 } | 40 } |
| 41 | 41 |
| 42 const underline = const <TextDecoration> [TextDecoration.underline]; | 42 const underline = const <TextDecoration>[TextDecoration.underline]; |
| 43 const overline = const <TextDecoration> [TextDecoration.overline]; | 43 const overline = const <TextDecoration>[TextDecoration.overline]; |
| 44 const lineThrough = const <TextDecoration> [TextDecoration.lineThrough]; | 44 const lineThrough = const <TextDecoration>[TextDecoration.lineThrough]; |
| 45 | 45 |
| 46 enum TextDecorationStyle { | 46 enum TextDecorationStyle { |
| 47 solid, | 47 solid, |
| 48 double, | 48 double, |
| 49 dotted, | 49 dotted, |
| 50 dashed, | 50 dashed, |
| 51 wavy | 51 wavy |
| 52 } | 52 } |
| 53 | 53 |
| 54 class TextStyle { | 54 class TextStyle { |
| 55 const TextStyle({ | 55 const TextStyle({ |
| 56 this.color, | 56 this.color, |
| 57 this.fontFamily, | 57 this.fontFamily, |
| 58 this.fontSize, | 58 this.fontSize, |
| 59 this.fontWeight, | 59 this.fontWeight, |
| 60 this.textAlign, | 60 this.textAlign, |
| 61 List<TextDecoration> this.decoration, | 61 this.decoration, |
| 62 this.decorationColor, | 62 this.decorationColor, |
| 63 this.decorationStyle | 63 this.decorationStyle |
| 64 }); | 64 }); |
| 65 | 65 |
| 66 final Color color; | 66 final Color color; |
| 67 final String fontFamily; | 67 final String fontFamily; |
| 68 final double fontSize; // in pixels | 68 final double fontSize; // in pixels |
| 69 final FontWeight fontWeight; | 69 final FontWeight fontWeight; |
| 70 final TextAlign textAlign; | 70 final TextAlign textAlign; |
| 71 final List<TextDecoration> decoration; | 71 final List<TextDecoration> decoration; // TODO(ianh): Switch this to a Set<> o
nce Dart supports constant Sets |
| 72 final Color decorationColor; | 72 final Color decorationColor; |
| 73 final TextDecorationStyle decorationStyle; | 73 final TextDecorationStyle decorationStyle; |
| 74 | 74 |
| 75 String _decorationToString() { | |
| 76 assert(decoration != null); | |
| 77 const toCSS = const { | |
| 78 TextDecoration.none: 'none', | |
| 79 TextDecoration.underline: 'underline', | |
| 80 TextDecoration.overline: 'overline', | |
| 81 TextDecoration.lineThrough: 'lineThrough' | |
| 82 }; | |
| 83 return decoration.map((d) => toCSS[d]).join(' '); | |
| 84 } | |
| 85 | |
| 86 TextStyle copyWith({ | 75 TextStyle copyWith({ |
| 87 Color color, | 76 Color color, |
| 88 double fontSize, | 77 double fontSize, |
| 89 FontWeight fontWeight, | 78 FontWeight fontWeight, |
| 90 TextAlign textAlign, | 79 TextAlign textAlign, |
| 91 TextDecoration decoration, | 80 TextDecoration decoration, |
| 92 Color decorationColor, | 81 Color decorationColor, |
| 93 TextDecorationStyle decorationStyle | 82 TextDecorationStyle decorationStyle |
| 94 }) { | 83 }) { |
| 95 return new TextStyle( | 84 return new TextStyle( |
| 96 color: color != null ? color : this.color, | 85 color: color != null ? color : this.color, |
| 97 fontFamily: fontFamily != null ? fontFamily : this.fontFamily, | 86 fontFamily: fontFamily != null ? fontFamily : this.fontFamily, |
| 98 fontSize: fontSize != null ? fontSize : this.fontSize, | 87 fontSize: fontSize != null ? fontSize : this.fontSize, |
| 99 fontWeight: fontWeight != null ? fontWeight : this.fontWeight, | 88 fontWeight: fontWeight != null ? fontWeight : this.fontWeight, |
| 100 textAlign: textAlign != null ? textAlign : this.textAlign, | 89 textAlign: textAlign != null ? textAlign : this.textAlign, |
| 101 decoration: decoration != null ? decoration : this.decoration, | 90 decoration: decoration != null ? decoration : this.decoration, |
| 102 decorationColor: decorationColor != null ? decorationColor : this.decorati
onColor, | 91 decorationColor: decorationColor != null ? decorationColor : this.decorati
onColor, |
| 103 decorationStyle: decorationStyle != null ? decorationStyle : this.decorati
onStyle | 92 decorationStyle: decorationStyle != null ? decorationStyle : this.decorati
onStyle |
| 104 ); | 93 ); |
| 105 } | 94 } |
| 106 | 95 |
| 107 bool operator ==(other) { | 96 static String _colorToCSSString(Color color) { |
| 108 if (identical(this, other)) | 97 return 'rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha / 25
5.0})'; |
| 109 return true; | |
| 110 return other is TextStyle && | |
| 111 color == other.color && | |
| 112 fontFamily == other.fontFamily && | |
| 113 fontSize == other.fontSize && | |
| 114 fontWeight == other.fontWeight && | |
| 115 textAlign == other.textAlign && | |
| 116 decoration == other.decoration && | |
| 117 decorationColor == other.decorationColor && | |
| 118 decorationStyle == other.decorationStyle; | |
| 119 } | 98 } |
| 120 | 99 |
| 121 int get hashCode { | 100 static String _fontFamilyToCSSString(String fontFamily) { |
| 122 // Use Quiver: https://github.com/domokit/mojo/issues/236 | 101 // TODO(hansmuller): escape the fontFamily string. |
| 123 int value = 373; | 102 return fontFamily; |
| 124 value = 37 * value + color.hashCode; | 103 } |
| 125 value = 37 * value + fontFamily.hashCode; | 104 |
| 126 value = 37 * value + fontSize.hashCode; | 105 static String _decorationToCSSString(List<TextDecoration> decoration) { |
| 127 value = 37 * value + fontWeight.hashCode; | 106 assert(decoration != null); |
| 128 value = 37 * value + textAlign.hashCode; | 107 const toCSS = const <TextDecoration, String>{ |
| 129 value = 37 * value + decoration.hashCode; | 108 TextDecoration.none: 'none', |
| 130 value = 37 * value + decorationColor.hashCode; | 109 TextDecoration.underline: 'underline', |
| 131 value = 37 * value + decorationStyle.hashCode; | 110 TextDecoration.overline: 'overline', |
| 132 return value; | 111 TextDecoration.lineThrough: 'lineThrough' |
| 112 }; |
| 113 return decoration.map((d) => toCSS[d]).join(' '); |
| 114 } |
| 115 |
| 116 static String _decorationStyleToCSSString(TextDecorationStyle decorationStyle)
{ |
| 117 assert(decorationStyle != null); |
| 118 const toCSS = const <TextDecorationStyle, String>{ |
| 119 TextDecorationStyle.solid: 'solid', |
| 120 TextDecorationStyle.double: 'double', |
| 121 TextDecorationStyle.dotted: 'dotted', |
| 122 TextDecorationStyle.dashed: 'dashed', |
| 123 TextDecorationStyle.wavy: 'wavy' |
| 124 }; |
| 125 return toCSS[decorationStyle]; |
| 133 } | 126 } |
| 134 | 127 |
| 135 void applyToCSSStyle(CSSStyleDeclaration cssStyle) { | 128 void applyToCSSStyle(CSSStyleDeclaration cssStyle) { |
| 136 if (color != null) { | 129 if (color != null) { |
| 137 cssStyle['color'] = 'rgba(${color.red}, ${color.green}, ${color.blue}, ${c
olor.alpha / 255.0})'; | 130 cssStyle['color'] = _colorToCSSString(color); |
| 138 } | 131 } |
| 139 // TODO(hansmuller): escape the fontFamily string. | |
| 140 if (fontFamily != null) { | 132 if (fontFamily != null) { |
| 141 cssStyle['font-family'] = fontFamily; | 133 cssStyle['font-family'] = _fontFamilyToCSSString(fontFamily); |
| 142 } | 134 } |
| 143 if (fontSize != null) { | 135 if (fontSize != null) { |
| 144 cssStyle['font-size'] = "${fontSize}px"; | 136 cssStyle['font-size'] = "${fontSize}px"; |
| 145 } | 137 } |
| 146 if (fontWeight != null) { | 138 if (fontWeight != null) { |
| 147 cssStyle['font-weight'] = const { | 139 cssStyle['font-weight'] = const { |
| 148 FontWeight.w100: '100', | 140 FontWeight.w100: '100', |
| 149 FontWeight.w200: '200', | 141 FontWeight.w200: '200', |
| 150 FontWeight.w300: '300', | 142 FontWeight.w300: '300', |
| 151 FontWeight.w400: '400', | 143 FontWeight.w400: '400', |
| 152 FontWeight.w500: '500', | 144 FontWeight.w500: '500', |
| 153 FontWeight.w600: '600', | 145 FontWeight.w600: '600', |
| 154 FontWeight.w700: '700', | 146 FontWeight.w700: '700', |
| 155 FontWeight.w800: '800', | 147 FontWeight.w800: '800', |
| 156 FontWeight.w900: '900' | 148 FontWeight.w900: '900' |
| 157 }[fontWeight]; | 149 }[fontWeight]; |
| 158 } | 150 } |
| 159 if (textAlign != null) { | 151 if (textAlign != null) { |
| 160 cssStyle['text-align'] = const { | 152 cssStyle['text-align'] = const { |
| 161 TextAlign.left: 'left', | 153 TextAlign.left: 'left', |
| 162 TextAlign.right: 'right', | 154 TextAlign.right: 'right', |
| 163 TextAlign.center: 'center', | 155 TextAlign.center: 'center', |
| 164 }[textAlign]; | 156 }[textAlign]; |
| 165 } | 157 } |
| 166 if (decoration != null) { | 158 if (decoration != null) { |
| 167 cssStyle['text-decoration'] = _decorationToString(); | 159 cssStyle['text-decoration'] = _decorationToCSSString(decoration); |
| 160 if (decorationColor != null) |
| 161 cssStyle['text-decoration-color'] = _colorToCSSString(decorationColor); |
| 162 if (decorationStyle != null) |
| 163 cssStyle['text-decoration-style'] = _decorationStyleToCSSString(decorati
onStyle); |
| 168 } | 164 } |
| 169 // TODO(hansmuller): add support for decoration color and style. | 165 } |
| 166 |
| 167 bool operator ==(other) { |
| 168 if (identical(this, other)) |
| 169 return true; |
| 170 return other is TextStyle && |
| 171 color == other.color && |
| 172 fontFamily == other.fontFamily && |
| 173 fontSize == other.fontSize && |
| 174 fontWeight == other.fontWeight && |
| 175 textAlign == other.textAlign && |
| 176 decoration == other.decoration && |
| 177 decorationColor == other.decorationColor && |
| 178 decorationStyle == other.decorationStyle; |
| 179 } |
| 180 |
| 181 int get hashCode { |
| 182 // Use Quiver: https://github.com/domokit/mojo/issues/236 |
| 183 int value = 373; |
| 184 value = 37 * value + color.hashCode; |
| 185 value = 37 * value + fontFamily.hashCode; |
| 186 value = 37 * value + fontSize.hashCode; |
| 187 value = 37 * value + fontWeight.hashCode; |
| 188 value = 37 * value + textAlign.hashCode; |
| 189 value = 37 * value + decoration.hashCode; |
| 190 value = 37 * value + decorationColor.hashCode; |
| 191 value = 37 * value + decorationStyle.hashCode; |
| 192 return value; |
| 170 } | 193 } |
| 171 | 194 |
| 172 String toString([String prefix = '']) { | 195 String toString([String prefix = '']) { |
| 173 List<String> result = []; | 196 List<String> result = []; |
| 174 if (color != null) | 197 if (color != null) |
| 175 result.add('${prefix}color: $color'); | 198 result.add('${prefix}color: $color'); |
| 176 // TODO(hansmuller): escape the fontFamily string. | 199 // TODO(hansmuller): escape the fontFamily string. |
| 177 if (fontFamily != null) | 200 if (fontFamily != null) |
| 178 result.add('${prefix}fontFamily: "${fontFamily}"'); | 201 result.add('${prefix}fontFamily: "${fontFamily}"'); |
| 179 if (fontSize != null) | 202 if (fontSize != null) |
| 180 result.add('${prefix}fontSize: $fontSize'); | 203 result.add('${prefix}fontSize: $fontSize'); |
| 181 if (fontWeight != null) | 204 if (fontWeight != null) |
| 182 result.add('${prefix}fontWeight: $fontWeight'); | 205 result.add('${prefix}fontWeight: $fontWeight'); |
| 183 if (textAlign != null) | 206 if (textAlign != null) |
| 184 result.add('${prefix}textAlign: $textAlign'); | 207 result.add('${prefix}textAlign: $textAlign'); |
| 185 if (decoration != null) | 208 if (decoration != null) |
| 186 result.add('${prefix}decoration: ${_decorationToString()}'); | 209 result.add('${prefix}decoration: $decoration'); |
| 187 if (decorationColor != null) | 210 if (decorationColor != null) |
| 188 result.add('${prefix}decorationColor: $decorationColor'); | 211 result.add('${prefix}decorationColor: $decorationColor'); |
| 189 if (decorationStyle != null) | 212 if (decorationStyle != null) |
| 190 result.add('${prefix}decorationStyle: $decorationStyle'); | 213 result.add('${prefix}decorationStyle: $decorationStyle'); |
| 191 if (result.isEmpty) | 214 if (result.isEmpty) |
| 192 return '${prefix}<no style specified>'; | 215 return '${prefix}<no style specified>'; |
| 193 return result.join('\n'); | 216 return result.join('\n'); |
| 194 } | 217 } |
| 195 } | 218 } |
| OLD | NEW |