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

Side by Side Diff: sky/sdk/lib/framework/rendering/paragraph.dart

Issue 1178913003: Add support for text styles. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: 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/examples/stocks2/lib/stock_row.dart ('k') | sky/sdk/lib/framework/theme2/typography.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'box.dart'; 6 import 'box.dart';
7 import 'object.dart'; 7 import 'object.dart';
8 8
9 class RenderInline extends RenderObject { 9 class RenderInline extends RenderObject {
10 RenderInline(this.data); 10 RenderInline(this.data);
11 String data; 11 String data;
12 } 12 }
13 13
14 enum FontWeight {
15 light, // 300
16 regular, // 400
17 medium, // 500
18 }
19
20 int trueTypeWeightNumber(FontWeight weight) {
jackson 2015/06/12 01:32:50 This global function might make sense as a getter
21 switch(weight) {
22 case FontWeight.light:
23 return 300;
24 case FontWeight.regular:
25 return 400;
26 case FontWeight.medium:
27 return 500;
28 }
29 }
30
31 class TextStyle {
32 final Color color;
33 final String fontSize;
34 final FontWeight fontWeight;
35
36 const TextStyle({
37 this.color,
38 this.fontSize,
39 this.fontWeight
40 });
41
42 bool operator ==(other) => other is TextStyle &&
43 color == other.color &&
44 fontSize == other.fontSize &&
45 fontWeight == other.fontWeight;
46 }
47
14 // Unfortunately, using full precision floating point here causes bad layouts 48 // Unfortunately, using full precision floating point here causes bad layouts
15 // because floating point math isn't associative. If we add and subtract 49 // because floating point math isn't associative. If we add and subtract
16 // padding, for example, we'll get different values when we estimate sizes and 50 // padding, for example, we'll get different values when we estimate sizes and
17 // when we actually compute layout because the operations will end up associated 51 // when we actually compute layout because the operations will end up associated
18 // differently. To work around this problem for now, we round fractional pixel 52 // differently. To work around this problem for now, we round fractional pixel
19 // values up to the nearest whole pixel value. The right long-term fix is to do 53 // values up to the nearest whole pixel value. The right long-term fix is to do
20 // layout using fixed precision arithmetic. 54 // layout using fixed precision arithmetic.
21 double _applyFloatingPointHack(double layoutValue) { 55 double _applyFloatingPointHack(double layoutValue) {
22 return layoutValue.ceilToDouble(); 56 return layoutValue.ceilToDouble();
23 } 57 }
24 58
25 class RenderParagraph extends RenderBox { 59 class RenderParagraph extends RenderBox {
26 60
27 RenderParagraph({ 61 RenderParagraph({
28 String text, 62 String text,
29 Color color 63 Color color,
30 }) : _color = color { 64 TextStyle style
65 }) : _style = style {
31 _layoutRoot.rootElement = _document.createElement('p'); 66 _layoutRoot.rootElement = _document.createElement('p');
32 this.text = text; 67 this.text = text;
33 } 68 }
34 69
35 final sky.Document _document = new sky.Document(); 70 final sky.Document _document = new sky.Document();
36 final sky.LayoutRoot _layoutRoot = new sky.LayoutRoot(); 71 final sky.LayoutRoot _layoutRoot = new sky.LayoutRoot();
37 72
38 String get text => (_layoutRoot.rootElement.firstChild as sky.Text).data; 73 String get text => (_layoutRoot.rootElement.firstChild as sky.Text).data;
39 void set text (String value) { 74 void set text (String value) {
40 _layoutRoot.rootElement.setChild(_document.createText(value)); 75 _layoutRoot.rootElement.setChild(_document.createText(value));
41 markNeedsLayout(); 76 markNeedsLayout();
42 } 77 }
43 78
44 Color _color = const Color(0xFF000000); 79 TextStyle _style;
45 Color get color => _color; 80 TextStyle get style => _style;
46 void set color (Color value) { 81 void set style (TextStyle value) {
47 if (_color != value) { 82 if (_style != value) {
48 _color = value; 83 // TODO(hansmuller): decide if a new layout or paint is needed
49 markNeedsPaint(); 84 markNeedsLayout();
85 _style = value;
50 } 86 }
51 } 87 }
52 88
53 BoxConstraints _constraintsForCurrentLayout; 89 BoxConstraints _constraintsForCurrentLayout;
54 90
55 sky.Element _layout(BoxConstraints constraints) { 91 sky.Element _layout(BoxConstraints constraints) {
56 _layoutRoot.maxWidth = constraints.maxWidth; 92 _layoutRoot.maxWidth = constraints.maxWidth;
57 _layoutRoot.minWidth = constraints.minWidth; 93 _layoutRoot.minWidth = constraints.minWidth;
58 _layoutRoot.minHeight = constraints.minHeight; 94 _layoutRoot.minHeight = constraints.minHeight;
59 _layoutRoot.maxHeight = constraints.maxHeight; 95 _layoutRoot.maxHeight = constraints.maxHeight;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // Ideally we could compute the min/max intrinsic width/height with a 135 // Ideally we could compute the min/max intrinsic width/height with a
100 // non-destructive operation. However, currently, computing these values 136 // non-destructive operation. However, currently, computing these values
101 // will destroy state inside the layout root. If that happens, we need to 137 // will destroy state inside the layout root. If that happens, we need to
102 // get back the correct state by calling _layout again. 138 // get back the correct state by calling _layout again.
103 // 139 //
104 // TODO(abarth): Make computing the min/max intrinsic width/height a 140 // TODO(abarth): Make computing the min/max intrinsic width/height a
105 // non-destructive operation. 141 // non-destructive operation.
106 if (_constraintsForCurrentLayout != constraints && constraints != null) 142 if (_constraintsForCurrentLayout != constraints && constraints != null)
107 _layout(constraints); 143 _layout(constraints);
108 144
109 if (_color != null) { 145 if (style != null) {
110 _layoutRoot.rootElement.style['color'] = 146 var cssStyle = _layoutRoot.rootElement.style;
111 'rgba(${_color.red}, ${_color.green}, ${_color.blue}, ${_color.alpha / 255.0 })'; 147 if (style.color != null) {
148 Color c = style.color;
149 cssStyle['color'] =
150 'rgba(${c.red}, ${c.green}, ${c.blue}, ${c.alpha / 255.0 })';
jackson 2015/06/12 01:32:50 you've got an extra space here
151 }
152 if (style.fontSize != null) {
153 cssStyle['font-size'] = style.fontSize;
154 }
155 if (style.fontWeight != null) {
156 cssStyle['font-weight'] = trueTypeWeightNumber(style.fontWeight).toStrin g();
157 }
112 } 158 }
113 _layoutRoot.paint(canvas); 159 _layoutRoot.paint(canvas);
114 } 160 }
115 161
116 // we should probably expose a way to do precise (inter-glpyh) hit testing 162 // we should probably expose a way to do precise (inter-glpyh) hit testing
117 163
118 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}color: ${color}\n${prefix}text: ${text}\n'; 164 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}color: ${color}\n${prefix}text: ${text}\n';
119 } 165 }
OLDNEW
« no previous file with comments | « sky/examples/stocks2/lib/stock_row.dart ('k') | sky/sdk/lib/framework/theme2/typography.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698