Draw more text

This commit is contained in:
K. Lange 2018-03-01 15:10:57 +09:00 committed by Kevin Lange
parent 2cd9ce1125
commit 476fbb5ad1
5 changed files with 293 additions and 26 deletions

View File

@ -68,11 +68,14 @@ base/lib/libtoaru_rline.so: lib/rline.c lib/rline.h base/lib/libtoaru_kbd.so
base/lib/libtoaru_termemu.so: lib/termemu.c lib/termemu.h base/lib/libtoaru_graphics.so
$(CC) -o $@ $(CFLAGS) -shared -fPIC $< -ltoaru_graphics
base/lib/libtoaru_drawstring.so: lib/drawstring.c lib/drawstring.h base/lib/libtoaru_graphics.so
$(CC) -o $@ $(CFLAGS) -shared -fPIC $< -ltoaru_graphics
base/lib/libtoaru_decorations.so: lib/decorations.c lib/decorations.h base/lib/libtoaru_graphics.so
$(CC) -o $@ $(CFLAGS) -shared -fPIC $< -ltoaru_graphics
base/lib/libtoaru-decor-fancy.so: decors/decor-fancy.c lib/decorations.h base/lib/libtoaru_graphics.so base/lib/libtoaru_decorations.so
$(CC) -o $@ $(CFLAGS) -shared -fPIC $< -ltoaru_decorations -ltoaru_graphics
base/lib/libtoaru-decor-fancy.so: decors/decor-fancy.c lib/decorations.h base/lib/libtoaru_graphics.so base/lib/libtoaru_decorations.so base/lib/libtoaru_drawstring.so
$(CC) -o $@ $(CFLAGS) -shared -fPIC $< -ltoaru_decorations -ltoaru_drawstring -ltoaru_graphics
base/bin/init: init.c base/lib/libnihc.a | dirs
$(CC) -static -Wl,-static $(CFLAGS) -o $@ $< $(LIBS)
@ -86,8 +89,8 @@ base/bin/sysinfo: sysinfo.c base/lib/libnihc.so base/lib/libtoaru_graphics.so ba
base/bin/terminal: terminal.c base/lib/libnihc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_decorations.so base/lib/libtoaru_dlfcn.so base/lib/libtoaru_list.so base/lib/libtoaru_kbd.so base/lib/libtoaru_termemu.so base/lib/libtoaru_pex.so base/lib/libtoaru_hashmap.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_termemu -ltoaru_decorations -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_hashmap -ltoaru_dlfcn -ltoaru_kbd -ltoaru_list $(LIBS)
base/bin/background: background.c base/lib/libnihc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)
base/bin/background: background.c base/lib/libnihc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so base/lib/libtoaru_drawstring.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_drawstring -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)
base/bin/drawlines: drawlines.c base/lib/libnihc.so base/lib/libtoaru_graphics.so base/lib/libtoaru_yutani.so base/lib/libtoaru_pthread.so
$(CC) $(CFLAGS) -o $@ $< -ltoaru_yutani -ltoaru_graphics -ltoaru_pex -ltoaru_pthread -ltoaru_hashmap -ltoaru_list $(LIBS)

View File

