OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/plugins/ppapi/webkit_forwarding_impl.h" |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "ppapi/c/dev/ppb_font_dev.h" |
| 11 #include "ppapi/c/pp_point.h" |
| 12 #include "ppapi/c/pp_rect.h" |
| 13 #include "skia/ext/platform_canvas.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" |
| 21 #include "webkit/glue/webkit_glue.h" |
| 22 |
| 23 using pp::shared_impl::WebKitForwarding; |
| 24 using WebKit::WebCanvas; |
| 25 using WebKit::WebFloatPoint; |
| 26 using WebKit::WebFloatRect; |
| 27 using WebKit::WebFont; |
| 28 using WebKit::WebFontDescription; |
| 29 using WebKit::WebRect; |
| 30 using WebKit::WebTextRun; |
| 31 |
| 32 namespace webkit { |
| 33 namespace ppapi { |
| 34 |
| 35 namespace { |
| 36 |
| 37 // The PP_* version lacks "None", so is just one value shifted from the |
| 38 // WebFontDescription version. These values are checked in |
| 39 // PPFontDescToWebFontDesc to make sure the conversion is correct. This is a |
| 40 // macro so it can also be used in the COMPILE_ASSERTS. |
| 41 #define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ |
| 42 static_cast<WebFontDescription::GenericFamily>(f + 1) |
| 43 |
| 44 // Assumes the given PP_FontDescription has been validated. |
| 45 WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font, |
| 46 const std::string& face) { |
| 47 // Verify that the enums match so we can just static cast. |
| 48 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == |
| 49 static_cast<int>(PP_FONTWEIGHT_100), |
| 50 FontWeight100); |
| 51 COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == |
| 52 static_cast<int>(PP_FONTWEIGHT_900), |
| 53 FontWeight900); |
| 54 COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == |
| 55 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), |
| 56 StandardFamily); |
| 57 COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == |
| 58 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), |
| 59 SerifFamily); |
| 60 COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == |
| 61 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), |
| 62 SansSerifFamily); |
| 63 COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == |
| 64 PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), |
| 65 MonospaceFamily); |
| 66 |
| 67 WebFontDescription result; |
| 68 result.family = UTF8ToUTF16(face); |
| 69 result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); |
| 70 result.size = static_cast<float>(font.size); |
| 71 result.italic = font.italic != PP_FALSE; |
| 72 result.smallCaps = font.small_caps != PP_FALSE; |
| 73 result.weight = static_cast<WebFontDescription::Weight>(font.weight); |
| 74 result.letterSpacing = static_cast<short>(font.letter_spacing); |
| 75 result.wordSpacing = static_cast<short>(font.word_spacing); |
| 76 return result; |
| 77 } |
| 78 |
| 79 WebTextRun TextRunToWebTextRun(const WebKitForwarding::Font::TextRun& run) { |
| 80 return WebTextRun(UTF8ToUTF16(run.text), |
| 81 run.rtl != PP_FALSE, |
| 82 run.override_direction != PP_FALSE); |
| 83 } |
| 84 |
| 85 // FontImpl -------------------------------------------------------------------- |
| 86 |
| 87 class FontImpl : public WebKitForwarding::Font { |
| 88 public: |
| 89 FontImpl(const PP_FontDescription_Dev& desc, |
| 90 const std::string& desc_face); |
| 91 virtual ~FontImpl(); |
| 92 |
| 93 virtual void Describe(base::WaitableEvent* event, |
| 94 PP_FontDescription_Dev* description, |
| 95 std::string* face, |
| 96 PP_FontMetrics_Dev* metrics, |
| 97 PP_Bool* result) OVERRIDE; |
| 98 virtual void DrawTextAt(base::WaitableEvent* event, |
| 99 const DrawTextParams& params) OVERRIDE; |
| 100 virtual void MeasureText(base::WaitableEvent* event, |
| 101 const TextRun& text, |
| 102 int32_t* result) OVERRIDE; |
| 103 virtual void CharacterOffsetForPixel(base::WaitableEvent* event, |
| 104 const TextRun& text, |
| 105 int32_t pixel_position, |
| 106 uint32_t* result) OVERRIDE; |
| 107 virtual void PixelOffsetForCharacter(base::WaitableEvent* event, |
| 108 const TextRun& text, |
| 109 uint32_t char_offset, |
| 110 int32_t* result) OVERRIDE; |
| 111 |
| 112 private: |
| 113 scoped_ptr<WebFont> font_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(FontImpl); |
| 116 }; |
| 117 |
| 118 FontImpl::FontImpl(const PP_FontDescription_Dev& desc, |
| 119 const std::string& desc_face) { |
| 120 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc, desc_face); |
| 121 font_.reset(WebFont::create(web_font_desc)); |
| 122 } |
| 123 |
| 124 FontImpl::~FontImpl() { |
| 125 } |
| 126 |
| 127 void FontImpl::Describe(base::WaitableEvent* event, |
| 128 PP_FontDescription_Dev* description, |
| 129 std::string* face, |
| 130 PP_FontMetrics_Dev* metrics, |
| 131 PP_Bool* result) { |
| 132 if (description->face.type != PP_VARTYPE_UNDEFINED) { |
| 133 *result = PP_FALSE; |
| 134 } else { |
| 135 WebFontDescription web_desc = font_->fontDescription(); |
| 136 |
| 137 // While converting the other way in PPFontDescToWebFontDesc we validated |
| 138 // that the enums can be casted. |
| 139 description->face = PP_MakeUndefined(); |
| 140 description->family = |
| 141 static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); |
| 142 description->size = static_cast<uint32_t>(web_desc.size); |
| 143 description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); |
| 144 description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; |
| 145 description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; |
| 146 |
| 147 *face = UTF16ToUTF8(web_desc.family); |
| 148 |
| 149 metrics->height = font_->height(); |
| 150 metrics->ascent = font_->ascent(); |
| 151 metrics->descent = font_->descent(); |
| 152 metrics->line_spacing = font_->lineSpacing(); |
| 153 metrics->x_height = static_cast<int32_t>(font_->xHeight()); |
| 154 |
| 155 *result = PP_TRUE; |
| 156 } |
| 157 if (event) |
| 158 event->Signal(); |
| 159 } |
| 160 |
| 161 void FontImpl::DrawTextAt(base::WaitableEvent* event, |
| 162 const DrawTextParams& params) { |
| 163 WebTextRun run = TextRunToWebTextRun(params.text); |
| 164 |
| 165 // Convert position and clip. |
| 166 WebFloatPoint web_position(static_cast<float>(params.position->x), |
| 167 static_cast<float>(params.position->y)); |
| 168 WebRect web_clip; |
| 169 if (!params.clip) { |
| 170 // Use entire canvas. SkCanvas doesn't have a size on it, so we just use |
| 171 // the current clip bounds. |
| 172 SkRect skclip; |
| 173 params.destination->getClipBounds(&skclip); |
| 174 web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, |
| 175 skclip.fBottom - skclip.fTop); |
| 176 } else { |
| 177 web_clip = WebRect(params.clip->point.x, params.clip->point.y, |
| 178 params.clip->size.width, params.clip->size.height); |
| 179 } |
| 180 |
| 181 font_->drawText(webkit_glue::ToWebCanvas(params.destination), run, |
| 182 web_position, params.color, web_clip, |
| 183 params.image_data_is_opaque == PP_TRUE); |
| 184 if (event) |
| 185 event->Signal(); |
| 186 } |
| 187 |
| 188 void FontImpl::MeasureText(base::WaitableEvent* event, |
| 189 const TextRun& text, int32_t* result) { |
| 190 *result = font_->calculateWidth(TextRunToWebTextRun(text)); |
| 191 if (event) |
| 192 event->Signal(); |
| 193 } |
| 194 |
| 195 void FontImpl::CharacterOffsetForPixel(base::WaitableEvent* event, |
| 196 const TextRun& text, |
| 197 int32_t pixel_position, |
| 198 uint32_t* result) { |
| 199 *result = static_cast<uint32_t>(font_->offsetForPosition( |
| 200 TextRunToWebTextRun(text), static_cast<float>(pixel_position))); |
| 201 if (event) |
| 202 event->Signal(); |
| 203 } |
| 204 |
| 205 void FontImpl::PixelOffsetForCharacter(base::WaitableEvent* event, |
| 206 const TextRun& text, |
| 207 uint32_t char_offset, |
| 208 int32_t* result) { |
| 209 WebTextRun run = TextRunToWebTextRun(text); |
| 210 if (char_offset >= run.text.length()) { |
| 211 *result = -1; |
| 212 } else { |
| 213 WebFloatRect rect = font_->selectionRectForText( |
| 214 run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); |
| 215 *result = static_cast<int>(rect.width); |
| 216 } |
| 217 if (event) |
| 218 event->Signal(); |
| 219 } |
| 220 |
| 221 } // namespace |
| 222 |
| 223 // WebKitForwardingImpl -------------------------------------------------------- |
| 224 |
| 225 WebKitForwardingImpl::WebKitForwardingImpl() { |
| 226 } |
| 227 |
| 228 WebKitForwardingImpl::~WebKitForwardingImpl() { |
| 229 } |
| 230 |
| 231 void WebKitForwardingImpl::CreateFontForwarding( |
| 232 base::WaitableEvent* event, |
| 233 const PP_FontDescription_Dev& desc, |
| 234 const std::string& desc_face, |
| 235 Font** result) { |
| 236 *result = new FontImpl(desc, desc_face); |
| 237 if (event) |
| 238 event->Signal(); |
| 239 } |
| 240 |
| 241 } // namespace ppapi |
| 242 } // namespace webkit |
OLD | NEW |