Skip to content

Commit

Permalink
feat: call skia text draw directly because fonts are broken in skia l…
Browse files Browse the repository at this point in the history
…af? and use a custom font. also reduce window size
  • Loading branch information
f0e committed Nov 3, 2024
1 parent b3659de commit f150f6c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function(setup_target target)
)
endfunction()

option(BUILD_CLI "Build CLI application" OFF)
option(BUILD_CLI "Build CLI application" ON)
option(BUILD_GUI "Build GUI application" ON)

# cli
Expand All @@ -73,7 +73,6 @@ if (BUILD_GUI)
set(LAF_BACKEND "skia")
set(SKIA_DIR ${PROJECT_SOURCE_DIR}/dependencies/skia)
set(SKIA_LIBRARY_DIR ${SKIA_DIR}/out/Release-arm64)
set(SKIA_LIBRARY ${SKIA_LIBRARY_DIR}/libskia.a)

set(LAF_WITH_EXAMPLES OFF) # disable examples
set(LAF_WITH_TESTS OFF) # disable tests
Expand Down
5 changes: 5 additions & 0 deletions src/gui/font.h

Large diffs are not rendered by default.

75 changes: 61 additions & 14 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#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"

const int font_height = 18;
const int spacing = 4;
const int pad_x = 24;
const int pad_y = 35;

SkFont font;

void gui::DragTarget::dragEnter(os::DragEvent& ev) {
// v.dropResult(os::DropOperation::None); // TODO: what does this do? is it needed?
Expand Down Expand Up @@ -39,7 +55,7 @@ void gui::DragTarget::drop(os::DragEvent& ev) {
static os::WindowRef create_window(os::DragTarget& dragTarget) {
auto screen = os::instance()->mainScreen();

os::WindowRef window = os::instance()->makeWindow(800, 600);
os::WindowRef window = os::instance()->makeWindow(500, 350);
window->setCursor(os::NativeCursor::Arrow);
window->setTitle("Blur");
window->setDragTarget(&dragTarget);
Expand Down Expand Up @@ -83,36 +99,50 @@ void gui::redraw_window(os::Window* window) {

os::Paint paint;

// black background
paint.color(gfx::rgba(0, 0, 0, 255));
// background
int bg_shade = windowData.dragging ? 10 : 0;
paint.color(gfx::rgba(bg_shade, bg_shade, bg_shade, 255));
s->drawRect(rc, paint);

const static int spacing = 20;
const static int pad_x = 25;
const static int pad_y = 35;
// text
int y = 0;

auto draw_str_temp = [&y, &s, &paint](std::string text, gfx::Color colour = gfx::rgba(255, 255, 255, 255)) {
paint.color(colour);

os::TextAlign textAlign = os::TextAlign::Left;

gfx::Point pos(pad_x, pad_y + y);

// todo: clip string
os::draw_text(s, nullptr, text, gfx::Point(pad_x, pad_y + y), &paint);
// os::draw_text font broken with skia bruh
SkTextUtils::Draw(
&static_cast<os::SkiaSurface*>(s)->canvas(),
text.c_str(),
text.size(),
SkTextEncoding::kUTF8,
SkIntToScalar(pos.x),
SkIntToScalar(pos.y),
font,
paint.skPaint(),
(SkTextUtils::Align)textAlign
);

y += font_height + spacing;
};

y += spacing;
auto draw_wstr_temp = [&draw_str_temp](std::wstring text, gfx::Color colour = gfx::rgba(255, 255, 255, 255)) {
draw_str_temp(base::to_utf8(text), colour);
};

draw_str_temp("blur");
draw_str_temp("drop a file...");

if (windowData.dragging) {
draw_str_temp("drop me bro");
}

if (!rendering.queue.empty()) {
for (const auto [i, render] : helpers::enumerate(rendering.queue)) {
bool current = render == rendering.current_render;

std::string name_str = helpers::tostring(render->get_video_name());
draw_str_temp(name_str, gfx::rgba(255, 255, 255, (current ? 255 : 100)));
draw_wstr_temp(render->get_video_name(), gfx::rgba(255, 255, 255, (current ? 255 : 100)));

if (current) {
auto render_status = render->get_status();
Expand All @@ -135,8 +165,25 @@ 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);

system->setAppMode(os::AppMode::GUI);
system->handleWindowResize = redraw_window;

Expand Down

0 comments on commit f150f6c

Please sign in to comment.