1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
// Copyright (c) 2012 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/cpp/trusted/browser_font_trusted.h"
#include <algorithm>
#include "ppapi/c/dev/ppb_font_dev.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/module_impl.h"
namespace pp {
namespace {
template <> const char* interface_name<PPB_BrowserFont_Trusted_1_0>() {
return PPB_BROWSERFONT_TRUSTED_INTERFACE_1_0;
}
template <> const char* interface_name<PPB_Font_Dev_0_6>() {
return PPB_FONT_DEV_INTERFACE_0_6;
}
// This class provides backwards compat for PPB_Font, which is binary
// compatible with the BrowserFont interface.
// TODO(brettw) remove PPB_Font altogether when Flash is updated.
const PP_FontDescription_Dev* BrowserFontDescToFontDesc(
const PP_BrowserFont_Trusted_Description* desc) {
return reinterpret_cast<const PP_FontDescription_Dev*>(desc);
}
PP_FontDescription_Dev* BrowserFontDescToFontDesc(
PP_BrowserFont_Trusted_Description* desc) {
return reinterpret_cast<PP_FontDescription_Dev*>(desc);
}
PP_FontMetrics_Dev* BrowserFontMetricsToFontMetrics(
PP_BrowserFont_Trusted_Metrics* metrics) {
return reinterpret_cast<PP_FontMetrics_Dev*>(metrics);
}
const PP_TextRun_Dev* BrowserFontTextRunToFontTextRun(
const PP_BrowserFont_Trusted_TextRun* run) {
return reinterpret_cast<const PP_TextRun_Dev*>(run);
}
} // namespace
// BrowserFontDescription ------------------------------------------------------
BrowserFontDescription::BrowserFontDescription() {
pp_font_description_.face = face_.pp_var();
set_family(PP_BROWSERFONT_TRUSTED_FAMILY_DEFAULT);
set_size(0);
set_weight(PP_BROWSERFONT_TRUSTED_WEIGHT_NORMAL);
set_italic(false);
set_small_caps(false);
set_letter_spacing(0);
set_word_spacing(0);
}
BrowserFontDescription::BrowserFontDescription(
const BrowserFontDescription& other) {
set_face(other.face());
set_family(other.family());
set_size(other.size());
set_weight(other.weight());
set_italic(other.italic());
set_small_caps(other.small_caps());
set_letter_spacing(other.letter_spacing());
set_word_spacing(other.word_spacing());
}
BrowserFontDescription::~BrowserFontDescription() {
}
BrowserFontDescription& BrowserFontDescription::operator=(
const BrowserFontDescription& other) {
pp_font_description_ = other.pp_font_description_;
// Be careful about the refcount of the string, the copy that operator= made
// above didn't copy a ref.
pp_font_description_.face = PP_MakeUndefined();
set_face(other.face());
return *this;
}
// BrowserFontTextRun ----------------------------------------------------------
BrowserFontTextRun::BrowserFontTextRun() {
pp_text_run_.text = text_.pp_var();
pp_text_run_.rtl = PP_FALSE;
pp_text_run_.override_direction = PP_FALSE;
}
BrowserFontTextRun::BrowserFontTextRun(const std::string& text,
bool rtl,
bool override_direction)
: text_(text) {
pp_text_run_.text = text_.pp_var();
pp_text_run_.rtl = PP_FromBool(rtl);
pp_text_run_.override_direction = PP_FromBool(override_direction);
}
BrowserFontTextRun::BrowserFontTextRun(const BrowserFontTextRun& other)
: text_(other.text_) {
pp_text_run_.text = text_.pp_var();
pp_text_run_.rtl = other.pp_text_run_.rtl;
pp_text_run_.override_direction = other.pp_text_run_.override_direction;
}
BrowserFontTextRun::~BrowserFontTextRun() {
}
BrowserFontTextRun& BrowserFontTextRun::operator=(
const BrowserFontTextRun& other) {
pp_text_run_ = other.pp_text_run_;
text_ = other.text_;
pp_text_run_.text = text_.pp_var();
return *this;
}
// BrowserFont_Trusted ---------------------------------------------------------
BrowserFont_Trusted::BrowserFont_Trusted() : Resource() {
}
BrowserFont_Trusted::BrowserFont_Trusted(PP_Resource resource)
: Resource(resource) {
}
BrowserFont_Trusted::BrowserFont_Trusted(
Instance* instance,
const BrowserFontDescription& description) {
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
PassRefFromConstructor(get_interface<PPB_BrowserFont_Trusted_1_0>()->Create(
instance->pp_instance(),
&description.pp_font_description()));
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
PassRefFromConstructor(get_interface<PPB_Font_Dev_0_6>()->Create(
instance->pp_instance(),
BrowserFontDescToFontDesc(&description.pp_font_description())));
}
}
BrowserFont_Trusted::BrowserFont_Trusted(const BrowserFont_Trusted& other)
: Resource(other) {
}
BrowserFont_Trusted& BrowserFont_Trusted::operator=(
const BrowserFont_Trusted& other) {
Resource::operator=(other);
return *this;
}
// static
Var BrowserFont_Trusted::GetFontFamilies(Instance* instance) {
if (!has_interface<PPB_Font_Dev_0_6>())
return Var();
return Var(PASS_REF,
get_interface<PPB_Font_Dev_0_6>()->GetFontFamilies(
instance->pp_instance()));
}
bool BrowserFont_Trusted::Describe(
BrowserFontDescription* description,
PP_BrowserFont_Trusted_Metrics* metrics) const {
// Be careful with ownership of the |face| string. It will come back with
// a ref of 1, which we want to assign to the |face_| member of the C++ class.
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
if (!get_interface<PPB_BrowserFont_Trusted_1_0>()->Describe(
pp_resource(), &description->pp_font_description_, metrics))
return false;
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
if (!get_interface<PPB_Font_Dev_0_6>()->Describe(
pp_resource(),
BrowserFontDescToFontDesc(&description->pp_font_description_),
BrowserFontMetricsToFontMetrics(metrics)))
return false;
}
description->face_ = Var(PASS_REF,
description->pp_font_description_.face);
return true;
}
bool BrowserFont_Trusted::DrawTextAt(ImageData* dest,
const BrowserFontTextRun& text,
const Point& position,
uint32_t color,
const Rect& clip,
bool image_data_is_opaque) const {
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
return PP_ToBool(get_interface<PPB_BrowserFont_Trusted_1_0>()->DrawTextAt(
pp_resource(),
dest->pp_resource(),
&text.pp_text_run(),
&position.pp_point(),
color,
&clip.pp_rect(),
PP_FromBool(image_data_is_opaque)));
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
return PP_ToBool(get_interface<PPB_Font_Dev_0_6>()->DrawTextAt(
pp_resource(),
dest->pp_resource(),
BrowserFontTextRunToFontTextRun(&text.pp_text_run()),
&position.pp_point(),
color,
&clip.pp_rect(),
PP_FromBool(image_data_is_opaque)));
}
return false;
}
int32_t BrowserFont_Trusted::MeasureText(const BrowserFontTextRun& text) const {
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
return get_interface<PPB_BrowserFont_Trusted_1_0>()->MeasureText(
pp_resource(),
&text.pp_text_run());
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
return get_interface<PPB_Font_Dev_0_6>()->MeasureText(
pp_resource(),
BrowserFontTextRunToFontTextRun(&text.pp_text_run()));
}
return -1;
}
uint32_t BrowserFont_Trusted::CharacterOffsetForPixel(
const BrowserFontTextRun& text,
int32_t pixel_position) const {
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
return get_interface<PPB_BrowserFont_Trusted_1_0>()->
CharacterOffsetForPixel(
pp_resource(),
&text.pp_text_run(),
pixel_position);
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
return get_interface<PPB_Font_Dev_0_6>()->CharacterOffsetForPixel(
pp_resource(),
BrowserFontTextRunToFontTextRun(&text.pp_text_run()),
pixel_position);
}
return 0;
}
int32_t BrowserFont_Trusted::PixelOffsetForCharacter(
const BrowserFontTextRun& text,
uint32_t char_offset) const {
if (has_interface<PPB_BrowserFont_Trusted_1_0>()) {
return get_interface<PPB_BrowserFont_Trusted_1_0>()->
PixelOffsetForCharacter(
pp_resource(),
&text.pp_text_run(),
char_offset);
} else if (!has_interface<PPB_Font_Dev_0_6>()) {
return get_interface<PPB_Font_Dev_0_6>()->PixelOffsetForCharacter(
pp_resource(),
BrowserFontTextRunToFontTextRun(&text.pp_text_run()),
char_offset);
}
return 0;
}
bool BrowserFont_Trusted::DrawSimpleText(
ImageData* dest,
const std::string& text,
const Point& position,
uint32_t color,
bool image_data_is_opaque) const {
return DrawTextAt(dest, BrowserFontTextRun(text), position, color,
Rect(dest->size()), image_data_is_opaque);
}
int32_t BrowserFont_Trusted::MeasureSimpleText(const std::string& text) const {
return MeasureText(BrowserFontTextRun(text));
}
} // namespace pp
|