Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: ui/gfx/render_text_win.cc

Issue 23522018: RenderTextWin: Break runs between any two characters that are not in the same code block (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ui/gfx/render_text_unittest.cc ('K') | « ui/gfx/render_text_win.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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());
Alexei Svitkine (slow) 2013/09/04 21:52:36 Nit: Use |layout_text_length| here or get rid of i
ckocagil 2013/09/05 20:13:15 Done, used |layout_text_length| everywhere.
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 (!ui::IsValidCodePointIndex(layout_text, run_break))
Alexei Svitkine (slow) 2013/09/04 21:52:36 Move this into the block of the above if.
msw 2013/09/04 22:05:38 Should this loop? Does it need to handle multiple
ckocagil 2013/09/05 20:13:15 Done.
ckocagil 2013/09/05 20:13:15 There can't be more than 2 words in a UTF-16 chara
603 --run_break;
604
605 // Break runs between characters in different code blocks. This avoids using
606 // fallback fonts for more characters than needed. http://crbug.com/278913
607 size_t run_start = run->range.start();
Alexei Svitkine (slow) 2013/09/04 21:52:36 Nit: const
ckocagil 2013/09/05 20:13:15 We now modify this, see the updated code.
608 base::i18n::UTF16CharIterator iter(layout_text.c_str() + run_start,
609 layout_text.length() - run_start);
Alexei Svitkine (slow) 2013/09/04 21:52:36 This should be run.range.length().
ckocagil 2013/09/05 20:13:15 Done.
610 const UBlockCode first_block = ublock_getCode(iter.get());
Alexei Svitkine (slow) 2013/09/04 21:52:36 Does this work correctly if there's an empty run?
ckocagil 2013/09/05 20:13:15 Fixed.
611 while (iter.Advance() &&
612 static_cast<size_t>(iter.array_pos() + run_start) < run_break) {
Alexei Svitkine (slow) 2013/09/04 21:52:36 Instead of doing iter.array_pos() + run_start < ru
ckocagil 2013/09/05 20:13:15 Done. Also I removed the DCHECKs.
613 if (ublock_getCode(iter.get()) != first_block) {
614 DCHECK_LE(static_cast<size_t>(iter.array_pos() + run_start), run_break);
msw 2013/09/04 22:05:38 This seems unnecessary; it's checked by the loop.
ckocagil 2013/09/05 20:13:15 Done.
615 DCHECK_GT(static_cast<size_t>(iter.array_pos() + run_start),
616 run->range.start());
617 run_break = iter.array_pos() + run_start;
618 break;
msw 2013/09/04 22:05:38 I actually think the break is unnecessary, since t
ckocagil 2013/09/05 20:13:15 I replaced the |run_break| use in the loop conditi
619 }
620 }
621
Alexei Svitkine (slow) 2013/09/04 21:52:36 Add a DCHECK(ui::IsValidCodePointIndex(run_break))
ckocagil 2013/09/05 20:13:15 The code above tries not to disrupt runs that star
Alexei Svitkine (slow) 2013/09/05 20:32:41 Fair enough. Could you add a TODO and file a crbug
598 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); 622 style.UpdatePosition(LayoutIndexToTextIndex(run_break));
599 if (script_item_break == run_break) 623 if (script_item_break == run_break)
600 script_item++; 624 script_item++;
601 run->range.set_end(run_break); 625 run->range.set_end(run_break);
602 runs_.push_back(run); 626 runs_.push_back(run);
603 } 627 }
604 628
605 // Undo the temporarily applied composition underlines and selection colors. 629 // Undo the temporarily applied composition underlines and selection colors.
606 UndoCompositionAndSelectionStyles(); 630 UndoCompositionAndSelectionStyles();
607 } 631 }
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 size_t position = LayoutIndexToTextIndex(run->range.end()); 921 size_t position = LayoutIndexToTextIndex(run->range.end());
898 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 922 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
899 return SelectionModel(position, CURSOR_FORWARD); 923 return SelectionModel(position, CURSOR_FORWARD);
900 } 924 }
901 925
902 RenderText* RenderText::CreateInstance() { 926 RenderText* RenderText::CreateInstance() {
903 return new RenderTextWin; 927 return new RenderTextWin;
904 } 928 }
905 929
906 } // namespace gfx 930 } // namespace gfx
OLDNEW
« ui/gfx/render_text_unittest.cc ('K') | « ui/gfx/render_text_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698