srctree

Robin Linden parent 04644a99 c459e9d2
gfx: Turn FontStyle into a bit flag enum

This will simplify handling combinations of styles.

inlinesplit
gfx/font.h added: 64, removed: 5, total 59
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -6,6 +6,7 @@
#define GFX_FONT_H_
 
#include <string_view>
#include <utility>
 
namespace gfx {
 
@@ -18,10 +19,41 @@ struct FontSize {
};
 
enum class FontStyle {
Normal,
Italic,
Normal = 0,
Italic = 1 << 0,
};
 
constexpr FontStyle operator|(FontStyle lhs, FontStyle rhs) {
return static_cast<FontStyle>(std::to_underlying(lhs) | std::to_underlying(rhs));
}
 
constexpr FontStyle &operator|=(FontStyle &lhs, FontStyle rhs) {
lhs = lhs | rhs;
return lhs;
}
 
constexpr FontStyle operator&(FontStyle lhs, FontStyle rhs) {
return static_cast<FontStyle>(std::to_underlying(lhs) & std::to_underlying(rhs));
}
 
constexpr FontStyle &operator&=(FontStyle &lhs, FontStyle rhs) {
lhs = lhs & rhs;
return lhs;
}
 
constexpr FontStyle operator^(FontStyle lhs, FontStyle rhs) {
return static_cast<FontStyle>(std::to_underlying(lhs) ^ std::to_underlying(rhs));
}
 
constexpr FontStyle &operator^=(FontStyle &lhs, FontStyle rhs) {
lhs = lhs ^ rhs;
return lhs;
}
 
constexpr FontStyle operator~(FontStyle v) {
return static_cast<FontStyle>(~std::to_underlying(v));
}
 
} // namespace gfx
 
#endif
 
filename was Deleted added: 64, removed: 5, total 59
@@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "gfx/font.h"
 
#include "etest/etest.h"
 
using etest::expect;
using etest::expect_eq;
 
int main() {
etest::test("used as a bitset", [] {
auto style = gfx::FontStyle::Normal;
expect((style & gfx::FontStyle::Italic) != gfx::FontStyle::Italic);
 
style |= gfx::FontStyle::Italic;
expect((style & gfx::FontStyle::Italic) == gfx::FontStyle::Italic);
 
style &= ~gfx::FontStyle::Italic;
expect((style & gfx::FontStyle::Italic) != gfx::FontStyle::Italic);
 
expect_eq(style, gfx::FontStyle::Normal);
});
 
return etest::run_all_tests();
}