YSTest: ReaderSetting.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

ReaderSetting.cpp
浏览该文件的文档.
1 /*
2 © 2012-2013, 2015-2016, 2019-2020 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 "ReaderSetting.h" // ystdex::min, to_std_string, to_string,
29 // to_pmr_string;
30
31 namespace YReader
32 {
33
34 using namespace Text;
35 using std::chrono::milliseconds;
36
38 namespace
39 {
40
42
43 template<typename _type>
44 YB_ATTR_nodiscard YB_PURE _type
45 FetchSetting(const ValueNode::Container&, const string&);
46
47 template<>
48 YB_ATTR_nodiscard YB_PURE inline string
49 FetchSetting<string>(const ValueNode::Container& con, const string& name)
50 {
51 return Access<string>(AccessNode(con, name));
52 }
53
54 template<>
55 YB_ATTR_nodiscard YB_PURE int
56 FetchSetting<int>(const ValueNode::Container& con, const string& name)
57 {
58 return std::stoi(to_std_string(FetchSetting<string>(con, name)));
59 }
60
61 template<>
62 YB_ATTR_nodiscard YB_PURE Color
63 FetchSetting<Color>(const ValueNode::Container& con, const string& name)
64 {
65 const auto s(FetchSetting<string>(con, name).c_str());
66 unsigned r, g, b;
67
68 if(std::sscanf(s, "%u%u%u", &r, &g, &b) != 3)
69 throw std::invalid_argument("Color components are not enough.");
70 #if false
71 if(r < 0x100 && g < 0x100 && b < 0x100)
72 return Color(r, g, b);
73 throw std::invalid_argument("Invalid color components found.");
74 #endif
75 return Color(ystdex::min<MonoType>(r, 0xFF), ystdex::min<MonoType>(g, 0xFF),
76 ystdex::min<MonoType>(b, 0xFF));
77 }
79
81 YB_ATTR_nodiscard YB_PURE ValueNode
82 ColorToNode(const string& name, const Color& value)
83 {
84 return YSLib::AsNode(name, to_pmr_string(to_string(value.GetR())) + ' '
85 + to_pmr_string(to_string(value.GetG())) + ' '
86 + to_pmr_string(to_string(value.GetB())));
87 }
88
90 YB_ATTR_nodiscard YB_PURE Font
91 FetchFontSetting(const ValueNode::Container& con, const string& family,
92 const string& size)
93 {
94 if(const auto p = FetchDefaultFontCache().GetFontFamilyPtr(
95 FetchSetting<string>(con, family)))
96 return Font(*p, FetchSetting<int>(con, size));
97 return {};
98 }
99
100 } // unnamed namespace;
101
102 ReaderSetting::ReaderSetting()
103 : UpColor(240, 216, 192), DownColor(192, 216, 240), FontColor(),
104 Font(FetchDefaultTypeface().GetFontFamily(), 14), SmoothScroll(true),
105 ScrollDuration(1000), SmoothScrollDuration(80)
106 {}
107 ReaderSetting::ReaderSetting(const ValueNode::Container& con)
108 : UpColor(FetchSetting<Color>(con, "UpColor")), DownColor(
109 FetchSetting<Color>(con, "DownColor")), FontColor(FetchSetting<Color>(con,
110 "FontColor")), Font(FetchFontSetting(con, "FontFamily", "FontSize")),
111 SmoothScroll(FetchSetting<int>(con, "SmoothScroll") != 0),
112 ScrollDuration(FetchSetting<int>(con, "ScrollDuration")),
113 SmoothScrollDuration(FetchSetting<int>(con, "SmoothScrollDuration"))
114 {}
115
116 ReaderSetting::operator ValueNode::Container() const
117 {
118 return ValueNode::Container{ColorToNode("UpColor", UpColor),
119 ColorToNode("DownColor", DownColor),
120 ColorToNode("FontColor", FontColor),
121 AsNode("FontFamily", Font.GetFontFamily().GetFamilyName()),
122 StringifyToNode("FontSize", Font.GetSize()),
123 StringifyToNode("SmoothScroll", int(SmoothScroll)),
124 StringifyToNode("ScrollDuration", ScrollDuration.count()),
125 StringifyToNode("SmoothScrollDuration", SmoothScrollDuration.count())};
126 }
127
128 } // namespace YReader;
129
ystdex::mapped_set< ValueNode, ystdex::less<>, MappedSetTraits, allocator_type >
YB_ATTR_nodiscard
#define YB_ATTR_nodiscard
标记返回值不被忽略的属性。
Definition: ydef.h:715
size
size_t size
Definition: memory_resource.cpp:295
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
YReader
Definition: About.h:34
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
ReaderSetting.h
阅读器设置。
YSLib::IO::int
YF_API int
Definition: File.h:51
YSLib::FetchDefaultFontCache
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
Definition: Initialization.cpp:822
platform::strings::to_std_string
_tString to_std_string(basic_string_view< _tChar > sv)
Definition: Container.h:256
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
platform::strings::to_pmr_string
_tString to_pmr_string(basic_string_view< _tChar > sv)
Definition: Container.h:226
YSLib::StringifyToNode
YB_ATTR_nodiscard YB_PURE ValueNode StringifyToNode(_tString &&str, _tParams &&... args)
取指定名称和转换为字符串的值类型节点。
Definition: ValueNode.h:1341
YReader::ReaderSetting::ReaderSetting
ReaderSetting()
Definition: ReaderSetting.cpp:102
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
YSLib::AsNode
YB_ATTR_nodiscard YB_PURE ValueNode AsNode(const ValueNode::allocator_type &a, _tString &&str, _tParams &&... args)
Definition: ValueNode.h:1304
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
Color
Pixel Color
Definition: CharRenderer.cpp:59
YSLib::ValueNode::Container
ystdex::mapped_set< ValueNode, ystdex::less<>, MappedSetTraits, allocator_type > Container
Definition: ValueNode.h:201
NPL::A1::name
const char * name
Definition: NPLA1.h:1876
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:140
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:589
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::AccessNode
YB_ATTR_nodiscard YF_API YB_PURE ValueNode & AccessNode(ValueNode::Container *, const string &)
访问节点。
Definition: ValueNode.cpp:102
YSLib::Drawing::FetchDefaultTypeface
YB_ATTR_nodiscard YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:391