Add/resurrect support for bundles of WebStore items.
[chromium-blink-merge.git] / ppapi / cpp / dev / font_dev.cc
blob4c74d472903d0eeec7a63e8cf81bb627147f5ff4
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"
7 #include <algorithm>
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"
15 namespace pp {
17 namespace {
19 template <> const char* interface_name<PPB_Font_Dev>() {
20 return PPB_FONT_DEV_INTERFACE;
23 } // namespace
25 // FontDescription_Dev ---------------------------------------------------------
27 FontDescription_Dev::FontDescription_Dev() {
28 pp_font_description_.face = face_.pp_var();
29 set_family(PP_FONTFAMILY_DEFAULT);
30 set_size(0);
31 set_weight(PP_FONTWEIGHT_NORMAL);
32 set_italic(false);
33 set_small_caps(false);
34 set_letter_spacing(0);
35 set_word_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());
61 return *this;
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,
73 bool rtl,
74 bool override_direction)
75 : text_(text) {
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_;
92 text_ = other.text_;
93 pp_text_run_.text = text_.pp_var();
94 return *this;
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>())
108 return;
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);
118 return *this;
121 // static
122 Var Font_Dev::GetFontFamilies(const InstanceHandle& instance) {
123 if (!has_interface<PPB_Font_Dev>())
124 return Var();
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>())
132 return false;
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))
138 return false;
139 description->face_ = Var(PASS_REF,
140 description->pp_font_description_.face);
142 return true;
145 bool Font_Dev::DrawTextAt(ImageData* dest,
146 const TextRun_Dev& text,
147 const Point& position,
148 uint32_t color,
149 const Rect& clip,
150 bool image_data_is_opaque) const {
151 if (!has_interface<PPB_Font_Dev>())
152 return false;
153 return PP_ToBool(get_interface<PPB_Font_Dev>()->DrawTextAt(
154 pp_resource(),
155 dest->pp_resource(),
156 &text.pp_text_run(),
157 &position.pp_point(),
158 color,
159 &clip.pp_rect(),
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>())
165 return -1;
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>())
173 return 0;
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>())
182 return 0;
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,
190 uint32_t color,
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));
200 } // namespace pp