Skip to content

Commit

Permalink
talipot-core/FontIcons: Store icon UTF-8 strings as static data
Browse files Browse the repository at this point in the history
Instead of computing them dynamically, prefer to do it a priori in
the Python script generating icons data.

Remove no longer needed string conversion function.
  • Loading branch information
anlambert committed Mar 18, 2024
1 parent e161049 commit 86b71a2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
4 changes: 1 addition & 3 deletions library/talipot-core/include/talipot/TlpTools.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2023 The Talipot developers
* Copyright (C) 2019-2024 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -250,8 +250,6 @@ TLP_SCOPE std::istream *getZstdInputFileStream(const std::string &filename);
TLP_SCOPE std::ostream *getZstdOutputFileStream(const std::string &filename,
int compressionLevel = 3);

TLP_SCOPE std::string utf32to8(const std::u32string &s);

TLP_SCOPE std::wstring utf8to16(const std::string &s);

TLP_SCOPE std::vector<std::string> tokenize(const std::string &str,
Expand Down
6 changes: 3 additions & 3 deletions library/talipot-core/src/FontAwesome.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2023 The Talipot developers
* Copyright (C) 2019-2024 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -50,7 +50,7 @@ const vector<string> &FontAwesome::getSupportedIcons() {

uint FontAwesome::getIconCodePoint(const string &iconName) {
if (const auto it = iconCodePoint.find(iconName.c_str()); it != iconCodePoint.end()) {
return it->second;
return it->second.first;
}
return 0;
}
Expand All @@ -65,7 +65,7 @@ string FontAwesome::getIconFamily(const string &iconName) {

string FontAwesome::getIconUtf8String(const string &iconName) {
try {
return utf32to8(u32string(1, static_cast<char32_t>(iconCodePoint.at(iconName.c_str()))));
return iconCodePoint.at(iconName.c_str()).second;
} catch (std::out_of_range &) {
tlp::warning() << iconName << " icon does not exist, falling back to "
<< FontAwesome::Solid::QuestionCircle << std::endl;
Expand Down
6 changes: 3 additions & 3 deletions library/talipot-core/src/MaterialDesignIcons.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright (C) 2019-2022 The Talipot developers
* Copyright (C) 2019-2024 The Talipot developers
*
* Talipot is a fork of Tulip, created by David Auber
* and the Tulip development Team from LaBRI, University of Bordeaux
Expand Down Expand Up @@ -49,7 +49,7 @@ const vector<string> &MaterialDesignIcons::getSupportedIcons() {

uint MaterialDesignIcons::getIconCodePoint(const string &iconName) {
if (const auto it = iconCodePoint.find(iconName.c_str()); it != iconCodePoint.end()) {
return it->second;
return it->second.first;
}
return 0;
}
Expand All @@ -60,7 +60,7 @@ string MaterialDesignIcons::getIconFamily(const string &) {

string MaterialDesignIcons::getIconUtf8String(const string &iconName) {
try {
return utf32to8(u32string(1, static_cast<char32_t>(iconCodePoint.at(iconName.c_str()))));
return iconCodePoint.at(iconName.c_str()).second;
} catch (std::out_of_range &) {
tlp::warning() << iconName << " icon does not exist, falling back to "
<< MaterialDesignIcons::HelpCircle << std::endl;
Expand Down
17 changes: 0 additions & 17 deletions library/talipot-core/src/TlpTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,28 +461,11 @@ std::ostream *tlp::getZstdOutputFileStream(const std::string &filename, int comp

//=========================================================

// silent not critical codecvt deprecation warnings
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif

string tlp::utf32to8(const u32string &s) {
wstring_convert<codecvt_utf8<char32_t>, char32_t> conv;
return conv.to_bytes(s);
}

//=========================================================

wstring tlp::utf8to16(const string &s) {
wstring_convert<codecvt_utf8_utf16<wchar_t>, wchar_t> conv;
return conv.from_bytes(s);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif

//=========================================================

vector<string> tlp::tokenize(const string &str, const string &delimiter) {
Expand Down
18 changes: 11 additions & 7 deletions utils/scripts/fonticons/generate_font_icons_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import json
import os
import sys

from collections import defaultdict

talipot_source_dir = sys.argv[1]
Expand Down Expand Up @@ -41,6 +40,10 @@ def version_tuple(v):
return tuple(map(int, (d.split('-')[0] for d in v.split("."))))


def to_c_str(codepoint):
return str(chr(int(codepoint, 16)).encode('utf-8'))[2:-1]


path = os.path.dirname(__file__)

open_kwargs = {'encoding': 'utf-8'}
Expand Down Expand Up @@ -88,7 +91,7 @@ def version_tuple(v):
fa_constants_h.write(license_header)
fa_constants = ''
init_icon_code_points = (
'static const unordered_map<const char *, uint, HashString, EqualString> iconCodePoint = {\n'
'static const unordered_map<const char *, pair<uint, const char *>, HashString, EqualString> iconCodePoint = {\n'
)
for style in sorted(fa_icons.keys()):
style_upper = style[0].upper() + style[1:]
Expand All @@ -102,8 +105,9 @@ def version_tuple(v):
fa_constants_h.write(
' static const char *%s;\n' % icon_constant_name)
init_icon_code_points += (
' {FontAwesome::%s::%s, 0x%s},\n' %
(style_upper, icon_constant_name, icon_data['unicode']))
' {FontAwesome::%s::%s, {0x%s, "%s"}},\n' %
(style_upper, icon_constant_name, icon_data['unicode'],
to_c_str(icon_data['unicode'])))
fa_constants += ('const char *FontAwesome::%s::%s = "%s";\n' %
(style_upper, icon_constant_name,
icon_name_prefix))
Expand Down Expand Up @@ -163,15 +167,15 @@ def version_tuple(v):
md_constants_h.write(license_header)
md_constants = ''
init_icon_code_points = (
'static const unordered_map<const char *, uint, HashString, EqualString> iconCodePoint = {\n'
'static const unordered_map<const char *, pair<uint, const char *>, HashString, EqualString> iconCodePoint = {\n'
)
for icon in md_data:
icon_constant_name = to_upper_style(icon['name'])
md_constants_h.write(
'static const char *%s;\n' % icon_constant_name)
init_icon_code_points += (
' {MaterialDesignIcons::%s, 0x%s},\n' %
(icon_constant_name, icon['codepoint'].lower()))
' {MaterialDesignIcons::%s, {0x%s, "%s"}},\n' %
(icon_constant_name, icon['codepoint'].lower(), to_c_str(icon['codepoint'].lower())))
md_constants += ('const char *MaterialDesignIcons::%s = "%s";\n' %
(icon_constant_name, 'md-' + icon['name']))

Expand Down

0 comments on commit 86b71a2

Please sign in to comment.