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 |
| 25 using ppapi::StringVar; |
| 26 using ppapi::thunk::EnterResourceNoLock; |
| 27 using ppapi::thunk::PPB_ImageData_API; |
| 28 using WebKit::WebFloatPoint; |
| 29 using WebKit::WebFloatRect; |
| 30 using WebKit::WebFont; |
| 31 using WebKit::WebFontDescription; |
| 32 using WebKit::WebRect; |
| 33 using WebKit::WebTextRun; |
| 34 using WebKit::WebCanvas; |
| 35 |
| 36 namespace ppapi { |
| 37 |
| 38 namespace { |
| 39 |
| 40 bool PPTextRunToWebTextRun(const PP_BrowserFont_Trusted_TextRun& text, |
| 41 WebTextRun* run) { |
| 42 StringVar* text_string = StringVar::FromPPVar(text.text); |
| 43 if (!text_string) |
| 44 return false; |
| 45 |
| 46 *run = WebTextRun(UTF8ToUTF16(text_string->value()), |
| 47 PP_ToBool(text.rtl), |
| 48 PP_ToBool(text.override_direction)); |
| 49 return true; |
| 50 } |
| 51 |
| 52 // The PP_* version lacks "None", so is just one value shifted from the |
| 53 // WebFontDescription version. These values are checked in |
| 54 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a |
| 55 // macro so it can also be used in the COMPILE_ASSERTS. |
| 56 #define PP_FAMILY_TO_WEB_FAMILY(f) \ |
| 57 static_cast<WebFontDescription::GenericFamily>(f + 1) |
| 58 |
| 59 // Assumes the given PP_FontDescription has been validated. |
| 60 WebFontDescription PPFontDescToWebFontDesc( |
| 61 const PP_BrowserFont_Trusted_Description& font, |
| 62 const Preferences& prefs) { |
| 63 // Verify that the enums match so we can just static cast. |
| 64 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == |
| 65 static_cast<int>(PP_BROWSERFONT_TRUSTED_WEIGHT_100), |
| 66 FontWeight100); |
| 67 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == |
| 68 static_cast<int>(PP_BROWSERFONT_TRUSTED_WEIGHT_900), |
| 69 FontWeight900); |
| 70 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == |
| 71 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_DEFAULT), |
| 72 StandardFamily); |
| 73 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == |
| 74 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_SERIF), |
| 75 SerifFamily); |
| 76 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == |
| 77 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_SANSSERIF), |
| 78 SansSerifFamily); |
| 79 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == |
| 80 PP_FAMILY_TO_WEB_FAMILY(PP_FONTFAMILY_MONOSPACE), |
| 81 MonospaceFamily); |
| 82 |
| 83 StringVar* face_name = StringVar::FromPPVar(font.face); // Possibly null. |
| 84 |
| 85 WebFontDescription result; |
| 86 string16 resolved_family; |
| 87 if (!face_name || face_name->value().empty()) { |
| 88 // Resolve the generic family. |
| 89 switch (font.family) { |
| 90 case PP_BROWSERFONT_TRUSTED_FAMILY_SERIF: |
| 91 resolved_family = prefs.serif_font_family; |
| 92 break; |
| 93 case PP_BROWSERFONT_TRUSTED_FAMILY_SANSSERIF: |
| 94 resolved_family = prefs.sans_serif_font_family; |
| 95 break; |
| 96 case PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE: |
| 97 resolved_family = prefs.fixed_font_family; |
| 98 break; |
| 99 case PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT: |
| 100 default: |
| 101 resolved_family = prefs.standard_font_family; |
| 102 break; |
| 103 } |
| 104 } else { |
| 105 // Use the exact font. |
| 106 resolved_family = UTF8ToUTF16(face_name->value()); |
| 107 } |
| 108 result.family = resolved_family; |
| 109 |
| 110 result.genericFamily = PP_FAMILY_TO_WEB_FAMILY(font.family); |
| 111 |
| 112 if (font.size == 0) { |
| 113 // Resolve the default font size, using the resolved family to see if |
| 114 // we should use the fixed or regular font size. It's difficult at this |
| 115 // level to detect if the requested font is fixed width, so we only apply |
| 116 // the alternate font size to the default fixed font family. |
| 117 if (StringToLowerASCII(resolved_family) == |
| 118 StringToLowerASCII(prefs.fixed_font_family)) |
| 119 result.size = static_cast<float>(prefs.default_fixed_font_size); |
| 120 else |
| 121 result.size = static_cast<float>(prefs.default_font_size); |
| 122 } else { |
| 123 // Use the exact size. |
| 124 result.size = static_cast<float>(font.size); |
| 125 } |
| 126 |
| 127 result.italic = font.italic != PP_FALSE; |
| 128 result.smallCaps = font.small_caps != PP_FALSE; |
| 129 result.weight = static_cast<WebFontDescription::Weight>(font.weight); |
| 130 result.letterSpacing = static_cast<short>(font.letter_spacing); |
| 131 result.wordSpacing = static_cast<short>(font.word_spacing); |
| 132 return result; |
| 133 } |
| 134 |
| 135 } // namespace |
| 136 |
| 137 // static |
| 138 bool PPB_BrowserFont_Trusted_Shared::IsPPFontDescriptionValid( |
| 139 const PP_BrowserFont_Trusted_Description& desc) { |
| 140 // Check validity of string. We can't check the actual text since we could |
| 141 // be on the wrong thread and don't know if we're in the plugin or the host. |
| 142 if (desc.face.type != PP_VARTYPE_STRING && |
| 143 desc.face.type != PP_VARTYPE_UNDEFINED) |
| 144 return false; |
| 145 |
| 146 // Check enum ranges. |
| 147 if (static_cast<int>(desc.family) < PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT || |
| 148 static_cast<int>(desc.family) > PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE) |
| 149 return false; |
| 150 if (static_cast<int>(desc.weight) < PP_BROWSERFONT_TRUSTED_WEIGHT_100 || |
| 151 static_cast<int>(desc.weight) > PP_BROWSERFONT_TRUSTED_WEIGHT_900) |
| 152 return false; |
| 153 |
| 154 // Check for excessive sizes which may cause layout to get confused. |
| 155 if (desc.size > 200) |
| 156 return false; |
| 157 |
| 158 return true; |
| 159 } |
| 160 |
| 161 // static |
| 162 PP_Resource PPB_BrowserFont_Trusted_Shared::Create( |
| 163 ResourceObjectType type, |
| 164 PP_Instance instance, |
| 165 const PP_BrowserFont_Trusted_Description& description, |
| 166 const Preferences& prefs) { |
| 167 if (!PPB_BrowserFont_Trusted_Shared::IsPPFontDescriptionValid(description)) |
| 168 return 0; |
| 169 return (new PPB_BrowserFont_Trusted_Shared(type, instance, |
| 170 description, |
| 171 prefs))->GetReference(); |
| 172 } |
| 173 |
| 174 PPB_BrowserFont_Trusted_Shared::PPB_BrowserFont_Trusted_Shared( |
| 175 ResourceObjectType type, |
| 176 PP_Instance instance, |
| 177 const PP_BrowserFont_Trusted_Description& desc, |
| 178 const Preferences& prefs) |
| 179 : Resource(type, instance), |
| 180 font_(WebFont::create(PPFontDescToWebFontDesc(desc, prefs))) { |
| 181 } |
| 182 |
| 183 PPB_BrowserFont_Trusted_Shared::~PPB_BrowserFont_Trusted_Shared() { |
| 184 } |
| 185 |
| 186 thunk::PPB_BrowserFont_Trusted_API* |
| 187 PPB_BrowserFont_Trusted_Shared::AsPPB_BrowserFont_Trusted_API() { |
| 188 return this; |
| 189 } |
| 190 |
| 191 PP_Bool PPB_BrowserFont_Trusted_Shared::Describe( |
| 192 PP_BrowserFont_Trusted_Description* description, |
| 193 PP_BrowserFont_Trusted_Metrics* metrics) { |
| 194 if (description->face.type != PP_VARTYPE_UNDEFINED) |
| 195 return PP_FALSE; |
| 196 |
| 197 // While converting the other way in PPFontDescToWebFontDesc we validated |
| 198 // that the enums can be casted. |
| 199 WebFontDescription web_desc = font_->fontDescription(); |
| 200 description->face = StringVar::StringToPPVar(UTF16ToUTF8(web_desc.family)); |
| 201 description->family = |
| 202 static_cast<PP_BrowserFont_Trusted_Family>(web_desc.genericFamily); |
| 203 description->size = static_cast<uint32_t>(web_desc.size); |
| 204 description->weight = static_cast<PP_BrowserFont_Trusted_Weight>( |
| 205 web_desc.weight); |
| 206 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; |
| 207 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; |
| 208 description->letter_spacing = static_cast<int32_t>(web_desc.letterSpacing); |
| 209 description->word_spacing = static_cast<int32_t>(web_desc.wordSpacing); |
| 210 |
| 211 metrics->height = font_->height(); |
| 212 metrics->ascent = font_->ascent(); |
| 213 metrics->descent = font_->descent(); |
| 214 metrics->line_spacing = font_->lineSpacing(); |
| 215 metrics->x_height = static_cast<int32_t>(font_->xHeight()); |
| 216 |
| 217 // Convert the string. |
| 218 return PP_TRUE; |
| 219 } |
| 220 |
| 221 PP_Bool PPB_BrowserFont_Trusted_Shared::DrawTextAt( |
| 222 PP_Resource image_data, |
| 223 const PP_BrowserFont_Trusted_TextRun* text, |
| 224 const PP_Point* position, |
| 225 uint32_t color, |
| 226 const PP_Rect* clip, |
| 227 PP_Bool image_data_is_opaque) { |
| 228 PP_Bool result = PP_FALSE; |
| 229 // Get and map the image data we're painting to. |
| 230 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); |
| 231 if (enter.failed()) |
| 232 return result; |
| 233 |
| 234 PPB_ImageData_API* image = static_cast<PPB_ImageData_API*>( |
| 235 enter.object()); |
| 236 skia::PlatformCanvas* canvas = image->GetPlatformCanvas(); |
| 237 bool needs_unmapping = false; |
| 238 if (!canvas) { |
| 239 needs_unmapping = true; |
| 240 image->Map(); |
| 241 canvas = image->GetPlatformCanvas(); |
| 242 if (!canvas) |
| 243 return result; // Failure mapping. |
| 244 } |
| 245 |
| 246 DrawTextToCanvas(canvas, *text, position, color, clip, image_data_is_opaque); |
| 247 |
| 248 if (needs_unmapping) |
| 249 image->Unmap(); |
| 250 return PP_TRUE; |
| 251 } |
| 252 |
| 253 int32_t PPB_BrowserFont_Trusted_Shared::MeasureText( |
| 254 const PP_BrowserFont_Trusted_TextRun* text) { |
| 255 WebTextRun run; |
| 256 if (!PPTextRunToWebTextRun(*text, &run)) |
| 257 return -1; |
| 258 return font_->calculateWidth(run); |
| 259 } |
| 260 |
| 261 uint32_t PPB_BrowserFont_Trusted_Shared::CharacterOffsetForPixel( |
| 262 const PP_BrowserFont_Trusted_TextRun* text, |
| 263 int32_t pixel_position) { |
| 264 WebTextRun run; |
| 265 if (!PPTextRunToWebTextRun(*text, &run)) |
| 266 return -1; |
| 267 return static_cast<uint32_t>(font_->offsetForPosition( |
| 268 run, static_cast<float>(pixel_position))); |
| 269 } |
| 270 |
| 271 int32_t PPB_BrowserFont_Trusted_Shared::PixelOffsetForCharacter( |
| 272 const PP_BrowserFont_Trusted_TextRun* text, |
| 273 uint32_t char_offset) { |
| 274 WebTextRun run; |
| 275 if (!PPTextRunToWebTextRun(*text, &run)) |
| 276 return -1; |
| 277 if (char_offset >= run.text.length()) |
| 278 return -1; |
| 279 |
| 280 WebFloatRect rect = font_->selectionRectForText( |
| 281 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); |
| 282 return static_cast<int>(rect.width); |
| 283 } |
| 284 |
| 285 void PPB_BrowserFont_Trusted_Shared::DrawTextToCanvas( |
| 286 skia::PlatformCanvas* destination, |
| 287 const PP_BrowserFont_Trusted_TextRun& text, |
| 288 const PP_Point* position, |
| 289 uint32_t color, |
| 290 const PP_Rect* clip, |
| 291 PP_Bool image_data_is_opaque) { |
| 292 WebTextRun run; |
| 293 if (!PPTextRunToWebTextRun(text, &run)) |
| 294 return; |
| 295 |
| 296 // Convert position and clip. |
| 297 WebFloatPoint web_position(static_cast<float>(position->x), |
| 298 static_cast<float>(position->y)); |
| 299 WebRect web_clip; |
| 300 if (!clip) { |
| 301 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use |
| 302 // the current clip bounds. |
| 303 SkRect skclip; |
| 304 destination->getClipBounds(&skclip); |
| 305 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, |
| 306 skclip.fBottom - skclip.fTop); |
| 307 } else { |
| 308 web_clip = WebRect(clip->point.x, clip->point.y, |
| 309 clip->size.width, clip->size.height); |
| 310 } |
| 311 |
| 312 #if WEBKIT_USING_SKIA |
| 313 WebCanvas* canvas = destination; |
| 314 #elif WEBKIT_USING_CG |
| 315 WebCanvas* canvas = skia::GetBitmapContext(skia::GetTopDevice(*destination)); |
| 316 #else |
| 317 NOTIMPLEMENTED(); |
| 318 return; |
| 319 #endif |
| 320 font_->drawText(canvas, run, web_position, color, web_clip, |
| 321 PP_ToBool(image_data_is_opaque)); |
| 322 } |
| 323 |
| 324 } // namespace ppapi |
| 325 |
OLD | NEW |