diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-28 18:54:50 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-28 18:54:50 +0000 |
commit | c8a5c738737660e788c6f30455406546362cace1 (patch) | |
tree | 74f4519ea7d7c18c0e847d3d529086e91ee80a71 /gfx | |
parent | ebb54a416dd427df5f2c51eb71ac89f1a7e936dc (diff) | |
download | chromium_src-c8a5c738737660e788c6f30455406546362cace1.zip chromium_src-c8a5c738737660e788c6f30455406546362cace1.tar.gz chromium_src-c8a5c738737660e788c6f30455406546362cace1.tar.bz2 |
linux: Make PlatformFontGtk deal only in pixels.
gfx::Font deals with pixels (at least, its GetFontSize()
method is documented as returning them and that's what all
of the metrics-related methods return; I'm assuming that the
|font_size| argument in its constructors deals with the
same units). PlatformFontGtk's constructor treated
|font_size| as points, though, and mostly used points
internally.
This change makes PlatformFontGtk use pixels internally and
renames all of the variables to describe their units.
It also changes the size of the omnibox suggestion text:
- on Chrome OS, it matches the size of the omnibox text
- on Linux, it's slightly smaller (similar to Windows)
It also makes us update the suggestion text size when
switching between standard and GTK+ themes (the omnibox
itself already handled this correctly).
Tested with the following configurations in a regular GTK+
build:
120 DPI, GTK+ theme:
- 13-pixel tab text
- 16-pixel omnibox text
- 13-pixel omnibox popup text
- 16-pixel popup location text
96 DPI, GTK+ theme:
- 13-pixel tab text
- 13-pixel omnibox popup text
- 12-pixel omnibox popup text
- 13-pixel popup location text
72 DPI, GTK+ theme:
- 13-pixel tab text
- 9-pixel omnibox popup text
- 9-pixel omnibox popup text
- 9-pixel popup location text
120 DPI, Classic theme:
- 13-pixel tab text
- 13-pixel omnibox text
- 12-pixel omnibox popup text
- 10-pixel popup location text
96 DPI, Classic theme:
- 13-pixel tab text
- 13-pixel omnibox text
- 12-pixel omnibox popup text
- 10-pixel popup location text
72 DPI, Classic theme:
- 13-pixel tab text
- 13-pixel omnibox text
- 12-pixel omnibox popup text
- 10-pixel popup location text
Put more simply:
- Regular themes use hardcoded font sizes.
- The GTK+ theme uses a hardcoded font size for tabs and
sizes based on the DPI for the omnibox.
BUG=61433,26354,chromium-os:8658
TEST=see above. also took screenshots and checked that the onscreen font size matches that passed to gfx::Font and that the computed metrics are correct, and built a Chrome OS image to verify that things look reasonable, and verified that text isn't clipped on the "Aw, Snap!" page
Review URL: http://codereview.chromium.org/6378007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72985 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/font.cc | 1 | ||||
-rw-r--r-- | gfx/font.h | 1 | ||||
-rw-r--r-- | gfx/gtk_util.cc | 4 | ||||
-rw-r--r-- | gfx/platform_font_gtk.cc | 140 | ||||
-rw-r--r-- | gfx/platform_font_gtk.h | 16 |
5 files changed, 80 insertions, 82 deletions
diff --git a/gfx/font.cc b/gfx/font.cc index 8b8241d..78b1a34 100644 --- a/gfx/font.cc +++ b/gfx/font.cc @@ -83,4 +83,3 @@ NativeFont Font::GetNativeFont() const { } } // namespace gfx - @@ -111,4 +111,3 @@ class Font { } // namespace gfx #endif // GFX_FONT_H_ - diff --git a/gfx/gtk_util.cc b/gfx/gtk_util.cc index 96195fe..fc240e0 100644 --- a/gfx/gtk_util.cc +++ b/gfx/gtk_util.cc @@ -171,10 +171,6 @@ double GetPangoResolution() { determined_resolution = true; PangoContext* default_context = gdk_pango_context_get(); resolution = pango_cairo_context_get_resolution(default_context); -#if !defined(OS_CHROMEOS) - // Until we switch to vector graphics, force the max DPI to 96.0. - resolution = std::min(resolution, 96.); -#endif g_object_unref(default_context); } return resolution; diff --git a/gfx/platform_font_gtk.cc b/gfx/platform_font_gtk.cc index 61a0933..82b2e06 100644 --- a/gfx/platform_font_gtk.cc +++ b/gfx/platform_font_gtk.cc @@ -27,6 +27,27 @@ namespace { // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. const char* kFallbackFontFamilyName = "sans"; +// 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 = gfx::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; +} + // 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. @@ -127,14 +148,18 @@ PlatformFontGtk::PlatformFontGtk(const Font& other) { } PlatformFontGtk::PlatformFontGtk(NativeFont native_font) { - gint size = pango_font_description_get_size(native_font); const char* family_name = pango_font_description_get_family(native_font); + gint size_in_pixels = 0; if (pango_font_description_get_size_is_absolute(native_font)) { - // font_size_ is treated as scaled (see comment in GetPangoScaleFactor). If - // we get here the font size is absolute though, and we need to invert the - // scale so that when scaled in the rest of this class everything lines up. - size /= GetPangoScaleFactor(); + // If the size is absolute, then it's in Pango units rather than points. + // There are PANGO_SCALE Pango units in a device unit (pixel). + size_in_pixels = pango_font_description_get_size(native_font) / PANGO_SCALE; + } else { + // Otherwise, we need to convert from points. + size_in_pixels = + pango_font_description_get_size(native_font) * GetPixelsInPoint() / + PANGO_SCALE; } // Find best match font for |family_name| to make sure we can get @@ -142,7 +167,7 @@ PlatformFontGtk::PlatformFontGtk(NativeFont native_font) { // TODO(agl): remove this. string16 font_family = FindBestMatchFontFamilyName(family_name); - InitWithNameAndSize(font_family, size / PANGO_SCALE); + InitWithNameAndSize(font_family, size_in_pixels); int style = 0; if (pango_font_description_get_weight(native_font) == PANGO_WEIGHT_BOLD) { // TODO(davemoore) What should we do about other weights? We currently @@ -164,12 +189,12 @@ PlatformFontGtk::PlatformFontGtk(const string16& font_name, double PlatformFontGtk::underline_position() const { const_cast<PlatformFontGtk*>(this)->InitPangoMetrics(); - return underline_position_; + return underline_position_pixels_; } double PlatformFontGtk::underline_thickness() const { const_cast<PlatformFontGtk*>(this)->InitPangoMetrics(); - return underline_thickness_; + return underline_thickness_pixels_; } //////////////////////////////////////////////////////////////////////////////// @@ -178,13 +203,13 @@ double PlatformFontGtk::underline_thickness() const { Font PlatformFontGtk::DeriveFont(int size_delta, int style) const { // If the delta is negative, if must not push the size below 1 if (size_delta < 0) - DCHECK_LT(-size_delta, font_size_); + DCHECK_LT(-size_delta, font_size_pixels_); if (style == style_) { // Fast path, we just use the same typeface at a different size return Font(new PlatformFontGtk(typeface_, font_family_, - font_size_ + size_delta, + font_size_pixels_ + size_delta, style_)); } @@ -202,20 +227,20 @@ Font PlatformFontGtk::DeriveFont(int size_delta, int style) const { return Font(new PlatformFontGtk(typeface, font_family_, - font_size_ + size_delta, + font_size_pixels_ + size_delta, style)); } int PlatformFontGtk::GetHeight() const { - return height_; + return height_pixels_; } int PlatformFontGtk::GetBaseline() const { - return ascent_; + return ascent_pixels_; } int PlatformFontGtk::GetAverageCharacterWidth() const { - return SkScalarRound(average_width_); + return SkScalarRound(average_width_pixels_); } int PlatformFontGtk::GetStringWidth(const string16& text) const { @@ -239,15 +264,18 @@ string16 PlatformFontGtk::GetFontName() const { } int PlatformFontGtk::GetFontSize() const { - return font_size_; + return font_size_pixels_; } NativeFont PlatformFontGtk::GetNativeFont() const { PangoFontDescription* pfd = pango_font_description_new(); pango_font_description_set_family(pfd, UTF16ToUTF8(GetFontName()).c_str()); // Set the absolute size to avoid overflowing UI elements. - pango_font_description_set_absolute_size(pfd, - GetFontSize() * PANGO_SCALE * GetPangoScaleFactor()); + // pango_font_description_set_absolute_size() takes a size in Pango units. + // There are PANGO_SCALE Pango units in one device unit. Screen output + // devices use pixels as their device units. + pango_font_description_set_absolute_size( + pfd, font_size_pixels_ * PANGO_SCALE); switch (GetStyle()) { case gfx::Font::NORMAL: @@ -314,20 +342,20 @@ void PlatformFontGtk::InitWithTypefaceNameSizeAndStyle( typeface_ = typeface; typeface_->ref(); font_family_ = font_family; - font_size_ = font_size; + font_size_pixels_ = font_size; style_ = style; pango_metrics_inited_ = false; - average_width_ = 0.0f; - underline_position_ = 0.0f; - underline_thickness_ = 0.0f; + average_width_pixels_ = 0.0f; + underline_position_pixels_ = 0.0f; + underline_thickness_pixels_ = 0.0f; SkPaint paint; SkPaint::FontMetrics metrics; PaintSetup(&paint); paint.getFontMetrics(&metrics); - ascent_ = SkScalarCeil(-metrics.fAscent); - height_ = ascent_ + SkScalarCeil(metrics.fDescent); + ascent_pixels_ = SkScalarCeil(-metrics.fAscent); + height_pixels_ = ascent_pixels_ + SkScalarCeil(metrics.fDescent); } void PlatformFontGtk::InitFromPlatformFont(const PlatformFontGtk* other) { @@ -335,21 +363,20 @@ void PlatformFontGtk::InitFromPlatformFont(const PlatformFontGtk* other) { typeface_ = other->typeface_; typeface_->ref(); font_family_ = other->font_family_; - font_size_ = other->font_size_; + font_size_pixels_ = other->font_size_pixels_; style_ = other->style_; - height_ = other->height_; - ascent_ = other->ascent_; + height_pixels_ = other->height_pixels_; + ascent_pixels_ = other->ascent_pixels_; pango_metrics_inited_ = other->pango_metrics_inited_; - average_width_ = other->average_width_; - underline_position_ = other->underline_position_; - underline_thickness_ = other->underline_thickness_; + average_width_pixels_ = other->average_width_pixels_; + underline_position_pixels_ = other->underline_position_pixels_; + underline_thickness_pixels_ = other->underline_thickness_pixels_; } void PlatformFontGtk::PaintSetup(SkPaint* paint) const { paint->setAntiAlias(false); paint->setSubpixelText(false); - paint->setTextSize( - SkFloatToScalar(font_size_ * GetPangoScaleFactor())); + paint->setTextSize(font_size_pixels_); paint->setTypeface(typeface_); paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? @@ -362,57 +389,38 @@ void PlatformFontGtk::InitPangoMetrics() { PangoFontDescription* pango_desc = GetNativeFont(); PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc); - underline_position_ = - pango_font_metrics_get_underline_position(pango_metrics); - underline_position_ /= PANGO_SCALE; + underline_position_pixels_ = + pango_font_metrics_get_underline_position(pango_metrics) / + PANGO_SCALE; - // todo(davemoore) Come up with a better solution. + // TODO(davemoore): Come up with a better solution. // This is a hack, but without doing this the underlines // we get end up fuzzy. So we align to the midpoint of a pixel. - underline_position_ /= 2; + underline_position_pixels_ /= 2; - underline_thickness_ = - pango_font_metrics_get_underline_thickness(pango_metrics); - underline_thickness_ /= PANGO_SCALE; + underline_thickness_pixels_ = + pango_font_metrics_get_underline_thickness(pango_metrics) / + PANGO_SCALE; - // First get the pango based width - double pango_width = - pango_font_metrics_get_approximate_char_width(pango_metrics); - pango_width /= PANGO_SCALE; + // First get the Pango-based width (converting from Pango units to pixels). + double pango_width_pixels = + pango_font_metrics_get_approximate_char_width(pango_metrics) / + PANGO_SCALE; // Yes, this is how Microsoft recommends calculating the dialog unit // conversions. - int text_width = GetStringWidth( + int text_width_pixels = GetStringWidth( ASCIIToUTF16("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); - double dialog_units = (text_width / 26 + 1) / 2; - average_width_ = std::min(pango_width, dialog_units); + double dialog_units_pixels = (text_width_pixels / 26 + 1) / 2; + average_width_pixels_ = std::min(pango_width_pixels, dialog_units_pixels); pango_font_description_free(pango_desc); } } -float PlatformFontGtk::GetPangoScaleFactor() { - // 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. - static float scale_factor = gfx::GetPangoResolution(); - static bool determined_scale = false; - if (!determined_scale) { - if (scale_factor <= 0) - scale_factor = 1; - else - scale_factor /= 72.0; - determined_scale = true; - } - return scale_factor; -} - double PlatformFontGtk::GetAverageWidth() const { const_cast<PlatformFontGtk*>(this)->InitPangoMetrics(); - return average_width_; + return average_width_pixels_; } //////////////////////////////////////////////////////////////////////////////// diff --git a/gfx/platform_font_gtk.h b/gfx/platform_font_gtk.h index fedbfc1..e69c24a 100644 --- a/gfx/platform_font_gtk.h +++ b/gfx/platform_font_gtk.h @@ -27,9 +27,6 @@ class PlatformFontGtk : public PlatformFont { // pango_font_description_free(). static PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font); - // Return the scale factor for fonts that account for DPI. - static float GetPangoScaleFactor(); - // Position as an offset from the height of the drawn text, used to draw // an underline. This is a negative number, so the underline would be // drawn at y + height + underline_position; @@ -87,19 +84,19 @@ class PlatformFontGtk : public PlatformFont { // Additional information about the face // Skia actually expects a family name and not a font name. string16 font_family_; - int font_size_; + int font_size_pixels_; int style_; // Cached metrics, generated at construction - int height_; - int ascent_; + int height_pixels_; + int ascent_pixels_; // The pango metrics are much more expensive so we wait until we need them // to compute them. bool pango_metrics_inited_; - double average_width_; - double underline_position_; - double underline_thickness_; + double average_width_pixels_; + double underline_position_pixels_; + double underline_thickness_pixels_; // The default font, used for the default constructor. static Font* default_font_; @@ -108,4 +105,3 @@ class PlatformFontGtk : public PlatformFont { } // namespace gfx #endif // GFX_PLATFORM_FONT_GTK_ - |