Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sky/sdk/lib/painting/text_style.dart

Issue 1173323004: Add TextStyle fontFamily:, extend support for fontWeight: (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Changes per review feedback Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/sdk/BUILD.gn ('k') | sky/sdk/lib/rendering/paragraph.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « sky/sdk/BUILD.gn ('k') | sky/sdk/lib/rendering/paragraph.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698