Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/platform_font_pango.h" | 5 #include "ui/gfx/platform_font_pango.h" |
| 6 | 6 |
| 7 #include <fontconfig/fontconfig.h> | 7 #include <fontconfig/fontconfig.h> |
| 8 #include <pango/pango.h> | 8 #include <pango/pango.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "ui/gfx/pango_util.h" | 24 #include "ui/gfx/pango_util.h" |
| 25 #include "ui/gfx/text_utils.h" | 25 #include "ui/gfx/text_utils.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 // The font family name which is used when a user's application font for | 29 // The font family name which is used when a user's application font for |
| 30 // GNOME/KDE is a non-scalable one. The name should be listed in the | 30 // GNOME/KDE is a non-scalable one. The name should be listed in the |
| 31 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. | 31 // IsFallbackFontAllowed function in skia/ext/SkFontHost_fontconfig_direct.cpp. |
| 32 const char* kFallbackFontFamilyName = "sans"; | 32 const char* kFallbackFontFamilyName = "sans"; |
| 33 | 33 |
| 34 // Creates a SkTypeface for the passed-in Font::FontStyle and family. If a | |
| 35 // fallback typeface is used instead of the requested family, |family| will be | |
| 36 // updated to contain the fallback's family name. | |
| 37 skia::RefPtr<SkTypeface> CreateSkTypeface(int style, std::string* family) { | |
| 38 DCHECK(family); | |
| 39 | |
| 40 int skia_style = SkTypeface::kNormal; | |
| 41 if (gfx::Font::BOLD & style) | |
| 42 skia_style |= SkTypeface::kBold; | |
| 43 if (gfx::Font::ITALIC & style) | |
| 44 skia_style |= SkTypeface::kItalic; | |
| 45 | |
| 46 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(SkTypeface::CreateFromName( | |
| 47 family->c_str(), static_cast<SkTypeface::Style>(skia_style))); | |
|
Daniel Erat
2014/07/18 01:16:19
note that InitFromDetails() used to always pass th
msw
2014/07/18 03:58:27
Hmm, I wonder if we were always doing setFakeBoldT
| |
| 48 if (!typeface) { | |
| 49 // A non-scalable font such as .pcf is specified. Fall back to a default | |
| 50 // scalable font. | |
| 51 typeface = skia::AdoptRef(SkTypeface::CreateFromName( | |
| 52 kFallbackFontFamilyName, static_cast<SkTypeface::Style>(skia_style))); | |
|
Daniel Erat
2014/07/18 01:16:19
i'm less sure about this one. should i uncondition
msw
2014/07/18 03:58:27
I suppose you could add another if (!typeface) blo
Daniel Erat
2014/07/18 04:49:59
i think i'll just watch for crashes, since i don't
Daniel Erat
2014/07/18 14:53:45
just to follow up on this: skia looks like it's do
msw
2014/07/18 17:32:26
Nice! Good call testing that behavior!
| |
| 53 CHECK(typeface) << "Could not find any font: " << family << ", " | |
| 54 << kFallbackFontFamilyName; | |
| 55 *family = kFallbackFontFamilyName; | |
| 56 } | |
| 57 return typeface; | |
| 58 } | |
| 59 | |
| 34 } // namespace | 60 } // namespace |
| 35 | 61 |
| 36 namespace gfx { | 62 namespace gfx { |
| 37 | 63 |
| 38 // static | 64 // static |
| 39 Font* PlatformFontPango::default_font_ = NULL; | 65 Font* PlatformFontPango::default_font_ = NULL; |
| 40 | 66 |
| 41 #if defined(OS_CHROMEOS) | 67 #if defined(OS_CHROMEOS) |
| 42 // static | 68 // static |
| 43 std::string* PlatformFontPango::default_font_description_ = NULL; | 69 std::string* PlatformFontPango::default_font_description_ = NULL; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 default_font_description_ = new std::string(font_description); | 158 default_font_description_ = new std::string(font_description); |
| 133 } | 159 } |
| 134 | 160 |
| 135 #endif | 161 #endif |
| 136 | 162 |
| 137 Font PlatformFontPango::DeriveFont(int size_delta, int style) const { | 163 Font PlatformFontPango::DeriveFont(int size_delta, int style) const { |
| 138 const int new_size = font_size_pixels_ + size_delta; | 164 const int new_size = font_size_pixels_ + size_delta; |
| 139 DCHECK_GT(new_size, 0); | 165 DCHECK_GT(new_size, 0); |
| 140 | 166 |
| 141 // If the style changed, we may need to load a new face. | 167 // If the style changed, we may need to load a new face. |
| 142 skia::RefPtr<SkTypeface> typeface = typeface_; | 168 std::string new_family = font_family_; |
| 143 if (style != style_) { | 169 skia::RefPtr<SkTypeface> typeface = |
| 144 int skstyle = SkTypeface::kNormal; | 170 (style == style_) ? typeface_ : CreateSkTypeface(style, &new_family); |
| 145 if (gfx::Font::BOLD & style) | |
| 146 skstyle |= SkTypeface::kBold; | |
| 147 if (gfx::Font::ITALIC & style) | |
| 148 skstyle |= SkTypeface::kItalic; | |
| 149 typeface = skia::AdoptRef(SkTypeface::CreateFromName( | |
| 150 font_family_.c_str(), static_cast<SkTypeface::Style>(skstyle))); | |
| 151 } | |
| 152 | 171 |
| 153 // If the size changed, get updated rendering settings. | 172 // If the size changed, get updated rendering settings. |
|
msw
2014/07/18 03:58:27
Should we also do this if the family (or style) ch
Daniel Erat
2014/07/18 04:49:59
think i mentioned the style question in an earlier
| |
| 154 FontRenderParams render_params = font_render_params_; | 173 FontRenderParams render_params = font_render_params_; |
| 155 if (size_delta != 0) { | 174 if (size_delta != 0) { |
| 156 const std::vector<std::string> family_list(1, font_family_); | 175 const std::vector<std::string> family_list(1, font_family_); |
| 157 render_params = GetCustomFontRenderParams( | 176 render_params = GetCustomFontRenderParams( |
| 158 false, &family_list, &new_size, NULL, NULL); | 177 false, &family_list, &new_size, NULL, NULL); |
| 159 } | 178 } |
| 160 | 179 |
| 161 return Font(new PlatformFontPango(typeface, | 180 return Font(new PlatformFontPango(typeface, |
| 162 font_family_, | 181 new_family, |
| 163 new_size, | 182 new_size, |
| 164 style, | 183 style, |
| 165 render_params)); | 184 render_params)); |
| 166 } | 185 } |
| 167 | 186 |
| 168 int PlatformFontPango::GetHeight() const { | 187 int PlatformFontPango::GetHeight() const { |
| 169 return height_pixels_; | 188 return height_pixels_; |
| 170 } | 189 } |
| 171 | 190 |
| 172 int PlatformFontPango::GetBaseline() const { | 191 int PlatformFontPango::GetBaseline() const { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 PlatformFontPango::~PlatformFontPango() {} | 266 PlatformFontPango::~PlatformFontPango() {} |
| 248 | 267 |
| 249 void PlatformFontPango::InitFromDetails( | 268 void PlatformFontPango::InitFromDetails( |
| 250 const skia::RefPtr<SkTypeface>& typeface, | 269 const skia::RefPtr<SkTypeface>& typeface, |
| 251 const std::string& font_family, | 270 const std::string& font_family, |
| 252 int font_size_pixels, | 271 int font_size_pixels, |
| 253 int style, | 272 int style, |
| 254 const FontRenderParams& render_params) { | 273 const FontRenderParams& render_params) { |
| 255 DCHECK_GT(font_size_pixels, 0); | 274 DCHECK_GT(font_size_pixels, 0); |
| 256 | 275 |
| 257 typeface_ = typeface; | |
| 258 font_family_ = font_family; | 276 font_family_ = font_family; |
| 259 if (!typeface_) { | 277 typeface_ = typeface ? typeface : CreateSkTypeface(style, &font_family_); |
| 260 typeface_ = skia::AdoptRef( | |
| 261 SkTypeface::CreateFromName(font_family.c_str(), SkTypeface::kNormal)); | |
| 262 if (!typeface_) { | |
| 263 // A non-scalable font such as .pcf is specified. Fall back to a default | |
| 264 // scalable font. | |
| 265 typeface_ = skia::AdoptRef(SkTypeface::CreateFromName( | |
| 266 kFallbackFontFamilyName, SkTypeface::kNormal)); | |
| 267 CHECK(typeface_) << "Could not find any font: " << font_family << ", " | |
| 268 << kFallbackFontFamilyName; | |
| 269 font_family_ = kFallbackFontFamilyName; | |
| 270 } | |
| 271 } | |
| 272 | 278 |
| 273 font_size_pixels_ = font_size_pixels; | 279 font_size_pixels_ = font_size_pixels; |
| 274 style_ = style; | 280 style_ = style; |
| 275 font_render_params_ = render_params; | 281 font_render_params_ = render_params; |
| 276 | 282 |
| 277 SkPaint paint; | 283 SkPaint paint; |
| 278 SkPaint::FontMetrics metrics; | 284 SkPaint::FontMetrics metrics; |
| 279 PaintSetup(&paint); | 285 PaintSetup(&paint); |
| 280 paint.getFontMetrics(&metrics); | 286 paint.getFontMetrics(&metrics); |
| 281 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); | 287 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 return new PlatformFontPango(native_font); | 372 return new PlatformFontPango(native_font); |
| 367 } | 373 } |
| 368 | 374 |
| 369 // static | 375 // static |
| 370 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, | 376 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, |
| 371 int font_size) { | 377 int font_size) { |
| 372 return new PlatformFontPango(font_name, font_size); | 378 return new PlatformFontPango(font_name, font_size); |
| 373 } | 379 } |
| 374 | 380 |
| 375 } // namespace gfx | 381 } // namespace gfx |
| OLD | NEW |