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