da7bcdfcd1ed73a20a8d7288a48840e6aad96414
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / text / visual-model.cpp
1 /*
2  * Copyright (c) 2015 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 // CLASS HEADER
19 #include <dali-toolkit/public-api/text/visual-model.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/math/vector2.h>
24
25 // EXTERNAL INCLUDES
26 #include <memory.h>
27 #include <vector>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Text
36 {
37
38 struct VisualModel::Impl
39 {
40   Vector<GlyphInfo>      mGlyphs;
41   Vector<CharacterIndex> mGlyphsToCharacters;
42   Vector<Length>         mCharactersPerGlyph;
43   std::vector<Vector2>   mGlyphPositions;
44 };
45
46 VisualModelPtr VisualModel::New()
47 {
48   return VisualModelPtr( new VisualModel() );
49 }
50
51 void VisualModel::SetGlyphs( const GlyphInfo* glyphs,
52                              const CharacterIndex* characterIndices,
53                              const Length* charactersPerGlyph,
54                              Length numberOfGlyphs )
55 {
56   Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
57   modelGlyphs.Resize( numberOfGlyphs );
58   memcpy( &modelGlyphs[0], glyphs, numberOfGlyphs*sizeof(GlyphInfo) );
59
60   Vector<CharacterIndex>& glyphsToCharacters = mImpl->mGlyphsToCharacters;
61   glyphsToCharacters.Resize( numberOfGlyphs );
62   memcpy( &glyphsToCharacters[0], characterIndices, numberOfGlyphs*sizeof(CharacterIndex) );
63
64   Vector<Length>& modelCharactersPerGlyph = mImpl->mCharactersPerGlyph;
65   modelCharactersPerGlyph.Resize( numberOfGlyphs );
66   memcpy( &modelCharactersPerGlyph[0], charactersPerGlyph, numberOfGlyphs*sizeof(Length) );
67 }
68
69 Length VisualModel::GetNumberOfGlyphs() const
70 {
71   return mImpl->mGlyphs.Count();
72 }
73
74 void VisualModel::GetGlyphs( GlyphIndex glyphIndex,
75                              GlyphInfo* glyphs,
76                              Length numberOfGlyphs ) const
77 {
78   Vector<GlyphInfo>& modelGlyphs = mImpl->mGlyphs;
79   memcpy( glyphs, &modelGlyphs[glyphIndex], numberOfGlyphs*sizeof(GlyphInfo) );
80 }
81
82 CharacterIndex VisualModel::GetCharacterIndex( GlyphIndex glyphIndex ) const
83 {
84   return mImpl->mGlyphsToCharacters[glyphIndex];
85 }
86
87 Length VisualModel::GetCharactersPerGlyph( GlyphIndex glyphIndex ) const
88 {
89   return mImpl->mCharactersPerGlyph[glyphIndex];
90 }
91
92 GlyphIndex VisualModel::GetGlyphIndex( CharacterIndex characterIndex ) const
93 {
94   GlyphIndex index( 0 );
95
96   for( unsigned int i=0; i<mImpl->mGlyphsToCharacters.Count(); ++i )
97   {
98     if( mImpl->mGlyphsToCharacters[i] == characterIndex )
99     {
100       index = i;
101       break;
102     }
103   }
104
105   return index;
106 }
107
108 void VisualModel::SetGlyphPositions( const Vector2* glyphPositions,
109                                      Length numberOfGlyphs )
110 {
111   std::vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
112   modelPositions.resize( numberOfGlyphs );
113   memcpy( &modelPositions[0], glyphPositions, numberOfGlyphs*sizeof(Vector2) );
114 }
115
116 void VisualModel::GetGlyphPositions( GlyphIndex glyphIndex,
117                                      Vector2* glyphPositions,
118                                      Length numberOfGlyphs ) const
119 {
120   std::vector<Vector2>& modelPositions = mImpl->mGlyphPositions;
121   memcpy( glyphPositions, &modelPositions[0], numberOfGlyphs*sizeof(Vector2) );
122 }
123
124 VisualModel::~VisualModel()
125 {
126   delete mImpl;
127 }
128
129 VisualModel::VisualModel()
130 : mImpl( NULL )
131 {
132   mImpl = new VisualModel::Impl();
133 }
134
135 } // namespace Text
136
137 } // namespace Toolkit
138
139 } // namespace Dali