OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/plugins/ppapi/webkit_forwarding_impl.h" | 5 #include "webkit/plugins/ppapi/webkit_forwarding_impl.h" |
6 | 6 |
7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
8 #include "base/string_util.h" | |
8 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "ppapi/c/dev/ppb_font_dev.h" | 11 #include "ppapi/c/dev/ppb_font_dev.h" |
11 #include "ppapi/c/pp_point.h" | 12 #include "ppapi/c/pp_point.h" |
12 #include "ppapi/c/pp_rect.h" | 13 #include "ppapi/c/pp_rect.h" |
14 #include "ppapi/shared_impl/ppapi_preferences.h" | |
13 #include "skia/ext/platform_canvas.h" | 15 #include "skia/ext/platform_canvas.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" |
21 #include "webkit/glue/webkit_glue.h" | 23 #include "webkit/glue/webkit_glue.h" |
22 | 24 |
(...skipping 13 matching lines...) Expand all Loading... | |
36 | 38 |
37 // The PP_* version lacks "None", so is just one value shifted from the | 39 // The PP_* version lacks "None", so is just one value shifted from the |
38 // WebFontDescription version. These values are checked in | 40 // WebFontDescription version. These values are checked in |
39 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a | 41 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a |
40 // macro so it can also be used in the COMPILE_ASSERTS. | 42 // macro so it can also be used in the COMPILE_ASSERTS. |
41 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ | 43 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ |
42 static_cast<WebFontDescription::GenericFamily>(f + 1) | 44 static_cast<WebFontDescription::GenericFamily>(f + 1) |
43 | 45 |
44 // Assumes the given PP_FontDescription has been validated. | 46 // Assumes the given PP_FontDescription has been validated. |
45 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font, | 47 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font, |
46 const std::string& face) { | 48 const std::string& face, |
49 const ::ppapi::Preferences& prefs) { | |
47 // Verify that the enums match so we can just static cast. | 50 // Verify that the enums match so we can just static cast. |
48 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == | 51 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == |
49 static_cast<int>(PP_FONTWEIGHT_100), | 52 static_cast<int>(PP_FONTWEIGHT_100), |
50 FontWeight100); | 53 FontWeight100); |
51 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == | 54 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == |
52 static_cast<int>(PP_FONTWEIGHT_900), | 55 static_cast<int>(PP_FONTWEIGHT_900), |
53 FontWeight900); | 56 FontWeight900); |
54 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == | 57 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == |
55 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), | 58 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), |
56 StandardFamily); | 59 StandardFamily); |
57 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == | 60 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == |
58 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), | 61 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), |
59 SerifFamily); | 62 SerifFamily); |
60 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == | 63 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == |
61 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), | 64 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), |
62 SansSerifFamily); | 65 SansSerifFamily); |
63 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == | 66 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == |
64 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), | 67 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), |
65 MonospaceFamily); | 68 MonospaceFamily); |
66 | 69 |
67 WebFontDescription result; | 70 WebFontDescription result; |
68 result.family = UTF8ToUTF16(face); | 71 string16 resolved_family; |
72 if (face.empty()) { | |
73 // Resolve the generic family. | |
74 switch (font.family) { | |
75 case PP_FONTFAMILY_SERIF: | |
yzshen1
2011/05/25 23:40:11
Indent: need to have 2 more spaces.
| |
76 resolved_family = prefs.serif_font_family; | |
77 break; | |
78 case PP_FONTFAMILY_SANSSERIF: | |
79 resolved_family = prefs.sans_serif_font_family; | |
80 break; | |
81 case PP_FONTFAMILY_MONOSPACE: | |
82 resolved_family = prefs.fixed_font_family; | |
83 break; | |
84 case PP_FONTFAMILY_DEFAULT: | |
85 default: | |
86 resolved_family = prefs.standard_font_family; | |
87 break; | |
88 } | |
89 } else { | |
90 // Use the exact font. | |
91 resolved_family = UTF8ToUTF16(face); | |
92 } | |
93 result.family = resolved_family; | |
94 | |
69 result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); | 95 result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); |
70 result.size = static_cast<float>(font.size); | 96 |
97 if (font.size == 0) { | |
98 // Resolve the default font size, using the resolved family to see if | |
99 // we should use the fixed or regular font size. It's difficult at this | |
100 // level to detect if the requested font is fixed width, so we only apply | |
101 // the alternate font size to the default fixed font family. | |
102 if (StringToLowerASCII(resolved_family) == | |
103 StringToLowerASCII(prefs.fixed_font_family)) | |
104 result.size = static_cast<float>(prefs.default_fixed_font_size); | |
105 else | |
106 result.size = static_cast<float>(prefs.default_font_size); | |
107 } else { | |
108 // Use the exact size. | |
109 result.size = static_cast<float>(font.size); | |
110 } | |
111 | |
71 result.italic = font.italic != PP_FALSE; | 112 result.italic = font.italic != PP_FALSE; |
72 result.smallCaps = font.small_caps != PP_FALSE; | 113 result.smallCaps = font.small_caps != PP_FALSE; |
73 result.weight = static_cast<WebFontDescription::Weight>(font.weight); | 114 result.weight = static_cast<WebFontDescription::Weight>(font.weight); |
74 result.letterSpacing = static_cast<short>(font.letter_spacing); | 115 result.letterSpacing = static_cast<short>(font.letter_spacing); |
75 result.wordSpacing = static_cast<short>(font.word_spacing); | 116 result.wordSpacing = static_cast<short>(font.word_spacing); |
76 return result; | 117 return result; |
77 } | 118 } |
78 | 119 |
79 WebTextRun TextRunToWebTextRun(const WebKitForwarding::Font::TextRun& run) { | 120 WebTextRun TextRunToWebTextRun(const WebKitForwarding::Font::TextRun& run) { |
80 return WebTextRun(UTF8ToUTF16(run.text), | 121 return WebTextRun(UTF8ToUTF16(run.text), |
81 run.rtl != PP_FALSE, | 122 run.rtl != PP_FALSE, |
82 run.override_direction != PP_FALSE); | 123 run.override_direction != PP_FALSE); |
83 } | 124 } |
84 | 125 |
85 // FontImpl -------------------------------------------------------------------- | 126 // FontImpl -------------------------------------------------------------------- |
86 | 127 |
87 class FontImpl : public WebKitForwarding::Font { | 128 class FontImpl : public WebKitForwarding::Font { |
88 public: | 129 public: |
89 FontImpl(const PP_FontDescription_Dev& desc, | 130 FontImpl(const PP_FontDescription_Dev& desc, |
90 const std::string& desc_face); | 131 const std::string& desc_face, |
132 const ::ppapi::Preferences& prefs); | |
91 virtual ~FontImpl(); | 133 virtual ~FontImpl(); |
92 | 134 |
93 virtual void Describe(base::WaitableEvent* event, | 135 virtual void Describe(base::WaitableEvent* event, |
94 PP_FontDescription_Dev* description, | 136 PP_FontDescription_Dev* description, |
95 std::string* face, | 137 std::string* face, |
96 PP_FontMetrics_Dev* metrics, | 138 PP_FontMetrics_Dev* metrics, |
97 PP_Bool* result) OVERRIDE; | 139 PP_Bool* result) OVERRIDE; |
98 virtual void DrawTextAt(base::WaitableEvent* event, | 140 virtual void DrawTextAt(base::WaitableEvent* event, |
99 const DrawTextParams& params) OVERRIDE; | 141 const DrawTextParams& params) OVERRIDE; |
100 virtual void MeasureText(base::WaitableEvent* event, | 142 virtual void MeasureText(base::WaitableEvent* event, |
101 const TextRun& text, | 143 const TextRun& text, |
102 int32_t* result) OVERRIDE; | 144 int32_t* result) OVERRIDE; |
103 virtual void CharacterOffsetForPixel(base::WaitableEvent* event, | 145 virtual void CharacterOffsetForPixel(base::WaitableEvent* event, |
104 const TextRun& text, | 146 const TextRun& text, |
105 int32_t pixel_position, | 147 int32_t pixel_position, |
106 uint32_t* result) OVERRIDE; | 148 uint32_t* result) OVERRIDE; |
107 virtual void PixelOffsetForCharacter(base::WaitableEvent* event, | 149 virtual void PixelOffsetForCharacter(base::WaitableEvent* event, |
108 const TextRun& text, | 150 const TextRun& text, |
109 uint32_t char_offset, | 151 uint32_t char_offset, |
110 int32_t* result) OVERRIDE; | 152 int32_t* result) OVERRIDE; |
111 | 153 |
112 private: | 154 private: |
113 scoped_ptr<WebFont> font_; | 155 scoped_ptr<WebFont> font_; |
114 | 156 |
115 DISALLOW_COPY_AND_ASSIGN(FontImpl); | 157 DISALLOW_COPY_AND_ASSIGN(FontImpl); |
116 }; | 158 }; |
117 | 159 |
118 FontImpl::FontImpl(const PP_FontDescription_Dev& desc, | 160 FontImpl::FontImpl(const PP_FontDescription_Dev& desc, |
119 const std::string& desc_face) { | 161 const std::string& desc_face, |
120 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc, desc_face); | 162 const ::ppapi::Preferences& prefs) { |
163 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc, desc_face, | |
164 prefs); | |
121 font_.reset(WebFont::create(web_font_desc)); | 165 font_.reset(WebFont::create(web_font_desc)); |
122 } | 166 } |
123 | 167 |
124 FontImpl::~FontImpl() { | 168 FontImpl::~FontImpl() { |
125 } | 169 } |
126 | 170 |
127 void FontImpl::Describe(base::WaitableEvent* event, | 171 void FontImpl::Describe(base::WaitableEvent* event, |
128 PP_FontDescription_Dev* description, | 172 PP_FontDescription_Dev* description, |
129 std::string* face, | 173 std::string* face, |
130 PP_FontMetrics_Dev* metrics, | 174 PP_FontMetrics_Dev* metrics, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 WebKitForwardingImpl::WebKitForwardingImpl() { | 269 WebKitForwardingImpl::WebKitForwardingImpl() { |
226 } | 270 } |
227 | 271 |
228 WebKitForwardingImpl::~WebKitForwardingImpl() { | 272 WebKitForwardingImpl::~WebKitForwardingImpl() { |
229 } | 273 } |
230 | 274 |
231 void WebKitForwardingImpl::CreateFontForwarding( | 275 void WebKitForwardingImpl::CreateFontForwarding( |
232 base::WaitableEvent* event, | 276 base::WaitableEvent* event, |
233 const PP_FontDescription_Dev& desc, | 277 const PP_FontDescription_Dev& desc, |
234 const std::string& desc_face, | 278 const std::string& desc_face, |
279 const ::ppapi::Preferences& prefs, | |
235 Font** result) { | 280 Font** result) { |
236 *result = new FontImpl(desc, desc_face); | 281 *result = new FontImpl(desc, desc_face, prefs); |
237 if (event) | 282 if (event) |
238 event->Signal(); | 283 event->Signal(); |
239 } | 284 } |
240 | 285 |
241 } // namespace ppapi | 286 } // namespace ppapi |
242 } // namespace webkit | 287 } // namespace webkit |
OLD | NEW |