| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ppapi/shared_impl/private/ppb_browser_font_trusted_shared.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "ppapi/c/dev/ppb_font_dev.h" | |
| 10 #include "ppapi/shared_impl/ppapi_preferences.h" | |
| 11 #include "ppapi/shared_impl/var.h" | |
| 12 #include "ppapi/thunk/enter.h" | |
| 13 #include "ppapi/thunk/ppb_image_data_api.h" | |
| 14 #include "ppapi/thunk/thunk.h" | |
| 15 #include "skia/ext/platform_canvas.h" | |
| 16 #include "third_party/skia/include/core/SkRect.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" | |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebFloatRect
.h" | |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.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" | |
| 25 | |
| 26 using ppapi::StringVar; | |
| 27 using ppapi::thunk::EnterResourceNoLock; | |
| 28 using ppapi::thunk::PPB_ImageData_API; | |
| 29 using WebKit::WebFloatPoint; | |
| 30 using WebKit::WebFloatRect; | |
| 31 using WebKit::WebFont; | |
| 32 using WebKit::WebFontDescription; | |
| 33 using WebKit::WebRect; | |
| 34 using WebKit::WebTextRun; | |
| 35 using WebKit::WebCanvas; | |
| 36 | |
| 37 namespace ppapi { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // Same as WebPreferences::kCommonScript. I'd use that directly here, but get an | |
| 42 // undefined reference linker error. | |
| 43 const char kCommonScript[] = "Zyyy"; | |
| 44 | |
| 45 string16 GetFontFromMap( | |
| 46 const webkit_glue::WebPreferences::ScriptFontFamilyMap& map, | |
| 47 const std::string& script) { | |
| 48 webkit_glue::WebPreferences::ScriptFontFamilyMap::const_iterator it = | |
| 49 map.find(script); | |
| 50 if (it != map.end()) | |
| 51 return it->second; | |
| 52 return string16(); | |
| 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<int32_t>(text_.size()); | |
| 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 | |
| 123 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, | |
| 124 WebTextRun* run) { | |
| 125 StringVar* text_string = StringVar::FromPPVar(text.text); | |
| 126 if (!text_string) | |
| 127 return false; | |
| 128 | |
| 129 *run = WebTextRun(UTF8ToUTF16(text_string->value()), | |
| 130 PP_ToBool(text.rtl), | |
| 131 PP_ToBool(text.override_direction)); | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 // The PP_* version lacks "None", so is just one value shifted from the | |
| 136 // WebFontDescription version. These values are checked in | |
| 137 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a | |
| 138 // macro so it can also be used in the COMPILE_ASSERTS. | |
| 139 #define PP_FAMILY_TO_WEB_FAMILY(f) \ | |
| 140 static_cast<WebFontDescription::GenericFamily>(f + 1) | |
| 141 | |
| 142 // Assumes the given PP_FontDescription has been validated. | |
| 143 WebFontDescription PPFontDescToWebFontDesc( | |
| 144 const PP_BrowserFont_Trusted_Description& font, | |
| 145 const Preferences& prefs) { | |
| 146 // Verify that the enums match so we can just static cast. | |
| 147 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == | |
| 148 static_cast<int>(PP_BROWSERFONT_TRUSTED_WEIGHT_100), | |
| 149 FontWeight100); | |
| 150 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == | |
| 151 static_cast<int>(PP_BROWSERFONT_TRUSTED_WEIGHT_900), | |
| 152 FontWeight900); | |
| 153 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == | |
| 154 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_DEFAULT), | |
| 155 StandardFamily); | |
| 156 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == | |
| 157 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_SERIF), | |
| 158 SerifFamily); | |
| 159 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == | |
| 160 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_SANSSERIF), | |
| 161 SansSerifFamily); | |
| 162 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == | |
| 163 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_MONOSPACE), | |
| 164 MonospaceFamily); | |
| 165 | |
| 166 StringVar* face_name = StringVar::FromPPVar(font.face); // Possibly null. | |
| 167 | |
| 168 WebFontDescription result; | |
| 169 string16 resolved_family; | |
| 170 if (!face_name || face_name->value().empty()) { | |
| 171 // Resolve the generic family. | |
| 172 switch (font.family) { | |
| 173 case PP_BROWSERFONT_TRUSTED_FAMILY_SERIF: | |
| 174 resolved_family = GetFontFromMap(prefs.serif_font_family_map, | |
| 175 kCommonScript); | |
| 176 break; | |
| 177 case PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF: | |
| 178 resolved_family = GetFontFromMap(prefs.sans_serif_font_family_map, | |
| 179 kCommonScript); | |
| 180 break; | |
| 181 case PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE: | |
| 182 resolved_family = GetFontFromMap(prefs.fixed_font_family_map, | |
| 183 kCommonScript); | |
| 184 break; | |
| 185 case PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT: | |
| 186 default: | |
| 187 resolved_family = GetFontFromMap(prefs.standard_font_family_map, | |
| 188 kCommonScript); | |
| 189 break; | |
| 190 } | |
| 191 } else { | |
| 192 // Use the exact font. | |
| 193 resolved_family = UTF8ToUTF16(face_name->value()); | |
| 194 } | |
| 195 result.family = resolved_family; | |
| 196 | |
| 197 result.genericFamily = PP_FAMILY_TO_WEB_FAMILY(font.family); | |
| 198 | |
| 199 if (font.size == 0) { | |
| 200 // Resolve the default font size, using the resolved family to see if | |
| 201 // we should use the fixed or regular font size. It's difficult at this | |
| 202 // level to detect if the requested font is fixed width, so we only apply | |
| 203 // the alternate font size to the default fixed font family. | |
| 204 if (StringToLowerASCII(resolved_family) == | |
| 205 StringToLowerASCII(GetFontFromMap(prefs.fixed_font_family_map, | |
| 206 kCommonScript))) | |
| 207 result.size = static_cast<float>(prefs.default_fixed_font_size); | |
| 208 else | |
| 209 result.size = static_cast<float>(prefs.default_font_size); | |
| 210 } else { | |
| 211 // Use the exact size. | |
| 212 result.size = static_cast<float>(font.size); | |
| 213 } | |
| 214 | |
| 215 result.italic = font.italic != PP_FALSE; | |
| 216 result.smallCaps = font.small_caps != PP_FALSE; | |
| 217 result.weight = static_cast<WebFontDescription::Weight>(font.weight); | |
| 218 result.letterSpacing = static_cast<short>(font.letter_spacing); | |
| 219 result.wordSpacing = static_cast<short>(font.word_spacing); | |
| 220 return result; | |
| 221 } | |
| 222 | |
| 223 } // namespace | |
| 224 | |
| 225 // static | |
| 226 bool PPB_BrowserFont_Trusted_Shared::IsPPFontDescriptionValid( | |
| 227 const PP_BrowserFont_Trusted_Description& desc) { | |
| 228 // 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 if (desc.face.type != PP_VARTYPE_STRING && | |
| 231 desc.face.type != PP_VARTYPE_UNDEFINED) | |
| 232 return false; | |
| 233 | |
| 234 // Check enum ranges. | |
| 235 if (static_cast<int>(desc.family) < PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT || | |
| 236 static_cast<int>(desc.family) > PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE) | |
| 237 return false; | |
| 238 if (static_cast<int>(desc.weight) < PP_BROWSERFONT_TRUSTED_WEIGHT_100 || | |
| 239 static_cast<int>(desc.weight) > PP_BROWSERFONT_TRUSTED_WEIGHT_900) | |
| 240 return false; | |
| 241 | |
| 242 // Check for excessive sizes which may cause layout to get confused. | |
| 243 if (desc.size > 200) | |
| 244 return false; | |
| 245 | |
| 246 return true; | |
| 247 } | |
| 248 | |
| 249 // static | |
| 250 PP_Resource PPB_BrowserFont_Trusted_Shared::Create( | |
| 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, | |
| 265 const PP_BrowserFont_Trusted_Description& desc, | |
| 266 const Preferences& prefs) | |
| 267 : Resource(type, instance), | |
| 268 font_(WebFont::create(PPFontDescToWebFontDesc(desc, prefs))) { | |
| 269 } | |
| 270 | |
| 271 PPB_BrowserFont_Trusted_Shared::~PPB_BrowserFont_Trusted_Shared() { | |
| 272 } | |
| 273 | |
| 274 thunk::PPB_BrowserFont_Trusted_API* | |
| 275 PPB_BrowserFont_Trusted_Shared::AsPPB_BrowserFont_Trusted_API() { | |
| 276 return this; | |
| 277 } | |
| 278 | |
| 279 PP_Bool PPB_BrowserFont_Trusted_Shared::Describe( | |
| 280 PP_BrowserFont_Trusted_Description* description, | |
| 281 PP_BrowserFont_Trusted_Metrics* metrics) { | |
| 282 if (description->face.type != PP_VARTYPE_UNDEFINED) | |
| 283 return PP_FALSE; | |
| 284 | |
| 285 // While converting the other way in PPFontDescToWebFontDesc we validated | |
| 286 // that the enums can be casted. | |
| 287 WebFontDescription web_desc = font_->fontDescription(); | |
| 288 description->face = StringVar::StringToPPVar(UTF16ToUTF8(web_desc.family)); | |
| 289 description->family = | |
| 290 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); | |
| 291 description->size = static_cast<uint32_t>(web_desc.size); | |
| 292 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( | |
| 293 web_desc.weight); | |
| 294 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; | |
| 295 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; | |
| 296 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); | |
| 297 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); | |
| 298 | |
| 299 metrics->height = font_->height(); | |
| 300 metrics->ascent = font_->ascent(); | |
| 301 metrics->descent = font_->descent(); | |
| 302 metrics->line_spacing = font_->lineSpacing(); | |
| 303 metrics->x_height = static_cast<int32_t>(font_->xHeight()); | |
| 304 | |
| 305 // Convert the string. | |
| 306 return PP_TRUE; | |
| 307 } | |
| 308 | |
| 309 PP_Bool PPB_BrowserFont_Trusted_Shared::DrawTextAt( | |
| 310 PP_Resource image_data, | |
| 311 const PP_BrowserFont_Trusted_TextRun* text, | |
| 312 const PP_Point* position, | |
| 313 uint32_t color, | |
| 314 const PP_Rect* clip, | |
| 315 PP_Bool image_data_is_opaque) { | |
| 316 PP_Bool result = PP_FALSE; | |
| 317 // Get and map the image data we're painting to. | |
| 318 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); | |
| 319 if (enter.failed()) | |
| 320 return result; | |
| 321 | |
| 322 PPB_ImageData_API* image = static_cast<PPB_ImageData_API*>( | |
| 323 enter.object()); | |
| 324 SkCanvas* canvas = image->GetPlatformCanvas(); | |
| 325 bool needs_unmapping = false; | |
| 326 if (!canvas) { | |
| 327 needs_unmapping = true; | |
| 328 image->Map(); | |
| 329 canvas = image->GetPlatformCanvas(); | |
| 330 if (!canvas) | |
| 331 return result; // Failure mapping. | |
| 332 } | |
| 333 | |
| 334 DrawTextToCanvas(canvas, *text, position, color, clip, image_data_is_opaque); | |
| 335 | |
| 336 if (needs_unmapping) | |
| 337 image->Unmap(); | |
| 338 return PP_TRUE; | |
| 339 } | |
| 340 | |
| 341 int32_t PPB_BrowserFont_Trusted_Shared::MeasureText( | |
| 342 const PP_BrowserFont_Trusted_TextRun* text) { | |
| 343 WebTextRun run; | |
| 344 if (!PPTextRunToWebTextRun(*text, &run)) | |
| 345 return -1; | |
| 346 return font_->calculateWidth(run); | |
| 347 } | |
| 348 | |
| 349 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( | |
| 350 const PP_BrowserFont_Trusted_TextRun* text, | |
| 351 int32_t pixel_position) { | |
| 352 TextRunCollection runs(*text); | |
| 353 int32_t cur_pixel_offset = 0; | |
| 354 for (int i = 0; i < runs.num_runs(); i++) { | |
| 355 int32_t run_begin = 0; | |
| 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(); | |
| 368 } | |
| 369 | |
| 370 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( | |
| 371 const PP_BrowserFont_Trusted_TextRun* text, | |
| 372 uint32_t char_offset) { | |
| 373 TextRunCollection runs(*text); | |
| 374 int32_t cur_pixel_offset = 0; | |
| 375 for (int i = 0; i < runs.num_runs(); i++) { | |
| 376 int32_t run_begin = 0; | |
| 377 int32_t run_len = 0; | |
| 378 WebTextRun run = runs.GetRunAt(i, &run_begin, &run_len); | |
| 379 if (char_offset >= static_cast<uint32_t>(run_begin) && | |
| 380 char_offset < static_cast<uint32_t>(run_begin + run_len)) { | |
| 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. | |
| 399 } | |
| 400 | |
| 401 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( | |
| 402 SkCanvas* destination, | |
| 403 const PP_BrowserFont_Trusted_TextRun& text, | |
| 404 const PP_Point* position, | |
| 405 uint32_t color, | |
| 406 const PP_Rect* clip, | |
| 407 PP_Bool image_data_is_opaque) { | |
| 408 // Convert position and clip. | |
| 409 WebFloatPoint web_position(static_cast<float>(position->x), | |
| 410 static_cast<float>(position->y)); | |
| 411 WebRect web_clip; | |
| 412 if (!clip) { | |
| 413 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use | |
| 414 // the current clip bounds. | |
| 415 SkRect skclip; | |
| 416 destination->getClipBounds(&skclip); | |
| 417 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, | |
| 418 skclip.fBottom - skclip.fTop); | |
| 419 } else { | |
| 420 web_clip = WebRect(clip->point.x, clip->point.y, | |
| 421 clip->size.width, clip->size.height); | |
| 422 } | |
| 423 | |
| 424 TextRunCollection runs(text); | |
| 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 } | |
| 438 } | |
| 439 | |
| 440 } // namespace ppapi | |
| OLD | NEW |