Skip to content

Commit

Permalink
Feature/settings (#26)
Browse files Browse the repository at this point in the history
* refactor a little more.

* add settings.

* fix collapsing parameter crashes.

* add background color
  • Loading branch information
wutipong authored Jan 22, 2024
1 parent 7324909 commit 1633d15
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 152 deletions.
15 changes: 8 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ project(font-render-tester)

# add the executable
add_executable(font-render-tester
"main.cpp"
"scene.hpp" "scene.cpp"
"colors.hpp"
"context.hpp"
"draw_glyph.cpp" "io_util.hpp"
"font.cpp" "font.hpp"
"imgui-filebrowser/imfilebrowser.h"
"texture.hpp" "texture.cpp"
"main_scene.hpp" "main_scene.cpp"
"font.cpp" "font.hpp"
"main.cpp"
"scene.hpp" "scene.cpp"
"settings.hpp" "settings.cpp"
"text_renderer.hpp" "text_renderer.cpp"
"context.cpp"
"draw_glyph.cpp" "io_util.hpp"
"colors.hpp"
"texture.hpp" "texture.cpp"
)

target_compile_features(font-render-tester PRIVATE cxx_std_23)
Expand Down
38 changes: 23 additions & 15 deletions colors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,46 @@
#define COLORS_HPP

#include <SDL2/SDL.h>
#include <array>
#include <imgui.h>

constexpr SDL_Color debugGlyphBoundColor{0xFF, 0xFF, 0x80, 0xFF};
constexpr SDL_Color debugBaselineColor{0xFF, 0x00, 0x00, 0xFF};
constexpr SDL_Color debugCaretColor{0x00, 0xFF, 0xFF, 0xFF};
constexpr SDL_Color debugAscendColor{0x40, 0x40, 0xFF, 0x80};
constexpr SDL_Color debugDescendColor{0x40, 0xFF, 0x40, 0x80};
constexpr SDL_Color backgroundColor{0x80, 0x80, 0x80, 0xFF};

constexpr SDL_Color defaultForegroundColor{0x00, 0x00, 0x00, 0xFF};
constexpr SDL_Color defaultBackgroundColor{0x80, 0x80, 0x80, 0xFF};

constexpr ImVec4 SDLColorToImVec4(const SDL_Color &color) {
ImVec4 output{
return ImVec4(static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f);
}

constexpr std::array<float, 4> SDLColorToFloat4(const SDL_Color &color) {
return {
static_cast<float>(color.r) / 255.0f,
static_cast<float>(color.g) / 255.0f,
static_cast<float>(color.b) / 255.0f,
static_cast<float>(color.a) / 255.0f,
};

return output;
}

constexpr auto f4DebugGlyphBoundColor = SDLColorToImVec4(debugGlyphBoundColor);
constexpr auto f4DebugAscendColor = SDLColorToImVec4(debugAscendColor);
constexpr auto f4DebugDescendColor = SDLColorToImVec4(debugDescendColor);
constexpr auto f4DebugBaselineColor = SDLColorToImVec4(debugBaselineColor);
constexpr auto f4DebugCaretColor = SDLColorToImVec4(debugCaretColor);

constexpr SDL_Color Float4ToSDLColor(const float& r, const float& g, const float &b, const float &a = 1.0f ){
constexpr SDL_Color Float4ToSDLColor(const float &r, const float &g,
const float &b, const float &a = 1.0f) {
return {
static_cast<uint8_t>(r * 255.0f),
static_cast<uint8_t>(g * 255.0f),
static_cast<uint8_t>(b * 255.0f),
static_cast<uint8_t>(a * 255.0f),
.r = static_cast<uint8_t>(r * 255.0f),
.g = static_cast<uint8_t>(g * 255.0f),
.b = static_cast<uint8_t>(b * 255.0f),
.a = static_cast<uint8_t>(a * 255.0f),
};
}

constexpr SDL_Color Float4ToSDLColor(const std::array<float, 4> &f4) {
return Float4ToSDLColor(f4[0], f4[1], f4[2], f4[3]);
}

#endif
18 changes: 1 addition & 17 deletions context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@
#define CONTEXT_HPP

#include <SDL2/SDL.h>
#include <filesystem>

constexpr int MININUM_WIDTH = 800;
constexpr int MINIMUM_HEIGHT = 600;

struct Context {
SDL_Rect windowBound{
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MININUM_WIDTH,
MINIMUM_HEIGHT,
};

SDL_Rect windowBound{};
bool debug{false};
bool debugGlyphBound{true};
bool debugBaseline{true};
bool debugCaret{true};
bool debugAscend{true};
bool debugDescend{true};
bool showTextEditor{true};

std::string fontPath{std::filesystem::absolute("fonts").string()};
};

void SaveContext(const Context &ctx, const std::filesystem::path &path);
void LoadContext(Context &ctx, const std::filesystem::path &path);

#endif
7 changes: 5 additions & 2 deletions draw_glyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
static_cast<float>(g.bound.h),
};

SDL_Rect bound;
SDL_RenderGetViewport(renderer, &bound);

/*
* Adjust the coordinate, and recalculate the new y origin of the rectangle.
*
* The given rectangle value has its origin in the bottom-left corner while
* SDL expects the origin in the top-left corner.
*/
rect.y = static_cast<float>(ctx.windowBound.h) - rect.y - rect.h;
rect.y = static_cast<float>(bound.h) - rect.y - rect.h;

SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
Expand All @@ -53,7 +56,7 @@ void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
1,
};

rect.y = static_cast<float>(ctx.windowBound.h) - rect.y - rect.h;
rect.y = static_cast<float>(bound.h) - rect.y - rect.h;

SDL_SetRenderDrawColor(renderer, debugCaretColor.r, debugCaretColor.g,
debugCaretColor.b, debugCaretColor.a);
Expand Down
8 changes: 6 additions & 2 deletions draw_glyph.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#ifndef DRAW_GLYPH_HPP
#define DRAW_GLYPH_HPP

#include "font.hpp"

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
Expand All @@ -7,4 +9,6 @@ void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
const Glyph &g, const SDL_Color &color, const int &x,
const int &y, const hb_glyph_position_t &hb_glyph_pos);
const int &y, const hb_glyph_position_t &hb_glyph_pos);

