YSTest: CharRenderer.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

CharRenderer.cpp
浏览该文件的文档.
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 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_CharRenderer
30 #include YFM_YSLib_Service_YBlend // for Drawing::Shaders::BlitAlphaPoint;
31 #include <ystdex/bitseg.hpp> // for ystdex::bitseg_iterator;
32
33 using namespace ystdex;
34
35 namespace YSLib
36 {
37
38 using namespace Drawing;
39 using namespace Text;
40
41 namespace Drawing
42 {
43
44 namespace
45 {
46
48 const AlphaType BLT_TEXT_ALPHA_THRESHOLD(16);
49
55 struct BlitTextPoint
56 {
58 Pixel Color;
59
61 template<typename _tOut, typename _tIn>
62 void
63 operator()(_tOut dst_iter, _tIn src_iter) const
64 {
65 if(*src_iter >= BLT_TEXT_ALPHA_THRESHOLD)
66 yunseq(*get<1>(dst_iter.base()) = *src_iter,
67 *dst_iter = Color);
68 }
69 };
70
72 template<size_t _vN>
73 struct tr_seg
74 {
75 static_assert(_vN < CHAR_BIT, "Specified bits should be within a byte.");
76
77 byte v;
78
79 const byte&
80 operator()(const bitseg_iterator<_vN, true>& i) ynothrow
81 {
82 return v = byte(unsigned(*i << (size_t(CHAR_BIT) - _vN))
83 | ((1U << _vN) - 1));
84 }
85 };
86
87
89
90 using PixelIt = pseudo_iterator<const Pixel>;
91
93 template<size_t _vBit>
94 using MonoItPairN = pair_iterator<PixelIt,
95 transformed_iterator<bitseg_iterator<_vBit, true>, tr_seg<_vBit>>>;
96
97 template<unsigned char _vN>
98 auto
99 tr_buf(byte* p)
100 -> decltype(make_transform(bitseg_iterator<_vN, true>(p), tr_seg<_vN>()))
101 {
102 return make_transform(bitseg_iterator<_vN, true>(p), tr_seg<_vN>());
103 }
105
106 } // unnamed namespace;
107
108 void
109 RenderChar(PaintContext&& pc, Color c, bool neg_pitch,
110 CharBitmap::BufferType cbuf, CharBitmap::FormatType fmt, const Size& ss)
111 {
112 YAssert(cbuf, "Invalid buffer found.");
113
114 const Shaders::BlitAlphaPoint bp{};
115 const auto dst(pc.Target.GetBufferPtr());
116
117 switch(fmt)
118 {
119 case CharBitmap::Mono:
120 BlitGlyphPixels(bp, dst, MonoItPairN<1>(PixelIt(c), tr_buf<1>(cbuf)),
121 ss, pc, neg_pitch);
122 break;
123 case CharBitmap::Gray2:
124 BlitGlyphPixels(bp, dst, MonoItPairN<2>(PixelIt(c), tr_buf<2>(cbuf)),
125 ss, pc, neg_pitch);
126 break;
127 case CharBitmap::Gray4:
128 BlitGlyphPixels(bp, dst, MonoItPairN<4>(PixelIt(c), tr_buf<4>(cbuf)),
129 ss, pc, neg_pitch);
130 break;
131 case CharBitmap::Gray:
132 BlitGlyphPixels(bp, dst, pair_iterator<PixelIt, const AlphaType*>(
133 PixelIt(c), cbuf), ss, pc, neg_pitch);
134 default:
135 break;
136 }
137 }
138
139 void
140 RenderCharAlpha(PaintContext&& pc, Color c, bool neg_pitch,
141 CharBitmap::BufferType cbuf, CharBitmap::FormatType fmt, const Size& ss,
142 AlphaType* alpha)
143 {
144 YAssert(cbuf, "Invalid buffer found.");
145
146 const BlitTextPoint bp{c};
147 const pair_iterator<BitmapPtr, AlphaType*>
148 dst(pc.Target.GetBufferPtr(), alpha);
149
150 switch(fmt)
151 {
152 case CharBitmap::Mono:
153 BlitGlyphPixels(bp, dst, tr_buf<1>(cbuf), ss, pc, neg_pitch);
154 break;
155 case CharBitmap::Gray2:
156 BlitGlyphPixels(bp, dst, tr_buf<2>(cbuf), ss, pc, neg_pitch);
157 break;
158 case CharBitmap::Gray4:
159 BlitGlyphPixels(bp, dst, tr_buf<4>(cbuf), ss, pc, neg_pitch);
160 break;
161 case CharBitmap::Gray:
162 BlitGlyphPixels(bp, dst, cbuf, ss, pc, neg_pitch);
163 default:
164 break;
165 }
166 }
167
168
169 PutCharResult
170 PutCharBase(TextState& ts, SDst eol, char32_t c)
171 {
172 if(c == '\n')
173 {
174 ts.PutNewline();
175 return PutCharResult::PutNewline;
176 }
177 if(YB_UNLIKELY(!IsPrint(c)))
178 return PutCharResult::NotPrintable;
179
180 const SPos w_adv(ts.Pen.X + ts.Font.GetAdvance(c));
181
182 if(YB_UNLIKELY(w_adv > 0 && SDst(w_adv) > eol))
183 {
184 ts.PutNewline();
185 return PutCharResult::NeedNewline;
186 }
187 return PutCharResult::Normal;
188 }
189
190 } // namespace Drawing;
191
192 } // namespace YSLib;
193
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:95
YSLib::Drawing::BlitGlyphPixels
void BlitGlyphPixels(_fPixelShader blit, _tOut dst, _tIn src, const Size &ss, const PaintContext &pc, bool neg_pitch)
字符按像素块传输。
Definition: CharRenderer.h:91
YModules.h
v
byte v
Definition: CharRenderer.cpp:75
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::Drawing::Shaders::BlitAlphaPoint
像素计算：Alpha 混合。
Definition: YBlend.h:80
bitseg.hpp
位段数据结构和访问。
YSLib::Drawing::PutCharResult
PutCharResult
输出字符结果。
Definition: CharRenderer.h:169
YSLib::Drawing::PenStyle::Font
Drawing::Font Font
字体。
Definition: TextBase.h:57
ystdex::bitseg_iterator
位段迭代器。
Definition: bitseg.hpp:206
Text
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:464
Color
Pixel Color
Definition: CharRenderer.cpp:58
YSLib::Drawing::RenderCharAlpha
YF_API void RenderCharAlpha(PaintContext &&pc, Color, bool, CharBitmap::BufferType, CharBitmap::FormatType, const Size &, AlphaType *)
渲染带 Alpha 缓冲的单个字符。
Definition: CharRenderer.cpp:140
ystdex::pseudo_iterator
伪迭代器。
Definition: iterator.hpp:62
YSLib::Drawing::CharBitmap::FormatType
FormatType
Definition: Font.h:392
ystdex::tuple_iterator
元组迭代器。
Definition: iterator.hpp:651
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1022
platform::AlphaType
octet AlphaType
Definition: Video.h:253
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:111
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::Drawing::TextState
文本状态。
Definition: TextBase.h:86
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::transformed_iterator
变换迭代器。
Definition: iterator.hpp:216
ystdex
特性测试宏。
Definition: ydef.h:822
YSLib::i
return i
Definition: YCoreUtilities.h:176
Drawing
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
platform::Color
颜色。
Definition: Video.h:416
YSLib::Drawing::CharBitmap::BufferType
byte * BufferType
Definition: Font.h:387
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:293
YSLib::Drawing::GBinaryGroup::X
_type X
Definition: YGDIBase.h:63
YSLib::Drawing::PutCharBase
YF_API PutCharResult PutCharBase(TextState &, SDst, char32_t)
使用指定的文本状态和行末位置按需打印换行并判断是否需要渲染单个字符。
Definition: CharRenderer.cpp:170
CHRLib::c
c
Definition: CharacterProcessing.h:81
YSLib::Drawing::char32_t
tr TextRegion &tr char32_t
Definition: TextLayout.h:92
YSLib::Consoles::Gray
Definition: YConsole.h:60
platform_ex::Windows::Normal
Definition: MinGW32.h:455
YSLib::Drawing::TextState::Pen
Point Pen
笔坐标。
Definition: TextBase.h:98
CHRLib::p
const auto p(d)
YSLib::Drawing::RenderChar
YF_API void RenderChar(PaintContext &&pc, Color, bool, CharBitmap::BufferType, CharBitmap::FormatType, const Size &)
渲染单个字符。
Definition: CharRenderer.cpp:109