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 "ppapi/shared_impl/private/ppb_browser_font_trusted_shared.h" | 5 #include "ppapi/proxy/browser_font_resource_trusted.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.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/shared_impl/ppapi_preferences.h" | 10 #include "ppapi/shared_impl/ppapi_preferences.h" |
11 #include "ppapi/shared_impl/var.h" | 11 #include "ppapi/shared_impl/var.h" |
12 #include "ppapi/thunk/enter.h" | 12 #include "ppapi/thunk/enter.h" |
13 #include "ppapi/thunk/ppb_image_data_api.h" | 13 #include "ppapi/thunk/ppb_image_data_api.h" |
14 #include "ppapi/thunk/thunk.h" | 14 #include "ppapi/thunk/thunk.h" |
15 #include "skia/ext/platform_canvas.h" | 15 #include "skia/ext/platform_canvas.h" |
16 #include "third_party/skia/include/core/SkRect.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin t.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin t.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect .h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect .h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" | 23 #include "third_party/skia/include/core/SkRect.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" | |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" | |
24 #include "unicode/ubidi.h" | 24 #include "unicode/ubidi.h" |
25 | 25 |
26 using ppapi::StringVar; | 26 using ppapi::StringVar; |
27 using ppapi::thunk::EnterResourceNoLock; | 27 using ppapi::thunk::EnterResourceNoLock; |
28 using ppapi::thunk::PPB_ImageData_API; | 28 using ppapi::thunk::PPB_ImageData_API; |
29 using WebKit::WebFloatPoint; | 29 using WebKit::WebFloatPoint; |
30 using WebKit::WebFloatRect; | 30 using WebKit::WebFloatRect; |
31 using WebKit::WebFont; | 31 using WebKit::WebFont; |
32 using WebKit::WebFontDescription; | 32 using WebKit::WebFontDescription; |
33 using WebKit::WebRect; | 33 using WebKit::WebRect; |
34 using WebKit::WebTextRun; | 34 using WebKit::WebTextRun; |
35 using WebKit::WebCanvas; | 35 using WebKit::WebCanvas; |
36 | 36 |
37 namespace ppapi { | 37 namespace ppapi { |
38 namespace proxy { | |
38 | 39 |
39 namespace { | 40 namespace { |
40 | 41 |
41 // Same as WebPreferences::kCommonScript. I'd use that directly here, but get an | 42 // Same as WebPreferences::kCommonScript. I'd use that directly here, but get an |
42 // undefined reference linker error. | 43 // undefined reference linker error. |
43 const char kCommonScript[] = "Zyyy"; | 44 const char kCommonScript[] = "Zyyy"; |
44 | 45 |
45 string16 GetFontFromMap( | 46 string16 GetFontFromMap( |
46 const webkit_glue::WebPreferences::ScriptFontFamilyMap& map, | 47 const webkit_glue::WebPreferences::ScriptFontFamilyMap& map, |
47 const std::string& script) { | 48 const std::string& script) { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 result.smallCaps = font.small_caps != PP_FALSE; | 217 result.smallCaps = font.small_caps != PP_FALSE; |
217 result.weight = static_cast<WebFontDescription::Weight>(font.weight); | 218 result.weight = static_cast<WebFontDescription::Weight>(font.weight); |
218 result.letterSpacing = static_cast<short>(font.letter_spacing); | 219 result.letterSpacing = static_cast<short>(font.letter_spacing); |
219 result.wordSpacing = static_cast<short>(font.word_spacing); | 220 result.wordSpacing = static_cast<short>(font.word_spacing); |
220 return result; | 221 return result; |
221 } | 222 } |
222 | 223 |
223 } // namespace | 224 } // namespace |
224 | 225 |
225 // static | 226 // static |
226 bool PPB_BrowserFont_Trusted_Shared::IsPPFontDescriptionValid( | 227 bool BrowserFontResource_Trusted::IsPPFontDescriptionValid( |
227 const PP_BrowserFont_Trusted_Description& desc) { | 228 const PP_BrowserFont_Trusted_Description& desc) { |
228 // Check validity of string. We can't check the actual text since we could | 229 // Check validity of string. We can't check the actual text since we could |
229 // be on the wrong thread and don't know if we're in the plugin or the host. | 230 // be on the wrong thread and don't know if we're in the plugin or the host. |
230 if (desc.face.type != PP_VARTYPE_STRING && | 231 if (desc.face.type != PP_VARTYPE_STRING && |
231 desc.face.type != PP_VARTYPE_UNDEFINED) | 232 desc.face.type != PP_VARTYPE_UNDEFINED) |
232 return false; | 233 return false; |
233 | 234 |
234 // Check enum ranges. | 235 // Check enum ranges. |
235 if (static_cast<int>(desc.family) < PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT || | 236 if (static_cast<int>(desc.family) < PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT || |
236 static_cast<int>(desc.family) > PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE) | 237 static_cast<int>(desc.family) > PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE) |
237 return false; | 238 return false; |
238 if (static_cast<int>(desc.weight) < PP_BROWSERFONT_TRUSTED_WEIGHT_100 || | 239 if (static_cast<int>(desc.weight) < PP_BROWSERFONT_TRUSTED_WEIGHT_100 || |
239 static_cast<int>(desc.weight) > PP_BROWSERFONT_TRUSTED_WEIGHT_900) | 240 static_cast<int>(desc.weight) > PP_BROWSERFONT_TRUSTED_WEIGHT_900) |
240 return false; | 241 return false; |
241 | 242 |
242 // Check for excessive sizes which may cause layout to get confused. | 243 // Check for excessive sizes which may cause layout to get confused. |
243 if (desc.size > 200) | 244 if (desc.size > 200) |
244 return false; | 245 return false; |
245 | 246 |
246 return true; | 247 return true; |
247 } | 248 } |
248 | 249 |
249 // static | 250 BrowserFontResource_Trusted::BrowserFontResource_Trusted( |
250 PP_Resource PPB_BrowserFont_Trusted_Shared::Create( | 251 Connection connection, |
251 ResourceObjectType type, | |
252 PP_Instance instance, | |
253 const PP_BrowserFont_Trusted_Description& description, | |
254 const Preferences& prefs) { | |
255 if (!PPB_BrowserFont_Trusted_Shared::IsPPFontDescriptionValid(description)) | |
256 return 0; | |
257 return (new PPB_BrowserFont_Trusted_Shared(type, instance, | |
258 description, | |
259 prefs))->GetReference(); | |
260 } | |
261 | |
262 PPB_BrowserFont_Trusted_Shared::PPB_BrowserFont_Trusted_Shared( | |
263 ResourceObjectType type, | |
264 PP_Instance instance, | 252 PP_Instance instance, |
265 const PP_BrowserFont_Trusted_Description& desc, | 253 const PP_BrowserFont_Trusted_Description& desc, |
266 const Preferences& prefs) | 254 const Preferences& prefs) |
267 : Resource(type, instance), | 255 : PluginResource(connection, instance), |
268 font_(WebFont::create(PPFontDescToWebFontDesc(desc, prefs))) { | 256 font_(WebFont::create(PPFontDescToWebFontDesc(desc, prefs))) { |
269 } | 257 } |
270 | 258 |
271 PPB_BrowserFont_Trusted_Shared::~PPB_BrowserFont_Trusted_Shared() { | 259 BrowserFontResource_Trusted::~BrowserFontResource_Trusted() { |
272 } | 260 } |
273 | 261 |
274 thunk::PPB_BrowserFont_Trusted_API* | 262 thunk::PPB_BrowserFont_Trusted_API* |
275 PPB_BrowserFont_Trusted_Shared::AsPPB_BrowserFont_Trusted_API() { | 263 BrowserFontResource_Trusted::AsPPB_BrowserFont_Trusted_API() { |
276 return this; | 264 return this; |
277 } | 265 } |
278 | 266 |
279 PP_Bool PPB_BrowserFont_Trusted_Shared::Describe( | 267 PP_Bool BrowserFontResource_Trusted::Describe( |
280 PP_BrowserFont_Trusted_Description* description, | 268 PP_BrowserFont_Trusted_Description* description, |
281 PP_BrowserFont_Trusted_Metrics* metrics) { | 269 PP_BrowserFont_Trusted_Metrics* metrics) { |
282 if (description->face.type != PP_VARTYPE_UNDEFINED) | 270 if (description->face.type != PP_VARTYPE_UNDEFINED) |
283 return PP_FALSE; | 271 return PP_FALSE; |
284 | 272 |
285 // While converting the other way in PPFontDescToWebFontDesc we validated | 273 // While converting the other way in PPFontDescToWebFontDesc we validated |
286 // that the enums can be casted. | 274 // that the enums can be casted. |
287 WebFontDescription web_desc = font_->fontDescription(); | 275 WebFontDescription web_desc = font_->fontDescription(); |
288 description->face = StringVar::StringToPPVar(UTF16ToUTF8(web_desc.family)); | 276 description->face = StringVar::StringToPPVar(UTF16ToUTF8(web_desc.family)); |
289 description->family = | 277 description->family = |
290 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); | 278 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); |
291 description->size = static_cast<uint32_t>(web_desc.size); | 279 description->size = static_cast<uint32_t>(web_desc.size); |
292 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( | 280 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( |
293 web_desc.weight); | 281 web_desc.weight); |
294 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; | 282 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; |
295 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; | 283 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; |
296 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); | 284 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); |
297 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); | 285 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); |
298 | 286 |
299 metrics->height = font_->height(); | 287 metrics->height = font_->height(); |
300 metrics->ascent = font_->ascent(); | 288 metrics->ascent = font_->ascent(); |
301 metrics->descent = font_->descent(); | 289 metrics->descent = font_->descent(); |
302 metrics->line_spacing = font_->lineSpacing(); | 290 metrics->line_spacing = font_->lineSpacing(); |
303 metrics->x_height = static_cast<int32_t>(font_->xHeight()); | 291 metrics->x_height = static_cast<int32_t>(font_->xHeight()); |
304 | 292 |
305 // Convert the string. | 293 // Convert the string. |
306 return PP_TRUE; | 294 return PP_TRUE; |
307 } | 295 } |
308 | 296 |
309 PP_Bool PPB_BrowserFont_Trusted_Shared::DrawTextAt( | 297 PP_Bool BrowserFontResource_Trusted::DrawTextAt( |
310 PP_Resource image_data, | 298 PP_Resource image_data, |
311 const PP_BrowserFont_Trusted_TextRun* text, | 299 const PP_BrowserFont_Trusted_TextRun* text, |
312 const PP_Point* position, | 300 const PP_Point* position, |
313 uint32_t color, | 301 uint32_t color, |
314 const PP_Rect* clip, | 302 const PP_Rect* clip, |
315 PP_Bool image_data_is_opaque) { | 303 PP_Bool image_data_is_opaque) { |
316 PP_Bool result = PP_FALSE; | 304 PP_Bool result = PP_FALSE; |
317 // Get and map the image data we're painting to. | 305 // Get and map the image data we're painting to. |
318 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); | 306 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); |
319 if (enter.failed()) | 307 if (enter.failed()) |
(...skipping 11 matching lines...) Expand all Loading... | |
331 return result; // Failure mapping. | 319 return result; // Failure mapping. |
332 } | 320 } |
333 | 321 |
334 DrawTextToCanvas(canvas, *text, position, color, clip, image_data_is_opaque); | 322 DrawTextToCanvas(canvas, *text, position, color, clip, image_data_is_opaque); |
335 | 323 |
336 if (needs_unmapping) | 324 if (needs_unmapping) |
337 image->Unmap(); | 325 image->Unmap(); |
338 return PP_TRUE; | 326 return PP_TRUE; |
339 } | 327 } |
340 | 328 |
341 int32_t PPB_BrowserFont_Trusted_Shared::MeasureText( | 329 int32_t BrowserFontResource_Trusted::MeasureText( |
342 const PP_BrowserFont_Trusted_TextRun* text) { | 330 const PP_BrowserFont_Trusted_TextRun* text) { |
343 WebTextRun run; | 331 WebTextRun run; |
344 if (!PPTextRunToWebTextRun(*text, &run)) | 332 if (!PPTextRunToWebTextRun(*text, &run)) |
345 return -1; | 333 return -1; |
346 return font_->calculateWidth(run); | 334 return font_->calculateWidth(run); |
347 } | 335 } |
348 | 336 |
349 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( | 337 uint32_t BrowserFontResource_Trusted::CharacterOffsetForPixel( |
350 const PP_BrowserFont_Trusted_TextRun* text, | 338 const PP_BrowserFont_Trusted_TextRun* text, |
351 int32_t pixel_position) { | 339 int32_t pixel_position) { |
352 TextRunCollection runs(*text); | 340 TextRunCollection runs(*text); |
353 int32_t cur_pixel_offset = 0; | 341 int32_t cur_pixel_offset = 0; |
354 for (int i = 0; i < runs.num_runs(); i++) { | 342 for (int i = 0; i < runs.num_runs(); i++) { |
355 int32_t run_begin = 0; | 343 int32_t run_begin = 0; |
356 int32_t run_len = 0; | 344 int32_t run_len = 0; |
357 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | 345 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); |
358 int run_width = font_->calculateWidth(run); | 346 int run_width = font_->calculateWidth(run); |
359 if (pixel_position < cur_pixel_offset + run_width) { | 347 if (pixel_position < cur_pixel_offset + run_width) { |
360 // Offset is in this run. | 348 // Offset is in this run. |
361 return static_cast<uint32_t>(font_->offsetForPosition( | 349 return static_cast<uint32_t>(font_->offsetForPosition( |
362 run, static_cast<float>(pixel_position - cur_pixel_offset))) + | 350 run, static_cast<float>(pixel_position - cur_pixel_offset))) + |
363 run_begin; | 351 run_begin; |
364 } | 352 } |
365 cur_pixel_offset += run_width; | 353 cur_pixel_offset += run_width; |
366 } | 354 } |
367 return runs.text().size(); | 355 return runs.text().size(); |
368 } | 356 } |
369 | 357 |
370 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( | 358 int32_t BrowserFontResource_Trusted::PixelOffsetForCharacter( |
371 const PP_BrowserFont_Trusted_TextRun* text, | 359 const PP_BrowserFont_Trusted_TextRun* text, |
372 uint32_t char_offset) { | 360 uint32_t char_offset) { |
373 TextRunCollection runs(*text); | 361 TextRunCollection runs(*text); |
374 int32_t cur_pixel_offset = 0; | 362 int32_t cur_pixel_offset = 0; |
375 for (int i = 0; i < runs.num_runs(); i++) { | 363 for (int i = 0; i < runs.num_runs(); i++) { |
376 int32_t run_begin = 0; | 364 int32_t run_begin = 0; |
377 int32_t run_len = 0; | 365 int32_t run_len = 0; |
378 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | 366 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); |
379 if (char_offset >= static_cast<uint32_t>(run_begin) && | 367 if (char_offset >= static_cast<uint32_t>(run_begin) && |
380 char_offset < static_cast<uint32_t>(run_begin + run_len)) { | 368 char_offset < static_cast<uint32_t>(run_begin + run_len)) { |
(...skipping 10 matching lines...) Expand all Loading... | |
391 return cur_pixel_offset + static_cast<int>(rect.x); | 379 return cur_pixel_offset + static_cast<int>(rect.x); |
392 } else { | 380 } else { |
393 // Character is past this run, account for the pixels and continue | 381 // Character is past this run, account for the pixels and continue |
394 // looking. | 382 // looking. |
395 cur_pixel_offset += font_->calculateWidth(run); | 383 cur_pixel_offset += font_->calculateWidth(run); |
396 } | 384 } |
397 } | 385 } |
398 return -1; // Requested a char beyond the end. | 386 return -1; // Requested a char beyond the end. |
399 } | 387 } |
400 | 388 |
401 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( | 389 void BrowserFontResource_Trusted::DrawTextToCanvas( |
402 SkCanvas* destination, | 390 SkCanvas* destination, |
403 const PP_BrowserFont_Trusted_TextRun& text, | 391 const PP_BrowserFont_Trusted_TextRun& text, |
404 const PP_Point* position, | 392 const PP_Point* position, |
405 uint32_t color, | 393 uint32_t color, |
406 const PP_Rect* clip, | 394 const PP_Rect* clip, |
407 PP_Bool image_data_is_opaque) { | 395 PP_Bool image_data_is_opaque) { |
408 // Convert position and clip. | 396 // Convert position and clip. |
409 WebFloatPoint web_position(static_cast<float>(position->x), | 397 WebFloatPoint web_position(static_cast<float>(position->x), |
410 static_cast<float>(position->y)); | 398 static_cast<float>(position->y)); |
411 WebRect web_clip; | 399 WebRect web_clip; |
(...skipping 18 matching lines...) Expand all Loading... | |
430 PP_ToBool(image_data_is_opaque)); | 418 PP_ToBool(image_data_is_opaque)); |
431 | 419 |
432 // Advance to the next run. Note that we avoid doing this for the last run | 420 // Advance to the next run. Note that we avoid doing this for the last run |
433 // since it's unnecessary, measuring text is slow, and most of the time | 421 // since it's unnecessary, measuring text is slow, and most of the time |
434 // there will be only one run anyway. | 422 // there will be only one run anyway. |
435 if (i != runs.num_runs() - 1) | 423 if (i != runs.num_runs() - 1) |
436 web_position.x += font_->calculateWidth(run); | 424 web_position.x += font_->calculateWidth(run); |
437 } | 425 } |
438 } | 426 } |
439 | 427 |
440 } // namespace ppapi | 428 } // namespace ppapi |
brettw
2012/11/26 23:50:37
Nit: swap these two lines.
victorhsieh
2012/11/27 03:00:50
Done.
| |
429 } // namespace proxy | |
OLD | NEW |