YSTest: TextRenderer.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

TextRenderer.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_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
46
48
55 template<typename _tIter, class _tRenderer,
56 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
57 _tIter
58 PrintLine(_tRenderer& rd, _tIter s)
59 {
60 while(*s != 0 && *s != '\n')
61 {
62 PrintChar(rd, *s);
63 ++s;
64 }
65 return s;
66 }
77 template<typename _tIter, class _tRenderer,
78 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
79 _tIter
80 PrintLine(_tRenderer& rd, _tIter s, _tIter g, char32_t c = {})
81 {
82 while(s != g && char32_t(*s) != c && *s != '\n')
83 {
84 PrintChar(rd, *s);
85 ++s;
86 }
87 return s;
88 }
96 template<class _tRenderer, class _tString,
97 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
98 inline String::size_type
99 PrintLine(_tRenderer& rd, const _tString& str)
100 {
101 return String::size_type(Drawing::PrintLine(rd, ystdex::string_begin(str),
102 ystdex::string_end(str)) - ystdex::string_begin(str));
103 }
104
112 template<typename _tIter, class _tRenderer,
113 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
114 _tIter
115 PrintString(_tRenderer& rd, _tIter s)
116 {
117 while(*s != 0 && *s != '\n')
118 PrintChar(rd, *s++);
119 return s;
120 }
131 template<typename _tIter, class _tRenderer,
132 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
133 _tIter
134 PrintString(_tRenderer& rd, _tIter s, _tIter g, char32_t c = {})
135 {
136 while(s != g && char32_t(*s) != c && *s != '\n')
137 PrintChar(rd, *s++);
138 return s;
139 }
147 template<class _tRenderer, class _tString,
148 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
149 inline String::size_type
150 PrintString(_tRenderer& rd, const _tString& str)
151 {
152 return String::size_type(Drawing::PrintString(rd, ystdex::string_begin(str),
153 ystdex::string_end(str)) - ystdex::string_begin(str));
154 }
156
158
159
167 template<typename _tIter, class _tRenderer,
168 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
169 _tIter
170 PutLine(_tRenderer& rd, _tIter s)
171 {
172 const SPos seol(GetEndOfLineOffsetOf(rd));
173
174 if(seol >= 0)
175 {
176 TextState& ts(rd.GetTextState());
177 const SPos fpy(ts.Pen.Y);
178
179 while(*s != 0 && fpy == ts.Pen.Y)
180 if(PutChar(rd, *s, SDst(seol)) != PutCharResult::NeedNewline)
181 ++s;
182 }
183 return s;
184 }
196 template<typename _tIter, class _tRenderer,
197 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
198 _tIter
199 PutLine(_tRenderer& rd, _tIter s, _tIter g, char32_t c = {})
200 {
201 const SPos seol(GetEndOfLineOffsetOf(rd));
202
203 if(seol >= 0)
204 {
205 TextState& ts(rd.GetTextState());
206 const SPos fpy(ts.Pen.Y);
207
208 while(s != g && char32_t(*s) != c && fpy == ts.Pen.Y)
209 if(PutChar(rd, *s, SDst(seol)) != PutCharResult::NeedNewline)
210 ++s;
211 }
212 return s;
213 }
222 template<class _tRenderer, class _tString,
223 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
224 inline String::size_type
225 PutLine(_tRenderer& rd, const _tString& str)
226 {
227 return String::size_type(Drawing::PutLine(rd, ystdex::string_begin(str),
228 ystdex::string_end(str)) - ystdex::string_begin(str));
229 }
230
239 template<typename _tIter, class _tRenderer,
240 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
241 _tIter
242 PutString(_tRenderer& rd, _tIter s)
243 {
244 const SPos seol(GetEndOfLineOffsetOf(rd));
245
246 if(seol >= 0)
247 {
248 TextState& ts(rd.GetTextState());
249 const SPos mpy(FetchLastLineBasePosition(ts, rd.GetHeight()));
250
251 while(*s != 0 && ts.Pen.Y <= mpy)
252 if(PutChar(rd, *s, SDst(seol)) != PutCharResult::NeedNewline)
253 ++s;
254 }
255 return s;
256 }
268 template<typename _tIter, class _tRenderer,
269 yimpl(typename = ystdex::enable_for_iterator_t<_tIter>)>
270 _tIter
271 PutString(_tRenderer& rd, _tIter s, _tIter g, char32_t c = {})
272 {
273 const SPos seol(GetEndOfLineOffsetOf(rd));
274
275 if(seol >= 0)
276 {
277 TextState& ts(rd.GetTextState());
278 const SPos mpy(FetchLastLineBasePosition(ts, rd.GetHeight()));
279
280 while(s != g && char32_t(*s) != c && ts.Pen.Y <= mpy)
281 if(PutChar(rd, *s, SDst(seol)) != PutCharResult::NeedNewline)
282 ++s;
283 }
284 return s;
285 }
293 template<class _tRenderer, class _tString,
294 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
295 inline String::size_type
296 PutString(_tRenderer& rd, const _tString& str)
297 {
298 return String::size_type(Drawing::PutString(rd, ystdex::string_begin(str),
299 ystdex::string_end(str)) - ystdex::string_begin(str));
300 }
302
303
310 template<typename... _tParams>
311 void
312 PutText(bool multi, _tParams&&... args)
313 {
314 if(multi)
315 Drawing::PutString(yforward(args)...);
316 else
317 Drawing::PutLine(yforward(args)...);
318 }
319
320
331 class YF_API EmptyTextRenderer
332 {
333 public:
334 TextState& State;
335 SDst Height;
336
337 EmptyTextRenderer(TextState& ts, SDst h)
338 : State(ts), Height(h)
339 {}
340
345 PDefHOp(void, (), char32_t c)
346 ImplExpr(MovePen(State, c))
347
348 DefGetter(const ynothrow, const TextState&, TextState, State)
349 DefGetter(ynothrow, TextState&, TextState, State)
350 DefGetter(const ynothrow, SDst, Height, Height)
351 };
352
353
359 template<class _type>
360 class GTextRendererBase
361 {
362 public:
363 DeclSEntry(const TextState& GetTextState() const)
364 DeclSEntry(TextState& GetTextState())
365
369 DeclSEntry(Graphics GetContext() const)
370
371 #define This static_cast<_type*>(this)
372 #define CThis static_cast<const _type*>(this)
373
377 DefGetter(const, std::uint16_t, TextLineN, FetchResizedLineN(
378 CThis->GetTextState(), CThis->GetContext().GetHeight()))
382 DefGetter(const, std::uint16_t, TextLineNEx,
383 FetchResizedLineN(CThis->GetTextState(), CThis->GetContext().GetHeight()
384 + CThis->GetTextState().LineGap))
385
386 #undef CThis
387 #undef This
388
389 };
390
391
398 class YF_API TextRenderer : public GTextRendererBase<TextRenderer>
399 {
400 public:
401 TextState& State;
402 const Graphics& Buffer;
403 Rect ClipArea;
404
405 TextRenderer(TextState& ts, const Graphics& g)
406 : GTextRendererBase<TextRenderer>(),
407 State(ts), Buffer(g), ClipArea(g.GetSize())
408 {}
413 TextRenderer(TextState& ts, const Graphics& g, const Rect& bounds)
414 : GTextRendererBase<TextRenderer>(),
415 State(ts), Buffer(g), ClipArea(bounds)
416 {}
417
422 void
423 operator()(char32_t);
424
425 ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
426 TextState, State)
427 ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, State)
429 ImplS(GTextRendererBase) DefGetter(const ynothrow, Graphics, Context,
430 Buffer)
432
433 DefGetterMem(const ynothrow, SDst, Height, Buffer)
434 DefGetterMem(const ynothrow, SDst, Width, Buffer)
435 DefGetterMem(const ynothrow, const Size&, Size, Buffer)
437
445 void
446 ClearLine(size_t l, SDst n);
447 };
448
449
456 class YF_API CustomTextRenderer : public TextRenderer,
457 public GTextRendererBase<CustomTextRenderer>
458 {
459 public:
460 using RenderFunctionType = std::function<void(TextRenderer&, char32_t)>;
461
462 RenderFunctionType RenderFunction;
463
464 CustomTextRenderer(RenderFunctionType f, TextState& ts, const Graphics& g)
465 : TextRenderer(ts, g), RenderFunction(f)
466 {}
467 CustomTextRenderer(RenderFunctionType f, TextState& ts, const Graphics& g,
468 const Rect& bounds)
469 : TextRenderer(ts, g, bounds), RenderFunction(f)
470 {}
471
477 PDefHOp(void, (), char32_t c)
478 ImplExpr(RenderFunction(*this, c))
479 };
480
481
490 class YF_API TextRegion : public GTextRendererBase<TextRegion>,
491 public TextState, public CompactPixmapEx
492 {
493 public:
497 TextRegion();
502 template<typename... _tParams>
503 explicit
504 TextRegion(_tParams&&... args)
505 : GTextRendererBase<TextRegion>(),
506 TextState(yforward(args)...), CompactPixmapEx()
507 {
508 InitializeFont();
509 }
514 DefDeCopyCtor(TextRegion)
519 DefDeMoveCtor(TextRegion)
524 ~TextRegion() override;
525
529 TextRegion&
530 operator=(const TextState& ts)
531 {
532 TextState::operator=(ts);
533 return *this;
534 }
539 DefDeCopyAssignment(TextRegion)
544 DefDeMoveAssignment(TextRegion)
545
550 void
551 operator()(char32_t);
552
553 ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
554 TextState, *this)
555 ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, *this)
557 ImplS(GTextRendererBase) DefGetterBase(const ynothrow override, Graphics,
558 Context, CompactPixmapEx)
559
560 protected:
564 void
565 InitializeFont();
566
567 public:
573 void
574 ClearLine(size_t l, SDst n);
575
581 void
582 ClearTextLine(size_t);
583
589 void
590 Scroll(ptrdiff_t n);
596 void
597 Scroll(ptrdiff_t n, SDst h);
598 };
599
600
614 YF_API void
616 DrawClippedText(const Graphics& g, const Rect& bounds, TextState& ts,
617 const String& str, bool line_wrap);
623 YF_API void
624 DrawClippedText(const Graphics& g, const Rect& bounds, const Rect& r,
625 const String& str, const Padding& m, Color, bool line_wrap,
626 const Font& fnt = {});
628
630
631 YF_API void
633 DrawText(const Graphics& g, TextState& ts, const String& str, bool line_wrap);
639 YF_API void
640 DrawText(const Graphics& g, const Rect& bounds, const String& str,
641 const Padding& m, Color, bool line_wrap, const Font& fnt = {});
648 YF_API void
649 DrawText(TextRegion& r, const Graphics& g, const Point& pt, const Size& s,
650 const String& str, bool line_wrap);
652
653
654 } // namespace Drawing;
655
656 } // namespace YSLib;
657
658 #endif
659
CThis
#define CThis
YSLib::Drawing::TextRenderer::TextRenderer
TextRenderer(TextState &ts, const Graphics &g, const Rect &bounds)
构造：使用文本状态、图形接口上下文和指定区域边界。
Definition: TextRenderer.h:413
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::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:583
YSLib::Drawing::PrintLine
_tIter PrintLine(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至行尾或字符迭代终止。
Definition: TextRenderer.h:58
YSLib::Drawing::EmptyTextRenderer
空文本渲染器。
Definition: TextRenderer.h:331
YSLib::Drawing::DrawClippedText
YF_API void DrawClippedText(const Graphics &g, const Rect &bounds, TextState &ts, const String &str, bool line_wrap)
绘制剪切区域的文本。
Definition: TextRenderer.cpp:225
YSLib::Drawing::ImplExpr
tr TextRegion &tr ImplExpr(tr.Margin.Bottom=SPos(FetchResizedBottomMargin(tr))) YF_API SDst FetchCharWidth(const Font &
取指定的字符使用指定字体的显示宽度。
YSLib::Drawing::FetchResizedLineN
YF_API std::uint16_t FetchResizedLineN(const TextState &ts, SDst)
取指定文本状态和文本区域高所能显示的最大文本行数。
Definition: TextLayout.cpp:52
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib::Drawing::GTextRendererBase
文本渲染器静态多态基类模板。
Definition: TextRenderer.h:360
YSLib::Drawing::GetEndOfLineOffsetOf
SPos GetEndOfLineOffsetOf(const _tRenderer &rd)
取文本渲染器的行末位置剩余偏移。
Definition: CharRenderer.h:148
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::Drawing::EmptyTextRenderer::Height
SDst Height
Definition: TextRenderer.h:335
platform::Color
颜色。
Definition: Video.h:451
YSLib::Drawing::bounds
The boundary is out of the const Rect & bounds
Definition: YDraw.h:127
YSLib::Drawing::Padding
空白样式。
Definition: YGDI.h:46
YSLib::Drawing::EmptyTextRenderer::State
TextState & State
Definition: TextRenderer.h:334
YSLib::Drawing::PrintChar
void PrintChar(_tRenderer &rd, char32_t c)
打印单个可打印字符。
Definition: CharRenderer.h:160
YSLib::Drawing::FetchLastLineBasePosition
YF_API SPos FetchLastLineBasePosition(const TextState &, SDst)
取指定文本状态在指定高的区域中表示的最底行的基线位置（纵坐标）。
Definition: TextLayout.cpp:60
ystdex::string_end
auto string_end(_tRange &c) -> decltype(c.end())
Definition: string.hpp:264
YSLib::Drawing::CustomTextRenderer
定制文本渲染器：使用自定义的渲染函数替代的 TextRenderer 。
Definition: TextRenderer.h:456
YSLib::Drawing::PutLine
_tIter PutLine(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至行尾或字符迭代终止。
Definition: TextRenderer.h:170
YSLib::Drawing::TextRenderer
文本渲染器：简单实现。
Definition: TextRenderer.h:398
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:185
YSLib::Drawing::char32_t
tr TextRegion &tr char32_t
Definition: TextLayout.h:92
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:246
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:46
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
YSLib::Drawing::PrintString
_tIter PrintString(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:115
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
DeclSEntry
#define DeclSEntry(...)
静态接口。
Definition: YBaseMacro.h:403
YSLib::Drawing::TextRenderer::TextRenderer
TextRenderer(TextState &ts, const Graphics &g)
Definition: TextRenderer.h:405
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:219
YSLib::Drawing::CustomTextRenderer::RenderFunctionType
std::function< void(TextRenderer &, char32_t)> RenderFunctionType
Definition: TextRenderer.h:460
ImplS
#define ImplS(...)
静态接口实现。
Definition: YBaseMacro.h:408
YModules.h
YSLib::Drawing::PutText
void PutText(bool multi, _tParams &&...args)
打印文本。
Definition: TextRenderer.h:312
std
Definition: memory.hpp:1100
YSLib::Drawing::TextRenderer::State
TextState & State
Definition: TextRenderer.h:401
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:42
DefGetterBase
#define DefGetterBase(_q, _t, _n, _b)
Definition: YBaseMacro.h:244
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:190
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
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:161
YSLib::Drawing::GetHeight
tr GetHeight())) inline PDefH(void
按字体高度和行距调整文本区域的底边距。
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:540
YSLib::Text::str
const char const char * str
Definition: TextFile.h:77
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:166
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
ystdex::string_begin
auto string_begin(_tRange &c) -> decltype(c.begin())
Definition: string.hpp:233
YSLib::Drawing::MovePen
void MovePen(TextState &ts, char32_t c)
Definition: TextBase.cpp:82
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
Drawing
YSLib::Drawing::TextRegion
文本区域。
Definition: TextRenderer.h:490
YSLib::Drawing::CustomTextRenderer::CustomTextRenderer
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g)
Definition: TextRenderer.h:464
YSLib::Drawing::PutCharResult::NeedNewline
行内无法容纳而换行。
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
YSLib::Drawing::TextRegion::TextRegion
TextRegion(_tParams &&...args)
构造：使用指定参数确定文本状态。
Definition: TextRenderer.h:504
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::Drawing::PutString
_tIter PutString(_tRenderer &rd, _tIter s)
打印迭代器指定的起始字符的字符串，直至区域末尾或字符迭代终止。
Definition: TextRenderer.h:242
YSLib::Drawing::CustomTextRenderer::RenderFunction
RenderFunctionType RenderFunction
Definition: TextRenderer.h:462
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
YSLib::Drawing::EmptyTextRenderer::EmptyTextRenderer
EmptyTextRenderer(TextState &ts, SDst h)
Definition: TextRenderer.h:337
YSLib::Drawing::TextRenderer::Buffer
const Graphics & Buffer
Definition: TextRenderer.h:402
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:86
YSLib::Drawing::GGraphics
二维图形接口上下文模板。
Definition: YGraphics.h:62
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::Drawing::CustomTextRenderer::CustomTextRenderer
CustomTextRenderer(RenderFunctionType f, TextState &ts, const Graphics &g, const Rect &bounds)
Definition: TextRenderer.h:467
YSLib::Drawing::TextRenderer::ClipArea
Rect ClipArea
Definition: TextRenderer.h:403
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
YSLib::Drawing::PutChar
PutCharResult PutChar(_tRenderer &rd, char32_t c, SDst eol)
打印单个字符。
Definition: CharRenderer.h:197
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:43
YSLib::Drawing::DrawText
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
Definition: TextRenderer.cpp:247
YSLib::Drawing::GBinaryGroup< SPos >