Moved Core Rendering API from devel-api to public-api
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / vector-based / vector-blob-atlas.h
1 #ifndef DALI_TOOLKIT_TEXT_VECTOR_BLOB_ATLAS_H
2 #define DALI_TOOLKIT_TEXT_VECTOR_BLOB_ATLAS_H
3
4 /*
5  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
21 #include <dali/public-api/images/buffer-image.h>
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/public-api/rendering/shader.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/devel-api/text-abstraction/text-abstraction-definitions.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/text/text-definitions.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Text
37 {
38
39 typedef Dali::TextAbstraction::VectorBlob VectorBlob;
40
41 struct BlobCoordinate
42 {
43   float u;
44   float v;
45 };
46
47 /**
48  * @brief An atlas for vector blob data
49  *
50  */
51 class VectorBlobAtlas : public RefObject
52 {
53 public:
54
55   /**
56    * @brief Create a blob atlas.
57    *
58    * @param[in] textureWidth The atlas width.
59    * @param[in] textureHeight The atlas height.
60    * @param[in] itemWidth The width of an item in the atlas.
61    * @param[in] itemHeightQuantum The item height quantum.
62    * When blobs are added to columns in the atlas, the Y position is advanced by a multiple of this value.
63    */
64   VectorBlobAtlas( unsigned int textureWidth,
65                    unsigned int textureHeight,
66                    unsigned int itemWidth,
67                    unsigned int itemHeightQuantum );
68
69   /**
70    * @brief Query whether the atlas is full.
71    *
72    * @return True if the atlas is full.
73    */
74   bool IsFull() const;
75
76   /**
77    * @brief Find the UV coordinates for a glyph in the atlas.
78    *
79    * @param[in] fontId The ID of the font containing the glyph.
80    * @param[in] glyphIndex The index of the glyph within the font.
81    * @param[out] coords If the glyph was found, an array of 4 UV coordinates will be returned.
82    * Otherwise coords will not be written into.
83    * @return True if the glyph was found.
84    */
85   bool FindGlyph( FontId fontId,
86                   GlyphIndex glyphIndex,
87                   BlobCoordinate* coords );
88
89   /**
90    * @brief Add a glyph to the atlas.
91    *
92    * @param[in] fontId The ID of the font containing the glyph.
93    * @param[in] glyphIndex The index of the glyph within the font.
94    * @param[in] blobData A blob of vector data representing the glyph.
95    * @param[in] length The length of the blob data.
96    * @param[in] nominalWidth The width of the blob.
97    * @param[in] nominalHeight The height of the blob.
98    * @param[out] coords An array of 4 UV coordinates will be returned.
99    * @return True if the glyph was added. Otherwise the atlas is now full.
100    */
101   bool AddGlyph( unsigned int fontId,
102                  unsigned int glyphIndex,
103                  VectorBlob* blob,
104                  unsigned int length,
105                  unsigned int nominalWidth,
106                  unsigned int nominalHeight,
107                  BlobCoordinate* coords );
108
109   /**
110    * @brief Get the info required by the GLyphy shader.
111    *
112    * @return The shader uniform value.
113    */
114   Vector4 GetInfo() const
115   {
116     return Vector4( mTextureWidth, mTextureHeight, mItemWidth, mItemHeightQuantum );
117   }
118
119   /**
120    * @brief Retrieve the atlas texture.
121    *
122    * @return The texture used for rendering.
123    */
124   TextureSet GetTextureSet()
125   {
126     return mTextureSet;
127   }
128
129 private:
130
131   /**
132    * @brief Helper for uploading data to the texture atlas.
133    *
134    * @param[in] offsetX The x position within the atlas.
135    * @param[in] offsetY The y position within the atlas.
136    * @param[in] width The width of the data to upload.
137    * @param[in] height The height of the data to upload.
138    * @param[in] blob The blob of data to upload.
139    */
140   void TexSubImage( unsigned int offsetX,
141                     unsigned int offsetY,
142                     unsigned int width,
143                     unsigned int height,
144                     VectorBlob* blob );
145
146 private:
147
148   unsigned int mTextureWidth;
149   unsigned int mTextureHeight;
150
151   unsigned int mItemWidth;
152   unsigned int mItemHeightQuantum;
153
154   unsigned int mCursorX;
155   unsigned int mCursorY;
156
157   BufferImage mAtlasTexture;
158
159   TextureSet mTextureSet;
160
161   struct Key
162   {
163     unsigned int fontId;
164     unsigned int glyphIndex;
165     unsigned int cacheIndex;
166   };
167
168   struct Item
169   {
170     BlobCoordinate coords[4];
171   };
172
173   std::vector< Key >  mItemLookup;
174   std::vector< Item > mItemCache;
175
176   bool mIsFull;
177 };
178
179 } // namespace Text
180
181 } // namespace Toolkit
182
183 } // namespace Dali
184
185 #endif // DALI_TOOLKIT_TEXT_VECTOR_BLOB_ATLAS_H