Adaptor: Fix Klocwork issues
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / resource-text-requester.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 #include "resource-text-requester.h"
19
20 #include "debug/resource-loader-debug.h"
21
22 using namespace Dali::Integration;
23
24 namespace Dali
25 {
26
27 namespace SlpPlatform
28 {
29
30 namespace
31 {
32 const uint32_t FIRST_VISIBLE_CHAR( 0x21 ); // 0x20 is the white space
33
34 bool IsVisible(uint32_t code)
35 {
36   return !(code < FIRST_VISIBLE_CHAR);
37 }
38
39 // helper to remove invisible characters
40 void RemoveInvisibleCharacters( Integration::TextResourceType::CharacterList& text )
41 {
42   Integration::TextResourceType::CharacterList temp;
43   temp.reserve(text.size());
44   for(std::size_t i=0, length= text.size(); i< length; i++)
45   {
46     if (IsVisible( text[i].character ))
47     {
48       temp.push_back(text[i]);
49     }
50   }
51   text = temp;
52 }
53
54 } // Anonymous namespace
55
56 ResourceTextRequester::ResourceTextRequester( ResourceLoader& resourceLoader )
57 : ResourceRequesterBase( resourceLoader ),
58   mThreadText( NULL )
59 {
60 }
61
62 ResourceTextRequester::~ResourceTextRequester()
63 {
64   delete mThreadText;
65 }
66
67 void ResourceTextRequester::Pause()
68 {
69   if( mThreadText )
70   {
71     mThreadText->Pause();
72   }
73 }
74
75 void ResourceTextRequester::Resume()
76 {
77   if( mThreadText )
78   {
79     mThreadText->Resume();
80   }
81 }
82
83 void ResourceTextRequester::LoadResource( Integration::ResourceRequest& request )
84 {
85   TextResourceType* textRequest = dynamic_cast<TextResourceType*>(request.GetType());
86   if( textRequest )
87   {
88     TextResourceType::CharacterList& text(textRequest->mCharacterList);
89
90     // Remove from stored request
91     RemoveInvisibleCharacters( text );
92     if( text.empty() )
93     {
94       IntrusivePtr<GlyphSet> cachedGlyphs(  new GlyphSet() );
95       LoadedResource resource( request.GetId(), request.GetType()->id, cachedGlyphs );
96       mResourceLoader.AddLoadedResource(resource);
97     }
98     else
99     {
100       CreateThread();
101       mThreadText->AddRequest( request, ResourceThreadBase::RequestLoad );
102     }
103   }
104 }
105
106 ResourcePointer ResourceTextRequester::LoadResourceSynchronously( const Integration::ResourceType& type, const std::string& path )
107 {
108   DALI_ASSERT_ALWAYS( false && "Cannot load text synchronously" );
109 }
110
111 LoadStatus ResourceTextRequester::LoadFurtherResources( ResourceRequest& request, LoadedResource partialResource )
112 {
113   // nothing to do
114
115   return RESOURCE_COMPLETELY_LOADED;
116 }
117
118 void ResourceTextRequester::SaveResource(const Integration::ResourceRequest& request )
119 {
120   // Nothing to do
121 }
122
123 void ResourceTextRequester::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
124 {
125   if( mThreadText )
126   {
127     mThreadText->CancelRequest(id);
128   }
129 }
130
131 void ResourceTextRequester::CreateThread()
132 {
133   if( ! mThreadText )
134   {
135     mThreadText = new ResourceThreadText(  mResourceLoader , TextResourceType::TextQualityHigh );
136   }
137 }
138
139 } // SlpPlatform
140
141 } // Dali