#endif
6 changes: 3 additions & 3 deletions font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ void Font::SetVariationValues(
if (!IsValid())
return;

auto limits = GetAxisInfos();
auto axisInfos = GetAxisInfos();
std::vector<hb_variation_t> variations;

FT_MM_Var *amaster;
FT_Get_MM_Var(ftFace, &amaster);

magic_enum::enum_for_each<VariationAxis>(
[&limits, &variations, &values](const VariationAxis &axis) {
if (limits[axis].has_value()) {
[&axisInfos, &variations, &values](const VariationAxis &axis) {
if (axisInfos[axis].has_value()) {
variations.push_back({
.tag = axisTagMap.at(axis),
.value = values[axis],
Expand Down
6 changes: 6 additions & 0 deletions io_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <filesystem>
#include <fstream>
#include <streambuf>
#include <SDL2/SDL.h>

template <class container_type>
container_type LoadFile(const std::filesystem::path &path,
Expand All @@ -26,3 +27,8 @@ container_type LoadFile(const std::filesystem::path &path,

return data;
}

static std::filesystem::path GetPreferencePath() {
return std::filesystem::path(
SDL_GetPrefPath("sleepyheads.info", "font-render-tester"));
}
37 changes: 20 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,44 @@
#include <spdlog/spdlog.h>

#include "context.hpp"
#include "io_util.hpp"
#include "main_scene.hpp"
#include "scene.hpp"

static constexpr char imguiIni[] = "imgui.ini";
static constexpr char contextJson[] = "context.json";
static constexpr char logfile[] = "log.txt";

static constexpr int windowMinimumWidth = 1280;
static constexpr int windowMinimumHeight = 720;

int main(int argc, char **argv) {
auto preferencePath = std::filesystem::path(
SDL_GetPrefPath("sleepyheads.info", "font-render-tester"));
auto imguiIniPath = preferencePath / imguiIni;
auto contextIniPath = preferencePath / contextJson;
const auto preferencePath = GetPreferencePath();

auto max_size = 5 * 1024 * 1024;
auto max_files = 3;
constexpr auto maxLogFileSize = 5 * 1024 * 1024;
constexpr auto maxLogFile = 3;

auto logFilePath = preferencePath / logfile;
auto logger = spdlog::rotating_logger_mt("logger", logFilePath.string(),
max_size, max_files);
const auto logFilePath = preferencePath / logfile;
const auto logger = spdlog::rotating_logger_mt("logger", logFilePath.string(),
maxLogFileSize, maxLogFile);
logger->flush_on(spdlog::level::info);
spdlog::set_default_logger(logger);

Context ctx{};
LoadContext(ctx, contextIniPath);

SDL_Init(SDL_INIT_EVERYTHING);
SDL_Init(SDL_INIT_VIDEO);

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");

SDL_Window *window = SDL_CreateWindow(
"font-render-tester", ctx.windowBound.x, ctx.windowBound.y,
ctx.windowBound.w, ctx.windowBound.h,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL);
SDL_SetWindowMinimumSize(window, MININUM_WIDTH, MINIMUM_HEIGHT);
"font-render-tester", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
windowMinimumWidth, windowMinimumHeight,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);

SDL_SetWindowMinimumSize(window, windowMinimumWidth, windowMinimumHeight);

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

const auto imguiIniPath = preferencePath / imguiIni;
std::string imguiIniStr = imguiIniPath.string();

IMGUI_CHECKVERSION();
Expand Down Expand Up @@ -102,7 +105,7 @@ int main(int argc, char **argv) {
SDL_Delay(1);
}

SaveContext(ctx, contextIniPath);
Scene::Current()->CleanUp(ctx);

ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
Expand Down
Loading

0 comments on commit 1633d15

Please sign in to comment.