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