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