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

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

Issue 1176133002: Add support for text styles - Work In Progress (Closed) Base URL: https://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/sdk/lib/framework/fn2.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 class TextStyle {
15 final Color color;
Hixie 2015/06/11 17:39:28 constructor then fields (see sky/specs/style-guide
16 final String fontSize;
Hixie 2015/06/11 17:39:27 fontSize should be a double (pixels).
17 final String fontWeight;
Hixie 2015/06/11 17:39:27 fontWeight should be an enum.
18
19 const TextStyle({
20 this.color,
21 this.fontSize,
22 this.fontWeight
23 });
24
25 bool operator ==(other) => other is TextStyle &&
Hixie 2015/06/11 17:39:28 If you have an operator ==, you need a hashCode fu
26 color == other.color &&
27 fontSize == other.fontSize &&
28 fontWeight == other.fontWeight;
29 }
30
14 // Unfortunately, using full precision floating point here causes bad layouts 31 // Unfortunately, using full precision floating point here causes bad layouts
15 // because floating point math isn't associative. If we add and subtract 32 // 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 33 // 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 34 // 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 35 // 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 36 // values up to the nearest whole pixel value. The right long-term fix is to do
20 // layout using fixed precision arithmetic. 37 // layout using fixed precision arithmetic.
21 double _applyFloatingPointHack(double layoutValue) { 38 double _applyFloatingPointHack(double layoutValue) {
22 return layoutValue.ceilToDouble(); 39 return layoutValue.ceilToDouble();
23 } 40 }
24 41
25 class RenderParagraph extends RenderBox { 42 class RenderParagraph extends RenderBox {
26 43
27 RenderParagraph({ 44 RenderParagraph({
28 String text, 45 String text,
29 Color color 46 Color color,
30 }) : _color = color { 47 TextStyle style
48 }) : _color = color, _style = style {
31 _layoutRoot.rootElement = _document.createElement('p'); 49 _layoutRoot.rootElement = _document.createElement('p');
32 this.text = text; 50 this.text = text;
33 } 51 }
34 52
35 final sky.Document _document = new sky.Document(); 53 final sky.Document _document = new sky.Document();
36 final sky.LayoutRoot _layoutRoot = new sky.LayoutRoot(); 54 final sky.LayoutRoot _layoutRoot = new sky.LayoutRoot();
37 55
38 String get text => (_layoutRoot.rootElement.firstChild as sky.Text).data; 56 String get text => (_layoutRoot.rootElement.firstChild as sky.Text).data;
39 void set text (String value) { 57 void set text (String value) {
40 _layoutRoot.rootElement.setChild(_document.createText(value)); 58 _layoutRoot.rootElement.setChild(_document.createText(value));
41 markNeedsLayout(); 59 markNeedsLayout();
42 } 60 }
43 61
62 // TODO(hansmuller): remove this
44 Color _color = const Color(0xFF000000); 63 Color _color = const Color(0xFF000000);
45 Color get color => _color; 64 Color get color => _color;
46 void set color (Color value) { 65 void set color (Color value) {
47 if (_color != value) { 66 if (_color != value) {
48 _color = value; 67 _color = value;
49 markNeedsPaint(); 68 markNeedsPaint();
50 } 69 }
51 } 70 }
52 71
72 TextStyle _style;
73 TextStyle get style => _style;
74 void set style (TextStyle value) {
75 if (_style != value) {
76 _style = value;
77 // TODO(hansmuller): decide if a new layout or paint is needed
78 markNeedsLayout();
79 }
80 }
81
53 BoxConstraints _constraintsForCurrentLayout; 82 BoxConstraints _constraintsForCurrentLayout;
54 83
55 sky.Element _layout(BoxConstraints constraints) { 84 sky.Element _layout(BoxConstraints constraints) {
56 _layoutRoot.maxWidth = constraints.maxWidth; 85 _layoutRoot.maxWidth = constraints.maxWidth;
57 _layoutRoot.minWidth = constraints.minWidth; 86 _layoutRoot.minWidth = constraints.minWidth;
58 _layoutRoot.minHeight = constraints.minHeight; 87 _layoutRoot.minHeight = constraints.minHeight;
59 _layoutRoot.maxHeight = constraints.maxHeight; 88 _layoutRoot.maxHeight = constraints.maxHeight;
60 _layoutRoot.layout(); 89 _layoutRoot.layout();
61 _constraintsForCurrentLayout = constraints; 90 _constraintsForCurrentLayout = constraints;
62 } 91 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // Ideally we could compute the min/max intrinsic width/height with a 128 // Ideally we could compute the min/max intrinsic width/height with a
100 // non-destructive operation. However, currently, computing these values 129 // non-destructive operation. However, currently, computing these values
101 // will destroy state inside the layout root. If that happens, we need to 130 // will destroy state inside the layout root. If that happens, we need to
102 // get back the correct state by calling _layout again. 131 // get back the correct state by calling _layout again.
103 // 132 //
104 // TODO(abarth): Make computing the min/max intrinsic width/height a 133 // TODO(abarth): Make computing the min/max intrinsic width/height a
105 // non-destructive operation. 134 // non-destructive operation.
106 if (_constraintsForCurrentLayout != constraints && constraints != null) 135 if (_constraintsForCurrentLayout != constraints && constraints != null)
107 _layout(constraints); 136 _layout(constraints);
108 137
109 if (_color != null) { 138 if (style != null && style.color != null) {
139 Color color = style.color;
110 _layoutRoot.rootElement.style['color'] = 140 _layoutRoot.rootElement.style['color'] =
111 'rgba(${_color.red}, ${_color.green}, ${_color.blue}, ${_color.alpha / 255.0 })'; 141 'rgba(${color.red}, ${color.green}, ${color.blue}, ${color.alpha / 255 .0 })';
112 } 142 }
113 _layoutRoot.paint(canvas); 143 _layoutRoot.paint(canvas);
114 } 144 }
115 145
116 // we should probably expose a way to do precise (inter-glpyh) hit testing 146 // we should probably expose a way to do precise (inter-glpyh) hit testing
117 147
118 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}color: ${color}\n${prefix}text: ${text}\n'; 148 String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings( prefix)}${prefix}color: ${color}\n${prefix}text: ${text}\n';
119 } 149 }
OLDNEW
« no previous file with comments | « sky/sdk/lib/framework/fn2.dart ('k') | sky/sdk/lib/framework/theme2/typography.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698