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.
5 #include "ppapi/cpp/dev/font_dev.h"
9 #include "ppapi/cpp/image_data.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/point.h"
12 #include "ppapi/cpp/rect.h"
13 #include "ppapi/cpp/module_impl.h"
19 template <> const char* interface_name
<PPB_Font_Dev
>() {
20 return PPB_FONT_DEV_INTERFACE
;
25 // FontDescription_Dev ---------------------------------------------------------
27 FontDescription_Dev::FontDescription_Dev() {
28 pp_font_description_
.face
= face_
.pp_var();
29 set_family(PP_FONTFAMILY_DEFAULT
);
31 set_weight(PP_FONTWEIGHT_NORMAL
);
33 set_small_caps(false);
34 set_letter_spacing(0);
38 FontDescription_Dev::FontDescription_Dev(const FontDescription_Dev
& other
) {
39 set_face(other
.face());
40 set_family(other
.family());
41 set_size(other
.size());
42 set_weight(other
.weight());
43 set_italic(other
.italic());
44 set_small_caps(other
.small_caps());
45 set_letter_spacing(other
.letter_spacing());
46 set_word_spacing(other
.word_spacing());
49 FontDescription_Dev::~FontDescription_Dev() {
52 FontDescription_Dev
& FontDescription_Dev::operator=(
53 const FontDescription_Dev
& other
) {
54 pp_font_description_
= other
.pp_font_description_
;
56 // Be careful about the refcount of the string, the copy that operator= made
57 // above didn't copy a ref.
58 pp_font_description_
.face
= PP_MakeUndefined();
59 set_face(other
.face());
64 // TextRun_Dev -----------------------------------------------------------------
66 TextRun_Dev::TextRun_Dev() {
67 pp_text_run_
.text
= text_
.pp_var();
68 pp_text_run_
.rtl
= PP_FALSE
;
69 pp_text_run_
.override_direction
= PP_FALSE
;
72 TextRun_Dev::TextRun_Dev(const std::string
& text
,
74 bool override_direction
)
76 pp_text_run_
.text
= text_
.pp_var();
77 pp_text_run_
.rtl
= PP_FromBool(rtl
);
78 pp_text_run_
.override_direction
= PP_FromBool(override_direction
);
81 TextRun_Dev::TextRun_Dev(const TextRun_Dev
& other
) : text_(other
.text_
) {
82 pp_text_run_
.text
= text_
.pp_var();
83 pp_text_run_
.rtl
= other
.pp_text_run_
.rtl
;
84 pp_text_run_
.override_direction
= other
.pp_text_run_
.override_direction
;
87 TextRun_Dev::~TextRun_Dev() {
90 TextRun_Dev
& TextRun_Dev::operator=(const TextRun_Dev
& other
) {
91 pp_text_run_
= other
.pp_text_run_
;
93 pp_text_run_
.text
= text_
.pp_var();
97 // Font ------------------------------------------------------------------------
99 Font_Dev::Font_Dev() : Resource() {
102 Font_Dev::Font_Dev(PP_Resource resource
) : Resource(resource
) {
105 Font_Dev::Font_Dev(const InstanceHandle
& instance
,
106 const FontDescription_Dev
& description
) {
107 if (!has_interface
<PPB_Font_Dev
>())
109 PassRefFromConstructor(get_interface
<PPB_Font_Dev
>()->Create(
110 instance
.pp_instance(), &description
.pp_font_description()));
113 Font_Dev::Font_Dev(const Font_Dev
& other
) : Resource(other
) {
116 Font_Dev
& Font_Dev::operator=(const Font_Dev
& other
) {
117 Resource::operator=(other
);
122 Var
Font_Dev::GetFontFamilies(const InstanceHandle
& instance
) {
123 if (!has_interface
<PPB_Font_Dev
>())
125 return Var(PASS_REF
, get_interface
<PPB_Font_Dev
>()->GetFontFamilies(
126 instance
.pp_instance()));
129 bool Font_Dev::Describe(FontDescription_Dev
* description
,
130 PP_FontMetrics_Dev
* metrics
) const {
131 if (!has_interface
<PPB_Font_Dev
>())
134 // Be careful with ownership of the |face| string. It will come back with
135 // a ref of 1, which we want to assign to the |face_| member of the C++ class.
136 if (!get_interface
<PPB_Font_Dev
>()->Describe(
137 pp_resource(), &description
->pp_font_description_
, metrics
))
139 description
->face_
= Var(PASS_REF
,
140 description
->pp_font_description_
.face
);
145 bool Font_Dev::DrawTextAt(ImageData
* dest
,
146 const TextRun_Dev
& text
,
147 const Point
& position
,
150 bool image_data_is_opaque
) const {
151 if (!has_interface
<PPB_Font_Dev
>())
153 return PP_ToBool(get_interface
<PPB_Font_Dev
>()->DrawTextAt(
157 &position
.pp_point(),
160 PP_FromBool(image_data_is_opaque
)));
163 int32_t Font_Dev::MeasureText(const TextRun_Dev
& text
) const {
164 if (!has_interface
<PPB_Font_Dev
>())
166 return get_interface
<PPB_Font_Dev
>()->MeasureText(pp_resource(),
167 &text
.pp_text_run());
170 uint32_t Font_Dev::CharacterOffsetForPixel(const TextRun_Dev
& text
,
171 int32_t pixel_position
) const {
172 if (!has_interface
<PPB_Font_Dev
>())
174 return get_interface
<PPB_Font_Dev
>()->CharacterOffsetForPixel(
175 pp_resource(), &text
.pp_text_run(), pixel_position
);
179 int32_t Font_Dev::PixelOffsetForCharacter(const TextRun_Dev
& text
,
180 uint32_t char_offset
) const {
181 if (!has_interface
<PPB_Font_Dev
>())
183 return get_interface
<PPB_Font_Dev
>()->PixelOffsetForCharacter(
184 pp_resource(), &text
.pp_text_run(), char_offset
);
187 bool Font_Dev::DrawSimpleText(ImageData
* dest
,
188 const std::string
& text
,
189 const Point
& position
,
191 bool image_data_is_opaque
) const {
192 return DrawTextAt(dest
, TextRun_Dev(text
), position
, color
,
193 Rect(dest
->size()), image_data_is_opaque
);
196 int32_t Font_Dev::MeasureSimpleText(const std::string
& text
) const {
197 return MeasureText(TextRun_Dev(text
));