Refactored font-client-plugin-impl
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / plugin / bitmap-font-cache-item.cpp
1
2 /*
3  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <dali/internal/text/text-abstraction/plugin/bitmap-font-cache-item.h>
19
20 #include <dali/devel-api/adaptor-framework/image-loading.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/internal/text/text-abstraction/plugin/font-client-utils.h>
23
24 #if defined(DEBUG_ENABLED)
25 extern Dali::Integration::Log::Filter* gFontClientLogFilter;
26 #endif
27
28 namespace Dali::TextAbstraction::Internal
29 {
30 BitmapFontCacheItem::BitmapFontCacheItem(const BitmapFont& bitmapFont, FontId fontId)
31 : font(bitmapFont),
32   id(fontId)
33 {
34   // Resize the vector with the pixel buffers.
35   pixelBuffers.resize(bitmapFont.glyphs.size());
36
37   // Traverse all the glyphs and load the pixel buffer of those with ascender and descender equal to zero.
38   unsigned int index = 0u;
39   for(auto& glyph : font.glyphs)
40   {
41     Devel::PixelBuffer& pixelBuffer = pixelBuffers[index];
42
43     if(EqualsZero(glyph.ascender) && EqualsZero(glyph.descender))
44     {
45       // Load the glyph.
46       pixelBuffer = LoadImageFromFile(glyph.url);
47
48       if(pixelBuffer)
49       {
50         glyph.ascender = static_cast<float>(pixelBuffer.GetHeight());
51       }
52     }
53
54     font.ascender  = std::max(glyph.ascender, font.ascender);
55     font.descender = std::min(glyph.descender, font.descender);
56
57     ++index;
58   }
59 }
60
61 void BitmapFontCacheItem::GetFontMetrics(FontMetrics& metrics, unsigned int dpiVertical) const
62 {
63   metrics.ascender           = font.ascender;
64   metrics.descender          = font.descender;
65   metrics.height             = metrics.ascender - metrics.descender;
66   metrics.underlinePosition  = font.underlinePosition;
67   metrics.underlineThickness = font.underlineThickness;
68 }
69
70 bool BitmapFontCacheItem::GetGlyphMetrics(GlyphInfo& glyph, unsigned int dpiVertical, bool horizontal) const
71 {
72   bool success(false);
73
74   unsigned int index = 0u;
75   for(auto& item : font.glyphs)
76   {
77     if(item.utf32 == glyph.index)
78     {
79       Devel::PixelBuffer& pixelBuffer = const_cast<Devel::PixelBuffer&>(pixelBuffers[index]);
80       if(!pixelBuffer)
81       {
82         pixelBuffer = LoadImageFromFile(item.url);
83       }
84
85       glyph.width       = static_cast<float>(pixelBuffer.GetWidth());
86       glyph.height      = static_cast<float>(pixelBuffer.GetHeight());
87       glyph.xBearing    = 0.f;
88       glyph.yBearing    = glyph.height + item.descender;
89       glyph.advance     = glyph.width;
90       glyph.scaleFactor = 1.f;
91       success           = true;
92       break;
93     }
94     ++index;
95   }
96   return success;
97 }
98
99 void BitmapFontCacheItem::CreateBitmap(
100   GlyphIndex glyphIndex, Dali::TextAbstraction::FontClient::GlyphBufferData& data, int outlineWidth, bool isItalicRequired, bool isBoldRequired) const
101 {
102   unsigned int index = 0u;
103   for(auto& item : font.glyphs)
104   {
105     if(item.utf32 == glyphIndex)
106     {
107       Devel::PixelBuffer& pixelBuffer = const_cast<Devel::PixelBuffer&>(pixelBuffers[index]);
108       if(!pixelBuffer)
109       {
110         pixelBuffer = LoadImageFromFile(item.url);
111       }
112
113       data.width  = pixelBuffer.GetWidth();
114       data.height = pixelBuffer.GetHeight();
115
116       data.isColorBitmap = font.isColorFont;
117
118       ConvertBitmap(data, data.width, data.height, pixelBuffer.GetBuffer());
119
120       // Sets the pixel format.
121       data.format = pixelBuffer.GetPixelFormat();
122       break;
123     }
124     ++index;
125   }
126 }
127
128 bool BitmapFontCacheItem::IsCharacterSupported(Character character)
129 {
130   for(const auto& glyph : font.glyphs)
131   {
132     if(glyph.utf32 == character)
133     {
134       return true;
135     }
136   }
137   return false;
138 }
139
140 } // namespace Dali::TextAbstraction::Internal