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 {
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::Listener* listener,
ui::SelectFilePolicy* policy) const {
......
......@@ -70,6 +70,7 @@ class Gtk2UI : public views::LinuxUI {
virtual gfx::FontRenderParams::SubpixelRendering
GetSubpixelRenderingStyle() const OVERRIDE;
virtual std::string GetDefaultFontDescription() const OVERRIDE;
virtual double GetFontDPI() const OVERRIDE;
// ui::LinuxShellDialog:
virtual ui::SelectFileDialog* CreateSelectFileDialog(
......
......@@ -42,6 +42,10 @@ class GFX_EXPORT LinuxFontDelegate {
// Returns the Pango description for the default UI font. The format matches
// that returned by pango_font_description_to_string().
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
......
......@@ -17,6 +17,7 @@
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.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/text_utils.h"
......@@ -84,38 +85,24 @@ cairo_font_options_t* CreateCairoFontOptions(const FontRenderParams& params) {
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);
// Returns the DPI that should be used by Pango.
double GetPangoDPI() {
static double dpi = -1.0;
if (dpi < 0.0) {
const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
if (delegate)
dpi = delegate->GetFontDPI();
if (dpi <= 0.0)
dpi = 96.0;
}
return resolution;
return dpi;
}
// 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
float GetPixelsInPoint() {
static float pixels_in_point = 1.0;
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;
}
double GetPixelsInPoint() {
static double pixels_in_point = GetPangoDPI() / 72.0; // 72 points in an inch
return pixels_in_point;
}
......@@ -173,14 +160,10 @@ void SetUpPangoLayout(
pango_layout_set_width(layout, -1);
}
// Set the resolution to match that used by Gtk. If we don't set the
// resolution and the resolution differs from the default, Gtk and Chrome end
// up drawing at different sizes.
double resolution = GetPangoResolution();
if (resolution > 0) {
pango_cairo_context_set_resolution(pango_layout_get_context(layout),
resolution);
}
// Set the layout's resolution to match the resolution used to convert from
// points to pixels.
pango_cairo_context_set_resolution(pango_layout_get_context(layout),
GetPangoDPI());
// Set text and accelerator character if needed.
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