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