1 // Copyright (c) 2011 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 #ifndef PPAPI_CPP_TRUSTED_BROWSER_FONT_TRUSTED_H_ 6 #define PPAPI_CPP_TRUSTED_BROWSER_FONT_TRUSTED_H_ 7 8 #include <string> 9 10 #include "ppapi/c/trusted/ppb_browser_font_trusted.h" 11 #include "ppapi/cpp/resource.h" 12 #include "ppapi/cpp/var.h" 13 14 namespace pp { 15 16 class ImageData; 17 class InstanceHandle; 18 class Point; 19 class Rect; 20 21 // BrowserFontDescription ------------------------------------------------------ 22 23 class BrowserFontDescription { 24 public: 25 BrowserFontDescription(); 26 BrowserFontDescription(const BrowserFontDescription& other); 27 ~BrowserFontDescription(); 28 29 BrowserFontDescription& operator=(const BrowserFontDescription& other); 30 pp_font_description()31 const PP_BrowserFont_Trusted_Description& pp_font_description() const { 32 return pp_font_description_; 33 } 34 face()35 Var face() const { return face_; } set_face(const Var & face)36 void set_face(const Var& face) { 37 face_ = face; 38 pp_font_description_.face = face_.pp_var(); 39 } 40 family()41 PP_BrowserFont_Trusted_Family family() const { 42 return pp_font_description_.family; 43 } set_family(PP_BrowserFont_Trusted_Family f)44 void set_family(PP_BrowserFont_Trusted_Family f) { 45 pp_font_description_.family = f; 46 } 47 size()48 uint32_t size() const { return pp_font_description_.size; } set_size(uint32_t s)49 void set_size(uint32_t s) { pp_font_description_.size = s; } 50 weight()51 PP_BrowserFont_Trusted_Weight weight() const { 52 return pp_font_description_.weight; 53 } set_weight(PP_BrowserFont_Trusted_Weight w)54 void set_weight(PP_BrowserFont_Trusted_Weight w) { 55 pp_font_description_.weight = w; 56 } 57 italic()58 bool italic() const { return PP_ToBool(pp_font_description_.italic); } set_italic(bool i)59 void set_italic(bool i) { pp_font_description_.italic = PP_FromBool(i); } 60 small_caps()61 bool small_caps() const { 62 return PP_ToBool(pp_font_description_.small_caps); 63 } set_small_caps(bool s)64 void set_small_caps(bool s) { 65 pp_font_description_.small_caps = PP_FromBool(s); 66 } 67 letter_spacing()68 int letter_spacing() const { return pp_font_description_.letter_spacing; } set_letter_spacing(int s)69 void set_letter_spacing(int s) { pp_font_description_.letter_spacing = s; } 70 word_spacing()71 int word_spacing() const { return pp_font_description_.word_spacing; } set_word_spacing(int w)72 void set_word_spacing(int w) { pp_font_description_.word_spacing = w; } 73 74 private: 75 friend class BrowserFont_Trusted; 76 77 Var face_; // Manages memory for pp_font_description_.face 78 PP_BrowserFont_Trusted_Description pp_font_description_; 79 }; 80 81 // BrowserFontTextRun ---------------------------------------------------------- 82 83 class BrowserFontTextRun { 84 public: 85 BrowserFontTextRun(); 86 BrowserFontTextRun(const std::string& text, 87 bool rtl = false, 88 bool override_direction = false); 89 BrowserFontTextRun(const BrowserFontTextRun& other); 90 ~BrowserFontTextRun(); 91 92 BrowserFontTextRun& operator=(const BrowserFontTextRun& other); 93 pp_text_run()94 const PP_BrowserFont_Trusted_TextRun& pp_text_run() const { 95 return pp_text_run_; 96 } 97 98 private: 99 Var text_; // Manages memory for the reference in pp_text_run_. 100 PP_BrowserFont_Trusted_TextRun pp_text_run_; 101 }; 102 103 // BrowserFont_Trusted --------------------------------------------------------- 104 105 // Provides access to system fonts. 106 class BrowserFont_Trusted : public Resource { 107 public: 108 // Creates an is_null() Font object. 109 BrowserFont_Trusted(); 110 111 explicit BrowserFont_Trusted(PP_Resource resource); 112 BrowserFont_Trusted(const InstanceHandle& instance, 113 const BrowserFontDescription& description); 114 BrowserFont_Trusted(const BrowserFont_Trusted& other); 115 116 BrowserFont_Trusted& operator=(const BrowserFont_Trusted& other); 117 118 // PPB_Font methods: 119 static Var GetFontFamilies(const InstanceHandle& instance); 120 bool Describe(BrowserFontDescription* description, 121 PP_BrowserFont_Trusted_Metrics* metrics) const; 122 bool DrawTextAt(ImageData* dest, 123 const BrowserFontTextRun& text, 124 const Point& position, 125 uint32_t color, 126 const Rect& clip, 127 bool image_data_is_opaque) const; 128 int32_t MeasureText(const BrowserFontTextRun& text) const; 129 uint32_t CharacterOffsetForPixel(const BrowserFontTextRun& text, 130 int32_t pixel_position) const; 131 int32_t PixelOffsetForCharacter(const BrowserFontTextRun& text, 132 uint32_t char_offset) const; 133 134 // Convenience function that assumes a left-to-right string with no clipping. 135 bool DrawSimpleText(ImageData* dest, 136 const std::string& text, 137 const Point& position, 138 uint32_t color, 139 bool image_data_is_opaque = false) const; 140 141 // Convenience function that assumes a left-to-right string. 142 int32_t MeasureSimpleText(const std::string& text) const; 143 }; 144 145 } // namespace pp 146 147 #endif // PPAPI_CPP_TRUSTED_BROWSER_FONT_TRUSTED_H_ 148