Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/render_text_harfbuzz.h" | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 #include <iterator> | |
| 9 #include <map> | |
| 10 | |
| 11 #include "base/i18n/break_iterator.h" | |
| 12 #include "base/i18n/char_iterator.h" | |
| 13 #include "third_party/harfbuzz-ng/src/hb.h" | |
| 14 #include "third_party/icu/source/common/unicode/ubidi.h" | |
| 15 #include "third_party/skia/include/core/SkColor.h" | |
| 16 #include "third_party/skia/include/core/SkTypeface.h" | |
| 17 #include "ui/gfx/canvas.h" | |
| 18 #include "ui/gfx/render_text.h" | |
|
msw
2014/04/29 06:24:45
Remove this include; it's redundant with render_te
ckocagil
2014/05/01 22:02:01
Done.
| |
| 19 #include "ui/gfx/scoped_canvas.h" | |
| 20 #include "ui/gfx/utf16_indexing.h" | |
| 21 | |
| 22 namespace gfx { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Font data provider for HarfBuzz using Skia. Copied from Blink. | |
|
msw
2014/04/29 06:24:45
Add TODOs (and file a bug) to de-duplicate code li
ckocagil
2014/05/01 22:02:01
Done.
| |
| 27 struct HarfBuzzFontData { | |
|
msw
2014/04/29 06:24:45
nit: remove all "HarfBuzz" prefixes in this anonym
ckocagil
2014/05/01 22:02:01
Done.
| |
| 28 HarfBuzzFontData(std::map<uint32_t, uint16_t>* glyph_cache) | |
| 29 : glyph_cache_(glyph_cache) {} | |
| 30 | |
| 31 SkPaint paint_; | |
| 32 std::map<uint32_t, uint16_t>* glyph_cache_; | |
| 33 }; | |
| 34 | |
| 35 hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) { | |
| 36 return SkScalarToFixed(value); | |
| 37 } | |
| 38 | |
| 39 void SkiaGetGlyphWidthAndExtents(SkPaint* paint, | |
| 40 hb_codepoint_t codepoint, | |
|
msw
2014/04/29 06:24:45
nit: fix indentations.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 41 hb_position_t* width, | |
| 42 hb_glyph_extents_t* extents) { | |
| 43 CHECK(codepoint <= 0xFFFF); | |
|
msw
2014/04/29 06:24:45
nit: CHECK_LE
ckocagil
2014/05/01 22:02:01
Done.
| |
| 44 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
|
msw
2014/04/29 06:24:45
Is it okay to set this here? Should the value be r
ckocagil
2014/05/01 22:02:01
We call setTextEncoding before doing anything that
| |
| 45 | |
| 46 SkScalar skWidth; | |
|
msw
2014/04/29 06:24:45
nit: use chromium's unix_hacker naming convention
ckocagil
2014/05/01 22:02:01
Done.
| |
| 47 SkRect skBounds; | |
| 48 uint16_t glyph = codepoint; | |
| 49 | |
| 50 paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds); | |
| 51 if (width) | |
| 52 *width = SkiaScalarToHarfBuzzPosition(skWidth); | |
| 53 if (extents) { | |
| 54 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be | |
| 55 // y-grows-up. | |
| 56 extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft); | |
| 57 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop); | |
| 58 extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width()); | |
| 59 extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height()); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 hb_bool_t HarfBuzzGetGlyph(hb_font_t* font, | |
| 64 void* font_data, | |
| 65 hb_codepoint_t unicode, | |
| 66 hb_codepoint_t variation_selector, | |
| 67 hb_codepoint_t* glyph, | |
| 68 void* user_data) { | |
| 69 HarfBuzzFontData* hb_font_data = | |
| 70 reinterpret_cast<HarfBuzzFontData*>(font_data); | |
| 71 std::map<uint32_t, uint16_t>& cache = *hb_font_data->glyph_cache_; | |
| 72 | |
| 73 bool exists = cache.count(unicode) != 0; | |
| 74 if (!exists) { | |
| 75 SkPaint* paint = &hb_font_data->paint_; | |
| 76 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); | |
|
msw
2014/04/29 06:24:45
Is it okay to set this here? Should the value be r
ckocagil
2014/05/01 22:02:01
Addressed above.
| |
| 77 uint16_t glyph16; | |
| 78 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &glyph16); | |
| 79 cache[unicode] = glyph16; | |
| 80 *glyph = glyph16; | |
|
msw
2014/04/29 06:24:45
nit: this looks redundant with the assignment outs
ckocagil
2014/05/01 22:02:01
Ouch! Fixed!
| |
| 81 } | |
| 82 *glyph = cache[unicode]; | |
| 83 return !!*glyph; | |
| 84 } | |
| 85 | |
| 86 hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* font, | |
| 87 void* font_data, | |
| 88 hb_codepoint_t glyph, | |
| 89 void* user_data) { | |
| 90 HarfBuzzFontData* hb_font_data = | |
| 91 reinterpret_cast<HarfBuzzFontData*>(font_data); | |
| 92 hb_position_t advance = 0; | |
| 93 | |
| 94 SkiaGetGlyphWidthAndExtents(&hb_font_data->paint_, glyph, &advance, 0); | |
| 95 return advance; | |
| 96 } | |
| 97 | |
| 98 hb_bool_t HarfBuzzGetGlyphHorizontalOrigin(hb_font_t* font, | |
| 99 void* font_data, | |
| 100 hb_codepoint_t glyph, | |
| 101 hb_position_t* x, | |
| 102 hb_position_t* y, | |
| 103 void* user_data) { | |
| 104 // Just return true, following the way that HarfBuzz-FreeType | |
| 105 // implementation does. | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 hb_bool_t HarfBuzzGetGlyphExtents(hb_font_t* font, | |
| 110 void* font_data, | |
| 111 hb_codepoint_t glyph, | |
| 112 hb_glyph_extents_t* extents, | |
| 113 void* user_data) { | |
| 114 HarfBuzzFontData* hb_font_data = | |
| 115 reinterpret_cast<HarfBuzzFontData*>(font_data); | |
| 116 | |
| 117 SkiaGetGlyphWidthAndExtents(&hb_font_data->paint_, glyph, 0, extents); | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 hb_font_funcs_t* HarfBuzzSkiaGetFontFuncs() { | |
| 122 static hb_font_funcs_t* harfbuzz_skia_font_funcs = 0; | |
| 123 | |
| 124 // We don't set callback functions which we can't support. | |
| 125 // HarfBuzz will use the fallback implementation if they aren't set. | |
|
msw
2014/04/29 06:24:45
nit: Add a TODO to support SkTypeface kerning oper
ckocagil
2014/05/01 22:02:01
Done.
| |
| 126 if (!harfbuzz_skia_font_funcs) { | |
| 127 harfbuzz_skia_font_funcs = hb_font_funcs_create(); | |
| 128 hb_font_funcs_set_glyph_func( | |
| 129 harfbuzz_skia_font_funcs, HarfBuzzGetGlyph, 0, 0); | |
| 130 hb_font_funcs_set_glyph_h_advance_func( | |
| 131 harfbuzz_skia_font_funcs, HarfBuzzGetGlyphHorizontalAdvance, 0, 0); | |
| 132 hb_font_funcs_set_glyph_h_origin_func( | |
| 133 harfbuzz_skia_font_funcs, HarfBuzzGetGlyphHorizontalOrigin, 0, 0); | |
| 134 hb_font_funcs_set_glyph_extents_func( | |
| 135 harfbuzz_skia_font_funcs, HarfBuzzGetGlyphExtents, 0, 0); | |
| 136 hb_font_funcs_make_immutable(harfbuzz_skia_font_funcs); | |
| 137 } | |
| 138 return harfbuzz_skia_font_funcs; | |
| 139 } | |
| 140 | |
| 141 hb_blob_t* HarfBuzzSkiaGetTable(hb_face_t* face, | |
| 142 hb_tag_t tag, | |
| 143 void* user_data) { | |
| 144 SkTypeface* typeface = reinterpret_cast<SkTypeface*>(user_data); | |
| 145 | |
| 146 const size_t table_size = typeface->getTableSize(tag); | |
| 147 if (!table_size) | |
| 148 return 0; | |
| 149 | |
| 150 char* buffer = reinterpret_cast<char*>(malloc(table_size)); | |
| 151 if (!buffer) | |
| 152 return 0; | |
| 153 size_t actual_size = typeface->getTableData(tag, 0, table_size, buffer); | |
| 154 if (table_size != actual_size) { | |
| 155 free(buffer); | |
| 156 return 0; | |
| 157 } | |
| 158 | |
| 159 return hb_blob_create(const_cast<char*>(buffer), table_size, | |
| 160 HB_MEMORY_MODE_WRITABLE, buffer, free); | |
| 161 } | |
| 162 | |
| 163 template<typename Type> | |
| 164 void DeleteByType(void* data) { | |
| 165 Type* typed_data = reinterpret_cast<Type*>(data); | |
| 166 delete typed_data; | |
| 167 } | |
| 168 | |
| 169 // TODO(ckocagil): Implement a cache for HarfBuzz faces. SkTypeface provides | |
| 170 // unique face IDs, which we can use as keys. Also see base::MRUCache. | |
| 171 hb_face_t* CreateHarfBuzzFace(SkTypeface* skia_face) { | |
| 172 hb_face_t* face = hb_face_create_for_tables(HarfBuzzSkiaGetTable, skia_face, | |
| 173 0); | |
| 174 CHECK(face); | |
| 175 return face; | |
| 176 } | |
| 177 | |
| 178 hb_font_t* CreateHarfBuzzFont(SkTypeface* skia_face, int text_size) { | |
| 179 typedef std::map<uint32_t, uint16_t> GlyphCache; | |
| 180 // TODO: This shouldn't grow indefinitely. See the comment above | |
|
msw
2014/04/29 06:24:45
nit: TODO(ckocagil):
ckocagil
2014/05/01 22:02:01
Done.
| |
| 181 // CreateHarfBuzzFace; | |
| 182 static std::map<SkFontID, GlyphCache> glyph_caches; | |
|
msw
2014/04/29 06:24:45
Could this map to a struct composed of a GlyphCach
ckocagil
2014/05/01 22:02:01
Done. Good idea! This resolved the harfbuzz face c
msw
2014/05/02 05:08:21
Awesome :D
| |
| 183 | |
| 184 SkFontID font_id = skia_face->uniqueID(); | |
| 185 if (glyph_caches.count(font_id) == 0) | |
| 186 glyph_caches[font_id] = GlyphCache(); | |
| 187 | |
| 188 hb_face_t* harfbuzz_face = CreateHarfBuzzFace(skia_face); | |
| 189 hb_font_t* harfbuzz_font = hb_font_create(harfbuzz_face); | |
| 190 unsigned int upem = hb_face_get_upem(harfbuzz_face); | |
|
msw
2014/04/29 06:24:45
Please add a comment explaining what's going on he
ckocagil
2014/05/01 22:02:01
AFAIK this shapes the glyphs at maximum size and d
| |
| 191 hb_face_destroy(harfbuzz_face); | |
| 192 hb_font_set_scale(harfbuzz_font, upem, upem); | |
|
msw
2014/04/29 06:24:45
Would it make sense to set the scale separately?
ckocagil
2014/05/01 22:02:01
Separate from what?
msw
2014/05/02 05:08:21
In a separate function from the creation of the fo
ckocagil
2014/05/06 03:38:40
I'm still not exactly sure what the hb_font_set_sc
| |
| 193 HarfBuzzFontData* hb_font_data = new HarfBuzzFontData(&glyph_caches[font_id]); | |
|
msw
2014/04/29 06:24:45
Could this be cached as well?
ckocagil
2014/05/01 22:02:01
I implement a cache for this, but both FontData an
msw
2014/05/02 05:08:21
Fair enough, thanks for giving it a shot.
| |
| 194 hb_font_data->paint_.setTypeface(skia_face); | |
| 195 hb_font_data->paint_.setTextSize(text_size); | |
| 196 hb_font_set_funcs(harfbuzz_font, HarfBuzzSkiaGetFontFuncs(), hb_font_data, | |
| 197 DeleteByType<HarfBuzzFontData>); | |
| 198 hb_font_make_immutable(harfbuzz_font); | |
| 199 return harfbuzz_font; | |
| 200 } | |
| 201 | |
| 202 size_t CharToGlyph(const internal::TextRunHarfBuzz& run, size_t pos) { | |
| 203 DCHECK(run.range.start() <= pos && pos < run.range.end()); | |
| 204 | |
| 205 if (run.direction == UBIDI_LTR) { | |
| 206 for (size_t i = 0; i < run.glyph_count - 1; ++i) { | |
|
msw
2014/04/29 06:24:45
optional nit: remove braces here and with the for
ckocagil
2014/05/01 22:02:01
Done.
| |
| 207 if (pos < run.glyph_to_char[i + 1]) | |
| 208 return i; | |
| 209 } | |
| 210 return run.glyph_count - 1; | |
| 211 } | |
| 212 | |
| 213 for (size_t i = 0; i < run.glyph_count; ++i) { | |
| 214 if (pos >= run.glyph_to_char[i]) | |
| 215 return i; | |
| 216 } | |
| 217 DCHECK(false); | |
|
msw
2014/04/29 06:24:45
nit: use NOTREACHED().
ckocagil
2014/05/01 22:02:01
Done.
| |
| 218 return -1; | |
|
msw
2014/04/29 06:24:45
nit: should this return 0 to match the equally via
ckocagil
2014/05/01 22:02:01
Done.
| |
| 219 } | |
| 220 | |
| 221 Range CharRangeToGlyphRange(const internal::TextRunHarfBuzz& run, | |
|
msw
2014/04/29 06:24:45
nit: Copy/adapt the comment from render_text_win.c
ckocagil
2014/05/01 22:02:01
Done.
| |
| 222 const Range& range) { | |
| 223 DCHECK(run.range.Contains(range)); | |
| 224 DCHECK(!range.is_reversed()); | |
| 225 DCHECK(!range.is_empty()); | |
| 226 | |
| 227 size_t first = CharToGlyph(run, range.start()); | |
|
msw
2014/04/29 06:24:45
nit: const for |first| and |last|.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 228 size_t last = CharToGlyph(run, range.end() - 1); | |
| 229 return Range(std::min(first, last), std::max(first, last) + 1); | |
| 230 } | |
| 231 | |
| 232 // Returns true if characters of |block_code| may trigger font fallback. | |
| 233 bool IsUnusualBlockCode(const UBlockCode block_code) { | |
| 234 return block_code == UBLOCK_GEOMETRIC_SHAPES || | |
| 235 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS; | |
| 236 } | |
| 237 | |
| 238 bool HasMissingGlyphs(const internal::TextRunHarfBuzz& run) { | |
| 239 const int kMissingGlyphId = 0; | |
|
msw
2014/04/29 06:24:45
nit: static
ckocagil
2014/05/01 22:02:01
Done.
| |
| 240 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 241 for (size_t i = 0; i < run.glyph_count; ++i) | |
| 242 if (run.glyphs[i] == kMissingGlyphId) | |
| 243 return true; | |
| 244 return false; | |
| 245 } | |
| 246 | |
| 247 } // namespace | |
| 248 | |
| 249 namespace internal { | |
| 250 | |
| 251 TextRunHarfBuzz::TextRunHarfBuzz() | |
| 252 : width(0), | |
| 253 direction(UBIDI_LTR), | |
| 254 glyph_count(-1), | |
| 255 strike(false), | |
| 256 diagonal_strike(false), | |
| 257 underline(false) {} | |
|
msw
2014/04/29 06:24:45
nit: initialize |level|, |font_size|, and |font_st
ckocagil
2014/05/01 22:02:01
Done.
| |
| 258 | |
| 259 TextRunHarfBuzz::~TextRunHarfBuzz() {} | |
| 260 | |
| 261 } // namespace internal | |
| 262 | |
| 263 RenderTextHarfBuzz::RenderTextHarfBuzz() | |
| 264 : RenderText(), | |
| 265 needs_layout_(true) {} | |
|
msw
2014/04/29 06:24:45
nit: does an empty object need layout? (RenderText
ckocagil
2014/05/01 22:02:01
Nope. Fixed.
| |
| 266 | |
| 267 RenderTextHarfBuzz::~RenderTextHarfBuzz() {} | |
| 268 | |
| 269 Size RenderTextHarfBuzz::GetStringSize() { | |
| 270 EnsureLayout(); | |
| 271 int width = 0; | |
| 272 for (size_t i = 0; i < runs_.size(); ++i) | |
| 273 width += runs_[i]->width; | |
|
msw
2014/04/29 06:24:45
I think starting with a single-line implementation
ckocagil
2014/05/01 22:02:01
But this is already single-line. Now I changed it
msw
2014/05/02 05:08:21
Right, I was just saying that you chose the right
| |
| 274 return Size(width, font_list().GetHeight()); | |
| 275 } | |
| 276 | |
| 277 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) { | |
| 278 EnsureLayout(); | |
| 279 | |
| 280 int x = ToTextPoint(point).x(); | |
| 281 int offset = 0; | |
| 282 size_t run_index = GetRunContainingXCoord(x, &offset); | |
| 283 if (run_index >= runs_.size()) | |
| 284 return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT); | |
| 285 internal::TextRunHarfBuzz& run = *runs_[run_index]; | |
|
msw
2014/04/29 06:24:45
nit: use a const ref or a pointer.
ckocagil
2014/05/01 22:02:01
Done (the former)
| |
| 286 | |
| 287 for (size_t i = 0; i < run.glyph_count; ++i) { | |
| 288 const SkScalar end = | |
| 289 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); | |
| 290 const SkScalar middle = (end + run.positions[i].x()) / 2; | |
| 291 if (offset < middle) | |
| 292 return SelectionModel(LayoutIndexToTextIndex( | |
| 293 run.glyph_to_char[i] + (run.direction == UBIDI_RTL)), | |
|
msw
2014/04/29 06:24:45
nit: I think these should be explicitly "* ? 1 : 0
ckocagil
2014/05/01 22:02:01
Done.
| |
| 294 CURSOR_BACKWARD); | |
|
msw
2014/04/29 06:24:45
nit: I think these are opposite from how RenderTex
ckocagil
2014/05/01 22:02:01
Hopefully fixed. I think I managed to get them rig
| |
| 295 if (offset < end) | |
| 296 return SelectionModel(LayoutIndexToTextIndex( | |
| 297 run.glyph_to_char[i] + (run.direction == UBIDI_LTR)), | |
| 298 CURSOR_FORWARD); | |
| 299 } | |
| 300 return EdgeSelectionModel(CURSOR_RIGHT); | |
| 301 } | |
| 302 | |
| 303 bool RenderTextHarfBuzz::IsCursorablePosition(size_t position) { | |
|
msw
2014/04/29 06:24:45
nit: rebase once https://codereview.chromium.org/2
ckocagil
2014/05/01 22:02:01
Will do.
| |
| 304 if (position == 0 || position == text().length()) | |
| 305 return true; | |
| 306 EnsureLayout(); | |
| 307 | |
| 308 // TODO: Can this be simplified by using |TextRunHarfBuzz::glyph_to_char|? | |
|
msw
2014/04/29 06:24:45
nit: TODO(ckocagil):, and yeah, that ought to work
ckocagil
2014/05/01 22:02:01
Done (added my name, didn't resolve it)
| |
| 309 // Check that the index is at a valid code point (not mid-surrgate-pair), | |
| 310 // that it is not truncated from layout text (its glyph is shown on screen), | |
| 311 // and that its glyph has distinct bounds (not mid-multi-character-grapheme). | |
| 312 // An example of a multi-character-grapheme that is not a surrogate-pair is: | |
| 313 // \x0915\x093f - (ki) - one of many Devanagari biconsonantal conjuncts. | |
| 314 return gfx::IsValidCodePointIndex(text(), position) && | |
|
msw
2014/04/29 06:24:45
nit: remove unnecessary gfx:: namespace qualifier.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 315 position < LayoutIndexToTextIndex(GetLayoutText().length()) && | |
| 316 GetGlyphBounds(position) != GetGlyphBounds(position - 1); | |
| 317 } | |
| 318 | |
| 319 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { | |
| 320 NOTIMPLEMENTED(); | |
| 321 return std::vector<RenderText::FontSpan>(); | |
| 322 } | |
| 323 | |
| 324 int RenderTextHarfBuzz::GetLayoutTextBaseline() { | |
| 325 EnsureLayout(); | |
| 326 return lines()[0].baseline; | |
| 327 } | |
| 328 | |
| 329 SelectionModel RenderTextHarfBuzz::AdjacentCharSelectionModel( | |
| 330 const SelectionModel& selection, | |
| 331 VisualCursorDirection direction) { | |
| 332 DCHECK(!needs_layout_); | |
| 333 internal::TextRunHarfBuzz* run; | |
| 334 size_t run_index = GetRunContainingCaret(selection); | |
| 335 if (run_index >= runs_.size()) { | |
| 336 // The cursor is not in any run: we're at the visual and logical edge. | |
| 337 SelectionModel edge = EdgeSelectionModel(direction); | |
| 338 if (edge.caret_pos() == selection.caret_pos()) | |
| 339 return edge; | |
| 340 int visual_index = (direction == CURSOR_RIGHT) ? 0 : runs_.size() - 1; | |
| 341 run = runs_[visual_to_logical_[visual_index]]; | |
| 342 } else { | |
| 343 // If the cursor is moving within the current run, just move it by one | |
| 344 // grapheme in the appropriate direction. | |
| 345 run = runs_[run_index]; | |
| 346 size_t caret = selection.caret_pos(); | |
| 347 bool forward_motion = | |
| 348 (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); | |
| 349 if (forward_motion) { | |
| 350 if (caret < LayoutIndexToTextIndex(run->range.end())) { | |
| 351 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | |
| 352 return SelectionModel(caret, CURSOR_BACKWARD); | |
| 353 } | |
| 354 } else { | |
| 355 if (caret > LayoutIndexToTextIndex(run->range.start())) { | |
| 356 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); | |
| 357 return SelectionModel(caret, CURSOR_FORWARD); | |
| 358 } | |
| 359 } | |
| 360 // The cursor is at the edge of a run; move to the visually adjacent run. | |
| 361 int visual_index = logical_to_visual_[run_index]; | |
| 362 visual_index += (direction == CURSOR_LEFT) ? -1 : 1; | |
| 363 if (visual_index < 0 || visual_index >= static_cast<int>(runs_.size())) | |
| 364 return EdgeSelectionModel(direction); | |
| 365 run = runs_[visual_to_logical_[visual_index]]; | |
| 366 } | |
| 367 bool forward_motion = | |
| 368 (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); | |
| 369 return forward_motion ? FirstSelectionModelInsideRun(run) : | |
| 370 LastSelectionModelInsideRun(run); | |
| 371 } | |
| 372 | |
| 373 SelectionModel RenderTextHarfBuzz::AdjacentWordSelectionModel( | |
|
msw
2014/04/29 06:24:45
Having a common implementation (that matches the c
ckocagil
2014/05/01 22:02:01
Added TODO.
| |
| 374 const SelectionModel& selection, | |
| 375 VisualCursorDirection direction) { | |
| 376 if (obscured()) | |
| 377 return EdgeSelectionModel(direction); | |
| 378 | |
| 379 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | |
| 380 bool success = iter.Init(); | |
| 381 DCHECK(success); | |
| 382 if (!success) | |
| 383 return selection; | |
| 384 | |
| 385 size_t pos; | |
| 386 if (direction == CURSOR_RIGHT) { | |
| 387 pos = std::min(selection.caret_pos() + 1, text().length()); | |
| 388 while (iter.Advance()) { | |
| 389 pos = iter.pos(); | |
| 390 if (iter.IsWord() && pos > selection.caret_pos()) | |
| 391 break; | |
| 392 } | |
| 393 } else { // direction == CURSOR_LEFT | |
| 394 // Notes: We always iterate words from the beginning. | |
| 395 // This is probably fast enough for our usage, but we may | |
| 396 // want to modify WordIterator so that it can start from the | |
| 397 // middle of string and advance backwards. | |
| 398 pos = std::max<int>(selection.caret_pos() - 1, 0); | |
| 399 while (iter.Advance()) { | |
| 400 if (iter.IsWord()) { | |
| 401 size_t begin = iter.pos() - iter.GetString().length(); | |
| 402 if (begin == selection.caret_pos()) { | |
| 403 // The cursor is at the beginning of a word. | |
| 404 // Move to previous word. | |
| 405 break; | |
| 406 } else if (iter.pos() >= selection.caret_pos()) { | |
| 407 // The cursor is in the middle or at the end of a word. | |
| 408 // Move to the top of current word. | |
| 409 pos = begin; | |
| 410 break; | |
| 411 } else { | |
| 412 pos = iter.pos() - iter.GetString().length(); | |
| 413 } | |
| 414 } | |
| 415 } | |
| 416 } | |
| 417 return SelectionModel(pos, CURSOR_FORWARD); | |
| 418 } | |
| 419 | |
| 420 Range RenderTextHarfBuzz::GetGlyphBounds(size_t index) { | |
| 421 const size_t run_index = | |
| 422 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD)); | |
| 423 // Return edge bounds if the index is invalid or beyond the layout text size. | |
| 424 if (run_index >= runs_.size()) | |
| 425 return Range(GetStringSize().width()); | |
| 426 const size_t layout_index = TextIndexToLayoutIndex(index); | |
| 427 return Range(GetGlyphXBoundary(run_index, layout_index, false), | |
| 428 GetGlyphXBoundary(run_index, layout_index, true)); | |
| 429 } | |
| 430 | |
| 431 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { | |
| 432 DCHECK(!needs_layout_); | |
| 433 DCHECK(Range(0, text().length()).Contains(range)); | |
| 434 Range layout_range(TextIndexToLayoutIndex(range.start()), | |
| 435 TextIndexToLayoutIndex(range.end())); | |
| 436 DCHECK(Range(0, GetLayoutText().length()).Contains(layout_range)); | |
| 437 | |
| 438 std::vector<Rect> rects; | |
| 439 if (layout_range.is_empty()) | |
| 440 return rects; | |
| 441 std::vector<Range> bounds; | |
| 442 | |
| 443 // Add a Range for each run/selection intersection. | |
| 444 // TODO(msw): The bounds should probably not always be leading the range ends. | |
| 445 for (size_t i = 0; i < runs_.size(); ++i) { | |
| 446 const internal::TextRunHarfBuzz* run = runs_[visual_to_logical_[i]]; | |
| 447 Range intersection = run->range.Intersect(layout_range); | |
| 448 if (intersection.IsValid()) { | |
| 449 DCHECK(!intersection.is_reversed()); | |
| 450 Range range_x(GetGlyphXBoundary(i, intersection.start(), false), | |
| 451 GetGlyphXBoundary(i, intersection.end(), false)); | |
| 452 if (range_x.is_empty()) | |
| 453 continue; | |
| 454 range_x = Range(range_x.GetMin(), range_x.GetMax()); | |
| 455 // Union this with the last range if they're adjacent. | |
| 456 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin()); | |
| 457 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { | |
| 458 range_x = Range(bounds.back().GetMin(), range_x.GetMax()); | |
| 459 bounds.pop_back(); | |
| 460 } | |
| 461 bounds.push_back(range_x); | |
| 462 } | |
| 463 } | |
| 464 for (size_t i = 0; i < bounds.size(); ++i) { | |
| 465 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); | |
| 466 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); | |
| 467 } | |
| 468 return rects; | |
| 469 } | |
| 470 | |
| 471 size_t RenderTextHarfBuzz::TextIndexToLayoutIndex(size_t index) const { | |
| 472 DCHECK_LE(index, text().length()); | |
| 473 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index; | |
| 474 CHECK_GE(i, 0); | |
| 475 // Clamp layout indices to the length of the text actually used for layout. | |
| 476 return std::min<size_t>(GetLayoutText().length(), i); | |
| 477 } | |
| 478 | |
| 479 size_t RenderTextHarfBuzz::LayoutIndexToTextIndex(size_t index) const { | |
| 480 if (!obscured()) | |
| 481 return index; | |
| 482 | |
| 483 DCHECK_LE(index, GetLayoutText().length()); | |
| 484 const size_t text_index = UTF16OffsetToIndex(text(), 0, index); | |
| 485 DCHECK_LE(text_index, text().length()); | |
| 486 return text_index; | |
| 487 } | |
| 488 | |
| 489 void RenderTextHarfBuzz::ResetLayout() { | |
| 490 needs_layout_ = true; | |
| 491 } | |
| 492 | |
| 493 void RenderTextHarfBuzz::EnsureLayout() { | |
|
msw
2014/04/29 06:24:45
I recommend splitting some of this out like Render
ckocagil
2014/05/01 22:02:01
I did some splitting here.
| |
| 494 if (needs_layout_) { | |
| 495 // TODO: Skip complex processing if text isn't complex. | |
|
msw
2014/04/29 06:24:45
nit: you can remove this TODO here (it's only appl
ckocagil
2014/05/01 22:02:01
Done.
| |
| 496 // TODO: Handle styles. | |
|
msw
2014/04/29 06:24:45
nit: TODO(ckocagil):
ckocagil
2014/05/01 22:02:01
Resolved! :-)
| |
| 497 const base::string16& text = GetLayoutText(); | |
| 498 UErrorCode result = U_ZERO_ERROR; | |
| 499 | |
| 500 runs_.clear(); | |
| 501 | |
| 502 if (!text.empty()) { | |
| 503 UBiDi* line = ubidi_openSized(text.length(), 0, &result); | |
|
msw
2014/04/29 06:24:45
Can you use base::i18n::BiDiLineIterator?
ckocagil
2014/05/01 22:02:01
It lacks |ubidi_getBaseDirection| which we need he
| |
| 504 CHECK(U_SUCCESS(result)); | |
|
msw
2014/04/29 06:24:45
Can you DCHECK or bail gracefully if this fails? D
ckocagil
2014/05/01 22:02:01
Done, we now NOTREACHED() and return in the case o
msw
2014/05/02 05:08:21
Yikes, please avoid a stack overflow, even on fail
ckocagil
2014/05/06 03:38:40
Done, we now fallback to fake layout data.
| |
| 505 | |
| 506 ubidi_setPara(line, text.c_str(), text.length(), UBIDI_DEFAULT_LTR, NULL, | |
|
msw
2014/04/29 06:24:45
This default value should probably come from GetTe
ckocagil
2014/05/01 22:02:01
Done.
| |
| 507 &result); | |
| 508 CHECK(U_SUCCESS(result)); | |
| 509 | |
| 510 // Temporarily apply composition underlines and selection colors. | |
| 511 ApplyCompositionAndSelectionStyles(); | |
| 512 | |
| 513 // Build the list of runs from the script items and ranged styles. Use an | |
| 514 // empty color BreakList to avoid breaking runs at color boundaries. | |
| 515 BreakList<SkColor> empty_colors; | |
| 516 empty_colors.SetMax(text.length()); | |
| 517 internal::StyleIterator style(empty_colors, styles()); | |
| 518 //const size_t max_run_length = kMaxGlyphs / 2; | |
|
msw
2014/04/29 06:24:45
Remove this and the commented code below too.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 519 for (size_t run_break = 0; run_break < text.length();) { | |
| 520 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; | |
| 521 run->range.set_start(run_break); | |
| 522 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | | |
| 523 (style.style(ITALIC) ? Font::ITALIC : 0); | |
| 524 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 525 run->strike = style.style(STRIKE); | |
| 526 run->diagonal_strike = style.style(DIAGONAL_STRIKE); | |
| 527 run->underline = style.style(UNDERLINE); | |
| 528 | |
| 529 int32 script_item_break = 0; | |
| 530 ubidi_getLogicalRun(line, run_break, &script_item_break, &run->level); | |
| 531 | |
| 532 // Find the next break and advance the iterators as needed. | |
| 533 run_break = std::min(static_cast<size_t>(script_item_break), | |
| 534 TextIndexToLayoutIndex(style.GetRange().end())); | |
| 535 | |
| 536 // Clamp run lengths to avoid exceeding the maximum supported glyph coun t. | |
| 537 /*if ((run_break - run->range.start()) > max_run_length) { | |
| 538 run_break = run->range.start() + max_run_length; | |
| 539 if (!IsValidCodePointIndex(layout_text, run_break)) | |
| 540 --run_break; | |
| 541 }*/ | |
| 542 | |
| 543 // Break runs adjacent to character substrings in certain code blocks. | |
| 544 // This avoids using their fallback fonts for more characters than neede d, | |
|
msw
2014/04/29 06:24:45
nit: limit to 80 chars here and elsewhere below.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 545 // in cases like "\x25B6 Media Title", etc. http://crbug.com/278913 | |
| 546 if (run_break > run->range.start()) { | |
| 547 const size_t run_start = run->range.start(); | |
| 548 const int32 run_length = static_cast<int32>(run_break - run_start); | |
| 549 base::i18n::UTF16CharIterator iter(text.c_str() + run_start, | |
| 550 run_length); | |
| 551 const UBlockCode first_block_code = ublock_getCode(iter.get()); | |
| 552 const bool first_block_unusual = IsUnusualBlockCode(first_block_code); | |
| 553 while (iter.Advance() && iter.array_pos() < run_length) { | |
| 554 const UBlockCode current_block_code = ublock_getCode(iter.get()); | |
| 555 if (current_block_code != first_block_code && | |
| 556 (first_block_unusual || IsUnusualBlockCode(current_block_code))) { | |
| 557 run_break = run_start + iter.array_pos(); | |
| 558 break; | |
| 559 } | |
| 560 } | |
| 561 } | |
| 562 | |
| 563 DCHECK(IsValidCodePointIndex(text, run_break)); | |
| 564 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 565 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); | |
| 566 run->range.set_end(run_break); | |
| 567 const UChar* uchar_start = ubidi_getText(line); | |
| 568 run->direction = ubidi_getBaseDirection(uchar_start + run->range.start() , | |
| 569 run->range.length()); | |
| 570 // TODO: Is there any case where we would prefer defaulting to UBIDI_RTL ? | |
|
msw
2014/04/29 06:24:45
nit: TODO(ckocagil):, also the neutral direction s
ckocagil
2014/05/01 22:02:01
Used GetTextDirection() here.
| |
| 571 if (run->direction == UBIDI_NEUTRAL) | |
| 572 run->direction = UBIDI_LTR; | |
| 573 runs_.push_back(run); | |
| 574 } | |
| 575 | |
| 576 for (size_t i = 0; i < runs_.size(); ++i) | |
| 577 ShapeRun(runs_[i]); | |
| 578 | |
| 579 const size_t num_runs = runs_.size(); | |
| 580 scoped_ptr<UBiDiLevel[]> levels(new UBiDiLevel[num_runs]); | |
| 581 for (size_t i = 0; i < num_runs; ++i) | |
| 582 levels[i] = runs_[i]->level; | |
| 583 visual_to_logical_.resize(num_runs); | |
| 584 ubidi_reorderVisual(levels.get(), num_runs, &visual_to_logical_[0]); | |
| 585 logical_to_visual_.resize(num_runs); | |
| 586 ubidi_reorderLogical(levels.get(), num_runs, &logical_to_visual_[0]); | |
| 587 | |
| 588 // Undo the temporarily applied composition underlines and selection color s. | |
| 589 UndoCompositionAndSelectionStyles(); | |
| 590 | |
| 591 ubidi_close(line); | |
| 592 } | |
| 593 | |
| 594 needs_layout_ = false; | |
| 595 std::vector<internal::Line> empty_lines; | |
| 596 set_lines(&empty_lines); | |
| 597 } | |
| 598 | |
| 599 if (lines().empty()) { | |
| 600 std::vector<internal::Line> lines; | |
| 601 lines.push_back(internal::Line()); | |
| 602 | |
| 603 int current_x = 0; | |
| 604 SkPaint paint; | |
| 605 | |
| 606 for (size_t i = 0; i < runs_.size(); ++i) { | |
| 607 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; | |
| 608 internal::LineSegment segment; | |
| 609 segment.x_range = Range(current_x, current_x + run.width); | |
| 610 segment.char_range = run.range; | |
| 611 segment.run = i; | |
| 612 lines[0].segments.push_back(segment); | |
| 613 | |
| 614 paint.setTypeface(run.skia_face.get()); | |
| 615 paint.setTextSize(run.font_size); | |
| 616 SkPaint::FontMetrics metrics; | |
| 617 paint.getFontMetrics(&metrics); | |
| 618 | |
| 619 lines[0].size.set_width(lines[0].size.width() + run.width); | |
| 620 lines[0].size.set_height(std::max(lines[0].size.height(), | |
| 621 SkScalarRoundToInt(metrics.fDescent - metrics.fAscent))); | |
| 622 lines[0].baseline = std::max(lines[0].baseline, | |
| 623 SkScalarRoundToInt(-metrics.fAscent)); | |
| 624 } | |
| 625 | |
| 626 set_lines(&lines); | |
| 627 } | |
| 628 } | |
| 629 | |
| 630 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { | |
|
msw
2014/04/29 06:24:45
nit: DCHECK(!needs_layout_);
ckocagil
2014/05/01 22:02:01
Done.
| |
| 631 int current_x = 0; | |
| 632 | |
| 633 internal::SkiaTextRenderer renderer(canvas); | |
|
msw
2014/04/29 06:24:45
Should this apply font smoothing and cleartype set
ckocagil
2014/05/01 22:02:01
Looks like it is needed for disabling cleartype wh
| |
| 634 ApplyFadeEffects(&renderer); | |
| 635 ApplyTextShadows(&renderer); | |
| 636 | |
| 637 ApplyCompositionAndSelectionStyles(); | |
| 638 | |
| 639 const Vector2d line_offset = GetLineOffset(0); | |
| 640 | |
| 641 for (size_t i = 0; i < runs_.size(); ++i) { | |
|
msw
2014/04/29 06:24:45
Do you prefer to do this in terms of runs and not
ckocagil
2014/05/01 22:02:01
Simply because I implemented Lines and Segments af
| |
| 642 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; | |
| 643 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 644 renderer.SetTypeface(run.skia_face.get()); | |
| 645 renderer.SetTextSize(run.font_size); | |
| 646 | |
| 647 ScopedCanvas scoped_canvas(canvas); | |
| 648 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline); | |
| 649 canvas->Translate(origin); | |
|
msw
2014/04/29 06:24:45
Won't this undesirably apply the vector atop the e
ckocagil
2014/05/01 22:02:01
Why would that be undesirable? If the RenderText c
msw
2014/05/02 05:08:21
It looks like this continuously applies offsets wi
ckocagil
2014/05/06 03:38:40
The ScopedCanvas resets it. It's now an explicit S
msw
2014/05/09 22:55:18
Ah! Sorry I missed that; ScopedCanvas is fine if y
| |
| 650 | |
| 651 for (BreakList<SkColor>::const_iterator it = | |
| 652 colors().GetBreak(run.range.start()); | |
| 653 it != colors().breaks().end() && it->first < run.range.end(); | |
| 654 ++it) { | |
| 655 const Range intersection = colors().GetRange(it).Intersect(run.range); | |
| 656 const Range colored_glyphs = CharRangeToGlyphRange(run, intersection); | |
| 657 DCHECK(!colored_glyphs.is_empty()); | |
|
msw
2014/04/29 06:24:45
See how I've adjusted this in https://codereview.c
ckocagil
2014/05/01 22:02:01
I'll merge that CL here after it's landed.
msw
2014/05/09 22:55:18
It has landed, please sync and rebase (in a separa
ckocagil
2014/05/12 09:53:29
Done.
| |
| 658 | |
| 659 renderer.SetForegroundColor(it->second); | |
| 660 renderer.DrawPosText(&run.positions[colored_glyphs.start()], | |
| 661 &run.glyphs[colored_glyphs.start()], | |
| 662 colored_glyphs.length()); | |
| 663 int width = (colored_glyphs.end() == run.glyph_count ? | |
| 664 run.width : run.positions[colored_glyphs.end()].x()) - | |
|
msw
2014/04/29 06:24:45
nit: wrap after ":" and indent the following line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 665 run.positions[colored_glyphs.start()].x(); | |
| 666 renderer.DrawDecorations(0, 0, width, run.underline, run.strike, | |
|
msw
2014/04/29 06:24:45
nit: don't you need to supply the starting positio
ckocagil
2014/05/01 22:02:01
No, we achieve that by translating the canvas abov
| |
| 667 run.diagonal_strike); | |
| 668 } | |
| 669 | |
| 670 current_x += run.width; | |
| 671 } | |
| 672 | |
| 673 renderer.EndDiagonalStrike(); | |
| 674 | |
| 675 UndoCompositionAndSelectionStyles(); | |
| 676 } | |
| 677 | |
| 678 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { | |
| 679 const base::string16& text = GetLayoutText(); | |
| 680 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 681 const Font& primary_font = font_list().GetPrimaryFont(); | |
|
msw
2014/04/29 06:24:45
Add a TODO about handling FontList better and CC y
ckocagil
2014/05/01 22:02:01
Added a TODO for fallback. Better how?
msw
2014/05/02 05:08:21
We should use the FontList's other Fonts besides G
| |
| 682 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(), | |
| 683 run->font_style); | |
| 684 run->font_size = primary_font.GetFontSize(); | |
| 685 /*DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), | |
|
msw
2014/04/29 06:24:45
nit: remove this if the size is properly adjusted
ckocagil
2014/05/01 22:02:01
Done.
| |
| 686 run->font_style, &run->font);*/ | |
| 687 | |
| 688 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), | |
| 689 run->font_size); | |
| 690 | |
| 691 hb_buffer_t* text_buffer = hb_buffer_create(); | |
|
msw
2014/04/29 06:24:45
nit: please add some comments for these HarfBuzz f
ckocagil
2014/05/01 22:02:01
Done.
| |
| 692 hb_buffer_add_utf16(text_buffer, | |
| 693 reinterpret_cast<const uint16*>(text.c_str()), | |
| 694 text.length(), run->range.start(), run->range.length()); | |
| 695 hb_buffer_guess_segment_properties(text_buffer); | |
|
msw
2014/04/29 06:24:45
Address eae's comment; at least add a TODO and fil
eae
2014/04/29 16:01:53
Ideally we'd be able to share the script/direction
ckocagil
2014/05/01 22:02:01
I added logic to split runs by scripts which uses
| |
| 696 | |
| 697 hb_shape(harfbuzz_font, text_buffer, NULL, 0); | |
| 698 | |
| 699 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(text_buffer, | |
| 700 &run->glyph_count); | |
| 701 CHECK(run->glyph_count != -1); | |
|
msw
2014/04/29 06:24:45
nit: CHECK_NE, bail more gracefully if possible.
ckocagil
2014/05/01 22:02:01
Actually there isn't anything to check here. Remov
| |
| 702 run->glyphs.reset(new uint16[run->glyph_count]); | |
| 703 for (size_t j = 0; j < run->glyph_count; ++j) | |
|
msw
2014/04/29 06:24:45
nit: Use |i| or some better name instead of |j| he
ckocagil
2014/05/01 22:02:01
Done.
| |
| 704 run->glyphs[j] = infos[j].codepoint; | |
| 705 | |
| 706 run->glyph_to_char.reset(new uint32[run->glyph_count]); | |
| 707 for (size_t j = 0; j < run->glyph_count; ++j) | |
|
msw
2014/04/29 06:24:45
nit: would it make more sense to loop over the gly
ckocagil
2014/05/01 22:02:01
Done.
| |
| 708 run->glyph_to_char[j] = infos[j].cluster; | |
| 709 | |
| 710 hb_glyph_position_t* hb_positions = | |
| 711 hb_buffer_get_glyph_positions(text_buffer, NULL); | |
| 712 | |
| 713 run->positions.reset(new SkPoint[run->glyph_count]); | |
|
msw
2014/04/29 06:24:45
nit: should this use gfx::Point or PointF (for [fu
ckocagil
2014/05/01 22:02:01
Is there any benefit other than using a gfx:: type
msw
2014/05/02 05:08:21
I see that the SkPoint values are used later on; t
| |
| 714 | |
| 715 for (size_t j = 0; j < run->glyph_count; ++j) { | |
| 716 const int x_offset = | |
| 717 SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].x_offset)); | |
| 718 const int y_offset = | |
| 719 SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].y_offset)); | |
| 720 run->positions[j].set(run->width + x_offset, y_offset); | |
| 721 run->width += | |
|
msw
2014/04/29 06:24:45
nit: does incrementing the run width like this yie
ckocagil
2014/05/01 22:02:01
It visually does, and I don't see why it shouldn't
msw
2014/05/02 05:08:21
I figured that each glyph's x_offset would be rela
ckocagil
2014/05/06 03:38:40
Nope, those offsets are for individual glyphs. e.g
| |
| 722 SkScalarRoundToInt(SkFixedToScalar(hb_positions[j].x_advance)); | |
| 723 } | |
| 724 | |
| 725 hb_buffer_destroy(text_buffer); | |
| 726 hb_font_destroy(harfbuzz_font); | |
| 727 } | |
| 728 | |
| 729 size_t RenderTextHarfBuzz::GetRunContainingCaret( | |
| 730 const SelectionModel& caret) const { | |
| 731 DCHECK(!needs_layout_); | |
| 732 size_t layout_position = TextIndexToLayoutIndex(caret.caret_pos()); | |
| 733 LogicalCursorDirection affinity = caret.caret_affinity(); | |
| 734 for (size_t run = 0; run < runs_.size(); ++run) | |
| 735 if (RangeContainsCaret(runs_[run]->range, layout_position, affinity)) | |
| 736 return run; | |
| 737 return runs_.size(); | |
| 738 } | |
| 739 | |
| 740 int RenderTextHarfBuzz::GetGlyphXBoundary(size_t run_index, | |
| 741 size_t text_index, | |
| 742 bool trailing) { | |
| 743 internal::TextRunHarfBuzz& run = *runs_[run_index]; | |
| 744 | |
| 745 int x = 0; | |
| 746 | |
|
msw
2014/04/29 06:24:45
nit: remove blank line
ckocagil
2014/05/01 22:02:01
Done.
| |
| 747 for (size_t i = 0; i < run_index; ++i) | |
| 748 x += runs_[i]->width; | |
|
msw
2014/04/29 06:24:45
Add TextRunHarfBuzz.preceding_run_widths or simila
ckocagil
2014/05/01 22:02:01
Done.
| |
| 749 | |
| 750 const bool last_glyph = text_index == run.range.end(); | |
|
msw
2014/04/29 06:24:45
nit: inline this at its one use below
ckocagil
2014/05/01 22:02:01
Done.
| |
| 751 Range glyph_range; | |
| 752 if (last_glyph) { | |
| 753 trailing = true; | |
| 754 glyph_range = run.direction == UBIDI_LTR ? | |
| 755 Range(run.glyph_count - 1, run.glyph_count) : Range(0, 1); | |
| 756 } else { | |
| 757 glyph_range = CharRangeToGlyphRange(run, Range(text_index, text_index + 1)); | |
| 758 } | |
| 759 int trailing_step = trailing; | |
|
msw
2014/04/29 06:24:45
nit: use explicit "* ? 1 : 0" here.
ckocagil
2014/05/01 22:02:01
Done.
| |
| 760 size_t glyph_pos = glyph_range.start() + | |
|
msw
2014/04/29 06:24:45
Would it be helpful to keep HarfBuzz's glyph advan
ckocagil
2014/05/01 22:02:01
Which glyph advance value and helpful how?
msw
2014/05/02 05:08:21
Could hb_positions[j].x_advance be used to get the
ckocagil
2014/05/06 03:38:40
See my comment about x_offset above.
| |
| 761 (run.direction == UBIDI_LTR ? trailing : !trailing); | |
|
msw
2014/04/29 06:24:45
nit: use "* ? trailing_step : (1 - trailing_step)"
ckocagil
2014/05/01 22:02:01
Done.
| |
| 762 x += glyph_pos < run.glyph_count ? | |
| 763 SkScalarRoundToInt(run.positions[glyph_pos].x()) : run.width; | |
| 764 return x; | |
| 765 } | |
| 766 | |
| 767 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( | |
| 768 const internal::TextRunHarfBuzz* run) { | |
| 769 size_t position = LayoutIndexToTextIndex(run->range.start()); | |
| 770 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); | |
| 771 return SelectionModel(position, CURSOR_BACKWARD); | |
| 772 } | |
| 773 | |
| 774 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( | |
| 775 const internal::TextRunHarfBuzz* run) { | |
| 776 size_t position = LayoutIndexToTextIndex(run->range.end()); | |
| 777 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | |
| 778 return SelectionModel(position, CURSOR_FORWARD); | |
| 779 } | |
| 780 | |
| 781 size_t RenderTextHarfBuzz::GetRunContainingXCoord(int x, int* offset) const { | |
| 782 DCHECK(!needs_layout_); | |
| 783 if (x < 0) | |
| 784 return runs_.size(); | |
| 785 // Find the text run containing the argument point (assumed already offset). | |
| 786 int current_x = 0; | |
| 787 for (size_t run = 0; run < runs_.size(); ++run) { | |
|
msw
2014/04/29 06:24:45
The header says |runs_| is in logical order, shoul
ckocagil
2014/05/01 22:02:01
No idea how this worked, maybe we usually don't ha
msw
2014/05/02 05:08:21
I'm scared :p (I'll need to debug this on Windows
| |
| 788 current_x += runs_[run]->width; | |
| 789 if (x < current_x) { | |
| 790 *offset = x - (current_x - runs_[run]->width); | |
| 791 return run; | |
| 792 } | |
| 793 } | |
| 794 return runs_.size(); | |
| 795 } | |
| 796 | |
| 797 } // namespace gfx | |
| OLD | NEW |