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 that is able to display the greatest number of |
| 683 // characters for which ScriptShape() returned S_OK. This font will be used |
| 684 // in the case where no font is able to display the entire run. |
| 685 int best_partial_font_missing_char_count = INT_MAX; |
| 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 < best_partial_font_missing_char_count) { |
| 723 best_partial_font_missing_char_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 a font was able to partially display the run, use that now. |
| 784 if (best_partial_font_missing_char_count != INT_MAX) { |
| 785 ApplySubstituteFont(run, best_partial_font); |
| 786 using_best_partial_font = true; |
| 787 continue; |
| 788 } |
| 789 |
| 790 // If no font was able to partially display the run, replace all glyphs |
| 791 // with |wgDefault| to ensure they don't hold garbage values. |
| 792 SCRIPT_FONTPROPERTIES properties; |
| 793 memset(&properties, 0, sizeof(properties)); |
| 794 properties.cBytes = sizeof(properties); |
| 795 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); |
| 796 for (int i = 0; i < run->glyph_count; ++i) |
| 797 run->glyphs[i] = properties.wgDefault; |
| 798 |
770 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can | 799 // TODO(msw): Don't use SCRIPT_UNDEFINED. Apparently Uniscribe can |
771 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. | 800 // crash on certain surrogate pairs with SCRIPT_UNDEFINED. |
772 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 | 801 // See https://bugzilla.mozilla.org/show_bug.cgi?id=341500 |
773 // And http://maxradi.us/documents/uniscribe/ | 802 // And http://maxradi.us/documents/uniscribe/ |
774 run->script_analysis.eScript = SCRIPT_UNDEFINED; | 803 run->script_analysis.eScript = SCRIPT_UNDEFINED; |
775 // Reset |hr| to 0 to not trigger the DCHECK() below when a font is | 804 // 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 | 805 // not found that can display the text. This is expected behavior |
777 // under Windows XP without additional language packs installed and | 806 // under Windows XP without additional language packs installed and |
778 // may also happen on newer versions when trying to display text in | 807 // 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. | 808 // 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, | 863 void RenderTextWin::ApplySubstituteFont(internal::TextRun* run, |
835 const Font& font) { | 864 const Font& font) { |
836 const int font_size = run->font.GetFontSize(); | 865 const int font_size = run->font.GetFontSize(); |
837 const int font_height = run->font.GetHeight(); | 866 const int font_height = run->font.GetHeight(); |
838 run->font = font; | 867 run->font = font; |
839 DeriveFontIfNecessary(font_size, font_height, run->font_style, &run->font); | 868 DeriveFontIfNecessary(font_size, font_height, run->font_style, &run->font); |
840 ScriptFreeCache(&run->script_cache); | 869 ScriptFreeCache(&run->script_cache); |
841 SelectObject(cached_hdc_, run->font.GetNativeFont()); | 870 SelectObject(cached_hdc_, run->font.GetNativeFont()); |
842 } | 871 } |
843 | 872 |
844 bool RenderTextWin::HasMissingGlyphs(internal::TextRun* run) const { | 873 int RenderTextWin::CountCharsWithMissingGlyphs(internal::TextRun* run) const { |
| 874 int chars_not_missing_glyphs = 0; |
845 SCRIPT_FONTPROPERTIES properties; | 875 SCRIPT_FONTPROPERTIES properties; |
846 memset(&properties, 0, sizeof(properties)); | 876 memset(&properties, 0, sizeof(properties)); |
847 properties.cBytes = sizeof(properties); | 877 properties.cBytes = sizeof(properties); |
848 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); | 878 ScriptGetFontProperties(cached_hdc_, &run->script_cache, &properties); |
849 | 879 |
850 const wchar_t* run_text = &(text()[run->range.start()]); | 880 const wchar_t* run_text = &(text()[run->range.start()]); |
851 for (size_t char_index = 0; char_index < run->range.length(); ++char_index) { | 881 for (size_t char_index = 0; char_index < run->range.length(); ++char_index) { |
852 const int glyph_index = run->logical_clusters[char_index]; | 882 const int glyph_index = run->logical_clusters[char_index]; |
853 DCHECK_GE(glyph_index, 0); | 883 DCHECK_GE(glyph_index, 0); |
854 DCHECK_LT(glyph_index, run->glyph_count); | 884 DCHECK_LT(glyph_index, run->glyph_count); |
855 | 885 |
856 if (run->glyphs[glyph_index] == properties.wgDefault) | 886 if (run->glyphs[glyph_index] == properties.wgDefault) |
857 return true; | 887 continue; |
858 | 888 |
859 // Windows Vista sometimes returns glyphs equal to wgBlank (instead of | 889 // Windows Vista sometimes returns glyphs equal to wgBlank (instead of |
860 // wgDefault), with fZeroWidth set. Treat such cases as having missing | 890 // wgDefault), with fZeroWidth set. Treat such cases as having missing |
861 // glyphs if the corresponding character is not whitespace. | 891 // glyphs if the corresponding character is not whitespace. |
862 // See: http://crbug.com/125629 | 892 // See: http://crbug.com/125629 |
863 if (run->glyphs[glyph_index] == properties.wgBlank && | 893 if (run->glyphs[glyph_index] == properties.wgBlank && |
864 run->visible_attributes[glyph_index].fZeroWidth && | 894 run->visible_attributes[glyph_index].fZeroWidth && |
865 !IsWhitespace(run_text[char_index]) && | 895 !IsWhitespace(run_text[char_index]) && |
866 !IsUnicodeBidiControlCharacter(run_text[char_index])) { | 896 !IsUnicodeBidiControlCharacter(run_text[char_index])) { |
867 return true; | 897 continue; |
868 } | 898 } |
| 899 |
| 900 ++chars_not_missing_glyphs; |
869 } | 901 } |
870 | 902 |
871 return false; | 903 DCHECK_LE(chars_not_missing_glyphs, static_cast<int>(run->range.length())); |
| 904 return run->range.length() - chars_not_missing_glyphs; |
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 |