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/plugins/ppapi/ppb_font_impl.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 "ppapi/c/dev/ppb_font_dev.h" | 9 #include "ppapi/c/dev/ppb_font_dev.h" |
10 #include "ppapi/c/pp_rect.h" | 10 #include "ppapi/c/pp_rect.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_common.h" | 17 #include "webkit/plugins/ppapi/common.h" |
18 #include "webkit/glue/plugins/pepper_image_data.h" | 18 #include "webkit/plugins/ppapi/plugin_module.h" |
19 #include "webkit/glue/plugins/pepper_plugin_module.h" | 19 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
20 #include "webkit/glue/plugins/pepper_string.h" | 20 #include "webkit/plugins/ppapi/string.h" |
21 #include "webkit/glue/plugins/pepper_var.h" | 21 #include "webkit/plugins/ppapi/var.h" |
22 #include "webkit/glue/webkit_glue.h" | 22 #include "webkit/glue/webkit_glue.h" |
23 | 23 |
24 using WebKit::WebFloatPoint; | 24 using WebKit::WebFloatPoint; |
25 using WebKit::WebFloatRect; | 25 using WebKit::WebFloatRect; |
26 using WebKit::WebFont; | 26 using WebKit::WebFont; |
27 using WebKit::WebFontDescription; | 27 using WebKit::WebFontDescription; |
28 using WebKit::WebRect; | 28 using WebKit::WebRect; |
29 using WebKit::WebTextRun; | 29 using WebKit::WebTextRun; |
30 | 30 |
31 namespace pepper { | 31 namespace webkit { |
| 32 namespace plugins { |
| 33 namespace ppapi { |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { | 37 bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { |
36 // Check validity of UTF-8. | 38 // Check validity of UTF-8. |
37 if (desc.face.type != PP_VARTYPE_STRING && | 39 if (desc.face.type != PP_VARTYPE_STRING && |
38 desc.face.type != PP_VARTYPE_UNDEFINED) | 40 desc.face.type != PP_VARTYPE_UNDEFINED) |
39 return false; | 41 return false; |
40 | 42 |
41 // Check enum ranges. | 43 // Check enum ranges. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 112 |
111 PP_Resource Create(PP_Module module_id, | 113 PP_Resource Create(PP_Module module_id, |
112 const PP_FontDescription_Dev* description) { | 114 const PP_FontDescription_Dev* description) { |
113 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); | 115 PluginModule* module = ResourceTracker::Get()->GetModule(module_id); |
114 if (!module) | 116 if (!module) |
115 return 0; | 117 return 0; |
116 | 118 |
117 if (!IsPPFontDescriptionValid(*description)) | 119 if (!IsPPFontDescriptionValid(*description)) |
118 return 0; | 120 return 0; |
119 | 121 |
120 scoped_refptr<Font> font(new Font(module, *description)); | 122 scoped_refptr<PPB_Font_Impl> font(new PPB_Font_Impl(module, *description)); |
121 return font->GetReference(); | 123 return font->GetReference(); |
122 } | 124 } |
123 | 125 |
124 PP_Bool IsFont(PP_Resource resource) { | 126 PP_Bool IsFont(PP_Resource resource) { |
125 return BoolToPPBool(!!Resource::GetAs<Font>(resource).get()); | 127 return BoolToPPBool(!!Resource::GetAs<PPB_Font_Impl>(resource).get()); |
126 } | 128 } |
127 | 129 |
128 PP_Bool Describe(PP_Resource font_id, | 130 PP_Bool Describe(PP_Resource font_id, |
129 PP_FontDescription_Dev* description, | 131 PP_FontDescription_Dev* description, |
130 PP_FontMetrics_Dev* metrics) { | 132 PP_FontMetrics_Dev* metrics) { |
131 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 133 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); |
132 if (!font.get()) | 134 if (!font.get()) |
133 return PP_FALSE; | 135 return PP_FALSE; |
134 return BoolToPPBool(font->Describe(description, metrics)); | 136 return BoolToPPBool(font->Describe(description, metrics)); |
135 } | 137 } |
136 | 138 |
137 PP_Bool DrawTextAt(PP_Resource font_id, | 139 PP_Bool DrawTextAt(PP_Resource font_id, |
138 PP_Resource image_data, | 140 PP_Resource image_data, |
139 const PP_TextRun_Dev* text, | 141 const PP_TextRun_Dev* text, |
140 const PP_Point* position, | 142 const PP_Point* position, |
141 uint32_t color, | 143 uint32_t color, |
142 const PP_Rect* clip, | 144 const PP_Rect* clip, |
143 PP_Bool image_data_is_opaque) { | 145 PP_Bool image_data_is_opaque) { |
144 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 146 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); |
145 if (!font.get()) | 147 if (!font.get()) |
146 return PP_FALSE; | 148 return PP_FALSE; |
147 return BoolToPPBool(font->DrawTextAt(image_data, text, position, color, clip, | 149 return BoolToPPBool(font->DrawTextAt(image_data, text, position, color, clip, |
148 PPBoolToBool(image_data_is_opaque))); | 150 PPBoolToBool(image_data_is_opaque))); |
149 } | 151 } |
150 | 152 |
151 int32_t MeasureText(PP_Resource font_id, const PP_TextRun_Dev* text) { | 153 int32_t MeasureText(PP_Resource font_id, const PP_TextRun_Dev* text) { |
152 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 154 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); |
153 if (!font.get()) | 155 if (!font.get()) |
154 return -1; | 156 return -1; |
155 return font->MeasureText(text); | 157 return font->MeasureText(text); |
156 } | 158 } |
157 | 159 |
158 uint32_t CharacterOffsetForPixel(PP_Resource font_id, | 160 uint32_t CharacterOffsetForPixel(PP_Resource font_id, |
159 const PP_TextRun_Dev* text, | 161 const PP_TextRun_Dev* text, |
160 int32_t pixel_position) { | 162 int32_t pixel_position) { |
161 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 163 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); |
162 if (!font.get()) | 164 if (!font.get()) |
163 return false; | 165 return false; |
164 return font->CharacterOffsetForPixel(text, pixel_position); | 166 return font->CharacterOffsetForPixel(text, pixel_position); |
165 } | 167 } |
166 | 168 |
167 int32_t PixelOffsetForCharacter(PP_Resource font_id, | 169 int32_t PixelOffsetForCharacter(PP_Resource font_id, |
168 const PP_TextRun_Dev* text, | 170 const PP_TextRun_Dev* text, |
169 uint32_t char_offset) { | 171 uint32_t char_offset) { |
170 scoped_refptr<Font> font(Resource::GetAs<Font>(font_id)); | 172 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); |
171 if (!font.get()) | 173 if (!font.get()) |
172 return false; | 174 return false; |
173 return font->PixelOffsetForCharacter(text, char_offset); | 175 return font->PixelOffsetForCharacter(text, char_offset); |
174 } | 176 } |
175 | 177 |
176 const PPB_Font_Dev ppb_font = { | 178 const PPB_Font_Dev ppb_font = { |
177 &Create, | 179 &Create, |
178 &IsFont, | 180 &IsFont, |
179 &Describe, | 181 &Describe, |
180 &DrawTextAt, | 182 &DrawTextAt, |
181 &MeasureText, | 183 &MeasureText, |
182 &CharacterOffsetForPixel, | 184 &CharacterOffsetForPixel, |
183 &PixelOffsetForCharacter | 185 &PixelOffsetForCharacter |
184 }; | 186 }; |
185 | 187 |
186 } // namespace | 188 } // namespace |
187 | 189 |
188 Font::Font(PluginModule* module, const PP_FontDescription_Dev& desc) | 190 PPB_Font_Impl::PPB_Font_Impl(PluginModule* module, |
| 191 const PP_FontDescription_Dev& desc) |
189 : Resource(module) { | 192 : Resource(module) { |
190 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); | 193 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); |
191 font_.reset(WebFont::create(web_font_desc)); | 194 font_.reset(WebFont::create(web_font_desc)); |
192 } | 195 } |
193 | 196 |
194 Font::~Font() { | 197 PPB_Font_Impl::~PPB_Font_Impl() { |
195 } | 198 } |
196 | 199 |
197 // static | 200 // static |
198 const PPB_Font_Dev* Font::GetInterface() { | 201 const PPB_Font_Dev* PPB_Font_Impl::GetInterface() { |
199 return &ppb_font; | 202 return &ppb_font; |
200 } | 203 } |
201 | 204 |
202 Font* Font::AsFont() { | 205 PPB_Font_Impl* PPB_Font_Impl::AsFont() { |
203 return this; | 206 return this; |
204 } | 207 } |
205 | 208 |
206 bool Font::Describe(PP_FontDescription_Dev* description, | 209 bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, |
207 PP_FontMetrics_Dev* metrics) { | 210 PP_FontMetrics_Dev* metrics) { |
208 if (description->face.type != PP_VARTYPE_UNDEFINED) | 211 if (description->face.type != PP_VARTYPE_UNDEFINED) |
209 return false; | 212 return false; |
210 | 213 |
211 WebFontDescription web_desc = font_->fontDescription(); | 214 WebFontDescription web_desc = font_->fontDescription(); |
212 | 215 |
213 // While converting the other way in PPFontDescToWebFontDesc we validated | 216 // While converting the other way in PPFontDescToWebFontDesc we validated |
214 // that the enums can be casted. | 217 // that the enums can be casted. |
215 description->face = StringVar::StringToPPVar(module(), | 218 description->face = StringVar::StringToPPVar(module(), |
216 UTF16ToUTF8(web_desc.family)); | 219 UTF16ToUTF8(web_desc.family)); |
217 description->family = static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); | 220 description->family = static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); |
218 description->size = static_cast<uint32_t>(web_desc.size); | 221 description->size = static_cast<uint32_t>(web_desc.size); |
219 description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); | 222 description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); |
220 description->italic = BoolToPPBool(web_desc.italic); | 223 description->italic = BoolToPPBool(web_desc.italic); |
221 description->small_caps = BoolToPPBool(web_desc.smallCaps); | 224 description->small_caps = BoolToPPBool(web_desc.smallCaps); |
222 | 225 |
223 metrics->height = font_->height(); | 226 metrics->height = font_->height(); |
224 metrics->ascent = font_->ascent(); | 227 metrics->ascent = font_->ascent(); |
225 metrics->descent = font_->descent(); | 228 metrics->descent = font_->descent(); |
226 metrics->line_spacing = font_->lineSpacing(); | 229 metrics->line_spacing = font_->lineSpacing(); |
227 metrics->x_height = static_cast<int32_t>(font_->xHeight()); | 230 metrics->x_height = static_cast<int32_t>(font_->xHeight()); |
228 | 231 |
229 return true; | 232 return true; |
230 } | 233 } |
231 | 234 |
232 bool Font::DrawTextAt(PP_Resource image_data, | 235 bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, |
233 const PP_TextRun_Dev* text, | 236 const PP_TextRun_Dev* text, |
234 const PP_Point* position, | 237 const PP_Point* position, |
235 uint32_t color, | 238 uint32_t color, |
236 const PP_Rect* clip, | 239 const PP_Rect* clip, |
237 bool image_data_is_opaque) { | 240 bool image_data_is_opaque) { |
238 WebTextRun run; | 241 WebTextRun run; |
239 if (!PPTextRunToWebTextRun(text, &run)) | 242 if (!PPTextRunToWebTextRun(text, &run)) |
240 return false; | 243 return false; |
241 | 244 |
242 // Get and map the image data we're painting to. | 245 // Get and map the image data we're painting to. |
243 scoped_refptr<ImageData> image_resource( | 246 scoped_refptr<PPB_ImageData_Impl> image_resource( |
244 Resource::GetAs<ImageData>(image_data)); | 247 Resource::GetAs<PPB_ImageData_Impl>(image_data)); |
245 if (!image_resource.get()) | 248 if (!image_resource.get()) |
246 return false; | 249 return false; |
247 ImageDataAutoMapper mapper(image_resource); | 250 ImageDataAutoMapper mapper(image_resource); |
248 if (!mapper.is_valid()) | 251 if (!mapper.is_valid()) |
249 return false; | 252 return false; |
250 | 253 |
251 // Convert position and clip. | 254 // Convert position and clip. |
252 WebFloatPoint web_position(static_cast<float>(position->x), | 255 WebFloatPoint web_position(static_cast<float>(position->x), |
253 static_cast<float>(position->y)); | 256 static_cast<float>(position->y)); |
254 WebRect web_clip; | 257 WebRect web_clip; |
255 if (!clip) { | 258 if (!clip) { |
256 // Use entire canvas. | 259 // Use entire canvas. |
257 web_clip = WebRect(0, 0, image_resource->width(), image_resource->height()); | 260 web_clip = WebRect(0, 0, image_resource->width(), image_resource->height()); |
258 } else { | 261 } else { |
259 web_clip = WebRect(clip->point.x, clip->point.y, | 262 web_clip = WebRect(clip->point.x, clip->point.y, |
260 clip->size.width, clip->size.height); | 263 clip->size.width, clip->size.height); |
261 } | 264 } |
262 | 265 |
263 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), | 266 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), |
264 run, web_position, color, web_clip, image_data_is_opaque); | 267 run, web_position, color, web_clip, image_data_is_opaque); |
265 return true; | 268 return true; |
266 } | 269 } |
267 | 270 |
268 int32_t Font::MeasureText(const PP_TextRun_Dev* text) { | 271 int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) { |
269 WebTextRun run; | 272 WebTextRun run; |
270 if (!PPTextRunToWebTextRun(text, &run)) | 273 if (!PPTextRunToWebTextRun(text, &run)) |
271 return -1; | 274 return -1; |
272 return font_->calculateWidth(run); | 275 return font_->calculateWidth(run); |
273 } | 276 } |
274 | 277 |
275 uint32_t Font::CharacterOffsetForPixel(const PP_TextRun_Dev* text, | 278 uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text, |
276 int32_t pixel_position) { | 279 int32_t pixel_position) { |
277 WebTextRun run; | 280 WebTextRun run; |
278 if (!PPTextRunToWebTextRun(text, &run)) | 281 if (!PPTextRunToWebTextRun(text, &run)) |
279 return -1; | 282 return -1; |
280 | 283 |
281 return static_cast<uint32_t>(font_->offsetForPosition( | 284 return static_cast<uint32_t>(font_->offsetForPosition( |
282 run, static_cast<float>(pixel_position))); | 285 run, static_cast<float>(pixel_position))); |
283 } | 286 } |
284 | 287 |
285 int32_t Font::PixelOffsetForCharacter(const PP_TextRun_Dev* text, | 288 int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text, |
286 uint32_t char_offset) { | 289 uint32_t char_offset) { |
287 WebTextRun run; | 290 WebTextRun run; |
288 if (!PPTextRunToWebTextRun(text, &run)) | 291 if (!PPTextRunToWebTextRun(text, &run)) |
289 return -1; | 292 return -1; |
290 if (char_offset >= run.text.length()) | 293 if (char_offset >= run.text.length()) |
291 return -1; | 294 return -1; |
292 | 295 |
293 WebFloatRect rect = font_->selectionRectForText( | 296 WebFloatRect rect = font_->selectionRectForText( |
294 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); | 297 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); |
295 return static_cast<int>(rect.width); | 298 return static_cast<int>(rect.width); |
296 } | 299 } |
297 | 300 |
298 } // namespace pepper | 301 } // namespace ppapi |
| 302 } // namespace plugins |
| 303 } // namespace webkit |
| 304 |
OLD | NEW |