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" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/win/registry.h" | 15 #include "base/win/registry.h" |
16 #include "base/win/windows_version.h" | |
16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/font_smoothing_win.h" | 18 #include "ui/gfx/font_smoothing_win.h" |
18 #include "ui/gfx/platform_font.h" | 19 #include "ui/gfx/platform_font_win.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // The maximum supported number of Uniscribe runs; a SCRIPT_ITEM is 8 bytes. | 23 // The maximum supported number of Uniscribe runs; a SCRIPT_ITEM is 8 bytes. |
23 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? | 24 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? |
24 const int kGuessItems = 100; | 25 const int kGuessItems = 100; |
25 const int kMaxItems = 10000; | 26 const int kMaxItems = 10000; |
26 | 27 |
27 // The maximum supported number of Uniscribe glyphs; a glyph is 1 word. | 28 // The maximum supported number of Uniscribe glyphs; a glyph is 1 word. |
28 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? | 29 // TODO(msw): Review memory use/failure? Max string length? Alternate approach? |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 if ((index != string16::npos) && (index + 1 != values[i].length())) { | 110 if ((index != string16::npos) && (index + 1 != values[i].length())) { |
110 const std::string linked_name = UTF16ToUTF8(values[i].substr(index + 1)); | 111 const std::string linked_name = UTF16ToUTF8(values[i].substr(index + 1)); |
111 const gfx::Font linked_font(linked_name, font.GetFontSize()); | 112 const gfx::Font linked_font(linked_name, font.GetFontSize()); |
112 linked_fonts->push_back(linked_font); | 113 linked_fonts->push_back(linked_font); |
113 } | 114 } |
114 } | 115 } |
115 | 116 |
116 key.Close(); | 117 key.Close(); |
117 } | 118 } |
118 | 119 |
119 // Changes |font| to have the specified |font_size| and |font_style| if it does | 120 // Changes |font| to have the specified |font_size| (or |font_height| on Windows |
120 // not already. Only considers bold and italic styles, since the underlined | 121 // XP) and |font_style| if it is not the case already. Only considers bold and |
121 // style has no effect on glyph shaping. | 122 // italic styles, since the underlined style has no effect on glyph shaping. |
122 void DeriveFontIfNecessary(int font_size, int font_style, gfx::Font* font) { | 123 void DeriveFontIfNecessary(int font_size, |
msw
2012/04/26 21:04:42
It would be nice to have this function take a cons
Alexei Svitkine (slow)
2012/04/26 21:45:56
That won't work for the call site that applies the
msw
2012/04/26 22:14:13
Fair enough, I agree :), the function comment suff
| |
124 int font_height, | |
125 int font_style, | |
126 gfx::Font* font) { | |
123 const int kStyleMask = (gfx::Font::BOLD | gfx::Font::ITALIC); | 127 const int kStyleMask = (gfx::Font::BOLD | gfx::Font::ITALIC); |
128 const int target_style = (font_style & kStyleMask); | |
129 | |
130 // On Windows XP, the font must be resized using |font_height| instead of | |
131 // |font_size| to match GDI behavior. | |
132 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | |
133 gfx::PlatformFontWin* platform_font = | |
134 static_cast<gfx::PlatformFontWin*>(font->platform_font()); | |
135 *font = platform_font->DeriveFontWithHeight(font_height, target_style); | |
136 return; | |
137 } | |
138 | |
124 const int current_style = (font->GetStyle() & kStyleMask); | 139 const int current_style = (font->GetStyle() & kStyleMask); |
125 const int target_style = (font_style & kStyleMask); | |
126 const int current_size = font->GetFontSize(); | 140 const int current_size = font->GetFontSize(); |
127 if (current_style != target_style || current_size != font_size) | 141 if (current_style != target_style || current_size != font->GetFontSize()) |
128 *font = font->DeriveFont(font_size - current_size, font_style); | 142 *font = font->DeriveFont(font_size - font->GetFontSize(), font_style); |
129 } | 143 } |
130 | 144 |
131 } // namespace | 145 } // namespace |
132 | 146 |
133 namespace gfx { | 147 namespace gfx { |
134 | 148 |
135 namespace internal { | 149 namespace internal { |
136 | 150 |
137 TextRun::TextRun() | 151 TextRun::TextRun() |
138 : strike(false), | 152 : strike(false), |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 // TODO(msw): Only break for font changes, not color etc. See TextRun comment. | 569 // TODO(msw): Only break for font changes, not color etc. See TextRun comment. |
556 StyleRanges styles(style_ranges()); | 570 StyleRanges styles(style_ranges()); |
557 ApplyCompositionAndSelectionStyles(&styles); | 571 ApplyCompositionAndSelectionStyles(&styles); |
558 StyleRanges::const_iterator style = styles.begin(); | 572 StyleRanges::const_iterator style = styles.begin(); |
559 SCRIPT_ITEM* script_item = &script_items[0]; | 573 SCRIPT_ITEM* script_item = &script_items[0]; |
560 for (int run_break = 0; run_break < text_length;) { | 574 for (int run_break = 0; run_break < text_length;) { |
561 internal::TextRun* run = new internal::TextRun(); | 575 internal::TextRun* run = new internal::TextRun(); |
562 run->range.set_start(run_break); | 576 run->range.set_start(run_break); |
563 run->font = GetFont(); | 577 run->font = GetFont(); |
564 run->font_style = style->font_style; | 578 run->font_style = style->font_style; |
565 DeriveFontIfNecessary(run->font.GetFontSize(), run->font_style, &run->font); | 579 DeriveFontIfNecessary(run->font.GetFontSize(), run->font.GetHeight(), |
580 run->font_style, &run->font); | |
566 run->foreground = style->foreground; | 581 run->foreground = style->foreground; |
567 run->strike = style->strike; | 582 run->strike = style->strike; |
568 run->diagonal_strike = style->diagonal_strike; | 583 run->diagonal_strike = style->diagonal_strike; |
569 run->underline = style->underline; | 584 run->underline = style->underline; |
570 run->script_analysis = script_item->a; | 585 run->script_analysis = script_item->a; |
571 | 586 |
572 // Find the range end and advance the structures as needed. | 587 // Find the range end and advance the structures as needed. |
573 int script_item_end = (script_item + 1)->iCharPos; | 588 int script_item_end = (script_item + 1)->iCharPos; |
574 int style_range_end = style->range.end(); | 589 int style_range_end = style->range.end(); |
575 run_break = std::min(script_item_end, style_range_end); | 590 run_break = std::min(script_item_end, style_range_end); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
739 const ABC& abc = run->abc_widths; | 754 const ABC& abc = run->abc_widths; |
740 run->width = abc.abcA + abc.abcB + abc.abcC; | 755 run->width = abc.abcA + abc.abcB + abc.abcC; |
741 preceding_run_widths += run->width; | 756 preceding_run_widths += run->width; |
742 } | 757 } |
743 string_size_.set_width(preceding_run_widths); | 758 string_size_.set_width(preceding_run_widths); |
744 } | 759 } |
745 | 760 |
746 void RenderTextWin::ApplySubstituteFont(internal::TextRun* run, | 761 void RenderTextWin::ApplySubstituteFont(internal::TextRun* run, |
747 const Font& font) { | 762 const Font& font) { |
748 const int font_size = run->font.GetFontSize(); | 763 const int font_size = run->font.GetFontSize(); |
764 const int font_height = run->font.GetHeight(); | |
749 run->font = font; | 765 run->font = font; |
750 DeriveFontIfNecessary(font_size, run->font_style, &run->font); | 766 DeriveFontIfNecessary(font_size, font_height, run->font_style, &run->font); |
751 ScriptFreeCache(&run->script_cache); | 767 ScriptFreeCache(&run->script_cache); |
752 SelectObject(cached_hdc_, run->font.GetNativeFont()); | 768 SelectObject(cached_hdc_, run->font.GetNativeFont()); |
753 } | 769 } |
754 | 770 |
755 const std::vector<Font>* RenderTextWin::GetLinkedFonts(const Font& font) const { | 771 const std::vector<Font>* RenderTextWin::GetLinkedFonts(const Font& font) const { |
756 const std::string& font_name = font.GetFontName(); | 772 const std::string& font_name = font.GetFontName(); |
757 std::map<std::string, std::vector<Font> >::const_iterator it = | 773 std::map<std::string, std::vector<Font> >::const_iterator it = |
758 cached_linked_fonts_.find(font_name); | 774 cached_linked_fonts_.find(font_name); |
759 if (it != cached_linked_fonts_.end()) | 775 if (it != cached_linked_fonts_.end()) |
760 return &it->second; | 776 return &it->second; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
797 internal::TextRun* run) { | 813 internal::TextRun* run) { |
798 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); | 814 size_t caret = IndexOfAdjacentGrapheme(run->range.end(), CURSOR_BACKWARD); |
799 return SelectionModel(caret, CURSOR_FORWARD); | 815 return SelectionModel(caret, CURSOR_FORWARD); |
800 } | 816 } |
801 | 817 |
802 RenderText* RenderText::CreateRenderText() { | 818 RenderText* RenderText::CreateRenderText() { |
803 return new RenderTextWin; | 819 return new RenderTextWin; |
804 } | 820 } |
805 | 821 |
806 } // namespace gfx | 822 } // namespace gfx |
OLD | NEW |