Cache default fontDescription + use default when fontDescription empty
[platform/core/uifw/dali-adaptor.git] / dali / internal / text / text-abstraction / segmentation-impl.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/internal/text/text-abstraction/segmentation-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/singleton-service.h>
23 #include <third-party/libunibreak/linebreak.h>
24 #include <third-party/libunibreak/wordbreak.h>
25
26 namespace Dali
27 {
28 namespace TextAbstraction
29 {
30 namespace Internal
31 {
32 struct Segmentation::Plugin
33 {
34   void GetLineBreakPositions(const Character* const text,
35                              Length                 numberOfCharacters,
36                              LineBreakInfo*         breakInfo)
37   {
38     set_linebreaks_utf32(text, numberOfCharacters, NULL, breakInfo);
39   }
40
41   void GetWordBreakPositions(const Character* const text,
42                              Length                 numberOfCharacters,
43                              WordBreakInfo*         breakInfo)
44   {
45     set_wordbreaks_utf32(text, numberOfCharacters, NULL, breakInfo);
46   }
47 };
48
49 Segmentation::Segmentation()
50 : mPlugin(NULL)
51 {
52 }
53
54 Segmentation::~Segmentation()
55 {
56   delete mPlugin;
57 }
58
59 TextAbstraction::Segmentation Segmentation::Get()
60 {
61   TextAbstraction::Segmentation segmentationHandle;
62
63   SingletonService service(SingletonService::Get());
64   if(service)
65   {
66     // Check whether the singleton is already created
67     Dali::BaseHandle handle = service.GetSingleton(typeid(TextAbstraction::Segmentation));
68     if(handle)
69     {
70       // If so, downcast the handle
71       Segmentation* impl = dynamic_cast<Internal::Segmentation*>(handle.GetObjectPtr());
72       segmentationHandle = TextAbstraction::Segmentation(impl);
73     }
74     else // create and register the object
75     {
76       segmentationHandle = TextAbstraction::Segmentation(new Segmentation);
77       service.Register(typeid(segmentationHandle), segmentationHandle);
78     }
79   }
80
81   return segmentationHandle;
82 }
83
84 void Segmentation::GetLineBreakPositions(const Character* const text,
85                                          Length                 numberOfCharacters,
86                                          LineBreakInfo*         breakInfo)
87 {
88   CreatePlugin();
89
90   mPlugin->GetLineBreakPositions(text, numberOfCharacters, breakInfo);
91 }
92
93 void Segmentation::GetWordBreakPositions(const Character* const text,
94                                          Length                 numberOfCharacters,
95                                          WordBreakInfo*         breakInfo)
96 {
97   CreatePlugin();
98
99   mPlugin->GetWordBreakPositions(text, numberOfCharacters, breakInfo);
100 }
101
102 void Segmentation::CreatePlugin()
103 {
104   if(!mPlugin)
105   {
106     mPlugin = new Plugin();
107   }
108 }
109
110 } // namespace Internal
111
112 } // namespace TextAbstraction
113
114 } // namespace Dali