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' as sky; | 5 import 'dart:sky' as sky; |
6 | 6 |
7 import 'box.dart'; | 7 import 'box.dart'; |
8 import 'object.dart'; | 8 import 'object.dart'; |
9 | 9 import '../painting/text_style.dart'; |
10 enum FontWeight { | |
11 light, // 300 | |
12 regular, // 400 | |
13 medium, // 500 | |
14 } | |
15 | |
16 enum TextAlign { | |
17 left, | |
18 right, | |
19 center | |
20 } | |
21 | |
22 class TextStyle { | |
23 const TextStyle({ | |
24 this.color, | |
25 this.fontSize, | |
26 this.fontWeight, | |
27 this.textAlign | |
28 }); | |
29 | |
30 final Color color; | |
31 final double fontSize; // in pixels | |
32 final FontWeight fontWeight; | |
33 final TextAlign textAlign; | |
34 | |
35 TextStyle copyWith({ | |
36 Color color, | |
37 double fontSize, | |
38 FontWeight fontWeight, | |
39 TextAlign textAlign | |
40 }) { | |
41 return new TextStyle( | |
42 color: color != null ? color : this.color, | |
43 fontSize: fontSize != null ? fontSize : this.fontSize, | |
44 fontWeight: fontWeight != null ? fontWeight : this.fontWeight, | |
45 textAlign: textAlign != null ? textAlign : this.textAlign | |
46 ); | |
47 } | |
48 | |
49 bool operator ==(other) { | |
50 return other is TextStyle && | |
51 color == other.color && | |
52 fontSize == other.fontSize && | |
53 fontWeight == other.fontWeight && | |
54 textAlign == other.textAlign; | |
55 } | |
56 | |
57 int get hashCode { | |
58 // Use Quiver: https://github.com/domokit/mojo/issues/236 | |
59 int value = 373; | |
60 value = 37 * value + color.hashCode; | |
61 value = 37 * value + fontSize.hashCode; | |
62 value = 37 * value + fontWeight.hashCode; | |
63 value = 37 * value + textAlign.hashCode; | |
64 return value; | |
65 } | |
66 | |
67 void _applyToCSSStyle(sky.CSSStyleDeclaration cssStyle) { | |
68 if (color != null) { | |
69 cssStyle['color'] = 'rgba(${color.red}, ${color.green}, ${color.blue}, ${c
olor.alpha / 255.0})'; | |
70 } | |
71 if (fontSize != null) { | |
72 cssStyle['font-size'] = "${fontSize}px"; | |
73 } | |
74 if (fontWeight != null) { | |
75 cssStyle['font-weight'] = const { | |
76 FontWeight.light: '300', | |
77 FontWeight.regular: '400', | |
78 FontWeight.medium: '500', | |
79 }[fontWeight]; | |
80 } | |
81 if (textAlign != null) { | |
82 cssStyle['text-align'] = const { | |
83 TextAlign.left: 'left', | |
84 TextAlign.right: 'right', | |
85 TextAlign.center: 'center', | |
86 }[textAlign]; | |
87 } | |
88 } | |
89 | |
90 String toString([String prefix = '']) { | |
91 List<String> result = []; | |
92 if (color != null) | |
93 result.add('${prefix}color: $color'); | |
94 if (fontSize != null) | |
95 result.add('${prefix}fontSize: $fontSize'); | |
96 if (fontWeight != null) | |
97 result.add('${prefix}fontWeight: $fontWeight'); | |
98 if (textAlign != null) | |
99 result.add('${prefix}textAlign: $textAlign'); | |
100 if (result.isEmpty) | |
101 return '${prefix}<no style specified>'; | |
102 return result.join('\n'); | |
103 } | |
104 } | |
105 | 10 |
106 abstract class InlineBase { | 11 abstract class InlineBase { |
107 sky.Node _toDOM(sky.Document owner); | 12 sky.Node _toDOM(sky.Document owner); |
108 String toString([String prefix = '']); | 13 String toString([String prefix = '']); |
109 } | 14 } |
110 | 15 |
111 class InlineText extends InlineBase { | 16 class InlineText extends InlineBase { |
112 InlineText(this.text) { | 17 InlineText(this.text) { |
113 assert(text != null); | 18 assert(text != null); |
114 } | 19 } |
(...skipping 11 matching lines...) Expand all Loading... |
126 InlineStyle(this.style, this.children) { | 31 InlineStyle(this.style, this.children) { |
127 assert(style != null); | 32 assert(style != null); |
128 assert(children != null); | 33 assert(children != null); |
129 } | 34 } |
130 | 35 |
131 final TextStyle style; | 36 final TextStyle style; |
132 final List<InlineBase> children; | 37 final List<InlineBase> children; |
133 | 38 |
134 sky.Node _toDOM(sky.Document owner) { | 39 sky.Node _toDOM(sky.Document owner) { |
135 sky.Element parent = owner.createElement('t'); | 40 sky.Element parent = owner.createElement('t'); |
136 style._applyToCSSStyle(parent.style); | 41 style.applyToCSSStyle(parent.style); |
137 for (InlineBase child in children) { | 42 for (InlineBase child in children) { |
138 parent.appendChild(child._toDOM(owner)); | 43 parent.appendChild(child._toDOM(owner)); |
139 } | 44 } |
140 return parent; | 45 return parent; |
141 } | 46 } |
142 | 47 |
143 String toString([String prefix = '']) { | 48 String toString([String prefix = '']) { |
144 List<String> result = []; | 49 List<String> result = []; |
145 result.add('${prefix}InlineStyle:'); | 50 result.add('${prefix}InlineStyle:'); |
146 var indent = '${prefix} '; | 51 var indent = '${prefix} '; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 146 } |
242 | 147 |
243 // we should probably expose a way to do precise (inter-glpyh) hit testing | 148 // we should probably expose a way to do precise (inter-glpyh) hit testing |
244 | 149 |
245 String debugDescribeSettings(String prefix) { | 150 String debugDescribeSettings(String prefix) { |
246 String result = '${super.debugDescribeSettings(prefix)}'; | 151 String result = '${super.debugDescribeSettings(prefix)}'; |
247 result += '${prefix}inline: ${inline}\n'; | 152 result += '${prefix}inline: ${inline}\n'; |
248 return result; | 153 return result; |
249 } | 154 } |
250 } | 155 } |
OLD | NEW |