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