YSTest: TextLayout.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

TextLayout.h
浏览该文件的文档.
1 /*
2 © 2009-2015, 2019-2020 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YSL_INC_Service_TextLayout_h_
29 #define YSL_INC_Service_TextLayout_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Service_TextRenderer
33 #include <numeric> // for std::accumulate;
34
35 namespace YSLib
36 {
37
38 namespace Drawing
39 {
40
48 YB_ATTR_nodiscard YF_API YB_PURE SDst
49 FetchResizedBottomMargin(const TextState&, SDst);
50
57 YB_ATTR_nodiscard YF_API YB_PURE std::uint16_t
58 FetchResizedLineN(const TextState& ts, SDst);
59
66 YB_ATTR_nodiscard YF_API YB_PURE SPos
67 FetchLastLineBasePosition(const TextState&, SDst);
68
69
74 YB_ATTR_nodiscard YB_PURE inline
75 PDefH(SDst, FetchResizedBottomMargin, const TextRegion& tr)
76 ImplRet(FetchResizedBottomMargin(tr, tr.GetHeight()))
77
82 inline PDefH(void, AdjustBottomMarginOf, TextRegion& tr)
83 // XXX: Conversion to 'SPos' might be implementation-defined.
84 ImplExpr(tr.Margin.Bottom = SPos(FetchResizedBottomMargin(tr)))
85
86
92 YB_ATTR_nodiscard YF_API YB_PURE SDst
93 FetchCharWidth(const Font&, char32_t);
94
104 template<typename _tIter,
105 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
106 YB_ATTR_nodiscard YB_PURE pair<size_t, SDst>
107 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s)
108 {
109 size_t r(0);
110 SDst w(0);
111
112 for(; *s != char() && w < max_width; yunseq(++s, ++r))
113 {
114 using ystdex::is_undereferenceable;
115
116 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
117 w += FetchCharWidth(fnt, *s);
118 }
119 return {r, w};
120 }
122
123
127 template<typename _tIter,
128 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
129 YB_ATTR_nodiscard YB_PURE pair<size_t, SDst>
130 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, size_t n,
131 char32_t c = {})
132 {
133 size_t r(0);
134 SDst w(0);
135
136 for(; n-- != 0 && *s != c && w < max_width; yunseq(++s, ++r))
137 {
138 using ystdex::is_undereferenceable;
139
140 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
141 w += FetchCharWidth(fnt, *s);
142 }
143 return {r, w};
144 }
149 template<typename _tIter,
150 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
151 YB_ATTR_nodiscard YB_PURE pair<size_t, SDst>
152 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, _tIter g,
153 char32_t c = {})
154 {
155 size_t r(0);
156 SDst w(0);
157
158 for(; s != g && *s != c && w < max_width; yunseq(++s, ++r))
159 {
160 using ystdex::is_undereferenceable;
161
162 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
163 w += FetchCharWidth(fnt, *s);
164 }
165 return {r, w};
166 }
168 template<class _tString,
170 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
171 YB_ATTR_nodiscard YB_PURE inline pair<size_t, SDst>
172 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str)
173 {
174 return Drawing::FetchStringOffsets(max_width, fnt, str.c_str());
175 }
180 template<class _tString,
181 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
182 YB_ATTR_nodiscard YB_PURE inline pair<size_t, SDst>
183 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str,
184 size_t n)
185 {
186 return Drawing::FetchStringOffsets(max_width, fnt, str.data(), n);
187 }
189
195 template<typename _tIter,
196 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
197 YB_ATTR_nodiscard YB_PURE SDst
198 FetchStringWidth(const Font& fnt, _tIter s)
199 {
200 SDst w(0);
201
202 for(; *s != char(); ++s)
203 {
204 using ystdex::is_undereferenceable;
205
206 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
207 w += FetchCharWidth(fnt, *s);
208 }
209 return w;
210 }
212
213
217 template<typename _tIter,
218 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
219 YB_ATTR_nodiscard YB_PURE SDst
220 FetchStringWidth(const Font& fnt, _tIter s, size_t n, char32_t c = {})
221 {
222 SDst w(0);
223
224 for(; n-- != 0 && *s != c; ++s)
225 {
226 using ystdex::is_undereferenceable;
227
228 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
229 w += FetchCharWidth(fnt, *s);
230 }
231 return w;
232 }
237 template<typename _tIter,
238 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
239 YB_ATTR_nodiscard YB_PURE SDst
240 FetchStringWidth(const Font& fnt, _tIter s, _tIter g, char32_t c = {})
241 {
242 SDst w(0);
243
244 for(; s != g && *s != c; ++s)
245 {
246 using ystdex::is_undereferenceable;
247
248 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
249 w += FetchCharWidth(fnt, *s);
250 }
251 return w;
252 }
254
258 template<class _tString,
259 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
260 YB_ATTR_nodiscard YB_PURE inline SDst
261 FetchStringWidth(const Font& fnt, const _tString& str)
262 {
263 return Drawing::FetchStringWidth(fnt, str.c_str());
264 }
269 template<class _tString,
270 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
271 YB_ATTR_nodiscard YB_PURE inline SDst
272 FetchStringWidth(const Font& fnt, const _tString& str, size_t n)
273 {
274 return Drawing::FetchStringWidth(fnt, str.data(), n);
275 }
277
278
284 template<typename _tIter,
285 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
286 YB_ATTR_nodiscard YB_PURE SDst
287 FetchStringWidth(TextState& ts, SDst h, _tIter s)
288 {
289 const SPos x(ts.Pen.X);
290 EmptyTextRenderer r(ts, h);
291
292 PrintString(r, s);
293 return SDst(ts.Pen.X - x);
294 }
301 template<typename _tIter,
302 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
303 YB_ATTR_nodiscard YB_PURE SDst
304 FetchStringWidth(TextState& ts, SDst h, _tIter s, _tIter g, char32_t c = {})
305 {
306 const SPos x(ts.Pen.X);
307 EmptyTextRenderer r(ts, h);
308
309 PrintString(r, s, g, c);
310 return SDst(ts.Pen.X - x);
311 }
313
318 template<class _tString,
319 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
320 YB_ATTR_nodiscard YB_PURE inline SDst
321 FetchStringWidth(TextState& ts, SDst h, const _tString& str)
322 {
323 return Drawing::FetchStringWidth(ts, h, str.c_str());
324 }
325
326
331 template<typename _tIn,
332 yimpl(typename = ystdex::enable_for_iterator_t<_tIn>)>
333 YB_ATTR_nodiscard YB_PURE SDst
334 FetchMaxTextWidth(const Font& font, _tIn first, _tIn last)
335 {
336 return std::accumulate(first, last, SDst(),
337 [&](SDst val, decltype(*first) str) YB_PURE{
338 return ystdex::max(val, Drawing::FetchStringWidth(font, str));
339 });
340 }
341
342 } // namespace Drawing;
343
344 } // namespace YSLib;
345
346 #endif
347
YSLib::Drawing::SPos
YB_ATTR_nodiscard yconstfn YB_PURE x x x Bottom YB_ATTR_nodiscard yconstfn YB_PURE x Right y x Top y x Bottom y Bottom YB_ATTR_nodiscard yconstfn YB_PURE x Right y x Top y x Bottom y Bottom YB_ATTR_nodiscard yconstfn YB_PURE SPos(x.Right *ptrdiff_t(n))
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:87
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
YSLib::Drawing::FetchCharWidth
SDst FetchCharWidth(const Font &fnt, char32_t c)
Definition: TextLayout.cpp:72
YSLib::Drawing::ImplRet
YB_ATTR_nodiscard YB_STATELESS yconstfn Rotation rot ImplRet(Rotation((size_t(rot)+1) % 4)) YB_ATTR_nodiscard YB_STATELESS yconstfn PDefH(Rotation
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
YSLib::Drawing::c
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect Color c
Definition: YDraw.h:174
YSLib::Drawing::FetchResizedLineN
YB_ATTR_nodiscard YF_API YB_PURE std::uint16_t FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YSLib::Drawing::x
const TextState &ts const TextState &ts const TextState &ts TextState SPos x
Definition: TextBase.h:190
YSLib::Drawing::FetchStringWidth
YB_ATTR_nodiscard YB_PURE SDst FetchStringWidth(const Font &fnt, _tIter s)
取迭代器指定的单行字符串在字体指定、无边界限制时的显示宽度。
Definition: TextLayout.h:198
YSLib::Drawing::FetchLastLineBasePosition
YB_ATTR_nodiscard YF_API YB_PURE SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置（纵坐标）。
Definition: TextLayout.cpp:60
YSLib::Drawing::ts
const TextState &ts const TextState &ts const TextState &ts TextState & ts
Definition: TextBase.h:190
YSLib::Drawing::TextState::Pen
Point Pen
笔坐标。
Definition: TextBase.h:98
ystdex::cpp2014::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:635
YF_API
#define YF_API
Definition: Platform.h:56
ystdex::enable_for_string_class_t
enable_if_t< is_string_class< decay_t< _tParam > >::value, _type > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:1019
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::Drawing::PDefH
YB_ATTR_nodiscard YB_PURE yconstfn PDefH(Size, Transpose, const Size &s) ynothrow ImplRet(
转置。
Definition: YGDIBase.h:763
YSLib::Drawing::TextRegion
文本区域。
Definition: TextRenderer.h:492
YSLib::Drawing::FetchMaxTextWidth
YB_ATTR_nodiscard YB_PURE SDst FetchMaxTextWidth(const Font &font, _tIn first, _tIn last)
取指定字体显示的迭代器范围中的文本的最大宽度。
Definition: TextLayout.h:334
YSLib::Drawing::FetchResizedBottomMargin
YB_ATTR_nodiscard YF_API YB_PURE SDst FetchResizedBottomMargin(const TextState &, SDst)
取指定文本状态和文本区域高调整的底边距。
Definition: TextLayout.cpp:44
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
YSLib::IO::_tString
const string &path const String &path class class class _tString
Definition: YSLib/Service/FileSystem.h:612
YSLib::Drawing::PrintString
_tIter PrintString(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:115
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::Drawing::GBinaryGroup::X
_type X
分量。
Definition: YGDIBase.h:305
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YModules.h
YSLib 用户界面模块配置文件。
YSLib::str
const char * str
Definition: YException.cpp:60
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:46
YSLib::Drawing::ImplExpr
const TextState &ts const TextState &ts const TextState &ts TextState SPos SPos y ImplExpr(ts.Pen=Point(x, y)) YF_API void SetCurrentTextLineNOf(TextState &
设置笔的行位置。
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::Drawing::w
The boundary is out of the const Rect SDst w
Definition: YDraw.h:127
YSLib::Drawing::AdjustBottomMarginOf
YB_ATTR_nodiscard YB_PURE tr AdjustBottomMarginOf
Definition: TextLayout.h:82
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:589
ystdex::enable_for_iterator_t
enable_if_t< details::iterator_reqirement_t< _tParam >::value, _type > enable_for_iterator_t
选择迭代器类型的特定重载避免和其它类型冲突。
Definition: iterator_trait.hpp:98
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
YSLib::Drawing::GetHeight
YB_ATTR_nodiscard YB_PURE tr GetHeight())) inline PDefH(void
按字体高度和行距调整文本区域的底边距。
YSLib::Drawing::char32_t
YB_ATTR_nodiscard YB_PURE tr TextRegion &tr char32_t
Definition: TextLayout.h:93
YSLib::Drawing::FetchStringOffsets
YB_ATTR_nodiscard YB_PURE pair< size_t, SDst > FetchStringOffsets(size_t max_width, const Font &fnt, _tIter s)
取迭代器指定的单行字符串在指定字体和宽度时的最多能显示的字符数和宽。
Definition: TextLayout.h:107
YSLib::Drawing::EmptyTextRenderer
空文本渲染器。
Definition: TextRenderer.h:332