2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/public-api/text/text-controller.h>
22 #include <dali-toolkit/public-api/text/character-set-conversion.h>
23 #include <dali-toolkit/public-api/text/logical-model.h>
24 #include <dali-toolkit/public-api/text/text-view.h>
25 #include <dali-toolkit/public-api/text/visual-model.h>
36 struct Controller::Impl
40 mLogicalModel = LogicalModel::New();
41 mVisualModel = VisualModel::New();
43 mView.SetVisualModel( mVisualModel );
45 mFontClient = TextAbstraction::FontClient::Get();
48 LogicalModelPtr mLogicalModel;
49 VisualModelPtr mVisualModel;
53 TextAbstraction::FontClient mFontClient;
56 ControllerPtr Controller::New()
58 return ControllerPtr( new Controller() );
61 void Controller::SetText( const std::string& text )
63 // Convert text into UTF-32
64 Vector<Character> utf32Characters;
65 utf32Characters.Resize( text.size() );
67 // This is a bit horrible but std::string returns a (signed) char*
68 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
70 Length characterCount = Utf8ToUtf32( utf8, text.size(), &utf32Characters[0] );
72 // Manipulate the logical model
73 mImpl->mLogicalModel->SetText( &utf32Characters[0], characterCount );
78 View& Controller::GetView()
83 Controller::~Controller()
88 Controller::Controller()
91 mImpl = new Controller::Impl();
94 // TODO - Move this with LayoutEngine
95 void Controller::UpdateVisualModel()
97 if( mImpl->mLogicalModel &&
100 const LogicalModel& logicalModel = *(mImpl->mLogicalModel);
101 VisualModel& visualModel = *(mImpl->mVisualModel);
103 TextAbstraction::FontId fontId = mImpl->mFontClient.GetFontId( "/usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf", 13*64 );
105 const Length characterCount = logicalModel.GetNumberOfCharacters();
107 Vector<GlyphInfo> glyphs;
108 glyphs.Reserve( characterCount );
110 Vector<CharacterIndex> characterIndices;
111 characterIndices.Reserve( characterCount );
113 std::vector<Length> charactersPerGlyph;
114 charactersPerGlyph.assign( characterCount, 1 );
116 for( unsigned int i=0; i<characterCount; ++i )
119 logicalModel.GetText( i, &charcode, 1 );
121 // TODO - Perform shaping to get correct glyph indices
122 GlyphIndex glyphIndex = mImpl->mFontClient.GetGlyphIndex( fontId, charcode );
124 glyphs.PushBack( GlyphInfo(fontId, glyphIndex) );
125 characterIndices.PushBack( 1 );
128 if( mImpl->mFontClient.GetGlyphMetrics( &glyphs[0], glyphs.Size() ) )
130 visualModel.SetGlyphs( &glyphs[0],
131 &characterIndices[0],
132 &charactersPerGlyph[0],
135 UpdateVisualPositions();
140 // TODO - Move this with LayoutEngine
141 void Controller::UpdateVisualPositions()
143 if( mImpl->mVisualModel )
145 VisualModel& visualModel = *(mImpl->mVisualModel);
147 Length glyphCount = visualModel.GetNumberOfGlyphs();
149 std::vector<Vector2> glyphPositions;
150 glyphPositions.reserve( glyphCount );
154 // FIXME Single font assumption
155 Text::FontMetrics fontMetrics;
156 GlyphInfo firstGlyph;
157 visualModel.GetGlyphs( 0, &firstGlyph, 1 );
158 mImpl->mFontClient.GetFontMetrics( firstGlyph.fontId, fontMetrics );
161 float penY( fontMetrics.ascender ); // Move to baseline
163 for( unsigned int i=0; i<glyphCount; ++i )
166 visualModel.GetGlyphs( i, &glyph, 1 );
168 glyphPositions.push_back( Vector2( penX + glyph.xBearing,
169 penY - glyph.yBearing ) );
171 penX += glyph.advance;
174 visualModel.SetGlyphPositions( &glyphPositions[0], glyphCount );
181 } // namespace Toolkit