Skip to content

Commit

Permalink
Merge pull request #22 from wutipong/feature/clean-up2
Browse files Browse the repository at this point in the history
Clean Up #2
  • Loading branch information
wutipong authored Jan 16, 2024
2 parents 6c91c0f + ef38583 commit 082f23d
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 165 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ add_executable(font-render-tester
"text_renderer.hpp" "text_renderer.cpp"
"context.cpp"
"draw_glyph.cpp" "io_util.hpp"
)
"colors.hpp"
)

target_compile_features(font-render-tester PRIVATE cxx_std_17)
set_property(TARGET font-render-tester PROPERTY CXX_STANDARD 17)
Expand Down Expand Up @@ -58,7 +59,7 @@ function(copy_resources)
endfunction()

copy_resources(
TARGET CopyShaders
TARGET CopyFontFiles
DESTINATION "fonts"
INPUT "fonts/NotoSans-Regular.ttf" "fonts/NotoSansThai-Regular.ttf" "fonts/NotoSansCJKjp-Regular.otf" "fonts/NotoSansCJKkr-Regular.otf" "fonts/NotoSansCJKsc-Regular.otf"
)
13 changes: 13 additions & 0 deletions colors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef COLORS_HPP
#define COLORS_HPP

#include <SDL2/SDL.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};

#endif
29 changes: 14 additions & 15 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "io_util.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>

using namespace nlohmann;

