OLD | NEW |
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 "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 calculateMetrics(); | 34 calculateMetrics(); |
35 } | 35 } |
36 | 36 |
37 void Font::calculateMetrics() { | 37 void Font::calculateMetrics() { |
38 SkPaint paint; | 38 SkPaint paint; |
39 SkPaint::FontMetrics metrics; | 39 SkPaint::FontMetrics metrics; |
40 | 40 |
41 PaintSetup(&paint); | 41 PaintSetup(&paint); |
42 paint.getFontMetrics(&metrics); | 42 paint.getFontMetrics(&metrics); |
43 | 43 |
44 ascent_ = SkScalarRound(-metrics.fAscent); | 44 // We need to know the number of whole pixels for these measurements. The |
45 height_ = SkScalarRound(-metrics.fAscent + metrics.fDescent + | 45 // height is the number of whole pixels above the baseline, plus the number |
46 metrics.fLeading); | 46 // of whole pixels below it. |
| 47 ascent_ = SkScalarCeil(-metrics.fAscent); |
| 48 height_ = ascent_ + |
| 49 SkScalarCeil(metrics.fDescent) + |
| 50 SkScalarCeil(metrics.fLeading); |
47 | 51 |
48 if (metrics.fAvgCharWidth) { | 52 if (metrics.fAvgCharWidth) { |
49 avg_width_ = SkScalarRound(metrics.fAvgCharWidth); | 53 avg_width_ = SkScalarRound(metrics.fAvgCharWidth); |
50 } else { | 54 } else { |
51 static const char x_char = 'x'; | 55 static const char x_char = 'x'; |
52 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); | 56 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
53 SkScalar width = paint.measureText(&x_char, 1); | 57 SkScalar width = paint.measureText(&x_char, 1); |
54 | 58 |
55 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width))); | 59 avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width))); |
56 } | 60 } |
| 61 |
| 62 // HACK ALERT! Skia and Pango don't agree about how to vertically size text. |
| 63 // Since we use Pango in gfx::Canvas, we need to use its version of the |
| 64 // height or various layouts will look weird. |
| 65 // |
| 66 // From debugging, it looks like Skia's notion of ascenders and descenders |
| 67 // are correct from the pixel rendering, but includes no leading or other |
| 68 // space around the text. Pango includes extra space (at least for the fonts |
| 69 // tested). To hack around this, we now ask Pango to get the height of a |
| 70 // string: we use "l" for the highest ascender, and "g" to get a descender. |
| 71 // |
| 72 // TODO(brettw/jhawkins): figure out how to get the real height that Pango |
| 73 // will use in a reasonable way. This is a horrible hack! |
| 74 int width, height; |
| 75 Canvas cvs(1, 1, true); |
| 76 cvs.SizeStringInt(L"lg", *this, &width, &height, 0); |
| 77 height_ = height; |
57 } | 78 } |
58 | 79 |
59 void Font::CopyFont(const Font& other) { | 80 void Font::CopyFont(const Font& other) { |
60 typeface_helper_.reset(new SkAutoUnref(other.typeface_)); | 81 typeface_helper_.reset(new SkAutoUnref(other.typeface_)); |
61 typeface_ = other.typeface_; | 82 typeface_ = other.typeface_; |
62 typeface_->ref(); | 83 typeface_->ref(); |
63 font_family_ = other.font_family_; | 84 font_family_ = other.font_family_; |
64 font_size_ = other.font_size_; | 85 font_size_ = other.font_size_; |
65 style_ = other.style_; | 86 style_ = other.style_; |
66 height_ = other.height_; | 87 height_ = other.height_; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 172 |
152 int Font::FontSize() { | 173 int Font::FontSize() { |
153 return font_size_; | 174 return font_size_; |
154 } | 175 } |
155 | 176 |
156 NativeFont Font::nativeFont() const { | 177 NativeFont Font::nativeFont() const { |
157 return typeface_; | 178 return typeface_; |
158 } | 179 } |
159 | 180 |
160 } // namespace gfx | 181 } // namespace gfx |
OLD | NEW |