diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-09 22:55:54 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-09 22:55:54 +0000 |
commit | f0b8a7327937c06e9c494424fd87780adc3456e1 (patch) | |
tree | 8b4151358e0201757b74e66378b2dbe98726d139 /chrome/common | |
parent | ee5c29daf67e999166394e1acb0f89b755b70aaf (diff) | |
download | chromium_src-f0b8a7327937c06e9c494424fd87780adc3456e1.zip chromium_src-f0b8a7327937c06e9c494424fd87780adc3456e1.tar.gz chromium_src-f0b8a7327937c06e9c494424fd87780adc3456e1.tar.bz2 |
Implement default ChromeFont constructor on Linux.
Review URL: http://codereview.chromium.org/16590
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7842 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/common.scons | 1 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_font.h | 19 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_font_gtk.cc | 31 | ||||
-rw-r--r-- | chrome/common/gfx/chrome_font_skia.cc | 24 |
4 files changed, 58 insertions, 17 deletions
diff --git a/chrome/common/common.scons b/chrome/common/common.scons index 05f6975..7f06408 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -120,6 +120,7 @@ if env.Bit('windows'): elif env.Bit('linux'): input_files.extend([ 'gfx/chrome_canvas_skia.cc', + 'gfx/chrome_font_gtk.cc', 'gfx/chrome_font_skia.cc', ]) diff --git a/chrome/common/gfx/chrome_font.h b/chrome/common/gfx/chrome_font.h index d495584..d4cc49f 100644 --- a/chrome/common/gfx/chrome_font.h +++ b/chrome/common/gfx/chrome_font.h @@ -30,6 +30,7 @@ typedef SkTypeface* NativeFont; #include "base/basictypes.h" #include "base/ref_counted.h" +#include "base/scoped_ptr.h" // ChromeFont provides a wrapper around an underlying font. Copy and assignment // operators are explicitly allowed, and cheap. @@ -93,10 +94,10 @@ class ChromeFont { NativeFont nativeFont() const; -#if defined(OS_WIN) // Creates a font with the default name and style. ChromeFont(); +#if defined(OS_WIN) // Creates a ChromeFont from the specified HFONT. The supplied HFONT is // effectively copied. static ChromeFont CreateFont(HFONT hfont); @@ -165,7 +166,6 @@ class ChromeFont { DISALLOW_COPY_AND_ASSIGN(HFontRef); }; - // Returns the base font ref. This should ONLY be invoked on the // UI thread. static HFontRef* GetBaseFontRef(); @@ -183,19 +183,24 @@ class ChromeFont { #elif defined(OS_LINUX) explicit ChromeFont(SkTypeface* typeface, const std::wstring& name, int size, int style); - // Calculate and cache the font metrics + // Calculate and cache the font metrics. void calculateMetrics(); + // Make |this| a copy of |other|. + void CopyChromeFont(const ChromeFont& other); + + // The default font, used for the default constructor. + static ChromeFont* default_font_; // These two both point to the same SkTypeface. We use the SkAutoUnref to // handle the reference counting, but without @typeface_ we would have to // cast the SkRefCnt from @typeface_helper_ every time. - SkAutoUnref typeface_helper_; + scoped_ptr<SkAutoUnref> typeface_helper_; SkTypeface *typeface_; // Additional information about the face - const std::wstring font_name_; - const int font_size_; - const int style_; + std::wstring font_name_; + int font_size_; + int style_; // Cached metrics, generated at construction int height_; diff --git a/chrome/common/gfx/chrome_font_gtk.cc b/chrome/common/gfx/chrome_font_gtk.cc new file mode 100644 index 0000000..48f48a6 --- /dev/null +++ b/chrome/common/gfx/chrome_font_gtk.cc @@ -0,0 +1,31 @@ +// 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 <gtk/gtk.h> + +#include "base/string_util.h" + +// Get the default gtk system font (name and size). +// TODO(estade): is there a way to do this that does not involve making a +// temporary widget? +ChromeFont::ChromeFont() { + if (default_font_ == NULL) { + GtkWidget* widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); + PangoFontDescription* desc = pango_context_get_font_description( + gtk_widget_get_pango_context(widget)); + gint size = pango_font_description_get_size(desc); + const char* name = pango_font_description_get_family(desc); + + default_font_ = new ChromeFont(CreateFont(UTF8ToWide(name), size)); + + gtk_widget_destroy(widget); + + DCHECK(default_font_); + } + + CopyChromeFont(*default_font_); +} + diff --git a/chrome/common/gfx/chrome_font_skia.cc b/chrome/common/gfx/chrome_font_skia.cc index 77a9661..728893c 100644 --- a/chrome/common/gfx/chrome_font_skia.cc +++ b/chrome/common/gfx/chrome_font_skia.cc @@ -10,20 +10,13 @@ #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(const ChromeFont& other) { + CopyChromeFont(other); } ChromeFont::ChromeFont(SkTypeface* tf, const std::wstring& font_name, int font_size, int style) - : typeface_helper_(tf), + : typeface_helper_(new SkAutoUnref(tf)), typeface_(tf), font_name_(font_name), font_size_(font_size), @@ -57,6 +50,17 @@ void ChromeFont::calculateMetrics() { } } +void ChromeFont::CopyChromeFont(const ChromeFont& other) { + typeface_helper_.reset(new SkAutoUnref(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_; +} + int ChromeFont::height() const { return height_; } |