YSTest: TextLayout.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

TextLayout.h
浏览该文件的文档.
1 /*
2 © 2009-2015 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 YF_API SDst
49 FetchResizedBottomMargin(const TextState&, SDst);
50
57 YF_API std::uint16_t
58 FetchResizedLineN(const TextState& ts, SDst);
59
66 YF_API SPos
67 FetchLastLineBasePosition(const TextState&, SDst);
68
69
74 inline PDefH(SDst, FetchResizedBottomMargin, const TextRegion& tr)
75 ImplRet(FetchResizedBottomMargin(tr, tr.GetHeight()))
76
77
81 inline PDefH(void, AdjustBottomMarginOf, TextRegion& tr)
82 // XXX: Conversion to 'SPos' might be implementation-defined.
83 ImplExpr(tr.Margin.Bottom = SPos(FetchResizedBottomMargin(tr)))
84
85
91 YF_API SDst
92 FetchCharWidth(const Font&, char32_t);
93
103 template<typename _tIter,
104 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
105 pair<size_t, SDst>
106 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s)
107 {
108 size_t r(0);
109 SDst w(0);
110
111 for(; *s != char() && w < max_width; yunseq(++s, ++r))
112 {
113 using ystdex::is_undereferenceable;
114
115 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
116 w += FetchCharWidth(fnt, *s);
117 }
118 return {r, w};
119 }
121
122
126 template<typename _tIter,
127 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
128 pair<size_t, SDst>
129 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, size_t n,
130 char32_t c = {})
131 {
132 size_t r(0);
133 SDst w(0);
134
135 for(; n-- != 0 && *s != c && w < max_width; yunseq(++s, ++r))
136 {
137 using ystdex::is_undereferenceable;
138
139 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
140 w += FetchCharWidth(fnt, *s);
141 }
142 return {r, w};
143 }
148 template<typename _tIter,
149 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
150 pair<size_t, SDst>
151 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, _tIter g,
152 char32_t c = {})
153 {
154 size_t r(0);
155 SDst w(0);
156
157 for(; s != g && *s != c && w < max_width; yunseq(++s, ++r))
158 {
159 using ystdex::is_undereferenceable;
160
161 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
162 w += FetchCharWidth(fnt, *s);
163 }
164 return {r, w};
165 }
167 template<class _tString,
169 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
170 inline pair<size_t, SDst>
171 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str)
172 {
173 return Drawing::FetchStringOffsets(max_width, fnt, str.c_str());
174 }
179 template<class _tString,
180 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
181 inline pair<size_t, SDst>
182 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str,
183 size_t n)
184 {
185 return Drawing::FetchStringOffsets(max_width, fnt, str.data(), n);
186 }
188
194 template<typename _tIter,
195 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
196 SDst
197 FetchStringWidth(const Font& fnt, _tIter s)
198 {
199 SDst w(0);
200
201 for(; *s != char(); ++s)
202 {
203 using ystdex::is_undereferenceable;
204
205 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
206 w += FetchCharWidth(fnt, *s);
207 }
208 return w;
209 }
211
212
216 template<typename _tIter,
217 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
218 SDst
219 FetchStringWidth(const Font& fnt, _tIter s, size_t n, char32_t c = {})
220 {
221 SDst w(0);
222
223 for(; n-- != 0 && *s != c; ++s)
224 {
225 using ystdex::is_undereferenceable;
226
227 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
228 w += FetchCharWidth(fnt, *s);
229 }
230 return w;
231 }
236 template<typename _tIter,
237 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
238 SDst
239 FetchStringWidth(const Font& fnt, _tIter s, _tIter g, char32_t c = {})
240 {
241 SDst w(0);
242
243 for(; s != g && *s != c; ++s)
244 {
245 using ystdex::is_undereferenceable;
246
247 YAssert(!is_undereferenceable(s), "Invalid iterator found.");
248 w += FetchCharWidth(fnt, *s);
249 }
250 return w;
251 }
253
257 template<class _tString,
258 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
259 inline SDst
260 FetchStringWidth(const Font& fnt, const _tString& str)
261 {
262 return Drawing::FetchStringWidth(fnt, str.c_str());
263 }
268 template<class _tString,
269 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
270 inline SDst
271 FetchStringWidth(const Font& fnt, const _tString& str, size_t n)
272 {
273 return Drawing::FetchStringWidth(fnt, str.data(), n);
274 }
281 template<typename _tIter,
282 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
283 SDst
284 FetchStringWidth(TextState& ts, SDst h, _tIter s)
285 {
286 const SPos x(ts.Pen.X);
287 EmptyTextRenderer r(ts, h);
288
289 PrintString(r, s);
290 return SDst(ts.Pen.X - x);
291 }
298 template<typename _tIter,
299 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
300 SDst
301 FetchStringWidth(TextState& ts, SDst h, _tIter s, _tIter g, char32_t c = {})
302 {
303 const SPos x(ts.Pen.X);
304 EmptyTextRenderer r(ts, h);
305
306 PrintString(r, s, g, c);
307 return SDst(ts.Pen.X - x);
308 }
314 template<class _tString,
315 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
316 inline SDst
317 FetchStringWidth(TextState& ts, SDst h, const _tString& str)
318 {
319 return FetchStringWidth(ts, h, str.c_str());
320 }
321
322
327 template<typename _tIn,
328 yimpl(typename = ystdex::enable_for_iterator_t<_tIn>)>
329 SDst
330 FetchMaxTextWidth(const Font& font, _tIn first, _tIn last)
331 {
332 return std::accumulate(first, last, SDst(),
333 [&](SDst val, decltype(*first) str){
334 return max(val, FetchStringWidth(font, str));
335 });
336 }
337
338 } // namespace Drawing;
339
340 } // namespace YSLib;
341
342 #endif
343
YSLib::Drawing::TextRegion
文本区域。
Definition: TextRenderer.h:490
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:583
YSLib::Drawing::EmptyTextRenderer
空文本渲染器。
Definition: TextRenderer.h:331
YModules.h
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
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
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:549
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:274
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1022
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
YSLib::Drawing::w
The boundary is out of the const Rect SDst w
Definition: YDraw.h:127
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:86
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
YSLib::Drawing::AdjustBottomMarginOf
tr AdjustBottomMarginOf
Definition: TextLayout.h:81
YSLib::Drawing::ImplExpr
tr TextRegion &tr ImplExpr(tr.Margin.Bottom=SPos(FetchResizedBottomMargin(tr))) YF_API SDst FetchCharWidth(const Font &
取指定的字符使用指定字体的显示宽度。
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::Drawing::FetchCharWidth
SDst FetchCharWidth(const Font &fnt, char32_t c)
Definition: TextLayout.cpp:72
Drawing
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
YSLib::Drawing::FetchResizedLineN
YF_API std::uint16_t FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
ystdex::enable_for_iterator_t
enable_if_t< is_same< decltype(++std::declval< _tParam & >()), _tParam & >::value, _type > enable_for_iterator_t
选择迭代器类型的特定重载避免和其它类型冲突。
Definition: iterator_trait.hpp:57
YSLib::Drawing::FetchLastLineBasePosition
YF_API SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置（纵坐标）。
Definition: TextLayout.cpp:60
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
YSLib::Drawing::FetchMaxTextWidth
SDst FetchMaxTextWidth(const Font &font, _tIn first, _tIn last)
取指定字体显示的迭代器范围中的文本的最大宽度。
Definition: TextLayout.h:330
YSLib::Drawing::FetchStringOffsets
pair< size_t, SDst > FetchStringOffsets(size_t max_width, const Font &fnt, _tIter s)
取迭代器指定的单行字符串在指定字体和宽度时的最多能显示的字符数和宽。
Definition: TextLayout.h:106
YSLib::Text::str
const char const char * str
Definition: TextFile.h:81
YSLib::Drawing::GBinaryGroup::X
_type X
Definition: YGDIBase.h:63
YSLib::Drawing::PrintString
_tIter PrintString(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:115
YSLib::Drawing::char32_t
tr TextRegion &tr char32_t
Definition: TextLayout.h:92
YSLib::Drawing::PDefH
yconstfn PDefH(Size, Transpose, const Size &s) ynothrow ImplRet(
转置。
Definition: YGDIBase.h:516
YSLib::Drawing::FetchResizedBottomMargin
YF_API SDst FetchResizedBottomMargin(const TextState &, SDst)
取指定文本状态和文本区域高调整的底边距。
Definition: TextLayout.cpp:44
YSLib::Drawing::TextState::Pen
Point Pen
笔坐标。
Definition: TextBase.h:98
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::FetchStringWidth
SDst FetchStringWidth(const Font &fnt, _tIter s)
取迭代器指定的单行字符串在字体指定、无边界限制时的显示宽度。
Definition: TextLayout.h:197