Bitmap Font support for TextLabel.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / text / bitmap-font.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-toolkit/devel-api/text/bitmap-font.h>
20
21 // EXTERNAL INCLUDE
22 #include <dali/devel-api/text-abstraction/bitmap-font.h>
23 #include <cstring>
24
25 // INTERNAL INCLUDE
26 #include <dali-toolkit/internal/text/character-set-conversion.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace DevelText
35 {
36
37 Glyph::Glyph()
38 : url{},
39   utf8{},
40   ascender{ 0.f },
41   descender{ 0.f }
42 {}
43
44 Glyph::Glyph( const std::string& url, const std::string utf8Character, float ascender, float descender )
45 : url{ url },
46   utf8{},
47   ascender{ ascender },
48   descender{ descender }
49 {
50   DALI_ASSERT_DEBUG( utf8Character.size() <= 4u );
51
52   std::copy( utf8Character.begin(), utf8Character.end(), utf8 );
53 }
54
55 Glyph::~Glyph()
56 {}
57
58 BitmapFontDescription::BitmapFontDescription()
59 : glyphs{},
60   name{},
61   underlinePosition{ 0.f },
62   underlineThickness{ 1.f }
63 {}
64
65 BitmapFontDescription::~BitmapFontDescription()
66 {}
67
68 void CreateBitmapFont( const BitmapFontDescription& description, TextAbstraction::BitmapFont& bitmapFont )
69 {
70   bitmapFont.glyphs.reserve( description.glyphs.size() );
71   bitmapFont.name = description.name;
72   bitmapFont.underlinePosition = description.underlinePosition;
73   bitmapFont.underlineThickness = description.underlineThickness;
74   bitmapFont.isColorFont = description.isColorFont;
75
76   for( const auto& glyph : description.glyphs )
77   {
78     uint32_t c = 0u;
79     Text::Utf8ToUtf32( glyph.utf8, Text::GetUtf8Length( glyph.utf8[0u] ), &c );
80
81     TextAbstraction::BitmapGlyph bitmapGlyph( glyph.url, c, glyph.ascender, glyph.descender );
82
83     bitmapFont.glyphs.push_back( std::move( bitmapGlyph ) );
84   }
85 }
86
87 } // namespace DevelText
88
89 } // namespace Toolkit
90
91 } // namespace Dali