Commit ccf6a5b8 authored by derat@chromium.org's avatar derat@chromium.org

linux: Get font DPI from GTK.

Use GtkSettings's gtk-xft-dpi property to convert Pango font
descriptions from points to pixels. Previously
PangoContext's default resolution, which is sometimes
(always?) unset, was used, resulting in 96 DPI being used
for conversions regardless of the value reported by X.

BUG=375824

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284121 0039d316-1c4b-4281-b951-d872f2087c98
parent 13dee844
...@@ -659,6 +659,16 @@ std::string Gtk2UI::GetDefaultFontDescription() const { ...@@ -659,6 +659,16 @@ std::string Gtk2UI::GetDefaultFontDescription() const {
return default_font_description_; return default_font_description_;
} }
double Gtk2UI::GetFontDPI() const {
GtkSettings* gtk_settings = gtk_settings_get_default();
CHECK(gtk_settings);
gint dpi = -1;
g_object_get(gtk_settings, "gtk-xft-dpi", &dpi, NULL);
// GTK multiplies the DPI by 1024 before storing it.
return (dpi > 0) ? dpi / 1024.0 : dpi;
}
ui::SelectFileDialog* Gtk2UI::CreateSelectFileDialog( ui::SelectFileDialog* Gtk2UI::CreateSelectFileDialog(
ui::SelectFileDialog::Listener* listener, ui::SelectFileDialog::Listener* listener,
ui::SelectFilePolicy* policy) const { ui::SelectFilePolicy* policy) const {
......
...@@ -70,6 +70,7 @@ class Gtk2UI : public views::LinuxUI { ...@@ -70,6 +70,7 @@ class Gtk2UI : public views::LinuxUI {
virtual gfx::FontRenderParams::SubpixelRendering virtual gfx::FontRenderParams::SubpixelRendering
GetSubpixelRenderingStyle() const OVERRIDE; GetSubpixelRenderingStyle() const OVERRIDE;
virtual std::string GetDefaultFontDescription() const OVERRIDE; virtual std::string GetDefaultFontDescription() const OVERRIDE;
virtual double GetFontDPI() const OVERRIDE;
// ui::LinuxShellDialog: // ui::LinuxShellDialog:
virtual ui::SelectFileDialog* CreateSelectFileDialog( virtual ui::SelectFileDialog* CreateSelectFileDialog(
......
...@@ -42,6 +42,10 @@ class GFX_EXPORT LinuxFontDelegate { ...@@ -42,6 +42,10 @@ class GFX_EXPORT LinuxFontDelegate {
// Returns the Pango description for the default UI font. The format matches // Returns the Pango description for the default UI font. The format matches
// that returned by pango_font_description_to_string(). // that returned by pango_font_description_to_string().
virtual std::string GetDefaultFontDescription() const = 0; virtual std::string GetDefaultFontDescription() const = 0;
// Returns the resolution (as pixels-per-inch) that should be used to convert
// font sizes between points and pixels. -1 is returned if the DPI is unset.
virtual double GetFontDPI() const = 0;
}; };
} // namespace gfx } // namespace gfx
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/font_render_params.h" #include "ui/gfx/font_render_params.h"
#include "ui/gfx/linux_font_delegate.h"
#include "ui/gfx/platform_font_pango.h" #include "ui/gfx/platform_font_pango.h"
#include "ui/gfx/text_utils.h" #include "ui/gfx/text_utils.h"
...@@ -84,38 +85,24 @@ cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) { ...@@ -84,38 +85,24 @@ cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) {
return cairo_font_options; return cairo_font_options;
} }
// Returns the resolution (DPI) used by pango. A negative value means the // Returns the DPI that should be used by Pango.
// resolution hasn't been set. double GetPangoDPI() {
double GetPangoResolution() { static double dpi = -1.0;
static double resolution; if (dpi < 0.0) {
static bool determined_resolution = false; const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
if (!determined_resolution) { if (delegate)
determined_resolution = true; dpi = delegate->GetFontDPI();
PangoContext* default_context = GetPangoContext(); if (dpi <= 0.0)
resolution = pango_cairo_context_get_resolution(default_context); dpi = 96.0;
g_object_unref(default_context);
} }
return resolution; return dpi;
} }
// Returns the number of pixels in a point. // Returns the number of pixels in a point.
// - multiply a point size by this to get pixels ("device units") // - multiply a point size by this to get pixels ("device units")
// - divide a pixel size by this to get points // - divide a pixel size by this to get points
float GetPixelsInPoint() { double GetPixelsInPoint() {
static float pixels_in_point = 1.0; static double pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch
static bool determined_value = false;
if (!determined_value) {
// http://goo.gl/UIh5m: "This is a scale factor between points specified in
// a PangoFontDescription and Cairo units. The default value is 96, meaning
// that a 10 point font will be 13 units high. (10 * 96. / 72. = 13.3)."
double pango_dpi = GetPangoResolution();
if (pango_dpi <= 0)
pango_dpi = 96.0;
pixels_in_point = pango_dpi / 72.0; // 72 points in an inch
determined_value = true;
}
return pixels_in_point; return pixels_in_point;
} }
...@@ -173,14 +160,10 @@ void SetUpPangoLayout( ...@@ -173,14 +160,10 @@ void SetUpPangoLayout(
pango_layout_set_width(layout, -1); pango_layout_set_width(layout, -1);
} }
// Set the resolution to match that used by Gtk. If we don't set the // Set the layout's resolution to match the resolution used to convert from
// resolution and the resolution differs from the default, Gtk and Chrome end // points to pixels.
// up drawing at different sizes. pango_cairo_context_set_resolution(pango_layout_get_context(layout),
double resolution = GetPangoResolution(); GetPangoDPI());
if (resolution > 0) {
pango_cairo_context_set_resolution(pango_layout_get_context(layout),
resolution);
}
// Set text and accelerator character if needed. // Set text and accelerator character if needed.
if (flags & Canvas::SHOW_PREFIX) { if (flags & Canvas::SHOW_PREFIX) {
......
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