YSTest: CharRenderer.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

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