diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-10 21:17:48 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-10 21:17:48 +0000 |
commit | 7a1f7c6f6c982287b3f6bb2acded619f3824416c (patch) | |
tree | 82ff404c4bcf84520c21ea7f0526ad5a40661641 /webkit | |
parent | bafaee12825a06890f114a282880e135a8b0b1ae (diff) | |
download | chromium_src-7a1f7c6f6c982287b3f6bb2acded619f3824416c.zip chromium_src-7a1f7c6f6c982287b3f6bb2acded619f3824416c.tar.gz chromium_src-7a1f7c6f6c982287b3f6bb2acded619f3824416c.tar.bz2 |
Make the Pepper proxy support in-process font rendering.
This implements a WebKit thread in the PPAPI plugin process so we can do the
font calls without IPC. The existing font support was refactored into
a virtual class (to prevent PPAPI from depending on WebKit and creating a
circular GYP dependency).
This moves the renderer sandbox support into content/common so that it can
be used by the PPAPI process.
Review URL: http://codereview.chromium.org/6981001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84856 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/glue/gl_bindings_skia_cmd_buffer.cc | 2 | ||||
-rw-r--r-- | webkit/glue/webkit_glue.gypi | 6 | ||||
-rw-r--r-- | webkit/plugins/ppapi/plugin_module.cc | 7 | ||||
-rw-r--r-- | webkit/plugins/ppapi/plugin_module.h | 16 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_font_impl.cc | 297 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_font_impl.h | 34 | ||||
-rw-r--r-- | webkit/plugins/ppapi/resource_creation_impl.cc | 16 | ||||
-rw-r--r-- | webkit/plugins/ppapi/resource_creation_impl.h | 3 | ||||
-rw-r--r-- | webkit/plugins/ppapi/webkit_forwarding_impl.cc | 242 | ||||
-rw-r--r-- | webkit/plugins/ppapi/webkit_forwarding_impl.h | 24 |
10 files changed, 397 insertions, 250 deletions
diff --git a/webkit/glue/gl_bindings_skia_cmd_buffer.cc b/webkit/glue/gl_bindings_skia_cmd_buffer.cc index c7f8d4f..39653e2 100644 --- a/webkit/glue/gl_bindings_skia_cmd_buffer.cc +++ b/webkit/glue/gl_bindings_skia_cmd_buffer.cc @@ -5,7 +5,9 @@ #include "webkit/glue/gl_bindings_skia_cmd_buffer.h" +#ifndef GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES +#endif #include "gpu/GLES2/gl2.h" #include "gpu/GLES2/gl2ext.h" diff --git a/webkit/glue/webkit_glue.gypi b/webkit/glue/webkit_glue.gypi index 23b9634..53a6912 100644 --- a/webkit/glue/webkit_glue.gypi +++ b/webkit/glue/webkit_glue.gypi @@ -109,13 +109,13 @@ '<(DEPTH)/base/base.gyp:base_i18n', '<(DEPTH)/gpu/gpu.gyp:gles2_implementation', '<(DEPTH)/net/net.gyp:net', - '<(DEPTH)/ppapi/ppapi.gyp:ppapi_shared_impl', + '<(DEPTH)/ppapi/ppapi.gyp:ppapi_c', + '<(DEPTH)/ppapi/ppapi_internal.gyp:ppapi_shared_impl', '<(DEPTH)/printing/printing.gyp:printing', '<(DEPTH)/skia/skia.gyp:skia', '<(DEPTH)/third_party/icu/icu.gyp:icui18n', '<(DEPTH)/third_party/icu/icu.gyp:icuuc', '<(DEPTH)/third_party/npapi/npapi.gyp:npapi', - '<(DEPTH)/ppapi/ppapi.gyp:ppapi_c', 'webkit_resources', 'webkit_strings', 'webkit_user_agent', @@ -314,6 +314,8 @@ '../plugins/ppapi/string.h', '../plugins/ppapi/var.cc', '../plugins/ppapi/var.h', + '../plugins/ppapi/webkit_forwarding_impl.cc', + '../plugins/ppapi/webkit_forwarding_impl.h', '../plugins/sad_plugin.cc', '../plugins/sad_plugin.h', 'media/audio_decoder.cc', diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index f22ca92..6d4543f 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -105,6 +105,7 @@ #include "webkit/plugins/ppapi/ppb_widget_impl.h" #include "webkit/plugins/ppapi/resource_tracker.h" #include "webkit/plugins/ppapi/var.h" +#include "webkit/plugins/ppapi/webkit_forwarding_impl.h" #ifdef ENABLE_GPU #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" @@ -562,6 +563,12 @@ PluginDelegate::PpapiBroker* PluginModule::GetBroker(){ return broker_; } +pp::shared_impl::WebKitForwarding* PluginModule::GetWebKitForwarding() { + if (!webkit_forwarding_.get()) + webkit_forwarding_.reset(new WebKitForwardingImpl); + return webkit_forwarding_.get(); +} + bool PluginModule::InitializeModule() { DCHECK(!out_of_process_proxy_.get()) << "Don't call for proxied modules."; int retval = entry_points_.initialize_module(pp_module(), &GetInterface); diff --git a/webkit/plugins/ppapi/plugin_module.h b/webkit/plugins/ppapi/plugin_module.h index 1ade5a6..d1fa058 100644 --- a/webkit/plugins/ppapi/plugin_module.h +++ b/webkit/plugins/ppapi/plugin_module.h @@ -32,10 +32,16 @@ class WaitableEvent; } namespace pp { + namespace proxy { class HostDispatcher; -} // proxy -} // pp +} // namespace proxy + +namespace shared_impl { +class WebKitForwarding; +} // namespace shared_impl + +} // namespace pp namespace IPC { struct ChannelHandle; @@ -149,6 +155,9 @@ class PluginModule : public base::RefCounted<PluginModule>, void SetBroker(PluginDelegate::PpapiBroker* broker); PluginDelegate::PpapiBroker* GetBroker(); + // Retrieves the forwarding interface used for talking to WebKit. + pp::shared_impl::WebKitForwarding* GetWebKitForwarding(); + private: // Calls the InitializeModule entrypoint. The entrypoint must have been // set and the plugin must not be out of process (we don't maintain @@ -197,6 +206,9 @@ class PluginModule : public base::RefCounted<PluginModule>, PP_Bool (*reserve_instance_id_)(PP_Module, PP_Instance); + // Lazily created by GetWebKitForwarding. + scoped_ptr<pp::shared_impl::WebKitForwarding> webkit_forwarding_; + DISALLOW_COPY_AND_ASSIGN(PluginModule); }; diff --git a/webkit/plugins/ppapi/ppb_font_impl.cc b/webkit/plugins/ppapi/ppb_font_impl.cc index b0a99ada..02fd5a9 100644 --- a/webkit/plugins/ppapi/ppb_font_impl.cc +++ b/webkit/plugins/ppapi/ppb_font_impl.cc @@ -1,196 +1,50 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "webkit/plugins/ppapi/ppb_font_impl.h" -#include "base/logging.h" -#include "base/utf_string_conversions.h" #include "ppapi/c/dev/ppb_font_dev.h" -#include "ppapi/c/pp_rect.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" +#include "ppapi/thunk/thunk.h" #include "webkit/plugins/ppapi/common.h" +#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" #include "webkit/plugins/ppapi/string.h" #include "webkit/plugins/ppapi/var.h" -#include "webkit/glue/webkit_glue.h" -using WebKit::WebFloatPoint; -using WebKit::WebFloatRect; -using WebKit::WebFont; -using WebKit::WebFontDescription; -using WebKit::WebRect; -using WebKit::WebTextRun; +using pp::shared_impl::WebKitForwarding; namespace webkit { namespace ppapi { namespace { -bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { - // Check validity of UTF-8. - if (desc.face.type != PP_VARTYPE_STRING && - desc.face.type != PP_VARTYPE_UNDEFINED) - return false; - - // Check enum ranges. - if (static_cast<int>(desc.family) < PP_FONTFAMILY_DEFAULT || - static_cast<int>(desc.family) > PP_FONTFAMILY_MONOSPACE) - return false; - if (static_cast<int>(desc.weight) < PP_FONTWEIGHT_100 || - static_cast<int>(desc.weight) > PP_FONTWEIGHT_900) - return false; - - // Check for excessive sizes which may cause layout to get confused. - if (desc.size > 200) - return false; - - return true; -} - -// The PP_* version lacks "None", so is just one value shifted from the -// WebFontDescription version. These values are checked in -// PPFontDescToWebFontDesc to make sure the conversion is correct. This is a -// macro so it can also be used in the COMPILE_ASSERTS. -#define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ - static_cast<WebFontDescription::GenericFamily>(f + 1) - -// Assumes the given PP_FontDescription has been validated. -WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font) { - // Verify that the enums match so we can just static cast. - COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == - static_cast<int>(PP_FONTWEIGHT_100), - FontWeight100); - COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == - static_cast<int>(PP_FONTWEIGHT_900), - FontWeight900); - COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == - PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), - StandardFamily); - COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == - PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), - SerifFamily); - COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == - PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), - SansSerifFamily); - COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == - PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), - MonospaceFamily); - - WebFontDescription result; - scoped_refptr<StringVar> face_name(StringVar::FromPPVar(font.face)); - if (face_name) - result.family = UTF8ToUTF16(face_name->value()); - result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); - result.size = static_cast<float>(font.size); - result.italic = PPBoolToBool(font.italic); - result.smallCaps = PPBoolToBool(font.small_caps); - result.weight = static_cast<WebFontDescription::Weight>(font.weight); - result.letterSpacing = static_cast<short>(font.letter_spacing); - result.wordSpacing = static_cast<short>(font.word_spacing); - return result; -} - -// Converts the given PP_TextRun to a WebTextRun, returning true on success. +// Converts the given PP_TextRun to a TextRun, returning true on success. // False means the input was invalid. -bool PPTextRunToWebTextRun(const PP_TextRun_Dev* run, WebTextRun* output) { +bool PPTextRunToTextRun(const PP_TextRun_Dev* run, + WebKitForwarding::Font::TextRun* output) { scoped_refptr<StringVar> text_string(StringVar::FromPPVar(run->text)); if (!text_string) return false; - *output = WebTextRun(UTF8ToUTF16(text_string->value()), - PPBoolToBool(run->rtl), - PPBoolToBool(run->override_direction)); - return true; -} - -PP_Resource Create(PP_Instance instance_id, - const PP_FontDescription_Dev* description) { - PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); - if (!instance) - return 0; - - if (!IsPPFontDescriptionValid(*description)) - return 0; - - scoped_refptr<PPB_Font_Impl> font(new PPB_Font_Impl(instance, *description)); - return font->GetReference(); -} - -PP_Bool IsFont(PP_Resource resource) { - return BoolToPPBool(!!Resource::GetAs<PPB_Font_Impl>(resource).get()); -} - -PP_Bool Describe(PP_Resource font_id, - PP_FontDescription_Dev* description, - PP_FontMetrics_Dev* metrics) { - scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); - if (!font.get()) - return PP_FALSE; - return BoolToPPBool(font->Describe(description, metrics)); -} - -PP_Bool DrawTextAt(PP_Resource font_id, - PP_Resource image_data, - const PP_TextRun_Dev* text, - const PP_Point* position, - uint32_t color, - const PP_Rect* clip, - PP_Bool image_data_is_opaque) { - scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); - if (!font.get()) - return PP_FALSE; - return BoolToPPBool(font->DrawTextAt(image_data, text, position, color, clip, - PPBoolToBool(image_data_is_opaque))); -} - -int32_t MeasureText(PP_Resource font_id, const PP_TextRun_Dev* text) { - scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); - if (!font.get()) - return -1; - return font->MeasureText(text); -} -uint32_t CharacterOffsetForPixel(PP_Resource font_id, - const PP_TextRun_Dev* text, - int32_t pixel_position) { - scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); - if (!font.get()) - return -1; - return font->CharacterOffsetForPixel(text, pixel_position); -} - -int32_t PixelOffsetForCharacter(PP_Resource font_id, - const PP_TextRun_Dev* text, - uint32_t char_offset) { - scoped_refptr<PPB_Font_Impl> font(Resource::GetAs<PPB_Font_Impl>(font_id)); - if (!font.get()) - return false; - return font->PixelOffsetForCharacter(text, char_offset); + output->text = text_string->value(); + output->rtl = PPBoolToBool(run->rtl); + output->override_direction = PPBoolToBool(run->override_direction); + return true; } -const PPB_Font_Dev ppb_font = { - &Create, - &IsFont, - &Describe, - &DrawTextAt, - &MeasureText, - &CharacterOffsetForPixel, - &PixelOffsetForCharacter -}; - } // namespace PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance, const PP_FontDescription_Dev& desc) : Resource(instance) { - WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); - font_.reset(WebFont::create(web_font_desc)); + scoped_refptr<StringVar> face_name(StringVar::FromPPVar(desc.face)); + + WebKitForwarding::Font* result = NULL; + instance->module()->GetWebKitForwarding()->CreateFontForwarding( + NULL, desc, face_name ? face_name->value() : std::string(), &result); + font_forwarding_.reset(result); } PPB_Font_Impl::~PPB_Font_Impl() { @@ -198,103 +52,82 @@ PPB_Font_Impl::~PPB_Font_Impl() { // static const PPB_Font_Dev* PPB_Font_Impl::GetInterface() { - return &ppb_font; + return ::ppapi::thunk::GetPPB_Font_Thunk(); } -PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() { +::ppapi::thunk::PPB_Font_API* PPB_Font_Impl::AsFont_API() { return this; } -bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, - PP_FontMetrics_Dev* metrics) { - if (description->face.type != PP_VARTYPE_UNDEFINED) - return false; - - WebFontDescription web_desc = font_->fontDescription(); - - // While converting the other way in PPFontDescToWebFontDesc we validated - // that the enums can be casted. - description->face = StringVar::StringToPPVar(instance()->module(), - UTF16ToUTF8(web_desc.family)); - description->family = static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); - description->size = static_cast<uint32_t>(web_desc.size); - description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); - description->italic = BoolToPPBool(web_desc.italic); - description->small_caps = BoolToPPBool(web_desc.smallCaps); +PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() { + return this; +} - metrics->height = font_->height(); - metrics->ascent = font_->ascent(); - metrics->descent = font_->descent(); - metrics->line_spacing = font_->lineSpacing(); - metrics->x_height = static_cast<int32_t>(font_->xHeight()); +PP_Bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, + PP_FontMetrics_Dev* metrics) { + std::string face; + PP_Bool result = PP_FALSE; + font_forwarding_->Describe(NULL, description, &face, metrics, &result); + if (!result) + return PP_FALSE; - return true; + // Convert the string. + description->face = StringVar::StringToPPVar(instance()->module(), face); + return PP_TRUE; } -bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, - const PP_TextRun_Dev* text, - const PP_Point* position, - uint32_t color, - const PP_Rect* clip, - bool image_data_is_opaque) { - WebTextRun run; - if (!PPTextRunToWebTextRun(text, &run)) - return false; - +PP_Bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, + const PP_TextRun_Dev* text, + const PP_Point* position, + uint32_t color, + const PP_Rect* clip, + PP_Bool image_data_is_opaque) { // Get and map the image data we're painting to. scoped_refptr<PPB_ImageData_Impl> image_resource( Resource::GetAs<PPB_ImageData_Impl>(image_data)); if (!image_resource.get()) - return false; + return PP_FALSE; ImageDataAutoMapper mapper(image_resource); if (!mapper.is_valid()) - return false; + return PP_FALSE; - // Convert position and clip. - WebFloatPoint web_position(static_cast<float>(position->x), - static_cast<float>(position->y)); - WebRect web_clip; - if (!clip) { - // Use entire canvas. - web_clip = WebRect(0, 0, image_resource->width(), image_resource->height()); - } else { - web_clip = WebRect(clip->point.x, clip->point.y, - clip->size.width, clip->size.height); - } + WebKitForwarding::Font::TextRun run; + if (!PPTextRunToTextRun(text, &run)) + return PP_FALSE; - font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), - run, web_position, color, web_clip, image_data_is_opaque); - return true; + font_forwarding_->DrawTextAt(NULL, WebKitForwarding::Font::DrawTextParams( + image_resource->mapped_canvas(), run, position, + color, clip, image_data_is_opaque)); + return PP_TRUE; } int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) { - WebTextRun run; - if (!PPTextRunToWebTextRun(text, &run)) - return -1; - return font_->calculateWidth(run); + int32_t result = -1; + WebKitForwarding::Font::TextRun run; + if (PPTextRunToTextRun(text, &run)) + font_forwarding_->MeasureText(NULL, run, &result); + return result; } uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text, int32_t pixel_position) { - WebTextRun run; - if (!PPTextRunToWebTextRun(text, &run)) - return -1; - - return static_cast<uint32_t>(font_->offsetForPosition( - run, static_cast<float>(pixel_position))); + uint32_t result = -1; + WebKitForwarding::Font::TextRun run; + if (PPTextRunToTextRun(text, &run)) { + font_forwarding_->CharacterOffsetForPixel(NULL, run, pixel_position, + &result); + } + return result; } int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text, uint32_t char_offset) { - WebTextRun run; - if (!PPTextRunToWebTextRun(text, &run)) - return -1; - if (char_offset >= run.text.length()) - return -1; - - WebFloatRect rect = font_->selectionRectForText( - run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); - return static_cast<int>(rect.width); + int32_t result = -1; + WebKitForwarding::Font::TextRun run; + if (PPTextRunToTextRun(text, &run)) { + font_forwarding_->PixelOffsetForCharacter(NULL, run, char_offset, &result); + } + return result; } } // namespace ppapi diff --git a/webkit/plugins/ppapi/ppb_font_impl.h b/webkit/plugins/ppapi/ppb_font_impl.h index 368b935..c951327 100644 --- a/webkit/plugins/ppapi/ppb_font_impl.h +++ b/webkit/plugins/ppapi/ppb_font_impl.h @@ -8,6 +8,8 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "ppapi/c/dev/ppb_font_dev.h" +#include "ppapi/shared_impl/webkit_forwarding.h" +#include "ppapi/thunk/ppb_font_api.h" #include "webkit/plugins/ppapi/resource.h" namespace WebKit { @@ -19,7 +21,8 @@ namespace ppapi { class PluginInstance; -class PPB_Font_Impl : public Resource { +class PPB_Font_Impl : public Resource, + public ::ppapi::thunk::PPB_Font_API { public: PPB_Font_Impl(PluginInstance* instance, const PP_FontDescription_Dev& desc); virtual ~PPB_Font_Impl(); @@ -28,26 +31,29 @@ class PPB_Font_Impl : public Resource { // the plugin. static const PPB_Font_Dev* GetInterface(); + // ResourceObjectBase. + virtual ::ppapi::thunk::PPB_Font_API* AsFont_API() OVERRIDE; + // Resource overrides. - virtual PPB_Font_Impl* AsPPB_Font_Impl(); + virtual PPB_Font_Impl* AsPPB_Font_Impl() OVERRIDE; // PPB_Font implementation. - bool Describe(PP_FontDescription_Dev* description, - PP_FontMetrics_Dev* metrics); - bool DrawTextAt(PP_Resource image_data, - const PP_TextRun_Dev* text, - const PP_Point* position, - uint32_t color, - const PP_Rect* clip, - bool image_data_is_opaque); - int32_t MeasureText(const PP_TextRun_Dev* text); + PP_Bool Describe(PP_FontDescription_Dev* description, + PP_FontMetrics_Dev* metrics) OVERRIDE; + PP_Bool DrawTextAt(PP_Resource image_data, + const PP_TextRun_Dev* text, + const PP_Point* position, + uint32_t color, + const PP_Rect* clip, + PP_Bool image_data_is_opaque) OVERRIDE; + int32_t MeasureText(const PP_TextRun_Dev* text) OVERRIDE; uint32_t CharacterOffsetForPixel(const PP_TextRun_Dev* text, - int32_t pixel_position); + int32_t pixel_position) OVERRIDE; int32_t PixelOffsetForCharacter(const PP_TextRun_Dev* text, - uint32_t char_offset); + uint32_t char_offset) OVERRIDE; private: - scoped_ptr<WebKit::WebFont> font_; + scoped_ptr<pp::shared_impl::WebKitForwarding::Font> font_forwarding_; DISALLOW_COPY_AND_ASSIGN(PPB_Font_Impl); }; diff --git a/webkit/plugins/ppapi/resource_creation_impl.cc b/webkit/plugins/ppapi/resource_creation_impl.cc index 674aee1..6308cc2 100644 --- a/webkit/plugins/ppapi/resource_creation_impl.cc +++ b/webkit/plugins/ppapi/resource_creation_impl.cc @@ -5,7 +5,9 @@ #include "webkit/plugins/ppapi/resource_creation_impl.h" #include "ppapi/c/pp_size.h" +#include "ppapi/shared_impl/font_impl.h" #include "webkit/plugins/ppapi/common.h" +#include "webkit/plugins/ppapi/ppb_font_impl.h" #include "webkit/plugins/ppapi/ppb_graphics_2d_impl.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" @@ -23,6 +25,20 @@ ResourceCreationImpl::AsResourceCreation() { return this; } +PP_Resource ResourceCreationImpl::CreateFontObject( + PP_Instance pp_instance, + const PP_FontDescription_Dev* description) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); + if (!instance) + return 0; + + if (!pp::shared_impl::FontImpl::IsPPFontDescriptionValid(*description)) + return 0; + + scoped_refptr<PPB_Font_Impl> font(new PPB_Font_Impl(instance, *description)); + return font->GetReference(); +} + PP_Resource ResourceCreationImpl::CreateGraphics2D( PP_Instance pp_instance, const PP_Size& size, diff --git a/webkit/plugins/ppapi/resource_creation_impl.h b/webkit/plugins/ppapi/resource_creation_impl.h index 4d89715..2e31928 100644 --- a/webkit/plugins/ppapi/resource_creation_impl.h +++ b/webkit/plugins/ppapi/resource_creation_impl.h @@ -22,6 +22,9 @@ class ResourceCreationImpl : public ::ppapi::shared_impl::FunctionGroupBase, virtual ::ppapi::thunk::ResourceCreationAPI* AsResourceCreation(); // ResourceCreationAPI implementation. + virtual PP_Resource CreateFontObject( + PP_Instance instance, + const PP_FontDescription_Dev* description); virtual PP_Resource CreateGraphics2D(PP_Instance pp_instance, const PP_Size& size, PP_Bool is_always_opaque); diff --git a/webkit/plugins/ppapi/webkit_forwarding_impl.cc b/webkit/plugins/ppapi/webkit_forwarding_impl.cc new file mode 100644 index 0000000..8c79e77 --- /dev/null +++ b/webkit/plugins/ppapi/webkit_forwarding_impl.cc @@ -0,0 +1,242 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/plugins/ppapi/webkit_forwarding_impl.h" + +#include "base/scoped_ptr.h" +#include "base/synchronization/waitable_event.h" +#include "base/utf_string_conversions.h" +#include "ppapi/c/dev/ppb_font_dev.h" +#include "ppapi/c/pp_point.h" +#include "ppapi/c/pp_rect.h" +#include "skia/ext/platform_canvas.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFont.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFontDescription.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatPoint.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFloatRect.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebTextRun.h" +#include "webkit/glue/webkit_glue.h" + +using pp::shared_impl::WebKitForwarding; +using WebKit::WebCanvas; +using WebKit::WebFloatPoint; +using WebKit::WebFloatRect; +using WebKit::WebFont; +using WebKit::WebFontDescription; +using WebKit::WebRect; +using WebKit::WebTextRun; + +namespace webkit { +namespace ppapi { + +namespace { + +// The PP_* version lacks "None", so is just one value shifted from the +// WebFontDescription version. These values are checked in +// PPFontDescToWebFontDesc to make sure the conversion is correct. This is a +// macro so it can also be used in the COMPILE_ASSERTS. +#define PP_FONTFAMILY_TO_WEB_FONTFAMILY(f) \ + static_cast<WebFontDescription::GenericFamily>(f + 1) + +// Assumes the given PP_FontDescription has been validated. +WebFontDescription PPFontDescToWebFontDesc(const PP_FontDescription_Dev& font, + const std::string& face) { + // Verify that the enums match so we can just static cast. + COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight100) == + static_cast<int>(PP_FONTWEIGHT_100), + FontWeight100); + COMPILE_ASSERT(static_cast<int>(WebFontDescription::Weight900) == + static_cast<int>(PP_FONTWEIGHT_900), + FontWeight900); + COMPILE_ASSERT(WebFontDescription::GenericFamilyStandard == + PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_DEFAULT), + StandardFamily); + COMPILE_ASSERT(WebFontDescription::GenericFamilySerif == + PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SERIF), + SerifFamily); + COMPILE_ASSERT(WebFontDescription::GenericFamilySansSerif == + PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_SANSSERIF), + SansSerifFamily); + COMPILE_ASSERT(WebFontDescription::GenericFamilyMonospace == + PP_FONTFAMILY_TO_WEB_FONTFAMILY(PP_FONTFAMILY_MONOSPACE), + MonospaceFamily); + + WebFontDescription result; + result.family = UTF8ToUTF16(face); + result.genericFamily = PP_FONTFAMILY_TO_WEB_FONTFAMILY(font.family); + result.size = static_cast<float>(font.size); + result.italic = font.italic != PP_FALSE; + result.smallCaps = font.small_caps != PP_FALSE; + result.weight = static_cast<WebFontDescription::Weight>(font.weight); + result.letterSpacing = static_cast<short>(font.letter_spacing); + result.wordSpacing = static_cast<short>(font.word_spacing); + return result; +} + +WebTextRun TextRunToWebTextRun(const WebKitForwarding::Font::TextRun& run) { + return WebTextRun(UTF8ToUTF16(run.text), + run.rtl != PP_FALSE, + run.override_direction != PP_FALSE); +} + +// FontImpl -------------------------------------------------------------------- + +class FontImpl : public WebKitForwarding::Font { + public: + FontImpl(const PP_FontDescription_Dev& desc, + const std::string& desc_face); + virtual ~FontImpl(); + + virtual void Describe(base::WaitableEvent* event, + PP_FontDescription_Dev* description, + std::string* face, + PP_FontMetrics_Dev* metrics, + PP_Bool* result) OVERRIDE; + virtual void DrawTextAt(base::WaitableEvent* event, + const DrawTextParams& params) OVERRIDE; + virtual void MeasureText(base::WaitableEvent* event, + const TextRun& text, + int32_t* result) OVERRIDE; + virtual void CharacterOffsetForPixel(base::WaitableEvent* event, + const TextRun& text, + int32_t pixel_position, + uint32_t* result) OVERRIDE; + virtual void PixelOffsetForCharacter(base::WaitableEvent* event, + const TextRun& text, + uint32_t char_offset, + int32_t* result) OVERRIDE; + + private: + scoped_ptr<WebFont> font_; + + DISALLOW_COPY_AND_ASSIGN(FontImpl); +}; + +FontImpl::FontImpl(const PP_FontDescription_Dev& desc, + const std::string& desc_face) { + WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc, desc_face); + font_.reset(WebFont::create(web_font_desc)); +} + +FontImpl::~FontImpl() { +} + +void FontImpl::Describe(base::WaitableEvent* event, + PP_FontDescription_Dev* description, + std::string* face, + PP_FontMetrics_Dev* metrics, + PP_Bool* result) { + if (description->face.type != PP_VARTYPE_UNDEFINED) { + *result = PP_FALSE; + } else { + WebFontDescription web_desc = font_->fontDescription(); + + // While converting the other way in PPFontDescToWebFontDesc we validated + // that the enums can be casted. + description->face = PP_MakeUndefined(); + description->family = + static_cast<PP_FontFamily_Dev>(web_desc.genericFamily); + description->size = static_cast<uint32_t>(web_desc.size); + description->weight = static_cast<PP_FontWeight_Dev>(web_desc.weight); + description->italic = web_desc.italic ? PP_TRUE : PP_FALSE; + description->small_caps = web_desc.smallCaps ? PP_TRUE : PP_FALSE; + + *face = UTF16ToUTF8(web_desc.family); + + metrics->height = font_->height(); + metrics->ascent = font_->ascent(); + metrics->descent = font_->descent(); + metrics->line_spacing = font_->lineSpacing(); + metrics->x_height = static_cast<int32_t>(font_->xHeight()); + + *result = PP_TRUE; + } + if (event) + event->Signal(); +} + +void FontImpl::DrawTextAt(base::WaitableEvent* event, + const DrawTextParams& params) { + WebTextRun run = TextRunToWebTextRun(params.text); + + // Convert position and clip. + WebFloatPoint web_position(static_cast<float>(params.position->x), + static_cast<float>(params.position->y)); + WebRect web_clip; + if (!params.clip) { + // Use entire canvas. SkCanvas doesn't have a size on it, so we just use + // the current clip bounds. + SkRect skclip; + params.destination->getClipBounds(&skclip); + web_clip = WebRect(skclip.fLeft, skclip.fTop, skclip.fRight - skclip.fLeft, + skclip.fBottom - skclip.fTop); + } else { + web_clip = WebRect(params.clip->point.x, params.clip->point.y, + params.clip->size.width, params.clip->size.height); + } + + font_->drawText(webkit_glue::ToWebCanvas(params.destination), run, + web_position, params.color, web_clip, + params.image_data_is_opaque == PP_TRUE); + if (event) + event->Signal(); +} + +void FontImpl::MeasureText(base::WaitableEvent* event, + const TextRun& text, int32_t* result) { + *result = font_->calculateWidth(TextRunToWebTextRun(text)); + if (event) + event->Signal(); +} + +void FontImpl::CharacterOffsetForPixel(base::WaitableEvent* event, + const TextRun& text, + int32_t pixel_position, + uint32_t* result) { + *result = static_cast<uint32_t>(font_->offsetForPosition( + TextRunToWebTextRun(text), static_cast<float>(pixel_position))); + if (event) + event->Signal(); +} + +void FontImpl::PixelOffsetForCharacter(base::WaitableEvent* event, + const TextRun& text, + uint32_t char_offset, + int32_t* result) { + WebTextRun run = TextRunToWebTextRun(text); + if (char_offset >= run.text.length()) { + *result = -1; + } else { + WebFloatRect rect = font_->selectionRectForText( + run, WebFloatPoint(0.0f, 0.0f), font_->height(), 0, char_offset); + *result = static_cast<int>(rect.width); + } + if (event) + event->Signal(); +} + +} // namespace + +// WebKitForwardingImpl -------------------------------------------------------- + +WebKitForwardingImpl::WebKitForwardingImpl() { +} + +WebKitForwardingImpl::~WebKitForwardingImpl() { +} + +void WebKitForwardingImpl::CreateFontForwarding( + base::WaitableEvent* event, + const PP_FontDescription_Dev& desc, + const std::string& desc_face, + Font** result) { + *result = new FontImpl(desc, desc_face); + if (event) + event->Signal(); +} + +} // namespace ppapi +} // namespace webkit diff --git a/webkit/plugins/ppapi/webkit_forwarding_impl.h b/webkit/plugins/ppapi/webkit_forwarding_impl.h new file mode 100644 index 0000000..3914495 --- /dev/null +++ b/webkit/plugins/ppapi/webkit_forwarding_impl.h @@ -0,0 +1,24 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/shared_impl/webkit_forwarding.h" + +#include "base/compiler_specific.h" + +namespace webkit { +namespace ppapi { + +class WebKitForwardingImpl : public pp::shared_impl::WebKitForwarding { + public: + WebKitForwardingImpl(); + virtual ~WebKitForwardingImpl(); + + virtual void CreateFontForwarding(base::WaitableEvent* event, + const PP_FontDescription_Dev& desc, + const std::string& desc_face, + Font** result) OVERRIDE; +}; + +} // namespace ppapi +} // namespace webkit |