YSTest: TextRenderer.h 源文件

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,
50 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
72 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
91 yimpl(typename = ystdex::enable_for_string_class_t<_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,
108 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
132 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
153 yimpl(typename = ystdex::enable_for_string_class_t<_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,
169 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
188 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
204 yimpl(typename = ystdex::enable_for_string_class_t<_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,
221 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
245 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
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,
266 yimpl(typename = ystdex::enable_for_string_class_t<_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
300 class YF_API EmptyTextRenderer
301 {
302 public:
303 TextState& State;
304 SDst Height;
305
306 EmptyTextRenderer(TextState& ts, SDst h)
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>
328 class GTextRendererBase
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;
366 Rect ClipArea;
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
416 class YF_API CustomTextRenderer : public TextRenderer,
417 public GTextRendererBase<CustomTextRenderer>
418 {
419 public:
420 using RenderFunctionType = std::function<void(TextRenderer&, ucs4_t)>;
421
422 RenderFunctionType RenderFunction;
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 }
473 DefDeCopyCtor(TextRegion)
478 DefDeMoveCtor(TextRegion)
479
483 TextRegion&
484 operator=(const TextState& ts)
485 {
486 TextState::operator=(ts);
487 return *this;
488 }
493 DefDeCopyAssignment(TextRegion)
498 DefDeMoveAssignment(TextRegion)
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
YSLib::Drawing::TextRenderer::Buffer
const Graphics & Buffer
Definition: TextRenderer.h:365
YSLib::Drawing::PrintChar
void PrintChar(_tRenderer &r, ucs4_t c)
打印单个可打印字符。
Definition: CharRenderer.h:148
YSLib::Drawing::EmptyTextRenderer::EmptyTextRenderer
EmptyTextRenderer(TextState &ts, SDst h)
Definition: TextRenderer.h:306
YSLib::Drawing::TextRenderer::ClipArea
Rect ClipArea
Definition: TextRenderer.h:366
YSLib::Drawing::TextRenderer::State
TextState & State
Definition: TextRenderer.h:364
YSLib::UI::args
yconstfn const string _tParams && args
Definition: Loader.h:111
YSLib::Drawing::bounds
bounds
Definition: ydraw.h:188
platform_ex::Windows::l
l
Definition: MinGW32.cpp:50
YSLib::Drawing::DrawClippedText
YF_API void DrawClippedText(const Graphics &g, const Rect &mask, TextState &ts, const String &str, bool line_wrap)
绘制剪切区域的文本。
Definition: TextRenderer.cpp:214
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
YF_API
#define YF_API
Definition: Platform.h:64
YSLib::Drawing::MovePen
void MovePen(TextState &ts, ucs4_t c)
Definition: TextBase.cpp:80
YSLib::Drawing::PutChar
u8 PutChar(_tRenderer &r, ucs4_t c)
打印单个字符。
Definition: CharRenderer.h:171
YSLib::Drawing::TextRenderer::TextRenderer
TextRenderer(TextState &ts, const Graphics &g, const Rect &mask)
Definition: TextRenderer.h:376
YSLib::Drawing::EmptyTextRenderer::Height
SDst Height
Definition: TextRenderer.h:304
YSLib::Drawing::PrintString
_tIter PrintString(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:171
YSLib::Drawing::TextRenderer::TextRenderer
TextRenderer(TextState &ts, const Graphics &g)
Definition: TextRenderer.h:368
ystdex::enable_for_string_class_t
enable_if_t< is_class< decay_t< _tParam >>::value, int > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:67
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:38
YSLib::Drawing::GTextRendererBase
文本渲染器静态多态基类模板。
Definition: TextRenderer.h:328
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
YSLib::Drawing::CustomTextRenderer
定制文本渲染器：使用自定义的渲染函数替代的 TextRenderer 。
Definition: TextRenderer.h:416
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:177
YSLib::Drawing::FetchLastLineBasePosition
YF_API SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置（纵坐标）。
Definition: TextLayout.cpp:60
YSLib::Drawing::operator=
CompactPixmapEx & operator=(const CompactPixmapEx &buf)
Definition: ygdi.h:328
YSLib::Drawing::CompactPixmapEx
CompactPixmapEx(ConstBitmapPtr, SDst, SDst)
构造：使用指定位图指针和大小。
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: ystring.h:47
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
ImplExpr
#define ImplExpr(...)
Definition: YBaseMacro.h:93
YSLib::Drawing::FetchResizedLineN
YF_API u16 FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::Drawing::CustomTextRenderer::RenderFunctionType
std::function< void(TextRenderer &, ucs4_t)> RenderFunctionType
Definition: TextRenderer.h:420
YSLib::Drawing::DrawText
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
Definition: TextRenderer.cpp:236
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:87
YSLib::Drawing::EmptyTextRenderer::State
TextState & State
Definition: TextRenderer.h:303
YSLib::Drawing::TextRenderer
文本渲染器：简单实现。
Definition: TextRenderer.h:361
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: ygdibase.h:416
YSLib::Drawing::TextRegion::TextRegion
TextRegion(_tParams &&...args)
构造：使用指定参数确定文本状态。
Definition: TextRenderer.h:463
YSLib::Drawing::GBinaryGroup< SPos >
YSLib::Drawing::EmptyTextRenderer
空文本渲染器。
Definition: TextRenderer.h:300
YSLib::Drawing::CustomTextRenderer::CustomTextRenderer
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g, const Rect &mask)
Definition: TextRenderer.h:427
DeclSEntry
#define DeclSEntry(...)
静态接口。
Definition: YBaseMacro.h:323
CThis
#define CThis
YSLib::Drawing::Graphics
二维图形接口上下文。
Definition: ygdibase.h:721
YSLib::UI::f
_tWidget _fCallable && f
Definition: ywgtevt.h:597
YSLib::Drawing::PrintLine
_tIter PrintLine(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串，直至行尾或字符迭代终止。
Definition: TextRenderer.h:52
YSLib::Drawing::CustomTextRenderer::CustomTextRenderer
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g)
Definition: TextRenderer.h:424
YSLib::Drawing::PutLine
_tIter PutLine(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串，直至行尾或字符迭代终止。
Definition: TextRenderer.h:110
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
YSLib::Drawing::PutString
_tIter PutString(_tRenderer &r, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:223
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
YSLib::Drawing::c
c
Definition: ystyle.h:93
YSLib::Drawing::Padding
空白样式。
Definition: ygdi.h:46
ImplS
#define ImplS(...)
静态接口实现。
Definition: YBaseMacro.h:328
ystdex::enable_for_iterator_t
enable_if_t< is_same< decltype(++std::declval< _tParam & >()), _tParam & >::value, int > enable_for_iterator_t
选择迭代器类型的特定重载避免和其它类型冲突。
Definition: iterator.hpp:60
CHRLib::ucs4_t
char32_t ucs4_t
UCS-4 字符类型。
Definition: chrdef.h:45
YSLib::Drawing::r
bounds & r
Definition: ydraw.h:220
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:185
YSLib::Drawing::PutText
void PutText(bool multi, _tParams &&...args)
打印文本。
Definition: TextRenderer.h:281
YSLib::Drawing::CustomTextRenderer::RenderFunction
RenderFunctionType RenderFunction
Definition: TextRenderer.h:422
YSLib::Drawing::Width
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
YSLib::Drawing::g
c yconstfn g
Definition: ystyle.h:104
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:546
YSLib::Drawing::DefGetter
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
YSLib::u16
std::uint16_t u16
Definition: yadaptor.h:68
platform::Color
颜色。
Definition: Video.h:339
YModules.h
YSLib::Drawing::TextRegion
文本区域。
Definition: TextRenderer.h:449
YSLib::Drawing::Size
屏幕区域大小。
Definition: ygdibase.h:249
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154