Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "app/gfx/font.h" | 5 #include "app/gfx/font.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 7 #include <fontconfig/fontconfig.h> | 8 #include <fontconfig/fontconfig.h> |
| 8 #include <gtk/gtk.h> | 9 #include <gtk/gtk.h> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 | 14 |
| 14 namespace gfx { | 15 namespace gfx { |
| 15 | 16 |
| 16 Font* Font::default_font_ = NULL; | 17 Font* Font::default_font_ = NULL; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 41 FcPatternGetString(match, FC_FAMILY, 0, &match_family); | 42 FcPatternGetString(match, FC_FAMILY, 0, &match_family); |
| 42 | 43 |
| 43 std::wstring font_family = UTF8ToWide( | 44 std::wstring font_family = UTF8ToWide( |
| 44 reinterpret_cast<char*>(match_family)); | 45 reinterpret_cast<char*>(match_family)); |
| 45 FcPatternDestroy(match); | 46 FcPatternDestroy(match); |
| 46 FcPatternDestroy(pattern); | 47 FcPatternDestroy(pattern); |
| 47 free(family_name_copy); | 48 free(family_name_copy); |
| 48 return font_family; | 49 return font_family; |
| 49 } | 50 } |
| 50 | 51 |
| 52 // Pango scales font sizes. This returns the scale factor. See | |
| 53 // pango_cairo_context_set_resolution for details. | |
| 54 // NOTE: this isn't entirely accurate, in that Pango also consults the | |
| 55 // FC_PIXEL_SIZE first (see get_font_size in pangocairo-fcfont), but this | |
| 56 // seems to give us the same sizes as used by Pango for all our fonts in both | |
| 57 // English and Thai. | |
| 58 float Font::GetPangoScaleFactor() { | |
| 59 static float scale_factor = 0; | |
| 60 static bool determined_scale = false; | |
| 61 if (!determined_scale) { | |
| 62 PangoContext* context = gdk_pango_context_get(); | |
| 63 scale_factor = pango_cairo_context_get_resolution(context); | |
| 64 // Until we switch to vector graphics, force the max DPI to 96.0. | |
| 65 scale_factor = std::min(scale_factor, 96.f); | |
|
Evan Martin
2010/03/19 04:56:56
This is the new code, right?
tony
2010/03/19 04:59:36
Yes.
| |
| 66 g_object_unref(context); | |
| 67 if (scale_factor <= 0) | |
| 68 scale_factor = 1; | |
| 69 else | |
| 70 scale_factor /= 72.0; | |
| 71 determined_scale = true; | |
| 72 } | |
| 73 return scale_factor; | |
| 74 } | |
| 75 | |
| 51 // static | 76 // static |
| 52 Font Font::CreateFont(PangoFontDescription* desc) { | 77 Font Font::CreateFont(PangoFontDescription* desc) { |
| 53 gint size = pango_font_description_get_size(desc); | 78 gint size = pango_font_description_get_size(desc); |
| 54 const char* family_name = pango_font_description_get_family(desc); | 79 const char* family_name = pango_font_description_get_family(desc); |
| 55 | 80 |
| 56 // Find best match font for |family_name| to make sure we can get | 81 // Find best match font for |family_name| to make sure we can get |
| 57 // a SkTypeface for the default font. | 82 // a SkTypeface for the default font. |
| 58 // TODO(agl): remove this. | 83 // TODO(agl): remove this. |
| 59 std::wstring font_family = FindBestMatchFontFamilyName(family_name); | 84 std::wstring font_family = FindBestMatchFontFamilyName(family_name); |
| 60 | 85 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 123 |
| 99 CopyFont(*default_font_); | 124 CopyFont(*default_font_); |
| 100 } | 125 } |
| 101 | 126 |
| 102 // static | 127 // static |
| 103 PangoFontDescription* Font::PangoFontFromGfxFont( | 128 PangoFontDescription* Font::PangoFontFromGfxFont( |
| 104 const gfx::Font& gfx_font) { | 129 const gfx::Font& gfx_font) { |
| 105 gfx::Font font = gfx_font; // Copy so we can call non-const methods. | 130 gfx::Font font = gfx_font; // Copy so we can call non-const methods. |
| 106 PangoFontDescription* pfd = pango_font_description_new(); | 131 PangoFontDescription* pfd = pango_font_description_new(); |
| 107 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); | 132 pango_font_description_set_family(pfd, WideToUTF8(font.FontName()).c_str()); |
| 108 pango_font_description_set_size(pfd, font.FontSize() * PANGO_SCALE); | 133 // Set the absolute size to avoid overflowing UI elements. |
| 134 pango_font_description_set_absolute_size(pfd, | |
| 135 font.FontSize() * PANGO_SCALE * Font::GetPangoScaleFactor()); | |
|
sky
2010/03/23 20:33:32
You also need to change Canvas::SetupPangoLayout o
| |
| 109 | 136 |
| 110 switch (font.style()) { | 137 switch (font.style()) { |
| 111 case gfx::Font::NORMAL: | 138 case gfx::Font::NORMAL: |
| 112 // Nothing to do, should already be PANGO_STYLE_NORMAL. | 139 // Nothing to do, should already be PANGO_STYLE_NORMAL. |
| 113 break; | 140 break; |
| 114 case gfx::Font::BOLD: | 141 case gfx::Font::BOLD: |
| 115 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); | 142 pango_font_description_set_weight(pfd, PANGO_WEIGHT_BOLD); |
| 116 break; | 143 break; |
| 117 case gfx::Font::ITALIC: | 144 case gfx::Font::ITALIC: |
| 118 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); | 145 pango_font_description_set_style(pfd, PANGO_STYLE_ITALIC); |
| 119 break; | 146 break; |
| 120 case gfx::Font::UNDERLINED: | 147 case gfx::Font::UNDERLINED: |
| 121 // TODO(deanm): How to do underlined? Where do we use it? Probably have | 148 // TODO(deanm): How to do underlined? Where do we use it? Probably have |
| 122 // to paint it ourselves, see pango_font_metrics_get_underline_position. | 149 // to paint it ourselves, see pango_font_metrics_get_underline_position. |
| 123 break; | 150 break; |
| 124 } | 151 } |
| 125 | 152 |
| 126 return pfd; | 153 return pfd; |
| 127 } | 154 } |
| 128 | 155 |
| 129 } // namespace gfx | 156 } // namespace gfx |
| OLD | NEW |