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/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 672 string_size_.set_height(0); | 672 string_size_.set_height(0); |
| 673 for (size_t i = 0; i < runs_.size(); ++i) { | 673 for (size_t i = 0; i < runs_.size(); ++i) { |
| 674 internal::TextRun* run = runs_[i]; | 674 internal::TextRun* run = runs_[i]; |
| 675 const size_t run_length = run->range.length(); | 675 const size_t run_length = run->range.length(); |
| 676 const wchar_t* run_text = &(text()[run->range.start()]); | 676 const wchar_t* run_text = &(text()[run->range.start()]); |
| 677 bool tried_cached_font = false; | 677 bool tried_cached_font = false; |
| 678 bool tried_fallback = false; | 678 bool tried_fallback = false; |
| 679 size_t linked_font_index = 0; | 679 size_t linked_font_index = 0; |
| 680 const std::vector<Font>* linked_fonts = NULL; | 680 const std::vector<Font>* linked_fonts = NULL; |
| 681 Font original_font = run->font; | 681 Font original_font = run->font; |
| 682 // Keep track of the font producing the least missing glyphs for which | |
| 683 // ScriptShape() returned S_OK. This font will be used in the case where | |
| 684 // no font is able to display all the characters in this run. | |
| 685 int least_missing_count = INT_MAX; | |
|
msw
2012/07/10 20:52:15
nit: optionally rename this for more context, like
Alexei Svitkine (slow)
2012/07/10 21:16:35
Done.
| |
| 686 Font best_partial_font = run->font; | |
| 687 bool using_best_partial_font = false; | |
| 682 | 688 |
| 683 // Select the font desired for glyph generation. | 689 // Select the font desired for glyph generation. |
| 684 SelectObject(cached_hdc_, run->font.GetNativeFont()); | 690 SelectObject(cached_hdc_, run->font.GetNativeFont()); |
| 685 | 691 |
| 686 run->logical_clusters.reset(new WORD[run_length]); | 692 run->logical_clusters.reset(new WORD[run_length]); |
| 687 run->glyph_count = 0; | 693 run->glyph_count = 0; |
| 688 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx | 694 // Max glyph guess: http://msdn.microsoft.com/en-us/library/dd368564.aspx |
| 689 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); | 695 size_t max_glyphs = static_cast<size_t>(1.5 * run_length + 16); |
| 690 while (max_glyphs < kMaxGlyphs) { | 696 while (max_glyphs < kMaxGlyphs) { |
| 691 run->glyphs.reset(new WORD[max_glyphs]); | 697 run->glyphs.reset(new WORD[max_glyphs]); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 704 max_glyphs *= 2; | 710 max_glyphs *= 2; |
| 705 continue; | 711 continue; |
| 706 } | 712 } |
| 707 | 713 |
| 708 bool glyphs_missing = false; | 714 bool glyphs_missing = false; |
| 709 if (hr == USP_E_SCRIPT_NOT_IN_FONT) { | 715 if (hr == USP_E_SCRIPT_NOT_IN_FONT) { |
| 710 glyphs_missing = true; | 716 glyphs_missing = true; |
| 711 } else if (hr == S_OK) { | 717 } else if (hr == S_OK) { |
| 712 // If |hr| is S_OK, there could still be missing glyphs in the output, | 718 // If |hr| is S_OK, there could still be missing glyphs in the output, |
| 713 // see: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368564 .aspx | 719 // see: http://msdn.microsoft.com/en-us/library/windows/desktop/dd368564 .aspx |
| 714 glyphs_missing = HasMissingGlyphs(run); | 720 const int missing_count = CountCharsWithMissingGlyphs(run); |
| 721 // Track the font that produced the least missing glyphs. | |
| 722 if (missing_count < least_missing_count) { | |
| 723 least_missing_count = missing_count; | |
| 724 best_partial_font = run->font; | |
| 725 } | |
| 726 glyphs_missing = (missing_count != 0); | |
| 715 } | 727 } |
| 716 | 728 |
| 717 // Skip font substitution if there are no missing glyphs. | 729 // Skip font substitution if there are no missing glyphs or if the font |
| 718 if (!glyphs_missing) { | 730 // with the least missing glyphs is being used as a last resort. |
| 731 if (!glyphs_missing || using_best_partial_font) { | |
| 719 // Save the successful fallback font that was chosen. | 732 // Save the successful fallback font that was chosen. |
| 720 if (tried_fallback) | 733 if (tried_fallback && !using_best_partial_font) |
| 721 successful_substitute_fonts_[original_font.GetFontName()] = run->font; | 734 successful_substitute_fonts_[original_font.GetFontName()] = run->font; |
| 722 break; | 735 break; |
| 723 } | 736 } |
| 724 | 737 |
| 725 // First, try the cached font from previous runs, if any. | 738 // First, try the cached font from previous runs, if any. |
| 726 if (!tried_cached_font) { | 739 if (!tried_cached_font) { |
| 727 tried_cached_font = true; | 740 tried_cached_font = true; |
| 728 std::map<std::string, Font>::const_iterator it = | 741 std::map<std::string, Font>::const_iterator it = |
| 729 successful_substitute_fonts_.find(original_font.GetFontName()); | 742 successful_substitute_fonts_.find(original_font.GetFontName()); |
| 730 if (it != successful_substitute_fonts_.end()) { | 743 if (it != successful_substitute_fonts_.end()) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 758 // ones for the Uniscribe fallback font. This may happen if the first | 771 // ones for the Uniscribe fallback font. This may happen if the first |
| 759 // font is a custom font that has no linked fonts in the Registry. | 772 // font is a custom font that has no linked fonts in the Registry. |
| 760 // | 773 // |
| 761 // Note: One possibility would be to always merge both lists of fonts, | 774 // Note: One possibility would be to always merge both lists of fonts, |
| 762 // but it is not clear whether there are any real world scenarios | 775 // but it is not clear whether there are any real world scenarios |
| 763 // where this would actually help. | 776 // where this would actually help. |
| 764 if (linked_fonts->empty()) | 777 if (linked_fonts->empty()) |
| 765 linked_fonts = GetLinkedFonts(run->font); | 778 linked_fonts = GetLinkedFonts(run->font); |
| 766 } | 779 } |
| 767 | 780 |
| 768 // None of the linked fonts worked, break out of the loop. | 781 // None of the fallback fonts were able to display the entire run. |
| 769 if (linked_font_index == linked_fonts->size()) { | 782 if (linked_font_index == linked_fonts->size()) { |
| 783 // If there was a font that was able to partially display the run, use | |
|
msw
2012/07/10 20:52:15
nit: make one-liner as "If a font could partially
Alexei Svitkine (slow)
2012/07/10 21:16:35
Done.
| |
| 784 // that now. | |
| 785 if (least_missing_count != INT_MAX) { | |
| 786 ApplySubstituteFont(run, best_partial_font); | |
| 787 using_best_partial_font = true; | |
| 788 continue; | |
| 789 } | |
| 790 | |
| 791 // If no font was able to partially display the run, replace all glyphs | |
|
msw
2012/07/10 20:52:15
In the bug, you wrote (and I agree that) "In the f
Alexei Svitkine (slow)
2012/07/10 21:16:35
You're right that this is not solving that specifi
msw
2012/07/10 21:40:24
sgtm
| |
| 792 // with |wgDefault| to ensure they don't hold garbage values. | |
| 793 SCRIPT_FONTPROPERTIES properties; | |
| 794 memset(&properties, 0, sizeof(properties)); | |
| 795 properties.cBytes = sizeof(properties); | |
| 796 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); | |
| 797 for (int i = 0; i < run->glyph_count; ++i) | |
| 798 run->glyphs[i] = properties.wgDefault; | |
| 799 | |
| 770 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can | 800 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can |
| 771 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. | 801 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. |
| 772 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 | 802 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 |
| 773 // And http://maxradi.us/documents/uniscribe/ | 803 // And http://maxradi.us/documents/uniscribe/ |
| 774 run->script_analysis.eScript = SCRIPT_UNDEFINED; | 804 run->script_analysis.eScript = SCRIPT_UNDEFINED; |
| 775 // Reset |hr| to 0 to not trigger the DCHECK() below when a font is | 805 // Reset |hr| to 0 to not trigger the DCHECK() below when a font is |
| 776 // not found that can display the text. This is expected behavior | 806 // not found that can display the text. This is expected behavior |
| 777 // under Windows XP without additional language packs installed and | 807 // under Windows XP without additional language packs installed and |
| 778 // may also happen on newer versions when trying to display text in | 808 // may also happen on newer versions when trying to display text in |
| 779 // an obscure script that the system doesn't have the right font for. | 809 // an obscure script that the system doesn't have the right font for. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 834 void RenderTextWin::ApplySubstituteFont(internal::TextRun* run, | 864 void RenderTextWin::ApplySubstituteFont(internal::TextRun* run, |
| 835 const Font& font) { | 865 const Font& font) { |
| 836 const int font_size = run->font.GetFontSize(); | 866 const int font_size = run->font.GetFontSize(); |
| 837 const int font_height = run->font.GetHeight(); | 867 const int font_height = run->font.GetHeight(); |
| 838 run->font = font; | 868 run->font = font; |
| 839 DeriveFontIfNecessary(font_size, font_height, run->font_style, &run->font); | 869 DeriveFontIfNecessary(font_size, font_height, run->font_style, &run->font); |
| 840 ScriptFreeCache(&run->script_cache); | 870 ScriptFreeCache(&run->script_cache); |
| 841 SelectObject(cached_hdc_, run->font.GetNativeFont()); | 871 SelectObject(cached_hdc_, run->font.GetNativeFont()); |
| 842 } | 872 } |
| 843 | 873 |
| 844 bool RenderTextWin::HasMissingGlyphs(internal::TextRun* run) const { | 874 int RenderTextWin::CountCharsWithMissingGlyphs(internal::TextRun* run) const { |
| 875 int chars_not_missing_glyphs = 0; | |
|
msw
2012/07/10 20:52:15
Isn't this counting the number glyphs found? (not
Alexei Svitkine (slow)
2012/07/10 21:16:35
It is counting chars, in fact, due to how the loop
| |
| 845 SCRIPT_FONTPROPERTIES properties; | 876 SCRIPT_FONTPROPERTIES properties; |
| 846 memset(&properties, 0, sizeof(properties)); | 877 memset(&properties, 0, sizeof(properties)); |
| 847 properties.cBytes = sizeof(properties); | 878 properties.cBytes = sizeof(properties); |
| 848 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); | 879 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); |
| 849 | 880 |
| 850 const wchar_t* run_text = &(text()[run->range.start()]); | 881 const wchar_t* run_text = &(text()[run->range.start()]); |
| 851 for (size_t char_index = 0; char_index < run->range.length(); ++char_index) { | 882 for (size_t char_index = 0; char_index < run->range.length(); ++char_index) { |
| 852 const int glyph_index = run->logical_clusters[char_index]; | 883 const int glyph_index = run->logical_clusters[char_index]; |
| 853 DCHECK_GE(glyph_index, 0); | 884 DCHECK_GE(glyph_index, 0); |
| 854 DCHECK_LT(glyph_index, run->glyph_count); | 885 DCHECK_LT(glyph_index, run->glyph_count); |
| 855 | 886 |
| 856 if (run->glyphs[glyph_index] == properties.wgDefault) | 887 if (run->glyphs[glyph_index] == properties.wgDefault) |
| 857 return true; | 888 continue; |
| 858 | 889 |
| 859 // Windows Vista sometimes returns glyphs equal to wgBlank (instead of | 890 // Windows Vista sometimes returns glyphs equal to wgBlank (instead of |
| 860 // wgDefault), with fZeroWidth set. Treat such cases as having missing | 891 // wgDefault), with fZeroWidth set. Treat such cases as having missing |
| 861 // glyphs if the corresponding character is not whitespace. | 892 // glyphs if the corresponding character is not whitespace. |
| 862 // See: http://crbug.com/125629 | 893 // See: http://crbug.com/125629 |
| 863 if (run->glyphs[glyph_index] == properties.wgBlank && | 894 if (run->glyphs[glyph_index] == properties.wgBlank && |
| 864 run->visible_attributes[glyph_index].fZeroWidth && | 895 run->visible_attributes[glyph_index].fZeroWidth && |
| 865 !IsWhitespace(run_text[char_index]) && | 896 !IsWhitespace(run_text[char_index]) && |
| 866 !IsUnicodeBidiControlCharacter(run_text[char_index])) { | 897 !IsUnicodeBidiControlCharacter(run_text[char_index])) { |
| 867 return true; | 898 continue; |
| 868 } | 899 } |
| 900 | |
| 901 ++chars_not_missing_glyphs; | |
| 869 } | 902 } |
| 870 | 903 |
| 871 return false; | 904 return run->range.length() - chars_not_missing_glyphs; |
|
msw
2012/07/10 20:52:15
Shouldn't this be "return run->glyph_count - glyph
Alexei Svitkine (slow)
2012/07/10 21:16:35
Nope, it shouldn't. See above.
It can't be negati
| |
| 872 } | 905 } |
| 873 | 906 |
| 874 const std::vector<Font>* RenderTextWin::GetLinkedFonts(const Font& font) const { | 907 const std::vector<Font>* RenderTextWin::GetLinkedFonts(const Font& font) const { |
| 875 const std::string& font_name = font.GetFontName(); | 908 const std::string& font_name = font.GetFontName(); |
| 876 std::map<std::string, std::vector<Font> >::const_iterator it = | 909 std::map<std::string, std::vector<Font> >::const_iterator it = |
| 877 cached_linked_fonts_.find(font_name); | 910 cached_linked_fonts_.find(font_name); |
| 878 if (it != cached_linked_fonts_.end()) | 911 if (it != cached_linked_fonts_.end()) |
| 879 return &it->second; | 912 return &it->second; |
| 880 | 913 |
| 881 cached_linked_fonts_[font_name] = std::vector<Font>(); | 914 cached_linked_fonts_[font_name] = std::vector<Font>(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 916 const internal::TextRun* run) { | 949 const internal::TextRun* run) { |
| 917 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); | 950 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); |
| 918 return SelectionModel(caret, CURSOR_FORWARD); | 951 return SelectionModel(caret, CURSOR_FORWARD); |
| 919 } | 952 } |
| 920 | 953 |
| 921 RenderText* RenderText::CreateInstance() { | 954 RenderText* RenderText::CreateInstance() { |
| 922 return new RenderTextWin; | 955 return new RenderTextWin; |
| 923 } | 956 } |
| 924 | 957 |
| 925 } // namespace gfx | 958 } // namespace gfx |
| OLD | NEW |