Chromium Code Reviews| Index: ui/gfx/render_text_harfbuzz.cc |
| diff --git a/ui/gfx/render_text_harfbuzz.cc b/ui/gfx/render_text_harfbuzz.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9a1868aed0f18df419bd46a4b7909a0e13552e67 |
| --- /dev/null |
| +++ b/ui/gfx/render_text_harfbuzz.cc |
| @@ -0,0 +1,711 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/gfx/render_text_harfbuzz.h" |
| + |
| +#include <stdlib.h> |
| +#include <iterator> |
| +#include <map> |
| + |
| +#include "base/i18n/break_iterator.h" |
| +#include "third_party/harfbuzz-ng/src/hb.h" |
| +#include "third_party/icu/source/common/unicode/ubidi.h" |
| +#include "third_party/skia/include/core/SkColor.h" |
| +#include "third_party/skia/include/core/SkTypeface.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/render_text.h" |
| +#include "ui/gfx/utf16_indexing.h" |
| + |
| +namespace gfx { |
| + |
| +namespace { |
| + |
| +// Font data provider for HarfBuzz using Skia. Copied from Blink. |
| +struct HarfBuzzFontData { |
| + HarfBuzzFontData(std::map<uint32_t, uint16_t>* glyph_cache) |
| + : glyph_cache_(glyph_cache) {} |
| + |
| + SkPaint paint_; |
| + std::map<uint32_t, uint16_t>* glyph_cache_; |
| +}; |
| + |
| +hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) { |
| + return SkScalarToFixed(value); |
| +} |
| + |
| +void SkiaGetGlyphWidthAndExtents(SkPaint* paint, |
| + hb_codepoint_t codepoint, |
| + hb_position_t* width, |
| + hb_glyph_extents_t* extents) { |
| + CHECK(codepoint <= 0xFFFF); |
| + paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| + |
| + SkScalar skWidth; |
| + SkRect skBounds; |
| + uint16_t glyph = codepoint; |
| + |
| + paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds); |
| + if (width) |
| + *width = SkiaScalarToHarfBuzzPosition(skWidth); |
| + if (extents) { |
| + // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be |
| + // y-grows-up. |
| + extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft); |
| + extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop); |
| + extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width()); |
| + extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height()); |
| + } |
| +} |
| + |
| +hb_bool_t HarfBuzzGetGlyph(hb_font_t* font, |
| + void* font_data, |
| + hb_codepoint_t unicode, |
| + hb_codepoint_t variation_selector, |
| + hb_codepoint_t* glyph, |
| + void* user_data) { |
| + HarfBuzzFontData* hb_font_data = |
| + reinterpret_cast<HarfBuzzFontData*>(font_data); |
| + std::map<uint32_t, uint16_t>& cache = *hb_font_data->glyph_cache_; |
| + |
| + bool exists = cache.count(unicode) != 0; |
| + if (!exists) { |
| + SkPaint* paint = &hb_font_data->paint_; |
| + paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| + uint16_t glyph16; |
| + paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &glyph16); |
| + cache[unicode] = glyph16; |
| + *glyph = glyph16; |
| + } |
| + *glyph = cache[unicode]; |
| + return !!*glyph; |
| +} |
| + |
| +hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* font, |
| + void* font_data, |
| + hb_codepoint_t glyph, |
| + void* user_data) { |
| + HarfBuzzFontData* hb_font_data = |
| + reinterpret_cast<HarfBuzzFontData*>(font_data); |
| + hb_position_t advance = 0; |
| + |
| + SkiaGetGlyphWidthAndExtents(&hb_font_data->paint_, glyph, &advance, 0); |
| + return advance; |
| +} |
| + |
| +hb_bool_t HarfBuzzGetGlyphHorizontalOrigin(hb_font_t* font, |
| + void* font_data, |
| + hb_codepoint_t glyph, |
| + hb_position_t* x, |
| + hb_position_t* y, |
| + void* user_data) { |
| + // Just return true, following the way that HarfBuzz-FreeType |
| + // implementation does. |
| + return true; |
| +} |
| + |
| +hb_bool_t HarfBuzzGetGlyphExtents(hb_font_t* font, |
| + void* font_data, |
| + hb_codepoint_t glyph, |
| + hb_glyph_extents_t* extents, |
| + void* user_data) { |
| + HarfBuzzFontData* hb_font_data = |
| + reinterpret_cast<HarfBuzzFontData*>(font_data); |
| + |
| + SkiaGetGlyphWidthAndExtents(&hb_font_data->paint_, glyph, 0, extents); |
| + return true; |
| +} |
| + |
| +hb_font_funcs_t* HarfBuzzSkiaGetFontFuncs() { |
| + static hb_font_funcs_t* harfbuzz_skia_font_funcs = 0; |
| + |
| + // We don't set callback functions which we can't support. |
| + // HarfBuzz will use the fallback implementation if they aren't set. |
| + if (!harfbuzz_skia_font_funcs) { |
| + harfbuzz_skia_font_funcs = hb_font_funcs_create(); |
| + hb_font_funcs_set_glyph_func( |
| + harfbuzz_skia_font_funcs, HarfBuzzGetGlyph, 0, 0); |
| + hb_font_funcs_set_glyph_h_advance_func( |
| + harfbuzz_skia_font_funcs, HarfBuzzGetGlyphHorizontalAdvance, 0, 0); |
| + hb_font_funcs_set_glyph_h_origin_func( |
| + harfbuzz_skia_font_funcs, HarfBuzzGetGlyphHorizontalOrigin, 0, 0); |
| + hb_font_funcs_set_glyph_extents_func( |
| + harfbuzz_skia_font_funcs, HarfBuzzGetGlyphExtents, 0, 0); |
| + hb_font_funcs_make_immutable(harfbuzz_skia_font_funcs); |
| + } |
| + return harfbuzz_skia_font_funcs; |
| +} |
| + |
| +hb_blob_t* HarfBuzzSkiaGetTable(hb_face_t* face, |
| + hb_tag_t tag, |
| + void* user_data) { |
| + SkTypeface* typeface = reinterpret_cast<SkTypeface*>(user_data); |
| + |
| + const size_t table_size = typeface->getTableSize(tag); |
| + if (!table_size) |
| + return 0; |
| + |
| + char* buffer = reinterpret_cast<char*>(malloc(table_size)); |
| + if (!buffer) |
| + return 0; |
| + size_t actual_size = typeface->getTableData(tag, 0, table_size, buffer); |
| + if (table_size != actual_size) { |
| + free(buffer); |
| + return 0; |
| + } |
| + |
| + return hb_blob_create(const_cast<char*>(buffer), table_size, |
| + HB_MEMORY_MODE_WRITABLE, buffer, free); |
| +} |
| + |
| +template<typename Type> |
| +void DeleteByType(void* data) { |
| + Type* typed_data = reinterpret_cast<Type*>(data); |
| + delete typed_data; |
| +} |
| + |
| +// TODO(ckocagil): Implement a cache for HarfBuzz faces. SkTypeface provides |
| +// unique face IDs, which we can use as keys. Also see base::MRUCache. |
| +hb_face_t* CreateHarfBuzzFace(SkTypeface* skia_face) { |
| + hb_face_t* face = hb_face_create_for_tables(HarfBuzzSkiaGetTable, skia_face, |
| + 0); |
| + CHECK(face); |
| + return face; |
| +} |
| + |
| +hb_font_t* CreateHarfBuzzFont(SkTypeface* skia_face, int text_size) { |
| + typedef std::map<uint32_t, uint16_t> GlyphCache; |
| + // TODO: This shouldn't grow indefinitely. See the comment above |
| + // CreateHarfBuzzFace; |
| + static std::map<SkFontID, GlyphCache> glyph_caches; |
| + |
| + SkFontID font_id = skia_face->uniqueID(); |
| + if (glyph_caches.count(font_id) == 0) |
| + glyph_caches[font_id] = GlyphCache(); |
| + |
| + hb_face_t* harfbuzz_face = CreateHarfBuzzFace(skia_face); |
| + hb_font_t* harfbuzz_font = hb_font_create(harfbuzz_face); |
| + unsigned int upem = hb_face_get_upem(harfbuzz_face); |
| + hb_face_destroy(harfbuzz_face); |
| + hb_font_set_scale(harfbuzz_font, upem, upem); |
| + HarfBuzzFontData* hb_font_data = new HarfBuzzFontData(&glyph_caches[font_id]); |
| + hb_font_data->paint_.setTypeface(skia_face); |
| + hb_font_data->paint_.setTextSize(text_size); |
| + hb_font_set_funcs(harfbuzz_font, HarfBuzzSkiaGetFontFuncs(), hb_font_data, |
| + DeleteByType<HarfBuzzFontData>); |
| + hb_font_make_immutable(harfbuzz_font); |
| + return harfbuzz_font; |
| +} |
| + |
| +size_t CharToGlyph(const internal::TextRunHarfBuzz& run, size_t pos) { |
| + DCHECK(run.range.start() <= pos && pos < run.range.end()); |
| + |
| + if (run.direction == UBIDI_LTR) { |
| + for (size_t i = 0; i < run.glyph_count - 1; ++i) { |
| + if (pos < run.glyph_to_char[i + 1]) |
| + return i; |
| + } |
| + return run.glyph_count - 1; |
| + } |
| + |
| + for (size_t i = 0; i < run.glyph_count; ++i) { |
| + if (pos >= run.glyph_to_char[i]) |
| + return i; |
| + } |
| + DCHECK(false); |
| + return -1; |
| +} |
| + |
| +Range CharRangeToGlyphRange(const internal::TextRunHarfBuzz& run, |
| + const Range& range) { |
| + DCHECK(run.range.Contains(range)); |
| + DCHECK(!range.is_reversed()); |
| + DCHECK(!range.is_empty()); |
| + |
| + size_t first = CharToGlyph(run, range.start()); |
| + size_t last = CharToGlyph(run, range.end() - 1); |
| + return Range(std::min(first, last), std::max(first, last) + 1); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace internal { |
| + |
| +TextRunHarfBuzz::TextRunHarfBuzz() |
| + : direction(UBIDI_LTR), |
| + glyph_count(-1), |
| + width(0) {} |
| + |
| +TextRunHarfBuzz::~TextRunHarfBuzz() { |
| + SkSafeUnref(skia_face); |
| +} |
| + |
| +} // namespace internal |
| + |
| +RenderTextHarfBuzz::RenderTextHarfBuzz() |
| + : RenderText(), |
| + num_runs_(0), |
| + needs_layout_(true) {} |
| + |
| +RenderTextHarfBuzz::~RenderTextHarfBuzz() {} |
| + |
| +Size RenderTextHarfBuzz::GetStringSize() { |
| + EnsureLayout(); |
| + int width = 0; |
| + for (size_t i = 0; i < num_runs_; ++i) |
| + width += runs_[i].width; |
| + return Size(width, font_list().GetHeight()); |
| +} |
| + |
| +SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) { |
| + EnsureLayout(); |
| + |
| + int x = ToTextPoint(point).x(); |
| + int offset = 0; |
| + size_t run_index = GetRunContainingXCoord(x, &offset); |
| + if (run_index >= num_runs_) |
| + return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT); |
| + internal::TextRunHarfBuzz& run = runs_[run_index]; |
| + |
| + for (size_t i = 0; i < run.glyph_count; ++i) { |
| + const SkScalar end = |
| + i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); |
| + const SkScalar middle = (end + run.positions[i].x()) / 2; |
| + if (offset < middle) |
| + return SelectionModel(LayoutIndexToTextIndex( |
| + run.glyph_to_char[i] + (run.direction == UBIDI_RTL)), |
| + CURSOR_BACKWARD); |
| + if (offset < end) |
| + return SelectionModel(LayoutIndexToTextIndex( |
| + run.glyph_to_char[i] + (run.direction == UBIDI_LTR)), |
| + CURSOR_FORWARD); |
| + } |
| + return EdgeSelectionModel(CURSOR_RIGHT); |
| +} |
| + |
| +bool RenderTextHarfBuzz::IsCursorablePosition(size_t position) { |
| + if (position == 0 || position == text().length()) |
| + return true; |
| + EnsureLayout(); |
| + |
| + // TODO: Can this be simplified by using |TextRunHarfBuzz::glyph_to_char|? |
| + // Check that the index is at a valid code point (not mid-surrgate-pair), |
| + // that it is not truncated from layout text (its glyph is shown on screen), |
| + // and that its glyph has distinct bounds (not mid-multi-character-grapheme). |
| + // An example of a multi-character-grapheme that is not a surrogate-pair is: |
| + // \x0915\x093f - (ki) - one of many Devanagari biconsonantal conjuncts. |
| + return gfx::IsValidCodePointIndex(text(), position) && |
| + position < LayoutIndexToTextIndex(GetLayoutText().length()) && |
| + GetGlyphBounds(position) != GetGlyphBounds(position - 1); |
| +} |
| + |
| +std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { |
| + NOTIMPLEMENTED(); |
| + return std::vector<RenderText::FontSpan>(); |
| +} |
| + |
| +int RenderTextHarfBuzz::GetLayoutTextBaseline() { |
| + EnsureLayout(); |
| + return lines()[0].baseline; |
| +} |
| + |
| +SelectionModel RenderTextHarfBuzz::AdjacentCharSelectionModel( |
| + const SelectionModel& selection, |
| + VisualCursorDirection direction) { |
| + DCHECK(!needs_layout_); |
| + internal::TextRunHarfBuzz* run; |
| + size_t run_index = GetRunContainingCaret(selection); |
| + if (run_index >= num_runs_) { |
| + // The cursor is not in any run: we're at the visual and logical edge. |
| + SelectionModel edge = EdgeSelectionModel(direction); |
| + if (edge.caret_pos() == selection.caret_pos()) |
| + return edge; |
| + run_index = (direction == CURSOR_RIGHT) ? 0 : num_runs_ - 1; |
| + run = &runs_[run_index]; |
| + } else { |
| + // If the cursor is moving within the current run, just move it by one |
| + // grapheme in the appropriate direction. |
| + run = &runs_[run_index]; |
| + size_t caret = selection.caret_pos(); |
| + bool forward_motion = |
| + (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); |
| + if (forward_motion) { |
| + if (caret < LayoutIndexToTextIndex(run->range.end())) { |
| + caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); |
| + return SelectionModel(caret, CURSOR_BACKWARD); |
| + } |
| + } else { |
| + if (caret > LayoutIndexToTextIndex(run->range.start())) { |
| + caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); |
| + return SelectionModel(caret, CURSOR_FORWARD); |
| + } |
| + } |
| + // The cursor is at the edge of a run; move to the visually adjacent run. |
| + run_index += (direction == CURSOR_LEFT) ? -1 : 1; |
| + if (run_index < 0 || run_index >= static_cast<int>(num_runs_)) |
| + return EdgeSelectionModel(direction); |
| + run = &runs_[run_index]; |
| + } |
| + bool forward_motion = |
| + (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); |
| + return forward_motion ? FirstSelectionModelInsideRun(run) : |
| + LastSelectionModelInsideRun(run); |
| +} |
| + |
| +SelectionModel RenderTextHarfBuzz::AdjacentWordSelectionModel( |
| + const SelectionModel& selection, |
| + VisualCursorDirection direction) { |
| + if (obscured()) |
| + return EdgeSelectionModel(direction); |
| + |
| + base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
| + bool success = iter.Init(); |
| + DCHECK(success); |
| + if (!success) |
| + return selection; |
| + |
| + size_t pos; |
| + if (direction == CURSOR_RIGHT) { |
| + pos = std::min(selection.caret_pos() + 1, text().length()); |
| + while (iter.Advance()) { |
| + pos = iter.pos(); |
| + if (iter.IsWord() && pos > selection.caret_pos()) |
| + break; |
| + } |
| + } else { // direction == CURSOR_LEFT |
| + // Notes: We always iterate words from the beginning. |
| + // This is probably fast enough for our usage, but we may |
| + // want to modify WordIterator so that it can start from the |
| + // middle of string and advance backwards. |
| + pos = std::max<int>(selection.caret_pos() - 1, 0); |
| + while (iter.Advance()) { |
| + if (iter.IsWord()) { |
| + size_t begin = iter.pos() - iter.GetString().length(); |
| + if (begin == selection.caret_pos()) { |
| + // The cursor is at the beginning of a word. |
| + // Move to previous word. |
| + break; |
| + } else if (iter.pos() >= selection.caret_pos()) { |
| + // The cursor is in the middle or at the end of a word. |
| + // Move to the top of current word. |
| + pos = begin; |
| + break; |
| + } else { |
| + pos = iter.pos() - iter.GetString().length(); |
| + } |
| + } |
| + } |
| + } |
| + return SelectionModel(pos, CURSOR_FORWARD); |
| +} |
| + |
| +Range RenderTextHarfBuzz::GetGlyphBounds(size_t index) { |
| + const size_t run_index = |
| + GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD)); |
| + // Return edge bounds if the index is invalid or beyond the layout text size. |
| + if (run_index >= num_runs_) |
| + return Range(GetStringSize().width()); |
| + const size_t layout_index = TextIndexToLayoutIndex(index); |
| + return Range(GetGlyphXBoundary(run_index, layout_index, false), |
| + GetGlyphXBoundary(run_index, layout_index, true)); |
| +} |
| + |
| +std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { |
| + DCHECK(!needs_layout_); |
| + DCHECK(Range(0, text().length()).Contains(range)); |
| + Range layout_range(TextIndexToLayoutIndex(range.start()), |
| + TextIndexToLayoutIndex(range.end())); |
| + DCHECK(Range(0, GetLayoutText().length()).Contains(layout_range)); |
| + |
| + std::vector<Rect> rects; |
| + if (layout_range.is_empty()) |
| + return rects; |
| + std::vector<Range> bounds; |
| + |
| + // Add a Range for each run/selection intersection. |
| + // TODO(msw): The bounds should probably not always be leading the range ends. |
| + for (size_t i = 0; i < num_runs_; ++i) { |
| + Range intersection = runs_[i].range.Intersect(layout_range); |
| + if (intersection.IsValid()) { |
| + DCHECK(!intersection.is_reversed()); |
| + Range range_x(GetGlyphXBoundary(i, intersection.start(), false), |
| + GetGlyphXBoundary(i, intersection.end(), false)); |
| + if (range_x.is_empty()) |
| + continue; |
| + range_x = Range(range_x.GetMin(), range_x.GetMax()); |
| + // Union this with the last range if they're adjacent. |
| + DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin()); |
| + if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { |
| + range_x = Range(bounds.back().GetMin(), range_x.GetMax()); |
| + bounds.pop_back(); |
| + } |
| + bounds.push_back(range_x); |
| + } |
| + } |
| + for (size_t i = 0; i < bounds.size(); ++i) { |
| + std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); |
| + rects.insert(rects.end(), current_rects.begin(), current_rects.end()); |
| + } |
| + return rects; |
| +} |
| + |
| +size_t RenderTextHarfBuzz::TextIndexToLayoutIndex(size_t index) const { |
| + DCHECK_LE(index, text().length()); |
| + ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index; |
| + CHECK_GE(i, 0); |
| + // Clamp layout indices to the length of the text actually used for layout. |
| + return std::min<size_t>(GetLayoutText().length(), i); |
| +} |
| + |
| +size_t RenderTextHarfBuzz::LayoutIndexToTextIndex(size_t index) const { |
| + if (!obscured()) |
| + return index; |
| + |
| + DCHECK_LE(index, GetLayoutText().length()); |
| + const size_t text_index = UTF16OffsetToIndex(text(), 0, index); |
| + DCHECK_LE(text_index, text().length()); |
| + return text_index; |
| +} |
| + |
| +void RenderTextHarfBuzz::ResetLayout() { |
| + needs_layout_ = true; |
| +} |
| + |
| +void RenderTextHarfBuzz::EnsureLayout() { |
| + if (needs_layout_) { |
| + // TODO: Skip complex processing if text isn't complex. |
| + // TODO: Handle styles. |
| + UErrorCode result = U_ZERO_ERROR; |
| + const base::string16& text = GetLayoutText(); |
| + |
| + UBiDi* line = ubidi_openSized(text.length(), 0, &result); |
| + CHECK(U_SUCCESS(result)); |
| + |
| + ubidi_setPara(line, text.c_str(), text.length(), UBIDI_DEFAULT_LTR, NULL, |
| + &result); |
| + CHECK(U_SUCCESS(result)); |
| + |
| + num_runs_ = ubidi_countRuns(line, &result); |
| + CHECK(U_SUCCESS(result)); |
| + |
| + runs_.reset(new internal::TextRunHarfBuzz[num_runs_]); |
| + for (size_t i = 0; i < num_runs_; ++i) { |
| + internal::TextRunHarfBuzz& run = runs_[i]; |
| + int32 start = 0; |
| + int32 length = 0; |
| + UBiDiDirection direction = ubidi_getVisualRun(line, i, &start, &length); |
| + DCHECK_GT(length, 0); |
| + ShapeRun(Range(start, start + length), direction, &runs_[i]); |
| + } |
| + |
| + ubidi_close(line); |
| + needs_layout_ = false; |
| + std::vector<internal::Line> empty_lines; |
| + set_lines(&empty_lines); |
| + } |
| + |
| + if (lines().empty()) { |
| + // TODO: implement single-line lines() support for now. |
| + std::vector<internal::Line> lines; |
| + lines.push_back(internal::Line()); |
| + int current_x = 0; |
| + for (size_t i = 0; i < num_runs_; ++i) { |
| + const internal::TextRunHarfBuzz& run = runs_[i]; |
| + internal::LineSegment segment; |
| + segment.x_range = Range(current_x, current_x + run.width); |
| + segment.char_range = run.range; |
| + segment.run = i; |
| + lines[0].segments.push_back(segment); |
| + |
| + Canvas canvas; |
| + internal::SkiaTextRenderer renderer(&canvas); |
| + renderer.SetTypeface(run.skia_face); |
| + renderer.SetTextSize(run.font_size); |
| + SkPaint::FontMetrics metrics; |
| + renderer.paint().getFontMetrics(&metrics); |
| + |
| + lines[0].size.set_width(lines[0].size.width() + run.width); |
| + lines[0].size.set_height(std::max(lines[0].size.height(), |
| + SkScalarRoundToInt(metrics.fDescent - metrics.fAscent))); |
| + lines[0].baseline = std::max(lines[0].baseline, |
| + SkScalarRoundToInt(-metrics.fAscent)); |
| + } |
| + set_lines(&lines); |
| + } |
| +} |
| + |
| +void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { |
| + int current_x = 0; |
| + |
| + internal::SkiaTextRenderer renderer(canvas); |
| + ApplyFadeEffects(&renderer); |
| + ApplyTextShadows(&renderer); |
| + |
| + ApplyCompositionAndSelectionStyles(); |
| + |
| + const Vector2d line_offset = GetLineOffset(0); |
| + |
| + for (size_t i = 0; i < num_runs_; ++i) { |
| + const internal::TextRunHarfBuzz& run = runs_[i]; |
| + |
| + renderer.SetTypeface(run.skia_face); |
| + renderer.SetTextSize(run.font_size); |
| + |
| + canvas->Save(); |
| + canvas->Translate(line_offset + Vector2d(current_x, 0)); |
| + |
| + for (BreakList<SkColor>::const_iterator it = |
| + colors().GetBreak(run.range.start()); |
| + it != colors().breaks().end() && it->first < run.range.end(); |
| + ++it) { |
| + const Range intersection = colors().GetRange(it).Intersect(run.range); |
| + const Range colored_glyphs = CharRangeToGlyphRange(run, intersection); |
| + DCHECK(!colored_glyphs.is_empty()); |
| + |
| + renderer.SetForegroundColor(it->second); |
| + renderer.DrawPosText(&run.positions[colored_glyphs.start()], |
| + &run.glyphs[colored_glyphs.start()], |
| + colored_glyphs.length()); |
| + } |
| + |
| + canvas->Restore(); |
| + current_x += run.width; |
| + } |
| + |
| + UndoCompositionAndSelectionStyles(); |
| +} |
| + |
| +void RenderTextHarfBuzz::ShapeRun(Range range, |
| + UBiDiDirection direction, |
| + internal::TextRunHarfBuzz* run) { |
| + const base::string16& text = GetLayoutText(); |
| + |
| + run->range = range; |
| + run->direction = direction; |
| + |
| + const Font& primary_font = font_list().GetPrimaryFont(); |
| + Canvas canvas; |
| + internal::SkiaTextRenderer renderer(&canvas); |
| + |
| + ApplyFadeEffects(&renderer); |
| + ApplyTextShadows(&renderer); |
| + |
| + renderer.SetFontSmoothingSettings(true, true); |
| + renderer.SetTextSize(primary_font.GetFontSize()); |
| + renderer.SetFontFamilyWithStyle(primary_font.GetFontName(), 0); |
| + |
| + run->skia_face = SkRef(renderer.paint().getTypeface()); |
| + run->font_size = renderer.paint().getTextSize(); |
| + hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face, run->font_size); |
| + |
| + hb_buffer_t* text_buffer = hb_buffer_create(); |
| + hb_buffer_add_utf16(text_buffer, |
| + reinterpret_cast<const uint16*>(text.c_str()), |
| + text.length(), run->range.start(), run->range.length()); |
| + hb_buffer_guess_segment_properties(text_buffer); |
|
eae
2014/03/07 18:43:06
You really don't want to do this. We've had a lot
ckocagil
2014/05/01 22:02:00
Done, we now split text by script runs taking into
|
| + |
| + hb_shape(harfbuzz_font, text_buffer, NULL, 0); |
| + |
| + hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(text_buffer, |
| + &run->glyph_count); |
| + CHECK(run->glyph_count != -1); |
| + run->glyphs.reset(new uint16[run->glyph_count]); |
| + for (size_t j = 0; j < run->glyph_count; ++j) |
| + run->glyphs[j] = infos[j].codepoint; |
| + |
| + run->glyph_to_char.reset(new uint32[run->glyph_count]); |
| + for (size_t j = 0; j < run->glyph_count; ++j) |
| + run->glyph_to_char[j] = infos[j].cluster; |
| + |
| + SkPaint::FontMetrics metrics; |
| + renderer.paint().getFontMetrics(&metrics); |
| + |
| + hb_glyph_position_t* hb_positions = |
| + hb_buffer_get_glyph_positions(text_buffer, NULL); |
| + |
| + run->positions.reset(new SkPoint[run->glyph_count]); |
| + |
| + for (size_t j = 0; j < run->glyph_count; ++j) { |
| + const int x_offset = |
| + SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].x_offset)); |
| + const int y_offset = |
| + SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].y_offset)); |
| + run->positions[j].set(run->width + x_offset, |
| + y_offset - metrics.fAscent); |
| + run->width += |
| + SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].x_advance)); |
| + } |
| + |
| + hb_buffer_destroy(text_buffer); |
| + hb_font_destroy(harfbuzz_font); |
| +} |
| + |
| +size_t RenderTextHarfBuzz::GetRunContainingCaret( |
| + const SelectionModel& caret) const { |
| + DCHECK(!needs_layout_); |
| + size_t layout_position = TextIndexToLayoutIndex(caret.caret_pos()); |
| + LogicalCursorDirection affinity = caret.caret_affinity(); |
| + for (size_t run = 0; run < num_runs_; ++run) |
| + if (RangeContainsCaret(runs_[run].range, layout_position, affinity)) |
| + return run; |
| + return num_runs_; |
| +} |
| + |
| +int RenderTextHarfBuzz::GetGlyphXBoundary(size_t run_index, |
| + size_t text_index, |
| + bool trailing) { |
| + internal::TextRunHarfBuzz& run = runs_[run_index]; |
| + |
| + int x = 0; |
| + |
| + for (size_t i = 0; i < run_index; ++i) |
| + x += runs_[i].width; |
| + |
| + const bool last_glyph = text_index == run.range.end(); |
| + Range glyph_range; |
| + if (last_glyph) { |
| + trailing = true; |
| + glyph_range = run.direction == UBIDI_LTR ? |
| + Range(run.glyph_count - 1, run.glyph_count) : Range(0, 1); |
| + } else { |
| + glyph_range = CharRangeToGlyphRange(run, Range(text_index, text_index + 1)); |
| + } |
| + int trailing_step = trailing; |
| + size_t glyph_pos = glyph_range.start() + |
| + (run.direction == UBIDI_LTR ? trailing : !trailing); |
| + x += glyph_pos < run.glyph_count ? |
| + SkScalarRoundToInt(run.positions[glyph_pos].x()) : run.width; |
| + return x; |
| +} |
| + |
| +SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( |
| + const internal::TextRunHarfBuzz* run) { |
| + size_t position = LayoutIndexToTextIndex(run->range.start()); |
| + position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); |
| + return SelectionModel(position, CURSOR_BACKWARD); |
| +} |
| + |
| +SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( |
| + const internal::TextRunHarfBuzz* run) { |
| + size_t position = LayoutIndexToTextIndex(run->range.end()); |
| + position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
| + return SelectionModel(position, CURSOR_FORWARD); |
| +} |
| + |
| +size_t RenderTextHarfBuzz::GetRunContainingXCoord(int x, int* offset) const { |
| + DCHECK(!needs_layout_); |
| + if (x < 0) |
| + return num_runs_; |
| + // Find the text run containing the argument point (assumed already offset). |
| + int current_x = 0; |
| + for (size_t run = 0; run < num_runs_; ++run) { |
| + current_x += runs_[run].width; |
| + if (x < current_x) { |
| + *offset = x - (current_x - runs_[run].width); |
| + return run; |
| + } |
| + } |
| + return num_runs_; |
| +} |
| + |
| +} // namespace gfx |