YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
TextRenderer.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
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 "CharRenderer.h"
32 #include "../Core/ystring.h"
33 
35 
36 YSL_BEGIN_NAMESPACE(Drawing)
37 
38 
46 template<typename _tIn, class _tRenderer>
47 _tIn
48 PrintLine(_tRenderer& r, _tIn s)
49 {
50  while(*s != 0 && *s != '\n')
51  {
52  PrintChar(r, *s);
53  ++s;
54  }
55  return s;
56 }
67 template<typename _tIn, class _tRenderer>
68 _tIn
69 PrintLine(_tRenderer& r, _tIn s, _tIn g, ucs4_t c = {})
70 {
71  while(s != g && ucs4_t(*s) != c && *s != '\n')
72  {
73  PrintChar(r, *s);
74  ++s;
75  }
76  return s;
77 }
85 template<class _tRenderer>
86 inline String::size_type
87 PrintLine(_tRenderer& r, const String& str)
88 {
89  return PrintLine(r, str.c_str()) - str.c_str();
90 }
91 
101 template<typename _tIn, class _tRenderer>
102 _tIn
103 PutLine(_tRenderer& r, _tIn s)
104 {
105  TextState& ts(r.GetTextState());
106  const SPos fpy(ts.Pen.Y);
107 
108  while(*s != 0 && fpy == ts.Pen.Y)
109  if(!PutChar(r, *s))
110  ++s;
111  return s;
112 }
124 template<typename _tIn, class _tRenderer>
125 _tIn
126 PutLine(_tRenderer& r, _tIn s, _tIn g, ucs4_t c = {})
127 {
128  TextState& ts(r.GetTextState());
129  const SPos fpy(ts.Pen.Y);
130 
131  while(s != g && ucs4_t(*s) != c && fpy == ts.Pen.Y)
132  if(!PutChar(r, *s))
133  ++s;
134  return s;
135 }
144 template<class _tRenderer>
145 inline String::size_type
146 PutLine(_tRenderer& r, const String& str)
147 {
148  return PutLine(r, str.c_str()) - str.c_str();
149 }
150 
159 template<typename _tIn, class _tRenderer>
160 _tIn
161 PrintString(_tRenderer& r, _tIn s)
162 {
163  while(*s != 0 && *s != '\n')
164  PrintChar(r, *s++);
165  return s;
166 }
177 template<typename _tIn, class _tRenderer>
178 _tIn
179 PrintString(_tRenderer& r, _tIn s, _tIn g, ucs4_t c = {})
180 {
181  while(s != g && ucs4_t(*s) != c && *s != '\n')
182  PrintChar(r, *s++);
183  return s;
184 }
192 template<class _tRenderer>
193 inline String::size_type
194 PrintString(_tRenderer& r, const String& str)
195 {
196  return PrintString(r, str.c_str()) - str.c_str();
197 }
198 
208 template<typename _tIn, class _tRenderer>
209 _tIn
210 PutString(_tRenderer& r, _tIn s)
211 {
212  TextState& ts(r.GetTextState());
213  const SPos mpy(FetchLastLineBasePosition(ts, r.GetHeight()));
214 
215  while(*s != 0 && ts.Pen.Y <= mpy)
216  if(!PutChar(r, *s))
217  ++s;
218  return s;
219 }
231 template<typename _tIn, class _tRenderer>
232 _tIn
233 PutString(_tRenderer& r, _tIn s, _tIn g, ucs4_t c = {})
234 {
235  TextState& ts(r.GetTextState());
236  const SPos mpy(FetchLastLineBasePosition(ts, r.GetHeight()));
237 
238  while(s != g && ucs4_t(*s) != c && ts.Pen.Y <= mpy)
239  if(!PutChar(r, *s))
240  ++s;
241  return s;
242 }
251 template<class _tRenderer>
252 inline String::size_type
253 PutString(_tRenderer& r, const String& str)
254 {
255  return PutString(r, str.c_str()) - str.c_str();
256 }
257 
258 
270 {
271 public:
274 
276  : State(ts), Height(h)
277  {}
278 
282  void
283  operator()(ucs4_t c)
284  {
285  MovePen(State, c);
286  }
287 
288  DefGetter(const ynothrow, const TextState&, TextState, State)
289  DefGetter(ynothrow, TextState&, TextState, State)
290  DefGetter(const ynothrow, SDst, Height, Height)
291 };
292 
293 
299 template<class _type>
301 {
302 public:
303  DeclSEntry(const TextState& GetTextState() const)
304  DeclSEntry(TextState& GetTextState())
305  DeclSEntry(const Graphics& GetContext() const)
306 
307 #define This static_cast<_type*>(this)
308 #define CThis static_cast<const _type*>(this)
309 
313  DefGetter(const, u16, TextLineN, FetchResizedLineN(CThis->GetTextState(),
314  CThis->GetContext().GetHeight()))
318  DefGetter(const, u16, TextLineNEx, FetchResizedLineN(CThis->GetTextState(),
319  CThis->GetContext().GetHeight() + CThis->GetTextState().LineGap))
320 
321 #undef CThis
322 #undef This
323 
324 };
325 
326 
335 class YF_API TextRenderer : public GTextRendererBase<TextRenderer>
336 {
337 public:
338  TextState& State;
339  const Graphics& Buffer;
341 
342  TextRenderer(TextState& ts, const Graphics& g)
343  : GTextRendererBase<TextRenderer>(),
344  State(ts), Buffer(g), ClipArea(g.GetSize())
345  {}
346  /*
347  \brief 构造:使用文本状态、图形接口上下文和指定区域边界。
348  \since build 265
349  */
350  TextRenderer(TextState& ts, const Graphics& g, const Rect& mask)
351  : GTextRendererBase<TextRenderer>(),
352  State(ts), Buffer(g), ClipArea(mask)
353  {}
354 
358  void
359  operator()(ucs4_t);
360 
361  ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
362  TextState, State)
363  ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, State)
364  ImplS(GTextRendererBase) DefGetter(const ynothrow, const Graphics&, Context,
365  Buffer)
367 
368  DefGetterMem(const ynothrow, SDst, Height, Buffer)
369  DefGetterMem(const ynothrow, SDst, Width, Buffer)
370  DefGetterMem(const ynothrow, const Size&, Size, Buffer)
372 
379  void
380  ClearLine(u16 l, SDst n);
381 };
382 
383 
391 class YF_API TextRegion : public GTextRendererBase<TextRegion>,
392  public TextState, public BitmapBufferEx
393 {
394 public:
398  TextRegion();
403  template<typename... _tParams>
404  explicit
405  TextRegion(_tParams&&... args)
406  : GTextRendererBase<TextRegion>(),
407  TextState(yforward(args)...), BitmapBufferEx()
408  {
409  InitializeFont();
410  }
415  DefDeCopyCtor(TextRegion)
420  DefDeMoveCtor(TextRegion)
421 
425  TextRegion&
426  operator=(const TextState& ts)
427  {
428  TextState::operator=(ts);
429  return *this;
430  }
435  DefDeCopyAssignment(TextRegion)
440  DefDeMoveAssignment(TextRegion)
441 
445  void
446  operator()(ucs4_t);
447 
448  ImplS(GTextRendererBase) DefGetter(const ynothrow, const TextState&,
449  TextState, *this)
450  ImplS(GTextRendererBase) DefGetter(ynothrow, TextState&, TextState, *this)
451  ImplS(GTextRendererBase) DefGetter(const ynothrow, const Graphics&, Context,
452  *this)
453 
454 protected:
458  void
459  InitializeFont();
460 
461 public:
466  void
467  ClearLine(u16 l, SDst n);
468 
473  void
474  ClearTextLine(u16);
475 
481  void
482  Scroll(ptrdiff_t n);
488  void
489  Scroll(ptrdiff_t n, SDst h);
490 };
491 
492 
502 YF_API void
503 DrawClippedText(const Graphics& g, const Rect& mask, TextState& ts,
504  const String& str, bool line_wrap);
516 YF_API void
517 DrawClippedText(const Graphics& g, const Rect& mask, const Rect& bounds,
518  const String& str, const Padding& m, Color, bool line_wrap,
519  const Font& fnt = {});
520 
529 YF_API void
530 DrawText(const Graphics& g, TextState& ts, const String& str, bool line_wrap);
541 YF_API void
542 DrawText(const Graphics& g, const Rect& bounds, const String& str,
543  const Padding& m, Color, bool line_wrap, const Font& fnt = {});
555 YF_API void
556 DrawText(TextRegion& r, const Graphics& g, const Point& pt, const Size& s,
557  const String& str, bool line_wrap);
558 
559 YSL_END_NAMESPACE(Drawing)
560 
561 YSL_END
562 
563 #endif
564