utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
code_points.cpp
1/*
2** ETIB PROJECT, 2026
3** utility
4** File description:
5** code_points
6*/
7
8#include <sstream>
9#include <utility/graphic/text/code_points.hpp>
10
11namespace utility::graphic
12{
13 CodePoints::CodePoints(const std::string &content)
14 {
15 parse(content);
16 }
17
18 void CodePoints::parse(const std::string &content)
19 {
20 std::istringstream stream(content);
21 std::string line;
22 while (std::getline(stream, line)) {
23 size_t spacePos = line.find(' ');
24 if (spacePos != std::string::npos) {
25 std::string name = line.substr(0, spacePos);
26 uint32_t code =
27 std::stoul(line.substr(spacePos + 1), &spacePos, 16);
28 _codes[name] = code;
29 }
30 }
31 }
32
33 uint32_t CodePoints::getCode(const std::string &name) const
34 {
35 auto it = _codes.find(name);
36 if (it != _codes.end()) {
37 return it->second;
38 }
39 return 0;
40 }
41
42 std::string CodePoints::toUtf8(uint32_t codePoint)
43 {
44 std::string result;
45 if (codePoint <= 0x7F) {
46 result.push_back(static_cast<char>(codePoint));
47 } else if (codePoint <= 0x7FF) {
48 result.push_back(
49 static_cast<char>(0xC0 | ((codePoint >> 6) & 0x1F)));
50 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
51 } else if (codePoint <= 0xFFFF) {
52 result.push_back(
53 static_cast<char>(0xE0 | ((codePoint >> 12) & 0x0F)));
54 result.push_back(
55 static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)));
56 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
57 } else {
58 result.push_back(
59 static_cast<char>(0xF0 | ((codePoint >> 18) & 0x07)));
60 result.push_back(
61 static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F)));
62 result.push_back(
63 static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F)));
64 result.push_back(static_cast<char>(0x80 | (codePoint & 0x3F)));
65 }
66 return result;
67 }
68} // namespace utility::graphic
void parse(const std::string &content)
Populates the mapping from a .codepoints formatted string.
std::map< std::string, uint32_t > _codes
Internal name-to-code mapping.
uint32_t getCode(const std::string &name) const
Retrieves the Unicode code point for a given glyph name.
static std::string toUtf8(uint32_t codePoint)
Converts a single Unicode code point to a UTF-8 encoded string.
CodePoints()=default
Constructs an empty CodePoints object.