Commit 93da7658 authored by derat@chromium.org's avatar derat@chromium.org

Simplify PangoLayout configuration.

Remove SetupPangoLayout() and
SetupPangoLayoutWithoutFontDescription().
Rename SetupPangoLayoutWithFontDescription() to
SetUpPangoLayout() and pass a FontList to it instead of a
PangoFontDescription, along with removing an unused "width"
parameter.

BUG=125235

Review URL: https://codereview.chromium.org/374833005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281925 0039d316-1c4b-4281-b951-d872f2087c98
parent 0350804c
......@@ -5,22 +5,18 @@
#include "ui/gfx/pango_util.h"
#include <cairo/cairo.h>
#include <fontconfig/fontconfig.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/font_render_params_linux.h"
#include "ui/gfx/platform_font_pango.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/text_utils.h"
namespace gfx {
......@@ -30,7 +26,13 @@ namespace {
// Marker for accelerators in the text.
const gunichar kAcceleratorChar = '&';
// Return |cairo_font_options|. If needed, allocate and update it.
// Creates and returns a PangoContext. The caller owns the context.
PangoContext* GetPangoContext() {
PangoFontMap* font_map = pango_cairo_font_map_get_default();
return pango_font_map_create_context(font_map);
}
// Returns a static cairo_font_options_t. If needed, allocates and updates it.
// TODO(derat): Return font-specific options: http://crbug.com/125235
cairo_font_options_t* GetCairoFontOptions() {
// Font settings that we initialize once and then use when drawing text.
......@@ -88,6 +90,20 @@ cairo_font_options_t* GetCairoFontOptions() {
return cairo_font_options;
}
// Returns the resolution (DPI) used by pango. A negative value means the
// resolution hasn't been set.
double GetPangoResolution() {
static double resolution;
static bool determined_resolution = false;
if (!determined_resolution) {
determined_resolution = true;
PangoContext* default_context = GetPangoContext();
resolution = pango_cairo_context_get_resolution(default_context);
g_object_unref(default_context);
}
return resolution;
}
// Returns the number of pixels in a point.
// - multiply a point size by this to get pixels ("device units")
// - divide a pixel size by this to get points
......@@ -111,30 +127,13 @@ float GetPixelsInPoint() {
} // namespace
PangoContext* GetPangoContext() {
PangoFontMap* font_map = pango_cairo_font_map_get_default();
return pango_font_map_create_context(font_map);
}
double GetPangoResolution() {
static double resolution;
static bool determined_resolution = false;
if (!determined_resolution) {
determined_resolution = true;
PangoContext* default_context = GetPangoContext();
resolution = pango_cairo_context_get_resolution(default_context);
g_object_unref(default_context);
}
return resolution;
}
// Pass a width greater than 0 to force wrapping and eliding.
static void SetupPangoLayoutWithoutFont(
void SetUpPangoLayout(
PangoLayout* layout,
const base::string16& text,
int width,
const FontList& font_list,
base::i18n::TextDirection text_direction,
int flags) {
// TODO(derat): Use rendering parameters from |font_list| instead of defaults.
cairo_font_options_t* cairo_font_options = GetCairoFontOptions();
// If we got an explicit request to turn off subpixel rendering, disable it on
......@@ -164,9 +163,6 @@ static void SetupPangoLayoutWithoutFont(
(text_direction == base::i18n::RIGHT_TO_LEFT ?
PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR));
if (width > 0)
pango_layout_set_width(layout, width * PANGO_SCALE);
if (flags & Canvas::TEXT_ALIGN_CENTER) {
// We don't support center aligned w/ eliding.
DCHECK(gfx::Canvas::NO_ELLIPSIS);
......@@ -228,31 +224,9 @@ static void SetupPangoLayoutWithoutFont(
pango_layout_set_text(layout, utf8.data(), utf8.size());
}
}
void SetupPangoLayout(PangoLayout* layout,
const base::string16& text,
const Font& font,
int width,
base::i18n::TextDirection text_direction,
int flags) {
SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags);
ScopedPangoFontDescription desc(font.GetNativeFont());
pango_layout_set_font_description(layout, desc.get());
}
void SetupPangoLayoutWithFontDescription(
PangoLayout* layout,
const base::string16& text,
const std::string& font_description,
int width,
base::i18n::TextDirection text_direction,
int flags) {
SetupPangoLayoutWithoutFont(layout, text, width, text_direction, flags);
ScopedPangoFontDescription desc(
pango_font_description_from_string(font_description.c_str()));
ScopedPangoFontDescription desc(pango_font_description_from_string(
font_list.GetFontDescriptionString().c_str()));
pango_layout_set_font_description(layout, desc.get());
}
......
......@@ -7,7 +7,6 @@
#include <cairo/cairo.h>
#include <pango/pango.h>
#include <string>
#include "base/i18n/rtl.h"
#include "base/logging.h"
......@@ -15,20 +14,9 @@
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/gfx_export.h"
typedef struct _PangoContext PangoContext;
namespace gfx {
class Font;
class PlatformFontPango;
class Rect;
// Creates and returns a PangoContext. The caller owns the context.
PangoContext* GetPangoContext();
// Returns the resolution (DPI) used by pango. A negative values means the
// resolution hasn't been set.
double GetPangoResolution();
class FontList;
// Utility class to ensure that PangoFontDescription is freed.
class ScopedPangoFontDescription {
......@@ -55,14 +43,11 @@ class ScopedPangoFontDescription {
// They are shared with internal skia interfaces.
// ----------------------------------------------------------------------------
// Setup pango |layout|; set the |text|, the font description based on
// |font_description|, the |width| in PANGO_SCALE for RTL locale, the base
// |text_direction|, alignment, ellipsis, word wrapping, resolution, etc.
void SetupPangoLayoutWithFontDescription(
// Configures |layout| for the passed-in parameters.
void SetUpPangoLayout(
PangoLayout* layout,
const base::string16& text,
const std::string& font_description,
int width,
const FontList& font_list,
base::i18n::TextDirection text_direction,
int flags);
......
......@@ -73,14 +73,20 @@ std::string* PlatformFontPango::default_font_description_ = NULL;
// PlatformFontPango, public:
PlatformFontPango::PlatformFontPango() {
if (default_font_ == NULL) {
std::string font_name = GetDefaultFont();
if (!default_font_) {
std::string desc_string;
#if defined(OS_CHROMEOS)
// Font name must have been provided by way of SetDefaultFontDescription().
CHECK(default_font_description_);
desc_string = *default_font_description_;
#else
const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
desc_string = delegate ? delegate->GetDefaultFontDescription() : "sans 10";
#endif
ScopedPangoFontDescription desc(
pango_font_description_from_string(font_name.c_str()));
pango_font_description_from_string(desc_string.c_str()));
default_font_ = new Font(desc.get());
DCHECK(default_font_);
}
InitFromPlatformFont(
......@@ -248,18 +254,6 @@ PlatformFontPango::PlatformFontPango(const skia::RefPtr<SkTypeface>& typeface,
PlatformFontPango::~PlatformFontPango() {}
// static
std::string PlatformFontPango::GetDefaultFont() {
#if defined(OS_CHROMEOS)
// Font name must have been provided by way of SetDefaultFontDescription().
CHECK(default_font_description_);
return *default_font_description_;
#else
const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
return delegate ? delegate->GetDefaultFontDescription() : "sans 10";
#endif // defined(OS_CHROMEOS)
}
void PlatformFontPango::InitFromDetails(
const skia::RefPtr<SkTypeface>& typeface,
const std::string& font_family,
......
......@@ -66,10 +66,6 @@ class GFX_EXPORT PlatformFontPango : public PlatformFont {
int style);
virtual ~PlatformFontPango();
// Returns a Pango font description (suitable for parsing by
// pango_font_description_from_string()) for the default UI font.
static std::string GetDefaultFont();
// Initializes this object based on the passed-in details. If |typeface| is
// empty, a new typeface will be loaded.
void InitFromDetails(
......
......@@ -307,12 +307,8 @@ void RenderTextPango::EnsureLayout() {
cairo_destroy(cr);
cairo_surface_destroy(surface);
SetupPangoLayoutWithFontDescription(layout_,
GetLayoutText(),
font_list().GetFontDescriptionString(),
0,
GetTextDirection(),
Canvas::DefaultCanvasTextAlignment());
SetUpPangoLayout(layout_, GetLayoutText(), font_list(), GetTextDirection(),
Canvas::DefaultCanvasTextAlignment());
// No width set so that the x-axis position is relative to the start of the
// text. ToViewPoint and ToTextPoint take care of the position conversion
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment