| Index: ppapi/shared_impl/font_impl.cc
|
| ===================================================================
|
| --- ppapi/shared_impl/font_impl.cc (revision 0)
|
| +++ ppapi/shared_impl/font_impl.cc (revision 0)
|
| @@ -0,0 +1,233 @@
|
| +// 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/font_impl.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/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 WebKit::WebFloatPoint;
|
| +using WebKit::WebFloatRect;
|
| +using WebKit::WebFont;
|
| +using WebKit::WebFontDescription;
|
| +using WebKit::WebRect;
|
| +using WebKit::WebTextRun;
|
| +
|
| +namespace pp {
|
| +namespace shared_impl {
|
| +
|
| +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 FontImpl::TextRun& run) {
|
| + return WebTextRun(UTF8ToUTF16(run.text),
|
| + run.rtl != PP_FALSE,
|
| + run.override_direction != PP_FALSE);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +FontImpl::DrawTextParams::DrawTextParams(skia::PlatformCanvas* destination_arg,
|
| + const TextRun& text_arg,
|
| + const PP_Point* position_arg,
|
| + uint32_t color_arg,
|
| + const PP_Rect* clip_arg,
|
| + PP_Bool image_data_is_opaque_arg)
|
| + : destination(destination_arg),
|
| + text(text_arg),
|
| + position(position_arg),
|
| + color(color_arg),
|
| + clip(clip_arg),
|
| + image_data_is_opaque(image_data_is_opaque_arg) {
|
| +}
|
| +
|
| +FontImpl::DrawTextParams::~DrawTextParams() {
|
| +}
|
| +
|
| +FontImpl::FontImpl() {
|
| +}
|
| +
|
| +FontImpl::~FontImpl() {
|
| +}
|
| +
|
| +// static
|
| +bool FontImpl::IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) {
|
| + // Check validity of string. We can't check the actual text since we could
|
| + // be on the wrong thread and don't know if we're in the plugin or the host.
|
| + 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;
|
| +}
|
| +
|
| +void FontImpl::DoCreate(base::WaitableEvent* event,
|
| + 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));
|
| + if (event)
|
| + event->Signal();
|
| +}
|
| +
|
| +void FontImpl::DoDescribe(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::DoDrawTextAt(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::DoMeasureText(base::WaitableEvent* event,
|
| + const TextRun& text, int32_t* result) {
|
| + *result = font_->calculateWidth(TextRunToWebTextRun(text));
|
| + if (event)
|
| + event->Signal();
|
| +}
|
| +
|
| +void FontImpl::DoCharacterOffsetForPixel(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::DoPixelOffsetForCharacter(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 shared_impl
|
| +} // namespace pp
|
|
|
| Property changes on: ppapi/shared_impl/font_impl.cc
|
| ___________________________________________________________________
|
| Added: svn:eol-style
|
| + LF
|
|
|
|
|