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

Unified Diff: ui/gfx/canvas_skia.cc

Issue 22835002: Supports gfx::FontList in gfx::Canvas and ui::ElideText family. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updates based on review comments. Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/canvas_skia.cc
diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc
index bb8ce7db5e5ed575d5f82e175828e3e6b7a36169..b9ffc1ff33852464de68d061d4540b6eb17b6aa2 100644
--- a/ui/gfx/canvas_skia.cc
+++ b/ui/gfx/canvas_skia.cc
@@ -9,7 +9,6 @@
#include "base/memory/scoped_ptr.h"
#include "ui/base/range/range.h"
#include "ui/base/text/text_elider.h"
-#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/rect.h"
@@ -98,13 +97,13 @@ ui::Range StripAcceleratorChars(int flags, base::string16* text) {
// Elides |text| and adjusts |range| appropriately. If eliding causes |range|
// to no longer point to the same character in |text|, |range| is made invalid.
-void ElideTextAndAdjustRange(const Font& font,
+void ElideTextAndAdjustRange(const FontList& font_list,
int width,
base::string16* text,
ui::Range* range) {
const base::char16 start_char =
(range->IsValid() ? text->at(range->start()) : 0);
- *text = ui::ElideText(*text, font, width, ui::ELIDE_AT_END);
+ *text = ui::ElideText(*text, font_list, width, ui::ELIDE_AT_END);
if (!range->IsValid())
return;
if (range->start() >= text->length() ||
@@ -116,16 +115,16 @@ void ElideTextAndAdjustRange(const Font& font,
// Updates |render_text| from the specified parameters.
void UpdateRenderText(const Rect& rect,
const base::string16& text,
- const Font& font,
+ const FontList& font_list,
int flags,
SkColor color,
RenderText* render_text) {
- render_text->SetFont(font);
+ render_text->SetFontList(font_list);
render_text->SetText(text);
render_text->SetCursorEnabled(false);
Rect display_rect = rect;
- display_rect.set_height(font.GetHeight());
+ display_rect.set_height(font_list.GetHeight());
render_text->SetDisplayRect(display_rect);
// Set the text alignment explicitly based on the directionality of the UI,
@@ -147,9 +146,10 @@ void UpdateRenderText(const Rect& rect,
render_text->set_background_is_transparent(true);
render_text->SetColor(color);
- render_text->SetStyle(BOLD, (font.GetStyle() & Font::BOLD) != 0);
- render_text->SetStyle(ITALIC, (font.GetStyle() & Font::ITALIC) != 0);
- render_text->SetStyle(UNDERLINE, (font.GetStyle() & Font::UNDERLINE) != 0);
+ const int font_style = font_list.GetFontStyle();
+ render_text->SetStyle(BOLD, (font_style & Font::BOLD) != 0);
+ render_text->SetStyle(ITALIC, (font_style & Font::ITALIC) != 0);
+ render_text->SetStyle(UNDERLINE, (font_style & Font::UNDERLINE) != 0);
}
// Returns updated |flags| to match platform-specific expected behavior.
@@ -168,7 +168,7 @@ int AdjustPlatformSpecificFlags(const base::string16& text, int flags) {
// static
void Canvas::SizeStringInt(const base::string16& text,
- const Font& font,
+ const FontList& font_list,
int* width, int* height,
int line_height,
int flags) {
@@ -191,17 +191,19 @@ void Canvas::SizeStringInt(const base::string16& text,
Rect rect(*width, INT_MAX);
std::vector<base::string16> strings;
- ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(),
+ ui::ElideRectangleText(adjusted_text, font_list,
+ rect.width(), rect.height(),
wrap_behavior, &strings);
scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
- UpdateRenderText(rect, base::string16(), font, flags, 0, render_text.get());
+ UpdateRenderText(rect, base::string16(), font_list, flags, 0,
+ render_text.get());
int h = 0;
int w = 0;
for (size_t i = 0; i < strings.size(); ++i) {
StripAcceleratorChars(flags, &strings[i]);
render_text->SetText(strings[i]);
- const Size string_size = render_text->GetStringSize();
+ const Size& string_size = render_text->GetStringSize();
msw 2013/08/13 17:39:22 nit: I'd feel safer leaving this a copy, not a ref
Yuki 2013/08/14 15:42:16 This is not harmful. This is safe and potentially
w = std::max(w, string_size.width());
h += (i > 0 && line_height > 0) ? line_height : string_size.height();
}
@@ -212,14 +214,15 @@ void Canvas::SizeStringInt(const base::string16& text,
// will inexplicably fail with result E_INVALIDARG. Guard against this.
const size_t kMaxRenderTextLength = 5000;
if (adjusted_text.length() >= kMaxRenderTextLength) {
- *width = adjusted_text.length() * font.GetAverageCharacterWidth();
- *height = font.GetHeight();
+ *width = font_list.GetExpectedTextWidth(adjusted_text.length());
+ *height = font_list.GetHeight();
} else {
scoped_ptr<RenderText> render_text(RenderText::CreateInstance());
Rect rect(*width, *height);
StripAcceleratorChars(flags, &adjusted_text);
- UpdateRenderText(rect, adjusted_text, font, flags, 0, render_text.get());
- const Size string_size = render_text->GetStringSize();
+ UpdateRenderText(rect, adjusted_text, font_list, flags, 0,
+ render_text.get());
+ const Size& string_size = render_text->GetStringSize();
msw 2013/08/13 17:39:22 ditto ref nit.
Yuki 2013/08/14 15:42:16 Done.
*width = string_size.width();
*height = string_size.height();
}
@@ -227,7 +230,7 @@ void Canvas::SizeStringInt(const base::string16& text,
}
void Canvas::DrawStringWithShadows(const base::string16& text,
- const Font& font,
+ const FontList& font_list,
SkColor color,
const Rect& text_bounds,
int line_height,
@@ -263,14 +266,15 @@ void Canvas::DrawStringWithShadows(const base::string16& text,
std::vector<base::string16> strings;
ui::ElideRectangleText(adjusted_text,
- font,
+ font_list,
text_bounds.width(), text_bounds.height(),
wrap_behavior,
&strings);
for (size_t i = 0; i < strings.size(); i++) {
ui::Range range = StripAcceleratorChars(flags, &strings[i]);
- UpdateRenderText(rect, strings[i], font, flags, color, render_text.get());
+ UpdateRenderText(rect, strings[i], font_list, flags, color,
+ render_text.get());
int line_padding = 0;
if (line_height > 0)
line_padding = line_height - render_text->GetStringSize().height();
@@ -311,13 +315,13 @@ void Canvas::DrawStringWithShadows(const base::string16& text,
#endif
if (elide_text) {
- ElideTextAndAdjustRange(font,
+ ElideTextAndAdjustRange(font_list,
text_bounds.width(),
&adjusted_text,
&range);
}
- UpdateRenderText(rect, adjusted_text, font, flags, color,
+ UpdateRenderText(rect, adjusted_text, font_list, flags, color,
render_text.get());
const int text_height = render_text->GetStringSize().height();
@@ -334,10 +338,10 @@ void Canvas::DrawStringWithShadows(const base::string16& text,
}
void Canvas::DrawStringWithHalo(const base::string16& text,
- const Font& font,
+ const FontList& font_list,
SkColor text_color,
SkColor halo_color_in,
- int x, int y, int w, int h,
+ const Rect& display_rect,
int flags) {
// Some callers will have semitransparent halo colors, which we don't handle
// (since the resulting image can have 1-bit transparency only).
@@ -345,7 +349,7 @@ void Canvas::DrawStringWithHalo(const base::string16& text,
// Create a temporary buffer filled with the halo color. It must leave room
// for the 1-pixel border around the text.
- Size size(w + 2, h + 2);
+ Size size(display_rect.width() + 2, display_rect.height() + 2);
Canvas text_canvas(size, scale_factor(), true);
SkPaint bkgnd_paint;
bkgnd_paint.setColor(halo_color);
@@ -353,7 +357,9 @@ void Canvas::DrawStringWithHalo(const base::string16& text,
// Draw the text into the temporary buffer. This will have correct
// ClearType since the background color is the same as the halo color.
- text_canvas.DrawStringInt(text, font, text_color, 1, 1, w, h, flags);
+ text_canvas.DrawStringWithFlags(
+ text, font_list, text_color,
+ Rect(1, 1, display_rect.width(), display_rect.height()), flags);
uint32_t halo_premul = SkPreMultiplyColor(halo_color);
SkBitmap& text_bitmap = const_cast<SkBitmap&>(
@@ -376,22 +382,21 @@ void Canvas::DrawStringWithHalo(const base::string16& text,
// Draw the halo bitmap with blur.
ImageSkia text_image = ImageSkia(ImageSkiaRep(text_bitmap,
text_canvas.scale_factor()));
- DrawImageInt(text_image, x - 1, y - 1);
+ DrawImageInt(text_image, display_rect.x() - 1, display_rect.y() - 1);
}
void Canvas::DrawFadeTruncatingString(
const base::string16& text,
TruncateFadeMode truncate_mode,
size_t desired_characters_to_truncate_from_head,
- const Font& font,
+ const FontList& font_list,
SkColor color,
const Rect& display_rect) {
int flags = NO_ELLIPSIS;
// If the whole string fits in the destination then just draw it directly.
- if (GetStringWidth(text, font) <= display_rect.width()) {
- DrawStringInt(text, font, color, display_rect.x(), display_rect.y(),
- display_rect.width(), display_rect.height(), flags);
+ if (GetStringWidth(text, font_list) <= display_rect.width()) {
+ DrawStringWithFlags(text, font_list, color, display_rect, flags);
return;
}
@@ -437,7 +442,8 @@ void Canvas::DrawFadeTruncatingString(
flags |= TEXT_ALIGN_LEFT;
Rect rect = display_rect;
- UpdateRenderText(rect, clipped_text, font, flags, color, render_text.get());
+ UpdateRenderText(rect, clipped_text, font_list, flags, color,
+ render_text.get());
const int line_height = render_text->GetStringSize().height();
// Center the text vertically.

Powered by Google App Engine
This is Rietveld 408576698