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 explicit TextRunCollection(const PP_BrowserFont_Trusted_TextRun& run) | |
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_.data(), text_.size(), run.rtl, NULL, &uerror); | |
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, int32_t* run_start, int32_t* run_len) const { | |
93 DCHECK(index < num_runs_); | |
94 if (bidi_) { | |
95 bool run_rtl = !!ubidi_getVisualRun(bidi_, index, run_start, run_len); | |
96 return WebTextRun(string16(&text_[*run_start], *run_len), | |
97 run_rtl, true); | |
98 } | |
99 | |
100 // Override run, return the single one. | |
101 DCHECK(index == 0); | |
102 *run_start = 0; | |
103 *run_len = static_cast<int>(text_.size()); | |
viettrungluu
2012/06/27 20:33:58
int32_t
| |
104 return override_run_; | |
105 } | |
106 | |
107 private: | |
108 // Will be null if we skipped autodetection. | |
109 UBiDi* bidi_; | |
110 | |
111 // Text of all the runs. | |
112 string16 text_; | |
113 | |
114 int num_runs_; | |
115 | |
116 // When the content specifies override_direction (bidi_ is null) then this | |
117 // will contain the single text run for WebKit. | |
118 WebTextRun override_run_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(TextRunCollection); | |
121 }; | |
122 | |
54 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, | 123 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, |
55 WebTextRun* run) { | 124 WebTextRun* run) { |
56 StringVar* text_string = StringVar::FromPPVar(text.text); | 125 StringVar* text_string = StringVar::FromPPVar(text.text); |
57 if (!text_string) | 126 if (!text_string) |
58 return false; | 127 return false; |
59 | 128 |
60 *run = WebTextRun(UTF8ToUTF16(text_string->value()), | 129 *run = WebTextRun(UTF8ToUTF16(text_string->value()), |
61 PP_ToBool(text.rtl), | 130 PP_ToBool(text.rtl), |
62 PP_ToBool(text.override_direction)); | 131 PP_ToBool(text.override_direction)); |
63 return true; | 132 return true; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 const PP_BrowserFont_Trusted_TextRun* text) { | 342 const PP_BrowserFont_Trusted_TextRun* text) { |
274 WebTextRun run; | 343 WebTextRun run; |
275 if (!PPTextRunToWebTextRun(*text, &run)) | 344 if (!PPTextRunToWebTextRun(*text, &run)) |
276 return -1; | 345 return -1; |
277 return font_->calculateWidth(run); | 346 return font_->calculateWidth(run); |
278 } | 347 } |
279 | 348 |
280 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( | 349 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( |
281 const PP_BrowserFont_Trusted_TextRun* text, | 350 const PP_BrowserFont_Trusted_TextRun* text, |
282 int32_t pixel_position) { | 351 int32_t pixel_position) { |
283 WebTextRun run; | 352 TextRunCollection runs(*text); |
284 if (!PPTextRunToWebTextRun(*text, &run)) | 353 int32_t cur_pixel_offset = 0; |
285 return -1; | 354 for (int i = 0; i < runs.num_runs(); i++) { |
286 return static_cast<uint32_t>(font_->offsetForPosition( | 355 int32_t run_begin = 0; |
287 run, static_cast<float>(pixel_position))); | 356 int32_t run_len = 0; |
357 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | |
358 int run_width = font_->calculateWidth(run); | |
359 if (pixel_position < cur_pixel_offset + run_width) { | |
360 // Offset is in this run. | |
361 return static_cast<uint32_t>(font_->offsetForPosition( | |
362 run, static_cast<float>(pixel_position - cur_pixel_offset))) + | |
363 run_begin; | |
364 } | |
365 cur_pixel_offset += run_width; | |
366 } | |
367 return runs.text().size(); | |
288 } | 368 } |
289 | 369 |
290 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( | 370 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( |
291 const PP_BrowserFont_Trusted_TextRun* text, | 371 const PP_BrowserFont_Trusted_TextRun* text, |
292 uint32_t char_offset) { | 372 uint32_t char_offset) { |
293 WebTextRun run; | 373 TextRunCollection runs(*text); |
294 if (!PPTextRunToWebTextRun(*text, &run)) | 374 int32_t cur_pixel_offset = 0; |
295 return -1; | 375 for (int i = 0; i < runs.num_runs(); i++) { |
296 if (char_offset >= run.text.length()) | 376 int32_t run_begin = 0; |
297 return -1; | 377 int32_t run_len = 0; |
298 | 378 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); |
299 WebFloatRect rect = font_->selectionRectForText( | 379 if (char_offset >= static_cast<uint32_t>(run_begin) && |
300 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); | 380 char_offset < static_cast<uint32_t>(run_begin + run_len)) { |
301 return static_cast<int>(rect.width); | 381 // Character we're looking for is in this run. |
382 // | |
383 // Here we ask WebKit to give us the rectangle around the character in | |
384 // question, and then return the left edge. If we asked for a range of | |
385 // 0 characters starting at the character in question, it would give us | |
386 // a 0-width rect around the insertion point. But that will be on the | |
387 // right side of the character for an RTL run, which would be wrong. | |
388 WebFloatRect rect = font_->selectionRectForText( | |
389 run, WebFloatPoint(0.0f, 0.0f), font_->height(), | |
390 char_offset - run_begin, char_offset - run_begin + 1); | |
391 return cur_pixel_offset + static_cast<int>(rect.x); | |
392 } else { | |
393 // Character is past this run, account for the pixels and continue | |
394 // looking. | |
395 cur_pixel_offset += font_->calculateWidth(run); | |
396 } | |
397 } | |
398 return -1; // Requested a char beyond the end. | |
302 } | 399 } |
303 | 400 |
304 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( | 401 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( |
305 skia::PlatformCanvas* destination, | 402 skia::PlatformCanvas* destination, |
306 const PP_BrowserFont_Trusted_TextRun& text, | 403 const PP_BrowserFont_Trusted_TextRun& text, |
307 const PP_Point* position, | 404 const PP_Point* position, |
308 uint32_t color, | 405 uint32_t color, |
309 const PP_Rect* clip, | 406 const PP_Rect* clip, |
310 PP_Bool image_data_is_opaque) { | 407 PP_Bool image_data_is_opaque) { |
311 WebTextRun run; | |
312 if (!PPTextRunToWebTextRun(text, &run)) | |
313 return; | |
314 | |
315 // Convert position and clip. | 408 // Convert position and clip. |
316 WebFloatPoint web_position(static_cast<float>(position->x), | 409 WebFloatPoint web_position(static_cast<float>(position->x), |
317 static_cast<float>(position->y)); | 410 static_cast<float>(position->y)); |
318 WebRect web_clip; | 411 WebRect web_clip; |
319 if (!clip) { | 412 if (!clip) { |
320 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use | 413 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use |
321 // the current clip bounds. | 414 // the current clip bounds. |
322 SkRect skclip; | 415 SkRect skclip; |
323 destination->getClipBounds(&skclip); | 416 destination->getClipBounds(&skclip); |
324 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, | 417 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, |
325 skclip.fBottom - skclip.fTop); | 418 skclip.fBottom - skclip.fTop); |
326 } else { | 419 } else { |
327 web_clip = WebRect(clip->point.x, clip->point.y, | 420 web_clip = WebRect(clip->point.x, clip->point.y, |
328 clip->size.width, clip->size.height); | 421 clip->size.width, clip->size.height); |
329 } | 422 } |
330 | 423 |
331 font_->drawText(destination, run, web_position, color, web_clip, | 424 TextRunCollection runs(text); |
332 PP_ToBool(image_data_is_opaque)); | 425 for (int i = 0; i < runs.num_runs(); i++) { |
426 int32_t run_begin = 0; | |
427 int32_t run_len = 0; | |
428 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | |
429 font_->drawText(destination, run, web_position, color, web_clip, | |
430 PP_ToBool(image_data_is_opaque)); | |
431 | |
432 // 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 | |
434 // there will be only one run anyway. | |
435 if (i != runs.num_runs() - 1) | |
436 web_position.x += font_->calculateWidth(run); | |
437 } | |
333 } | 438 } |
334 | 439 |
335 } // namespace ppapi | 440 } // namespace ppapi |
OLD | NEW |