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" | |
11 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
12 #include "base/logging.h" | 11 #include "base/logging.h" |
13 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
15 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
16 #include "third_party/icu/source/common/unicode/uchar.h" | |
17 #include "ui/base/text/utf16_indexing.h" | 15 #include "ui/base/text/utf16_indexing.h" |
18 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/font_fallback_win.h" | 17 #include "ui/gfx/font_fallback_win.h" |
20 #include "ui/gfx/font_smoothing_win.h" | 18 #include "ui/gfx/font_smoothing_win.h" |
21 #include "ui/gfx/platform_font_win.h" | 19 #include "ui/gfx/platform_font_win.h" |
22 | 20 |
23 namespace gfx { | 21 namespace gfx { |
24 | 22 |
25 namespace { | 23 namespace { |
26 | 24 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 runs_.clear(); | 536 runs_.clear(); |
539 // Make |string_size_|'s height and |common_baseline_| tall enough to draw | 537 // Make |string_size_|'s height and |common_baseline_| tall enough to draw |
540 // often-used characters which are rendered with fonts in the font list. | 538 // often-used characters which are rendered with fonts in the font list. |
541 string_size_ = Size(0, font_list().GetHeight()); | 539 string_size_ = Size(0, font_list().GetHeight()); |
542 common_baseline_ = font_list().GetBaseline(); | 540 common_baseline_ = font_list().GetBaseline(); |
543 | 541 |
544 // Set Uniscribe's base text direction. | 542 // Set Uniscribe's base text direction. |
545 script_state_.uBidiLevel = | 543 script_state_.uBidiLevel = |
546 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0; | 544 (GetTextDirection() == base::i18n::RIGHT_TO_LEFT) ? 1 : 0; |
547 | 545 |
548 const base::string16& layout_text = GetLayoutText(); | 546 if (text().empty()) |
549 if (layout_text.empty()) | |
550 return; | 547 return; |
551 | 548 |
552 HRESULT hr = E_OUTOFMEMORY; | 549 HRESULT hr = E_OUTOFMEMORY; |
553 int script_items_count = 0; | 550 int script_items_count = 0; |
554 std::vector<SCRIPT_ITEM> script_items; | 551 std::vector<SCRIPT_ITEM> script_items; |
555 const size_t layout_text_length = layout_text.length(); | 552 const size_t layout_text_length = GetLayoutText().length(); |
556 // Ensure that |kMaxRuns| is attempted and the loop terminates afterward. | 553 // Ensure that |kMaxRuns| is attempted and the loop terminates afterward. |
557 for (size_t runs = kGuessRuns; hr == E_OUTOFMEMORY && runs <= kMaxRuns; | 554 for (size_t runs = kGuessRuns; hr == E_OUTOFMEMORY && runs <= kMaxRuns; |
558 runs = std::max(runs + 1, std::min(runs * 2, kMaxRuns))) { | 555 runs = std::max(runs + 1, std::min(runs * 2, kMaxRuns))) { |
559 // Derive the array of Uniscribe script items from the logical text. | 556 // Derive the array of Uniscribe script items from the logical text. |
560 // ScriptItemize always adds a terminal array item so that the length of | 557 // ScriptItemize always adds a terminal array item so that the length of |
561 // the last item can be derived from the terminal SCRIPT_ITEM::iCharPos. | 558 // the last item can be derived from the terminal SCRIPT_ITEM::iCharPos. |
562 script_items.resize(runs); | 559 script_items.resize(runs); |
563 hr = ScriptItemize(layout_text.c_str(), layout_text_length, runs - 1, | 560 hr = ScriptItemize(GetLayoutText().c_str(), layout_text_length, |
564 &script_control_, &script_state_, &script_items[0], | 561 runs - 1, &script_control_, &script_state_, |
565 &script_items_count); | 562 &script_items[0], &script_items_count); |
566 } | 563 } |
567 DCHECK(SUCCEEDED(hr)); | 564 DCHECK(SUCCEEDED(hr)); |
568 if (!SUCCEEDED(hr) || script_items_count <= 0) | 565 if (!SUCCEEDED(hr) || script_items_count <= 0) |
569 return; | 566 return; |
570 | 567 |
571 // Temporarily apply composition underlines and selection colors. | 568 // Temporarily apply composition underlines and selection colors. |
572 ApplyCompositionAndSelectionStyles(); | 569 ApplyCompositionAndSelectionStyles(); |
573 | 570 |
574 // Build the list of runs from the script items and ranged styles. Use an | 571 // Build the list of runs from the script items and ranged styles. Use an |
575 // empty color BreakList to avoid breaking runs at color boundaries. | 572 // empty color BreakList to avoid breaking runs at color boundaries. |
576 BreakList<SkColor> empty_colors; | 573 BreakList<SkColor> empty_colors; |
577 empty_colors.SetMax(layout_text_length); | 574 empty_colors.SetMax(text().length()); |
578 internal::StyleIterator style(empty_colors, styles()); | 575 internal::StyleIterator style(empty_colors, styles()); |
579 SCRIPT_ITEM* script_item = &script_items[0]; | 576 SCRIPT_ITEM* script_item = &script_items[0]; |
580 const size_t max_run_length = kMaxGlyphs / 2; | 577 const size_t max_run_length = kMaxGlyphs / 2; |
581 for (size_t run_break = 0; run_break < layout_text_length;) { | 578 for (size_t run_break = 0; run_break < layout_text_length;) { |
582 internal::TextRun* run = new internal::TextRun(); | 579 internal::TextRun* run = new internal::TextRun(); |
583 run->range.set_start(run_break); | 580 run->range.set_start(run_break); |
584 run->font = GetPrimaryFont(); | 581 run->font = GetPrimaryFont(); |
585 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | | 582 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | |
586 (style.style(ITALIC) ? Font::ITALIC : 0); | 583 (style.style(ITALIC) ? Font::ITALIC : 0); |
587 DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), | 584 DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), |
588 run->font_style, &run->font); | 585 run->font_style, &run->font); |
589 run->strike = style.style(STRIKE); | 586 run->strike = style.style(STRIKE); |
590 run->diagonal_strike = style.style(DIAGONAL_STRIKE); | 587 run->diagonal_strike = style.style(DIAGONAL_STRIKE); |
591 run->underline = style.style(UNDERLINE); | 588 run->underline = style.style(UNDERLINE); |
592 run->script_analysis = script_item->a; | 589 run->script_analysis = script_item->a; |
593 | 590 |
594 // Find the next break and advance the iterators as needed. | 591 // Find the next break and advance the iterators as needed. |
595 const size_t script_item_break = (script_item + 1)->iCharPos; | 592 const size_t script_item_break = (script_item + 1)->iCharPos; |
596 run_break = std::min(script_item_break, | 593 run_break = std::min(script_item_break, |
597 TextIndexToLayoutIndex(style.GetRange().end())); | 594 TextIndexToLayoutIndex(style.GetRange().end())); |
598 | |
599 // Clamp run lengths to avoid exceeding the maximum supported glyph count. | 595 // Clamp run lengths to avoid exceeding the maximum supported glyph count. |
600 if ((run_break - run->range.start()) > max_run_length) { | 596 if ((run_break - run->range.start()) > max_run_length) |
601 run_break = run->range.start() + max_run_length; | 597 run_break = run->range.start() + max_run_length; |
602 if (!ui::IsValidCodePointIndex(layout_text, run_break)) | |
603 --run_break; | |
604 } | |
605 | |
606 // Break runs between characters in different code blocks. This avoids using | |
607 // fallback fonts for more characters than needed. http://crbug.com/278913 | |
608 if (run_break > run->range.start()) { | |
609 const size_t run_start = run->range.start(); | |
610 const int32 run_length = static_cast<int32>(run_break - run_start); | |
611 base::i18n::UTF16CharIterator iter(layout_text.c_str() + run_start, | |
612 run_length); | |
613 const UBlockCode first_block_code = ublock_getCode(iter.get()); | |
614 while (iter.Advance() && iter.array_pos() < run_length) { | |
615 if (ublock_getCode(iter.get()) != first_block_code) { | |
616 run_break = run_start + iter.array_pos(); | |
617 break; | |
618 } | |
619 } | |
620 } | |
621 | |
622 DCHECK(ui::IsValidCodePointIndex(layout_text, run_break)); | |
623 | |
624 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); | 598 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); |
625 if (script_item_break == run_break) | 599 if (script_item_break == run_break) |
626 script_item++; | 600 script_item++; |
627 run->range.set_end(run_break); | 601 run->range.set_end(run_break); |
628 runs_.push_back(run); | 602 runs_.push_back(run); |
629 } | 603 } |
630 | 604 |
631 // Undo the temporarily applied composition underlines and selection colors. | 605 // Undo the temporarily applied composition underlines and selection colors. |
632 UndoCompositionAndSelectionStyles(); | 606 UndoCompositionAndSelectionStyles(); |
633 } | 607 } |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 size_t position = LayoutIndexToTextIndex(run->range.end()); | 896 size_t position = LayoutIndexToTextIndex(run->range.end()); |
923 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); | 897 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); |
924 return SelectionModel(position, CURSOR_FORWARD); | 898 return SelectionModel(position, CURSOR_FORWARD); |
925 } | 899 } |
926 | 900 |
927 RenderText* RenderText::CreateInstance() { | 901 RenderText* RenderText::CreateInstance() { |
928 return new RenderTextWin; | 902 return new RenderTextWin; |
929 } | 903 } |
930 | 904 |
931 } // namespace gfx | 905 } // namespace gfx |
OLD | NEW |