Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ui/gfx/platform_font_pango.cc

Issue 509093004: linux: Make RenderTextPango avoid clipping underlines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 PlatformFontPango::PlatformFontPango(const std::string& font_name, 123 PlatformFontPango::PlatformFontPango(const std::string& font_name,
124 int font_size_pixels) { 124 int font_size_pixels) {
125 FontRenderParamsQuery query(false); 125 FontRenderParamsQuery query(false);
126 query.families.push_back(font_name); 126 query.families.push_back(font_name);
127 query.pixel_size = font_size_pixels; 127 query.pixel_size = font_size_pixels;
128 query.style = gfx::Font::NORMAL; 128 query.style = gfx::Font::NORMAL;
129 InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels, 129 InitFromDetails(skia::RefPtr<SkTypeface>(), font_name, font_size_pixels,
130 query.style, gfx::GetFontRenderParams(query, NULL)); 130 query.style, gfx::GetFontRenderParams(query, NULL));
131 } 131 }
132 132
133 double PlatformFontPango::underline_position() const {
134 const_cast<PlatformFontPango*>(this)->InitPangoMetrics();
135 return underline_position_pixels_;
136 }
137
138 double PlatformFontPango::underline_thickness() const {
139 const_cast<PlatformFontPango*>(this)->InitPangoMetrics();
140 return underline_thickness_pixels_;
141 }
142
143 //////////////////////////////////////////////////////////////////////////////// 133 ////////////////////////////////////////////////////////////////////////////////
144 // PlatformFontPango, PlatformFont implementation: 134 // PlatformFontPango, PlatformFont implementation:
145 135
146 // static 136 // static
147 void PlatformFontPango::ReloadDefaultFont() { 137 void PlatformFontPango::ReloadDefaultFont() {
148 delete default_font_; 138 delete default_font_;
149 default_font_ = NULL; 139 default_font_ = NULL;
150 } 140 }
151 141
152 #if defined(OS_CHROMEOS) 142 #if defined(OS_CHROMEOS)
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 SkPaint paint; 266 SkPaint paint;
277 SkPaint::FontMetrics metrics; 267 SkPaint::FontMetrics metrics;
278 PaintSetup(&paint); 268 PaintSetup(&paint);
279 paint.getFontMetrics(&metrics); 269 paint.getFontMetrics(&metrics);
280 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent); 270 ascent_pixels_ = SkScalarCeilToInt(-metrics.fAscent);
281 height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent); 271 height_pixels_ = ascent_pixels_ + SkScalarCeilToInt(metrics.fDescent);
282 cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight); 272 cap_height_pixels_ = SkScalarCeilToInt(metrics.fCapHeight);
283 273
284 pango_metrics_inited_ = false; 274 pango_metrics_inited_ = false;
285 average_width_pixels_ = 0.0f; 275 average_width_pixels_ = 0.0f;
286 underline_position_pixels_ = 0.0f;
287 underline_thickness_pixels_ = 0.0f;
288 } 276 }
289 277
290 void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) { 278 void PlatformFontPango::InitFromPlatformFont(const PlatformFontPango* other) {
291 typeface_ = other->typeface_; 279 typeface_ = other->typeface_;
292 font_family_ = other->font_family_; 280 font_family_ = other->font_family_;
293 font_size_pixels_ = other->font_size_pixels_; 281 font_size_pixels_ = other->font_size_pixels_;
294 style_ = other->style_; 282 style_ = other->style_;
295 font_render_params_ = other->font_render_params_; 283 font_render_params_ = other->font_render_params_;
296 ascent_pixels_ = other->ascent_pixels_; 284 ascent_pixels_ = other->ascent_pixels_;
297 height_pixels_ = other->height_pixels_; 285 height_pixels_ = other->height_pixels_;
298 cap_height_pixels_ = other->cap_height_pixels_; 286 cap_height_pixels_ = other->cap_height_pixels_;
299 pango_metrics_inited_ = other->pango_metrics_inited_; 287 pango_metrics_inited_ = other->pango_metrics_inited_;
300 average_width_pixels_ = other->average_width_pixels_; 288 average_width_pixels_ = other->average_width_pixels_;
301 underline_position_pixels_ = other->underline_position_pixels_;
302 underline_thickness_pixels_ = other->underline_thickness_pixels_;
303 } 289 }
304 290
305 void PlatformFontPango::PaintSetup(SkPaint* paint) const { 291 void PlatformFontPango::PaintSetup(SkPaint* paint) const {
306 paint->setAntiAlias(false); 292 paint->setAntiAlias(false);
307 paint->setSubpixelText(false); 293 paint->setSubpixelText(false);
308 paint->setTextSize(font_size_pixels_); 294 paint->setTextSize(font_size_pixels_);
309 paint->setTypeface(typeface_.get()); 295 paint->setTypeface(typeface_.get());
310 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold()); 296 paint->setFakeBoldText((gfx::Font::BOLD & style_) && !typeface_->isBold());
311 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ? 297 paint->setTextSkewX((gfx::Font::ITALIC & style_) && !typeface_->isItalic() ?
312 -SK_Scalar1/4 : 0); 298 -SK_Scalar1/4 : 0);
313 } 299 }
314 300
315 void PlatformFontPango::InitPangoMetrics() { 301 void PlatformFontPango::InitPangoMetrics() {
316 if (!pango_metrics_inited_) { 302 if (!pango_metrics_inited_) {
317 pango_metrics_inited_ = true; 303 pango_metrics_inited_ = true;
318 ScopedPangoFontDescription pango_desc(GetNativeFont()); 304 ScopedPangoFontDescription pango_desc(GetNativeFont());
319 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get()); 305 PangoFontMetrics* pango_metrics = GetPangoFontMetrics(pango_desc.get());
320 306
321 underline_position_pixels_ =
322 pango_font_metrics_get_underline_position(pango_metrics) /
323 PANGO_SCALE;
324
325 // TODO(davemoore): Come up with a better solution.
326 // This is a hack, but without doing this the underlines
327 // we get end up fuzzy. So we align to the midpoint of a pixel.
328 underline_position_pixels_ /= 2;
329
330 underline_thickness_pixels_ =
331 pango_font_metrics_get_underline_thickness(pango_metrics) /
332 PANGO_SCALE;
333
334 // First get the Pango-based width (converting from Pango units to pixels). 307 // First get the Pango-based width (converting from Pango units to pixels).
335 const double pango_width_pixels = 308 const double pango_width_pixels =
336 pango_font_metrics_get_approximate_char_width(pango_metrics) / 309 pango_font_metrics_get_approximate_char_width(pango_metrics) /
337 PANGO_SCALE; 310 PANGO_SCALE;
338 311
339 // Yes, this is how Microsoft recommends calculating the dialog unit 312 // Yes, this is how Microsoft recommends calculating the dialog unit
340 // conversions. 313 // conversions.
341 const int text_width_pixels = GetStringWidth( 314 const int text_width_pixels = GetStringWidth(
342 base::ASCIIToUTF16( 315 base::ASCIIToUTF16(
343 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), 316 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
(...skipping 21 matching lines...) Expand all
365 return new PlatformFontPango(native_font); 338 return new PlatformFontPango(native_font);
366 } 339 }
367 340
368 // static 341 // static
369 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name, 342 PlatformFont* PlatformFont::CreateFromNameAndSize(const std::string& font_name,
370 int font_size) { 343 int font_size) {
371 return new PlatformFontPango(font_name, font_size); 344 return new PlatformFontPango(font_name, font_size);
372 } 345 }
373 346
374 } // namespace gfx 347 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698