Formatting

This commit is contained in:
patrick96 2023-05-10 12:30:32 +02:00 committed by Patrick Ziegler
parent d74a4fab77
commit 6e716296ff
5 changed files with 840 additions and 835 deletions

View File

@ -15,15 +15,15 @@
POLYBAR_NS
namespace cairo {
/**
/**
* @brief Global pointer to the Freetype library handler
*/
static FT_Library g_ftlib;
static FT_Library g_ftlib;
/**
/**
* @brief Abstract font face
*/
class font {
class font {
public:
explicit font(cairo_t* cairo, double offset) : m_cairo(cairo), m_offset(offset) {}
virtual ~font(){};
@ -49,12 +49,12 @@ namespace cairo {
cairo_font_face_t* m_font_face{nullptr};
cairo_font_extents_t m_extents{};
double m_offset{0.0};
};
};
/**
/**
* @brief Font based on fontconfig/freetype
*/
class font_fc : public font {
class font_fc : public font {
public:
explicit font_fc(cairo_t* cairo, FcPattern* pattern, double offset, double dpi_x, double dpi_y)
: font(cairo, offset), m_pattern(pattern) {
@ -212,14 +212,19 @@ namespace cairo {
cairo_glyph_t* glyphs{nullptr};
cairo_text_cluster_t* clusters{nullptr};
cairo_text_cluster_flags_t cf{};
int nglyphs = 0, nclusters = 0;
int nglyphs = 0;
int nclusters = 0;
string utf8 = string(text);
auto status = cairo_scaled_font_text_to_glyphs(
m_scaled, x, y, utf8.c_str(), utf8.size(), &glyphs, &nglyphs, &clusters, &nclusters, &cf);
if (status != CAIRO_STATUS_SUCCESS) {
throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs()" << cairo_status_to_string(status));
logger::make().notice("ERROR %d", status);
for (char& c : utf8) {
logger::make().notice("0x%02x", c);
}
throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs() " << cairo_status_to_string(status));
}
size_t bytes = 0;
@ -240,7 +245,7 @@ namespace cairo {
m_scaled, x, y, utf8.c_str(), utf8.size(), &glyphs, &nglyphs, &clusters, &nclusters, &cf);
if (status != CAIRO_STATUS_SUCCESS) {
throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs()" << cairo_status_to_string(status));
throw application_error(sstream() << "cairo_scaled_font_text_to_glyphs() " << cairo_status_to_string(status));
}
}
@ -294,12 +299,12 @@ namespace cairo {
private:
cairo_scaled_font_t* m_scaled{nullptr};
FcPattern* m_pattern{nullptr};
};
};
/**
/**
* Match and create font from given fontconfig pattern
*/
inline decltype(auto) make_font(cairo_t* cairo, string&& fontname, double offset, double dpi_x, double dpi_y) {
inline decltype(auto) make_font(cairo_t* cairo, string&& fontname, double offset, double dpi_x, double dpi_y) {
static bool fc_init{false};
if (!fc_init && !(fc_init = FcInit())) {
throw application_error("Could not load fontconfig");
@ -335,7 +340,7 @@ namespace cairo {
#endif
return make_shared<font_fc>(cairo, match, offset, dpi_x, dpi_y);
}
}
} // namespace cairo
POLYBAR_NS_END

View File

@ -1,6 +1,7 @@
#pragma once
#include <cairo/cairo-ft.h>
#include <list>
#include "common.hpp"
@ -8,7 +9,7 @@
POLYBAR_NS
namespace cairo {
namespace utils {
namespace utils {
/**
* @brief RAII wrapper used acquire cairo_device_t
*/
@ -64,7 +65,7 @@ namespace cairo {
* @brief Convert a UCS-4 codepoint to a utf-8 encoded string
*/
size_t ucs4_to_utf8(char* utf8, unsigned int ucs);
}
}
} // namespace utils
} // namespace cairo
POLYBAR_NS_END

View File

@ -34,53 +34,53 @@ class sstream {
};
namespace string_util {
/**
/**
* Hash type
*/
using hash_type = unsigned long;
using hash_type = unsigned long;
bool contains(const string& haystack, const string& needle);
bool contains_ignore_case(const string& haystack, const string& needle);
bool ends_with(const string& haystack, const string& suffix);
string upper(const string& s);
string lower(const string& s);
bool compare(const string& s1, const string& s2);
bool contains(const string& haystack, const string& needle);
bool contains_ignore_case(const string& haystack, const string& needle);
bool ends_with(const string& haystack, const string& suffix);
string upper(const string& s);
string lower(const string& s);
bool compare(const string& s1, const string& s2);
string replace(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
string replace(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
size_t end = string::npos);
string replace_all(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
string replace_all(const string& haystack, const string& needle, const string& replacement, size_t start = 0,
size_t end = string::npos);
string squeeze(const string& haystack, char needle);
string squeeze(const string& haystack, char needle);
string strip(const string& haystack, char needle);
string strip_trailing_newline(const string& haystack);
string strip(const string& haystack, char needle);
string strip_trailing_newline(const string& haystack);
string ltrim(string value, function<bool(char)> pred);
string rtrim(string value, function<bool(char)> pred);
string trim(string value, function<bool(char)> pred);
string ltrim(string value, function<bool(char)> pred);
string rtrim(string value, function<bool(char)> pred);
string trim(string value, function<bool(char)> pred);
string ltrim(string&& value, const char& needle = ' ');
string rtrim(string&& value, const char& needle = ' ');
string trim(string&& value, const char& needle = ' ');
string ltrim(string&& value, const char& needle = ' ');
string rtrim(string&& value, const char& needle = ' ');
string trim(string&& value, const char& needle = ' ');
size_t char_len(const string& value);
string utf8_truncate(string&& value, size_t len);
size_t char_len(const string& value);
string utf8_truncate(string&& value, size_t len);
string join(const vector<string>& strs, const string& delim);
vector<string> split(const string& s, char delim);
std::vector<std::string> tokenize(const string& str, char delimiters);
string join(const vector<string>& strs, const string& delim);
vector<string> split(const string& s, char delim);
std::vector<std::string> tokenize(const string& str, char delimiters);
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth);
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth);
string floating_point(double value, size_t precision, bool fixed = false, const string& locale = "");
string filesize_mib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib_mib(
string floating_point(double value, size_t precision, bool fixed = false, const string& locale = "");
string filesize_mib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib(unsigned long long kibibytes, size_t precision = 0, const string& locale = "");
string filesize_gib_mib(
unsigned long long kibibytes, size_t precision_mib = 0, size_t precision_gib = 0, const string& locale = "");
string filesize(unsigned long long kbytes, size_t precision = 0, bool fixed = false, const string& locale = "");
string filesize(unsigned long long kbytes, size_t precision = 0, bool fixed = false, const string& locale = "");
hash_type hash(const string& src);
hash_type hash(const string& src);
} // namespace string_util
POLYBAR_NS_END

View File

@ -1,11 +1,11 @@
#include <map>
#include "cairo/utils.hpp"
#include <map>
POLYBAR_NS
namespace cairo {
namespace utils {
namespace utils {
// implementation : device_lock {{{
@ -170,7 +170,7 @@ namespace cairo {
return 0;
}
}
}
}
} // namespace utils
} // namespace cairo
POLYBAR_NS_END

View File

@ -8,61 +8,61 @@
POLYBAR_NS
namespace string_util {
/**
/**
* Check if haystack contains needle
*/
bool contains(const string& haystack, const string& needle) {
bool contains(const string& haystack, const string& needle) {
return haystack.find(needle) != string::npos;
}
}
bool ends_with(const string& haystack, const string& suffix) {
bool ends_with(const string& haystack, const string& suffix) {
if (haystack.length() < suffix.length()) {
return false;
}
return haystack.compare(haystack.length() - suffix.length(), suffix.length(), suffix) == 0;
}
}
/**
/**
* Check if haystack contains needle ignoring case
*/
bool contains_ignore_case(const string& haystack, const string& needle) {
bool contains_ignore_case(const string& haystack, const string& needle) {
return lower(haystack).find(lower(needle)) != string::npos;
}
}
/**
/**
* Convert string to uppercase
*/
string upper(const string& s) {
string upper(const string& s) {
string str(s);
for (auto& c : str) {
c = toupper(c);
}
return str;
}
}
/**
/**
* Convert string to lowercase
*/
string lower(const string& s) {
string lower(const string& s) {
string str(s);
for (auto& c : str) {
c = tolower(c);
}
return str;
}
}
/**
/**
* Test lower case equality
*/
bool compare(const string& s1, const string& s2) {
bool compare(const string& s1, const string& s2) {
return lower(s1) == lower(s2);
}
}
/**
/**
* Replace first occurrence of needle in haystack
*/
string replace(const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) {
string replace(const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) {
string str(haystack);
string::size_type pos;
@ -73,13 +73,12 @@ namespace string_util {
}
return str;
}
}
/**
/**
* Replace all occurrences of needle in haystack
*/
string replace_all(
const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) {
string replace_all(const string& haystack, const string& needle, const string& replacement, size_t start, size_t end) {
string result{haystack};
string::size_type pos;
while ((pos = result.find(needle, start)) != string::npos && pos < result.length() &&
@ -88,69 +87,69 @@ namespace string_util {
start = pos + replacement.length();
}
return result;
}
}
/**
/**
* Replace all consecutive occurrences of needle in haystack
*/
string squeeze(const string& haystack, char needle) {
string squeeze(const string& haystack, char needle) {
string result = haystack;
while (result.find({needle, needle}) != string::npos) {
result = replace_all(result, {needle, needle}, {needle});
}
return result;
}
}
/**
/**
* Remove all occurrences of needle in haystack
*/
string strip(const string& haystack, char needle) {
string strip(const string& haystack, char needle) {
string str(haystack);
string::size_type pos;
while ((pos = str.find(needle)) != string::npos) {
str.erase(pos, 1);
}
return str;
}
}
/**
/**
* Remove trailing newline
*/
string strip_trailing_newline(const string& haystack) {
string strip_trailing_newline(const string& haystack) {
string str(haystack);
if (str[str.length() - 1] == '\n') {
str.erase(str.length() - 1, 1);
}
return str;
}
}
/**
/**
* Trims all characters that match pred from the left
*/
string ltrim(string value, function<bool(char)> pred) {
string ltrim(string value, function<bool(char)> pred) {
value.erase(value.begin(), find_if(value.begin(), value.end(), std::not_fn(pred)));
return value;
}
}
/**
/**
* Trims all characters that match pred from the right
*/
string rtrim(string value, function<bool(char)> pred) {
string rtrim(string value, function<bool(char)> pred) {
value.erase(find_if(value.rbegin(), value.rend(), std::not_fn(pred)).base(), value.end());
return value;
}
}
/**
/**
* Trims all characters that match pred from both sides
*/
string trim(string value, function<bool(char)> pred) {
string trim(string value, function<bool(char)> pred) {
return ltrim(rtrim(move(value), pred), pred);
}
}
/**
/**
* Remove needle from the start of the string
*/
string ltrim(string&& value, const char& needle) {
string ltrim(string&& value, const char& needle) {
if (value.empty()) {
return "";
}
@ -158,12 +157,12 @@ namespace string_util {
value.erase(0, 1);
}
return forward<string>(value);
}
}
/**
/**
* Remove needle from the end of the string
*/
string rtrim(string&& value, const char& needle) {
string rtrim(string&& value, const char& needle) {
if (value.empty()) {
return "";
}
@ -171,37 +170,37 @@ namespace string_util {
value.erase(value.length() - 1, 1);
}
return forward<string>(value);
}
}
/**
/**
* Remove needle from the start and end of the string
*/
string trim(string&& value, const char& needle) {
string trim(string&& value, const char& needle) {
if (value.empty()) {
return "";
}
return rtrim(ltrim(forward<string>(value), needle), needle);
}
}
/**
/**
* Counts the number of codepoints in a utf8 encoded string.
*/
size_t char_len(const string& value) {
size_t char_len(const string& value) {
// utf-8 bytes of the form 10xxxxxx are continuation bytes, so we
// simply count the number of bytes not of this form.
//
// 0xc0 = 11000000
// 0x80 = 10000000
return std::count_if(value.begin(), value.end(), [](char c) { return (c & 0xc0) != 0x80; });
}
}
/**
/**
* Truncates a utf8 string at len number of codepoints. This isn't 100%
* matching the user-perceived character count, but it should be close
* enough and avoids having to pull in something like ICU to count actual
* grapheme clusters.
*/
string utf8_truncate(string&& value, size_t len) {
string utf8_truncate(string&& value, size_t len) {
if (value.empty()) {
return "";
}
@ -223,23 +222,23 @@ namespace string_util {
value.erase(it, end);
return forward<string>(value);
}
}
/**
/**
* Join all strings in vector into a single string separated by delim
*/
string join(const vector<string>& strs, const string& delim) {
string join(const vector<string>& strs, const string& delim) {
string str;
for (auto& s : strs) {
str += (str.empty() ? "" : delim) + s;
}
return str;
}
}
/**
/**
* Explode string by delim, ignore empty tokens
*/
vector<string> split(const string& s, char delim) {
vector<string> split(const string& s, char delim) {
std::string::size_type pos = 0;
std::vector<std::string> result;
@ -250,12 +249,12 @@ namespace string_util {
}
return result;
}
}
/**
/**
* Explode string by delim, include empty tokens
*/
std::vector<std::string> tokenize(const string& str, char delimiters) {
std::vector<std::string> tokenize(const string& str, char delimiters) {
std::vector<std::string> tokens;
std::string::size_type lastPos = 0;
auto pos = str.find_first_of(delimiters, lastPos);
@ -269,59 +268,59 @@ namespace string_util {
tokens.emplace_back(str.substr(lastPos, pos - lastPos));
return tokens;
}
}
/**
/**
* Find the nth occurrence of needle in haystack starting from pos
*/
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth) {
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth) {
size_t found_pos = haystack.find(needle, pos);
if (1 == nth || string::npos == found_pos) {
return found_pos;
}
return find_nth(haystack, found_pos + 1, needle, nth - 1);
}
}
/**
/**
* Create a floating point string
*/
string floating_point(double value, size_t precision, bool fixed, const string& locale) {
string floating_point(double value, size_t precision, bool fixed, const string& locale) {
std::stringstream ss;
ss.imbue(!locale.empty() ? std::locale(locale.c_str()) : std::locale::classic());
ss << std::fixed << std::setprecision(precision) << value;
return fixed ? ss.str() : replace(ss.str(), ".00", "");
}
}
/**
/**
* Create a MiB filesize string
*/
string filesize_mib(unsigned long long kibibytes, size_t precision, const string& locale) {
string filesize_mib(unsigned long long kibibytes, size_t precision, const string& locale) {
return floating_point(kibibytes / 1024.0, precision, true, locale) + " MiB";
}
}
/**
/**
* Create a GiB filesize string
*/
string filesize_gib(unsigned long long kibibytes, size_t precision, const string& locale) {
string filesize_gib(unsigned long long kibibytes, size_t precision, const string& locale) {
return floating_point(kibibytes / 1024.0 / 1024.0, precision, true, locale) + " GiB";
}
}
/**
/**
* Create a GiB string, if the value in GiB is >= 1.0. Otherwise, create a MiB string.
*/
string filesize_gib_mib(
string filesize_gib_mib(
unsigned long long kibibytes, size_t precision_mib, size_t precision_gib, const string& locale) {
if (kibibytes < 1024 * 1024) {
return filesize_mib(kibibytes, precision_mib, locale);
} else {
return filesize_gib(kibibytes, precision_gib, locale);
}
}
}
/**
/**
* Create a filesize string by converting given bytes to highest unit possible
*/
string filesize(unsigned long long bytes, size_t precision, bool fixed, const string& locale) {
string filesize(unsigned long long bytes, size_t precision, bool fixed, const string& locale) {
vector<string> suffixes{"TB", "GB", "MB", "KB"};
string suffix{"B"};
double value = bytes;
@ -331,14 +330,14 @@ namespace string_util {
value /= 1024.0;
}
return floating_point(value, precision, fixed, locale) + " " + suffix;
}
}
/**
/**
* Compute string hash
*/
hash_type hash(const string& src) {
hash_type hash(const string& src) {
return std::hash<string>()(src);
}
}
} // namespace string_util
POLYBAR_NS_END