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/shared_impl/private/ppb_browser_font_trusted_shared.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/skia/include/core/SkRect.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin t.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatPoin t.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect .h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect .h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" |
24 #include "unicode/ubidi.h" | |
24 | 25 |
25 using ppapi::StringVar; | 26 using ppapi::StringVar; |
26 using ppapi::thunk::EnterResourceNoLock; | 27 using ppapi::thunk::EnterResourceNoLock; |
27 using ppapi::thunk::PPB_ImageData_API; | 28 using ppapi::thunk::PPB_ImageData_API; |
28 using WebKit::WebFloatPoint; | 29 using WebKit::WebFloatPoint; |
29 using WebKit::WebFloatRect; | 30 using WebKit::WebFloatRect; |
30 using WebKit::WebFont; | 31 using WebKit::WebFont; |
31 using WebKit::WebFontDescription; | 32 using WebKit::WebFontDescription; |
32 using WebKit::WebRect; | 33 using WebKit::WebRect; |
33 using WebKit::WebTextRun; | 34 using WebKit::WebTextRun; |
(...skipping 10 matching lines...) Expand all Loading... | |
44 string16 GetFontFromMap( | 45 string16 GetFontFromMap( |
45 const webkit_glue::WebPreferences::ScriptFontFamilyMap& map, | 46 const webkit_glue::WebPreferences::ScriptFontFamilyMap& map, |
46 const std::string& script) { | 47 const std::string& script) { |
47 webkit_glue::WebPreferences::ScriptFontFamilyMap::const_iterator it = | 48 webkit_glue::WebPreferences::ScriptFontFamilyMap::const_iterator it = |
48 map.find(script); | 49 map.find(script); |
49 if (it != map.end()) | 50 if (it != map.end()) |
50 return it->second; | 51 return it->second; |
51 return string16(); | 52 return string16(); |
52 } | 53 } |
53 | 54 |
55 // Splits a PP_BrowserFont_Trusted_TextRun into a sequence or LTR and RTL | |
56 // WebTextRuns that can be used for WebKit. Normally WebKit does this for us, | |
57 // but the font drawing and measurement routines we call happen after this | |
58 // step. So for correct rendering of RTL content, we need to do it ourselves. | |
59 class TextRunCollection { | |
60 public: | |
61 TextRunCollection(const PP_BrowserFont_Trusted_TextRun& run) | |
viettrungluu
2012/06/26 17:59:40
explicit?
| |
62 : bidi_(NULL), | |
63 num_runs_(0) { | |
64 StringVar* text_string = StringVar::FromPPVar(run.text); | |
65 if (!text_string) | |
66 return; // Leave num_runs_ = 0 so we'll do nothing. | |
67 text_ = UTF8ToUTF16(text_string->value()); | |
68 | |
69 if (run.override_direction) { | |
70 // Skip autodetection. | |
71 num_runs_ = 1; | |
72 override_run_ = WebTextRun(text_, PP_ToBool(run.rtl), true); | |
73 } else { | |
74 bidi_ = ubidi_open(); | |
75 UErrorCode uerror = U_ZERO_ERROR; | |
76 ubidi_setPara(bidi_, text_.c_str(), text_.size(), run.rtl, NULL, &uerror); | |
viettrungluu
2012/06/26 17:59:40
text_.data() instead .c_str()?
| |
77 if (U_SUCCESS(uerror)) | |
78 num_runs_ = ubidi_countRuns(bidi_, &uerror); | |
79 } | |
80 } | |
81 | |
82 ~TextRunCollection() { | |
83 if (bidi_) | |
84 ubidi_close(bidi_); | |
85 } | |
86 | |
87 const string16& text() const { return text_; } | |
88 int num_runs() const { return num_runs_; } | |
89 | |
90 // Returns a WebTextRun with the info for the run at the given index. | |
91 // The range covered by the run is in the two output params. | |
92 WebTextRun GetRunAt(int index, int* run_start, int* run_len) const { | |
93 if (bidi_) { | |
94 bool run_rtl = !!ubidi_getVisualRun(bidi_, index, run_start, run_len); | |
viettrungluu
2012/06/26 17:59:40
If ubidi_setPara() failed, do you really want to d
brettw
2012/06/27 17:42:46
If setPara failed, num_runs will be 0 and we'll ne
| |
95 return WebTextRun(string16(&text_[*run_start], *run_len), | |
96 run_rtl, true); | |
97 } | |
98 | |
99 // Override run, return the single one. | |
100 DCHECK(index == 0); | |
101 *run_start = 0; | |
102 *run_len = static_cast<int>(text_.size()); | |
103 return override_run_; | |
104 } | |
105 | |
106 private: | |
107 // Will be null if we skipped autodetection. | |
108 UBiDi* bidi_; | |
109 | |
110 // Text of all the runs. | |
111 string16 text_; | |
112 | |
113 int num_runs_; | |
114 | |
115 // When the content specifies override_direction (bidi_ is null) then this | |
116 // will contain the single text run for WebKit. | |
117 WebTextRun override_run_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(TextRunCollection); | |
120 }; | |
121 | |
54 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, | 122 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, |
55 WebTextRun* run) { | 123 WebTextRun* run) { |
56 StringVar* text_string = StringVar::FromPPVar(text.text); | 124 StringVar* text_string = StringVar::FromPPVar(text.text); |
57 if (!text_string) | 125 if (!text_string) |
58 return false; | 126 return false; |
59 | 127 |
60 *run = WebTextRun(UTF8ToUTF16(text_string->value()), | 128 *run = WebTextRun(UTF8ToUTF16(text_string->value()), |
61 PP_ToBool(text.rtl), | 129 PP_ToBool(text.rtl), |
62 PP_ToBool(text.override_direction)); | 130 PP_ToBool(text.override_direction)); |
63 return true; | 131 return true; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 const PP_BrowserFont_Trusted_TextRun* text) { | 341 const PP_BrowserFont_Trusted_TextRun* text) { |
274 WebTextRun run; | 342 WebTextRun run; |
275 if (!PPTextRunToWebTextRun(*text, &run)) | 343 if (!PPTextRunToWebTextRun(*text, &run)) |
276 return -1; | 344 return -1; |
277 return font_->calculateWidth(run); | 345 return font_->calculateWidth(run); |
278 } | 346 } |
279 | 347 |
280 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( | 348 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( |
281 const PP_BrowserFont_Trusted_TextRun* text, | 349 const PP_BrowserFont_Trusted_TextRun* text, |
282 int32_t pixel_position) { | 350 int32_t pixel_position) { |
283 WebTextRun run; | 351 TextRunCollection runs(*text); |
284 if (!PPTextRunToWebTextRun(*text, &run)) | 352 int32_t cur_pixel_offset = 0; |
285 return -1; | 353 for (int i = 0; i < runs.num_runs(); i++) { |
286 return static_cast<uint32_t>(font_->offsetForPosition( | 354 int run_begin = 0, run_len = 0; |
viettrungluu
2012/06/26 17:59:40
Does our style guide really allow this?
| |
287 run, static_cast<float>(pixel_position))); | 355 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); |
356 int cur_width = font_->calculateWidth(run); | |
viettrungluu
2012/06/26 17:59:40
I think "run_width" would be a better name for thi
| |
357 if (pixel_position < cur_pixel_offset + cur_width) { | |
358 // Offset is in this run. | |
359 return static_cast<uint32_t>(font_->offsetForPosition( | |
360 run, static_cast<float>(pixel_position - cur_pixel_offset))) + | |
361 run_begin; | |
362 } | |
363 cur_pixel_offset += cur_width; | |
364 } | |
365 return runs.text().size(); | |
288 } | 366 } |
289 | 367 |
290 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( | 368 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( |
291 const PP_BrowserFont_Trusted_TextRun* text, | 369 const PP_BrowserFont_Trusted_TextRun* text, |
292 uint32_t char_offset) { | 370 uint32_t char_offset) { |
293 WebTextRun run; | 371 TextRunCollection runs(*text); |
294 if (!PPTextRunToWebTextRun(*text, &run)) | 372 int32_t cur_pixel_offset = 0; |
295 return -1; | 373 for (int i = 0; i < runs.num_runs(); i++) { |
296 if (char_offset >= run.text.length()) | 374 int run_begin = 0, run_len = 0; |
viettrungluu
2012/06/26 17:59:40
...
| |
297 return -1; | 375 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); |
298 | 376 if (char_offset >= static_cast<uint32_t>(run_begin) && |
299 WebFloatRect rect = font_->selectionRectForText( | 377 char_offset < static_cast<uint32_t>(run_begin + run_len)) { |
300 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); | 378 // Character we're looking for is in this run. |
301 return static_cast<int>(rect.width); | 379 WebFloatRect rect = font_->selectionRectForText( |
380 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, | |
381 char_offset - run_begin); | |
382 return cur_pixel_offset + static_cast<int>(rect.width); | |
383 } else { | |
384 // Character is past this run, account for the pixels and continue | |
385 // looking. | |
386 cur_pixel_offset += font_->calculateWidth(run); | |
387 } | |
388 } | |
389 return -1; // Requested a char beyond the end. | |
302 } | 390 } |
303 | 391 |
304 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( | 392 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( |
305 skia::PlatformCanvas* destination, | 393 skia::PlatformCanvas* destination, |
306 const PP_BrowserFont_Trusted_TextRun& text, | 394 const PP_BrowserFont_Trusted_TextRun& text, |
307 const PP_Point* position, | 395 const PP_Point* position, |
308 uint32_t color, | 396 uint32_t color, |
309 const PP_Rect* clip, | 397 const PP_Rect* clip, |
310 PP_Bool image_data_is_opaque) { | 398 PP_Bool image_data_is_opaque) { |
311 WebTextRun run; | |
312 if (!PPTextRunToWebTextRun(text, &run)) | |
313 return; | |
314 | |
315 // Convert position and clip. | 399 // Convert position and clip. |
316 WebFloatPoint web_position(static_cast<float>(position->x), | 400 WebFloatPoint web_position(static_cast<float>(position->x), |
317 static_cast<float>(position->y)); | 401 static_cast<float>(position->y)); |
318 WebRect web_clip; | 402 WebRect web_clip; |
319 if (!clip) { | 403 if (!clip) { |
320 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use | 404 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use |
321 // the current clip bounds. | 405 // the current clip bounds. |
322 SkRect skclip; | 406 SkRect skclip; |
323 destination->getClipBounds(&skclip); | 407 destination->getClipBounds(&skclip); |
324 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, | 408 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, |
325 skclip.fBottom - skclip.fTop); | 409 skclip.fBottom - skclip.fTop); |
326 } else { | 410 } else { |
327 web_clip = WebRect(clip->point.x, clip->point.y, | 411 web_clip = WebRect(clip->point.x, clip->point.y, |
328 clip->size.width, clip->size.height); | 412 clip->size.width, clip->size.height); |
329 } | 413 } |
330 | 414 |
331 font_->drawText(destination, run, web_position, color, web_clip, | 415 TextRunCollection runs(text); |
332 PP_ToBool(image_data_is_opaque)); | 416 for (int i = 0; i < runs.num_runs(); i++) { |
417 int run_begin = 0, run_len = 0; | |
viettrungluu
2012/06/26 17:59:40
...
| |
418 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | |
419 font_->drawText(destination, run, web_position, color, web_clip, | |
420 PP_ToBool(image_data_is_opaque)); | |
421 | |
422 // Advance to the next run. Note that we avoid doing this for the last run | |
423 // since it's unnecessary, measuring text is slow, and most of the time | |
424 // there will be only one run anyway. | |
425 if (i != runs.num_runs() - 1) | |
426 web_position.x += font_->calculateWidth(run); | |
427 } | |
333 } | 428 } |
334 | 429 |
335 } // namespace ppapi | 430 } // namespace ppapi |
OLD | NEW |