2 * Copyright (c) 2022 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.
17 #include <dali/devel-api/common/singleton-service.h>
18 #include <dali/devel-api/text-abstraction/bidirectional-support.h>
19 #include <dali/devel-api/text-abstraction/font-client.h>
20 #include <dali/devel-api/text-abstraction/font-metrics.h>
21 #include <dali/devel-api/text-abstraction/glyph-info.h>
22 #include <dali/devel-api/text-abstraction/script.h>
23 #include <dali/devel-api/text-abstraction/segmentation.h>
24 #include <dali/devel-api/text-abstraction/shaping.h>
25 #include <dali/public-api/object/base-object.h>
34 namespace TextAbstraction
38 class BidirectionalSupport : public BaseObject
41 BidirectionalSupport()
45 ~BidirectionalSupport()
49 static TextAbstraction::BidirectionalSupport Get()
51 TextAbstraction::BidirectionalSupport bidirectionalSupportHandle;
53 Dali::SingletonService service(Dali::SingletonService::Get());
56 // Check whether the singleton is already created
57 BaseHandle handle = service.GetSingleton(typeid(TextAbstraction::BidirectionalSupport));
60 // If so, downcast the handle
61 BidirectionalSupport* impl = dynamic_cast<Internal::BidirectionalSupport*>(handle.GetObjectPtr());
62 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport(impl);
64 else // create and register the object
66 bidirectionalSupportHandle = TextAbstraction::BidirectionalSupport(new BidirectionalSupport);
67 service.Register(typeid(bidirectionalSupportHandle), bidirectionalSupportHandle);
70 return bidirectionalSupportHandle;
72 BidiInfoIndex CreateInfo(const Character* const paragraph, Length numberOfCharacters, bool matchSystemLanguageDirection, LayoutDirection::Type layoutDirection)
77 void DestroyInfo(BidiInfoIndex bidiInfoIndex)
81 void Reorder(BidiInfoIndex bidiInfoIndex, CharacterIndex firstCharacterIndex, Length numberOfCharacters, CharacterIndex* visualToLogicalMap)
85 bool GetMirroredText(Character* text, CharacterDirection* directions, Length numberOfCharacters)
90 bool GetParagraphDirection(BidiInfoIndex bidiInfoIndex) const
95 void GetCharactersDirection(BidiInfoIndex bidiInfoIndex, CharacterDirection* directions, Length numberOfCharacters)
98 }; // class BidirectionalSupport
100 class FontClient : public BaseObject
112 static Dali::TextAbstraction::FontClient Get()
114 Dali::TextAbstraction::FontClient fontClientHandle;
116 Dali::SingletonService service(SingletonService::Get());
119 // Check whether the singleton is already created
120 Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::TextAbstraction::FontClient));
123 // If so, downcast the handle
124 FontClient* impl = dynamic_cast<Dali::TextAbstraction::Internal::FontClient*>(handle.GetObjectPtr());
125 fontClientHandle = Dali::TextAbstraction::FontClient(impl);
127 else // create and register the object
129 fontClientHandle = Dali::TextAbstraction::FontClient(new FontClient);
130 service.Register(typeid(fontClientHandle), fontClientHandle);
134 return fontClientHandle;
137 void SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi)
140 void GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi)
142 horizontalDpi = verticalDpi = 96;
145 void ResetSystemDefaults()
148 void GetDefaultFonts(FontList& defaultFonts)
151 void GetDefaultPlatformFontDescription(FontDescription& fontDescription)
154 void GetSystemFonts(FontList& systemFonts)
157 void GetDescription(FontId id, FontDescription& fontDescription)
160 PointSize26Dot6 GetPointSize(FontId id)
164 FontId FindDefaultFont(Character charcode, PointSize26Dot6 pointSize, bool preferColor)
168 FontId FindFallbackFont(Character charcode, const FontDescription& fontDescription, PointSize26Dot6 pointSize, bool preferColor)
172 FontId GetFontId(const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex)
176 FontId GetFontId(const FontDescription& fontDescription, PointSize26Dot6 pointSize, FaceIndex faceIndex)
180 bool IsScalable(const FontPath& path)
184 bool IsScalable(const FontDescription& fontDescription)
188 void GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes)
191 void GetFixedSizes(const FontDescription& fontDescription, Dali::Vector<PointSize26Dot6>& sizes)
194 void GetFontMetrics(FontId fontId, FontMetrics& metrics)
197 GlyphIndex GetGlyphIndex(FontId fontId, Character charcode)
201 bool GetGlyphMetrics(GlyphInfo* array, uint32_t size, bool horizontal)
205 void CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool softwareItailc, bool softwareBold, Dali::TextAbstraction::GlyphBufferData& data, int outlineWidth)
208 PixelData CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth)
212 void CreateVectorBlob(FontId fontId, GlyphIndex glyphIndex, VectorBlob*& blob, unsigned int& blobLength, unsigned int& nominalWidth, unsigned int& nominalHeight)
216 const GlyphInfo& GetEllipsisGlyph(PointSize26Dot6 pointSize)
220 bool IsColorGlyph(FontId fontId, GlyphIndex glyphIndex)
226 GlyphInfo mGlyphInfo;
227 }; // class FontClient
229 class Shaping : public BaseObject
243 static Dali::TextAbstraction::Shaping Get()
245 Dali::TextAbstraction::Shaping shapingHandle;
247 Dali::SingletonService service(SingletonService::Get());
250 // Check whether the singleton is already created
251 Dali::BaseHandle handle = service.GetSingleton(typeid(Dali::TextAbstraction::Shaping));
254 // If so, downcast the handle
255 Shaping* impl = dynamic_cast<Dali::TextAbstraction::Internal::Shaping*>(handle.GetObjectPtr());
256 shapingHandle = Dali::TextAbstraction::Shaping(impl);
258 else // create and register the object
260 shapingHandle = Dali::TextAbstraction::Shaping(new Shaping);
261 service.Register(typeid(shapingHandle), shapingHandle);
264 return shapingHandle;
267 void GetGlyphs(GlyphInfo* glyphStore, unsigned int* mappingTable)
269 // Return store & mapping table (0, 1, 2, 3... N-1))
272 memcpy(glyphStore, mText, mNumChars);
274 for(unsigned int i = 0; i < mNumChars; ++i)
280 Length Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
282 mText = new unsigned char[numChars];
283 mNumChars = numChars;
285 memcpy(mText, text, numChars);
291 unsigned char* mText;
292 unsigned int mNumChars;
295 } // namespace Internal
296 } // namespace TextAbstraction
298 inline static TextAbstraction::Internal::BidirectionalSupport& GetImplementation(TextAbstraction::BidirectionalSupport& bidirectionalSupport)
300 DALI_ASSERT_ALWAYS(bidirectionalSupport && "bidirectional support handle is empty");
301 BaseObject& object = bidirectionalSupport.GetBaseObject();
302 return static_cast<TextAbstraction::Internal::BidirectionalSupport&>(object);
305 inline static const TextAbstraction::Internal::BidirectionalSupport& GetImplementation(const TextAbstraction::BidirectionalSupport& bidirectionalSupport)
307 DALI_ASSERT_ALWAYS(bidirectionalSupport && "bidirectional support handle is empty");
308 const BaseObject& object = bidirectionalSupport.GetBaseObject();
309 return static_cast<const TextAbstraction::Internal::BidirectionalSupport&>(object);
312 inline static TextAbstraction::Internal::FontClient& GetImplementation(TextAbstraction::FontClient& fontClient)
314 DALI_ASSERT_ALWAYS(fontClient && "fontClient handle is empty");
315 BaseObject& handle = fontClient.GetBaseObject();
316 return static_cast<TextAbstraction::Internal::FontClient&>(handle);
319 inline static TextAbstraction::Internal::Shaping& GetImplementation(TextAbstraction::Shaping& shaping)
321 DALI_ASSERT_ALWAYS(shaping && "shaping handle is empty");
322 BaseObject& handle = shaping.GetBaseObject();
323 return static_cast<TextAbstraction::Internal::Shaping&>(handle);
326 ////////////////////////////////////////////////////////////////////////////////
327 /******************************************************************************/
329 namespace TextAbstraction
331 const PointSize26Dot6 FontClient::DEFAULT_POINT_SIZE = 768u; // 12*64
333 BidirectionalSupport::BidirectionalSupport()
337 BidirectionalSupport::~BidirectionalSupport()
341 BidirectionalSupport::BidirectionalSupport(Internal::BidirectionalSupport* implementation)
342 : BaseHandle(implementation)
346 BidirectionalSupport BidirectionalSupport::Get()
348 return Internal::BidirectionalSupport::Get();
351 BidiInfoIndex BidirectionalSupport::CreateInfo(const Character* const paragraph,
352 Length numberOfCharacters,
353 bool matchSystemLanguageDirection,
354 LayoutDirection::Type layoutDirection)
356 return GetImplementation(*this).CreateInfo(paragraph, numberOfCharacters, matchSystemLanguageDirection, layoutDirection);
359 void BidirectionalSupport::DestroyInfo(BidiInfoIndex bidiInfoIndex)
361 GetImplementation(*this).DestroyInfo(bidiInfoIndex);
364 void BidirectionalSupport::Reorder(BidiInfoIndex bidiInfoIndex,
365 CharacterIndex firstCharacterIndex,
366 Length numberOfCharacters,
367 CharacterIndex* visualToLogicalMap)
369 GetImplementation(*this).Reorder(bidiInfoIndex, firstCharacterIndex, numberOfCharacters, visualToLogicalMap);
372 bool BidirectionalSupport::GetMirroredText(Character* text,
373 CharacterDirection* directions,
374 Length numberOfCharacters)
376 return GetImplementation(*this).GetMirroredText(text, directions, numberOfCharacters);
379 bool BidirectionalSupport::GetParagraphDirection(BidiInfoIndex bidiInfoIndex) const
381 return GetImplementation(*this).GetParagraphDirection(bidiInfoIndex);
384 void BidirectionalSupport::GetCharactersDirection(BidiInfoIndex bidiInfoIndex,
385 CharacterDirection* directions,
386 Length numberOfCharacters)
388 GetImplementation(*this).GetCharactersDirection(bidiInfoIndex, directions, numberOfCharacters);
391 FontClient FontClient::Get()
393 return Internal::FontClient::Get();
396 FontClient::FontClient()
400 FontClient::~FontClient()
404 FontClient::FontClient(const FontClient& handle)
409 GlyphBufferData::GlyphBufferData()
413 GlyphBufferData::~GlyphBufferData()
417 FontClient& FontClient::operator=(const FontClient& handle)
419 BaseHandle::operator=(handle);
423 void FontClient::SetDpi(unsigned int horizontalDpi, unsigned int verticalDpi)
425 GetImplementation(*this).SetDpi(horizontalDpi, verticalDpi);
428 void FontClient::GetDpi(unsigned int& horizontalDpi, unsigned int& verticalDpi)
430 GetImplementation(*this).GetDpi(horizontalDpi, verticalDpi);
433 void FontClient::ResetSystemDefaults()
435 GetImplementation(*this).ResetSystemDefaults();
438 void FontClient::GetDefaultFonts(FontList& defaultFonts)
440 GetImplementation(*this).GetDefaultFonts(defaultFonts);
443 void FontClient::GetDefaultPlatformFontDescription(FontDescription& fontDescription)
445 GetImplementation(*this).GetDefaultPlatformFontDescription(fontDescription);
448 void FontClient::GetSystemFonts(FontList& systemFonts)
450 GetImplementation(*this).GetSystemFonts(systemFonts);
453 void FontClient::GetDescription(FontId id, FontDescription& fontDescription)
455 GetImplementation(*this).GetDescription(id, fontDescription);
458 PointSize26Dot6 FontClient::GetPointSize(FontId id)
460 return GetImplementation(*this).GetPointSize(id);
463 FontId FontClient::FindDefaultFont(Character charcode, PointSize26Dot6 pointSize, bool preferColor)
465 return GetImplementation(*this).FindDefaultFont(charcode, pointSize, preferColor);
468 FontId FontClient::FindFallbackFont(Character charcode, const FontDescription& fontDescription, PointSize26Dot6 pointSize, bool preferColor)
470 return GetImplementation(*this).FindFallbackFont(charcode, fontDescription, pointSize, preferColor);
473 FontId FontClient::GetFontId(const FontPath& path, PointSize26Dot6 pointSize, FaceIndex faceIndex)
475 return GetImplementation(*this).GetFontId(path, pointSize, faceIndex);
478 FontId FontClient::GetFontId(const FontDescription& fontDescription,
479 PointSize26Dot6 pointSize,
482 return GetImplementation(*this).GetFontId(fontDescription,
487 bool FontClient::IsScalable(const FontPath& path)
489 return GetImplementation(*this).IsScalable(path);
492 bool FontClient::IsScalable(const FontDescription& fontDescription)
494 return GetImplementation(*this).IsScalable(fontDescription);
497 void FontClient::GetFixedSizes(const FontPath& path, Dali::Vector<PointSize26Dot6>& sizes)
499 GetImplementation(*this).GetFixedSizes(path, sizes);
502 void FontClient::GetFixedSizes(const FontDescription& fontDescription,
503 Dali::Vector<PointSize26Dot6>& sizes)
505 GetImplementation(*this).GetFixedSizes(fontDescription, sizes);
508 void FontClient::GetFontMetrics(FontId fontId, FontMetrics& metrics)
510 GetImplementation(*this).GetFontMetrics(fontId, metrics);
513 GlyphIndex FontClient::GetGlyphIndex(FontId fontId, Character charcode)
515 return GetImplementation(*this).GetGlyphIndex(fontId, charcode);
518 bool FontClient::GetGlyphMetrics(GlyphInfo* array, uint32_t size, GlyphType type, bool horizontal)
520 return GetImplementation(*this).GetGlyphMetrics(array, size, horizontal);
523 void FontClient::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, bool softwareItailc, bool softwareBold, Dali::TextAbstraction::GlyphBufferData& data, int outlineWidth)
525 GetImplementation(*this).CreateBitmap(fontId, glyphIndex, softwareItailc, softwareBold, data, outlineWidth);
528 PixelData FontClient::CreateBitmap(FontId fontId, GlyphIndex glyphIndex, int outlineWidth)
530 return GetImplementation(*this).CreateBitmap(fontId, glyphIndex, outlineWidth);
533 void FontClient::CreateVectorBlob(FontId fontId,
534 GlyphIndex glyphIndex,
536 unsigned int& blobLength,
537 unsigned int& nominalWidth,
538 unsigned int& nominalHeight)
540 GetImplementation(*this).CreateVectorBlob(fontId, glyphIndex, blob, blobLength, nominalWidth, nominalHeight);
543 const GlyphInfo& FontClient::GetEllipsisGlyph(PointSize26Dot6 pointSize)
545 return GetImplementation(*this).GetEllipsisGlyph(pointSize);
548 bool FontClient::IsColorGlyph(FontId fontId, GlyphIndex glyphIndex)
550 return GetImplementation(*this).IsColorGlyph(fontId, glyphIndex);
553 FontClient::FontClient(Internal::FontClient* internal)
554 : BaseHandle(internal)
558 FontMetrics::FontMetrics()
562 underlinePosition(0.f),
563 underlineThickness(0.f)
567 GlyphInfo::GlyphInfo()
571 Script GetCharacterScript(unsigned int x)
575 bool HasLigatureMustBreak(Script x)
579 bool IsCommonScript(unsigned int character)
583 bool IsNewParagraph(unsigned int character)
587 bool IsRightToLeftScript(Script)
591 bool IsWhiteSpace(unsigned int character)
593 return character < 0x21;
596 Segmentation Segmentation::Get()
598 return Segmentation();
600 Segmentation::Segmentation()
603 Segmentation::~Segmentation()
606 void Segmentation::GetLineBreakPositions(unsigned int const*, unsigned int, char*)
609 void Segmentation::GetWordBreakPositions(unsigned int const*, unsigned int, char*)
612 void Segmentation::GetLineBreakPositionsUtf8(unsigned char const*, unsigned int, char*)
615 void Segmentation::GetWordBreakPositionsUtf8(unsigned char const*, unsigned int, char*)
619 Shaping Shaping::Get()
621 return TextAbstraction::Internal::Shaping::Get();
628 Shaping::Shaping(Internal::Shaping* internal)
629 : BaseHandle(internal)
637 void Shaping::GetGlyphs(GlyphInfo* glyphStore, unsigned int* mappingTable)
639 // Return store & mapping table (0, 1, 2, 3... N-1))
640 GetImplementation(*this).GetGlyphs(glyphStore, mappingTable);
643 Length Shaping::Shape(unsigned int const* text, unsigned int numChars, unsigned int fontId, Script script)
645 return GetImplementation(*this).Shape(text, numChars, fontId, script);
648 } // namespace TextAbstraction