| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/plugins/ppapi/ppb_font_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_font_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "ppapi/c/dev/ppb_font_dev.h" | 7 #include "ppapi/c/dev/ppb_font_dev.h" |
| 10 #include "ppapi/c/pp_rect.h" | 8 #include "ppapi/thunk/thunk.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" | |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" | |
| 17 #include "webkit/plugins/ppapi/common.h" | 9 #include "webkit/plugins/ppapi/common.h" |
| 18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 10 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 19 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 11 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
| 20 #include "webkit/plugins/ppapi/string.h" | 12 #include "webkit/plugins/ppapi/string.h" |
| 21 #include "webkit/plugins/ppapi/var.h" | 13 #include "webkit/plugins/ppapi/var.h" |
| 22 #include "webkit/glue/webkit_glue.h" | |
| 23 | |
| 24 using WebKit::WebFloatPoint; | |
| 25 using WebKit::WebFloatRect; | |
| 26 using WebKit::WebFont; | |
| 27 using WebKit::WebFontDescription; | |
| 28 using WebKit::WebRect; | |
| 29 using WebKit::WebTextRun; | |
| 30 | 14 |
| 31 namespace webkit { | 15 namespace webkit { |
| 32 namespace ppapi { | 16 namespace ppapi { |
| 33 | 17 |
| 34 namespace { | 18 namespace { |
| 35 | 19 |
| 36 bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { | 20 // Converts the given PP_TextRun to a TextRun, returning true on success. |
| 37 // Check validity of UTF-8. | |
| 38 if (desc.face.type != PP_VARTYPE_STRING && | |
| 39 desc.face.type != PP_VARTYPE_UNDEFINED) | |
| 40 return false; | |
| 41 | |
| 42 // Check enum ranges. | |
| 43 if (static_cast<int>(desc.family) < PP_FONTFAMILY_DEFAULT || | |
| 44 static_cast<int>(desc.family) > PP_FONTFAMILY_MONOSPACE) | |
| 45 return false; | |
| 46 if (static_cast<int>(desc.weight) < PP_FONTWEIGHT_100 || | |
| 47 static_cast<int>(desc.weight) > PP_FONTWEIGHT_900) | |
| 48 return false; | |
| 49 | |
| 50 // Check for excessive sizes which may cause layout to get confused. | |
| 51 if (desc.size > 200) | |
| 52 return false; | |
| 53 | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 // The PP_* version lacks "None", so is just one value shifted from the | |
| 58 // WebFontDescription version. These values are checked in | |
| 59 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a | |
| 60 // macro so it can also be used in the COMPILE_ASSERTS. | |
| 61 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ | |
| 62 static_cast<WebFontDescription::GenericFamily>(f + 1) | |
| 63 | |
| 64 // Assumes the given PP_FontDescription has been validated. | |
| 65 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font) { | |
| 66 // Verify that the enums match so we can just static cast. | |
| 67 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == | |
| 68 static_cast<int>(PP_FONTWEIGHT_100), | |
| 69 FontWeight100); | |
| 70 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == | |
| 71 static_cast<int>(PP_FONTWEIGHT_900), | |
| 72 FontWeight900); | |
| 73 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == | |
| 74 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), | |
| 75 StandardFamily); | |
| 76 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == | |
| 77 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), | |
| 78 SerifFamily); | |
| 79 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == | |
| 80 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), | |
| 81 SansSerifFamily); | |
| 82 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == | |
| 83 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), | |
| 84 MonospaceFamily); | |
| 85 | |
| 86 WebFontDescription result; | |
| 87 scoped_refptr<StringVar> face_name(StringVar::FromPPVar(font.face)); | |
| 88 if (face_name) | |
| 89 result.family = UTF8ToUTF16(face_name->value()); | |
| 90 result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); | |
| 91 result.size = static_cast<float>(font.size); | |
| 92 result.italic = PPBoolToBool(font.italic); | |
| 93 result.smallCaps = PPBoolToBool(font.small_caps); | |
| 94 result.weight = static_cast<WebFontDescription::Weight>(font.weight); | |
| 95 result.letterSpacing = static_cast<short>(font.letter_spacing); | |
| 96 result.wordSpacing = static_cast<short>(font.word_spacing); | |
| 97 return result; | |
| 98 } | |
| 99 | |
| 100 // Converts the given PP_TextRun to a WebTextRun, returning true on success. | |
| 101 // False means the input was invalid. | 21 // False means the input was invalid. |
| 102 bool PPTextRunToWebTextRun(const PP_TextRun_Dev* run, WebTextRun* output) { | 22 bool PPTextRunToTextRun(const PP_TextRun_Dev* run, |
| 23 PPB_Font_Impl::TextRun* output) { |
| 103 scoped_refptr<StringVar> text_string(StringVar::FromPPVar(run->text)); | 24 scoped_refptr<StringVar> text_string(StringVar::FromPPVar(run->text)); |
| 104 if (!text_string) | 25 if (!text_string) |
| 105 return false; | 26 return false; |
| 106 *output = WebTextRun(UTF8ToUTF16(text_string->value()), | 27 |
| 107 PPBoolToBool(run->rtl), | 28 output->text = text_string->value(); |
| 108 PPBoolToBool(run->override_direction)); | 29 output->rtl = PPBoolToBool(run->rtl); |
| 30 output->override_direction = PPBoolToBool(run->override_direction); |
| 109 return true; | 31 return true; |
| 110 } | 32 } |
| 111 | 33 |
| 112 PP_Resource Create(PP_Instance instance_id, | |
| 113 const PP_FontDescription_Dev* description) { | |
| 114 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 115 if (!instance) | |
| 116 return 0; | |
| 117 | |
| 118 if (!IsPPFontDescriptionValid(*description)) | |
| 119 return 0; | |
| 120 | |
| 121 scoped_refptr<PPB_Font_Impl> font(new PPB_Font_Impl(instance, *description)); | |
| 122 return font->GetReference(); | |
| 123 } | |
| 124 | |
| 125 PP_Bool IsFont(PP_Resource resource) { | |
| 126 return BoolToPPBool(!!Resource::GetAs<PPB_Font_Impl>(resource).get()); | |
| 127 } | |
| 128 | |
| 129 PP_Bool Describe(PP_Resource font_id, | |
| 130 PP_FontDescription_Dev* description, | |
| 131 PP_FontMetrics_Dev* metrics) { | |
| 132 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); | |
| 133 if (!font.get()) | |
| 134 return PP_FALSE; | |
| 135 return BoolToPPBool(font->Describe(description, metrics)); | |
| 136 } | |
| 137 | |
| 138 PP_Bool DrawTextAt(PP_Resource font_id, | |
| 139 PP_Resource image_data, | |
| 140 const PP_TextRun_Dev* text, | |
| 141 const PP_Point* position, | |
| 142 uint32_t color, | |
| 143 const PP_Rect* clip, | |
| 144 PP_Bool image_data_is_opaque) { | |
| 145 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); | |
| 146 if (!font.get()) | |
| 147 return PP_FALSE; | |
| 148 return BoolToPPBool(font->DrawTextAt(image_data, text, position, color, clip, | |
| 149 PPBoolToBool(image_data_is_opaque))); | |
| 150 } | |
| 151 | |
| 152 int32_t MeasureText(PP_Resource font_id, const PP_TextRun_Dev* text) { | |
| 153 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); | |
| 154 if (!font.get()) | |
| 155 return -1; | |
| 156 return font->MeasureText(text); | |
| 157 } | |
| 158 | |
| 159 uint32_t CharacterOffsetForPixel(PP_Resource font_id, | |
| 160 const PP_TextRun_Dev* text, | |
| 161 int32_t pixel_position) { | |
| 162 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); | |
| 163 if (!font.get()) | |
| 164 return -1; | |
| 165 return font->CharacterOffsetForPixel(text, pixel_position); | |
| 166 } | |
| 167 | |
| 168 int32_t PixelOffsetForCharacter(PP_Resource font_id, | |
| 169 const PP_TextRun_Dev* text, | |
| 170 uint32_t char_offset) { | |
| 171 scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); | |
| 172 if (!font.get()) | |
| 173 return false; | |
| 174 return font->PixelOffsetForCharacter(text, char_offset); | |
| 175 } | |
| 176 | |
| 177 const PPB_Font_Dev ppb_font = { | |
| 178 &Create, | |
| 179 &IsFont, | |
| 180 &Describe, | |
| 181 &DrawTextAt, | |
| 182 &MeasureText, | |
| 183 &CharacterOffsetForPixel, | |
| 184 &PixelOffsetForCharacter | |
| 185 }; | |
| 186 | |
| 187 } // namespace | 34 } // namespace |
| 188 | 35 |
| 189 PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance, | 36 PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance, |
| 190 const PP_FontDescription_Dev& desc) | 37 const PP_FontDescription_Dev& desc) |
| 191 : Resource(instance) { | 38 : Resource(instance) { |
| 192 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); | 39 scoped_refptr<StringVar> face_name(StringVar::FromPPVar(desc.face)); |
| 193 font_.reset(WebFont::create(web_font_desc)); | 40 DoCreate(NULL, desc, face_name ? face_name->value() : std::string()); |
| 194 } | 41 } |
| 195 | 42 |
| 196 PPB_Font_Impl::~PPB_Font_Impl() { | 43 PPB_Font_Impl::~PPB_Font_Impl() { |
| 197 } | 44 } |
| 198 | 45 |
| 199 // static | 46 // static |
| 200 const PPB_Font_Dev* PPB_Font_Impl::GetInterface() { | 47 const PPB_Font_Dev* PPB_Font_Impl::GetInterface() { |
| 201 return &ppb_font; | 48 return ::ppapi::thunk::GetPPB_Font_Thunk(); |
| 49 } |
| 50 |
| 51 ::ppapi::thunk::PPB_Font_API* PPB_Font_Impl::AsFont_API() { |
| 52 return this; |
| 202 } | 53 } |
| 203 | 54 |
| 204 PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() { | 55 PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() { |
| 205 return this; | 56 return this; |
| 206 } | 57 } |
| 207 | 58 |
| 208 bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, | 59 PP_Bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, |
| 209 PP_FontMetrics_Dev* metrics) { | 60 PP_FontMetrics_Dev* metrics) { |
| 210 if (description->face.type != PP_VARTYPE_UNDEFINED) | 61 std::string face; |
| 211 return false; | 62 PP_Bool result = PP_FALSE; |
| 63 DoDescribe(NULL, description, &face, metrics, &result); |
| 64 if (!result) |
| 65 return PP_FALSE; |
| 212 | 66 |
| 213 WebFontDescription web_desc = font_->fontDescription(); | 67 // Convert the string. |
| 214 | 68 description->face = StringVar::StringToPPVar(instance()->module(), face); |
| 215 // While converting the other way in PPFontDescToWebFontDesc we validated | 69 return PP_TRUE; |
| 216 // that the enums can be casted. | |
| 217 description->face = StringVar::StringToPPVar(instance()->module(), | |
| 218 UTF16ToUTF8(web_desc.family)); | |
| 219 description->family = static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); | |
| 220 description->size = static_cast<uint32_t>(web_desc.size); | |
| 221 description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); | |
| 222 description->italic = BoolToPPBool(web_desc.italic); | |
| 223 description->small_caps = BoolToPPBool(web_desc.smallCaps); | |
| 224 | |
| 225 metrics->height = font_->height(); | |
| 226 metrics->ascent = font_->ascent(); | |
| 227 metrics->descent = font_->descent(); | |
| 228 metrics->line_spacing = font_->lineSpacing(); | |
| 229 metrics->x_height = static_cast<int32_t>(font_->xHeight()); | |
| 230 | |
| 231 return true; | |
| 232 } | 70 } |
| 233 | 71 |
| 234 bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, | 72 PP_Bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, |
| 235 const PP_TextRun_Dev* text, | 73 const PP_TextRun_Dev* text, |
| 236 const PP_Point* position, | 74 const PP_Point* position, |
| 237 uint32_t color, | 75 uint32_t color, |
| 238 const PP_Rect* clip, | 76 const PP_Rect* clip, |
| 239 bool image_data_is_opaque) { | 77 PP_Bool image_data_is_opaque) { |
| 240 WebTextRun run; | |
| 241 if (!PPTextRunToWebTextRun(text, &run)) | |
| 242 return false; | |
| 243 | |
| 244 // Get and map the image data we're painting to. | 78 // Get and map the image data we're painting to. |
| 245 scoped_refptr<PPB_ImageData_Impl> image_resource( | 79 scoped_refptr<PPB_ImageData_Impl> image_resource( |
| 246 Resource::GetAs<PPB_ImageData_Impl>(image_data)); | 80 Resource::GetAs<PPB_ImageData_Impl>(image_data)); |
| 247 if (!image_resource.get()) | 81 if (!image_resource.get()) |
| 248 return false; | 82 return PP_FALSE; |
| 249 ImageDataAutoMapper mapper(image_resource); | 83 ImageDataAutoMapper mapper(image_resource); |
| 250 if (!mapper.is_valid()) | 84 if (!mapper.is_valid()) |
| 251 return false; | 85 return PP_FALSE; |
| 252 | 86 |
| 253 // Convert position and clip. | 87 TextRun run; |
| 254 WebFloatPoint web_position(static_cast<float>(position->x), | 88 if (!PPTextRunToTextRun(text, &run)) |
| 255 static_cast<float>(position->y)); | 89 return PP_FALSE; |
| 256 WebRect web_clip; | |
| 257 if (!clip) { | |
| 258 // Use entire canvas. | |
| 259 web_clip = WebRect(0, 0, image_resource->width(), image_resource->height()); | |
| 260 } else { | |
| 261 web_clip = WebRect(clip->point.x, clip->point.y, | |
| 262 clip->size.width, clip->size.height); | |
| 263 } | |
| 264 | 90 |
| 265 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), | 91 DoDrawTextAt(NULL, |
| 266 run, web_position, color, web_clip, image_data_is_opaque); | 92 DrawTextParams(image_resource->mapped_canvas(), run, position, |
| 267 return true; | 93 color, clip, image_data_is_opaque)); |
| 94 return PP_TRUE; |
| 268 } | 95 } |
| 269 | 96 |
| 270 int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) { | 97 int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) { |
| 271 WebTextRun run; | 98 int32_t result = -1; |
| 272 if (!PPTextRunToWebTextRun(text, &run)) | 99 TextRun run; |
| 273 return -1; | 100 if (PPTextRunToTextRun(text, &run)) |
| 274 return font_->calculateWidth(run); | 101 DoMeasureText(NULL, run, &result); |
| 102 return result; |
| 275 } | 103 } |
| 276 | 104 |
| 277 uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text, | 105 uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text, |
| 278 int32_t pixel_position) { | 106 int32_t pixel_position) { |
| 279 WebTextRun run; | 107 uint32_t result = -1; |
| 280 if (!PPTextRunToWebTextRun(text, &run)) | 108 TextRun run; |
| 281 return -1; | 109 if (PPTextRunToTextRun(text, &run)) |
| 282 | 110 DoCharacterOffsetForPixel(NULL, run, pixel_position, &result); |
| 283 return static_cast<uint32_t>(font_->offsetForPosition( | 111 return result; |
| 284 run, static_cast<float>(pixel_position))); | |
| 285 } | 112 } |
| 286 | 113 |
| 287 int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text, | 114 int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text, |
| 288 uint32_t char_offset) { | 115 uint32_t char_offset) { |
| 289 WebTextRun run; | 116 int32_t result = -1; |
| 290 if (!PPTextRunToWebTextRun(text, &run)) | 117 TextRun run; |
| 291 return -1; | 118 if (PPTextRunToTextRun(text, &run)) |
| 292 if (char_offset >= run.text.length()) | 119 DoPixelOffsetForCharacter(NULL, run, char_offset, &result); |
| 293 return -1; | 120 return result; |
| 294 | |
| 295 WebFloatRect rect = font_->selectionRectForText( | |
| 296 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); | |
| 297 return static_cast<int>(rect.width); | |
| 298 } | 121 } |
| 299 | 122 |
| 300 } // namespace ppapi | 123 } // namespace ppapi |
| 301 } // namespace webkit | 124 } // namespace webkit |
| 302 | 125 |
| OLD | NEW |