| 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/render_text.h" | 5 #include "ui/gfx/render_text.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <climits> | 10 #include <climits> |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // Some platforms don't support getting the cap height, and simply return | 83 // Some platforms don't support getting the cap height, and simply return |
| 84 // the entire font ascent from GetCapHeight(). Centering the ascent makes | 84 // the entire font ascent from GetCapHeight(). Centering the ascent makes |
| 85 // the font look too low, so if GetCapHeight() returns the ascent, center | 85 // the font look too low, so if GetCapHeight() returns the ascent, center |
| 86 // the entire font height instead. | 86 // the entire font height instead. |
| 87 const int space = | 87 const int space = |
| 88 display_height - ((internal_leading != 0) ? cap_height : font_height); | 88 display_height - ((internal_leading != 0) ? cap_height : font_height); |
| 89 const int baseline_shift = space / 2 - internal_leading; | 89 const int baseline_shift = space / 2 - internal_leading; |
| 90 return baseline + std::max(min_shift, std::min(max_shift, baseline_shift)); | 90 return baseline + std::max(min_shift, std::min(max_shift, baseline_shift)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 #if !defined(OS_MACOSX) | |
| 94 // Converts |Font::FontStyle| flags to |SkTypeface::Style| flags. | |
| 95 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { | |
| 96 int skia_style = SkTypeface::kNormal; | |
| 97 skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0; | |
| 98 skia_style |= (font_style & Font::ITALIC) ? SkTypeface::kItalic : 0; | |
| 99 return static_cast<SkTypeface::Style>(skia_style); | |
| 100 } | |
| 101 #endif | |
| 102 | |
| 103 int round(float value) { | 93 int round(float value) { |
| 104 return static_cast<int>(floor(value + 0.5f)); | 94 return static_cast<int>(floor(value + 0.5f)); |
| 105 } | 95 } |
| 106 | 96 |
| 107 // Given |font| and |display_width|, returns the width of the fade gradient. | 97 // Given |font| and |display_width|, returns the width of the fade gradient. |
| 108 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { | 98 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { |
| 109 // Fade in/out about 3 characters of the beginning/end of the string. | 99 // Fade in/out about 3 characters of the beginning/end of the string. |
| 110 // Use a 1/3 of the display width if the display width is very short. | 100 // Use a 1/3 of the display width if the display width is very short. |
| 111 const int narrow_width = font_list.GetExpectedTextWidth(3); | 101 const int narrow_width = font_list.GetExpectedTextWidth(3); |
| 112 const int gradient_width = std::min(narrow_width, round(display_width / 3.f)); | 102 const int gradient_width = std::min(narrow_width, round(display_width / 3.f)); |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 240 } |
| 251 | 241 |
| 252 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { | 242 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { |
| 253 paint_.setTypeface(typeface); | 243 paint_.setTypeface(typeface); |
| 254 } | 244 } |
| 255 | 245 |
| 256 void SkiaTextRenderer::SetTextSize(SkScalar size) { | 246 void SkiaTextRenderer::SetTextSize(SkScalar size) { |
| 257 paint_.setTextSize(size); | 247 paint_.setTextSize(size); |
| 258 } | 248 } |
| 259 | 249 |
| 260 void SkiaTextRenderer::SetFontWithStyle(const Font& font, int style) { | |
| 261 skia::RefPtr<SkTypeface> typeface = CreateSkiaTypeface(font, style); | |
| 262 if (typeface) { | |
| 263 // |paint_| adds its own ref. So don't |release()| it from the ref ptr here. | |
| 264 SetTypeface(typeface.get()); | |
| 265 | |
| 266 // Enable fake bold text if bold style is needed but new typeface does not | |
| 267 // have it. | |
| 268 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 250 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 273 paint_.setColor(foreground); | 251 paint_.setColor(foreground); |
| 274 } | 252 } |
| 275 | 253 |
| 276 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { | 254 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { |
| 277 paint_.setShader(std::move(shader)); | 255 paint_.setShader(std::move(shader)); |
| 278 } | 256 } |
| 279 | 257 |
| 280 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 258 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
| 281 SkScalar position) { | 259 SkScalar position) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 LineSegment::LineSegment() : run(0) {} | 394 LineSegment::LineSegment() : run(0) {} |
| 417 | 395 |
| 418 LineSegment::~LineSegment() {} | 396 LineSegment::~LineSegment() {} |
| 419 | 397 |
| 420 Line::Line() : preceding_heights(0), baseline(0) {} | 398 Line::Line() : preceding_heights(0), baseline(0) {} |
| 421 | 399 |
| 422 Line::Line(const Line& other) = default; | 400 Line::Line(const Line& other) = default; |
| 423 | 401 |
| 424 Line::~Line() {} | 402 Line::~Line() {} |
| 425 | 403 |
| 426 #if !defined(OS_MACOSX) | |
| 427 skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, int style) { | |
| 428 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); | |
| 429 return skia::AdoptRef( | |
| 430 SkTypeface::CreateFromName(font.GetFontName().c_str(), skia_style)); | |
| 431 } | |
| 432 #endif | |
| 433 | |
| 434 void ApplyRenderParams(const FontRenderParams& params, | 404 void ApplyRenderParams(const FontRenderParams& params, |
| 435 bool subpixel_rendering_suppressed, | 405 bool subpixel_rendering_suppressed, |
| 436 SkPaint* paint) { | 406 SkPaint* paint) { |
| 437 paint->setAntiAlias(params.antialiasing); | 407 paint->setAntiAlias(params.antialiasing); |
| 438 paint->setLCDRenderText(!subpixel_rendering_suppressed && | 408 paint->setLCDRenderText(!subpixel_rendering_suppressed && |
| 439 params.subpixel_rendering != FontRenderParams::SUBPIXEL_RENDERING_NONE); | 409 params.subpixel_rendering != FontRenderParams::SUBPIXEL_RENDERING_NONE); |
| 440 paint->setSubpixelText(params.subpixel_positioning); | 410 paint->setSubpixelText(params.subpixel_positioning); |
| 441 paint->setAutohinted(params.autohinter); | 411 paint->setAutohinted(params.autohinter); |
| 442 paint->setHinting(FontRenderParamsHintingToSkPaintHinting(params.hinting)); | 412 paint->setHinting(FontRenderParamsHintingToSkPaintHinting(params.hinting)); |
| 443 } | 413 } |
| (...skipping 1101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1545 | 1515 |
| 1546 SetDisplayOffset(display_offset_.x() + delta_x); | 1516 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1547 } | 1517 } |
| 1548 | 1518 |
| 1549 void RenderText::DrawSelection(Canvas* canvas) { | 1519 void RenderText::DrawSelection(Canvas* canvas) { |
| 1550 for (const Rect& s : GetSubstringBounds(selection())) | 1520 for (const Rect& s : GetSubstringBounds(selection())) |
| 1551 canvas->FillRect(s, selection_background_focused_color_); | 1521 canvas->FillRect(s, selection_background_focused_color_); |
| 1552 } | 1522 } |
| 1553 | 1523 |
| 1554 } // namespace gfx | 1524 } // namespace gfx |
| OLD | NEW |