Chromium Code Reviews| 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/logging.h" | 10 #include "base/logging.h" |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 SCRIPT_FONTPROPERTIES font_properties; | 605 SCRIPT_FONTPROPERTIES font_properties; |
| 606 memset(&font_properties, 0, sizeof(font_properties)); | 606 memset(&font_properties, 0, sizeof(font_properties)); |
| 607 font_properties.cBytes = sizeof(SCRIPT_FONTPROPERTIES); | 607 font_properties.cBytes = sizeof(SCRIPT_FONTPROPERTIES); |
| 608 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &font_properties); | 608 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &font_properties); |
| 609 | 609 |
| 610 run->logical_clusters.reset(new WORD[run_length]); | 610 run->logical_clusters.reset(new WORD[run_length]); |
| 611 run->glyph_count = 0; | 611 run->glyph_count = 0; |
| 612 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx | 612 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx |
| 613 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); | 613 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); |
| 614 while (max_glyphs < kMaxGlyphs) { | 614 while (max_glyphs < kMaxGlyphs) { |
| 615 bool glyphs_missing = false; | |
| 615 run->glyphs.reset(new WORD[max_glyphs]); | 616 run->glyphs.reset(new WORD[max_glyphs]); |
| 616 run->visible_attributes.reset(new SCRIPT_VISATTR[max_glyphs]); | 617 run->visible_attributes.reset(new SCRIPT_VISATTR[max_glyphs]); |
| 617 hr = ScriptShape(cached_hdc_, | 618 hr = ScriptShape(cached_hdc_, |
| 618 &run->script_cache, | 619 &run->script_cache, |
| 619 run_text, | 620 run_text, |
| 620 run_length, | 621 run_length, |
| 621 max_glyphs, | 622 max_glyphs, |
| 622 &(run->script_analysis), | 623 &(run->script_analysis), |
| 623 run->glyphs.get(), | 624 run->glyphs.get(), |
| 624 run->logical_clusters.get(), | 625 run->logical_clusters.get(), |
| 625 run->visible_attributes.get(), | 626 run->visible_attributes.get(), |
| 626 &(run->glyph_count)); | 627 &(run->glyph_count)); |
| 627 if (hr == E_OUTOFMEMORY) { | 628 if (hr == E_OUTOFMEMORY) { |
| 628 max_glyphs *= 2; | 629 max_glyphs *= 2; |
| 630 continue; | |
| 631 } else if (hr == USP_E_SCRIPT_NOT_IN_FONT) { | |
| 632 glyphs_missing = true; | |
| 629 } else if (hr == S_OK) { | 633 } else if (hr == S_OK) { |
| 630 // If |hr| is S_OK, there could still be missing glyphs in the output, | 634 // If |hr| is S_OK, there could still be missing glyphs in the output, |
| 631 // see: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368564 .aspx | 635 // see: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368564 .aspx |
| 632 // | 636 for (int i = 0; i < run->glyph_count; ++i) { |
| 633 // If there are missing glyphs, use font linking to try to find a | |
| 634 // matching font. | |
| 635 bool glyphs_missing = false; | |
| 636 for (int i = 0; i < run->glyph_count; i++) { | |
| 637 if (run->glyphs[i] == font_properties.wgDefault) { | 637 if (run->glyphs[i] == font_properties.wgDefault) { |
| 638 glyphs_missing = true; | 638 glyphs_missing = true; |
| 639 break; | 639 break; |
| 640 } | 640 } |
| 641 } | 641 } |
| 642 // No glyphs missing - good to go. | 642 } |
| 643 if (!glyphs_missing) | |
| 644 break; | |
| 645 | 643 |
| 646 // First time through, get the linked fonts list. | 644 // If there are no missing glyphs, we're done. |
|
msw
2012/04/12 19:58:32
nit: Try to avoid "we" and similar words, here and
Alexei Svitkine (slow)
2012/04/12 20:40:24
Done.
| |
| 647 if (linked_fonts == NULL) | 645 if (!glyphs_missing) |
| 648 linked_fonts = GetLinkedFonts(run->font); | 646 break; |
| 649 | 647 |
| 650 // None of the linked fonts worked - break out of the loop. | 648 // If there are missing glyphs, first, try finding a fallback font using a |
|
msw
2012/04/12 19:58:32
nit: Remove the comma after "first,".
Alexei Svitkine (slow)
2012/04/12 20:40:24
Done.
| |
| 651 if (linked_font_index == linked_fonts->size()) | 649 // meta file, if it hasn't yet been attempted for this run. |
| 652 break; | 650 // TODO(msw|asvitkine): Support RenderText's font_list()? |
| 653 | 651 if (!tried_fallback) { |
| 654 // Try the next linked font. | 652 tried_fallback = true; |
| 655 run->font = linked_fonts->at(linked_font_index++); | |
| 656 DeriveFontIfNecessary(font_size, run->font_style, &run->font); | |
| 657 ScriptFreeCache(&run->script_cache); | |
| 658 SelectObject(cached_hdc_, run->font.GetNativeFont()); | |
| 659 } else if (hr == USP_E_SCRIPT_NOT_IN_FONT) { | |
| 660 // Only try font fallback if it hasn't yet been attempted for this run. | |
| 661 if (tried_fallback) { | |
| 662 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can | |
| 663 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. | |
| 664 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 | |
| 665 // And http://maxradi.us/documents/uniscribe/ | |
| 666 run->script_analysis.eScript = SCRIPT_UNDEFINED; | |
| 667 // Reset |hr| to 0 to not trigger the DCHECK() below when a font is | |
| 668 // not found that can display the text. This is expected behavior | |
| 669 // under Windows XP without additional language packs installed and | |
| 670 // may also happen on newer versions when trying to display text in | |
| 671 // an obscure script that the system doesn't have the right font for. | |
| 672 hr = 0; | |
| 673 break; | |
| 674 } | |
| 675 | |
| 676 // The run's font doesn't contain the required glyphs, use an alternate. | |
| 677 // TODO(msw): support RenderText's font_list(). | |
| 678 if (ChooseFallbackFont(cached_hdc_, run->font, run_text, run_length, | 653 if (ChooseFallbackFont(cached_hdc_, run->font, run_text, run_length, |
| 679 &run->font)) { | 654 &run->font)) { |
| 680 DeriveFontIfNecessary(font_size, run->font_style, &run->font); | 655 DeriveFontIfNecessary(font_size, run->font_style, &run->font); |
|
msw
2012/04/12 19:58:32
This 3-line block is duplicated with line 685-687.
Alexei Svitkine (slow)
2012/04/12 20:40:24
Done.
| |
| 681 ScriptFreeCache(&run->script_cache); | 656 ScriptFreeCache(&run->script_cache); |
| 682 SelectObject(cached_hdc_, run->font.GetNativeFont()); | 657 SelectObject(cached_hdc_, run->font.GetNativeFont()); |
| 658 continue; | |
| 683 } | 659 } |
| 660 } | |
| 684 | 661 |
| 685 tried_fallback = true; | 662 // We didn't find a font using a meta file, try to find one using font |
|
msw
2012/04/12 19:58:32
nit: "We"
Alexei Svitkine (slow)
2012/04/12 20:40:24
Done.
| |
| 686 } else { | 663 // linking. First time through, get the linked fonts list. |
| 664 if (linked_fonts == NULL) | |
| 665 linked_fonts = GetLinkedFonts(run->font); | |
| 666 | |
| 667 // None of the linked fonts worked - break out of the loop. | |
| 668 if (linked_font_index == linked_fonts->size()) { | |
| 669 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can | |
| 670 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. | |
| 671 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 | |
| 672 // And http://maxradi.us/documents/uniscribe/ | |
| 673 run->script_analysis.eScript = SCRIPT_UNDEFINED; | |
| 674 // Reset |hr| to 0 to not trigger the DCHECK() below when a font is | |
| 675 // not found that can display the text. This is expected behavior | |
| 676 // under Windows XP without additional language packs installed and | |
| 677 // may also happen on newer versions when trying to display text in | |
| 678 // an obscure script that the system doesn't have the right font for. | |
| 679 hr = 0; | |
| 687 break; | 680 break; |
| 688 } | 681 } |
| 682 | |
| 683 // Try the next linked font. | |
| 684 run->font = linked_fonts->at(linked_font_index++); | |
| 685 DeriveFontIfNecessary(font_size, run->font_style, &run->font); | |
| 686 ScriptFreeCache(&run->script_cache); | |
| 687 SelectObject(cached_hdc_, run->font.GetNativeFont()); | |
| 689 } | 688 } |
| 690 DCHECK(SUCCEEDED(hr)); | 689 DCHECK(SUCCEEDED(hr)); |
| 691 string_size_.set_height(std::max(string_size_.height(), | 690 string_size_.set_height(std::max(string_size_.height(), |
| 692 run->font.GetHeight())); | 691 run->font.GetHeight())); |
| 693 common_baseline_ = std::max(common_baseline_, run->font.GetBaseline()); | 692 common_baseline_ = std::max(common_baseline_, run->font.GetBaseline()); |
| 694 | 693 |
| 695 if (run->glyph_count > 0) { | 694 if (run->glyph_count > 0) { |
| 696 run->advance_widths.reset(new int[run->glyph_count]); | 695 run->advance_widths.reset(new int[run->glyph_count]); |
| 697 run->offsets.reset(new GOFFSET[run->glyph_count]); | 696 run->offsets.reset(new GOFFSET[run->glyph_count]); |
| 698 hr = ScriptPlace(cached_hdc_, | 697 hr = ScriptPlace(cached_hdc_, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 779 internal::TextRun* run) { | 778 internal::TextRun* run) { |
| 780 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); | 779 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); |
| 781 return SelectionModel(caret, CURSOR_FORWARD); | 780 return SelectionModel(caret, CURSOR_FORWARD); |
| 782 } | 781 } |
| 783 | 782 |
| 784 RenderText* RenderText::CreateRenderText() { | 783 RenderText* RenderText::CreateRenderText() { |
| 785 return new RenderTextWin; | 784 return new RenderTextWin; |
| 786 } | 785 } |
| 787 | 786 |
| 788 } // namespace gfx | 787 } // namespace gfx |
| OLD | NEW |