summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-12 01:52:21 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-12 01:52:21 +0000
commitf95b68396b06021b5aac4f7f1f4d15cb67b0414a (patch)
tree2fc535b5745e011024acb8478d9704521c0d4b39 /chrome
parent92fee7f85eda4a8cd3482d2ff9da51ade9a58b65 (diff)
downloadchromium_src-f95b68396b06021b5aac4f7f1f4d15cb67b0414a.zip
chromium_src-f95b68396b06021b5aac4f7f1f4d15cb67b0414a.tar.gz
chromium_src-f95b68396b06021b5aac4f7f1f4d15cb67b0414a.tar.bz2
...
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6870 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/common/gfx/chrome_font_skia.cc147
1 files changed, 147 insertions, 0 deletions
diff --git a/chrome/common/gfx/chrome_font_skia.cc b/chrome/common/gfx/chrome_font_skia.cc
new file mode 100644
index 0000000..17e5fdf
--- /dev/null
+++ b/chrome/common/gfx/chrome_font_skia.cc
@@ -0,0 +1,147 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/gfx/chrome_font.h"
+
+#include "base/logging.h"
+#include "base/sys_string_conversions.h"
+
+#include "skia/include/SkTypeface.h"
+#include "skia/include/SkPaint.h"
+
+ChromeFont::ChromeFont(const ChromeFont& other)
+ : typeface_helper_(other.typeface_),
+ typeface_(other.typeface_),
+ font_name_(other.font_name_),
+ font_size_(other.font_size_),
+ style_(other.style_),
+ height_(other.height_),
+ ascent_(other.ascent_),
+ avg_width_(other.avg_width_) {
+}
+
+ChromeFont::ChromeFont(SkTypeface* tf, const std::wstring& font_name,
+ int font_size, int style)
+ : typeface_helper_(tf),
+ typeface_(tf),
+ font_name_(font_name),
+ font_size_(font_size),
+ style_(style) {
+ calculateMetrics();
+}
+
+void ChromeFont::calculateMetrics() {
+ SkPaint paint;
+ SkPaint::FontMetrics metrics;
+
+ setupPaint(&paint);
+ paint.getFontMetrics(&metrics);
+
+ if (metrics.fVDMXMetricsValid) {
+ ascent_ = metrics.fVDMXAscent;
+ height_ = ascent_ + metrics.fVDMXDescent;
+ } else {
+ ascent_ = SkScalarRound(-metrics.fAscent);
+ height_ = SkScalarRound(metrics.fHeight);
+ }
+
+ if (metrics.fAvgCharWidth) {
+ avg_width_ = SkScalarRound(metrics.fAvgCharWidth);
+ } else {
+ static const char x_char = 'x';
+ paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
+ SkScalar width = paint.measureText(&x_char, 1);
+
+ avg_width_ = static_cast<int>(ceilf(SkScalarToFloat(width)));
+ }
+}
+
+int ChromeFont::height() const {
+ return height_;
+}
+
+int ChromeFont::baseline() const {
+ return ascent_;
+}
+
+int ChromeFont::ave_char_width() const {
+ return avg_width_;
+}
+
+ChromeFont ChromeFont::CreateFont(const std::wstring& font_name, int font_size) {
+ DCHECK_GT(font_size, 0);
+
+ SkTypeface* tf = SkTypeface::Create(base::SysWideToUTF8(font_name).c_str(),
+ SkTypeface::kNormal);
+ SkAutoUnref tf_helper(tf);
+
+ return ChromeFont(tf, font_name, font_size, NORMAL);
+}
+
+ChromeFont ChromeFont::DeriveFont(int size_delta, int style) const {
+ // If the delta is negative, if must not push the size below 1
+ if (size_delta < 0) {
+ DCHECK_LT(-size_delta, font_size_);
+ }
+
+ if (style == style_) {
+ // Fast path, we just use the same typeface at a different size
+ return ChromeFont(typeface_, font_name_, font_size_ + size_delta, style_);
+ }
+
+ // If the style has changed we may need to load a new face
+ int skstyle = SkTypeface::kNormal;
+ if (BOLD & style)
+ skstyle |= SkTypeface::kBold;
+ if (ITALIC & style)
+ skstyle |= SkTypeface::kItalic;
+
+ SkTypeface* tf = SkTypeface::Create(base::SysWideToUTF8(font_name_).c_str(),
+ static_cast<SkTypeface::Style>(skstyle));
+ SkAutoUnref tf_helper(tf);
+
+ return ChromeFont(tf, font_name_, font_size_ + size_delta, skstyle);
+}
+
+void ChromeFont::setupPaint(SkPaint* paint) const {
+ paint->setAntiAlias(false);
+ paint->setSubpixelText(false);
+ paint->setTextSize(SkFloatToScalar(font_size_));
+ paint->setTypeface(typeface_);
+ paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold());
+ paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ?
+ -SK_Scalar1/4 : 0);
+}
+
+int ChromeFont::GetStringWidth(const std::wstring& text) const {
+ const std::string utf8(base::SysWideToUTF8(text));
+
+ SkPaint paint;
+ setupPaint(&paint);
+ paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
+ SkScalar width = paint.measureText(utf8.data(), utf8.size());
+
+ return static_cast<int>(ceilf(SkScalarToFloat(width)));
+}
+
+int ChromeFont::GetExpectedTextWidth(int length) const {
+ return length * avg_width_;
+}
+
+
+int ChromeFont::style() const {
+ return style_;
+}
+
+std::wstring ChromeFont::FontName() {
+ return font_name_;
+}
+
+int ChromeFont::FontSize() {
+ return font_size_;
+}
+
+gfx::NativeFont ChromeFont::nativeFont() const {
+ return typeface_;
+}