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

Side by Side Diff: app/gfx/chrome_font_skia.cc

Issue 113441: ChromeFont->gfx::Font... (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: Created 11 years, 7 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 | « app/gfx/chrome_font_mac.mm ('k') | app/gfx/chrome_font_unittest.cc » ('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 (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #include "app/gfx/chrome_font.h" 5 #include "app/gfx/chrome_font.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/sys_string_conversions.h" 8 #include "base/sys_string_conversions.h"
9 9
10 #include "third_party/skia/include/core/SkTypeface.h" 10 #include "third_party/skia/include/core/SkTypeface.h"
11 #include "third_party/skia/include/core/SkPaint.h" 11 #include "third_party/skia/include/core/SkPaint.h"
12 12
13 ChromeFont::ChromeFont(const ChromeFont& other) { 13 namespace gfx {
14 CopyChromeFont(other); 14
15 Font::Font(const Font& other) {
16 CopyFont(other);
15 } 17 }
16 18
17 ChromeFont& ChromeFont::operator=(const ChromeFont& other) { 19 Font& Font::operator=(const Font& other) {
18 CopyChromeFont(other); 20 CopyFont(other);
19 return *this; 21 return *this;
20 } 22 }
21 23
22 ChromeFont::ChromeFont(SkTypeface* tf, const std::wstring& font_family, 24 Font::Font(SkTypeface* tf, const std::wstring& font_family, int font_size,
23 int font_size, int style) 25 int style)
24 : typeface_helper_(new SkAutoUnref(tf)), 26 : typeface_helper_(new SkAutoUnref(tf)),
25 typeface_(tf), 27 typeface_(tf),
26 font_family_(font_family), 28 font_family_(font_family),
27 font_size_(font_size), 29 font_size_(font_size),
28 style_(style) { 30 style_(style) {
29 tf->ref(); 31 tf->ref();
30 calculateMetrics(); 32 calculateMetrics();
31 } 33 }
32 34
33 void ChromeFont::calculateMetrics() { 35 void Font::calculateMetrics() {
34 SkPaint paint; 36 SkPaint paint;
35 SkPaint::FontMetrics metrics; 37 SkPaint::FontMetrics metrics;
36 38
37 PaintSetup(&paint); 39 PaintSetup(&paint);
38 paint.getFontMetrics(&metrics); 40 paint.getFontMetrics(&metrics);
39 41
40 ascent_ = SkScalarRound(-metrics.fAscent); 42 ascent_ = SkScalarRound(-metrics.fAscent);
41 height_ = SkScalarRound(-metrics.fAscent + metrics.fDescent + 43 height_ = SkScalarRound(-metrics.fAscent + metrics.fDescent +
42 metrics.fLeading); 44 metrics.fLeading);
43 45
44 if (metrics.fAvgCharWidth) { 46 if (metrics.fAvgCharWidth) {
45 avg_width_ = SkScalarRound(metrics.fAvgCharWidth); 47 avg_width_ = SkScalarRound(metrics.fAvgCharWidth);
46 } else { 48 } else {
47 static const char x_char = 'x'; 49 static const char x_char = 'x';
48 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); 50 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
49 SkScalar width = paint.measureText(&x_char, 1); 51 SkScalar width = paint.measureText(&x_char, 1);
50 52
51 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width))); 53 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width)));
52 } 54 }
53 } 55 }
54 56
55 void ChromeFont::CopyChromeFont(const ChromeFont& other) { 57 void Font::CopyFont(const Font& other) {
56 typeface_helper_.reset(new SkAutoUnref(other.typeface_)); 58 typeface_helper_.reset(new SkAutoUnref(other.typeface_));
57 typeface_ = other.typeface_; 59 typeface_ = other.typeface_;
58 typeface_->ref(); 60 typeface_->ref();
59 font_family_ = other.font_family_; 61 font_family_ = other.font_family_;
60 font_size_ = other.font_size_; 62 font_size_ = other.font_size_;
61 style_ = other.style_; 63 style_ = other.style_;
62 height_ = other.height_; 64 height_ = other.height_;
63 ascent_ = other.ascent_; 65 ascent_ = other.ascent_;
64 avg_width_ = other.avg_width_; 66 avg_width_ = other.avg_width_;
65 } 67 }
66 68
67 int ChromeFont::height() const { 69 int Font::height() const {
68 return height_; 70 return height_;
69 } 71 }
70 72
71 int ChromeFont::baseline() const { 73 int Font::baseline() const {
72 return ascent_; 74 return ascent_;
73 } 75 }
74 76
75 int ChromeFont::ave_char_width() const { 77 int Font::ave_char_width() const {
76 return avg_width_; 78 return avg_width_;
77 } 79 }
78 80
79 ChromeFont ChromeFont::CreateFont(const std::wstring& font_family, 81 Font Font::CreateFont(const std::wstring& font_family, int font_size) {
80 int font_size) {
81 DCHECK_GT(font_size, 0); 82 DCHECK_GT(font_size, 0);
82 83
83 SkTypeface* tf = SkTypeface::CreateFromName( 84 SkTypeface* tf = SkTypeface::CreateFromName(
84 base::SysWideToUTF8(font_family).c_str(), SkTypeface::kNormal); 85 base::SysWideToUTF8(font_family).c_str(), SkTypeface::kNormal);
85 DCHECK(tf) << "Could not find font: " << base::SysWideToUTF8(font_family); 86 DCHECK(tf) << "Could not find font: " << base::SysWideToUTF8(font_family);
86 SkAutoUnref tf_helper(tf); 87 SkAutoUnref tf_helper(tf);
87 88
88 return ChromeFont(tf, font_family, font_size, NORMAL); 89 return Font(tf, font_family, font_size, NORMAL);
89 } 90 }
90 91
91 ChromeFont ChromeFont::DeriveFont(int size_delta, int style) const { 92 Font Font::DeriveFont(int size_delta, int style) const {
92 // If the delta is negative, if must not push the size below 1 93 // If the delta is negative, if must not push the size below 1
93 if (size_delta < 0) { 94 if (size_delta < 0) {
94 DCHECK_LT(-size_delta, font_size_); 95 DCHECK_LT(-size_delta, font_size_);
95 } 96 }
96 97
97 if (style == style_) { 98 if (style == style_) {
98 // Fast path, we just use the same typeface at a different size 99 // Fast path, we just use the same typeface at a different size
99 return ChromeFont(typeface_, font_family_, font_size_ + size_delta, style_); 100 return Font(typeface_, font_family_, font_size_ + size_delta, style_);
100 } 101 }
101 102
102 // If the style has changed we may need to load a new face 103 // If the style has changed we may need to load a new face
103 int skstyle = SkTypeface::kNormal; 104 int skstyle = SkTypeface::kNormal;
104 if (BOLD & style) 105 if (BOLD & style)
105 skstyle |= SkTypeface::kBold; 106 skstyle |= SkTypeface::kBold;
106 if (ITALIC & style) 107 if (ITALIC & style)
107 skstyle |= SkTypeface::kItalic; 108 skstyle |= SkTypeface::kItalic;
108 109
109 SkTypeface* tf = SkTypeface::CreateFromName( 110 SkTypeface* tf = SkTypeface::CreateFromName(
110 base::SysWideToUTF8(font_family_).c_str(), 111 base::SysWideToUTF8(font_family_).c_str(),
111 static_cast<SkTypeface::Style>(skstyle)); 112 static_cast<SkTypeface::Style>(skstyle));
112 SkAutoUnref tf_helper(tf); 113 SkAutoUnref tf_helper(tf);
113 114
114 return ChromeFont(tf, font_family_, font_size_ + size_delta, skstyle); 115 return Font(tf, font_family_, font_size_ + size_delta, skstyle);
115 } 116 }
116 117
117 void ChromeFont::PaintSetup(SkPaint* paint) const { 118 void Font::PaintSetup(SkPaint* paint) const {
118 paint->setAntiAlias(false); 119 paint->setAntiAlias(false);
119 paint->setSubpixelText(false); 120 paint->setSubpixelText(false);
120 paint->setTextSize(SkFloatToScalar(font_size_)); 121 paint->setTextSize(SkFloatToScalar(font_size_));
121 paint->setTypeface(typeface_); 122 paint->setTypeface(typeface_);
122 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold()); 123 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold());
123 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ? 124 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ?
124 -SK_Scalar1/4 : 0); 125 -SK_Scalar1/4 : 0);
125 } 126 }
126 127
127 int ChromeFont::GetStringWidth(const std::wstring& text) const { 128 int Font::GetStringWidth(const std::wstring& text) const {
128 const std::string utf8(base::SysWideToUTF8(text)); 129 const std::string utf8(base::SysWideToUTF8(text));
129 130
130 SkPaint paint; 131 SkPaint paint;
131 PaintSetup(&paint); 132 PaintSetup(&paint);
132 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); 133 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
133 SkScalar width = paint.measureText(utf8.data(), utf8.size()); 134 SkScalar width = paint.measureText(utf8.data(), utf8.size());
134 135
135 int breadth = static_cast<int>(ceilf(SkScalarToFloat(width))); 136 int breadth = static_cast<int>(ceilf(SkScalarToFloat(width)));
136 // Check for overflow. We should probably be returning an unsigned 137 // Check for overflow. We should probably be returning an unsigned
137 // int, but in practice we'll never have a screen massive enough 138 // int, but in practice we'll never have a screen massive enough
138 // to show that much text anyway. 139 // to show that much text anyway.
139 if (breadth < 0) 140 if (breadth < 0)
140 return INT_MAX; 141 return INT_MAX;
141 142
142 return breadth; 143 return breadth;
143 } 144 }
144 145
145 int ChromeFont::GetExpectedTextWidth(int length) const { 146 int Font::GetExpectedTextWidth(int length) const {
146 return length * avg_width_; 147 return length * avg_width_;
147 } 148 }
148 149
149 150
150 int ChromeFont::style() const { 151 int Font::style() const {
151 return style_; 152 return style_;
152 } 153 }
153 154
154 std::wstring ChromeFont::FontName() { 155 std::wstring Font::FontName() {
155 return font_family_; 156 return font_family_;
156 } 157 }
157 158
158 int ChromeFont::FontSize() { 159 int Font::FontSize() {
159 return font_size_; 160 return font_size_;
160 } 161 }
161 162
162 NativeFont ChromeFont::nativeFont() const { 163 NativeFont Font::nativeFont() const {
163 return typeface_; 164 return typeface_;
164 } 165 }
166
167 } // namespace gfx
OLDNEW
« no previous file with comments | « app/gfx/chrome_font_mac.mm ('k') | app/gfx/chrome_font_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698