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