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

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

Powered by Google App Engine
This is Rietveld 408576698