OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:sky'; |
| 6 |
| 7 enum FontWeight { |
| 8 w100, |
| 9 w200, |
| 10 w300, |
| 11 w400, |
| 12 w500, |
| 13 w600, |
| 14 w700, |
| 15 w800, |
| 16 w900 |
| 17 } |
| 18 |
| 19 const thin = FontWeight.w100; |
| 20 const extraLight = FontWeight.w200; |
| 21 const light = FontWeight.w300; |
| 22 const normal = FontWeight.w400; |
| 23 const medium = FontWeight.w500; |
| 24 const semiBold = FontWeight.w600; |
| 25 const bold = FontWeight.w700; |
| 26 const extraBold = FontWeight.w800; |
| 27 const black = FontWeight.w900; |
| 28 |
| 29 enum TextAlign { |
| 30 left, |
| 31 right, |
| 32 center |
| 33 } |
| 34 |
| 35 class TextStyle { |
| 36 const TextStyle({ |
| 37 this.color, |
| 38 this.fontFamily, |
| 39 this.fontSize, |
| 40 this.fontWeight, |
| 41 this.textAlign |
| 42 }); |
| 43 |
| 44 final Color color; |
| 45 final String fontFamily; |
| 46 final double fontSize; // in pixels |
| 47 final FontWeight fontWeight; |
| 48 final TextAlign textAlign; |
| 49 |
| 50 TextStyle copyWith({ |
| 51 Color color, |
| 52 double fontSize, |
| 53 FontWeight fontWeight, |
| 54 TextAlign textAlign |
| 55 }) { |
| 56 return new TextStyle( |
| 57 color: color != null ? color : this.color, |
| 58 fontFamily: fontFamily != null ? fontFamily : this.fontFamily, |
| 59 fontSize: fontSize != null ? fontSize : this.fontSize, |
| 60 fontWeight: fontWeight != null ? fontWeight : this.fontWeight, |
| 61 textAlign: textAlign != null ? textAlign : this.textAlign |
| 62 ); |
| 63 } |
| 64 |
| 65 bool operator ==(other) { |
| 66 return other is TextStyle && |
| 67 color == other.color && |
| 68 fontFamily == other.fontFamily && |
| 69 fontSize == other.fontSize && |
| 70 fontWeight == other.fontWeight && |
| 71 textAlign == other.textAlign; |
| 72 } |
| 73 |
| 74 int get hashCode { |
| 75 // Use Quiver: https://github.com/domokit/mojo/issues/236 |
| 76 int value = 373; |
| 77 value = 37 * value + color.hashCode; |
| 78 value = 37 * value + fontFamily.hashCode; |
| 79 value = 37 * value + fontSize.hashCode; |
| 80 value = 37 * value + fontWeight.hashCode; |
| 81 value = 37 * value + textAlign.hashCode; |
| 82 return value; |
| 83 } |
| 84 |
| 85 void applyToCSSStyle(CSSStyleDeclaration cssStyle) { |
| 86 if (color != null) { |
| 87 cssStyle['color'] = 'rgba(${color.red}, ${color.green}, ${color.blue}, ${c
olor.alpha / 255.0})'; |
| 88 } |
| 89 // TODO(hansmuller): escape the fontFamily string. |
| 90 if (fontFamily != null) { |
| 91 cssStyle['font-family'] = fontFamily; |
| 92 } |
| 93 if (fontSize != null) { |
| 94 cssStyle['font-size'] = "${fontSize}px"; |
| 95 } |
| 96 if (fontWeight != null) { |
| 97 cssStyle['font-weight'] = const { |
| 98 FontWeight.w100: '100', |
| 99 FontWeight.w200: '200', |
| 100 FontWeight.w300: '300', |
| 101 FontWeight.w400: '400', |
| 102 FontWeight.w500: '500', |
| 103 FontWeight.w600: '600', |
| 104 FontWeight.w700: '700', |
| 105 FontWeight.w800: '800', |
| 106 FontWeight.w900: '900' |
| 107 }[fontWeight]; |
| 108 } |
| 109 if (textAlign != null) { |
| 110 cssStyle['text-align'] = const { |
| 111 TextAlign.left: 'left', |
| 112 TextAlign.right: 'right', |
| 113 TextAlign.center: 'center', |
| 114 }[textAlign]; |
| 115 } |
| 116 } |
| 117 |
| 118 String toString([String prefix = '']) { |
| 119 List<String> result = []; |
| 120 if (color != null) |
| 121 result.add('${prefix}color: $color'); |
| 122 // TODO(hansmuller): escape the fontFamily string. |
| 123 if (fontFamily != null) |
| 124 result.add('${prefix}fontFamily: "${fontFamily}"'); |
| 125 if (fontSize != null) |
| 126 result.add('${prefix}fontSize: $fontSize'); |
| 127 if (fontWeight != null) |
| 128 result.add('${prefix}fontWeight: $fontWeight'); |
| 129 if (textAlign != null) |
| 130 result.add('${prefix}textAlign: $textAlign'); |
| 131 if (result.isEmpty) |
| 132 return '${prefix}<no style specified>'; |
| 133 return result.join('\n'); |
| 134 } |
| 135 } |
OLD | NEW |