1// [Blend2D]
2// 2D Vector Graphics Powered by a JIT Compiler.
3//
4// [License]
5// Zlib - See LICENSE.md file in the package.
6
7#ifndef BLEND2D_BLGLYPHBUFFER_H
8#define BLEND2D_BLGLYPHBUFFER_H
9
10#include "./blfontdefs.h"
11
12//! \addtogroup blend2d_api_text
13//! \{
14
15// ============================================================================
16// [BLGlyphBuffer - Core]
17// ============================================================================
18
19//! Glyph buffer [C Interface - Impl].
20//!
21//! \note This is not a `BLVariantImpl` compatible Impl.
22struct BLGlyphBufferImpl {
23 union {
24 struct {
25 //! Glyph item data.
26 BLGlyphItem* glyphItemData;
27 //! Glyph placement data.
28 BLGlyphPlacement* placementData;
29 //! Number of either code points or glyph indexes in the glyph-buffer.
30 size_t size;
31 //! Reserved, used exclusively by BLGlyphRun.
32 uint32_t reserved;
33 //! Flags shared between BLGlyphRun and BLGlyphBuffer.
34 uint32_t flags;
35 };
36
37 //! Glyph run data that can be passed directly to the rendering context.
38 BLGlyphRun glyphRun;
39 };
40
41 //! Glyph info data - additional information of each `BLGlyphItem`.
42 BLGlyphInfo* glyphInfoData;
43};
44
45//! Glyph buffer [C Interface - Core].
46struct BLGlyphBufferCore {
47 BLGlyphBufferImpl* impl;
48};
49
50// ============================================================================
51// [BLGlyphBuffer - C++]
52// ============================================================================
53
54#ifdef __cplusplus
55//! Glyph buffer [C++ API].
56//!
57//! Can hold either text or glyphs and provides basic memory management that is
58//! used for text shaping, character to glyph mapping, glyph substitution, and
59//! glyph positioning.
60//!
61//! Glyph buffer provides two separate buffers called 'primary' and 'secondary'
62//! that serve different purposes during processing. Primary buffer always holds
63//! actualy `BLGlyphItem` array, and secondary buffer is either used as a scratch
64//! buffer during glyph substitution or hold glyph positions after the processing
65//! is complete and glyph positions were calculated.
66class BLGlyphBuffer : public BLGlyphBufferCore {
67public:
68 //! \name Constructors & Destructors
69 //! \{
70
71 BL_INLINE BLGlyphBuffer(const BLGlyphBuffer&) noexcept = delete;
72 BL_INLINE BLGlyphBuffer& operator=(const BLGlyphBuffer&) noexcept = delete;
73
74 BL_INLINE BLGlyphBuffer() noexcept { blGlyphBufferInit(this); }
75 BL_INLINE ~BLGlyphBuffer() noexcept { blGlyphBufferReset(this); }
76
77 //! \}
78
79 //! \name Overloaded Operators
80 //! \{
81
82 BL_INLINE explicit operator bool() const noexcept { return !empty(); }
83
84 //! \}
85
86 //! \name Accessors
87 //! \{
88
89 BL_INLINE bool empty() const noexcept { return impl->glyphRun.empty(); }
90
91 BL_INLINE size_t size() const noexcept { return impl->size; }
92 BL_INLINE uint32_t flags() const noexcept { return impl->flags; }
93
94 BL_INLINE BLGlyphItem* glyphItemData() const noexcept { return impl->glyphItemData; }
95 BL_INLINE BLGlyphPlacement* placementData() const noexcept { return impl->placementData; }
96 BL_INLINE BLGlyphInfo* glyphInfoData() const noexcept { return impl->glyphInfoData; }
97
98 BL_INLINE const BLGlyphRun& glyphRun() const noexcept { return impl->glyphRun; }
99
100 //! Tests whether the glyph-buffer has `flag` set.
101 BL_INLINE bool hasFlag(uint32_t flag) const noexcept { return (impl->flags & flag) != 0; }
102 //! Tests whether the buffer contains unicode data.
103 BL_INLINE bool hasText() const noexcept { return hasFlag(BL_GLYPH_RUN_FLAG_UCS4_CONTENT); }
104 //! Tests whether the buffer contains glyph-id data.
105 BL_INLINE bool hasGlyphs() const noexcept { return !hasFlag(BL_GLYPH_RUN_FLAG_UCS4_CONTENT); }
106
107 //! Tests whether the input string contained invalid characters (unicode encoding errors).
108 BL_INLINE bool hasInvalidChars() const noexcept { return hasFlag(BL_GLYPH_RUN_FLAG_INVALID_TEXT); }
109 //! Tests whether the input string contained undefined characters that weren't mapped properly to glyphs.
110 BL_INLINE bool hasUndefinedChars() const noexcept { return hasFlag(BL_GLYPH_RUN_FLAG_UNDEFINED_GLYPHS); }
111 //! Tests whether one or more operation was terminated before completion because of invalid data in a font.
112 BL_INLINE bool hasInvalidFontData() const noexcept { return hasFlag(BL_GLYPH_RUN_FLAG_INVALID_FONT_DATA); }
113
114 //! \}
115
116 //! \name Operations
117 //! \{
118
119 //! Resets the `BLGlyphBuffer` into its construction state. The content will
120 //! be cleared and allocated memory released.
121 BL_INLINE BLResult reset() noexcept {
122 return blGlyphBufferReset(this);
123 }
124
125 //! Clears the content of `BLGlyphBuffer` without releasing internal buffers.
126 BL_INLINE BLResult clear() noexcept {
127 return blGlyphBufferClear(this);
128 }
129
130 //! Sets text content of this `BLGlyphBuffer`.
131 //!
132 //! This is a generic function that accepts `void*` data, which is specified
133 //! by `encoding`. The `size` argument depends on encoding as well. If the
134 //! encoding specifies byte string (LATIN1 or UTF8) then it's bytes, if the
135 //! encoding specifies UTF16 or UTF32 then it would describe the number of
136 //! `uint16_t` or `uint32_t` code points, respectively.
137 //!
138 //! Null-terminated string can be specified by passing `SIZE_MAX` as `size`.
139 BL_INLINE BLResult setText(const void* data, size_t size, uint32_t encoding) noexcept {
140 return blGlyphBufferSetText(this, data, size, encoding);
141 }
142
143 BL_INLINE BLResult setLatin1Text(const char* data, size_t size = SIZE_MAX) noexcept {
144 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_LATIN1);
145 }
146
147 BL_INLINE BLResult setUtf8Text(const char* data, size_t size = SIZE_MAX) noexcept {
148 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_UTF8);
149 }
150
151 BL_INLINE BLResult setUtf8Text(const uint8_t* data, size_t size = SIZE_MAX) noexcept {
152 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_UTF8);
153 }
154
155 BL_INLINE BLResult setUtf16Text(const uint16_t* data, size_t size = SIZE_MAX) noexcept {
156 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_UTF16);
157 }
158
159 BL_INLINE BLResult setUtf32Text(const uint32_t* data, size_t size = SIZE_MAX) noexcept {
160 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_UTF32);
161 }
162
163 BL_INLINE BLResult setWCharText(const wchar_t* data, size_t size = SIZE_MAX) noexcept {
164 return blGlyphBufferSetText(this, data, size, BL_TEXT_ENCODING_WCHAR);
165 }
166
167 BL_INLINE BLResult setGlyphIds(const void* data, intptr_t advance, size_t size) noexcept {
168 return blGlyphBufferSetGlyphIds(this, data, advance, size);
169 }
170
171 //! \}
172};
173#endif
174
175//! \}
176
177#endif // BLEND2D_BLGLYPHBUFFER_H
178