New size negotiation
[platform/core/uifw/dali-core.git] / dali / integration-api / glyph-set.h
1 #ifndef __DALI_INTEGRATION_PLATFORM_FONT_H__
2 #define __DALI_INTEGRATION_PLATFORM_FONT_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <stdint.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/bitmap.h>
28 #include <dali/integration-api/resource-declarations.h>
29
30 namespace Dali
31 {
32
33 namespace Integration
34 {
35 class GlyphSet;
36 typedef IntrusivePtr<GlyphSet> GlyphSetPointer;
37
38 /**
39  * A Glyph holds information for a single character.
40  */
41 struct GlyphMetrics
42 {
43   enum GlyphQuality
44   {
45     LOW_QUALITY = 0x0,
46     HIGH_QUALITY = 0x1
47   };
48
49   uint32_t     code:21;   ///< character code (UTF-32), max value of 0x10ffff (21 bits)
50   uint32_t     quality:1; ///< 0 = low quality, 1 = high quality
51   float        width;     ///< glyph width in pixels
52   float        height;    ///< glyph height in pixels
53   float        top;       ///< distance between glyph's tallest pixel and baseline
54   float        left;      ///< where to place the glyph horizontally in relation to current 'pen' position
55   float        xAdvance;  ///< distance in pixels to move the 'pen' after displaying the character
56   unsigned int xPosition; ///< x position in target atlas texture
57   unsigned int yPosition; ///< y position in target atlas texture
58 };
59
60 /**
61  * Stores font global metrics.
62  */
63 struct GlobalMetrics
64 {
65   GlobalMetrics()
66   : lineHeight( 0.f ),
67     ascender( 0.f ),
68     unitsPerEM( 0.f ),
69     underlinePosition( 0.f ),
70     underlineThickness( 0.f ),
71     padAdjustX( 0.f ),
72     padAdjustY( 0.f ),
73     maxWidth( 0.f ),
74     maxHeight( 0.f )
75   {}
76
77   GlobalMetrics( float lh, float asc, float upem, float up, float ut, float pax, float pay, float mw, float mh )
78   : lineHeight( lh ),
79     ascender( asc ),
80     unitsPerEM( upem ),
81     underlinePosition( up ),
82     underlineThickness( ut ),
83     padAdjustX( pax ),
84     padAdjustY( pay ),
85     maxWidth( mw ),
86     maxHeight ( mh )
87   {}
88
89   float lineHeight;         ///< Distance between baselines
90   float ascender;           ///< Distance from baseline to top of cell
91   float unitsPerEM;         ///< font units/EM
92   float underlinePosition;  ///< Underline distance from baseline
93   float underlineThickness; ///< Underline thickness
94   float padAdjustX;         ///< X adjustment value for padding around distance field
95   float padAdjustY;         ///< Y adjustment value for padding around distance field
96   float maxWidth;           ///< Width of widest glyph
97   float maxHeight;          ///< Height of tallest glyph
98 };
99
100 /**
101  *
102  * Platform font class.
103  * A container for font data; consisting of some metrics and a list of bitmaps
104  */
105 class DALI_IMPORT_API GlyphSet : public Dali::RefObject
106 {
107 public:
108   typedef std::pair<BitmapPtr, GlyphMetrics>  Character;
109   typedef IntrusivePtr<Character>             CharacterPtr;
110   typedef std::vector<Character>              CharacterList;
111   typedef CharacterList::iterator             CharacterIter;
112   typedef CharacterList::const_iterator       CharacterConstIter;
113
114   /**
115    * Constructor.
116    */
117   GlyphSet();
118
119   /**
120    * Destructor.
121    */
122   ~GlyphSet();
123
124   /**
125    * Add a character to the platform font
126    * @param [in] bitmapData   A bitmap of the rendered character
127    * @param [in] glyphMetrics Metrics for the character, including its character code
128    */
129   void AddCharacter(BitmapPtr bitmapData, const GlyphMetrics& glyphMetrics);
130
131   /**
132    * Add a character to the platform font
133    * @param [in] character   The Character object
134    */
135   void AddCharacter(const Character& character);
136
137   /**
138    * Get the list of characters in the font
139    * @return The list of characters
140    */
141   const CharacterList& GetCharacterList() const;
142
143   /**
144    * Checks if the character is contained in the GlyphSet
145    * @param [in] charCode The character to search for.
146    * @return true if the character is contained in the GlyphSet
147    */
148   bool HasCharacter(const uint32_t charCode) const;
149
150   /**
151    * Checks if the character is contained in the GlyphSet
152    * @param [in] character The character to search for.
153    * @return true if the character is contained in the GlyphSet
154    */
155   bool HasCharacter(const Character& character) const;
156
157   /**
158    * Returns the resource ID of the texture atlas these bitmaps will be written to
159    * @return the resource id
160    */
161   ResourceId GetAtlasResourceId() const;
162
163   /**
164    * Sets the resource ID of the texture atlas these bitmaps will be written to
165    * @param[in] resourceId the resource identifier of the texture atlas.
166    */
167   void SetAtlasResourceId(ResourceId resourceId);
168
169   size_t        mFontHash;            ///< hash of the fontname the glyphs were loaded for
170   float         mLineHeight;          ///< Distance between baselines
171   float         mAscender;            ///< Distance from baseline to top of cell
172   float         mUnitsPerEM;          ///< font units/EM
173   float         mUnderlinePosition;   ///< Underline distance from baseline
174   float         mUnderlineThickness;  ///< Underline thickness
175   float         mPadAdjust;           ///< Adjustment value for padding around distance field
176
177 private:
178   CharacterList mCharacterList;
179   ResourceId    mAtlasId;             ///< Resource ID of target texture
180 };
181
182 } // namespace Integration
183
184 } // namespace Dali
185
186 #endif // __DALI_INTEGRATION_PLATFORM_FONT_H__