- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / dev / truetype_font_dev.h
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.
4
5 #ifndef PPAPI_CPP_TRUETYPE_FONT_H_
6 #define PPAPI_CPP_TRUETYPE_FONT_H_
7
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"
13
14 /// @file
15 /// This file defines the API to create a TrueType font object.
16
17 namespace pp {
18
19 class InstanceHandle;
20
21 // TrueTypeFontDesc_Dev --------------------------------------------------------
22
23 /// The <code>TrueTypeFontDesc_Dev</code> class represents a TrueType font
24 /// descriptor, used to Create and Describe fonts.
25 class TrueTypeFontDesc_Dev {
26  public:
27   /// Default constructor for creating a <code>TrueTypeFontDesc_Dev</code>
28   /// object.
29   TrueTypeFontDesc_Dev();
30
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
33   /// this instance.
34   TrueTypeFontDesc_Dev(PassRef, const PP_TrueTypeFontDesc_Dev& pp_desc);
35
36   /// The copy constructor for <code>TrueTypeFontDesc_Dev</code>.
37   ///
38   /// @param[in] other A reference to a <code>TrueTypeFontDesc_Dev</code>.
39   TrueTypeFontDesc_Dev(const TrueTypeFontDesc_Dev& other);
40
41   ~TrueTypeFontDesc_Dev();
42
43   TrueTypeFontDesc_Dev& operator=(const TrueTypeFontDesc_Dev& other);
44
45   const PP_TrueTypeFontDesc_Dev& pp_desc() const {
46     return desc_;
47   }
48
49   Var family() const {
50     return family_;
51   }
52   void set_family(const Var& family) {
53     family_ = 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();
57   }
58
59   PP_TrueTypeFontFamily_Dev generic_family() const {
60     return desc_.generic_family;
61   }
62   void set_generic_family(PP_TrueTypeFontFamily_Dev family) {
63     desc_.generic_family = family;
64   }
65
66   PP_TrueTypeFontStyle_Dev style() const { return desc_.style; }
67   void set_style(PP_TrueTypeFontStyle_Dev style) {
68     desc_.style = style;
69   }
70
71   PP_TrueTypeFontWeight_Dev weight() const { return desc_.weight; }
72   void set_weight(PP_TrueTypeFontWeight_Dev weight) {
73     desc_.weight = weight;
74   }
75
76   PP_TrueTypeFontWidth_Dev width() const { return desc_.width; }
77   void set_width(PP_TrueTypeFontWidth_Dev width) {
78     desc_.width = width;
79   }
80
81   PP_TrueTypeFontCharset_Dev charset() const { return desc_.charset; }
82   void set_charset(PP_TrueTypeFontCharset_Dev charset) {
83     desc_.charset = charset;
84   }
85
86  private:
87   pp::Var family_;  // This manages the PP_Var embedded in desc_.
88   PP_TrueTypeFontDesc_Dev desc_;
89 };
90
91 // TrueTypeFont_Dev ------------------------------------------------------------
92
93 /// The <code>TrueTypeFont_Dev</code> class represents a TrueType font resource.
94 class TrueTypeFont_Dev : public Resource {
95  public:
96   /// Default constructor for creating a <code>TrueTypeFont_Dev</code> object.
97   TrueTypeFont_Dev();
98
99   /// A constructor used to create a <code>TrueTypeFont_Dev</code> and associate
100   /// it with the provided <code>Instance</code>.
101   ///
102   /// @param[in] instance The instance that owns this resource.
103   TrueTypeFont_Dev(const InstanceHandle& instance,
104                    const TrueTypeFontDesc_Dev& desc);
105
106   /// The copy constructor for <code>TrueTypeFont_Dev</code>.
107   ///
108   /// @param[in] other A reference to a <code>TrueTypeFont_Dev</code>.
109   TrueTypeFont_Dev(const TrueTypeFont_Dev& other);
110
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.
113   ///
114   /// @param[in] resource A PP_Resource corresponding to a TrueType font.
115   TrueTypeFont_Dev(PassRef, PP_Resource resource);
116
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.
119   ///
120   /// @param[in] instance A <code>InstanceHandle</code> requesting the family
121   /// names.
122   /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
123   /// called upon completion of GetFontFamilies.
124   ///
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);
130
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.
134   ///
135   /// @param[in] instance A <code>PP_Instance</code> requesting the font
136   /// descriptors.
137   /// @param[in] family A <code>Var</code> holding a string specifying the font
138   /// family.
139   /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
140   /// called upon completion of GetFontsInFamily.
141   ///
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,
146       const Var& family,
147       const CompletionCallbackWithOutput<std::vector<TrueTypeFontDesc_Dev> >&
148           callback);
149
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.
153   ///
154   /// @param[in] callback A <code>CompletionCallback</code> to be called upon
155   /// completion of Describe.
156   ///
157   /// @return A return code from <code>pp_errors.h</code>. If an error code is
158   /// returned, the descriptor will be left unchanged.
159   int32_t Describe(
160       const CompletionCallbackWithOutput<TrueTypeFontDesc_Dev>& callback);
161
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.
164   ///
165   /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
166   /// called upon completion of GetTableTags.
167   ///
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);
172
173   /// Copies the given font table into client memory.
174   ///
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.
183   ///
184   /// @return If >= 0, the table size in bytes, otherwise an error code from
185   /// <code>pp_errors.h</code>.
186   int32_t GetTable(
187       uint32_t table,
188       int32_t offset,
189       int32_t max_data_length,
190       const CompletionCallbackWithOutput<std::vector<char> >& callback);
191 };
192
193 namespace internal {
194
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.
199 template<>
200 struct CallbackOutputTraits<TrueTypeFontDesc_Dev> {
201   typedef PP_TrueTypeFontDesc_Dev* APIArgType;
202   typedef PP_TrueTypeFontDesc_Dev StorageType;
203
204   static inline APIArgType StorageToAPIArg(StorageType& t) {
205     return &t;
206   }
207
208   static inline TrueTypeFontDesc_Dev StorageToPluginArg(StorageType& t) {
209     return TrueTypeFontDesc_Dev(PASS_REF, t);
210   }
211
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();
216   }
217 };
218
219 class TrueTypeFontDescArrayOutputAdapterWithStorage
220     : public ArrayOutputAdapter<PP_TrueTypeFontDesc_Dev> {
221  public:
222   TrueTypeFontDescArrayOutputAdapterWithStorage() {
223     set_output(&temp_storage_);
224   };
225
226   virtual ~TrueTypeFontDescArrayOutputAdapterWithStorage() {
227     if (!temp_storage_.empty()) {
228       // An easy way to release the resource references held by |temp_storage_|.
229       output();
230     }
231   };
232
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_;
241   }
242
243  private:
244   std::vector<PP_TrueTypeFontDesc_Dev> temp_storage_;
245   std::vector<TrueTypeFontDesc_Dev> output_storage_;
246 };
247
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.
251 template<>
252 struct CallbackOutputTraits< std::vector<TrueTypeFontDesc_Dev> > {
253   typedef PP_ArrayOutput APIArgType;
254   typedef TrueTypeFontDescArrayOutputAdapterWithStorage StorageType;
255
256   static inline APIArgType StorageToAPIArg(StorageType& t) {
257     return t.pp_array_output();
258   }
259
260   static inline std::vector<TrueTypeFontDesc_Dev>& StorageToPluginArg(
261       StorageType& t) {
262     return t.output();
263   }
264
265   static inline void Initialize(StorageType* /* t */) {}
266 };
267
268 }  // namespace internal
269
270 }  // namespace pp
271
272 #endif  // PPAPI_CPP_TRUETYPE_FONT_H_