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