text_style.dart 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:sky';

enum FontWeight { w100, w200, w300, w400, w500, w600, w700, w800, w900 }
const normal = FontWeight.w400;
const bold = FontWeight.w700;

11 12
enum FontStyle { normal, italic, oblique }

13 14
enum TextAlign { left, right, center }

15 16
enum TextBaseline { alphabetic, ideographic }

17 18 19 20 21 22 23 24 25 26 27 28 29
enum TextDecoration { none, underline, overline, lineThrough }
const underline = const <TextDecoration>[TextDecoration.underline];
const overline = const <TextDecoration>[TextDecoration.overline];
const lineThrough = const <TextDecoration>[TextDecoration.lineThrough];

enum TextDecorationStyle { solid, double, dotted, dashed, wavy }

class TextStyle {
  const TextStyle({
    this.color,
    this.fontFamily,
    this.fontSize,
    this.fontWeight,
30
    this.fontStyle,
31
    this.textAlign,
32
    this.textBaseline,
33 34 35 36 37 38 39 40 41 42
    this.height,
    this.decoration,
    this.decorationColor,
    this.decorationStyle
  });

  final Color color;
  final String fontFamily;
  final double fontSize; // in pixels
  final FontWeight fontWeight;
43
  final FontStyle fontStyle;
44
  final TextAlign textAlign;
45
  final TextBaseline textBaseline;
46 47 48 49 50 51 52 53 54 55
  final double height; // multiple of fontSize
  final List<TextDecoration> decoration; // TODO(ianh): Switch this to a Set<> once Dart supports constant Sets
  final Color decorationColor;
  final TextDecorationStyle decorationStyle;

  TextStyle copyWith({
    Color color,
    String fontFamily,
    double fontSize,
    FontWeight fontWeight,
56
    FontStyle fontStyle,
57
    TextAlign textAlign,
58
    TextBaseline textBaseline,
59 60 61 62 63 64 65 66 67 68
    double height,
    List<TextDecoration> decoration,
    Color decorationColor,
    TextDecorationStyle decorationStyle
  }) {
    return new TextStyle(
      color: color != null ? color : this.color,
      fontFamily: fontFamily != null ? fontFamily : this.fontFamily,
      fontSize: fontSize != null ? fontSize : this.fontSize,
      fontWeight: fontWeight != null ? fontWeight : this.fontWeight,
69
      fontStyle: fontStyle != null ? fontStyle : this.fontStyle,
70
      textAlign: textAlign != null ? textAlign : this.textAlign,
71
      textBaseline: textBaseline != null ? textBaseline : this.textBaseline,
72 73 74 75 76 77 78 79 80 81 82 83 84
      height: height != null ? height : this.height,
      decoration: decoration != null ? decoration : this.decoration,
      decorationColor: decorationColor != null ? decorationColor : this.decorationColor,
      decorationStyle: decorationStyle != null ? decorationStyle : this.decorationStyle
    );
  }

  TextStyle merge(TextStyle other) {
    return copyWith(
      color: other.color,
      fontFamily: other.fontFamily,
      fontSize: other.fontSize,
      fontWeight: other.fontWeight,
85
      fontStyle: other.fontStyle,
86
      textAlign: other.textAlign,
87
      textBaseline: other.textBaseline,
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      height: other.height,
      decoration: other.decoration,
      decorationColor: other.decorationColor,
      decorationStyle: other.decorationStyle
    );
  }

  static String _colorToCSSString(Color color) {
    return 'rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha / 255.0})';
  }

  static String _fontFamilyToCSSString(String fontFamily) {
    // TODO(hansmuller): escape the fontFamily string.
    return fontFamily;
  }

  static String _decorationToCSSString(List<TextDecoration> decoration) {
    assert(decoration != null);
    const toCSS = const <TextDecoration, String>{
      TextDecoration.none: 'none',
      TextDecoration.underline: 'underline',
      TextDecoration.overline: 'overline',
      TextDecoration.lineThrough: 'lineThrough'
    };
    return decoration.map((d) => toCSS[d]).join(' ');
  }

  static String _decorationStyleToCSSString(TextDecorationStyle decorationStyle) {
    assert(decorationStyle != null);
    const toCSS = const <TextDecorationStyle, String>{
      TextDecorationStyle.solid: 'solid',
      TextDecorationStyle.double: 'double',
      TextDecorationStyle.dotted: 'dotted',
      TextDecorationStyle.dashed: 'dashed',
      TextDecorationStyle.wavy: 'wavy'
    };
    return toCSS[decorationStyle];
  }

