From 8080400b22347ca1ab93d8756de234c2842e31a6 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 22 Jul 2021 20:43:01 +0900 Subject: [PATCH] kuroko: bind text library in _yutani module --- apps/krk_yutani_test.krk | 5 ++++- lib/kuroko/_yutani.c | 38 ++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/apps/krk_yutani_test.krk b/apps/krk_yutani_test.krk index fc841066..6071b09e 100644 --- a/apps/krk_yutani_test.krk +++ b/apps/krk_yutani_test.krk @@ -1,11 +1,13 @@ #!/bin/kuroko from _yutani import (color, Yutani, Window, Decorator, Message, - MenuBar, MenuList, MenuEntry, MenuEntrySeparator + MenuBar, MenuList, MenuEntry, MenuEntrySeparator, + Font ) let running = True let y = Yutani() let w = Window(640,480,title="Test Window",doublebuffer=True) +let dejavu = Font("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",13) w.move(200,200) @@ -28,6 +30,7 @@ def drawWindow(): let bounds = Decorator.get_bounds(w) mb.place(bounds['left_width'],bounds['top_height'],w.width-bounds['width'],w) mb.render(w) + dejavu.draw_string(w,"Hello, world.",20,120) w.flip() drawWindow() diff --git a/lib/kuroko/_yutani.c b/lib/kuroko/_yutani.c index 14f751db..5cb24b30 100644 --- a/lib/kuroko/_yutani.c +++ b/lib/kuroko/_yutani.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -57,10 +58,8 @@ struct YutaniColor { static KrkClass * YutaniFont; struct YutaniFont { KrkInstance inst; - int fontType; + struct TT_Font * fontData; int fontSize; - double fontGamma; - double fontStroke; uint32_t fontColor; }; @@ -879,27 +878,24 @@ static KrkValue _yutani_color_str(int argc, KrkValue argv[], int hasKw) { static KrkValue _font_init(int argc, KrkValue argv[], int hasKw) { CHECK_FONT(); - if (argc < 2 || !IS_INTEGER(argv[1])) - return krk_runtimeError(vm.exceptions->typeError, "expected int for font type"); + if (argc < 2 || !IS_STRING(argv[1])) + return krk_runtimeError(vm.exceptions->typeError, "expected string for font name"); if (argc < 3 || !IS_INTEGER(argv[2])) return krk_runtimeError(vm.exceptions->typeError, "expected int for font size"); - KrkValue fontGamma = FLOATING_VAL(1.7); - KrkValue fontStroke = FLOATING_VAL(0.75); KrkValue fontColor = NONE_VAL(); if (hasKw) { - krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("gamma")), &fontGamma); - krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("stroke")), &fontStroke); krk_tableGet(AS_DICT(argv[argc]), OBJECT_VAL(S("color")), &fontColor); - if (!IS_FLOATING(fontGamma)) return krk_runtimeError(vm.exceptions->typeError, "expected float for gamma"); - if (!IS_FLOATING(fontStroke)) return krk_runtimeError(vm.exceptions->typeError, "expected float for stroke"); if (!krk_isInstanceOf(fontColor, YutaniColor)) return krk_runtimeError(vm.exceptions->typeError, "expected color"); } - self->fontType = AS_INTEGER(argv[1]); + self->fontData = tt_font_from_file(AS_CSTRING(argv[1])); + if (!self->fontData) + return krk_runtimeError(vm.exceptions->typeError, "failed to load font"); + self->fontSize = AS_INTEGER(argv[2]); - self->fontGamma = AS_FLOATING(fontGamma); - self->fontStroke = AS_FLOATING(fontStroke); + tt_set_size(self->fontData, self->fontSize); + self->fontColor = IS_NONE(fontColor) ? rgb(0,0,0) : ((struct YutaniColor*)AS_INSTANCE(fontColor))->color; return argv[0]; @@ -910,6 +906,15 @@ static KrkValue _font_size(int argc, KrkValue argv[], int hasKw) { return INTEGER_VAL(self->fontSize); } +static KrkValue _font_set_size(int argc, KrkValue argv[], int hasKw) { + CHECK_FONT(); + if (argc < 2 || !IS_INTEGER(argv[1])) + return krk_runtimeError(vm.exceptions->typeError, "expected int for font size"); + self->fontSize = AS_INTEGER(argv[1]); + tt_set_size(self->fontData, self->fontSize); + return INTEGER_VAL(self->fontSize); +} + static KrkValue _font_draw_string(int argc, KrkValue argv[], int hasKw) { CHECK_FONT(); if (argc < 2 || !krk_isInstanceOf(argv[1], GraphicsContext)) @@ -924,7 +929,7 @@ static KrkValue _font_draw_string(int argc, KrkValue argv[], int hasKw) { int32_t x = AS_INTEGER(argv[3]); int32_t y = AS_INTEGER(argv[4]); - return INTEGER_VAL(-1); + return INTEGER_VAL(tt_draw_string(ctx, self->fontData, x, y, str, self->fontColor)); } static KrkValue _font_width(int argc, KrkValue argv[], int hasKw) { @@ -933,7 +938,7 @@ static KrkValue _font_width(int argc, KrkValue argv[], int hasKw) { return krk_runtimeError(vm.exceptions->typeError, "expected str"); const char * str = AS_CSTRING(argv[1]); - return INTEGER_VAL(-1); + return INTEGER_VAL(tt_string_width(self->fontData, str)); } static void _MenuBar_gcsweep(KrkInstance * _self) { @@ -1345,6 +1350,7 @@ KrkValue krk_module_onload__yutani(void) { "Font.width(string)\n" " Calculate the rendered width of the given string when drawn with this font."; krk_defineNative(&YutaniFont->methods, "size", _font_size)->flags |= KRK_NATIVE_FLAGS_IS_DYNAMIC_PROPERTY; + krk_defineNative(&YutaniFont->methods, "set_size", _font_set_size); krk_finalizeClass(YutaniFont); MenuBarClass = krk_createClass(module, "MenuBar", NULL);