NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
text.hpp
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <unordered_map>
17
19#include "NNL/common/io.hpp"
21#include "NNL/utility/data.hpp"
22namespace nnl {
29
34namespace text {
40
49constexpr u16 kSpecialCodeMask = 0xF000;
50
60const std::unordered_map<u16, std::string_view> kSpecialCodeToString{
61 {0x1000, "{TRIANGLE}"},
62 {0x1001, "{CIRCLE}"},
63 {0x1002, "{X}"},
64 {0x1003, "{SQUARE}"},
65 {0x1004, "{UP}"}, // D-pad up
66 {0x1005, "{DOWN}"}, // D-pad down
67 {0x1006, "{LEFT}"}, // D-pad left
68 {0x1007, "{RIGHT}"}, // D-pad right
69 {0x1008, "{L}"}, // L
70 {0x1009, ""}, // second half of L
71 {0x100A, "{R}"}, // R
72 {0x100B, ""}, // second half of R
73 {0x100C, "{SELECT}"}, // select
74 {0x100D, ""}, // second half of select
75 {0x100E, "{START}"}, // start
76 {0x100F, ""}, // second half of start
77 {0x1010, "{JOYSTICK}"}, // joystick
78 {0x1011, "{UNDERSCORE}"}, // underscore
79 {0x1012, "{HEART}"}, // heart
80 {0x1013, "{NOTE}"}, // note
81 {0x1014, "{HEART1}"}, // slim heart
82 {0x1015, "{NOTE1}"}, // slim note
83 {0x1016, "{STAR}"}, // star
84
85 {0x8000, "\0"}, // string terminator
86 {0x8001, "\n"}, // new line
87 {0x8002, "{8002}"},
88 {0x8003, "{8003}"},
89 {0x8004, "{8004}"},
90 {0x8005, "{SMALL}"}, // small text size
91 {0x8006, "{NORMAL}"}, // normal text size
92 {0x8007, "{LARGE}"}, // large text size
93 {0x8008, "{WHITE}"}, // reset/white
94 {0x800A, "{BLACK}"}, // black text
95 {0x800B, "{RED}"}, // red text
96 {0x800C, "{BLUE}"}, // blue text
97 {0x800D, "{ORANGE}"}, // orange text
98 {0x800E, "{GREEN}"}, // green text
99 {0x800F, "{800F}"}, //
100 {0x8010, "{8010}"}, //
101 {0x8011, "{8011}"}, //
102 {0x8012, "{8012}"}, //
103 {0x8013, "{8013}"}, //
104 {0x8014, "{8014}"}, //
105 {0x8015, "{8015}"}, //
106 {0x8016, "{8016}"}, //
107 {0x8017, "{8017}"}, //
108 {0x8018, "{8018}"}, //
109 {0x8019, "{8019}"}, //
110 {0x801A, "{801A}"}, //
111 {0x801B, "{801B}"}, //
112 {0x801C, "{801C}"}, //
113 {0x801D, "{801D}"}, // used 1 time in Spanish (tutorial)
114 {0x801E, "{801E}"}, //
115 {0x801F, "{801F}"}, //
116 {0x8020, "{8020}"}, //
117 {0x8021, "{8021}"}, //
118 {0x8022, "{CYAN}"}, // cyan text
119 {0x8023, "{MAGENTA}"}, // magenta text
120 {0x8031, "\t"}, //
121 {0x8032, "{EWHITE}"}, // Encircles a character with a white copy of itself
122 {0x8033, "{EBLACK}"}, //
123 {0x8034, "{EBLUE}"}, //
124 {0x8035, "{EMAGENTA}"}, //
125 {0x8036, "{ERED}"}, //
126 {0x8037, " "}, // space
127 {0x8038, "{VAR0}"},
128 {0x8039, "{VAR1}"},
129
130 {0x803A, "{803A}"}, //
131 {0x803B, "{803B}"}, //
132 {0x803C, "{803C}"}, //
133 {0x803D, "{803D}"}, //
134 {0x803E, "{803E}"}, //
135 {0x803F, "{803F}"}, //
136
137 {0x8040, "{VAR2}"}, // variable
138 {0x8041, "{VAR3}"}, // variable
139 {0x8042, "{VAR4}"}, // variable
140 {0x8043, "{VAR5}"}, // small text size //variable ???
141 {0x8044, "{VAR6}"}, // variable ???
142 {0x8045, "{VAR7}"}, // variable
143 {0x8046, "{VAR8}"}, // variable
144 {0x8047, "{VAR9}"}, // variable
145 {0x8048, "{VARA}"}, // small text size //variable ???
146 {0x8049, "{VARB}"}, // variable ???
147 {0x804A, "{804A}"}, // variable ???
148 {0x804B, "{804B}"}, // variable ???
149 {0x804C, "{804C}"}, // variable ???
150 {0x804D, "{804D}"}, // variable ???
151 {0x804E, "{804E}"}, // variable ???
152 {0x804F, "{804F}"}, // variable ???
153 {0x8050, "{VARC}"}, // variable ???
154 {0x8051, "{VARD}"},
155 {0x8052, "{VARE}"},
156 {0x8053, "{VARF}"},
157 // 1F4BD
158};
159
168using IndexedString = std::vector<u16>;
184struct Text {
185 std::vector<IndexedString>
187 std::vector<u16> characters;
188};
189
204std::vector<std::string> Convert(const Text& text,
205 const std::unordered_map<u16, std::string_view>& replacements = kSpecialCodeToString);
220Text Convert(const std::vector<std::string>& stext,
221 const std::unordered_map<u16, std::string_view>& replacements = kSpecialCodeToString,
222 const std::vector<u16>& characters = {});
223
235bool IsOfType(BufferView buffer);
236
237bool IsOfType(const std::filesystem::path& path);
238
239bool IsOfType(Reader& f);
240
255
256Text Import(const std::filesystem::path& path);
257
258Text Import(Reader& f);
268[[nodiscard]] Buffer Export(const Text& text);
269
270void Export(const Text& text, const std::filesystem::path& path);
271
272void Export(const Text& text, Writer& f);
274
291 std::vector<STexture> bitmaps;
292 std::vector<u8> advance_width;
294};
295
301 unsigned int size = 128;
303 unsigned int columns = 8;
304 float opacity_factor = 1.5f;
305 float scale_factor = 1.0f;
307 bool simulate_kerning = false;
309 bool filter_nearest = false;
310 unsigned int alpha_levels = 256;
311};
312
326BitmapFont GenerateBitmapFont(Text& text, const std::filesystem::path& ttf_font_path,
327 const BitmapFontParams& params = {});
328
329BitmapFont GenerateBitmapFont(Text& text, BufferView ttf_font_file, const BitmapFontParams& params = {});
330
348std::string GenerateFNT(const Text& text, const std::vector<u8>& advance_width, const std::vector<STexture>& bitmaps,
349 int columns = -1);
351
352namespace raw {
358constexpr u32 kMagicBytes = utl::data::FourCC("CDL!");
359
360struct RHeader {
361 u32 num_strings = 0;
362 std::vector<u32> offset_strings;
363 u32 offset_cdl = 0;
364 std::map<u32, std::vector<u16>> strings;
365};
366
367struct RCDL {
368 u32 magic_bytes = kMagicBytes;
369 u32 num_characters = 0;
370 u32 num_0 = 0; // reserved(?)
371 u32 num_1 = 0; // reserved(?)
372 std::vector<u16> characters;
373};
374
375struct RText {
376 RHeader header;
377 RCDL cdl;
378};
379
380Text Convert(RText&& rtext);
381
382RText Parse(Reader& f);
384} // namespace raw
385
386} // namespace text
387
388} // namespace nnl
Provides functions and classes for handling binary data.
Contains macros and definitions for fixed-width types.
constexpr u32 FourCC(const char(&str)[5]) noexcept
Generates a numeric four character code from a given string.
Definition data.hpp:36
std::uint16_t u16
16-bit unsigned integer
Definition fixed_type.hpp:62
std::uint32_t u32
32-bit unsigned integer
Definition fixed_type.hpp:60
Reader implementation for read-only memory buffers.
Definition io.hpp:598
Abstract class for writing data.
Definition io.hpp:136
Abstract class for reading data.
Definition io.hpp:78
std::vector< u8 > Buffer
A type alias for std::vector<u8> that denotes a raw, contiguous memory region that may be interpreted...
Definition io.hpp:40
std::vector< STexture > bitmaps
Glyph atlases.
Definition text.hpp:291
float opacity_factor
A multiplier that makes glyphs brighter or dimmer.
Definition text.hpp:304
unsigned int columns
Number of glyphs in a row of the bitmap.
Definition text.hpp:303
bool simulate_kerning
Definition text.hpp:307
int spacing_offset
An additional offset for character spacing.
Definition text.hpp:306
unsigned int alpha_levels
Number of shades to use.
Definition text.hpp:310
float scale_factor
A multiplier that forcibly scales glyphs.
Definition text.hpp:305
bool filter_nearest
Flag to use the nearest/linear neighbor filtering.
Definition text.hpp:309
std::vector< u8 > advance_width
Definition text.hpp:292
unsigned int size
Definition text.hpp:301
Parameters for generating a bitmap font.
Definition text.hpp:300
Represents a bitmap font consisting of glyph atlases and advance widths.
Definition text.hpp:290
std::string GenerateFNT(const Text &text, const std::vector< u8 > &advance_width, const std::vector< STexture > &bitmaps, int columns=-1)
Generates a BMFont .fnt file.
BitmapFont GenerateBitmapFont(Text &text, const std::filesystem::path &ttf_font_path, const BitmapFontParams &params={})
Generates a bitmap font from a TrueType font file.
std::vector< u16 > characters
A palette of unique UCS-2 encoded characters used by the strings.
Definition text.hpp:187
std::vector< IndexedString > strings
Strings consisting of special codes and indices that reference the characters array.
Definition text.hpp:186
Represents an in-game text archive.
Definition text.hpp:184
std::vector< std::string > Convert(const Text &text, const std::unordered_map< u16, std::string_view > &replacements=kSpecialCodeToString)
Converts an in-game text archive to a vector of UTF-8 encoded strings.
const std::unordered_map< u16, std::string_view > kSpecialCodeToString
A default mapping of special codes to string representations.
Definition text.hpp:60
Text Import(BufferView buffer)
Parses a binary file and converts it to a Text object.
std::vector< u16 > IndexedString
A string consisting of special codes and indices into the character lookup table.
Definition text.hpp:168
constexpr u16 kSpecialCodeMask
A bitmask for identifying special codes within an IndexedString.
Definition text.hpp:49
bool IsOfType(BufferView buffer)
Tests if the provided file is a text archive.
Buffer Export(const Text &text)
Converts a text archive to a binary file representation.
constexpr u32 kMagicBytes
Magic Bytes.
Definition text.hpp:358
Provides classes for reading and writing binary data to and from various sources.
Contains structures and functions for working with in-game text archives.
Definition text.hpp:34
Definition exception.hpp:56
Provides data structures for representing essential components of a texture.