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