OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/glue/plugins/pepper_font.h" | 5 #include "webkit/glue/plugins/pepper_font.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "third_party/ppapi/c/dev/ppb_font_dev.h" |
9 #include "third_party/ppapi/c/pp_rect.h" | 10 #include "third_party/ppapi/c/pp_rect.h" |
10 #include "third_party/ppapi/c/ppb_font.h" | |
11 #include "third_party/WebKit/WebKit/chromium/public/WebFont.h" | 11 #include "third_party/WebKit/WebKit/chromium/public/WebFont.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebFontDescription.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebFontDescription.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFloatPoint.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebFloatPoint.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebFloatRect.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebFloatRect.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebTextRun.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebTextRun.h" |
17 #include "webkit/glue/plugins/pepper_image_data.h" | 17 #include "webkit/glue/plugins/pepper_image_data.h" |
18 #include "webkit/glue/plugins/pepper_plugin_module.h" | 18 #include "webkit/glue/plugins/pepper_plugin_module.h" |
19 #include "webkit/glue/plugins/pepper_string.h" | 19 #include "webkit/glue/plugins/pepper_string.h" |
20 #include "webkit/glue/plugins/pepper_var.h" | 20 #include "webkit/glue/plugins/pepper_var.h" |
21 #include "webkit/glue/webkit_glue.h" | 21 #include "webkit/glue/webkit_glue.h" |
22 | 22 |
23 using WebKit::WebFloatPoint; | 23 using WebKit::WebFloatPoint; |
24 using WebKit::WebFloatRect; | 24 using WebKit::WebFloatRect; |
25 using WebKit::WebFont; | 25 using WebKit::WebFont; |
26 using WebKit::WebFontDescription; | 26 using WebKit::WebFontDescription; |
27 using WebKit::WebRect; | 27 using WebKit::WebRect; |
28 using WebKit::WebTextRun; | 28 using WebKit::WebTextRun; |
29 | 29 |
30 namespace pepper { | 30 namespace pepper { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 bool IsPPFontDescriptionValid(const PP_FontDescription& desc) { | 34 bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { |
35 // Check validity of UTF-8. | 35 // Check validity of UTF-8. |
36 if (desc.face.type != PP_VARTYPE_STRING && desc.face.type != PP_VARTYPE_VOID) | 36 if (desc.face.type != PP_VARTYPE_STRING && desc.face.type != PP_VARTYPE_VOID) |
37 return false; | 37 return false; |
38 | 38 |
39 // Check enum ranges. | 39 // Check enum ranges. |
40 if (static_cast<int>(desc.family) < PP_FONTFAMILY_DEFAULT || | 40 if (static_cast<int>(desc.family) < PP_FONTFAMILY_DEFAULT || |
41 static_cast<int>(desc.family) > PP_FONTFAMILY_MONOSPACE) | 41 static_cast<int>(desc.family) > PP_FONTFAMILY_MONOSPACE) |
42 return false; | 42 return false; |
43 if (static_cast<int>(desc.weight) < PP_FONTWEIGHT_100 || | 43 if (static_cast<int>(desc.weight) < PP_FONTWEIGHT_100 || |
44 static_cast<int>(desc.weight) > PP_FONTWEIGHT_900) | 44 static_cast<int>(desc.weight) > PP_FONTWEIGHT_900) |
45 return false; | 45 return false; |
46 | 46 |
47 // Check for excessive sizes which may cause layout to get confused. | 47 // Check for excessive sizes which may cause layout to get confused. |
48 if (desc.size > 200) | 48 if (desc.size > 200) |
49 return false; | 49 return false; |
50 | 50 |
51 return true; | 51 return true; |
52 } | 52 } |
53 | 53 |
54 // The PP_* version lacks "None", so is just one value shifted from the | 54 // The PP_* version lacks "None", so is just one value shifted from the |
55 // WebFontDescription version. These values are checked in | 55 // WebFontDescription version. These values are checked in |
56 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a | 56 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a |
57 // macro so it can also be used in the COMPILE_ASSERTS. | 57 // macro so it can also be used in the COMPILE_ASSERTS. |
58 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ | 58 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ |
59 static_cast<WebFontDescription::GenericFamily>(f + 1) | 59 static_cast<WebFontDescription::GenericFamily>(f + 1) |
60 | 60 |
61 // Assumes the given PP_FontDescription has been validated. | 61 // Assumes the given PP_FontDescription has been validated. |
62 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription& font) { | 62 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font) { |
63 // Verify that the enums match so we can just static cast. | 63 // Verify that the enums match so we can just static cast. |
64 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == | 64 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == |
65 static_cast<int>(PP_FONTWEIGHT_100), | 65 static_cast<int>(PP_FONTWEIGHT_100), |
66 FontWeight100); | 66 FontWeight100); |
67 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == | 67 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == |
68 static_cast<int>(PP_FONTWEIGHT_900), | 68 static_cast<int>(PP_FONTWEIGHT_900), |
69 FontWeight900); | 69 FontWeight900); |
70 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == | 70 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == |
71 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), | 71 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), |
72 StandardFamily); | 72 StandardFamily); |
(...skipping 16 matching lines...) Expand all Loading... |
89 result.italic = font.italic; | 89 result.italic = font.italic; |
90 result.smallCaps = font.small_caps; | 90 result.smallCaps = font.small_caps; |
91 result.weight = static_cast<WebFontDescription::Weight>(font.weight); | 91 result.weight = static_cast<WebFontDescription::Weight>(font.weight); |
92 result.letterSpacing = static_cast<short>(font.letter_spacing); | 92 result.letterSpacing = static_cast<short>(font.letter_spacing); |
93 result.wordSpacing = static_cast<short>(font.word_spacing); | 93 result.wordSpacing = static_cast<short>(font.word_spacing); |
94 return result; | 94 return result; |
95 } | 95 } |
96 | 96 |
97 // Converts the given PP_TextRun to a WebTextRun, returning true on success. | 97 // Converts the given PP_TextRun to a WebTextRun, returning true on success. |
98 // False means the input was invalid. | 98 // False means the input was invalid. |
99 bool PPTextRunToWebTextRun(const PP_TextRun* run, WebTextRun* output) { | 99 bool PPTextRunToWebTextRun(const PP_TextRun_Dev* run, WebTextRun* output) { |
100 String* text_string = GetString(run->text); | 100 String* text_string = GetString(run->text); |
101 if (!text_string) | 101 if (!text_string) |
102 return false; | 102 return false; |
103 *output = WebTextRun(UTF8ToUTF16(text_string->value()), | 103 *output = WebTextRun(UTF8ToUTF16(text_string->value()), |
104 run->rtl, run->override_direction); | 104 run->rtl, run->override_direction); |
105 return true; | 105 return true; |
106 } | 106 } |
107 | 107 |
108 PP_Resource Create(PP_Module module_id, | 108 PP_Resource Create(PP_Module module_id, |
109 const PP_FontDescription* description) { | 109 const PP_FontDescription_Dev* description) { |
110 PluginModule* module = PluginModule::FromPPModule(module_id); | 110 PluginModule* module = PluginModule::FromPPModule(module_id); |
111 if (!module) | 111 if (!module) |
112 return 0; | 112 return 0; |
113 | 113 |
114 if (!IsPPFontDescriptionValid(*description)) | 114 if (!IsPPFontDescriptionValid(*description)) |
115 return 0; | 115 return 0; |
116 | 116 |
117 scoped_refptr<Font> font(new Font(module, *description)); | 117 scoped_refptr<Font> font(new Font(module, *description)); |
118 return font->GetReference(); | 118 return font->GetReference(); |
119 } | 119 } |
120 | 120 |
121 bool IsFont(PP_Resource resource) { | 121 bool IsFont(PP_Resource resource) { |
122 return !!Resource::GetAs<Font>(resource).get(); | 122 return !!Resource::GetAs<Font>(resource).get(); |
123 } | 123 } |
124 | 124 |
125 bool Describe(PP_Resource font_id, | 125 bool Describe(PP_Resource font_id, |
126 PP_FontDescription* description, | 126 PP_FontDescription_Dev* description, |
127 PP_FontMetrics* metrics) { | 127 PP_FontMetrics_Dev* metrics) { |
128 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 128 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); |
129 if (!font.get()) | 129 if (!font.get()) |
130 return false; | 130 return false; |
131 return font->Describe(description, metrics); | 131 return font->Describe(description, metrics); |
132 } | 132 } |
133 | 133 |
134 bool DrawTextAt(PP_Resource font_id, | 134 bool DrawTextAt(PP_Resource font_id, |
135 PP_Resource image_data, | 135 PP_Resource image_data, |
136 const PP_TextRun* text, | 136 const PP_TextRun_Dev* text, |
137 const PP_Point* position, | 137 const PP_Point* position, |
138 uint32_t color, | 138 uint32_t color, |
139 const PP_Rect* clip, | 139 const PP_Rect* clip, |
140 bool image_data_is_opaque) { | 140 bool image_data_is_opaque) { |
141 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 141 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); |
142 if (!font.get()) | 142 if (!font.get()) |
143 return false; | 143 return false; |
144 return font->DrawTextAt(image_data, text, position, color, clip, | 144 return font->DrawTextAt(image_data, text, position, color, clip, |
145 image_data_is_opaque); | 145 image_data_is_opaque); |
146 } | 146 } |
147 | 147 |
148 int32_t MeasureText(PP_Resource font_id, const PP_TextRun* text) { | 148 int32_t MeasureText(PP_Resource font_id, const PP_TextRun_Dev* text) { |
149 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 149 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); |
150 if (!font.get()) | 150 if (!font.get()) |
151 return -1; | 151 return -1; |
152 return font->MeasureText(text); | 152 return font->MeasureText(text); |
153 } | 153 } |
154 | 154 |
155 uint32_t CharacterOffsetForPixel(PP_Resource font_id, | 155 uint32_t CharacterOffsetForPixel(PP_Resource font_id, |
156 const PP_TextRun* text, | 156 const PP_TextRun_Dev* text, |
157 int32_t pixel_position) { | 157 int32_t pixel_position) { |
158 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 158 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); |
159 if (!font.get()) | 159 if (!font.get()) |
160 return false; | 160 return false; |
161 return font->CharacterOffsetForPixel(text, pixel_position); | 161 return font->CharacterOffsetForPixel(text, pixel_position); |
162 } | 162 } |
163 | 163 |
164 int32_t PixelOffsetForCharacter(PP_Resource font_id, | 164 int32_t PixelOffsetForCharacter(PP_Resource font_id, |
165 const PP_TextRun* text, | 165 const PP_TextRun_Dev* text, |
166 uint32_t char_offset) { | 166 uint32_t char_offset) { |
167 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 167 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); |
168 if (!font.get()) | 168 if (!font.get()) |
169 return false; | 169 return false; |
170 return font->PixelOffsetForCharacter(text, char_offset); | 170 return font->PixelOffsetForCharacter(text, char_offset); |
171 } | 171 } |
172 | 172 |
173 const PPB_Font ppb_font = { | 173 const PPB_Font_Dev ppb_font = { |
174 &Create, | 174 &Create, |
175 &IsFont, | 175 &IsFont, |
176 &Describe, | 176 &Describe, |
177 &DrawTextAt, | 177 &DrawTextAt, |
178 &MeasureText, | 178 &MeasureText, |
179 &CharacterOffsetForPixel, | 179 &CharacterOffsetForPixel, |
180 &PixelOffsetForCharacter | 180 &PixelOffsetForCharacter |
181 }; | 181 }; |
182 | 182 |
183 } // namespace | 183 } // namespace |
184 | 184 |
185 Font::Font(PluginModule* module, const PP_FontDescription& desc) | 185 Font::Font(PluginModule* module, const PP_FontDescription_Dev& desc) |
186 : Resource(module) { | 186 : Resource(module) { |
187 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); | 187 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); |
188 font_.reset(WebFont::create(web_font_desc)); | 188 font_.reset(WebFont::create(web_font_desc)); |
189 } | 189 } |
190 | 190 |
191 Font::~Font() { | 191 Font::~Font() { |
192 } | 192 } |
193 | 193 |
194 // static | 194 // static |
195 const PPB_Font* Font::GetInterface() { | 195 const PPB_Font_Dev* Font::GetInterface() { |
196 return &ppb_font; | 196 return &ppb_font; |
197 } | 197 } |
198 | 198 |
199 bool Font::Describe(PP_FontDescription* description, | 199 bool Font::Describe(PP_FontDescription_Dev* description, |
200 PP_FontMetrics* metrics) { | 200 PP_FontMetrics_Dev* metrics) { |
201 if (description->face.type != PP_VARTYPE_VOID) | 201 if (description->face.type != PP_VARTYPE_VOID) |
202 return false; | 202 return false; |
203 | 203 |
204 WebFontDescription web_desc = font_->fontDescription(); | 204 WebFontDescription web_desc = font_->fontDescription(); |
205 | 205 |
206 // While converting the other way in PPFontDescToWebFontDesc we validated | 206 // While converting the other way in PPFontDescToWebFontDesc we validated |
207 // that the enums can be casted. | 207 // that the enums can be casted. |
208 description->face = StringToPPVar(UTF16ToUTF8(web_desc.family)); | 208 description->face = StringToPPVar(UTF16ToUTF8(web_desc.family)); |
209 description->family = static_cast<PP_FontFamily>(web_desc.genericFamily); | 209 description->family = static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); |
210 description->size = static_cast<uint32_t>(web_desc.size); | 210 description->size = static_cast<uint32_t>(web_desc.size); |
211 description->weight = static_cast<PP_FontWeight>(web_desc.weight); | 211 description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); |
212 description->italic = web_desc.italic; | 212 description->italic = web_desc.italic; |
213 description->small_caps = web_desc.smallCaps; | 213 description->small_caps = web_desc.smallCaps; |
214 | 214 |
215 metrics->height = font_->height(); | 215 metrics->height = font_->height(); |
216 metrics->ascent = font_->ascent(); | 216 metrics->ascent = font_->ascent(); |
217 metrics->descent = font_->descent(); | 217 metrics->descent = font_->descent(); |
218 metrics->line_spacing = font_->lineSpacing(); | 218 metrics->line_spacing = font_->lineSpacing(); |
219 metrics->x_height = static_cast<int32_t>(font_->xHeight()); | 219 metrics->x_height = static_cast<int32_t>(font_->xHeight()); |
220 | 220 |
221 return true; | 221 return true; |
222 } | 222 } |
223 | 223 |
224 bool Font::DrawTextAt(PP_Resource image_data, | 224 bool Font::DrawTextAt(PP_Resource image_data, |
225 const PP_TextRun* text, | 225 const PP_TextRun_Dev* text, |
226 const PP_Point* position, | 226 const PP_Point* position, |
227 uint32_t color, | 227 uint32_t color, |
228 const PP_Rect* clip, | 228 const PP_Rect* clip, |
229 bool image_data_is_opaque) { | 229 bool image_data_is_opaque) { |
230 WebTextRun run; | 230 WebTextRun run; |
231 if (!PPTextRunToWebTextRun(text, &run)) | 231 if (!PPTextRunToWebTextRun(text, &run)) |
232 return false; | 232 return false; |
233 | 233 |
234 // Get and map the image data we're painting to. | 234 // Get and map the image data we're painting to. |
235 scoped_refptr<ImageData> image_resource( | 235 scoped_refptr<ImageData> image_resource( |
(...skipping 14 matching lines...) Expand all Loading... |
250 } else { | 250 } else { |
251 web_clip = WebRect(clip->point.x, clip->point.y, | 251 web_clip = WebRect(clip->point.x, clip->point.y, |
252 clip->size.width, clip->size.height); | 252 clip->size.width, clip->size.height); |
253 } | 253 } |
254 | 254 |
255 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), | 255 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), |
256 run, web_position, color, web_clip, image_data_is_opaque); | 256 run, web_position, color, web_clip, image_data_is_opaque); |
257 return true; | 257 return true; |
258 } | 258 } |
259 | 259 |
260 int32_t Font::MeasureText(const PP_TextRun* text) { | 260 int32_t Font::MeasureText(const PP_TextRun_Dev* text) { |
261 WebTextRun run; | 261 WebTextRun run; |
262 if (!PPTextRunToWebTextRun(text, &run)) | 262 if (!PPTextRunToWebTextRun(text, &run)) |
263 return -1; | 263 return -1; |
264 return font_->calculateWidth(run); | 264 return font_->calculateWidth(run); |
265 } | 265 } |
266 | 266 |
267 uint32_t Font::CharacterOffsetForPixel(const PP_TextRun* text, | 267 uint32_t Font::CharacterOffsetForPixel(const PP_TextRun_Dev* text, |
268 int32_t pixel_position) { | 268 int32_t pixel_position) { |
269 WebTextRun run; | 269 WebTextRun run; |
270 if (!PPTextRunToWebTextRun(text, &run)) | 270 if (!PPTextRunToWebTextRun(text, &run)) |
271 return -1; | 271 return -1; |
272 | 272 |
273 return static_cast<uint32_t>(font_->offsetForPosition( | 273 return static_cast<uint32_t>(font_->offsetForPosition( |
274 run, static_cast<float>(pixel_position))); | 274 run, static_cast<float>(pixel_position))); |
275 } | 275 } |
276 | 276 |
277 int32_t Font::PixelOffsetForCharacter(const PP_TextRun* text, | 277 int32_t Font::PixelOffsetForCharacter(const PP_TextRun_Dev* text, |
278 uint32_t char_offset) { | 278 uint32_t char_offset) { |
279 WebTextRun run; | 279 WebTextRun run; |
280 if (!PPTextRunToWebTextRun(text, &run)) | 280 if (!PPTextRunToWebTextRun(text, &run)) |
281 return -1; | 281 return -1; |
282 if (char_offset >= run.text.length()) | 282 if (char_offset >= run.text.length()) |
283 return -1; | 283 return -1; |
284 | 284 |
285 WebFloatRect rect = font_->selectionRectForText( | 285 WebFloatRect rect = font_->selectionRectForText( |
286 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); | 286 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); |
287 return static_cast<int>(rect.width); | 287 return static_cast<int>(rect.width); |
288 } | 288 } |
289 | 289 |
290 } // namespace pepper | 290 } // namespace pepper |
OLD | NEW |