@ -1,32 +1,67 @@
#include <stdio.h>
#include <sys/utsname.h>
#include "lib/yutani.h"
#include "lib/graphics.h"
#include "lib/drawstring.h"
#define PANEL_HEIGHT 24
static yutani_t * yctx;
static yutani_window_t * wina;
static gfx_context_t * ctx;
static yutani_window_t * wallpaper_window;
static gfx_context_t * wallpaper_ctx;
static yutani_window_t * panel_window;
static gfx_context_t * panel_ctx;
static void draw_background(int width, int height) {
draw_fill(ctx, rgb(110,110,110));
draw_fill(wallpaper_ctx, rgb(110,110,110));
}
static void resize_finish(int width, int height) {
yutani_window_resize_accept(yctx, wina, width, height);
reinit_graphics_yutani(ctx, wina);
static void draw_panel(int width) {
char label[100];
struct utsname u;
uname(&u);
sprintf(label, "ToaruOS-NIH %s", u.release);
draw_fill(panel_ctx, rgb(20,20,20));
draw_string(panel_ctx, 1, 2, rgb(255,255,255), label);
}
static void resize_finish_wallpaper(int width, int height) {
yutani_window_resize_accept(yctx, wallpaper_window, width, height);
reinit_graphics_yutani(wallpaper_ctx, wallpaper_window);
draw_background(width, height);
yutani_window_resize_done(yctx, wina);
yutani_flip(yctx, wina);
yutani_window_resize_done(yctx, wallpaper_window);
yutani_flip(yctx, wallpaper_window);
}
static void resize_finish_panel(int width, int height) {
yutani_window_resize_accept(yctx, panel_window, width, height);
reinit_graphics_yutani(panel_ctx, panel_window);
draw_panel(width);
yutani_window_resize_done(yctx, panel_window);
yutani_flip(yctx, panel_window);
}
int main (int argc, char ** argv) {
yctx = yutani_init();
wina = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
yutani_window_move(yctx, wina, 0, 0);
yutani_set_stack(yctx, wina, YUTANI_ZORDER_BOTTOM);
ctx = init_graphics_yutani(wina);
/* wallpaper */
wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
yutani_window_move(yctx, wallpaper_window, 0, 0);
yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
wallpaper_ctx = init_graphics_yutani(wallpaper_window);
draw_background(yctx->display_width, yctx->display_height);
yutani_flip(yctx, wina);
yutani_flip(yctx, wallpaper_window);
/* panel */
panel_window = yutani_window_create(yctx, yctx->display_width, PANEL_HEIGHT);
yutani_window_move(yctx, panel_window, 0, 0);
yutani_set_stack(yctx, panel_window, YUTANI_ZORDER_TOP);
panel_ctx = init_graphics_yutani(panel_window);
draw_panel(yctx->display_width);
yutani_flip(yctx, panel_window);
int should_exit = 0;
@ -36,12 +71,17 @@ int main (int argc, char ** argv) {
switch (m->type) {
case YUTANI_MSG_WELCOME:
fprintf(stderr, "Request to resize desktop received, resizing to %d x %d\n", yctx->display_width, yctx->display_height);
yutani_window_resize_offer(yctx, wina, yctx->display_width, yctx->display_height);
yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
yutani_window_resize_offer(yctx, panel_window, yctx->display_width, PANEL_HEIGHT);
break;
case YUTANI_MSG_RESIZE_OFFER:
{
struct yutani_msg_window_resize * wr = (void*)m->data;
resize_finish(wr->width, wr->height);
if (wr->wid == wallpaper_window->wid) {
resize_finish_wallpaper(wr->width, wr->height);
} else if (wr->wid == panel_window->wid) {
resize_finish_panel(wr->width, wr->height);
}
}
break;
case YUTANI_MSG_SESSION_END:
@ -54,7 +94,7 @@ int main (int argc, char ** argv) {
free(m);
}
yutani_close(yctx, wina);
yutani_close(yctx, wallpaper_window);
return 0;
}

View File

@ -4,6 +4,7 @@
#include "lib/graphics.h"
//#include "lib/shmemfonts.h"
#include "lib/decorations.h"
#include "lib/drawstring.h"
#define INACTIVE 9
@ -24,7 +25,7 @@ static int lry_offset = 3;
static sprite_t * sprites[20];
#define TEXT_OFFSET 24
#define TEXT_OFFSET 10
static void init_sprite(int id, char * path) {
sprites[id] = malloc(sizeof(sprite_t));
@ -74,10 +75,6 @@ static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * c
}
draw_sprite(ctx, sprites[decors_active + 7], width - lr_width, height - l_height);
#if 0
set_font_face(FONT_SANS_SERIF_BOLD);
set_font_size(12);
char * tmp_title = strdup(title);
int t_l = strlen(tmp_title);
@ -100,7 +97,6 @@ static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * c
}
free(tmp_title);
#endif
/* Buttons */
draw_sprite(ctx, sprites[decors_active + 8], width - 28, 16);

223
lib/drawstring.c Normal file
View File

@ -0,0 +1,223 @@
#include "lib/utf8decode.h"
#include "lib/graphics.h"
#include "terminal-font.h"
static uint16_t char_width = 9; /* Width of a cell in pixels */
static uint16_t char_height = 20; /* Height of a cell in pixels */
static uint32_t ununicode(uint32_t c) {
switch (c) {
case L'': return 1;
case L'': return 2;
case L'': return 3;
case L'': return 4;
case L'': return 5;
case L'': return 6;
case L'': return 7;
case L'': return 8;
case L'': return 9;
case L'': return 10;
case L'': return 11;
case L'': return 12;
case L'': return 13;
case L'': return 14;
case L'': return 15;
case L'': return 16;
case L'': return 17;
case L'': return 18;
case L'': return 19;
case L'': return 20;
case L'§': return 21;
case L'': return 22;
case L'': return 23;
case L'': return 24;
case L'': return 25;
case L'': return 26;
case L'': return 27;
case L'': return 28;
case L'': return 29;
case L'': return 30;
case L'': return 31;
/* ASCII text */
case L'': return 127;
case L'Ç': return 128;
case L'ü': return 129;
case L'é': return 130;
case L'â': return 131;
case L'ä': return 132;
case L'à': return 133;
case L'å': return 134;
case L'ç': return 135;
case L'ê': return 136;
case L'ë': return 137;
case L'è': return 138;
case L'ï': return 139;
case L'î': return 140;
case L'ì': return 141;
case L'Ä': return 142;
case L'Å': return 143;
case L'É': return 144;
case L'æ': return 145;
case L'Æ': return 146;
case L'ô': return 147;
case L'ö': return 148;
case L'ò': return 149;
case L'û': return 150;
case L'ù': return 151;
case L'ÿ': return 152;
case L'Ö': return 153;
case L'Ü': return 154;
case L'¢': return 155;
case L'£': return 156;
case L'¥': return 157;
case L'': return 158;
case L'ƒ': return 159;
case L'á': return 160;
case L'í': return 161;
case L'ó': return 162;
case L'ú': return 163;
case L'ñ': return 164;
case L'Ñ': return 165;
case L'ª': return 166;
case L'º': return 167;
case L'¿': return 168;
case L'': return 169;
case L'¬': return 170;
case L'½': return 171;
case L'¼': return 172;
case L'¡': return 173;
case L'«': return 174;
case L'»': return 175;
case L'': return 176;
case L'': return 177;
case L'': return 178;
case L'': return 179;
case L'': return 180;
case L'': return 181;
case L'': return 182;
case L'': return 183;
case L'': return 184;
case L'': return 185;
case L'': return 186;
case L'': return 187;
case L'': return 188;
case L'': return 189;
case L'': return 190;
case L'': return 191;
case L'': return 192;
case L'': return 193;
case L'': return 194;
case L'': return 195;
case L'': return 196;
case L'': return 197;
case L'': return 198;
case L'': return 199;
case L'': return 200;
case L'': return 201;
case L'': return 202;
case L'': return 203;
case L'': return 204;
case L'': return 205;
case L'': return 206;
case L'': return 207;
case L'': return 208;
case L'': return 209;
case L'': return 210;
case L'': return 211;
case L'': return 212;
case L'': return 213;
case L'': return 214;
case L'': return 215;
case L'': return 216;
case L'': return 217;
case L'': return 218;
case L'': return 219;
case L'': return 220;
case L'': return 221;
case L'': return 222;
case L'': return 223;
case L'α': return 224;
case L'ß': return 225;
case L'Γ': return 226;
case L'π': return 227;
case L'Σ': return 228;
case L'σ': return 229;
case L'µ': return 230;
case L'τ': return 231;
case L'Φ': return 232;
case L'Θ': return 233;
case L'Ω': return 234;
case L'δ': return 235;
case L'': return 236;
case L'φ': return 237;
case L'ε': return 238;
case L'': return 239;
case L'': return 240;
case L'±': return 241;
case L'': return 242;
case L'': return 243;
case L'': return 244;
case L'': return 245;
case L'÷': return 246;
case L'': return 247;
case L'°': return 248;
case L'': return 249;
case L'·': return 250;
case L'': return 251;
case L'': return 252;
case L'²': return 253;
case L'': return 254;
}
return 4;
}
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t _fg, char * str) {
static uint32_t codepoint = 0;
static uint32_t unicode_state = 0;
while (*str) {
if (!decode(&unicode_state, &codepoint, (uint8_t)*str)) {
int val = codepoint;
if (val > 128) val = ununicode(val);
uint16_t * c = large_font[val];
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
if (c[i] & (1 << (15-j))) {
GFX(ctx, x+j, y+i) = _fg;
}
}
}
x += char_width;
unicode_state = 0;
codepoint = 0;
} else if (unicode_state == UTF8_REJECT) {
break;
unicode_state = 0;
codepoint = 0;
}
str++;
}
}
int draw_string_width(char * str) {
uint32_t codepoint = 0;
uint32_t unicode_state = 0;
int x = 0;
while (*str) {
if (!decode(&unicode_state, &codepoint, (uint8_t)*str)) {
x += char_width;
unicode_state = 0;
codepoint = 0;
} else if (unicode_state == UTF8_REJECT) {
break;
unicode_state = 0;
codepoint = 0;
}
str++;
}
return x;
}

5
lib/drawstring.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "lib/graphics.h"
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t _fg, char * str);
int draw_string_width(char * str);