1 // Copyright (c) 2013 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 #ifndef PPAPI_CPP_TRUETYPE_FONT_H_
6 #define PPAPI_CPP_TRUETYPE_FONT_H_
8 #include "ppapi/c/dev/ppb_truetype_font_dev.h"
9 #include "ppapi/c/pp_time.h"
10 #include "ppapi/cpp/completion_callback.h"
11 #include "ppapi/cpp/pass_ref.h"
12 #include "ppapi/cpp/resource.h"
15 /// This file defines the API to create a TrueType font object.
21 // TrueTypeFontDesc_Dev --------------------------------------------------------
23 /// The <code>TrueTypeFontDesc_Dev</code> class represents a TrueType font
24 /// descriptor, used to Create and Describe fonts.
25 class TrueTypeFontDesc_Dev {
27 /// Default constructor for creating a <code>TrueTypeFontDesc_Dev</code>
29 TrueTypeFontDesc_Dev();
31 /// Constructor that takes an existing <code>PP_TrueTypeFontDesc_Dev</code>
32 /// structure. The 'family' PP_Var field in the structure will be managed by
34 TrueTypeFontDesc_Dev(PassRef, const PP_TrueTypeFontDesc_Dev& pp_desc);
36 /// The copy constructor for <code>TrueTypeFontDesc_Dev</code>.
38 /// @param[in] other A reference to a <code>TrueTypeFontDesc_Dev</code>.
39 TrueTypeFontDesc_Dev(const TrueTypeFontDesc_Dev& other);
41 ~TrueTypeFontDesc_Dev();
43 TrueTypeFontDesc_Dev& operator=(const TrueTypeFontDesc_Dev& other);
45 const PP_TrueTypeFontDesc_Dev& pp_desc() const {
52 void set_family(const Var& family) {
54 // The assignment above manages the underlying PP_Vars. Copy the new one
55 // into the wrapped desc struct.
56 desc_.family = family_.pp_var();
59 PP_TrueTypeFontFamily_Dev generic_family() const {
60 return desc_.generic_family;
62 void set_generic_family(PP_TrueTypeFontFamily_Dev family) {
63 desc_.generic_family = family;
66 PP_TrueTypeFontStyle_Dev style() const { return desc_.style; }
67 void set_style(PP_TrueTypeFontStyle_Dev style) {
71 PP_TrueTypeFontWeight_Dev weight() const { return desc_.weight; }
72 void set_weight(PP_TrueTypeFontWeight_Dev weight) {
73 desc_.weight = weight;
76 PP_TrueTypeFontWidth_Dev width() const { return desc_.width; }
77 void set_width(PP_TrueTypeFontWidth_Dev width) {
81 PP_TrueTypeFontCharset_Dev charset() const { return desc_.charset; }
82 void set_charset(PP_TrueTypeFontCharset_Dev charset) {
83 desc_.charset = charset;
87 pp::Var family_; // This manages the PP_Var embedded in desc_.
88 PP_TrueTypeFontDesc_Dev desc_;
91 // TrueTypeFont_Dev ------------------------------------------------------------
93 /// The <code>TrueTypeFont_Dev</code> class represents a TrueType font resource.
94 class TrueTypeFont_Dev : public Resource {
96 /// Default constructor for creating a <code>TrueTypeFont_Dev</code> object.
99 /// A constructor used to create a <code>TrueTypeFont_Dev</code> and associate
100 /// it with the provided <code>Instance</code>.
102 /// @param[in] instance The instance that owns this resource.
103 TrueTypeFont_Dev(const InstanceHandle& instance,
104 const TrueTypeFontDesc_Dev& desc);
106 /// The copy constructor for <code>TrueTypeFont_Dev</code>.
108 /// @param[in] other A reference to a <code>TrueTypeFont_Dev</code>.
109 TrueTypeFont_Dev(const TrueTypeFont_Dev& other);
111 /// A constructor used when you have received a PP_Resource as a return
112 /// value that has had its reference count incremented for you.
114 /// @param[in] resource A PP_Resource corresponding to a TrueType font.
115 TrueTypeFont_Dev(PassRef, PP_Resource resource);
117 /// Gets an array of TrueType font family names available on the host.
118 /// These names can be used to create a font from a specific family.
120 /// @param[in] instance A <code>InstanceHandle</code> requesting the family
122 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
123 /// called upon completion of GetFontFamilies.
125 /// @return If >= 0, the number of family names returned, otherwise an error
126 /// code from <code>pp_errors.h</code>.
127 static int32_t GetFontFamilies(
128 const InstanceHandle& instance,
129 const CompletionCallbackWithOutput<std::vector<Var> >& callback);
131 /// Gets an array of TrueType font descriptors for a given font family. These
132 /// descriptors can be used to create a font in that family and matching the
133 /// descriptor attributes.
135 /// @param[in] instance A <code>PP_Instance</code> requesting the font
137 /// @param[in] family A <code>Var</code> holding a string specifying the font
139 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
140 /// called upon completion of GetFontsInFamily.
142 /// @return If >= 0, the number of font descriptors returned, otherwise an
143 /// error code from <code>pp_errors.h</code>.
144 static int32_t GetFontsInFamily(
145 const InstanceHandle& instance,
147 const CompletionCallbackWithOutput<std::vector<TrueTypeFontDesc_Dev> >&
150 /// Returns a description of the given font resource. This description may
151 /// differ from the description passed to Create, reflecting the host's font
152 /// matching and fallback algorithm.
154 /// @param[in] callback A <code>CompletionCallback</code> to be called upon
155 /// completion of Describe.
157 /// @return A return code from <code>pp_errors.h</code>. If an error code is
158 /// returned, the descriptor will be left unchanged.
160 const CompletionCallbackWithOutput<TrueTypeFontDesc_Dev>& callback);
162 /// Gets an array of identifying tags for each table in the font.
163 /// These tags can be used to request specific tables using GetTable.
165 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
166 /// called upon completion of GetTableTags.
168 /// @return If >= 0, the number of table tags returned, otherwise an error
169 /// code from <code>pp_errors.h</code>.
170 int32_t GetTableTags(
171 const CompletionCallbackWithOutput<std::vector<uint32_t> >& callback);
173 /// Copies the given font table into client memory.
175 /// @param[in] table A 4 byte value indicating which table to copy.
176 /// For example, 'glyf' will cause the outline table to be copied into the
177 /// output array. A zero tag value will cause the entire font to be copied.
178 /// @param[in] offset The offset into the font table.
179 /// @param[in] max_data_length The maximum number of bytes to transfer from
180 /// <code>offset</code>.
181 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
182 /// called upon completion of GetTable.
184 /// @return If >= 0, the table size in bytes, otherwise an error code from
185 /// <code>pp_errors.h</code>.
189 int32_t max_data_length,
190 const CompletionCallbackWithOutput<std::vector<char> >& callback);
195 // A specialization of CallbackOutputTraits to provide the callback system the
196 // information on how to handle pp::TrueTypeFontDesc_Dev. This converts
197 // PP_TrueTypeFontDesc_Dev to pp::TrueTypeFontDesc_Dev when passing to the
198 // plugin, and specifically manages the PP_Var embedded in the desc_ field.
200 struct CallbackOutputTraits<TrueTypeFontDesc_Dev> {
201 typedef PP_TrueTypeFontDesc_Dev* APIArgType;
202 typedef PP_TrueTypeFontDesc_Dev StorageType;
204 static inline APIArgType StorageToAPIArg(StorageType& t) {
208 static inline TrueTypeFontDesc_Dev StorageToPluginArg(StorageType& t) {
209 return TrueTypeFontDesc_Dev(PASS_REF, t);
212 static inline void Initialize(StorageType* t) {
213 // Use the same defaults as TrueTypeFontDesc_Dev does.
214 TrueTypeFontDesc_Dev dummy;
215 *t = dummy.pp_desc();
219 class TrueTypeFontDescArrayOutputAdapterWithStorage
220 : public ArrayOutputAdapter<PP_TrueTypeFontDesc_Dev> {
222 TrueTypeFontDescArrayOutputAdapterWithStorage() {
223 set_output(&temp_storage_);
226 virtual ~TrueTypeFontDescArrayOutputAdapterWithStorage() {
227 if (!temp_storage_.empty()) {
228 // An easy way to release the resource references held by |temp_storage_|.
233 std::vector<TrueTypeFontDesc_Dev>& output() {
234 PP_DCHECK(output_storage_.empty());
235 typedef std::vector<PP_TrueTypeFontDesc_Dev> Entries;
236 for (Entries::iterator it = temp_storage_.begin();
237 it != temp_storage_.end(); ++it)
238 output_storage_.push_back(TrueTypeFontDesc_Dev(PASS_REF, *it));
239 temp_storage_.clear();
240 return output_storage_;
244 std::vector<PP_TrueTypeFontDesc_Dev> temp_storage_;
245 std::vector<TrueTypeFontDesc_Dev> output_storage_;
248 // A specialization of CallbackOutputTraits to provide the callback system the
249 // information on how to handle vectors of TrueTypeFontDesc_Dev. This converts
250 // PP_TrueTypeFontDesc_Dev to TrueTypeFontDesc_Dev when passing to the plugin.
252 struct CallbackOutputTraits< std::vector<TrueTypeFontDesc_Dev> > {
253 typedef PP_ArrayOutput APIArgType;
254 typedef TrueTypeFontDescArrayOutputAdapterWithStorage StorageType;
256 static inline APIArgType StorageToAPIArg(StorageType& t) {
257 return t.pp_array_output();
260 static inline std::vector<TrueTypeFontDesc_Dev>& StorageToPluginArg(
265 static inline void Initialize(StorageType* /* t */) {}
268 } // namespace internal
272 #endif // PPAPI_CPP_TRUETYPE_FONT_H_