YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
TextRenderer.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_TextRenderer_h_
29 #define YSL_INC_Service_TextRenderer_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Service_CharRenderer
33 #include YFM_YSLib_Core_YString
34 
35 namespace YSLib
36 {
37 
38 namespace Drawing
39 {
40 
49 template<typename _tIter, class _tRenderer,
51 _tIter
52 PrintLine(_tRenderer& r, _tIter s)
53 {
54  while(*s != 0 && *s != '\n')
55  {
56  PrintChar(r, *s);
57  ++s;
58  }
59  return s;
60 }
71 template<typename _tIter, class _tRenderer,
73 _tIter
74 PrintLine(_tRenderer& r, _tIter s, _tIter g, ucs4_t c = {})
75 {
76  while(s != g && ucs4_t(*s) != c && *s != '\n')
77  {
78  PrintChar(r, *s);
79  ++s;
80  }
81  return s;
82 }
90 template<class _tRenderer, class _tString,
92 inline String::size_type
93 PrintLine(_tRenderer& r, const _tString& str)
94 {
95  return PrintLine(r, &str[0]) - &str[0];
96 }
97 
107 template<typename _tIter, class _tRenderer,
109 _tIter
110 PutLine(_tRenderer& r, _tIter s)
111 {
112  TextState& ts(r.GetTextState());
113  const SPos fpy(ts.Pen.Y);
114 
115  while(*s != 0 && fpy == ts.Pen.Y)
116  if(!PutChar(r, *s))
117  ++s;
118  return s;
119 }
131 template<typename _tIter, class _tRenderer,
133 _tIter
134 PutLine(_tRenderer& r, _tIter s, _tIter g, ucs4_t c = {})
135 {
136  TextState& ts(r.GetTextState());
137  const SPos fpy(ts.Pen.Y);
138 
139  while(s != g && ucs4_t(*s) != c && fpy == ts.Pen.Y)
140  if(!PutChar(r, *s))
141  ++s;
142  return s;
143 }
152 template<class _tRenderer, class _tString,
154 inline String::size_type
155 PutLine(_tRenderer& r, const _tString& str)
156 {
157  return PutLine(r, &str[0]) - &str[0];
158 }
159 
168 template<typename _tIter, class _tRenderer,
170 _tIter
171 PrintString(_tRenderer& r, _tIter s)
172 {
173  while(*s != 0 && *s != '\n')
174  PrintChar(r, *s++);
175  return s;
176 }
187 template<typename _tIter, class _tRenderer,
189 _tIter
190 PrintString(_tRenderer& r, _tIter s, _tIter g, ucs4_t c = {})
191 {
192  while(s != g && ucs4_t(*s) != c && *s != '\n')
193  PrintChar(r, *s++);
194  return s;
195 }
203 template<class _tRenderer, class _tString,
205 inline String::size_type
206 PrintString(_tRenderer& r, const _tString& str)
207 {
208  return PrintString(r, &str[0]) - &str[0];
209 }
210 
220 template<typename _tIter, class _tRenderer,
222 _tIter
223 PutString(_tRenderer& r, _tIter s)
224 {
225  TextState& ts(r.GetTextState());
226  const SPos mpy(FetchLastLineBasePosition(ts, r.GetHeight()));
227 
228  while(*s != 0 && ts.Pen.Y <= mpy)
229  if(!PutChar(r, *s))
230  ++s;
231  return s;
232 }
244 template<typename _tIter, class _tRenderer,
246 _tIter
247 PutString(_tRenderer& r, _tIter s, _tIter g, ucs4_t c = {})
248 {
249  TextState& ts(r.GetTextState());
250  const SPos mpy(FetchLastLineBasePosition(ts, r.GetHeight()));
251 
252  while(s != g && ucs4_t(*s) != c && ts.Pen.Y <= mpy)
253  if(!PutChar(r, *s))
254  ++s;
255  return s;
256 }
265 template<class _tRenderer, class _tString,
267 inline String::size_type
268 PutString(_tRenderer& r, const _tString& str)
269 {
270  return PutString(r, &str[0]) - &str[0];
271 }
272 
279 template<typename... _tParams>
280 void
281 PutText(bool multi, _tParams&&... args)
282 {
283  if(multi)
284  PutString(yforward(args)...);
285  else
286  PutLine(yforward(args)...);
287 }
288 
289 
301 {
302 public:
305 
307  : State(ts), Height(h)
308  {}
309 
313  PDefHOp(void, (), ucs4_t c)
314  ImplExpr(MovePen(State, c))
315 
316  DefGetter(const ynothrow, const TextState&, TextState, State)
317  DefGetter(ynothrow, TextState&, TextState, State)
318  DefGetter(const ynothrow, SDst, Height, Height)
319 };
320 
321 
327 template<class _type>
329 {
330 public:
331  DeclSEntry(const TextState& GetTextState() const)
332  DeclSEntry(TextState& GetTextState())
333  DeclSEntry(const Graphics& GetContext() const)
334 
335 #define This static_cast<_type*>(this)
336 #define CThis static_cast<const _type*>(this)
337 
341  DefGetter(const, u16, TextLineN, FetchResizedLineN(CThis->GetTextState(),
342  CThis->GetContext().GetHeight()))
346  DefGetter(const, u16, TextLineNEx, FetchResizedLineN(CThis->GetTextState(),
347  CThis->GetContext().GetHeight() + CThis->GetTextState().LineGap))
348 
349 #undef CThis
350 #undef This
351 
352 };
353 
354 
361 class YF_API TextRenderer : public GTextRendererBase<TextRenderer>
362 {
363 public:
364  TextState& State;
365  const Graphics& Buffer;
367 
368  TextRenderer(TextState& ts, const Graphics& g)
369  : GTextRendererBase<TextRenderer>(),
370  State(ts), Buffer(g), ClipArea(g.GetSize())
371  {}
372  /*
373  \brief 构造:使用文本状态、图形接口上下文和指定区域边界。
374  \since build 265
375  */
376  TextRenderer(TextState& ts, const Graphics& g, const Rect& mask)
377  : GTextRendererBase<TextRenderer>(),
378  State(ts), Buffer(g), ClipArea(mask)
379  {}
380 
384  void
385  operator()(ucs4_t);
386 
387  ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
388  TextState, State)
389  ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, State)
390  ImplS(GTextRendererBase) DefGetter(const ynothrow, const Graphics&, Context,
391  Buffer)
393 
394  DefGetterMem(const ynothrow, SDst, Height, Buffer)
395  DefGetterMem(const ynothrow, SDst, Width, Buffer)
396  DefGetterMem(const ynothrow, const Size&, Size, Buffer)
398 
405  void
406  ClearLine(u16 l, SDst n);
407 };
408 
409 
417  public GTextRendererBase<CustomTextRenderer>
418 {
419 public:
420  using RenderFunctionType = std::function<void(TextRenderer&, ucs4_t)>;
421 
423 
424  CustomTextRenderer(RenderFunctionType f, TextState& ts, const Graphics& g)
425  : TextRenderer(ts, g), RenderFunction(f)
426  {}
427  CustomTextRenderer(RenderFunctionType f, TextState& ts, const Graphics& g,
428  const Rect& mask)
429  : TextRenderer(ts, g, mask), RenderFunction(f)
430  {}
431 
436  PDefHOp(void, (), ucs4_t c)
437  ImplExpr(RenderFunction(*this, c))
438 };
439 
440 
449 class YF_API TextRegion : public GTextRendererBase<TextRegion>,
450  public TextState, public CompactPixmapEx
451 {
452 public:
456  TextRegion();
461  template<typename... _tParams>
462  explicit
463  TextRegion(_tParams&&... args)
464  : GTextRendererBase<TextRegion>(),
465  TextState(yforward(args)...), CompactPixmapEx()
466  {
467  InitializeFont();
468  }
479 
483  TextRegion&
484  operator=(const TextState& ts)
485  {
487  return *this;
488  }
499 
503  void
504  operator()(ucs4_t);
505 
506  ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
507  TextState, *this)
508  ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, *this)
509  ImplS(GTextRendererBase) DefGetter(const ynothrow, const Graphics&, Context,
510  *this)
511 
512 protected:
516  void
517  InitializeFont();
518 
519 public:
524  void
525  ClearLine(u16 l, SDst n);
526 
531  void
532  ClearTextLine(u16);
533 
539  void
540  Scroll(ptrdiff_t n);
546  void
547  Scroll(ptrdiff_t n, SDst h);
548 };
549 
550 
560 YF_API void
561 DrawClippedText(const Graphics& g, const Rect& mask, TextState& ts,
562  const String& str, bool line_wrap);
574 YF_API void
575 DrawClippedText(const Graphics& g, const Rect& mask, const Rect& bounds,
576  const String& str, const Padding& m, Color, bool line_wrap,
577  const Font& fnt = {});
578 
587 YF_API void
588 DrawText(const Graphics& g, TextState& ts, const String& str, bool line_wrap);
599 YF_API void
600 DrawText(const Graphics& g, const Rect& bounds, const String& str,
601  const Padding& m, Color, bool line_wrap, const Font& fnt = {});
613 YF_API void
614 DrawText(TextRegion& r, const Graphics& g, const Point& pt, const Size& s,
615  const String& str, bool line_wrap);
616 
617 } // namespace Drawing;
618 
619 } // namespace YSLib;
620 
621 #endif
622 
void PrintChar(_tRenderer &r, ucs4_t c)
打印单个可打印字符。
Definition: CharRenderer.h:148
EmptyTextRenderer(TextState &ts, SDst h)
Definition: TextRenderer.h:306
yconstfn const string _tParams && args
Definition: Loader.h:111
YF_API void DrawClippedText(const Graphics &g, const Rect &mask, TextState &ts, const String &str, bool line_wrap)
绘制剪切区域的文本。
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
#define YF_API
Definition: Platform.h:64
void MovePen(TextState &ts, ucs4_t c)
Definition: TextBase.cpp:80
u8 PutChar(_tRenderer &r, ucs4_t c)
打印单个字符。
Definition: CharRenderer.h:171
TextRenderer(TextState &ts, const Graphics &g, const Rect &mask)
Definition: TextRenderer.h:376
_tIter PrintString(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:171
TextRenderer(TextState &ts, const Graphics &g)
Definition: TextRenderer.h:368
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
文本渲染器静态多态基类模板。
Definition: TextRenderer.h:328
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
定制文本渲染器:使用自定义的渲染函数替代的 TextRenderer 。
Definition: TextRenderer.h:416
#define yimpl(...)
实现标签。
Definition: ydef.h:177
YF_API SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置(纵坐标)。
Definition: TextLayout.cpp:60
CompactPixmapEx & operator=(const CompactPixmapEx &buf)
Definition: ygdi.h:328
CompactPixmapEx(ConstBitmapPtr, SDst, SDst)
构造:使用指定位图指针和大小。
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
#define ImplExpr(...)
Definition: YBaseMacro.h:93
YF_API u16 FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
std::function< void(TextRenderer &, ucs4_t)> RenderFunctionType
Definition: TextRenderer.h:420
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
文本状态。
Definition: TextBase.h:87
文本渲染器:简单实现。
Definition: TextRenderer.h:361
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
TextRegion(_tParams &&...args)
构造:使用指定参数确定文本状态。
Definition: TextRenderer.h:463
空文本渲染器。
Definition: TextRenderer.h:300
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g, const Rect &mask)
Definition: TextRenderer.h:427
#define DeclSEntry(...)
静态接口。
Definition: YBaseMacro.h:323
#define CThis
二维图形接口上下文。
Definition: ygdibase.h:721
_tWidget _fCallable && f
Definition: ywgtevt.h:597
_tIter PrintLine(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至行尾或字符迭代终止。
Definition: TextRenderer.h:52
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g)
Definition: TextRenderer.h:424
_tIter PutLine(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至行尾或字符迭代终止。
Definition: TextRenderer.h:110
_tIter PutString(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串,直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:223
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
空白样式。
Definition: ygdi.h:46
#define ImplS(...)
静态接口实现。
Definition: YBaseMacro.h:328
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
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:185
void PutText(bool multi, _tParams &&...args)
打印文本。
Definition: TextRenderer.h:281
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
c yconstfn g
Definition: ystyle.h:104
字体:字模,包含字型、样式和大小。
Definition: Font.h:546
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
std::uint16_t u16
Definition: yadaptor.h:68
颜色。
Definition: Video.h:339
屏幕区域大小。
Definition: ygdibase.h:249
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154