Line data Source code
1 : /**
2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef CORE_FONT_SPFONTFORMATTER_H_
24 : #define CORE_FONT_SPFONTFORMATTER_H_
25 :
26 : #include "SPFontTextLayout.h"
27 : #include "SPFontHyphenMap.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::font {
30 :
31 : class Formatter : public InterfaceObject<memory::StandartInterface>{
32 : public:
33 : struct LinePosition {
34 : uint16_t offset;
35 : uint16_t width;
36 : };
37 :
38 : using LinePositionCallback = Function<LinePosition(uint16_t &, uint16_t &, float density)>;
39 : using FontCallback = Function<Rc<FontFaceSet>(const FontParameters &f)>;
40 :
41 : enum class ContentRequest {
42 : Normal,
43 : Minimize,
44 : Maximize,
45 : };
46 :
47 : Formatter();
48 : Formatter(FontCallback &&, TextLayoutData<memory::StandartInterface> *);
49 : Formatter(FontCallback &&, TextLayoutData<memory::PoolInterface> *);
50 :
51 : void setFontCallback(FontCallback &&);
52 :
53 : void reset(TextLayoutData<memory::StandartInterface> *);
54 : void reset(TextLayoutData<memory::PoolInterface> *);
55 : void reset();
56 : void finalize();
57 :
58 : void setLinePositionCallback(const LinePositionCallback &);
59 : void setWidth(uint16_t w);
60 : void setTextAlignment(TextAlign align);
61 : void setLineHeightAbsolute(uint16_t);
62 : void setLineHeightRelative(float);
63 :
64 : void setMaxWidth(uint16_t);
65 : void setMaxLines(size_t);
66 : void setOpticalAlignment(bool value);
67 : void setEmplaceAllChars(bool value);
68 : void setFillerChar(char16_t);
69 : void setHyphens(HyphenMap *);
70 : void setRequest(ContentRequest);
71 :
72 : void begin(uint16_t indent, uint16_t blockMargin = 0);
73 : bool read(const FontParameters &f, const TextParameters &s, WideStringView str, uint16_t front = 0, uint16_t back = 0);
74 : bool read(const FontParameters &f, const TextParameters &s, const char16_t *str, size_t len, uint16_t front = 0, uint16_t back = 0);
75 : bool read(const FontParameters &f, const TextParameters &s, uint16_t w, uint16_t h);
76 :
77 : uint16_t getHeight() const;
78 : uint16_t getWidth() const;
79 : uint16_t getMaxLineX() const;
80 : uint16_t getLineHeight() const;
81 :
82 : protected:
83 : bool isSpecial(char16_t) const;
84 : uint16_t checkBullet(uint16_t first, uint16_t len) const;
85 :
86 : void parseWhiteSpace(WhiteSpace whiteSpacePolicy);
87 : void parseFontLineHeight(uint16_t);
88 : bool updatePosition(uint16_t &linePos, uint16_t &lineHeight);
89 :
90 : uint16_t getAdvance(const CharLayoutData &c) const;
91 : uint16_t getAdvance(uint16_t pos) const;
92 :
93 : inline uint16_t getAdvancePosition(const CharLayoutData &) const;
94 : inline uint16_t getAdvancePosition(uint16_t pos) const;
95 :
96 : uint16_t getLineAdvancePos(uint16_t lastPos);
97 :
98 : inline uint16_t getOriginPosition(const CharLayoutData &) const;
99 : inline uint16_t getOriginPosition(uint16_t pos) const;
100 :
101 : bool readWithRange(RangeLayoutData &&, const TextParameters &s, const char16_t *str, size_t len, uint16_t front = 0, uint16_t back = 0);
102 : bool readWithRange(RangeLayoutData &&, const TextParameters &s, uint16_t w, uint16_t h);
103 :
104 : bool readChars(WideStringView &r, const Vector<uint8_t> & = Vector<uint8_t>());
105 : void pushLineFiller(bool replaceLastChar = false);
106 : bool pushChar(char16_t);
107 : bool pushSpace(bool wrap = true);
108 : bool pushTab();
109 : bool pushLine(uint16_t first, uint16_t len, bool forceAlign);
110 : bool pushLine(bool forceAlign);
111 : bool pushLineBreak();
112 : bool pushLineBreakChar();
113 :
114 : void updateLineHeight(uint16_t first, uint16_t last);
115 :
116 : struct Output {
117 : uint16_t *width = nullptr;
118 : uint16_t *height = nullptr;
119 : uint16_t *maxAdvance = nullptr;
120 : bool *overflow = nullptr;
121 :
122 : VectorAdapter<RangeLayoutData> ranges;
123 : VectorAdapter<CharLayoutData> chars;
124 : VectorAdapter<LineLayoutData> lines;
125 :
126 17139 : Output() = default;
127 : Output(TextLayoutData<memory::StandartInterface> *);
128 : Output(TextLayoutData<memory::PoolInterface> *);
129 :
130 : Output &operator=(Output &&) = default;
131 : };
132 :
133 : Rc<HyphenMap> _hyphens;
134 : Rc<FontFaceSet> _primaryFontSet;
135 :
136 : Output _output;
137 :
138 : size_t _charPosition = 0;
139 :
140 : TextParameters _textStyle;
141 :
142 : bool preserveLineBreaks = false;
143 : bool collapseSpaces = true;
144 : bool wordWrap = false;
145 : bool opticalAlignment = true;
146 : bool emplaceAllChars = false;
147 :
148 : uint16_t faceId = 0;
149 : char16_t b = 0;
150 : char16_t c = 0;
151 :
152 : uint16_t defaultWidth = 0;
153 : uint16_t width = 0;
154 : uint16_t lineOffset = 0;
155 : int16_t lineX = 0;
156 : uint16_t lineY = 0;
157 :
158 : uint16_t maxLineX = 0;
159 :
160 : uint16_t charNum = 0;
161 : uint16_t lineHeight = 0;
162 : uint16_t currentLineHeight = 0;
163 : uint16_t rangeLineHeight = 0;
164 :
165 : float lineHeightMod = 1.0f;
166 : bool lineHeightIsAbsolute = false;
167 :
168 : uint16_t firstInLine = 0;
169 : uint16_t wordWrapPos = 0;
170 :
171 : bool bufferedSpace = false;
172 :
173 : uint16_t maxWidth = 0;
174 : size_t maxLines = 0;
175 :
176 : char16_t _fillerChar = 0;
177 : TextAlign alignment = TextAlign::Left;
178 :
179 : ContentRequest request = ContentRequest::Normal;
180 :
181 : FontCallback fontCallback;
182 : LinePositionCallback linePositionFunc = nullptr;
183 : };
184 :
185 : }
186 :
187 : #endif /* CORE_FONT_SPFONTFORMATTER_H_ */
|