YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
TextLayout.h
浏览该文件的文档.
1 /*
2  © 2009-2014 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 
34 namespace YSLib
35 {
36 
37 namespace Drawing
38 {
39 
48 FetchResizedBottomMargin(const TextState&, SDst);
49 
56 YF_API u16
57 FetchResizedLineN(const TextState& ts, SDst);
58 
66 FetchLastLineBasePosition(const TextState&, SDst);
67 
68 
73 inline SDst
75 {
76  return FetchResizedBottomMargin(tr, tr.GetHeight());
77 }
78 
83 inline SDst
85 {
86  return tr.Margin.Bottom = FetchResizedBottomMargin(tr);
87 }
88 
89 
96 FetchCharWidth(const Font&, ucs4_t);
97 
107 template<typename _tIter,
109 pair<size_t, SDst>
110 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s)
111 {
112  size_t r(0);
113  SDst w(0);
114 
115  for(; *s != char() && w < max_width; yunseq(++s, ++r))
116  {
117  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
118  w += FetchCharWidth(fnt, *s);
119  }
120  return {r, w};
121 }
126 template<typename _tIter,
128 pair<size_t, SDst>
129 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, size_t n,
130  ucs4_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  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
138  w += FetchCharWidth(fnt, *s);
139  }
140  return {r, w};
141 }
146 template<typename _tIter,
148 pair<size_t, SDst>
149 FetchStringOffsets(size_t max_width, const Font& fnt, _tIter s, _tIter g,
150  ucs4_t c = {})
151 {
152  size_t r(0);
153  SDst w(0);
154 
155  for(; s != g && *s != c && w < max_width; yunseq(++s, ++r))
156  {
157  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
158  w += FetchCharWidth(fnt, *s);
159  }
160  return {r, w};
161 }
163 template<class _tString,
165 inline pair<size_t, SDst>
166 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str)
167 {
168  return FetchStringOffsets(max_width, fnt, &str[0]);
169 }
174 template<class _tString,
176 inline pair<size_t, SDst>
177 FetchStringOffsets(size_t max_width, const Font& fnt, const _tString& str,
178  size_t n)
179 {
180  return FetchStringOffsets(max_width, fnt, &str[0], n);
181 }
183 
189 template<typename _tIter,
191 SDst
192 FetchStringWidth(const Font& fnt, _tIter s)
193 {
194  SDst w(0);
195 
196  for(; *s != char(); ++s)
197  {
198  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
199  w += FetchCharWidth(fnt, *s);
200  }
201  return w;
202 }
208 template<typename _tIter,
210 SDst
211 FetchStringWidth(const Font& fnt, _tIter s, size_t n, ucs4_t c = {})
212 {
213  SDst w(0);
214 
215  for(; n-- != 0 && *s != c; ++s)
216  {
217  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
218  w += FetchCharWidth(fnt, *s);
219  }
220  return w;
221 }
227 template<typename _tIter,
229 SDst
230 FetchStringWidth(const Font& fnt, _tIter s, _tIter g, ucs4_t c = {})
231 {
232  SDst w(0);
233 
234  for(; s != g && *s != c; ++s)
235  {
236  YAssert(!ystdex::is_undereferenceable(s), "Invalid iterator found.");
237  w += FetchCharWidth(fnt, *s);
238  }
239  return w;
240 }
245 template<class _tString,
247 inline SDst
248 FetchStringWidth(const Font& fnt, const _tString& str)
249 {
250  return FetchStringWidth(fnt, &str[0]);
251 }
256 template<class _tString,
258 inline SDst
259 FetchStringWidth(const Font& fnt, const _tString& str, size_t n)
260 {
261  return FetchStringWidth(fnt, &str[0], n);
262 }
269 template<typename _tIter,
271 SDst
272 FetchStringWidth(TextState& ts, SDst h, _tIter s)
273 {
274  const SPos x(ts.Pen.X);
275  EmptyTextRenderer r(ts, h);
276 
277  PrintString(r, s);
278  return ts.Pen.X - x;
279 }
286 template<typename _tIter,
288 SDst
289 FetchStringWidth(TextState& ts, SDst h, _tIter s, _tIter g, ucs4_t c = {})
290 {
291  const SPos x(ts.Pen.X);
292  EmptyTextRenderer r(ts, h);
293 
294  PrintString(r, s, g, c);
295  return ts.Pen.X - x;
296 }
302 template<class _tString,
304 inline SDst
305 FetchStringWidth(TextState& ts, SDst h, const _tString& str)
306 {
307  return FetchStringWidth(ts, h, &str[0]);
308 }
309 
310 
315 template<typename _tIter,
317 SDst
318 FetchMaxTextWidth(const Font& font, _tIter first, _tIter last)
319 {
320  SDst max_width(0);
321 
322  std::for_each(first, last,
323  [&](decltype(*first)& str){
324  SDst ln_width(FetchStringWidth(font, str));
325 
326  if(ln_width > max_width)
327  max_width = ln_width;
328  });
329  return max_width;
330 }
331 
332 } // namespace Drawing;
333 
334 } // namespace YSLib;
335 
336 #endif
337 
Point Pen
笔坐标。
Definition: TextBase.h:99
SDst FetchStringWidth(const Font &fnt, _tIter s)
取迭代器指定的单行字符串在字体指定、无边界限制时的显示宽度。
Definition: TextLayout.h:192
static auto first(const _tIterator &i) -> decltype((i->first))
Definition: iterator.hpp:759
pair< size_t, SDst > FetchStringOffsets(size_t max_width, const Font &fnt, _tIter s)
取迭代器指定的单行字符串在指定字体和宽度时的最多能显示的字符数和宽。
Definition: TextLayout.h:110
SDst FetchMaxTextWidth(const Font &font, _tIter first, _tIter last)
取指定字体显示的迭代器范围中的文本的最大宽度。
Definition: TextLayout.h:318
#define YF_API
Definition: Platform.h:64
_tIter PrintString(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:171
enable_if_t< is_class< decay_t< _tParam >>::value, int > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:67
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:38
YF_API SDst FetchResizedBottomMargin(const TextState &, SDst)
取指定文本状态和文本区域高调整的底边距。
Definition: TextLayout.cpp:44
SDst AdjustBottomMarginOf(TextRegion &tr)
按字体高度和行距调整文本区域的底边距。
Definition: TextLayout.h:84
#define yimpl(...)
实现标签。
Definition: ydef.h:177
YF_API SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置(纵坐标)。
Definition: TextLayout.cpp:60
Padding Margin
边距:文本区域到显示区域的距离。
Definition: TextBase.h:94
YF_API SDst FetchCharWidth(const Font &, ucs4_t)
取指定的字符使用指定字体的显示宽度。
Definition: TextLayout.cpp:71
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
YF_API u16 FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
文本状态。
Definition: TextBase.h:87
空文本渲染器。
Definition: TextRenderer.h:300
bool is_undereferenceable(const any_input_iterator< _type, _tDifference, _tPointer, _tReference > &i)
enable_if_t< is_same< decltype(++std::declval< _tParam & >()), _tParam & >::value, int > enable_for_iterator_t
选择迭代器类型的特定重载避免和其它类型冲突。
Definition: iterator.hpp:60
char32_t ucs4_t
UCS-4 字符类型。
Definition: chrdef.h:45
bounds & r
Definition: ydraw.h:220
c yconstfn g
Definition: ystyle.h:104
字体:字模,包含字型、样式和大小。
Definition: Font.h:546
std::uint16_t u16
Definition: yadaptor.h:68
#define YAssert(_expr, _msg)
Definition: cassert.h:73