Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: webkit/plugins/ppapi/ppb_font_impl.cc

Issue 6981001: Make the Pepper proxy support in-process font rendering. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/plugins/ppapi/ppb_font_impl.h ('k') | webkit/plugins/ppapi/resource_creation_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
10 #include "webkit/plugins/ppapi/plugin_module.h"
18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 11 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
19 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" 12 #include "webkit/plugins/ppapi/ppb_image_data_impl.h"
20 #include "webkit/plugins/ppapi/string.h" 13 #include "webkit/plugins/ppapi/string.h"
21 #include "webkit/plugins/ppapi/var.h" 14 #include "webkit/plugins/ppapi/var.h"
22 #include "webkit/glue/webkit_glue.h"
23 15
24 using WebKit::WebFloatPoint; 16 using pp::shared_impl::WebKitForwarding;
25 using WebKit::WebFloatRect;
26 using WebKit::WebFont;
27 using WebKit::WebFontDescription;
28 using WebKit::WebRect;
29 using WebKit::WebTextRun;
30 17
31 namespace webkit { 18 namespace webkit {
32 namespace ppapi { 19 namespace ppapi {
33 20
34 namespace { 21 namespace {
35 22
36 bool IsPPFontDescriptionValid(const PP_FontDescription_Dev& desc) { 23 // 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. 24 // False means the input was invalid.
102 bool PPTextRunToWebTextRun(const PP_TextRun_Dev* run, WebTextRun* output) { 25 bool PPTextRunToTextRun(const PP_TextRun_Dev* run,
26 WebKitForwarding::Font::TextRun* output) {
103 scoped_refptr<StringVar> text_string(StringVar::FromPPVar(run->text)); 27 scoped_refptr<StringVar> text_string(StringVar::FromPPVar(run->text));
104 if (!text_string) 28 if (!text_string)
105 return false; 29 return false;
106 *output = WebTextRun(UTF8ToUTF16(text_string->value()), 30
107 PPBoolToBool(run->rtl), 31 output->text = text_string->value();
108 PPBoolToBool(run->override_direction)); 32 output->rtl = PPBoolToBool(run->rtl);
33 output->override_direction = PPBoolToBool(run->override_direction);
109 return true; 34 return true;
110 } 35 }
111 36
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 37 } // namespace
188 38
189 PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance, 39 PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance,
190 const PP_FontDescription_Dev& desc) 40 const PP_FontDescription_Dev& desc)
191 : Resource(instance) { 41 : Resource(instance) {
192 WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); 42 scoped_refptr<StringVar> face_name(StringVar::FromPPVar(desc.face));
193 font_.reset(WebFont::create(web_font_desc)); 43
44 WebKitForwarding::Font* result = NULL;
45 instance->module()->GetWebKitForwarding()->CreateFontForwarding(
46 NULL, desc, face_name ? face_name->value() : std::string(), &result);
47 font_forwarding_.reset(result);
194 } 48 }
195 49
196 PPB_Font_Impl::~PPB_Font_Impl() { 50 PPB_Font_Impl::~PPB_Font_Impl() {
197 } 51 }
198 52
199 // static 53 // static
200 const PPB_Font_Dev* PPB_Font_Impl::GetInterface() { 54 const PPB_Font_Dev* PPB_Font_Impl::GetInterface() {
201 return &ppb_font; 55 return ::ppapi::thunk::GetPPB_Font_Thunk();
56 }
57
58 ::ppapi::thunk::PPB_Font_API* PPB_Font_Impl::AsFont_API() {
59 return this;
202 } 60 }
203 61
204 PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() { 62 PPB_Font_Impl* PPB_Font_Impl::AsPPB_Font_Impl() {
205 return this; 63 return this;
206 } 64 }
207 65
208 bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, 66 PP_Bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description,
209 PP_FontMetrics_Dev* metrics) { 67 PP_FontMetrics_Dev* metrics) {
210 if (description->face.type != PP_VARTYPE_UNDEFINED) 68 std::string face;
211 return false; 69 PP_Bool result = PP_FALSE;
70 font_forwarding_->Describe(NULL, description, &face, metrics, &result);
71 if (!result)
72 return PP_FALSE;
212 73
213 WebFontDescription web_desc = font_->fontDescription(); 74 // Convert the string.
214 75 description->face = StringVar::StringToPPVar(instance()->module(), face);
215 // While converting the other way in PPFontDescToWebFontDesc we validated 76 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 } 77 }
233 78
234 bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data, 79 PP_Bool PPB_Font_Impl::DrawTextAt(PP_Resource image_data,
235 const PP_TextRun_Dev* text, 80 const PP_TextRun_Dev* text,
236 const PP_Point* position, 81 const PP_Point* position,
237 uint32_t color, 82 uint32_t color,
238 const PP_Rect* clip, 83 const PP_Rect* clip,
239 bool image_data_is_opaque) { 84 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. 85 // Get and map the image data we're painting to.
245 scoped_refptr<PPB_ImageData_Impl> image_resource( 86 scoped_refptr<PPB_ImageData_Impl> image_resource(
246 Resource::GetAs<PPB_ImageData_Impl>(image_data)); 87 Resource::GetAs<PPB_ImageData_Impl>(image_data));
247 if (!image_resource.get()) 88 if (!image_resource.get())
248 return false; 89 return PP_FALSE;
249 ImageDataAutoMapper mapper(image_resource); 90 ImageDataAutoMapper mapper(image_resource);
250 if (!mapper.is_valid()) 91 if (!mapper.is_valid())
251 return false; 92 return PP_FALSE;
252 93
253 // Convert position and clip. 94 WebKitForwarding::Font::TextRun run;
254 WebFloatPoint web_position(static_cast<float>(position->x), 95 if (!PPTextRunToTextRun(text, &run))
255 static_cast<float>(position->y)); 96 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 97
265 font_->drawText(webkit_glue::ToWebCanvas(image_resource->mapped_canvas()), 98 font_forwarding_->DrawTextAt(NULL, WebKitForwarding::Font::DrawTextParams(
266 run, web_position, color, web_clip, image_data_is_opaque); 99 image_resource->mapped_canvas(), run, position,
267 return true; 100 color, clip, image_data_is_opaque));
101 return PP_TRUE;
268 } 102 }
269 103
270 int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) { 104 int32_t PPB_Font_Impl::MeasureText(const PP_TextRun_Dev* text) {
271 WebTextRun run; 105 int32_t result = -1;
272 if (!PPTextRunToWebTextRun(text, &run)) 106 WebKitForwarding::Font::TextRun run;
273 return -1; 107 if (PPTextRunToTextRun(text, &run))
274 return font_->calculateWidth(run); 108 font_forwarding_->MeasureText(NULL, run, &result);
109 return result;
275 } 110 }
276 111
277 uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text, 112 uint32_t PPB_Font_Impl::CharacterOffsetForPixel(const PP_TextRun_Dev* text,
278 int32_t pixel_position) { 113 int32_t pixel_position) {
279 WebTextRun run; 114 uint32_t result = -1;
280 if (!PPTextRunToWebTextRun(text, &run)) 115 WebKitForwarding::Font::TextRun run;
281 return -1; 116 if (PPTextRunToTextRun(text, &run)) {
282 117 font_forwarding_->CharacterOffsetForPixel(NULL, run, pixel_position,
283 return static_cast<uint32_t>(font_->offsetForPosition( 118 &result);
284 run, static_cast<float>(pixel_position))); 119 }
120 return result;
285 } 121 }
286 122
287 int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text, 123 int32_t PPB_Font_Impl::PixelOffsetForCharacter(const PP_TextRun_Dev* text,
288 uint32_t char_offset) { 124 uint32_t char_offset) {
289 WebTextRun run; 125 int32_t result = -1;
290 if (!PPTextRunToWebTextRun(text, &run)) 126 WebKitForwarding::Font::TextRun run;
291 return -1; 127 if (PPTextRunToTextRun(text, &run)) {
292 if (char_offset >= run.text.length()) 128 font_forwarding_->PixelOffsetForCharacter(NULL, run, char_offset, &result);
293 return -1; 129 }
294 130 return result;
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 } 131 }
299 132
300 } // namespace ppapi 133 } // namespace ppapi
301 } // namespace webkit 134 } // namespace webkit
302 135
OLDNEW
« no previous file with comments | « webkit/plugins/ppapi/ppb_font_impl.h ('k') | webkit/plugins/ppapi/resource_creation_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698