diff options
author | yukishiino@chromium.org <yukishiino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-09 03:12:55 +0000 |
---|---|---|
committer | yukishiino@chromium.org <yukishiino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-09 03:12:55 +0000 |
commit | 2a25d802bdaf4b3d6dd543fa7a9ba999f556eeac (patch) | |
tree | 71b783c7f297edc6272ef29b4a112c90637d2b25 /ui/gfx/font_list_impl.h | |
parent | 96497b56a074a41dface00a8232165266af10605 (diff) | |
download | chromium_src-2a25d802bdaf4b3d6dd543fa7a9ba999f556eeac.zip chromium_src-2a25d802bdaf4b3d6dd543fa7a9ba999f556eeac.tar.gz chromium_src-2a25d802bdaf4b3d6dd543fa7a9ba999f556eeac.tar.bz2 |
Makes FontList light-weighted introducing FontListImpl.
Like Font class has scoped_refptr<PlatformFont>, makes FontList have scoped_refptr<FontListImpl>. FontListImpl is almost the same as the old FontList and implements all functionalities.
There are two major benefits with this CL.
1. Copying a FontList to another FontList is very quick. It just copies a scoped_refptr.
2. Calculated font metrics are shared among all FontList instances which refer to the same FontListImpl.
In addition, this CL may fix Issue 340396. I don't fully understand the cause yet, but it seems that copying the object in a ctor is causing the issue (It's a little tricky, but I thought it should be okay). This CL doesn't copy the object. So I think this CL may fix the issue.
BUG=340396
TEST=Run ui_unittests, views_unittests and unit_tests.
Review URL: https://codereview.chromium.org/148773007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249976 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx/font_list_impl.h')
-rw-r--r-- | ui/gfx/font_list_impl.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/ui/gfx/font_list_impl.h b/ui/gfx/font_list_impl.h new file mode 100644 index 0000000..94bb330 --- /dev/null +++ b/ui/gfx/font_list_impl.h @@ -0,0 +1,123 @@ +// Copyright 2014 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. + +#ifndef UI_GFX_FONT_LIST_IMPL_H_ +#define UI_GFX_FONT_LIST_IMPL_H_ + +#include <string> +#include <vector> + +#include "base/memory/ref_counted.h" + +namespace gfx { + +class Font; + +// FontListImpl is designed to provide the implementation of FontList and +// intended to be used only from FontList. You must not use this class +// directly. +// +// FontListImpl represents a list of fonts either in the form of Font vector or +// in the form of a string representing font names, styles, and size. +// +// FontListImpl could be initialized either way without conversion to the other +// form. The conversion to the other form is done only when asked to get the +// other form. +// +// For the format of font description string, see font_list.h for details. +class FontListImpl : public base::RefCounted<FontListImpl> { + public: + // Creates a font list from a string representing font names, styles, and + // size. + explicit FontListImpl(const std::string& font_description_string); + + // Creates a font list from font names, styles and size. + FontListImpl(const std::vector<std::string>& font_names, + int font_style, + int font_size); + + // Creates a font list from a Font vector. + // All fonts in this vector should have the same style and size. + explicit FontListImpl(const std::vector<Font>& fonts); + + // Creates a font list from a Font. + explicit FontListImpl(const Font& font); + + // Returns a new FontListImpl with the same font names but resized and the + // given style. |size_delta| is the size in pixels to add to the current font + // size. |font_style| specifies the new style, which is a bitmask of the + // values: Font::BOLD, Font::ITALIC and Font::UNDERLINE. + FontListImpl* Derive(int size_delta, int font_style) const; + + // Returns the height of this font list, which is max(ascent) + max(descent) + // for all the fonts in the font list. + int GetHeight() const; + + // Returns the baseline of this font list, which is max(baseline) for all the + // fonts in the font list. + int GetBaseline() const; + + // Returns the cap height of this font list. + // Currently returns the cap height of the primary font. + int GetCapHeight() const; + + // Returns the expected number of horizontal pixels needed to display the + // specified length of characters. Call GetStringWidth() to retrieve the + // actual number. + int GetExpectedTextWidth(int length) const; + + // Returns the |gfx::Font::FontStyle| style flags for this font list. + int GetFontStyle() const; + + // Returns a string representing font names, styles, and size. If the + // FontListImpl is initialized by a vector of Font, use the first font's style + // and size for the description. + const std::string& GetFontDescriptionString() const; + + // Returns the font size in pixels. + int GetFontSize() const; + + // Returns the Font vector. + const std::vector<Font>& GetFonts() const; + + // Returns the first font in the list. + const Font& GetPrimaryFont() const; + + private: + friend class base::RefCounted<FontListImpl>; + + ~FontListImpl(); + + // Extracts common font height and baseline into |common_height_| and + // |common_baseline_|. + void CacheCommonFontHeightAndBaseline() const; + + // Extracts font style and size into |font_style_| and |font_size_|. + void CacheFontStyleAndSize() const; + + // A vector of Font. If FontListImpl is constructed with font description + // string, |fonts_| is not initialized during construction. Instead, it is + // computed lazily when user asked to get the font vector. + mutable std::vector<Font> fonts_; + + // A string representing font names, styles, and sizes. + // Please refer to the comments before class declaration for details on string + // format. + // If FontListImpl is constructed with a vector of font, + // |font_description_string_| is not initialized during construction. Instead, + // it is computed lazily when user asked to get the font description string. + mutable std::string font_description_string_; + + // The cached common height and baseline of the fonts in the font list. + mutable int common_height_; + mutable int common_baseline_; + + // Cached font style and size. + mutable int font_style_; + mutable int font_size_; +}; + +} // namespace gfx + +#endif // UI_GFX_FONT_LIST_IMPL_H_ |