| 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 |
| 93 int round(float value) { | 103 int round(float value) { |
| 94 return static_cast<int>(floor(value + 0.5f)); | 104 return static_cast<int>(floor(value + 0.5f)); |
| 95 } | 105 } |
| 96 | 106 |
| 97 // Given |font| and |display_width|, returns the width of the fade gradient. | 107 // Given |font| and |display_width|, returns the width of the fade gradient. |
| 98 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { | 108 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { |
| 99 // Fade in/out about 3 characters of the beginning/end of the string. | 109 // Fade in/out about 3 characters of the beginning/end of the string. |
| 100 // Use a 1/3 of the display width if the display width is very short. | 110 // Use a 1/3 of the display width if the display width is very short. |
| 101 const int narrow_width = font_list.GetExpectedTextWidth(3); | 111 const int narrow_width = font_list.GetExpectedTextWidth(3); |
| 102 const int gradient_width = std::min(narrow_width, round(display_width / 3.f)); | 112 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... |
| 240 } | 250 } |
| 241 | 251 |
| 242 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { | 252 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { |
| 243 paint_.setTypeface(typeface); | 253 paint_.setTypeface(typeface); |
| 244 } | 254 } |
| 245 | 255 |
| 246 void SkiaTextRenderer::SetTextSize(SkScalar size) { | 256 void SkiaTextRenderer::SetTextSize(SkScalar size) { |
| 247 paint_.setTextSize(size); | 257 paint_.setTextSize(size); |
| 248 } | 258 } |
| 249 | 259 |
| 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 |
| 250 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 272 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 251 paint_.setColor(foreground); | 273 paint_.setColor(foreground); |
| 252 } | 274 } |
| 253 | 275 |
| 254 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { | 276 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { |
| 255 paint_.setShader(std::move(shader)); | 277 paint_.setShader(std::move(shader)); |
| 256 } | 278 } |
| 257 | 279 |
| 258 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 280 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
| 259 SkScalar position) { | 281 SkScalar position) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 LineSegment::LineSegment() : run(0) {} | 416 LineSegment::LineSegment() : run(0) {} |
| 395 | 417 |
| 396 LineSegment::~LineSegment() {} | 418 LineSegment::~LineSegment() {} |
| 397 | 419 |
| 398 Line::Line() : preceding_heights(0), baseline(0) {} | 420 Line::Line() : preceding_heights(0), baseline(0) {} |
| 399 | 421 |
| 400 Line::Line(const Line& other) = default; | 422 Line::Line(const Line& other) = default; |
| 401 | 423 |
| 402 Line::~Line() {} | 424 Line::~Line() {} |
| 403 | 425 |
| 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 |
| 404 void ApplyRenderParams(const FontRenderParams& params, | 434 void ApplyRenderParams(const FontRenderParams& params, |
| 405 bool subpixel_rendering_suppressed, | 435 bool subpixel_rendering_suppressed, |
| 406 SkPaint* paint) { | 436 SkPaint* paint) { |
| 407 paint->setAntiAlias(params.antialiasing); | 437 paint->setAntiAlias(params.antialiasing); |
| 408 paint->setLCDRenderText(!subpixel_rendering_suppressed && | 438 paint->setLCDRenderText(!subpixel_rendering_suppressed && |
| 409 params.subpixel_rendering != FontRenderParams::SUBPIXEL_RENDERING_NONE); | 439 params.subpixel_rendering != FontRenderParams::SUBPIXEL_RENDERING_NONE); |
| 410 paint->setSubpixelText(params.subpixel_positioning); | 440 paint->setSubpixelText(params.subpixel_positioning); |
| 411 paint->setAutohinted(params.autohinter); | 441 paint->setAutohinted(params.autohinter); |
| 412 paint->setHinting(FontRenderParamsHintingToSkPaintHinting(params.hinting)); | 442 paint->setHinting(FontRenderParamsHintingToSkPaintHinting(params.hinting)); |
| 413 } | 443 } |
| (...skipping 1101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1515 | 1545 |
| 1516 SetDisplayOffset(display_offset_.x() + delta_x); | 1546 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1517 } | 1547 } |
| 1518 | 1548 |
| 1519 void RenderText::DrawSelection(Canvas* canvas) { | 1549 void RenderText::DrawSelection(Canvas* canvas) { |
| 1520 for (const Rect& s : GetSubstringBounds(selection())) | 1550 for (const Rect& s : GetSubstringBounds(selection())) |
| 1521 canvas->FillRect(s, selection_background_focused_color_); | 1551 canvas->FillRect(s, selection_background_focused_color_); |
| 1522 } | 1552 } |
| 1523 | 1553 |
| 1524 } // namespace gfx | 1554 } // namespace gfx |
| OLD | NEW |