diff options
-rw-r--r-- | app/gfx/font.h | 4 | ||||
-rw-r--r-- | app/gfx/font_gtk.cc | 29 | ||||
-rw-r--r-- | app/gfx/font_skia.cc | 24 |
3 files changed, 33 insertions, 24 deletions
diff --git a/app/gfx/font.h b/app/gfx/font.h index 8086117..66f5a1d 100644 --- a/app/gfx/font.h +++ b/app/gfx/font.h @@ -232,6 +232,10 @@ class Font { // The default font, used for the default constructor. static Font* default_font_; + // Return the scale factor for fonts that account for DPI. We clamp the + // max DPI to prevent large fonts from overflowing UI elements. + static float GetPangoScaleFactor(); + // The average width of a character, initialized and cached if needed. double avg_width() const; diff --git a/app/gfx/font_gtk.cc b/app/gfx/font_gtk.cc index 85640b0..dbd4228 100644 --- a/app/gfx/font_gtk.cc +++ b/app/gfx/font_gtk.cc @@ -4,6 +4,7 @@ #include "app/gfx/font.h" +#include <algorithm> #include <fontconfig/fontconfig.h> #include <gtk/gtk.h> @@ -48,6 +49,30 @@ static std::wstring FindBestMatchFontFamilyName(const char* family_name) { return font_family; } +// Pango scales font sizes. This returns the scale factor. See +// pango_cairo_context_set_resolution for details. +// NOTE: this isn't entirely accurate, in that Pango also consults the +// FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this +// seems to give us the same sizes as used by Pango for all our fonts in both +// English and Thai. +float Font::GetPangoScaleFactor() { + static float scale_factor = 0; + static bool determined_scale = false; + if (!determined_scale) { + PangoContext* context = gdk_pango_context_get(); + scale_factor = pango_cairo_context_get_resolution(context); + // Until we switch to vector graphics, force the max DPI to 96.0. + scale_factor = std::min(scale_factor, 96.f); + g_object_unref(context); + if (scale_factor <= 0) + scale_factor = 1; + else + scale_factor /= 72.0; + determined_scale = true; + } + return scale_factor; +} + // static Font Font::CreateFont(PangoFontDescription* desc) { gint size = pango_font_description_get_size(desc); @@ -105,7 +130,9 @@ PangoFontDescription* Font::PangoFontFromGfxFont( gfx::Font font = gfx_font; // Copy so we can call non-const methods. PangoFontDescription* pfd = pango_font_description_new(); pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); - pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); + // Set the absolute size to avoid overflowing UI elements. + pango_font_description_set_absolute_size(pfd, + font.FontSize() * PANGO_SCALE * Font::GetPangoScaleFactor()); switch (font.style()) { case gfx::Font::NORMAL: diff --git a/app/gfx/font_skia.cc b/app/gfx/font_skia.cc index eee1238..3499caa 100644 --- a/app/gfx/font_skia.cc +++ b/app/gfx/font_skia.cc @@ -22,28 +22,6 @@ namespace { // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. const char* kFallbackFontFamilyName = "sans"; -// Pango scales font sizes. This returns the scale factor. See -// pango_cairo_context_set_resolution for details. -// NOTE: this isn't entirely accurate, in that Pango also consults the -// FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this -// seems to give us the same sizes as used by Pango for all our fonts in both -// English and Thia. -static double GetPangoScaleFactor() { - static float scale_factor = 0; - static bool determined_scale = false; - if (!determined_scale) { - PangoContext* context = gdk_pango_context_get(); - scale_factor = pango_cairo_context_get_resolution(context); - g_object_unref(context); - if (scale_factor <= 0) - scale_factor = 1; - else - scale_factor /= 72.0; - determined_scale = true; - } - return scale_factor; -} - // Retrieves the pango metrics for a pango font description. Caches the metrics // and never frees them. The metrics objects are relatively small and // very expensive to look up. @@ -190,7 +168,7 @@ Font Font::DeriveFont(int size_delta, int style) const { void Font::PaintSetup(SkPaint* paint) const { paint->setAntiAlias(false); paint->setSubpixelText(false); - paint->setTextSize(SkFloatToScalar(font_size_ * GetPangoScaleFactor())); + paint->setTextSize(SkFloatToScalar(font_size_ * Font::GetPangoScaleFactor())); paint->setTypeface(typeface_); paint->setFakeBoldText((BOLD & style_) && !typeface_->isBold()); paint->setTextSkewX((ITALIC & style_) && !typeface_->isItalic() ? |