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);
169 hnj_hyphen_hyphenate2(dict, (char*)(word), wordLength, hyphens, NULL, &rep, &pos, &cut);
171 hyphensList.PushBack(false);
173 for(Length i = 0; i < wordLength - 1; i++)
175 hyphensList.PushBack((bool)(hyphens[i + 1] & 1));
184 std::vector<HyphenDictionary> mHyphenDictionary;
187 Hyphenation::Hyphenation()
192 Hyphenation::~Hyphenation()
196 TextAbstraction::Hyphenation Hyphenation::Get()
198 TextAbstraction::Hyphenation hyphenationHandle;
200 SingletonService service(SingletonService::Get());
203 // Check whether the singleton is already created
204 Dali::BaseHandle handle = service.GetSingleton(typeid(TextAbstraction::Hyphenation));
207 // If so, downcast the handle
208 Hyphenation* impl = dynamic_cast<Internal::Hyphenation*>(handle.GetObjectPtr());
209 hyphenationHandle = TextAbstraction::Hyphenation(impl);
211 else // create and register the object
213 hyphenationHandle = TextAbstraction::Hyphenation(new Hyphenation);
214 service.Register(typeid(hyphenationHandle), hyphenationHandle);
218 return hyphenationHandle;
221 const char* Hyphenation::GetDictionaryEncoding(const char* lang)
225 return mPlugin->GetDictionaryEncoding(lang);
228 Vector<bool> Hyphenation::GetWordHyphens(const char* word,
234 return mPlugin->GetWordHyphens(word, wordLength, lang);
237 void Hyphenation::CreatePlugin()
241 mPlugin = std::make_unique<Plugin>();
245 } // namespace Internal
247 } // namespace TextAbstraction