OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/render_text_win.h" | 5 #include "ui/gfx/render_text_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/i18n/char_iterator.h" | |
10 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
16 #include "third_party/icu/source/common/unicode/uchar.h" | |
15 #include "ui/base/text/utf16_indexing.h" | 17 #include "ui/base/text/utf16_indexing.h" |
16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/font_fallback_win.h" | 19 #include "ui/gfx/font_fallback_win.h" |
18 #include "ui/gfx/font_smoothing_win.h" | 20 #include "ui/gfx/font_smoothing_win.h" |
19 #include "ui/gfx/platform_font_win.h" | 21 #include "ui/gfx/platform_font_win.h" |
20 | 22 |
21 namespace gfx { | 23 namespace gfx { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
536 runs_.clear(); | 538 runs_.clear(); |
537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 539 // Make |string_size_|'s height and |common_baseline_| tall enough to draw |
538 // often-used characters which are rendered with fonts in the font list. | 540 // often-used characters which are rendered with fonts in the font list. |
539 string_size_ = Size(0, font_list().GetHeight()); | 541 string_size_ = Size(0, font_list().GetHeight()); |
540 common_baseline_ = font_list().GetBaseline(); | 542 common_baseline_ = font_list().GetBaseline(); |
541 | 543 |
542 // Set Uniscribe's base text direction. | 544 // Set Uniscribe's base text direction. |
543 script_state_.uBidiLevel = | 545 script_state_.uBidiLevel = |
544 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0; | 546 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0; |
545 | 547 |
546 if (text().empty()) | 548 const base::string16& layout_text = GetLayoutText(); |
549 if (layout_text.empty()) | |
547 return; | 550 return; |
548 | 551 |
549 HRESULT hr = E_OUTOFMEMORY; | 552 HRESULT hr = E_OUTOFMEMORY; |
550 int script_items_count = 0; | 553 int script_items_count = 0; |
551 std::vector<SCRIPT_ITEM> script_items; | 554 std::vector<SCRIPT_ITEM> script_items; |
552 const size_t layout_text_length = GetLayoutText().length(); | 555 const size_t layout_text_length = layout_text.length(); |
553 // Ensure that |kMaxRuns| is attempted and the loop terminates afterward. | 556 // Ensure that |kMaxRuns| is attempted and the loop terminates afterward. |
554 for (size_t runs = kGuessRuns; hr == E_OUTOFMEMORY && runs <= kMaxRuns; | 557 for (size_t runs = kGuessRuns; hr == E_OUTOFMEMORY && runs <= kMaxRuns; |
555 runs = std::max(runs + 1, std::min(runs * 2, kMaxRuns))) { | 558 runs = std::max(runs + 1, std::min(runs * 2, kMaxRuns))) { |
556 // Derive the array of Uniscribe script items from the logical text. | 559 // Derive the array of Uniscribe script items from the logical text. |
557 // ScriptItemize always adds a terminal array item so that the length of | 560 // ScriptItemize always adds a terminal array item so that the length of |
558 // the last item can be derived from the terminal SCRIPT_ITEM::iCharPos. | 561 // the last item can be derived from the terminal SCRIPT_ITEM::iCharPos. |
559 script_items.resize(runs); | 562 script_items.resize(runs); |
560 hr = ScriptItemize(GetLayoutText().c_str(), layout_text_length, | 563 hr = ScriptItemize(layout_text.c_str(), layout_text_length, runs - 1, |
561 runs - 1, &script_control_, &script_state_, | 564 &script_control_, &script_state_, &script_items[0], |
562 &script_items[0], &script_items_count); | 565 &script_items_count); |
563 } | 566 } |
564 DCHECK(SUCCEEDED(hr)); | 567 DCHECK(SUCCEEDED(hr)); |
565 if (!SUCCEEDED(hr) || script_items_count <= 0) | 568 if (!SUCCEEDED(hr) || script_items_count <= 0) |
566 return; | 569 return; |
567 | 570 |
568 // Temporarily apply composition underlines and selection colors. | 571 // Temporarily apply composition underlines and selection colors. |
569 ApplyCompositionAndSelectionStyles(); | 572 ApplyCompositionAndSelectionStyles(); |
570 | 573 |
571 // Build the list of runs from the script items and ranged styles. Use an | 574 // Build the list of runs from the script items and ranged styles. Use an |
572 // empty color BreakList to avoid breaking runs at color boundaries. | 575 // empty color BreakList to avoid breaking runs at color boundaries. |
573 BreakList<SkColor> empty_colors; | 576 BreakList<SkColor> empty_colors; |
574 empty_colors.SetMax(text().length()); | 577 empty_colors.SetMax(layout_text_length); |
575 internal::StyleIterator style(empty_colors, styles()); | 578 internal::StyleIterator style(empty_colors, styles()); |
576 SCRIPT_ITEM* script_item = &script_items[0]; | 579 SCRIPT_ITEM* script_item = &script_items[0]; |
577 const size_t max_run_length = kMaxGlyphs / 2; | 580 const size_t max_run_length = kMaxGlyphs / 2; |
578 for (size_t run_break = 0; run_break < layout_text_length;) { | 581 for (size_t run_break = 0; run_break < layout_text_length;) { |
579 internal::TextRun* run = new internal::TextRun(); | 582 internal::TextRun* run = new internal::TextRun(); |
580 run->range.set_start(run_break); | 583 run->range.set_start(run_break); |
581 run->font = GetPrimaryFont(); | 584 run->font = GetPrimaryFont(); |
582 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | | 585 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | |
583 (style.style(ITALIC) ? Font::ITALIC : 0); | 586 (style.style(ITALIC) ? Font::ITALIC : 0); |
584 DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), | 587 DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), |
585 run->font_style, &run->font); | 588 run->font_style, &run->font); |
586 run->strike = style.style(STRIKE); | 589 run->strike = style.style(STRIKE); |
587 run->diagonal_strike = style.style(DIAGONAL_STRIKE); | 590 run->diagonal_strike = style.style(DIAGONAL_STRIKE); |
588 run->underline = style.style(UNDERLINE); | 591 run->underline = style.style(UNDERLINE); |
589 run->script_analysis = script_item->a; | 592 run->script_analysis = script_item->a; |
590 | 593 |
591 // Find the next break and advance the iterators as needed. | 594 // Find the next break and advance the iterators as needed. |
592 const size_t script_item_break = (script_item + 1)->iCharPos; | 595 const size_t script_item_break = (script_item + 1)->iCharPos; |
593 run_break = std::min(script_item_break, | 596 run_break = std::min(script_item_break, |
594 TextIndexToLayoutIndex(style.GetRange().end())); | 597 TextIndexToLayoutIndex(style.GetRange().end())); |
598 | |
595 // Clamp run lengths to avoid exceeding the maximum supported glyph count. | 599 // Clamp run lengths to avoid exceeding the maximum supported glyph count. |
596 if ((run_break - run->range.start()) > max_run_length) | 600 if ((run_break - run->range.start()) > max_run_length) { |
597 run_break = run->range.start() + max_run_length; | 601 run_break = run->range.start() + max_run_length; |
602 if (run_break < layout_text_length && | |
603 !ui::IsValidCodePointIndex(layout_text, run_break)) | |
Alexei Svitkine (slow)
2013/09/05 20:32:41
Nit: Add {}'s.
| |
604 --run_break; | |
605 } | |
606 | |
607 // Break runs between characters in different code blocks. This avoids using | |
608 // fallback fonts for more characters than needed. http://crbug.com/278913 | |
609 if (run_break > run->range.start()) { | |
610 size_t run_start = run->range.start(); | |
611 if (!ui::IsValidCodePointIndex(layout_text, run_start)) | |
Alexei Svitkine (slow)
2013/09/05 20:32:41
I'm not a fan of this logic here. Decrementing run
ckocagil
2013/09/05 21:08:57
|run_start| doesn't change |run->range.start()|. T
Alexei Svitkine (slow)
2013/09/06 13:28:42
Right, I understand this, but still am not a fan o
Alexei Svitkine (slow)
2013/09/06 13:36:18
Plus, looking at the UTF16CharIterator() code, it
| |
612 --run_start; | |
613 int32 run_length = static_cast<int32>(run_break - run_start); | |
614 if (!ui::IsValidCodePointIndex(layout_text, run_break)) | |
Alexei Svitkine (slow)
2013/09/05 20:32:41
In what cases could this be the case? For example,
ckocagil
2013/09/05 21:08:57
I'm not sure, style ranges maybe? Do they contain
msw
2013/09/06 16:51:28
Uniscribe or style ranges theoretically could, but
| |
615 ++run_length; | |
616 base::i18n::UTF16CharIterator iter(layout_text.c_str() + run_start, | |
617 run_length); | |
618 const UBlockCode first_block = ublock_getCode(iter.get()); | |
619 while (iter.Advance() && iter.array_pos() < run_length) { | |
620 if (ublock_getCode(iter.get()) != first_block) { | |
621 run_break = std::min(run_break, iter.array_pos() + run_start); | |
Alexei Svitkine (slow)
2013/09/05 20:32:41
The mins is only necessary because your code above
ckocagil
2013/09/05 21:08:57
No, it is necessary because of the ++run_length (t
| |
622 break; | |
623 } | |
624 } | |
625 } | |
626 | |
598 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); | 627 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); |
599 if (script_item_break == run_break) | 628 if (script_item_break == run_break) |
600 script_item++; | 629 script_item++; |
601 run->range.set_end(run_break); | 630 run->range.set_end(run_break); |
602 runs_.push_back(run); | 631 runs_.push_back(run); |
603 } | 632 } |
604 | 633 |
605 // Undo the temporarily applied composition underlines and selection colors. | 634 // Undo the temporarily applied composition underlines and selection colors. |
606 UndoCompositionAndSelectionStyles(); | 635 UndoCompositionAndSelectionStyles(); |
607 } | 636 } |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
897 size_t position = LayoutIndexToTextIndex(run->range.end()); | 926 size_t position = LayoutIndexToTextIndex(run->range.end()); |
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 927 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
899 return SelectionModel(position, CURSOR_FORWARD); | 928 return SelectionModel(position, CURSOR_FORWARD); |
900 } | 929 } |
901 | 930 |
902 RenderText* RenderText::CreateInstance() { | 931 RenderText* RenderText::CreateInstance() { |
903 return new RenderTextWin; | 932 return new RenderTextWin; |
904 } | 933 } |
905 | 934 |
906 } // namespace gfx | 935 } // namespace gfx |
OLD | NEW |