  void applyToCSSStyle(CSSStyleDeclaration cssStyle) {
    if (color != null) {
      cssStyle['color'] = _colorToCSSString(color);
    }
    if (fontFamily != null) {
      cssStyle['font-family'] = _fontFamilyToCSSString(fontFamily);
    }
    if (fontSize != null) {
      cssStyle['font-size'] = '${fontSize}px';
    }
    if (fontWeight != null) {
      cssStyle['font-weight'] = const {
        FontWeight.w100: '100',
        FontWeight.w200: '200',
        FontWeight.w300: '300',
        FontWeight.w400: '400',
        FontWeight.w500: '500',
        FontWeight.w600: '600',
        FontWeight.w700: '700',
        FontWeight.w800: '800',
        FontWeight.w900: '900'
      }[fontWeight];
    }
150 151 152 153 154 155 156
    if (fontStyle != null) {
      cssStyle['font-style'] = const {
        FontStyle.normal: 'normal',
        FontStyle.italic: 'italic',
        FontStyle.oblique: 'oblique',
      }[fontStyle];
    }
Adam Barth's avatar
Adam Barth committed
157 158 159 160 161 162 163 164 165 166
    if (decoration != null) {
      cssStyle['text-decoration'] = _decorationToCSSString(decoration);
      if (decorationColor != null)
        cssStyle['text-decoration-color'] = _colorToCSSString(decorationColor);
      if (decorationStyle != null)
        cssStyle['text-decoration-style'] = _decorationStyleToCSSString(decorationStyle);
    }
  }

  void applyToContainerCSSStyle(CSSStyleDeclaration cssStyle) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    if (textAlign != null) {
      cssStyle['text-align'] = const {
        TextAlign.left: 'left',
        TextAlign.right: 'right',
        TextAlign.center: 'center',
      }[textAlign];
    }
    if (height != null) {
      cssStyle['line-height'] = '${height}';
    }
  }

  bool operator ==(other) {
    if (identical(this, other))
      return true;
    return other is TextStyle &&
      color == other.color &&
184
      fontFamily == other.fontFamily &&
185 186
      fontSize == other.fontSize &&
      fontWeight == other.fontWeight &&
187
      fontStyle == other.fontStyle &&
188 189
      textAlign == other.textAlign &&
      textBaseline == other.textBaseline &&
190 191 192 193 194 195 196 197 198 199 200 201
      decoration == other.decoration &&
      decorationColor == other.decorationColor &&
      decorationStyle == other.decorationStyle;
  }

  int get hashCode {
    // Use Quiver: https://github.com/domokit/mojo/issues/236
    int value = 373;
    value = 37 * value + color.hashCode;
    value = 37 * value + fontFamily.hashCode;
    value = 37 * value + fontSize.hashCode;
    value = 37 * value + fontWeight.hashCode;
202
    value = 37 * value + fontStyle.hashCode;
203
    value = 37 * value + textAlign.hashCode;
204
    value = 37 * value + textBaseline.hashCode;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    value = 37 * value + decoration.hashCode;
    value = 37 * value + decorationColor.hashCode;
    value = 37 * value + decorationStyle.hashCode;
    return value;
  }

  String toString([String prefix = '']) {
    List<String> result = [];
    if (color != null)
      result.add('${prefix}color: $color');
    // TODO(hansmuller): escape the fontFamily string.
    if (fontFamily != null)
      result.add('${prefix}fontFamily: "${fontFamily}"');
    if (fontSize != null)
      result.add('${prefix}fontSize: $fontSize');
    if (fontWeight != null)
      result.add('${prefix}fontWeight: $fontWeight');
222 223
    if (fontStyle != null)
      result.add('${prefix}fontStyle: $fontStyle');
224 225
    if (textAlign != null)
      result.add('${prefix}textAlign: $textAlign');
226 227
    if (textBaseline != null)
      result.add('${prefix}textBaseline: $textBaseline');
228 229 230 231 232 233 234 235 236 237 238
    if (decoration != null)
      result.add('${prefix}decoration: $decoration');
    if (decorationColor != null)
      result.add('${prefix}decorationColor: $decorationColor');
    if (decorationStyle != null)
      result.add('${prefix}decorationStyle: $decorationStyle');
    if (result.isEmpty)
      return '${prefix}<no style specified>';
    return result.join('\n');
  }
}