OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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 <map> | |
8 | |
9 #include "base/i18n/break_iterator.h" | |
10 #include "base/i18n/char_iterator.h" | |
11 #include "third_party/harfbuzz-ng/src/hb-icu.h" | |
12 #include "third_party/harfbuzz-ng/src/hb.h" | |
13 #include "third_party/icu/source/common/unicode/ubidi.h" | |
14 #include "third_party/skia/include/core/SkColor.h" | |
15 #include "third_party/skia/include/core/SkTypeface.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/utf16_indexing.h" | |
18 | |
19 #if defined(OS_WIN) | |
20 #include "ui/gfx/font_smoothing_win.h" | |
21 #endif | |
22 | |
23 namespace gfx { | |
24 | |
25 namespace { | |
26 | |
27 // The maximum number of scripts a Unicode character can belong to. | |
Alexei Svitkine (slow)
2014/05/16 18:22:53
Can you expand this comment to explain where this
ckocagil
2014/05/20 18:35:11
Done.
| |
28 const size_t kMaxScripts = 5; | |
29 | |
30 typedef std::map<uint32_t, uint16_t> GlyphCache; | |
Alexei Svitkine (slow)
2014/05/16 18:22:53
Add a comment explaining what this mapping from an
ckocagil
2014/05/20 18:35:11
Done.
| |
31 | |
32 // Font data provider for HarfBuzz using Skia. Copied from Blink. | |
33 // TODO(ckocagil): Eliminate the duplication. http://crbug.com/368375 | |
34 struct FontData { | |
35 FontData(GlyphCache* glyph_cache) : glyph_cache_(glyph_cache) {} | |
36 | |
37 SkPaint paint_; | |
38 GlyphCache* glyph_cache_; | |
39 }; | |
40 | |
41 hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) { | |
42 return SkScalarToFixed(value); | |
43 } | |
44 | |
45 // Deletes the object at the given pointer after casting it to the given type. | |
46 template<typename Type> | |
47 void DeleteByType(void* data) { | |
48 Type* typed_data = reinterpret_cast<Type*>(data); | |
49 delete typed_data; | |
50 } | |
51 | |
52 template<typename Type> | |
53 void DeleteArrayByType(void* data) { | |
54 Type* typed_data = reinterpret_cast<Type*>(data); | |
55 delete[] typed_data; | |
56 } | |
57 | |
58 // Outputs the |width| and |extents| of the glyph with index |codepoint| in | |
59 // |paint|'s font. | |
60 void GetGlyphWidthAndExtents(SkPaint* paint, | |
61 hb_codepoint_t codepoint, | |
62 hb_position_t* width, | |
63 hb_glyph_extents_t* extents) { | |
64 DCHECK_LE(codepoint, 0xFFFFU); | |
65 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
66 | |
67 SkScalar sk_width; | |
68 SkRect sk_bounds; | |
69 uint16_t glyph = codepoint; | |
70 | |
71 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); | |
72 if (width) | |
73 *width = SkiaScalarToHarfBuzzPosition(sk_width); | |
74 if (extents) { | |
75 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be | |
76 // y-grows-up. | |
77 extents->x_bearing = SkiaScalarToHarfBuzzPosition(sk_bounds.fLeft); | |
78 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-sk_bounds.fTop); | |
79 extents->width = SkiaScalarToHarfBuzzPosition(sk_bounds.width()); | |
80 extents->height = SkiaScalarToHarfBuzzPosition(-sk_bounds.height()); | |
81 } | |
82 } | |
83 | |
84 // Writes the |glyph| index for the given |unicode| code point. Returns whether | |
85 // the glyph exists, i.e. it is not a missing glyph. | |
86 hb_bool_t GetGlyph(hb_font_t* font, | |
87 void* data, | |
88 hb_codepoint_t unicode, | |
89 hb_codepoint_t variation_selector, | |
90 hb_codepoint_t* glyph, | |
91 void* user_data) { | |
92 FontData* font_data = reinterpret_cast<FontData*>(data); | |
93 GlyphCache* cache = font_data->glyph_cache_; | |
94 | |
95 bool exists = cache->count(unicode) != 0; | |
96 if (!exists) { | |
97 SkPaint* paint = &font_data->paint_; | |
98 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); | |
99 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &(*cache)[unicode]); | |
100 } | |
101 *glyph = (*cache)[unicode]; | |
102 return !!*glyph; | |
103 } | |
104 | |
105 // Returns the horizontal advance value of the |glyph|. | |
106 hb_position_t GetGlyphHorizontalAdvance(hb_font_t* font, | |
107 void* data, | |
108 hb_codepoint_t glyph, | |
109 void* user_data) { | |
110 FontData* font_data = reinterpret_cast<FontData*>(data); | |
111 hb_position_t advance = 0; | |
112 | |
113 GetGlyphWidthAndExtents(&font_data->paint_, glyph, &advance, 0); | |
114 return advance; | |
115 } | |
116 | |
117 hb_bool_t GetGlyphHorizontalOrigin(hb_font_t* font, | |
118 void* data, | |
119 hb_codepoint_t glyph, | |
120 hb_position_t* x, | |
121 hb_position_t* y, | |
122 void* user_data) { | |
123 // Just return true, like the HarfBuzz-FreeType implementation. | |
124 return true; | |
125 } | |
126 | |
127 // Writes the |extents| of |glyph|. | |
128 hb_bool_t GetGlyphExtents(hb_font_t* font, | |
129 void* data, | |
130 hb_codepoint_t glyph, | |
131 hb_glyph_extents_t* extents, | |
132 void* user_data) { | |
133 FontData* font_data = reinterpret_cast<FontData*>(data); | |
134 | |
135 GetGlyphWidthAndExtents(&font_data->paint_, glyph, 0, extents); | |
136 return true; | |
137 } | |
138 | |
139 // Returns a HarfBuzz font data provider that uses Skia. | |
140 hb_font_funcs_t* GetFontFuncs() { | |
141 static hb_font_funcs_t* font_funcs = 0; | |
142 | |
143 // We don't set callback functions which we can't support. | |
144 // HarfBuzz will use the fallback implementation if they aren't set. | |
145 // TODO(ckocagil): Merge Blink's kerning funcs. | |
146 if (!font_funcs) { | |
147 // The object created by |hb_font_funcs_create()| below lives indefinitely | |
148 // and is intentionally leaked. | |
149 font_funcs = hb_font_funcs_create(); | |
150 hb_font_funcs_set_glyph_func(font_funcs, GetGlyph, 0, 0); | |
151 hb_font_funcs_set_glyph_h_advance_func( | |
152 font_funcs, GetGlyphHorizontalAdvance, 0, 0); | |
153 hb_font_funcs_set_glyph_h_origin_func( | |
154 font_funcs, GetGlyphHorizontalOrigin, 0, 0); | |
155 hb_font_funcs_set_glyph_extents_func( | |
156 font_funcs, GetGlyphExtents, 0, 0); | |
157 hb_font_funcs_make_immutable(font_funcs); | |
158 } | |
159 return font_funcs; | |
160 } | |
161 | |
162 // Returns the raw data of the font table |tag|. | |
163 hb_blob_t* GetFontTable(hb_face_t* face, hb_tag_t tag, void* user_data) { | |
164 SkTypeface* typeface = reinterpret_cast<SkTypeface*>(user_data); | |
165 | |
166 const size_t table_size = typeface->getTableSize(tag); | |
167 if (!table_size) | |
168 return 0; | |
169 | |
170 scoped_ptr<char[]> buffer(new char[table_size]); | |
171 if (!buffer) | |
172 return 0; | |
173 size_t actual_size = typeface->getTableData(tag, 0, table_size, buffer.get()); | |
174 if (table_size != actual_size) | |
175 return 0; | |
176 | |
177 char* buffer_raw = buffer.release(); | |
178 return hb_blob_create(buffer_raw, table_size, HB_MEMORY_MODE_WRITABLE, | |
179 buffer_raw, DeleteArrayByType<char>); | |
180 } | |
181 | |
182 void UnrefSkTypeface(void* data) { | |
183 SkTypeface* skia_face = reinterpret_cast<SkTypeface*>(data); | |
184 SkSafeUnref(skia_face); | |
185 } | |
186 | |
187 // Creates a HarfBuzz face from the given Skia face. | |
188 hb_face_t* CreateHarfBuzzFace(SkTypeface* skia_face) { | |
189 SkSafeRef(skia_face); | |
190 hb_face_t* face = hb_face_create_for_tables(GetFontTable, skia_face, | |
191 UnrefSkTypeface); | |
192 DCHECK(face); | |
193 return face; | |
194 } | |
195 | |
196 // Creates a HarfBuzz font from the given Skia face and text size. | |
197 hb_font_t* CreateHarfBuzzFont(SkTypeface* skia_face, int text_size) { | |
198 typedef std::pair<hb_face_t*, GlyphCache> FaceCache; | |
199 | |
200 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache? | |
201 static std::map<SkFontID, FaceCache> face_caches; | |
202 | |
203 FaceCache* face_cache = &face_caches[skia_face->uniqueID()]; | |
204 if (face_cache->first == 0) { | |
205 hb_face_t* harfbuzz_face = CreateHarfBuzzFace(skia_face); | |
206 *face_cache = FaceCache(harfbuzz_face, GlyphCache()); | |
207 } | |
208 | |
209 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first); | |
210 // TODO(ckocagil): Investigate whether disabling hinting here has any effect | |
211 // on text quality. | |
212 int upem = hb_face_get_upem(face_cache->first); | |
213 hb_font_set_scale(harfbuzz_font, upem, upem); | |
214 FontData* hb_font_data = new FontData(&face_cache->second); | |
215 hb_font_data->paint_.setTypeface(skia_face); | |
216 hb_font_data->paint_.setTextSize(text_size); | |
217 hb_font_set_funcs(harfbuzz_font, GetFontFuncs(), hb_font_data, | |
218 DeleteByType<FontData>); | |
219 hb_font_make_immutable(harfbuzz_font); | |
220 return harfbuzz_font; | |
221 } | |
222 | |
223 // Returns the index of the first glyph that corresponds to the character at | |
224 // |pos|. | |
225 size_t CharToGlyph(const internal::TextRunHarfBuzz& run, size_t pos) { | |
226 DCHECK(run.range.start() <= pos && pos < run.range.end()); | |
227 | |
228 if (run.direction == UBIDI_LTR) { | |
229 for (size_t i = 0; i < run.glyph_count - 1; ++i) { | |
230 if (pos < run.glyph_to_char[i + 1]) | |
231 return i; | |
232 } | |
233 return run.glyph_count - 1; | |
234 } | |
235 | |
236 for (size_t i = 0; i < run.glyph_count; ++i) { | |
237 if (pos >= run.glyph_to_char[i]) | |
238 return i; | |
239 } | |
240 NOTREACHED(); | |
241 return 0; | |
242 } | |
243 | |
244 // Returns the corresponding glyph range of the given character range. | |
245 // |range| is in text-space (0 corresponds to |GetLayoutText()[0]|). | |
246 // Returned value is in run-space (0 corresponds to the first glyph in the run). | |
247 Range CharRangeToGlyphRange(const internal::TextRunHarfBuzz& run, | |
248 const Range& range) { | |
249 DCHECK(run.range.Contains(range)); | |
250 DCHECK(!range.is_reversed()); | |
251 DCHECK(!range.is_empty()); | |
252 | |
253 const size_t first = CharToGlyph(run, range.start()); | |
254 const size_t last = CharToGlyph(run, range.end() - 1); | |
255 // TODO(ckocagil): What happens when the character has zero or multiple | |
256 // glyphs? Is the "+ 1" below correct then? | |
257 return Range(std::min(first, last), std::max(first, last) + 1); | |
258 } | |
259 | |
260 // Returns true if characters of |block_code| may trigger font fallback. | |
261 bool IsUnusualBlockCode(const UBlockCode block_code) { | |
262 return block_code == UBLOCK_GEOMETRIC_SHAPES || | |
263 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS; | |
264 } | |
265 | |
266 // Returns whether the given shaped run contains any missing glyphs. | |
267 bool HasMissingGlyphs(const internal::TextRunHarfBuzz& run) { | |
268 static const int kMissingGlyphId = 0; | |
269 for (size_t i = 0; i < run.glyph_count; ++i) { | |
270 if (run.glyphs[i] == kMissingGlyphId) | |
271 return true; | |
272 } | |
273 return false; | |
274 } | |
275 | |
276 // If the given scripts match, returns the one that isn't USCRIPT_COMMON or | |
277 // USCRIPT_INHERITED, i.e. the more specific one. Otherwise returns | |
278 // USCRIPT_INVALID_CODE. | |
279 UScriptCode ScriptIntersect(UScriptCode first, UScriptCode second) { | |
280 if (first == second || | |
281 (second > USCRIPT_INVALID_CODE && second <= USCRIPT_INHERITED)) { | |
282 return first; | |
283 } | |
284 if (first > USCRIPT_INVALID_CODE && first <= USCRIPT_INHERITED) | |
285 return second; | |
286 return USCRIPT_INVALID_CODE; | |
287 } | |
288 | |
289 // Writes the script and the script extensions of the character with the | |
290 // Unicode |codepoint|. Returns the number of written scripts. | |
291 int GetScriptExtensions(UChar32 codepoint, UScriptCode* scripts) { | |
292 UErrorCode icu_error = U_ZERO_ERROR; | |
293 // ICU documentation incorrectly states that the result of | |
294 // |uscript_getScriptExtensions| will contain the regular script property. | |
295 // Call |uscript_getScript| to get the script property. | |
296 scripts[0] = uscript_getScript(codepoint, &icu_error); | |
297 if (U_FAILURE(icu_error)) | |
298 return 0; | |
299 int count = uscript_getScriptExtensions(codepoint, scripts + 1, | |
300 kMaxScripts - 1, &icu_error); | |
301 if (U_FAILURE(icu_error)) | |
302 count = 0; | |
303 return count + 1; | |
304 } | |
305 | |
306 // Intersects the script extensions set of |codepoint| with |result| and writes | |
307 // to |result|, reading and updating |result_size|. | |
308 void ScriptSetIntersect(UChar32 codepoint, | |
309 UScriptCode* result, | |
310 size_t* result_size) { | |
311 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE }; | |
312 int count = GetScriptExtensions(codepoint, scripts); | |
313 | |
314 UScriptCode* out = result; | |
315 size_t out_size = 0; | |
316 | |
317 for (size_t i = 0; i < *result_size; ++i) { | |
318 for (int j = 0; j < count; ++j) { | |
319 UScriptCode intersection = ScriptIntersect(result[i], scripts[j]); | |
320 if (intersection != USCRIPT_INVALID_CODE) { | |
321 *out++ = intersection; | |
322 ++out_size; | |
Alexei Svitkine (slow)
2014/05/16 18:22:53
If you're already keeping track of |out_size|, I t
ckocagil
2014/05/20 18:35:11
Done.
| |
323 break; | |
324 } | |
325 } | |
326 } | |
327 | |
328 *result_size = out_size; | |
329 } | |
330 | |
331 // Find the longest sequence of characters from 0 and up to |length| that | |
332 // have at least one common UScriptCode value. Writes the common script value to | |
333 // |script| and returns the length of the sequence. Takes the characters' script | |
334 // extensions into account. http://www.unicode.org/reports/tr24/#ScriptX | |
335 int ScriptInterval(const base::string16& text, | |
336 size_t start, | |
337 size_t length, | |
338 UScriptCode* script) { | |
339 DCHECK_GE(length, 0U); | |
msw
2014/05/16 18:01:30
nit: This was DCHECK(length > 0) before, is 0 leng
ckocagil
2014/05/20 18:35:11
Done.
| |
340 | |
341 UScriptCode scripts[kMaxScripts] = { USCRIPT_INVALID_CODE }; | |
342 | |
343 base::i18n::UTF16CharIterator char_iterator(text.c_str() + start, length); | |
344 size_t scripts_size = GetScriptExtensions(char_iterator.get(), scripts); | |
345 *script = scripts[0]; | |
346 | |
347 while (char_iterator.Advance()) { | |
348 ScriptSetIntersect(char_iterator.get(), scripts, &scripts_size); | |
349 if (scripts_size > 0U) | |
msw
2014/05/16 18:01:30
nit: reverse the condition to return early, then s
ckocagil
2014/05/20 18:35:11
Done.
| |
350 *script = scripts[0]; | |
351 else | |
352 return char_iterator.array_pos(); | |
353 } | |
354 | |
355 return length; | |
356 } | |
357 | |
358 } // namespace | |
359 | |
360 namespace internal { | |
361 | |
362 TextRunHarfBuzz::TextRunHarfBuzz() | |
363 : width(0), | |
364 preceding_run_widths(0), | |
365 direction(UBIDI_LTR), | |
366 level(0), | |
367 script(USCRIPT_INVALID_CODE), | |
368 glyph_count(-1), | |
369 font_size(0), | |
370 font_style(0), | |
371 strike(false), | |
372 diagonal_strike(false), | |
373 underline(false) {} | |
374 | |
375 TextRunHarfBuzz::~TextRunHarfBuzz() {} | |
376 | |
377 } // namespace internal | |
378 | |
379 RenderTextHarfBuzz::RenderTextHarfBuzz() | |
380 : RenderText(), | |
381 needs_layout_(false) {} | |
382 | |
383 RenderTextHarfBuzz::~RenderTextHarfBuzz() {} | |
384 | |
385 Size RenderTextHarfBuzz::GetStringSize() { | |
386 EnsureLayout(); | |
387 return Size(lines()[0].size.width(), font_list().GetHeight()); | |
388 } | |
389 | |
390 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) { | |
391 EnsureLayout(); | |
392 | |
393 int x = ToTextPoint(point).x(); | |
394 int offset = 0; | |
395 size_t run_index = GetRunContainingXCoord(x, &offset); | |
396 if (run_index >= runs_.size()) | |
397 return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT); | |
398 const internal::TextRunHarfBuzz& run = *runs_[run_index]; | |
399 | |
400 for (size_t i = 0; i < run.glyph_count; ++i) { | |
401 const SkScalar end = | |
402 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); | |
403 const SkScalar middle = (end + run.positions[i].x()) / 2; | |
404 const bool is_rtl = run.direction == UBIDI_RTL; | |
405 if (offset < middle) { | |
406 return SelectionModel(LayoutIndexToTextIndex( | |
407 run.glyph_to_char[i] + (is_rtl ? 1 : 0)), | |
408 (is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD)); | |
409 } | |
410 if (offset < end) { | |
411 return SelectionModel(LayoutIndexToTextIndex( | |
412 run.glyph_to_char[i] + (is_rtl ? 0 : 1)), | |
413 (is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD)); | |
414 } | |
415 } | |
416 return EdgeSelectionModel(CURSOR_RIGHT); | |
417 } | |
418 | |
419 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { | |
420 NOTIMPLEMENTED(); | |
421 return std::vector<RenderText::FontSpan>(); | |
422 } | |
423 | |
424 int RenderTextHarfBuzz::GetLayoutTextBaseline() { | |
425 EnsureLayout(); | |
426 return lines()[0].baseline; | |
427 } | |
428 | |
429 SelectionModel RenderTextHarfBuzz::AdjacentCharSelectionModel( | |
430 const SelectionModel& selection, | |
431 VisualCursorDirection direction) { | |
432 DCHECK(!needs_layout_); | |
433 internal::TextRunHarfBuzz* run; | |
434 size_t run_index = GetRunContainingCaret(selection); | |
435 if (run_index >= runs_.size()) { | |
436 // The cursor is not in any run: we're at the visual and logical edge. | |
437 SelectionModel edge = EdgeSelectionModel(direction); | |
438 if (edge.caret_pos() == selection.caret_pos()) | |
439 return edge; | |
440 int visual_index = (direction == CURSOR_RIGHT) ? 0 : runs_.size() - 1; | |
441 run = runs_[visual_to_logical_[visual_index]]; | |
442 } else { | |
443 // If the cursor is moving within the current run, just move it by one | |
444 // grapheme in the appropriate direction. | |
445 run = runs_[run_index]; | |
446 size_t caret = selection.caret_pos(); | |
447 bool forward_motion = | |
448 (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); | |
449 if (forward_motion) { | |
450 if (caret < LayoutIndexToTextIndex(run->range.end())) { | |
451 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); | |
452 return SelectionModel(caret, CURSOR_BACKWARD); | |
453 } | |
454 } else { | |
455 if (caret > LayoutIndexToTextIndex(run->range.start())) { | |
456 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); | |
457 return SelectionModel(caret, CURSOR_FORWARD); | |
458 } | |
459 } | |
460 // The cursor is at the edge of a run; move to the visually adjacent run. | |
461 int visual_index = logical_to_visual_[run_index]; | |
462 visual_index += (direction == CURSOR_LEFT) ? -1 : 1; | |
463 if (visual_index < 0 || visual_index >= static_cast<int>(runs_.size())) | |
464 return EdgeSelectionModel(direction); | |
465 run = runs_[visual_to_logical_[visual_index]]; | |
466 } | |
467 bool forward_motion = | |
468 (run->direction == UBIDI_RTL) == (direction == CURSOR_LEFT); | |
469 return forward_motion ? FirstSelectionModelInsideRun(run) : | |
470 LastSelectionModelInsideRun(run); | |
471 } | |
472 | |
473 SelectionModel RenderTextHarfBuzz::AdjacentWordSelectionModel( | |
474 const SelectionModel& selection, | |
475 VisualCursorDirection direction) { | |
476 // TODO(ckocagil): This implementation currently matches RenderTextWin, but it | |
477 // should match the native behavior on other platforms. | |
478 if (obscured()) | |
479 return EdgeSelectionModel(direction); | |
480 | |
481 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | |
482 bool success = iter.Init(); | |
483 DCHECK(success); | |
484 if (!success) | |
485 return selection; | |
486 | |
487 size_t pos; | |
488 if (direction == CURSOR_RIGHT) { | |
489 pos = std::min(selection.caret_pos() + 1, text().length()); | |
490 while (iter.Advance()) { | |
491 pos = iter.pos(); | |
492 if (iter.IsWord() && pos > selection.caret_pos()) | |
493 break; | |
494 } | |
495 } else { // direction == CURSOR_LEFT | |
496 // Notes: We always iterate words from the beginning. | |
497 // This is probably fast enough for our usage, but we may | |
498 // want to modify WordIterator so that it can start from the | |
499 // middle of string and advance backwards. | |
500 pos = std::max<int>(selection.caret_pos() - 1, 0); | |
501 while (iter.Advance()) { | |
502 if (iter.IsWord()) { | |
503 size_t begin = iter.pos() - iter.GetString().length(); | |
504 if (begin == selection.caret_pos()) { | |
505 // The cursor is at the beginning of a word. | |
506 // Move to previous word. | |
507 break; | |
508 } else if (iter.pos() >= selection.caret_pos()) { | |
509 // The cursor is in the middle or at the end of a word. | |
510 // Move to the top of current word. | |
511 pos = begin; | |
512 break; | |
513 } | |
514 pos = iter.pos() - iter.GetString().length(); | |
515 } | |
516 } | |
517 } | |
518 return SelectionModel(pos, CURSOR_FORWARD); | |
519 } | |
520 | |
521 Range RenderTextHarfBuzz::GetGlyphBounds(size_t index) { | |
522 const size_t run_index = | |
523 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD)); | |
524 // Return edge bounds if the index is invalid or beyond the layout text size. | |
525 if (run_index >= runs_.size()) | |
526 return Range(GetStringSize().width()); | |
527 const size_t layout_index = TextIndexToLayoutIndex(index); | |
528 return Range(GetGlyphXBoundary(run_index, layout_index, false), | |
529 GetGlyphXBoundary(run_index, layout_index, true)); | |
530 } | |
531 | |
532 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { | |
533 DCHECK(!needs_layout_); | |
534 DCHECK(Range(0, text().length()).Contains(range)); | |
535 Range layout_range(TextIndexToLayoutIndex(range.start()), | |
536 TextIndexToLayoutIndex(range.end())); | |
537 DCHECK(Range(0, GetLayoutText().length()).Contains(layout_range)); | |
538 | |
539 std::vector<Rect> rects; | |
540 if (layout_range.is_empty()) | |
541 return rects; | |
542 std::vector<Range> bounds; | |
543 | |
544 // Add a Range for each run/selection intersection. | |
545 // TODO(msw): The bounds should probably not always be leading the range ends. | |
546 for (size_t i = 0; i < runs_.size(); ++i) { | |
547 const internal::TextRunHarfBuzz* run = runs_[visual_to_logical_[i]]; | |
548 Range intersection = run->range.Intersect(layout_range); | |
549 if (intersection.IsValid()) { | |
550 DCHECK(!intersection.is_reversed()); | |
551 Range range_x(GetGlyphXBoundary(i, intersection.start(), false), | |
552 GetGlyphXBoundary(i, intersection.end(), false)); | |
553 if (range_x.is_empty()) | |
554 continue; | |
555 range_x = Range(range_x.GetMin(), range_x.GetMax()); | |
556 // Union this with the last range if they're adjacent. | |
557 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin()); | |
558 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { | |
559 range_x = Range(bounds.back().GetMin(), range_x.GetMax()); | |
560 bounds.pop_back(); | |
561 } | |
562 bounds.push_back(range_x); | |
563 } | |
564 } | |
565 for (size_t i = 0; i < bounds.size(); ++i) { | |
566 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); | |
567 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); | |
568 } | |
569 return rects; | |
570 } | |
571 | |
572 size_t RenderTextHarfBuzz::TextIndexToLayoutIndex(size_t index) const { | |
573 DCHECK_LE(index, text().length()); | |
574 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index; | |
575 CHECK_GE(i, 0); | |
576 // Clamp layout indices to the length of the text actually used for layout. | |
577 return std::min<size_t>(GetLayoutText().length(), i); | |
578 } | |
579 | |
580 size_t RenderTextHarfBuzz::LayoutIndexToTextIndex(size_t index) const { | |
581 if (!obscured()) | |
582 return index; | |
583 | |
584 DCHECK_LE(index, GetLayoutText().length()); | |
585 const size_t text_index = UTF16OffsetToIndex(text(), 0, index); | |
586 DCHECK_LE(text_index, text().length()); | |
587 return text_index; | |
588 } | |
589 | |
590 bool RenderTextHarfBuzz::IsValidCursorIndex(size_t index) { | |
591 if (index == 0 || index == text().length()) | |
592 return true; | |
593 if (!IsValidLogicalIndex(index)) | |
594 return false; | |
595 EnsureLayout(); | |
596 // Disallow indices amid multi-character graphemes by checking glyph bounds. | |
597 // These characters are not surrogate-pairs, but may yield a single glyph: | |
598 // \x0915\x093f - (ki) - one of many Devanagari biconsonantal conjuncts. | |
599 // \x0e08\x0e33 - (cho chan + sara am) - a Thai consonant and vowel pair. | |
600 return GetGlyphBounds(index) != GetGlyphBounds(index - 1); | |
601 } | |
602 | |
603 void RenderTextHarfBuzz::ResetLayout() { | |
604 needs_layout_ = true; | |
605 } | |
606 | |
607 void RenderTextHarfBuzz::EnsureLayout() { | |
608 if (needs_layout_) { | |
609 runs_.clear(); | |
610 | |
611 if (!GetLayoutText().empty()) { | |
612 ItemizeText(); | |
613 | |
614 for (size_t i = 0; i < runs_.size(); ++i) | |
615 ShapeRun(runs_[i]); | |
616 | |
617 // Precalculate run width information. | |
618 size_t preceding_run_widths = 0; | |
619 for (size_t i = 0; i < runs_.size(); ++i) { | |
620 internal::TextRunHarfBuzz* run = runs_[visual_to_logical_[i]]; | |
621 run->preceding_run_widths = preceding_run_widths; | |
622 preceding_run_widths += run->width; | |
623 } | |
624 } | |
625 | |
626 needs_layout_ = false; | |
627 std::vector<internal::Line> empty_lines; | |
628 set_lines(&empty_lines); | |
629 } | |
630 | |
631 if (lines().empty()) { | |
632 std::vector<internal::Line> lines; | |
633 lines.push_back(internal::Line()); | |
634 | |
635 int current_x = 0; | |
636 SkPaint paint; | |
637 | |
638 for (size_t i = 0; i < runs_.size(); ++i) { | |
639 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; | |
640 internal::LineSegment segment; | |
641 segment.x_range = Range(current_x, current_x + run.width); | |
642 segment.char_range = run.range; | |
643 segment.run = i; | |
644 lines[0].segments.push_back(segment); | |
645 | |
646 paint.setTypeface(run.skia_face.get()); | |
647 paint.setTextSize(run.font_size); | |
648 SkPaint::FontMetrics metrics; | |
649 paint.getFontMetrics(&metrics); | |
650 | |
651 lines[0].size.set_width(lines[0].size.width() + run.width); | |
652 lines[0].size.set_height(std::max(lines[0].size.height(), | |
653 SkScalarRoundToInt(metrics.fDescent - metrics.fAscent))); | |
654 lines[0].baseline = std::max(lines[0].baseline, | |
655 SkScalarRoundToInt(-metrics.fAscent)); | |
656 } | |
657 | |
658 set_lines(&lines); | |
659 } | |
660 } | |
661 | |
662 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { | |
663 DCHECK(!needs_layout_); | |
664 | |
665 int current_x = 0; | |
666 | |
667 internal::SkiaTextRenderer renderer(canvas); | |
668 ApplyFadeEffects(&renderer); | |
669 ApplyTextShadows(&renderer); | |
670 | |
671 #if defined(OS_WIN) | |
672 bool smoothing_enabled; | |
673 bool cleartype_enabled; | |
674 GetCachedFontSmoothingSettings(&smoothing_enabled, &cleartype_enabled); | |
675 // Note that |cleartype_enabled| corresponds to Skia's |enable_lcd_text|. | |
676 renderer.SetFontSmoothingSettings( | |
677 smoothing_enabled, cleartype_enabled && !background_is_transparent(), | |
678 smoothing_enabled /* subpixel_positioning */); | |
679 #endif | |
680 | |
681 ApplyCompositionAndSelectionStyles(); | |
682 | |
683 const Vector2d line_offset = GetLineOffset(0); | |
684 | |
685 for (size_t i = 0; i < runs_.size(); ++i) { | |
686 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; | |
687 renderer.SetTypeface(run.skia_face.get()); | |
688 renderer.SetTextSize(run.font_size); | |
689 | |
690 canvas->Save(); | |
691 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline); | |
692 canvas->Translate(origin); | |
693 | |
694 for (BreakList<SkColor>::const_iterator it = | |
695 colors().GetBreak(run.range.start()); | |
696 it != colors().breaks().end() && it->first < run.range.end(); | |
697 ++it) { | |
698 const Range intersection = colors().GetRange(it).Intersect(run.range); | |
699 const Range colored_glyphs = CharRangeToGlyphRange(run, intersection); | |
700 // The range may be empty if a portion of a multi-character grapheme is | |
701 // selected, yielding two colors for a single glyph. For now, this just | |
702 // paints the glyph with a single style, but it should paint it twice, | |
703 // clipped according to selection bounds. See http://crbug.com/366786 | |
704 if (colored_glyphs.is_empty()) | |
705 continue; | |
706 | |
707 renderer.SetForegroundColor(it->second); | |
708 renderer.DrawPosText(&run.positions[colored_glyphs.start()], | |
709 &run.glyphs[colored_glyphs.start()], | |
710 colored_glyphs.length()); | |
711 int width = (colored_glyphs.end() == run.glyph_count ? run.width : | |
712 run.positions[colored_glyphs.end()].x()) - | |
713 run.positions[colored_glyphs.start()].x(); | |
714 renderer.DrawDecorations(0, 0, width, run.underline, run.strike, | |
715 run.diagonal_strike); | |
716 } | |
717 | |
718 canvas->Restore(); | |
719 current_x += run.width; | |
720 } | |
721 | |
722 renderer.EndDiagonalStrike(); | |
723 | |
724 UndoCompositionAndSelectionStyles(); | |
725 } | |
726 | |
727 size_t RenderTextHarfBuzz::GetRunContainingCaret( | |
728 const SelectionModel& caret) const { | |
729 DCHECK(!needs_layout_); | |
730 size_t layout_position = TextIndexToLayoutIndex(caret.caret_pos()); | |
731 LogicalCursorDirection affinity = caret.caret_affinity(); | |
732 for (size_t run = 0; run < runs_.size(); ++run) { | |
733 if (RangeContainsCaret(runs_[run]->range, layout_position, affinity)) | |
734 return run; | |
735 } | |
736 return runs_.size(); | |
737 } | |
738 | |
739 size_t RenderTextHarfBuzz::GetRunContainingXCoord(int x, int* offset) const { | |
740 DCHECK(!needs_layout_); | |
741 if (x < 0) | |
742 return runs_.size(); | |
743 // Find the text run containing the argument point (assumed already offset). | |
744 int current_x = 0; | |
745 for (size_t i = 0; i < runs_.size(); ++i) { | |
746 size_t run = visual_to_logical_[i]; | |
747 current_x += runs_[run]->width; | |
748 if (x < current_x) { | |
749 *offset = x - (current_x - runs_[run]->width); | |
750 return run; | |
751 } | |
752 } | |
753 return runs_.size(); | |
754 } | |
755 | |
756 int RenderTextHarfBuzz::GetGlyphXBoundary(size_t run_index, | |
757 size_t text_index, | |
758 bool trailing) { | |
759 const internal::TextRunHarfBuzz& run = *runs_[run_index]; | |
760 int x = run.preceding_run_widths; | |
761 | |
762 Range glyph_range; | |
763 if (text_index == run.range.end()) { | |
764 trailing = true; | |
765 glyph_range = run.direction == UBIDI_LTR ? | |
766 Range(run.glyph_count - 1, run.glyph_count) : Range(0, 1); | |
767 } else { | |
768 glyph_range = CharRangeToGlyphRange(run, Range(text_index, text_index + 1)); | |
769 } | |
770 const int trailing_step = trailing ? 1 : 0; | |
771 const size_t glyph_pos = glyph_range.start() + | |
772 (run.direction == UBIDI_LTR ? trailing_step : (1 - trailing_step)); | |
773 x += glyph_pos < run.glyph_count ? | |
774 SkScalarRoundToInt(run.positions[glyph_pos].x()) : run.width; | |
775 return x; | |
776 } | |
777 | |
778 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( | |
779 const internal::TextRunHarfBuzz* run) { | |
780 size_t position = LayoutIndexToTextIndex(run->range.start()); | |
781 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); | |
782 return SelectionModel(position, CURSOR_BACKWARD); | |
783 } | |
784 | |
785 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( | |
786 const internal::TextRunHarfBuzz* run) { | |
787 size_t position = LayoutIndexToTextIndex(run->range.end()); | |
788 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | |
789 return SelectionModel(position, CURSOR_FORWARD); | |
790 } | |
791 | |
792 void RenderTextHarfBuzz::ItemizeText() { | |
793 const base::string16& text = GetLayoutText(); | |
794 const bool is_rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; | |
795 DCHECK_NE(0U, text.length()); | |
796 | |
797 bool fake_runs = false; | |
798 UErrorCode result = U_ZERO_ERROR; | |
799 | |
800 UBiDi* line = ubidi_openSized(text.length(), 0, &result); | |
801 if (U_FAILURE(result)) { | |
802 NOTREACHED(); | |
803 fake_runs = true; | |
804 } else { | |
805 ubidi_setPara(line, text.c_str(), text.length(), | |
806 is_rtl ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, NULL, | |
807 &result); | |
808 if (U_FAILURE(result)) { | |
809 NOTREACHED(); | |
810 fake_runs = true; | |
811 } | |
812 } | |
813 | |
814 // Temporarily apply composition underlines and selection colors. | |
815 ApplyCompositionAndSelectionStyles(); | |
816 | |
817 // Build the list of runs from the script items and ranged styles. Use an | |
818 // empty color BreakList to avoid breaking runs at color boundaries. | |
819 BreakList<SkColor> empty_colors; | |
820 empty_colors.SetMax(text.length()); | |
821 internal::StyleIterator style(empty_colors, styles()); | |
822 | |
823 for (size_t run_break = 0; run_break < text.length();) { | |
824 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; | |
825 run->range.set_start(run_break); | |
826 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | | |
827 (style.style(ITALIC) ? Font::ITALIC : 0); | |
828 run->strike = style.style(STRIKE); | |
829 run->diagonal_strike = style.style(DIAGONAL_STRIKE); | |
830 run->underline = style.style(UNDERLINE); | |
831 | |
832 if (fake_runs) { | |
833 run_break = text.length(); | |
834 } else { | |
835 int32 script_item_break = 0; | |
836 ubidi_getLogicalRun(line, run_break, &script_item_break, &run->level); | |
837 // Find the length and script of this script run. | |
838 script_item_break = ScriptInterval(text, run_break, | |
839 script_item_break - run_break, &run->script) + run_break; | |
840 | |
841 // Find the next break and advance the iterators as needed. | |
842 run_break = std::min(static_cast<size_t>(script_item_break), | |
843 TextIndexToLayoutIndex(style.GetRange().end())); | |
844 | |
845 // Break runs adjacent to character substrings in certain code blocks. | |
846 // This avoids using their fallback fonts for more characters than needed, | |
847 // in cases like "\x25B6 Media Title", etc. http://crbug.com/278913 | |
848 if (run_break > run->range.start()) { | |
849 const size_t run_start = run->range.start(); | |
850 const int32 run_length = static_cast<int32>(run_break - run_start); | |
851 base::i18n::UTF16CharIterator iter(text.c_str() + run_start, | |
852 run_length); | |
853 const UBlockCode first_block_code = ublock_getCode(iter.get()); | |
854 const bool first_block_unusual = IsUnusualBlockCode(first_block_code); | |
855 while (iter.Advance() && iter.array_pos() < run_length) { | |
856 const UBlockCode current_block_code = ublock_getCode(iter.get()); | |
857 if (current_block_code != first_block_code && | |
858 (first_block_unusual || IsUnusualBlockCode(current_block_code))) { | |
859 run_break = run_start + iter.array_pos(); | |
860 break; | |
861 } | |
862 } | |
863 } | |
864 } | |
865 | |
866 DCHECK(IsValidCodePointIndex(text, run_break)); | |
867 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); | |
868 run->range.set_end(run_break); | |
869 const UChar* uchar_start = ubidi_getText(line); | |
870 // TODO(ckocagil): Add |ubidi_getBaseDirection| to i18n::BiDiLineIterator | |
871 // and remove the bare ICU use here. | |
872 run->direction = ubidi_getBaseDirection(uchar_start + run->range.start(), | |
873 run->range.length()); | |
874 if (run->direction == UBIDI_NEUTRAL) | |
875 run->direction = is_rtl ? UBIDI_RTL : UBIDI_LTR; | |
876 runs_.push_back(run); | |
877 } | |
878 | |
879 ubidi_close(line); | |
880 | |
881 // Undo the temporarily applied composition underlines and selection colors. | |
882 UndoCompositionAndSelectionStyles(); | |
883 | |
884 const size_t num_runs = runs_.size(); | |
885 scoped_ptr<UBiDiLevel[]> levels(new UBiDiLevel[num_runs]); | |
886 for (size_t i = 0; i < num_runs; ++i) | |
887 levels[i] = runs_[i]->level; | |
888 visual_to_logical_.resize(num_runs); | |
889 ubidi_reorderVisual(levels.get(), num_runs, &visual_to_logical_[0]); | |
890 logical_to_visual_.resize(num_runs); | |
891 ubidi_reorderLogical(levels.get(), num_runs, &logical_to_visual_[0]); | |
892 } | |
893 | |
894 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { | |
895 const base::string16& text = GetLayoutText(); | |
896 // TODO(ckocagil|yukishiino): Implement font fallback. | |
897 const Font& primary_font = font_list().GetPrimaryFont(); | |
898 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(), | |
899 run->font_style); | |
900 run->font_size = primary_font.GetFontSize(); | |
901 | |
902 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), | |
903 run->font_size); | |
904 | |
905 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz | |
906 // buffer holds our text, run information to be used by the shaping engine, | |
907 // and the resulting glyph data. | |
908 hb_buffer_t* buffer = hb_buffer_create(); | |
909 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), | |
910 text.length(), run->range.start(), run->range.length()); | |
911 hb_buffer_set_script(buffer, hb_icu_script_to_script(run->script)); | |
912 hb_buffer_set_direction(buffer, | |
913 run->direction == UBIDI_LTR ? HB_DIRECTION_LTR : HB_DIRECTION_RTL); | |
914 // TODO(ckocagil): Should we determine the actual language? | |
915 hb_buffer_set_language(buffer, hb_language_get_default()); | |
916 | |
917 // Shape the text. | |
918 hb_shape(harfbuzz_font, buffer, NULL, 0); | |
919 | |
920 // Populate the run fields with the resulting glyph data in the buffer. | |
921 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &run->glyph_count); | |
922 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, | |
923 NULL); | |
924 run->glyphs.reset(new uint16[run->glyph_count]); | |
925 run->glyph_to_char.reset(new uint32[run->glyph_count]); | |
926 run->positions.reset(new SkPoint[run->glyph_count]); | |
927 for (size_t i = 0; i < run->glyph_count; ++i) { | |
928 run->glyphs[i] = infos[i].codepoint; | |
929 run->glyph_to_char[i] = infos[i].cluster; | |
930 const int x_offset = | |
931 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); | |
932 const int y_offset = | |
933 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); | |
934 run->positions[i].set(run->width + x_offset, y_offset); | |
935 run->width += | |
936 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); | |
937 } | |
938 | |
939 hb_buffer_destroy(buffer); | |
940 hb_font_destroy(harfbuzz_font); | |
941 } | |
942 | |
943 } // namespace gfx | |
OLD | NEW |