diff options
Diffstat (limited to 'chromium/ui/gfx/canvas_skia.cc')
-rw-r--r-- | chromium/ui/gfx/canvas_skia.cc | 154 |
1 files changed, 86 insertions, 68 deletions
diff --git a/chromium/ui/gfx/canvas_skia.cc b/chromium/ui/gfx/canvas_skia.cc index bb8ce7db5e5..ef8ee3625a7 100644 --- a/chromium/ui/gfx/canvas_skia.cc +++ b/chromium/ui/gfx/canvas_skia.cc @@ -7,14 +7,13 @@ #include "base/i18n/rtl.h" #include "base/logging.h" #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/range/range.h" #include "ui/gfx/rect.h" #include "ui/gfx/render_text.h" #include "ui/gfx/shadow_value.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/text_utils.h" namespace gfx { @@ -57,9 +56,9 @@ bool AdjustStringDirection(int flags, base::string16* text) { // Checks each pixel immediately adjacent to the given pixel in the bitmap. If // any of them are not the halo color, returns true. This defines the halo of // pixels that will appear around the text. Note that we have to check each -// pixel against both the halo color and transparent since |DrawStringWithHalo| -// will modify the bitmap as it goes, and cleared pixels shouldn't count as -// changed. +// pixel against both the halo color and transparent since +// |DrawStringRectWithHalo| will modify the bitmap as it goes, and cleared +// pixels shouldn't count as changed. bool PixelShouldGetHalo(const SkBitmap& bitmap, int x, int y, SkColor halo_color) { @@ -83,49 +82,49 @@ bool PixelShouldGetHalo(const SkBitmap& bitmap, } // Strips accelerator character prefixes in |text| if needed, based on |flags|. -// Returns a range in |text| to underline or ui::Range::InvalidRange() if +// Returns a range in |text| to underline or gfx::Range::InvalidRange() if // underlining is not needed. -ui::Range StripAcceleratorChars(int flags, base::string16* text) { +Range StripAcceleratorChars(int flags, base::string16* text) { if (flags & (Canvas::SHOW_PREFIX | Canvas::HIDE_PREFIX)) { int char_pos = -1; int char_span = 0; *text = RemoveAcceleratorChar(*text, '&', &char_pos, &char_span); if ((flags & Canvas::SHOW_PREFIX) && char_pos != -1) - return ui::Range(char_pos, char_pos + char_span); + return Range(char_pos, char_pos + char_span); } - return ui::Range::InvalidRange(); + return Range::InvalidRange(); } // 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) { + 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 = gfx::ElideText(*text, font_list, width, gfx::ELIDE_AT_END); if (!range->IsValid()) return; if (range->start() >= text->length() || text->at(range->start()) != start_char) { - *range = ui::Range::InvalidRange(); + *range = Range::InvalidRange(); } } // 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) { @@ -183,25 +183,27 @@ void Canvas::SizeStringInt(const base::string16& text, #endif if ((flags & MULTI_LINE) && *width != 0) { - ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; + gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; if (flags & CHARACTER_BREAK) - wrap_behavior = ui::WRAP_LONG_WORDS; + wrap_behavior = gfx::WRAP_LONG_WORDS; else if (!(flags & NO_ELLIPSIS)) - wrap_behavior = ui::ELIDE_LONG_WORDS; + wrap_behavior = gfx::ELIDE_LONG_WORDS; Rect rect(*width, INT_MAX); std::vector<base::string16> strings; - ui::ElideRectangleText(adjusted_text, font, rect.width(), rect.height(), + gfx::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(); w = std::max(w, string_size.width()); h += (i > 0 && line_height > 0) ? line_height : string_size.height(); } @@ -212,27 +214,28 @@ 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(); *width = string_size.width(); *height = string_size.height(); } } } -void Canvas::DrawStringWithShadows(const base::string16& text, - const Font& font, - SkColor color, - const Rect& text_bounds, - int line_height, - int flags, - const ShadowValues& shadows) { +void Canvas::DrawStringRectWithShadows(const base::string16& text, + const FontList& font_list, + SkColor color, + const Rect& text_bounds, + int line_height, + int flags, + const ShadowValues& shadows) { if (!IntersectsClipRect(text_bounds)) return; @@ -255,22 +258,23 @@ void Canvas::DrawStringWithShadows(const base::string16& text, render_text->SetTextShadows(shadows); if (flags & MULTI_LINE) { - ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; + gfx::WordWrapBehavior wrap_behavior = gfx::IGNORE_LONG_WORDS; if (flags & CHARACTER_BREAK) - wrap_behavior = ui::WRAP_LONG_WORDS; + wrap_behavior = gfx::WRAP_LONG_WORDS; else if (!(flags & NO_ELLIPSIS)) - wrap_behavior = ui::ELIDE_LONG_WORDS; + wrap_behavior = gfx::ELIDE_LONG_WORDS; std::vector<base::string16> strings; - ui::ElideRectangleText(adjusted_text, - font, + gfx::ElideRectangleText(adjusted_text, + 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()); + Range range = StripAcceleratorChars(flags, &strings[i]); + 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(); @@ -295,7 +299,7 @@ void Canvas::DrawStringWithShadows(const base::string16& text, rect += Vector2d(0, line_height); } } else { - ui::Range range = StripAcceleratorChars(flags, &adjusted_text); + Range range = StripAcceleratorChars(flags, &adjusted_text); bool elide_text = ((flags & NO_ELLIPSIS) == 0); #if defined(OS_LINUX) @@ -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(); @@ -333,19 +337,19 @@ void Canvas::DrawStringWithShadows(const base::string16& text, canvas_->restore(); } -void Canvas::DrawStringWithHalo(const base::string16& text, - const Font& font, - SkColor text_color, - SkColor halo_color_in, - int x, int y, int w, int h, - int flags) { +void Canvas::DrawStringRectWithHalo(const base::string16& text, + const FontList& font_list, + SkColor text_color, + SkColor halo_color_in, + 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). SkColor halo_color = SkColorSetA(halo_color_in, 0xFF); // 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.DrawStringRectWithFlags( + 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, - SkColor color, - const Rect& display_rect) { +void Canvas::DrawFadeTruncatingStringRect( + const base::string16& text, + TruncateFadeMode truncate_mode, + size_t desired_characters_to_truncate_from_head, + 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()) { + DrawStringRectWithFlags(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. @@ -451,4 +457,16 @@ void Canvas::DrawFadeTruncatingString( canvas_->restore(); } +void Canvas::DrawFadeTruncatingString( + const base::string16& text, + TruncateFadeMode truncate_mode, + size_t desired_characters_to_truncate_from_head, + const Font& font, + SkColor color, + const Rect& display_rect) { + DrawFadeTruncatingStringRect(text, truncate_mode, + desired_characters_to_truncate_from_head, + FontList(font), color, display_rect); +} + } // namespace gfx |