2 * Copyright (c) 2021 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/internal/text/text-abstraction/hyphenation-impl.h>
22 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/common/singleton-service.h>
27 #ifdef HYPHEN_LIBRARY_AVAILABLE
30 #define HyphenDict void
34 #define HYPHEN_DIC "abc"
39 #if defined(DEBUG_ENABLED)
40 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_HYPHENATION");
47 namespace TextAbstraction
51 const char* const DEFAULT_LANGUAGE = "en_US";
52 const unsigned int DEFAULT_LANGUAGE_LENGTH = 5u;
54 struct Hyphenation::HyphenDictionary
56 HyphenDictionary(std::string lang, HyphenDict* dict)
66 std::string mLanguage;
67 HyphenDict* mDictionary;
70 struct Hyphenation::Plugin
79 #ifdef HYPHEN_LIBRARY_AVAILABLE
80 for(auto&& it : mHyphenDictionary)
82 hnj_hyphen_free(it.mDictionary);
87 std::string GetPathForLanguage(const char* lang)
89 std::string path = HYPHEN_DIC;
90 std::string filName = "/hyph_";
92 filName.append(".dic");
95 return path; // must be ..../hyph_en_US.dic
98 HyphenDict* LoadDictionary(const char* language)
100 #ifdef HYPHEN_LIBRARY_AVAILABLE
101 const char* lang = (language ? language : DEFAULT_LANGUAGE);
103 for(auto&& it : mHyphenDictionary)
105 if(!it.mLanguage.compare(lang))
107 return it.mDictionary;
111 std::string path = GetPathForLanguage(lang);
112 HyphenDict* dict = hnj_hyphen_load(path.c_str());
115 DALI_LOG_ERROR("Couldn't load hyphen dictionary:%s\n", lang);
119 mHyphenDictionary.push_back(HyphenDictionary(lang, dict));
128 const char* GetDictionaryEncoding(const char* lang)
130 #ifdef HYPHEN_LIBRARY_AVAILABLE
131 HyphenDict* dict = LoadDictionary(lang);
143 Vector<bool> GetWordHyphens(const char* word,
147 Vector<bool> hyphensList;
149 #ifdef HYPHEN_LIBRARY_AVAILABLE
150 char* hyphens = nullptr;
151 char** rep = nullptr;
156 if((!word) || (wordLength < 1))
161 dict = LoadDictionary(lang);
167 hyphens = (char*)malloc(wordLength + 5);
170 hnj_hyphen_hyphenate2(dict, (char*)(word), wordLength, hyphens, NULL, &rep, &pos, &cut);
172 hyphensList.PushBack(false);
174 for(Length i = 0; i < wordLength - 1; i++)
176 hyphensList.PushBack((bool)(hyphens[i + 1] & 1));
186 std::vector<HyphenDictionary> mHyphenDictionary;
189 Hyphenation::Hyphenation()
194 Hyphenation::~Hyphenation()
198 TextAbstraction::Hyphenation Hyphenation::Get()
200 TextAbstraction::Hyphenation hyphenationHandle;
202 SingletonService service(SingletonService::Get());
205 // Check whether the singleton is already created
206 Dali::BaseHandle handle = service.GetSingleton(typeid(TextAbstraction::Hyphenation));
209 // If so, downcast the handle
210 Hyphenation* impl = dynamic_cast<Internal::Hyphenation*>(handle.GetObjectPtr());
211 hyphenationHandle = TextAbstraction::Hyphenation(impl);
213 else // create and register the object
215 hyphenationHandle = TextAbstraction::Hyphenation(new Hyphenation);
216 service.Register(typeid(hyphenationHandle), hyphenationHandle);
220 return hyphenationHandle;
223 const char* Hyphenation::GetDictionaryEncoding(const char* lang)
227 return mPlugin->GetDictionaryEncoding(lang);
230 Vector<bool> Hyphenation::GetWordHyphens(const char* word,
236 return mPlugin->GetWordHyphens(word, wordLength, lang);
239 void Hyphenation::CreatePlugin()
243 mPlugin = std::make_unique<Plugin>();
247 } // namespace Internal
249 } // namespace TextAbstraction