Skip to content

Commit

Permalink
refactor: create gui_helpers namespace and move create font from data…
Browse files Browse the repository at this point in the history
… there
  • Loading branch information
f0e committed Nov 3, 2024
1 parent f004fac commit 9774ef9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
25 changes: 2 additions & 23 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#include "gui.h"
#include "tasks.h"
#include "font.h"

#include "os/skia/skia_helpers.h"
#include "os/skia/skia_surface.h"
#include "include/core/SkTextBlob.h"
#include "include/utils/SkTextUtils.h"
#include "include/core/SkFont.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkData.h"
#include "gui_helpers.h"

const int font_height = 18;
const int spacing = 4;
Expand Down Expand Up @@ -165,24 +158,10 @@ void gui::redraw_window(os::Window* window) {
window->invalidateRegion(gfx::Region(rc));
}

SkFont createFontFromTrueType(const unsigned char* fontData, size_t dataSize, float fontHeight) {
sk_sp<SkData> skData = SkData::MakeWithCopy(fontData, dataSize);

// Create a typeface from SkData
sk_sp<SkTypeface> typeface = SkTypeface::MakeFromData(skData);

if (!typeface) {
printf("failed to create font\n");
return SkFont();
}

return SkFont(typeface, SkIntToScalar(fontHeight));
}

void gui::run() {
auto system = os::make_system();

font = createFontFromTrueType(VT323_Regular_ttf, VT323_Regular_ttf_len, font_height);
font = gui_helpers::create_font_from_data(VT323_Regular_ttf, VT323_Regular_ttf_len, font_height);

system->setAppMode(os::AppMode::GUI);
system->handleWindowResize = redraw_window;
Expand Down
15 changes: 15 additions & 0 deletions src/gui/gui_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "gui_helpers.h"

SkFont gui_helpers::create_font_from_data(const unsigned char* font_data, size_t data_size, float font_height) {
sk_sp<SkData> skData = SkData::MakeWithCopy(font_data, data_size);

// Create a typeface from SkData
sk_sp<SkTypeface> typeface = SkTypeface::MakeFromData(skData);

if (!typeface) {
printf("failed to create font\n");
return SkFont();
}

return SkFont(typeface, SkIntToScalar(font_height));
}
5 changes: 5 additions & 0 deletions src/gui/gui_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace gui_helpers {
SkFont create_font_from_data(const unsigned char* font_data, size_t data_size, float font_height);
}
9 changes: 9 additions & 0 deletions src/gui/gui_pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
#include "base/paths.h"
#include "os/os.h"

#include "os/skia/skia_helpers.h"
#include "os/skia/skia_surface.h"

#include "include/core/SkTextBlob.h"
#include "include/utils/SkTextUtils.h"
#include "include/core/SkFont.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkData.h"

// resources
#include "../resources/resource.h"

0 comments on commit 9774ef9

Please sign in to comment.