[dali_1.0.31] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / event / text / text-request-helper.cpp
1 /*
2  * Copyright (c) 2014 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 "text-request-helper.h"
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/text/font-impl.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 TextRequestHelper::TextRequestHelper( GlyphTextureObserver& observer)
31 :mTextureId(0),
32  mTextureObserverInstalled( false ),
33  mTextureObserver( observer )
34 {
35 }
36
37 TextRequestHelper::~TextRequestHelper()
38 {
39   TextNotRequired();
40
41   if( mTextureObserverInstalled && mFont )
42   {
43     mFont->RemoveTextureObserver( mTextureObserver);
44   }
45 }
46
47 TextVertexBuffer* TextRequestHelper::SetText(const Integration::TextArray& text, const TextFormat& format)
48 {
49   // current text no longer required.
50   TextNotRequired();
51
52   mText = text;
53
54   return TextRequired( format );
55 }
56
57 TextVertexBuffer* TextRequestHelper::SetFont( const FontPointer& font, const TextFormat& format )
58 {
59   // current text no longer required.
60   TextNotRequired();
61
62   mFont = font;
63
64   // request text with new font
65   return TextRequired( format );
66 }
67
68 TextVertexBuffer* TextRequestHelper::SetTextAndFont( const Integration::TextArray& text,
69                                                      const FontPointer& font,
70                                                      const TextFormat& format )
71 {
72   // current text no longer required.
73   TextNotRequired();
74
75   mText = text;
76
77   mFont = font;
78
79   // request new text with new font
80   return TextRequired( format );
81 }
82
83 void TextRequestHelper::TextNotRequired()
84 {
85   if( mFont && ( 0u < mText.Count() ) )
86   {
87     mFont->TextNotRequired( mText, mFormat, mTextureId );
88   }
89 }
90
91 TextVertexBuffer* TextRequestHelper::TextRequired( const TextFormat& format )
92 {
93   mFormat = format;
94
95   if( mFont && ( 0u < mText.Count() ) )
96   {
97     // watch out for texture resizes / splits
98     AddTextureObserver();
99
100     // get the vertex buffer required to display the text
101     TextVertexBuffer* buffer =  mFont->TextRequired( mText, format );
102     if( buffer )
103     {
104       // keep track of the texture id, ( texture id == atlas id)
105       mTextureId = buffer->mTextureId;
106
107       return buffer;
108     }
109   }
110   else if( 0u == mText.Count()  )
111   {
112     // create an empty vertex buffer
113     TextVertexBuffer* buffer = new TextVertexBuffer;
114     buffer->mVertexMax = Vector2::ZERO;
115     buffer->mTextureId = 0;
116
117     return buffer;
118   }
119   return NULL;
120 }
121
122 void TextRequestHelper::TextureChanged( unsigned int oldTextureId, unsigned int newTextureId )
123 {
124   DALI_ASSERT_DEBUG( oldTextureId == mTextureId);
125   mTextureId = newTextureId;
126 }
127
128 bool TextRequestHelper::IsTextLoaded() const
129 {
130   if( mTextureId == 0 || (!mFont) || ( 0u == mText.Count() ) )
131   {
132     return false;
133   }
134   return mFont->IsTextLoaded( mText, mFormat, mTextureId );
135 }
136
137 void TextRequestHelper::AddTextureObserver()
138 {
139   if( !mTextureObserverInstalled )
140   {
141     mTextureObserverInstalled = true;
142     mFont->AddTextureObserver( mTextureObserver);
143   }
144 }
145
146 } // namespace Internal
147
148 } // namespace Dali