Expand All @@ -29,28 +30,26 @@ void LoadContext(Context &ctx, const std::filesystem::path &path) {

json js;

// If there's something wrong with reading file, just do nothing and return.
// If there's something wrong with reading file, log an error and return.
try {
std::string str;
LoadFile(path.string(), str);
str = LoadFile<std::string>(path);

js = json::parse(str);
} catch (...) {
} catch (const json::exception& e) {
spdlog::error("Error reading context file: {}", e.what());
return;
}

// FIXME: using try/catch just to ignore exceptions is not really a good
// practice.
try {
ctx.windowBound.w = js["window_bound"]["width"];
} catch (...) {
}
try {
ctx.windowBound.h = js["window_bound"]["height"];
} catch (...) {
}
Context c = ctx;

try {
ctx.fontPath = js["font_path"];
} catch (...) {
c.windowBound.w = js["window_bound"]["width"];
c.windowBound.h = js["window_bound"]["height"];
c.fontPath = js["font_path"];
} catch (const json::exception& e) {
spdlog::error("Error reading context file value: {}", e.what());
}

ctx = c;
}
9 changes: 0 additions & 9 deletions context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ struct Context {
bool debugDescend{true};

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

SDL_Renderer *renderer{nullptr};

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

void SaveContext(const Context &ctx, const std::filesystem::path &path);
Expand Down
42 changes: 22 additions & 20 deletions draw_glyph.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "draw_glyph.hpp"

#include "colors.hpp"
/*
* `SDL_Render` uses the coordinate system where the (0,0) is in the top left
* corner and the positive Y value is in down direction, whereas the
Expand All @@ -10,8 +12,9 @@
* direction is to match the modern rendering apis such as OpenGL or DirectX.
*/

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

SDL_FRect rect{
static_cast<float>(x + g.bound.x),
Expand All @@ -28,18 +31,18 @@ void DrawGlyph(Context &ctx, const Font &font, const Glyph &g,
*/
rect.y = static_cast<float>(ctx.windowBound.h) - rect.y - rect.h;

SDL_SetRenderDrawColor(ctx.renderer, color.r, color.g, color.b, color.a);
SDL_SetRenderDrawBlendMode(ctx.renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetTextureColorMod(g.texture, color.r, color.g, color.b);

SDL_RenderCopyF(ctx.renderer, g.texture, nullptr, &rect);
SDL_RenderCopyF(renderer, g.texture, nullptr, &rect);

if (ctx.debug && ctx.debugGlyphBound) {
SDL_SetRenderDrawColor(
ctx.renderer, ctx.debugGlyphBoundColor.r, ctx.debugGlyphBoundColor.g,
ctx.debugGlyphBoundColor.b, ctx.debugGlyphBoundColor.a);
SDL_SetRenderDrawBlendMode(ctx.renderer, SDL_BLENDMODE_BLEND);
SDL_RenderDrawRectF(ctx.renderer, &rect);
SDL_SetRenderDrawColor(renderer, debugGlyphBoundColor.r,
debugGlyphBoundColor.g, debugGlyphBoundColor.b,
debugGlyphBoundColor.a);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_RenderDrawRectF(renderer, &rect);
}

if (ctx.debug && ctx.debugCaret) {
Expand All @@ -52,21 +55,20 @@ void DrawGlyph(Context &ctx, const Font &font, const Glyph &g,

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

SDL_SetRenderDrawColor(ctx.renderer, ctx.debugCaretColor.r,
ctx.debugCaretColor.g, ctx.debugCaretColor.b,
ctx.debugCaretColor.a);

SDL_SetRenderDrawBlendMode(ctx.renderer, SDL_BLENDMODE_BLEND);
SDL_RenderDrawRectF(ctx.renderer, &rect);
SDL_SetRenderDrawColor(renderer, debugCaretColor.r, debugCaretColor.g,
debugCaretColor.b, debugCaretColor.a);

SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_RenderDrawRectF(renderer, &rect);
}
}

void DrawGlyph(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) {
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) {

auto xPos = x + HBPosToFloat(hb_glyph_pos.x_offset);
auto yPos = y + HBPosToFloat(hb_glyph_pos.y_offset);

DrawGlyph(ctx, font, g, color, xPos, yPos);
DrawGlyph(renderer, ctx, font, g, color, xPos, yPos);
}
11 changes: 6 additions & 5 deletions draw_glyph.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once
#include "font.hpp"

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

void DrawGlyph(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);
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);
26 changes: 13 additions & 13 deletions font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Font::~Font() {
}

bool Font::LoadFile(const std::string &path) {
::LoadFile(path, data, std::ios::in | std::ios::binary);
data = ::LoadFile<std::vector<char>>(path, std::ios::in | std::ios::binary);
return Initialize();
}

Expand Down Expand Up @@ -124,7 +124,7 @@ void Font::SetFontSize(const int &size) {
linegap = height + descend - ascend;
}

Glyph Font::CreateGlyph(Context &ctx, const int &index) {
Glyph Font::CreateGlyph(SDL_Renderer *renderer, const int &index) {
int bearing = 0;
int advance;

Expand All @@ -139,7 +139,7 @@ Glyph Font::CreateGlyph(Context &ctx, const int &index) {
FT_Bitmap_Init(&bitmap);
FT_Bitmap_Convert(library, &face->glyph->bitmap, &bitmap, 1);

auto texture = LoadTextureFromBitmap(ctx.renderer, bitmap);
auto texture = LoadTextureFromBitmap(renderer, bitmap);

FT_Bitmap_Done(library, &bitmap);

Expand All @@ -152,16 +152,16 @@ Glyph Font::CreateGlyph(Context &ctx, const int &index) {
return {texture, bound, advance, bearing};
}

Glyph Font::CreateGlyphFromChar(Context &ctx, const char16_t &ch) {
Glyph Font::CreateGlyphFromChar(SDL_Renderer *renderer, const char16_t &ch) {
auto index = FT_Get_Char_Index(face, ch);

return CreateGlyph(ctx, index);
return CreateGlyph(renderer, index);
}

Glyph &Font::GetGlyph(Context &ctx, const int &index) {
Glyph &Font::GetGlyph(SDL_Renderer *renderer, const int &index) {
auto iter = glyphMap.find(index);
if (iter == glyphMap.end()) {
Glyph g = CreateGlyph(ctx, index);
Glyph g = CreateGlyph(renderer, index);
auto [i, success] = glyphMap.insert({index, g});

iter = i;
Expand All @@ -170,10 +170,10 @@ Glyph &Font::GetGlyph(Context &ctx, const int &index) {
return iter->second;
}

Glyph &Font::GetGlyphFromChar(Context &ctx, const char16_t &ch) {
Glyph &Font::GetGlyphFromChar(SDL_Renderer *renderer, const char16_t &ch) {
const auto index = FT_Get_Char_Index(face, ch);

return Font::GetGlyph(ctx, index);
return Font::GetGlyph(renderer, index);
}

void Font::SetTextRenderer(const TextRenderEnum &t) {
Expand All @@ -194,11 +194,11 @@ void Font::SetTextRenderer(const TextRenderEnum &t) {
}
}

void Font::RenderText(Context &ctx, const std::string &str,
const SDL_Color &color, const std::string &language,
const hb_script_t &script) {
void Font::RenderText(SDL_Renderer *renderer, Context &ctx,
const std::string &str, const SDL_Color &color,
const std::string &language, const hb_script_t &script) {
if (!IsValid())
return;

textRenderer(ctx, *this, str, color, language, script);
textRenderer(renderer, ctx, *this, str, color, language, script);
}
26 changes: 16 additions & 10 deletions font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
enum class TextRenderEnum { NoShape, LeftToRight, RightToLeft, TopToBottom };
class Font;

typedef std::function<void(Context &ctx, Font &font, const std::string &str,
const SDL_Color &color, const std::string &language,
typedef std::function<void(SDL_Renderer *renderer, Context &ctx, Font &font,
const std::string &str, const SDL_Color &color,
const std::string &language,
const hb_script_t &script)>
TextRenderFunction;

Expand All @@ -29,8 +30,12 @@ struct Glyph {
int bearing;
};

inline float FTPosToFloat(const FT_Pos &value) { return static_cast<float>(value) / 64.0f; }
inline float HBPosToFloat(const hb_position_t &value) { return static_cast<float>(value) / 64.0f; }
inline float FTPosToFloat(const FT_Pos &value) {
return static_cast<float>(value) / 64.0f;
}
inline float HBPosToFloat(const hb_position_t &value) {
return static_cast<float>(value) / 64.0f;
}

class Font {
public:
Expand All @@ -55,8 +60,8 @@ class Font {
std::string GetSubFamilyName() const { return subFamily; }
bool IsValid() const { return face != nullptr; }

Glyph &GetGlyph(Context &ctx, const int &index);
Glyph &GetGlyphFromChar(Context &ctx, const char16_t &index);
Glyph &GetGlyph(SDL_Renderer *renderer, const int &index);
Glyph &GetGlyphFromChar(SDL_Renderer *renderer, const char16_t &index);

float Ascend() const { return ascend; }
float Descend() const { return descend; }
Expand All @@ -67,16 +72,17 @@ class Font {

void SetTextRenderer(const TextRenderEnum &t);

void RenderText(Context &ctx, const std::string &str, const SDL_Color &color,
const std::string &language, const hb_script_t &script);
void RenderText(SDL_Renderer *renderer, Context &ctx, const std::string &str,
const SDL_Color &color, const std::string &language,
const hb_script_t &script);

private:
static FT_Library library;

bool Initialize();

Glyph CreateGlyph(Context &ctx, const int &ch);
Glyph CreateGlyphFromChar(Context &ctx, const char16_t &ch);
Glyph CreateGlyph(SDL_Renderer *renderer, const int &ch);
Glyph CreateGlyphFromChar(SDL_Renderer *renderer, const char16_t &ch);

std::vector<char> data{};
FT_Face face{};
Expand Down
18 changes: 14 additions & 4 deletions io_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
#include <streambuf>

template <class container_type>
void LoadFile(const std::filesystem::path &path, container_type &data,
std::ios_base::openmode mode = std::ios_base::in) {
container_type LoadFile(const std::filesystem::path &path,
std::ios_base::openmode mode = std::ios_base::in) {

typedef typename container_type::value_type value_type;
container_type data;

std::basic_ifstream<value_type> file(path, mode);

data = {std::istreambuf_iterator<value_type>(file),
std::istreambuf_iterator<value_type>()};
std::error_code ec{};
if (auto filesize = std::filesystem::file_size(path, ec);
filesize != -1 && !ec) {
data.reserve(filesize);
}

data.insert(data.end(), std::istreambuf_iterator<value_type>(file),
std::istreambuf_iterator<value_type>());

file.close();

return data;
}
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ int main(int argc, char **argv) {
SDL_SetWindowMinimumSize(window, MININUM_WIDTH, MINIMUM_HEIGHT);

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

std::string imguiIniStr = imguiIniPath.string();

Expand Down Expand Up @@ -95,7 +94,7 @@ int main(int argc, char **argv) {
ImGui::EndFrame();
ImGui::Render();

Scene::TickCurrent(ctx);
Scene::TickCurrent(renderer, ctx);

ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
Expand Down
Loading

0 comments on commit 082f23d

Please sign in to comment.