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

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

Issue 251054: Attempt 4 at fixing font size on linux. I think I finally got... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | app/gfx/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/font.h" 5 #include "app/gfx/font.h"
6 6
7 #include <gdk/gdk.h>
8 #include <pango/pango.h>
9
7 #include "app/gfx/canvas.h" 10 #include "app/gfx/canvas.h"
8 #include "base/logging.h" 11 #include "base/logging.h"
9 #include "base/string_piece.h" 12 #include "base/string_piece.h"
10 #include "base/sys_string_conversions.h" 13 #include "base/sys_string_conversions.h"
11 #include "third_party/skia/include/core/SkTypeface.h" 14 #include "third_party/skia/include/core/SkTypeface.h"
12 #include "third_party/skia/include/core/SkPaint.h" 15 #include "third_party/skia/include/core/SkPaint.h"
13 16
14 namespace { 17 namespace {
15 18
16 // The font family name which is used when a user's application font for 19 // The font family name which is used when a user's application font for
17 // GNOME/KDE is a non-scalable one. The name should be listed in the 20 // GNOME/KDE is a non-scalable one. The name should be listed in the
18 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. 21 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp.
19 const char* kFallbackFontFamilyName = "sans"; 22 const char* kFallbackFontFamilyName = "sans";
20 23
24 // Pango scales font sizes. This returns the scale factor. See
25 // pango_cairo_context_set_resolution for details.
26 // NOTE: this isn't entirely accurate, in that Pango also consults the
27 // FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this
28 // seems to give us the same sizes as used by Pango for all our fonts in both
29 // English and Thia.
30 static double GetPangoScaleFactor() {
31 static float scale_factor = 0;
32 static bool determined_scale = false;
33 if (!determined_scale) {
34 PangoContext* context = gdk_pango_context_get();
35 scale_factor = pango_cairo_context_get_resolution(context);
36 g_object_unref(context);
37 if (scale_factor <= 0)
38 scale_factor = 1;
39 else
40 scale_factor /= 72.0;
41 }
42 return scale_factor;
43 }
44
21 } // namespace 45 } // namespace
22 46
23 namespace gfx { 47 namespace gfx {
24 48
25 Font::Font(const Font& other) { 49 Font::Font(const Font& other) {
26 CopyFont(other); 50 CopyFont(other);
27 } 51 }
28 52
29 Font& Font::operator=(const Font& other) { 53 Font& Font::operator=(const Font& other) {
30 CopyFont(other); 54 CopyFont(other);
(...skipping 11 matching lines...) Expand all
42 calculateMetrics(); 66 calculateMetrics();
43 } 67 }
44 68
45 void Font::calculateMetrics() { 69 void Font::calculateMetrics() {
46 SkPaint paint; 70 SkPaint paint;
47 SkPaint::FontMetrics metrics; 71 SkPaint::FontMetrics metrics;
48 72
49 PaintSetup(&paint); 73 PaintSetup(&paint);
50 paint.getFontMetrics(&metrics); 74 paint.getFontMetrics(&metrics);
51 75
52 // NOTE: we don't use the ascent/descent as it doesn't match with how pango 76 ascent_ = SkScalarCeil(-metrics.fAscent);
53 // ends up drawing the text, in particular if we clip to the ascent/descent 77 height_ = ascent_ + SkScalarCeil(metrics.fDescent);
54 // the text is clipped. This algorithm doesn't give us an exact match with
55 // the numbers returned from pango (we are off by 1 in some cases), but it
56 // is close enough that you won't notice clipping.
57 //
58 // NOTE2: I tried converting this to use Pango exclusively for measuring the
59 // text but it causes a startup regression. The best I could get it was
60 // ~10% slow down. Slow down appeared to be entirely in libfontconfig.
61 ascent_ = SkScalarCeil(-metrics.fTop);
62 height_ = SkScalarCeil(-metrics.fTop) + SkScalarCeil(metrics.fBottom) +
63 SkScalarCeil(metrics.fLeading);
64 78
65 if (metrics.fAvgCharWidth) { 79 if (metrics.fAvgCharWidth) {
66 avg_width_ = SkScalarRound(metrics.fAvgCharWidth); 80 avg_width_ = SkScalarRound(metrics.fAvgCharWidth);
67 } else { 81 } else {
68 static const char x_char = 'x'; 82 static const char x_char = 'x';
69 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); 83 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
70 SkScalar width = paint.measureText(&x_char, 1); 84 SkScalar width = paint.measureText(&x_char, 1);
71 85
72 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width))); 86 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width)));
73 } 87 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 base::SysWideToUTF8(font_family_).c_str(), 155 base::SysWideToUTF8(font_family_).c_str(),
142 static_cast<SkTypeface::Style>(skstyle)); 156 static_cast<SkTypeface::Style>(skstyle));
143 SkAutoUnref tf_helper(tf); 157 SkAutoUnref tf_helper(tf);
144 158
145 return Font(tf, font_family_, font_size_ + size_delta, skstyle); 159 return Font(tf, font_family_, font_size_ + size_delta, skstyle);
146 } 160 }
147 161
148 void Font::PaintSetup(SkPaint* paint) const { 162 void Font::PaintSetup(SkPaint* paint) const {
149 paint->setAntiAlias(false); 163 paint->setAntiAlias(false);
150 paint->setSubpixelText(false); 164 paint->setSubpixelText(false);
151 paint->setTextSize(SkFloatToScalar(font_size_)); 165 paint->setTextSize(SkFloatToScalar(font_size_ * GetPangoScaleFactor()));
152 paint->setTypeface(typeface_); 166 paint->setTypeface(typeface_);
153 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold()); 167 paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold());
154 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ? 168 paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ?
155 -SK_Scalar1/4 : 0); 169 -SK_Scalar1/4 : 0);
156 } 170 }
157 171
158 int Font::GetStringWidth(const std::wstring& text) const { 172 int Font::GetStringWidth(const std::wstring& text) const {
159 int width = 0, height = 0; 173 int width = 0, height = 0;
160 174
161 Canvas::SizeStringInt(text, *this, &width, &height, 0); 175 Canvas::SizeStringInt(text, *this, &width, &height, 0);
(...skipping 15 matching lines...) Expand all
177 191
178 int Font::FontSize() { 192 int Font::FontSize() {
179 return font_size_; 193 return font_size_;
180 } 194 }
181 195
182 NativeFont Font::nativeFont() const { 196 NativeFont Font::nativeFont() const {
183 return typeface_; 197 return typeface_;
184 } 198 }
185 199
186 } // namespace gfx 200 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | app/gfx/font_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698