Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
symbols.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
32
33#pragma once
34
35#include "colors.hpp"
36
37#include <array>
38#include <cstddef>
39#include <cstdint>
40#include <string>
41#include <string_view>
42#include <utility>
43#include <vector>
44
50{
51 using namespace __internals::utils;
52
56 struct Position
57 {
58 long long x = 0;
59 long long y = 0;
60 };
61
62 // // Not currently in use.
63 // enum PrintingAlignment
64 // {
65 // BASELINE = 0,
66 // MIDDLE = 1
67 // };
68
69 // struct PrintingObj
70 // {
71 // std::string str;
72 // PrintingAlignment alignment;
73 // };
74
75 enum class HorizontalAlignment : std::uint8_t
76 {
77 LEFT = 0,
78 CENTER = 1,
80 };
81
86 {
87 private:
90
92 std::vector<std::vector<std::string>> buffer;
93
95 size_t height = 10;
96
98 size_t width = 10;
99
108 void _write(const std::string& s,
109 const Position& pos,
110 bool updatePos = false,
111 const ColorFunc& color = colors::keepOriginal,
113
114 public:
121 ConsoleOutput(size_t height, size_t width);
122
132 void write(char c,
133 long long dLine,
134 long long dCol,
135 bool updatePos = false,
136 const ColorFunc& color = colors::keepOriginal,
138
147 void write(char c,
148 const Position& pos,
149 bool updatePos = false,
150 const ColorFunc& color = colors::keepOriginal,
152
161 void write(const std::string_view& s,
162 const Position& pos,
163 bool updatePos = false,
164 const ColorFunc& color = colors::keepOriginal,
166 {
167 _write(static_cast<std::string>(s), pos, updatePos, color, alignment);
168 }
169
174 [[nodiscard]] std::string asString() const;
175 };
176
183 size_t getStringWidth(const std::string& s);
184
191 size_t getStringHeight(const std::string& s);
192} // namespace steppable::prettyPrint
193
195{
196
205 bool isZeroWidthCharacter(uint32_t codepoint);
206
217 uint32_t getCodepoint(const std::string& str, size_t& i);
218
228 size_t getUnicodeDisplayWidth(const std::string& utf8Str);
229
230 bool utf8Decode(const std::string& s, size_t& i, uint32_t& cp);
231
232 bool isEmojiBase(uint32_t cp);
233
234 bool isClusterExtender(uint32_t cp);
235
237 {
238 public:
239 GraphemeIterator(std::string s) : str(std::move(s)) {}
240
241 // Returns false when there are no more clusters
242 bool next(std::string& cluster)
243 {
244 if (pos >= str.size())
245 return false;
246 size_t start = pos;
247 uint32_t cp = 0;
248 size_t temp = pos;
249 if (!utf8Decode(str, temp, cp))
250 return false;
251
252 // For emoji: group sequences with ZWJ or skin tone
253 if (isEmojiBase(cp) || (cp >= 0x1F1E6 && cp <= 0x1F1FF))
254 {
255 pos = temp;
256 while (true)
257 {
258 size_t save = pos;
259 uint32_t nextcp = 0;
260 if (!utf8Decode(str, save, nextcp))
261 break;
262 if (isClusterExtender(nextcp) ||
263 // For flags: two regional indicators
264 ((cp >= 0x1F1E6 && cp <= 0x1F1FF) && (nextcp >= 0x1F1E6 && nextcp <= 0x1F1FF)))
265 {
266 pos = save;
267 }
268 else
269 {
270 break;
271 }
272 }
273 cluster = str.substr(start, pos - start);
274 return true;
275 }
276
277 // For combining marks: group base + following combining
278 pos = temp;
279 while (true)
280 {
281 size_t save = pos;
282 uint32_t nextcp = 0;
283 if (!utf8Decode(str, save, nextcp))
284 break;
285 if (isZeroWidthCharacter(nextcp))
286 pos = save;
287 else
288 break;
289 }
290 cluster = str.substr(start, pos - start);
291 return true;
292 }
293
294 private:
295 std::string str;
296 size_t pos{};
297 };
298} // namespace steppable::__internals::stringUtils
299
310{
312 constexpr std::string_view BECAUSE = "\u2235";
314 constexpr std::string_view THEREFORE = "\u2234";
315
317 constexpr std::string_view MULTIPLY = "\u00D7";
319 constexpr std::string_view DIVIDED_BY = "\u00F7";
320
321 constexpr std::string_view SURD = "\u221A";
322 constexpr std::string_view COMBINE_MACRON = "\u0305";
323
325 constexpr std::string_view LARGE_DOT = "\u25C9";
326 constexpr std::string_view ABOVE_DOT = "\u02D9";
327
328 // Subscripts
333 constexpr std::string_view SUB_0 = "\u2080";
338 constexpr std::string_view SUB_Z = "\u1D69";
343 constexpr int SUB_MAGIC_NUMBER = 8272;
344
346 extern const std::array<std::string, 10>& SUPERSCRIPTS;
347
354 std::string makeSubscript(const std::string& normal);
355
362 std::string makeSubscript(int normal);
363
364 // Superscripts
369 constexpr std::string_view SUP_0 = "\u2070";
374 constexpr std::string_view SUP_Z = "\u1DBB";
379 constexpr int SUP_MAGIC_NUMBER = 8304;
380
387 std::string makeSuperscript(const std::string& normal);
388
395 std::string makeSuperscript(char normal);
396
403 std::string makeSurd(const std::string& radicand);
404
409 namespace BoxDrawing
410 {
411 constexpr std::string_view DOTTED_VERTICAL = "\u2575";
412 constexpr std::string_view DOTTED_HORIZONTAL = "\u2574";
413
414 constexpr std::string_view HORIZONTAL = "\u2500";
415 constexpr std::string_view VERTICAL = "\u2502";
416
417 // region CONNECTORS
419 constexpr std::string_view HORIZONTAL_UP = "\u2534";
420
422 constexpr std::string_view VERTICAL_LEFT = "\u2524";
423 // endregion
424
425 // region CORNERS
426 constexpr std::string_view BOTTOM_RIGHT_CORNER = "\u2518";
427 // endregion
428
429 constexpr std::string_view CROSS = "\u253C";
430 } // namespace BoxDrawing
431} // namespace steppable::__internals::symbols
432
438{
446 std::string ppRoot(const std::string& radicand, const std::string& index = "2");
447
456 std::string ppFraction(const std::string& top, const std::string& bottom, bool inLine = false);
457
465 std::string ppSubscript(const std::string& base, const std::string& subscript);
466
474 std::string ppSuperscript(const std::string& base, const std::string& superscript);
475} // namespace steppable::prettyPrint::printers
GraphemeIterator(std::string s)
Definition symbols.hpp:239
std::string str
Definition symbols.hpp:295
bool next(std::string &cluster)
Definition symbols.hpp:242
Position curPos
The current position.
Definition symbols.hpp:89
size_t height
The height of the buffer.
Definition symbols.hpp:95
void _write(const std::string &s, const Position &pos, bool updatePos=false, const ColorFunc &color=colors::keepOriginal, const HorizontalAlignment &alignment=HorizontalAlignment::LEFT)
Writes a string to the buffer.
Definition symbols.cpp:47
std::vector< std::vector< std::string > > buffer
The buffer object.
Definition symbols.hpp:92
void write(char c, long long dLine, long long dCol, bool updatePos=false, const ColorFunc &color=colors::keepOriginal, const HorizontalAlignment &alignment=HorizontalAlignment::LEFT)
Writes a character to the buffer.
Definition symbols.cpp:107
std::string asString() const
Gets the buffer as a string.
Definition symbols.cpp:147
size_t width
The width of the buffer.
Definition symbols.hpp:98
ConsoleOutput(size_t height, size_t width)
Creates a new console output buffer.
Definition symbols.cpp:42
void write(const std::string_view &s, const Position &pos, bool updatePos=false, const ColorFunc &color=colors::keepOriginal, const HorizontalAlignment &alignment=HorizontalAlignment::LEFT)
Writes a string_view object to the buffer.
Definition symbols.hpp:161
Utilities to operate strings.
Definition symbols.cpp:172
bool isZeroWidthCharacter(uint32_t codepoint)
Checks if a Unicode code point represents a zero-width character.
Definition symbols.cpp:173
size_t getUnicodeDisplayWidth(const std::string &utf8Str)
Calculates the display width of a UTF-8 encoded string.
Definition symbols.cpp:263
bool isEmojiBase(uint32_t cp)
Definition symbols.cpp:183
bool isClusterExtender(uint32_t cp)
Definition symbols.cpp:228
bool utf8Decode(const std::string &s, size_t &i, uint32_t &cp)
Definition symbols.cpp:195
uint32_t getCodepoint(const std::string &str, size_t &i)
Decodes a UTF-8 encoded string into a Unicode code point.
Definition symbols.cpp:236
Defines easy ascess to Unicode box-drawing characters.
constexpr std::string_view DOTTED_VERTICAL
Dashed vertical line.
Definition symbols.hpp:411
constexpr std::string_view BOTTOM_RIGHT_CORNER
The bottom right corner.
Definition symbols.hpp:426
constexpr std::string_view VERTICAL
Solid vertical line.
Definition symbols.hpp:415
constexpr std::string_view HORIZONTAL_UP
Horizontal line that connects to a vertical one, i.e., in _|_ shape.
Definition symbols.hpp:419
constexpr std::string_view CROSS
A combining cross between a horizontal and vertical line.
Definition symbols.hpp:429
constexpr std::string_view DOTTED_HORIZONTAL
Dashed horizontal line.
Definition symbols.hpp:412
constexpr std::string_view VERTICAL_LEFT
Vertical line that connects to a left one, i.e., in -| shape.
Definition symbols.hpp:422
constexpr std::string_view HORIZONTAL
Solid horizontal line.
Definition symbols.hpp:414
The namespace containing various unicode symbols.
Definition symbols.cpp:282
constexpr std::string_view SUB_Z
The subscript z (Unicode U+2098)
Definition symbols.hpp:338
constexpr std::string_view LARGE_DOT
The large dot symbol (Unicode U+25C9)
Definition symbols.hpp:325
constexpr std::string_view SUP_Z
The superscript z (Unicode U+1DBB)
Definition symbols.hpp:374
constexpr std::string_view THEREFORE
The therefore symbol (3 dots in a triangle, Unicode U+2234)
Definition symbols.hpp:314
constexpr std::string_view BECAUSE
The because symbol (3 dots in a triangle, Unicode U+2235)
Definition symbols.hpp:312
const std::array< std::string, 10 > & SUPERSCRIPTS
A list of subscript characters.
Definition symbols.cpp:286
constexpr int SUB_MAGIC_NUMBER
The subscript magic number (8272)
Definition symbols.hpp:343
constexpr std::string_view SUB_0
The subscript 0 (Unicode U+2080)
Definition symbols.hpp:333
constexpr std::string_view MULTIPLY
The multiply symbol (Unicode U+00D7)
Definition symbols.hpp:317
std::string makeSubscript(const std::string &normal)
Create a subscript string from a normal string.
Definition symbols.cpp:289
std::string makeSurd(const std::string &radicand)
Makes a surd expression from a radicand.
Definition symbols.cpp:322
std::string makeSuperscript(const std::string &normal)
Create a superscript string from a normal string.
Definition symbols.cpp:302
constexpr std::string_view SURD
Definition symbols.hpp:321
constexpr std::string_view ABOVE_DOT
Definition symbols.hpp:326
constexpr std::string_view COMBINE_MACRON
Definition symbols.hpp:322
constexpr std::string_view SUP_0
The superscript 0 (Unicode U+2070)
Definition symbols.hpp:369
constexpr int SUP_MAGIC_NUMBER
The superscript magic number (8304)
Definition symbols.hpp:379
constexpr std::string_view DIVIDED_BY
The divide symbol (Unicode U+00F7)
Definition symbols.hpp:319
std::ostream & keepOriginal(std::ostream &stream)
Does nothing.
Definition colors.cpp:94
std::function< std::ostream &(std::ostream &)> ColorFunc
Definition colors.hpp:65
The custom-implemented printer engines for outputting expressions.
Definition baseConvert.cpp:50
std::string ppFraction(const std::string &top, const std::string &bottom, const bool inLine)
Pretty print a fraction.
Definition fraction.cpp:52
std::string ppSubscript(const std::string &base, const std::string &subscript)
Pretty print a base expression, (aka, subscript).
Definition baseConvert.cpp:51
std::string ppRoot(const std::string &radicand, const std::string &index)
Pretty print a root expression.
Definition root.cpp:59
std::string ppSuperscript(const std::string &base, const std::string &superscript)
Pretty print a power expression, (aka, superscript).
Definition power.cpp:47
The namespace containing utilities for pretty printing.
Definition baseConvert.cpp:50
HorizontalAlignment
Definition symbols.hpp:76
size_t getStringWidth(const std::string &s)
Gets the minimal width needed to print a string.
Definition symbols.cpp:159
size_t getStringHeight(const std::string &s)
Gets the minimal height needed to print a string.
Definition symbols.cpp:168
Represents a position in the console.
Definition symbols.hpp:57
long long x
Definition symbols.hpp:58
long long y
Definition symbols.hpp:59
Untitled