| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/trusted/browser_font_trusted.h" | 5 #include "ppapi/cpp/trusted/browser_font_trusted.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ppapi/c/dev/ppb_font_dev.h" | 9 #include "ppapi/c/dev/ppb_font_dev.h" |
| 10 #include "ppapi/cpp/image_data.h" | 10 #include "ppapi/cpp/image_data.h" |
| 11 #include "ppapi/cpp/instance.h" | 11 #include "ppapi/cpp/instance.h" |
| 12 #include "ppapi/cpp/point.h" | 12 #include "ppapi/cpp/point.h" |
| 13 #include "ppapi/cpp/rect.h" | 13 #include "ppapi/cpp/rect.h" |
| 14 #include "ppapi/cpp/module_impl.h" | 14 #include "ppapi/cpp/module_impl.h" |
| 15 | 15 |
| 16 namespace pp { | 16 namespace pp { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 template <> const char* interface_name<PPB_BrowserFont_Trusted>() { | 20 template <> const char* interface_name<PPB_BrowserFont_Trusted_1_0>() { |
| 21 return PPB_BROWSERFONT_TRUSTED_INTERFACE; | 21 return PPB_BROWSERFONT_TRUSTED_INTERFACE_1_0; |
| 22 } | 22 } |
| 23 template <> const char* interface_name<PPB_Font_Dev>() { | 23 template <> const char* interface_name<PPB_Font_Dev_0_6>() { |
| 24 return PPB_FONT_DEV_INTERFACE; | 24 return PPB_FONT_DEV_INTERFACE_0_6; |
| 25 } | 25 } |
| 26 | 26 |
| 27 // This class provides backwards compat for PPB_Font, which is binary | 27 // This class provides backwards compat for PPB_Font, which is binary |
| 28 // compatible with the BrowserFont interface. | 28 // compatible with the BrowserFont interface. |
| 29 // TODO(brettw) remove PPB_Font altogether when Flash is updated. | 29 // TODO(brettw) remove PPB_Font altogether when Flash is updated. |
| 30 const PP_FontDescription_Dev* BrowserFontDescToFontDesc( | 30 const PP_FontDescription_Dev* BrowserFontDescToFontDesc( |
| 31 const PP_BrowserFont_Trusted_Description* desc) { | 31 const PP_BrowserFont_Trusted_Description* desc) { |
| 32 return reinterpret_cast<const PP_FontDescription_Dev*>(desc); | 32 return reinterpret_cast<const PP_FontDescription_Dev*>(desc); |
| 33 } | 33 } |
| 34 PP_FontDescription_Dev* BrowserFontDescToFontDesc( | 34 PP_FontDescription_Dev* BrowserFontDescToFontDesc( |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 BrowserFont_Trusted::BrowserFont_Trusted() : Resource() { | 126 BrowserFont_Trusted::BrowserFont_Trusted() : Resource() { |
| 127 } | 127 } |
| 128 | 128 |
| 129 BrowserFont_Trusted::BrowserFont_Trusted(PP_Resource resource) | 129 BrowserFont_Trusted::BrowserFont_Trusted(PP_Resource resource) |
| 130 : Resource(resource) { | 130 : Resource(resource) { |
| 131 } | 131 } |
| 132 | 132 |
| 133 BrowserFont_Trusted::BrowserFont_Trusted( | 133 BrowserFont_Trusted::BrowserFont_Trusted( |
| 134 Instance* instance, | 134 Instance* instance, |
| 135 const BrowserFontDescription& description) { | 135 const BrowserFontDescription& description) { |
| 136 if (has_interface<PPB_BrowserFont_Trusted>()) { | 136 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 137 PassRefFromConstructor(get_interface<PPB_BrowserFont_Trusted>()->Create( | 137 PassRefFromConstructor(get_interface<PPB_BrowserFont_Trusted_1_0>()->Create( |
| 138 instance->pp_instance(), | 138 instance->pp_instance(), |
| 139 &description.pp_font_description())); | 139 &description.pp_font_description())); |
| 140 } else if (!has_interface<PPB_Font_Dev>()) { | 140 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 141 PassRefFromConstructor(get_interface<PPB_Font_Dev>()->Create( | 141 PassRefFromConstructor(get_interface<PPB_Font_Dev_0_6>()->Create( |
| 142 instance->pp_instance(), | 142 instance->pp_instance(), |
| 143 BrowserFontDescToFontDesc(&description.pp_font_description()))); | 143 BrowserFontDescToFontDesc(&description.pp_font_description()))); |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 BrowserFont_Trusted::BrowserFont_Trusted(const BrowserFont_Trusted& other) | 147 BrowserFont_Trusted::BrowserFont_Trusted(const BrowserFont_Trusted& other) |
| 148 : Resource(other) { | 148 : Resource(other) { |
| 149 } | 149 } |
| 150 | 150 |
| 151 BrowserFont_Trusted& BrowserFont_Trusted::operator=( | 151 BrowserFont_Trusted& BrowserFont_Trusted::operator=( |
| 152 const BrowserFont_Trusted& other) { | 152 const BrowserFont_Trusted& other) { |
| 153 Resource::operator=(other); | 153 Resource::operator=(other); |
| 154 return *this; | 154 return *this; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // static | 157 // static |
| 158 Var BrowserFont_Trusted::GetFontFamilies(Instance* instance) { | 158 Var BrowserFont_Trusted::GetFontFamilies(Instance* instance) { |
| 159 if (!has_interface<PPB_Font_Dev>()) | 159 if (!has_interface<PPB_Font_Dev_0_6>()) |
| 160 return Var(); | 160 return Var(); |
| 161 return Var(PASS_REF, | 161 return Var(PASS_REF, |
| 162 get_interface<PPB_Font_Dev>()->GetFontFamilies( | 162 get_interface<PPB_Font_Dev_0_6>()->GetFontFamilies( |
| 163 instance->pp_instance())); | 163 instance->pp_instance())); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool BrowserFont_Trusted::Describe( | 166 bool BrowserFont_Trusted::Describe( |
| 167 BrowserFontDescription* description, | 167 BrowserFontDescription* description, |
| 168 PP_BrowserFont_Trusted_Metrics* metrics) const { | 168 PP_BrowserFont_Trusted_Metrics* metrics) const { |
| 169 // Be careful with ownership of the |face| string. It will come back with | 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. | 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>()) { | 171 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 172 if (!get_interface<PPB_BrowserFont_Trusted>()->Describe( | 172 if (!get_interface<PPB_BrowserFont_Trusted_1_0>()->Describe( |
| 173 pp_resource(), &description->pp_font_description_, metrics)) | 173 pp_resource(), &description->pp_font_description_, metrics)) |
| 174 return false; | 174 return false; |
| 175 } else if (!has_interface<PPB_Font_Dev>()) { | 175 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 176 if (!get_interface<PPB_Font_Dev>()->Describe( | 176 if (!get_interface<PPB_Font_Dev_0_6>()->Describe( |
| 177 pp_resource(), | 177 pp_resource(), |
| 178 BrowserFontDescToFontDesc(&description->pp_font_description_), | 178 BrowserFontDescToFontDesc(&description->pp_font_description_), |
| 179 BrowserFontMetricsToFontMetrics(metrics))) | 179 BrowserFontMetricsToFontMetrics(metrics))) |
| 180 return false; | 180 return false; |
| 181 } | 181 } |
| 182 description->face_ = Var(PASS_REF, | 182 description->face_ = Var(PASS_REF, |
| 183 description->pp_font_description_.face); | 183 description->pp_font_description_.face); |
| 184 return true; | 184 return true; |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool BrowserFont_Trusted::DrawTextAt(ImageData* dest, | 187 bool BrowserFont_Trusted::DrawTextAt(ImageData* dest, |
| 188 const BrowserFontTextRun& text, | 188 const BrowserFontTextRun& text, |
| 189 const Point& position, | 189 const Point& position, |
| 190 uint32_t color, | 190 uint32_t color, |
| 191 const Rect& clip, | 191 const Rect& clip, |
| 192 bool image_data_is_opaque) const { | 192 bool image_data_is_opaque) const { |
| 193 if (has_interface<PPB_BrowserFont_Trusted>()) { | 193 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 194 return PP_ToBool(get_interface<PPB_BrowserFont_Trusted>()->DrawTextAt( | 194 return PP_ToBool(get_interface<PPB_BrowserFont_Trusted_1_0>()->DrawTextAt( |
| 195 pp_resource(), | 195 pp_resource(), |
| 196 dest->pp_resource(), | 196 dest->pp_resource(), |
| 197 &text.pp_text_run(), | 197 &text.pp_text_run(), |
| 198 &position.pp_point(), | 198 &position.pp_point(), |
| 199 color, | 199 color, |
| 200 &clip.pp_rect(), | 200 &clip.pp_rect(), |
| 201 PP_FromBool(image_data_is_opaque))); | 201 PP_FromBool(image_data_is_opaque))); |
| 202 } else if (!has_interface<PPB_Font_Dev>()) { | 202 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 203 return PP_ToBool(get_interface<PPB_Font_Dev>()->DrawTextAt( | 203 return PP_ToBool(get_interface<PPB_Font_Dev_0_6>()->DrawTextAt( |
| 204 pp_resource(), | 204 pp_resource(), |
| 205 dest->pp_resource(), | 205 dest->pp_resource(), |
| 206 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), | 206 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), |
| 207 &position.pp_point(), | 207 &position.pp_point(), |
| 208 color, | 208 color, |
| 209 &clip.pp_rect(), | 209 &clip.pp_rect(), |
| 210 PP_FromBool(image_data_is_opaque))); | 210 PP_FromBool(image_data_is_opaque))); |
| 211 } | 211 } |
| 212 return false; | 212 return false; |
| 213 } | 213 } |
| 214 | 214 |
| 215 int32_t BrowserFont_Trusted::MeasureText(const BrowserFontTextRun& text) const { | 215 int32_t BrowserFont_Trusted::MeasureText(const BrowserFontTextRun& text) const { |
| 216 if (has_interface<PPB_BrowserFont_Trusted>()) { | 216 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 217 return get_interface<PPB_BrowserFont_Trusted>()->MeasureText( | 217 return get_interface<PPB_BrowserFont_Trusted_1_0>()->MeasureText( |
| 218 pp_resource(), | 218 pp_resource(), |
| 219 &text.pp_text_run()); | 219 &text.pp_text_run()); |
| 220 } else if (!has_interface<PPB_Font_Dev>()) { | 220 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 221 return get_interface<PPB_Font_Dev>()->MeasureText( | 221 return get_interface<PPB_Font_Dev_0_6>()->MeasureText( |
| 222 pp_resource(), | 222 pp_resource(), |
| 223 BrowserFontTextRunToFontTextRun(&text.pp_text_run())); | 223 BrowserFontTextRunToFontTextRun(&text.pp_text_run())); |
| 224 } | 224 } |
| 225 return -1; | 225 return -1; |
| 226 } | 226 } |
| 227 | 227 |
| 228 uint32_t BrowserFont_Trusted::CharacterOffsetForPixel( | 228 uint32_t BrowserFont_Trusted::CharacterOffsetForPixel( |
| 229 const BrowserFontTextRun& text, | 229 const BrowserFontTextRun& text, |
| 230 int32_t pixel_position) const { | 230 int32_t pixel_position) const { |
| 231 if (has_interface<PPB_BrowserFont_Trusted>()) { | 231 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 232 return get_interface<PPB_BrowserFont_Trusted>()->CharacterOffsetForPixel( | 232 return get_interface<PPB_BrowserFont_Trusted_1_0>()-> |
| 233 pp_resource(), | 233 CharacterOffsetForPixel( |
| 234 &text.pp_text_run(), | 234 pp_resource(), |
| 235 pixel_position); | 235 &text.pp_text_run(), |
| 236 } else if (!has_interface<PPB_Font_Dev>()) { | 236 pixel_position); |
| 237 return get_interface<PPB_Font_Dev>()->CharacterOffsetForPixel( | 237 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 238 return get_interface<PPB_Font_Dev_0_6>()->CharacterOffsetForPixel( |
| 238 pp_resource(), | 239 pp_resource(), |
| 239 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), | 240 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), |
| 240 pixel_position); | 241 pixel_position); |
| 241 } | 242 } |
| 242 return 0; | 243 return 0; |
| 243 } | 244 } |
| 244 | 245 |
| 245 int32_t BrowserFont_Trusted::PixelOffsetForCharacter( | 246 int32_t BrowserFont_Trusted::PixelOffsetForCharacter( |
| 246 const BrowserFontTextRun& text, | 247 const BrowserFontTextRun& text, |
| 247 uint32_t char_offset) const { | 248 uint32_t char_offset) const { |
| 248 if (has_interface<PPB_BrowserFont_Trusted>()) { | 249 if (has_interface<PPB_BrowserFont_Trusted_1_0>()) { |
| 249 return get_interface<PPB_BrowserFont_Trusted>()->PixelOffsetForCharacter( | 250 return get_interface<PPB_BrowserFont_Trusted_1_0>()-> |
| 250 pp_resource(), | 251 PixelOffsetForCharacter( |
| 251 &text.pp_text_run(), | 252 pp_resource(), |
| 252 char_offset); | 253 &text.pp_text_run(), |
| 253 } else if (!has_interface<PPB_Font_Dev>()) { | 254 char_offset); |
| 254 return get_interface<PPB_Font_Dev>()->PixelOffsetForCharacter( | 255 } else if (!has_interface<PPB_Font_Dev_0_6>()) { |
| 256 return get_interface<PPB_Font_Dev_0_6>()->PixelOffsetForCharacter( |
| 255 pp_resource(), | 257 pp_resource(), |
| 256 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), | 258 BrowserFontTextRunToFontTextRun(&text.pp_text_run()), |
| 257 char_offset); | 259 char_offset); |
| 258 } | 260 } |
| 259 return 0; | 261 return 0; |
| 260 } | 262 } |
| 261 | 263 |
| 262 bool BrowserFont_Trusted::DrawSimpleText( | 264 bool BrowserFont_Trusted::DrawSimpleText( |
| 263 ImageData* dest, | 265 ImageData* dest, |
| 264 const std::string& text, | 266 const std::string& text, |
| 265 const Point& position, | 267 const Point& position, |
| 266 uint32_t color, | 268 uint32_t color, |
| 267 bool image_data_is_opaque) const { | 269 bool image_data_is_opaque) const { |
| 268 return DrawTextAt(dest, BrowserFontTextRun(text), position, color, | 270 return DrawTextAt(dest, BrowserFontTextRun(text), position, color, |
| 269 Rect(dest->size()), image_data_is_opaque); | 271 Rect(dest->size()), image_data_is_opaque); |
| 270 } | 272 } |
| 271 | 273 |
| 272 int32_t BrowserFont_Trusted::MeasureSimpleText(const std::string& text) const { | 274 int32_t BrowserFont_Trusted::MeasureSimpleText(const std::string& text) const { |
| 273 return MeasureText(BrowserFontTextRun(text)); | 275 return MeasureText(BrowserFontTextRun(text)); |
| 274 } | 276 } |
| 275 | 277 |
| 276 } // namespace pp | 278 } // namespace pp |
| OLD | NEW |