Skip to content

Commit

Permalink
Merge pull request #23 from brisklib/rich-text
Browse files Browse the repository at this point in the history
Rich text support
  • Loading branch information
dancazarin authored Jan 29, 2025
2 parents 0bdeaa4 + 1b582b1 commit 73f8949
Show file tree
Hide file tree
Showing 67 changed files with 1,923 additions and 641 deletions.
2 changes: 1 addition & 1 deletion examples/calc/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CalcComponent final : public Component {
rcnew Text{
text = calc.valOutput(),
textAlign = TextAlign::End,
fontFamily = Monospace,
fontFamily = Font::Monospace,
fontSize = 40,
padding = 12,
color = 0x3F3F3F_rgb,
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ RC<Widget> ShowcaseButtons::build(RC<Notifications> notifications) {
rcnew Viewport{
[](Canvas& canvas, Rectangle rect) {
canvas.setFillColor(Palette::Standard::amber);
PreparedText text = fonts->prepare(Font{ FontFamily::Default, dp(18) },
PreparedText text = fonts->prepare(Font{ Font::DefaultPlusIconsEmoji, dp(18) },
"This text is rendered dynamically.");
float x = fract(currentTime() * 0.1) * text.bounds().width();
PointF offset = text.alignLines(0.f, 0.5f);
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/Dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
}),
rcnew Text{
text = Value{ &m_text },
fontFamily = Monospace,
fontFamily = Font::Monospace,
},
};
}
Expand Down
16 changes: 15 additions & 1 deletion examples/showcase/src/Editors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ RC<Widget> ShowcaseEditors::build(RC<Notifications> notifications) {

rcnew HLayout{
rcnew Widget{
rcnew PasswordEditor(Value{ &m_password }, width = 100_perc, fontFamily = Monospace,
rcnew PasswordEditor(Value{ &m_password }, width = 100_perc, fontFamily = Font::Monospace,
passwordChar =
Value{ &m_hidePassword }.transform([](bool v) -> char32_t {
return v ? '*' : 0;
Expand All @@ -116,6 +116,20 @@ RC<Widget> ShowcaseEditors::build(RC<Notifications> notifications) {
rcnew CheckBox{ value = Value{ &m_hidePassword }, rcnew Text{ "Hide password" } },
},

rcnew Text{ "Basic HTML", classes = { "section-header" } },

rcnew HLayout{
rcnew Widget{
rcnew TextEditor(Value{ &m_html }, fontSize = 150_perc, width = 100_perc),
&m_group,
},
gapColumn = 10_apx,
rcnew Text{
text = Value{ &m_html },
layoutOptions = LayoutOptions::HTML,
},
},

rcnew Text{ "ColorView (widgets/Color.hpp)", classes = { "section-header" } },

rcnew HLayout{
Expand Down
2 changes: 2 additions & 0 deletions examples/showcase/src/Editors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ShowcaseEditors : public BindingObject<ShowcaseEditors, &uiThread> {
float m_value = 50.f;
float m_y = 50.f;
std::string m_text;
std::string m_html = "The <b>quick</b> <font color=\"brown\">brown</font> <u>fox jumps</u> over "
"the <small>lazy</small> dog";
std::string m_multilineText = "abc\ndef\nghijklmnopqrstuvwxyz";
ColorF m_color = Palette::Standard::indigo;
std::string m_password = "";
Expand Down
2 changes: 1 addition & 1 deletion examples/showcase/src/ShowcaseComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static RC<Stylesheet> mainStylesheet = rcnew Stylesheet{
Selectors::Class{ "section-header" },
Rules{
fontSize = 14_px,
fontFamily = Monospace,
fontFamily = Font::Monospace,
color = 0x5599ff_rgb,
margin = { 0, 10_apx },

Expand Down
22 changes: 11 additions & 11 deletions examples/showcase/src/Typography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Brisk {
static Builder iconsBuilder() {
return Builder([](Widget* target) {
constexpr int columns = 16;
auto iconFontFamily = GoNoto;
auto iconFontFamily = Font::Icons;
int iconFontSize = 25;
for (int icon = ICON__first; icon < ICON__last; icon += columns) {
RC<HLayout> glyphs = rcnew HLayout{
Expand All @@ -18,8 +18,8 @@ static Builder iconsBuilder() {
},
};
for (int c = 0; c < columns; c++) {
char32_t ch = icon + c;
string u8 = utf32ToUtf8(std::u32string(1, ch));
char32_t ch = icon + c;
std::string u8 = utf32ToUtf8(std::u32string(1, ch));
glyphs->apply(rcnew Text{
u8,
classes = { "icon" },
Expand Down Expand Up @@ -63,19 +63,19 @@ RC<Widget> ShowcaseTypography::build(RC<Notifications> notifications) {
Builder([](Widget* target) {
for (int i = 0; i < 7; ++i) {
int size = 8 + i * 4;
auto row = [target, size](std::string name, FontFamily family, FontWeight weight) {
auto row = [target, size](std::string name, std::string family, FontWeight weight) {
target->apply(rcnew Text{
pangram + fmt::format(" [{}, {}px]", name, size),
fontFamily = family,
fontFamily = std::move(family),
fontWeight = weight,
fontSize = size,
});
};
row("Lato Light", Lato, FontWeight::Light);
row("Lato Regular", Lato, FontWeight::Regular);
row("Lato Bold", Lato, FontWeight::Bold);
row("GoNoto", GoNoto, FontWeight::Regular);
row("Monospace", Monospace, FontWeight::Regular);
row("Lato Light", "Lato", FontWeight::Light);
row("Lato Regular", "Lato", FontWeight::Regular);
row("Lato Bold", "Lato", FontWeight::Bold);
row("GoNoto", "Noto", FontWeight::Regular);
row("Monospace", Font::Monospace, FontWeight::Regular);
target->apply(rcnew Spacer{ height = 12_apx });
}
}),
Expand All @@ -88,7 +88,7 @@ RC<Widget> ShowcaseTypography::build(RC<Notifications> notifications) {
rcnew Text{
"gΥφ fi fl3.14 1/3 LT",
fontSize = 40,
fontFamily = Lato,
fontFamily = "Lato",
fontFeatures = Value{ &m_fontFeatures },
letterSpacing = Value{ &m_letterSpacing },
wordSpacing = Value{ &m_wordSpacing },
Expand Down
22 changes: 14 additions & 8 deletions examples/showcase/src/Visual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ RC<Widget> ShowcaseVisual::build(RC<Notifications> notifications) {
padding = { 16, 5 },
};
static const Rules headerCell = Rules{
layout = Layout::Horizontal, fontFamily = Lato, fontWeight = FontWeight::Bold,
color = 0x808080_rgb, padding = { 16, 5 },
layout = Layout::Horizontal,
fontWeight = FontWeight::Bold,
color = 0x808080_rgb,
padding = { 16, 5 },
};

return rcnew VLayout{
Expand Down Expand Up @@ -65,11 +67,15 @@ RC<Widget> ShowcaseVisual::build(RC<Notifications> notifications) {
},
// Overflow::ScrollX prevents this widget from stretching because of Text
overflow = Overflow::ScrollX,
rcnew Text{ loremIpsumShort, wordWrap = true, textAlign = Value{ &m_textAlign },
marginTop = 10_apx, fontSize = Value{ &m_fontSize }.transform([](float v) {
return v * 100_perc;
}),
fontFamily = Lato },
rcnew Text{
loremIpsumShort,
wordWrap = true,
textAlign = Value{ &m_textAlign },
marginTop = 10_apx,
fontSize = Value{ &m_fontSize }.transform([](float v) {
return v * 100_perc;
}),
},
},

rcnew Text{ "Viewport (widgets/Viewport.hpp)", classes = { "section-header" } },
Expand Down Expand Up @@ -152,7 +158,7 @@ RC<Widget> ShowcaseVisual::build(RC<Notifications> notifications) {
// Set the fill color to lime green and set the font to "Lato" with a size of 48
// pixels. Draw the text "Brisk" centered inside the rectangle 'frect'.
canvas.setFillColor(Palette::Standard::lime);
canvas.setFont(Font{ Lato, 48_dp });
canvas.setFont(Font{ "Lato", 48_dp });
canvas.fillText("Brisk", frect.at(0.5f, 0.5f));
},
dimensions = { 256, 256 },
Expand Down
147 changes: 83 additions & 64 deletions include/brisk/core/BasicTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,6 @@ std::span<const T> one(const T& value) noexcept {
return std::span<const T>{ &value, 1 };
}

/**
* @file
* @brief Type alias definitions and utility functions for working with various string and byte types.
*/

using std::string;
using std::string_view;
using std::u16string;
using std::u16string_view;
using std::u32string;
using std::u32string_view;
using std::wstring;
using std::wstring_view;

/**
* @brief Type alias for a single byte (8-bit unsigned integer).
*/
Expand Down Expand Up @@ -734,56 +720,89 @@ Range(T, T) -> Range<T, false>;
template <typename T>
using InclusiveRange = Range<T>;

#define BRISK_FLAGS(TYPE) \
constexpr std::underlying_type_t<TYPE> operator+(TYPE x) noexcept { \
return static_cast<std::underlying_type_t<TYPE>>(x); \
} \
constexpr bool operator&&(TYPE flags, TYPE flag) noexcept { \
return (+flags & +flag) != 0; \
} \
constexpr TYPE operator|(TYPE x, TYPE y) noexcept { \
return static_cast<TYPE>(+x | +y); \
} \
constexpr TYPE operator&(TYPE x, TYPE y) noexcept { \
return static_cast<TYPE>(+x & +y); \
} \
constexpr TYPE& operator|=(TYPE& x, TYPE y) noexcept { \
return x = static_cast<TYPE>(+x | +y); \
} \
constexpr TYPE& operator^=(TYPE& x, TYPE y) noexcept { \
return x = static_cast<TYPE>(+x ^ +y); \
} \
constexpr TYPE operator^(TYPE x, TYPE y) noexcept { \
return static_cast<TYPE>(+x ^ +y); \
} \
constexpr TYPE operator~(TYPE x) noexcept { \
return static_cast<TYPE>(~+x); \
} \
constexpr TYPE& operator&=(TYPE& x, TYPE y) noexcept { \
return x = static_cast<TYPE>(+x & +y); \
} \
/*constexpr TYPE operator<<(TYPE& x, int y) { return static_cast<TYPE>(+x << y); } */ \
constexpr TYPE operator>>(TYPE& x, int y) noexcept { \
return static_cast<TYPE>(+x >> y); \
} \
constexpr TYPE& operator<<=(TYPE& x, int y) noexcept { \
return x = static_cast<TYPE>(+x << y); \
} \
constexpr TYPE& operator>>=(TYPE& x, int y) noexcept { \
return x = static_cast<TYPE>(+x >> y); \
} \
constexpr TYPE operator+(TYPE x, TYPE y) noexcept { \
return static_cast<TYPE>(+x + +y); \
} \
constexpr TYPE& operator+=(TYPE& x, TYPE y) noexcept { \
return x = static_cast<TYPE>(+x + +y); \
} \
constexpr void toggle(TYPE& x, TYPE y, bool flag) noexcept { \
if (flag) \
x |= y; \
else \
x &= ~y; \
}
template <typename T>
constexpr inline bool isBitFlags = false;

template <typename T>
concept BitFlags = isBitFlags<T>;

template <BitFlags T>
constexpr std::underlying_type_t<T> operator+(T x) noexcept {
return static_cast<std::underlying_type_t<T>>(x);
}

template <BitFlags T>
constexpr bool operator&&(T flags, T flag) noexcept {
return (+flags & +flag) != 0;
}

template <BitFlags T>
constexpr T operator|(T x, T y) noexcept {
return static_cast<T>(+x | +y);
}

template <BitFlags T>
constexpr T operator&(T x, T y) noexcept {
return static_cast<T>(+x & +y);
}

template <BitFlags T>
constexpr T& operator|=(T& x, T y) noexcept {
return x = static_cast<T>(+x | +y);
}

template <BitFlags T>
constexpr T& operator^=(T& x, T y) noexcept {
return x = static_cast<T>(+x ^ +y);
}

template <BitFlags T>
constexpr T operator^(T x, T y) noexcept {
return static_cast<T>(+x ^ +y);
}

template <BitFlags T>
constexpr T operator~(T x) noexcept {
return static_cast<T>(~+x);
}

template <BitFlags T>
constexpr T& operator&=(T& x, T y) noexcept {
return x = static_cast<T>(+x & +y);
} /*constexpr T operator<<(T& x, int y) { return static_cast<T>(+x << y); } */

template <BitFlags T>
constexpr T operator>>(T& x, int y) noexcept {
return static_cast<T>(+x >> y);
}

template <BitFlags T>
constexpr T& operator<<=(T& x, int y) noexcept {
return x = static_cast<T>(+x << y);
}

template <BitFlags T>
constexpr T& operator>>=(T& x, int y) noexcept {
return x = static_cast<T>(+x >> y);
}

template <BitFlags T>
constexpr T operator+(T x, T y) noexcept {
return static_cast<T>(+x + +y);
}

template <BitFlags T>
constexpr T& operator+=(T& x, T y) noexcept {
return x = static_cast<T>(+x + +y);
}

template <BitFlags T>
constexpr void toggle(T& x, T y, bool flag) noexcept {
if (flag)
x |= y;
else
x &= ~y;
}

struct Empty {};

Expand Down
14 changes: 7 additions & 7 deletions include/brisk/core/Cryptography.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,55 +202,55 @@ using SHA3_512Hash = FixedBits<512>; ///< Alias for SHA3-512 hash (512 bits)
* @param data The string to hash.
* @return The resulting hash as a `Bytes` object.
*/
[[nodiscard]] Bytes hash(HashMethod method, string_view data);
[[nodiscard]] Bytes hash(HashMethod method, std::string_view data);

/**
* @brief Computes the MD5 hash of a string.
*
* @param data The string to hash.
* @return The resulting MD5 hash as an `MD5Hash` object.
*/
[[nodiscard]] MD5Hash md5(string_view data);
[[nodiscard]] MD5Hash md5(std::string_view data);

/**
* @brief Computes the SHA-1 hash of a string.
*
* @param data The string to hash.
* @return The resulting SHA-1 hash as a `SHA1Hash` object.
*/
[[nodiscard]] SHA1Hash sha1(string_view data);
[[nodiscard]] SHA1Hash sha1(std::string_view data);

/**
* @brief Computes the SHA-256 hash of a string.
*
* @param data The string to hash.
* @return The resulting SHA-256 hash as a `SHA256Hash` object.
*/
[[nodiscard]] SHA256Hash sha256(string_view data);
[[nodiscard]] SHA256Hash sha256(std::string_view data);

/**
* @brief Computes the SHA-512 hash of a string.
*
* @param data The string to hash.
* @return The resulting SHA-512 hash as a `SHA512Hash` object.
*/
[[nodiscard]] SHA512Hash sha512(string_view data);
[[nodiscard]] SHA512Hash sha512(std::string_view data);

/**
* @brief Computes the SHA3-256 hash of a string.
*
* @param data The string to hash.
* @return The resulting SHA3-256 hash as a `SHA3_256Hash` object.
*/
[[nodiscard]] SHA3_256Hash sha3_256(string_view data);
[[nodiscard]] SHA3_256Hash sha3_256(std::string_view data);

/**
* @brief Computes the SHA3-512 hash of a string.
*
* @param data The string to hash.
* @return The resulting SHA3-512 hash as a `SHA3_512Hash` object.
*/
[[nodiscard]] SHA3_512Hash sha3_512(string_view data);
[[nodiscard]] SHA3_512Hash sha3_512(std::string_view data);

/**
* @brief Provides a common interface for hashers, enabling generic hashing functionality.
Expand Down
Loading

0 comments on commit 73f8949

Please sign in to comment.