Chromium Code Reviews| Index: ui/gfx/render_text_linux.cc |
| =================================================================== |
| --- ui/gfx/render_text_linux.cc (revision 113213) |
| +++ ui/gfx/render_text_linux.cc (working copy) |
| @@ -6,10 +6,12 @@ |
| #include <pango/pangocairo.h> |
| #include <algorithm> |
| +#include <vector> |
| #include "base/i18n/break_iterator.h" |
| #include "base/logging.h" |
| #include "ui/gfx/canvas_skia.h" |
| +#include "ui/gfx/font.h" |
| #include "ui/gfx/pango_util.h" |
| #include "unicode/uchar.h" |
| #include "unicode/ustring.h" |
| @@ -260,27 +262,104 @@ |
| } |
| void RenderTextLinux::DrawVisualText(Canvas* canvas) { |
| - Rect bounds(display_rect()); |
| + DCHECK(layout_); |
| - // Clip the canvas to the text display area. |
| - SkCanvas* canvas_skia = canvas->GetSkCanvas(); |
| + Point offset(GetVisualOrigin()); |
| + SkScalar x = SkIntToScalar(offset.x()); |
| + SkScalar y = SkIntToScalar(offset.y()); |
| - skia::ScopedPlatformPaint scoped_platform_paint(canvas_skia); |
| - cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
| - cairo_save(cr); |
| - cairo_rectangle(cr, bounds.x(), bounds.y(), bounds.width(), bounds.height()); |
| - cairo_clip(cr); |
| - int text_width, text_height; |
| - pango_layout_get_pixel_size(layout_, &text_width, &text_height); |
| - Point offset(ToViewPoint(Point())); |
| - // Vertically centered. |
| - int text_y = offset.y() + ((bounds.height() - text_height) / 2); |
| - // TODO(xji): need to use SkCanvas->drawPosText() for gpu acceleration. |
| - cairo_move_to(cr, offset.x(), text_y); |
| - pango_cairo_show_layout(cr, layout_); |
| + std::vector<SkPoint> pos; |
| + std::vector<uint16> glyphs; |
| - cairo_restore(cr); |
| + StyleRanges styles(style_ranges()); |
| + ApplyCompositionAndSelectionStyles(&styles); |
| + |
| + // Pre-calculate UTF16 indeces style ranges to UTF16 indices. |
|
xji
2011/12/07 00:56:51
to *UTF8* indices.
Alexei Svitkine (slow)
2011/12/07 16:11:21
Done.
|
| + // TODO(asvitkine): Can we cache these? |
| + std::vector<ui::Range> style_ranges_utf8; |
| + style_ranges_utf8.reserve(styles.size()); |
| + size_t start_index = 0; |
| + for (size_t i = 0; i < styles.size(); ++i) { |
| + size_t end_index = Utf16IndexToUtf8Index(styles[i].range.end()); |
| + style_ranges_utf8.push_back(ui::Range(start_index, end_index)); |
| + start_index = end_index; |
| + } |
| + |
| + bool is_rtl = (GetTextDirection() == base::i18n::RIGHT_TO_LEFT); |
|
xji
2011/12/07 00:56:51
this should be run's directionality, not text's.
Alexei Svitkine (slow)
2011/12/07 16:11:21
Done. I've also added a helper function IsRunLTR()
|
| + size_t style_increment = (is_rtl ? -1 : 1); |
| + |
| + internal::SkiaTextRenderer renderer(canvas); |
| + for (GSList* it = current_line_->runs; it; it = it->next) { |
| + PangoLayoutRun* run = reinterpret_cast<PangoLayoutRun*>(it->data); |
| + size_t run_start = run->item->offset; |
| + size_t run_end = run_start + run->item->length; |
| + |
| + // Find the initial style for this run. |
| + // TODO(asvitkine): Can we avoid looping here, e.g. by caching this per run? |
| + int style; |
| + if (is_rtl) { |
|
xji
2011/12/07 00:56:51
sorry for the confusion.
I mean the style initiali
Alexei Svitkine (slow)
2011/12/07 16:11:21
That version of the code was structured in a way t
|
| + style = style_ranges_utf8.size() - 1; |
| + int prev = style - 1; |
| + while (prev >= 0 && style_ranges_utf8[prev].end() >= run_end) |
| + style = prev--; |
| + } else { |
| + style = 0; |
| + size_t next = style + 1; |
| + while (next < styles.size() && |
| + style_ranges_utf8[next].start() <= run_start) { |
| + style = next++; |
| + } |
| + } |
| + |
| + int glyph_count = run->glyphs->num_glyphs; |
| + glyphs.resize(glyph_count); |
| + pos.resize(glyph_count); |
| + |
| + PangoFontDescription* native_font = |
| + pango_font_describe(run->item->analysis.font); |
| + renderer.SetFont(gfx::Font(native_font)); |
| + pango_font_description_free(native_font); |
| + |
| + SkScalar glyph_x = x; |
| + SkScalar start_x = x; |
| + int start = 0; |
| + for (int i = 0; i < glyph_count; ++i) { |
| + const PangoGlyphInfo& glyph = run->glyphs->glyphs[i]; |
| + glyphs[i] = static_cast<uint16>(glyph.glyph); |
| + pos[i].set(glyph_x + PANGO_PIXELS(glyph.geometry.x_offset), |
| + y + PANGO_PIXELS(glyph.geometry.y_offset)); |
| + glyph_x += PANGO_PIXELS(glyph.geometry.width); |
| + |
| + // If this glyph is beyond the current style, draw the glyphs so far and |
| + // advance to the next style. |
| + size_t glyph_byte_index = run_start + run->glyphs->log_clusters[i]; |
| + if (!style_ranges_utf8[style].Contains(ui::Range(glyph_byte_index))) { |
|
xji
2011/12/07 00:56:51
isn't style_range's end exclusive? Then Contains()
Alexei Svitkine (slow)
2011/12/07 16:11:21
You're right! Fixed.
|
| + renderer.SetForegroundColor(styles[style].foreground); |
| + renderer.DrawPosText(&pos[start], &glyphs[start], i - start); |
| + if (styles[style].underline || styles[style].strike) { |
| + renderer.DrawDecorations(start_x, y, glyph_x - start_x, |
| + styles[style].underline, |
| + styles[style].strike); |
| + } |
| + |
| + style += style_increment; |
| + start = i; |
| + start_x = glyph_x; |
| + } |
| + } |
| + |
| + // Draw the remaining glyphs. |
| + renderer.SetForegroundColor(styles[style].foreground); |
| + renderer.DrawPosText(&pos[start], &glyphs[start], glyph_count - start); |
| + if (styles[style].underline || styles[style].strike) { |
| + renderer.DrawDecorations(start_x, y, glyph_x - start_x, |
| + styles[style].underline, |
| + styles[style].strike); |
| + } |
| + |
| + x = glyph_x; |
| + } |
| } |
| size_t RenderTextLinux::IndexOfAdjacentGrapheme(size_t index, bool next) { |