text: Switch several apps/libs to new text library

This commit is contained in:
K. Lange 2021-07-06 09:39:52 +09:00
parent 18f46976f8
commit f67b5425dd
9 changed files with 98 additions and 388 deletions

View File

@ -12,7 +12,6 @@
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/menu.h>
#include <toaru/text.h>
@ -39,19 +38,13 @@ static int center_x(int x) {
return (width - x) / 2;
}
static void draw_string(int y, const char * string, int font, uint32_t color) {
static void draw_string(int y, const char * string, struct TT_Font * font, uint32_t color) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
#if 0
draw_sdf_string(ctx, bounds.left_width + center_x(draw_sdf_string_width(string, 16, font)), bounds.top_height + 10 + logo.height + 10 + y, string, 16, color, font);
#else
struct TT_Font * _tt_font = (font == SDF_FONT_BOLD ? _tt_font_bold : _tt_font_thin);
tt_set_size(_tt_font, 13);
tt_draw_string(ctx, _tt_font, bounds.left_width + center_x(tt_string_width(_tt_font, string)), bounds.top_height + 10 + logo.height + 10 + y + 13, string, color);
#endif
tt_set_size(font, 13);
tt_draw_string(ctx, font, bounds.left_width + center_x(tt_string_width(font, string)), bounds.top_height + 10 + logo.height + 10 + y + 13, string, color);
}
static void redraw(void) {
@ -62,7 +55,7 @@ static void redraw(void) {
draw_fill(ctx, rgb(204,204,204));
draw_sprite(ctx, &logo, bounds.left_width + center_x(logo.width), bounds.top_height + 10);
draw_string(0, version_str, SDF_FONT_BOLD, rgb(0,0,0));
draw_string(0, version_str, _tt_font_bold, rgb(0,0,0));
int offset = 20;
@ -70,10 +63,10 @@ static void redraw(void) {
if (**copy_str == '-') {
offset += 10;
} else if (**copy_str == '%') {
draw_string(offset, *copy_str+1, SDF_FONT_THIN, rgb(0,0,255));
draw_string(offset, *copy_str+1, _tt_font_thin, rgb(0,0,255));
offset += 20;
} else {
draw_string(offset, *copy_str, SDF_FONT_THIN, rgb(0,0,0));
draw_string(offset, *copy_str, _tt_font_thin, rgb(0,0,0));
offset += 20;
}
}

View File

@ -33,9 +33,9 @@
#include <toaru/graphics.h>
#include <toaru/hashmap.h>
#include <toaru/spinlock.h>
#include <toaru/sdf.h>
#include <toaru/icon_cache.h>
#include <toaru/menu.h>
#include <toaru/text.h>
#include <kernel/mod/sound.h>
#define PANEL_HEIGHT 36
@ -101,6 +101,11 @@ static char * bg_blob;
static int width;
static int height;
static struct TT_Font * font = NULL;
static struct TT_Font * font_bold = NULL;
static struct TT_Font * font_mono = NULL;
static struct TT_Font * font_mono_bold = NULL;
static int widgets_width = 0;
static int widgets_volume_enabled = 0;
static int widgets_network_enabled = 0;
@ -800,8 +805,9 @@ static void redraw_altf2(void) {
draw_fill(a2ctx, 0);
draw_rounded_rectangle(a2ctx,0,0, ALTF2_WIDTH, ALTF2_HEIGHT, 10, ALTTAB_BACKGROUND);
int t = draw_sdf_string_width(altf2_buffer, 22, SDF_FONT_THIN);
draw_sdf_string(a2ctx, center_x_a2(t), 60, altf2_buffer, 22, rgb(255,255,255), SDF_FONT_THIN);
tt_set_size(font, 20);
int t = tt_string_width(font, altf2_buffer);
tt_draw_string(a2ctx, font, center_x_a2(t), 80, altf2_buffer, rgb(255,255,255));
flip(a2ctx);
yutani_flip(yctx, alt_f2);
@ -824,9 +830,9 @@ static void redraw_alttab(void) {
draw_sprite_scaled(actx, icon, center_x_a(48), ALTTAB_OFFSET, 48, 48);
}
int t = draw_sdf_string_width(ad->name, 18, SDF_FONT_THIN);
draw_sdf_string(actx, center_x_a(t), 12+ALTTAB_OFFSET+40, ad->name, 18, rgb(255,255,255), SDF_FONT_THIN);
tt_set_size(font, 16);
int t = tt_string_width(font, ad->name);
tt_draw_string(actx, font, center_x_a(t), 12 + ALTTAB_OFFSET + 40 + 16, ad->name, rgb(255,255,255));
}
flip(actx);
@ -1017,30 +1023,35 @@ static void redraw(void) {
/* Hours : Minutes : Seconds */
strftime(buffer, 80, "%H:%M:%S", timeinfo);
draw_sdf_string(ctx, width - TIME_LEFT, 3 + Y_PAD, buffer, 20, clockmenu->window ? HILIGHT_COLOR : txt_color, SDF_FONT_THIN);
tt_set_size(font, 18);
tt_draw_string(ctx, font, width - TIME_LEFT, 3 + Y_PAD + 17, buffer, clockmenu->window ? HILIGHT_COLOR : txt_color);
/* Day-of-week */
strftime(buffer, 80, "%A", timeinfo);
t = draw_sdf_string_width(buffer, 12, SDF_FONT_THIN);
tt_set_size(font, 11);
t = tt_string_width(font, buffer);
t = (DATE_WIDTH - t) / 2;
draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 2 + Y_PAD, buffer, 12, calmenu->window ? HILIGHT_COLOR : txt_color, SDF_FONT_THIN);
tt_draw_string(ctx, font, width - TIME_LEFT - DATE_WIDTH + t, 2 + Y_PAD + 11, buffer, calmenu->window ? HILIGHT_COLOR : txt_color);
/* Month Day */
strftime(buffer, 80, "%h %e", timeinfo);
t = draw_sdf_string_width(buffer, 12, SDF_FONT_BOLD);
tt_set_size(font_bold, 11);
t = tt_string_width(font_bold, buffer);
t = (DATE_WIDTH - t) / 2;
draw_sdf_string(ctx, width - TIME_LEFT - DATE_WIDTH + t, 12 + Y_PAD, buffer, 12, calmenu->window ? HILIGHT_COLOR : txt_color, SDF_FONT_BOLD);
tt_draw_string(ctx, font_bold, width - TIME_LEFT - DATE_WIDTH + t, 12 + Y_PAD + 11, buffer, calmenu->window ? HILIGHT_COLOR : txt_color);
/* Applications menu */
draw_sdf_string(ctx, 8, 3 + Y_PAD, "Applications", 20, appmenu->window ? HILIGHT_COLOR : txt_color, SDF_FONT_THIN);
tt_set_size(font, 18);
tt_draw_string(ctx, font, 12, 3 + Y_PAD + 17, "Applications", appmenu->window ? HILIGHT_COLOR : txt_color);
/* Draw each widget */
int widget = 0;
/* Weather */
if (widgets_weather_enabled) {
uint32_t color = (weather && weather->window) ? HILIGHT_COLOR : ICON_COLOR;
int t = draw_sdf_string_width(weather_temp_str, 15, SDF_FONT_THIN);
draw_sdf_string(ctx, WIDGET_POSITION(widget) + (WIDGET_WIDTH - t) / 2, 5 + Y_PAD, weather_temp_str, 15, color, SDF_FONT_THIN);
tt_set_size(font, 12);
int t = tt_string_width(font, weather_temp_str);
tt_draw_string(ctx, font, WIDGET_POSITION(widget) + (WIDGET_WIDTH - t) / 2, 5 + Y_PAD + 12, weather_temp_str, color);
draw_sprite_alpha_paint(ctx, weather_icon, WIDGET_POSITION(widget+1), ICON_Y_PAD, 1.0, color);
widget += 2;
}
@ -1095,7 +1106,8 @@ static void redraw(void) {
if (!ad->name[i]) break;
}
while (draw_sdf_string_width(tmp_title, 16, SDF_FONT_THIN) > title_width - ICON_PADDING) {
tt_set_size(font, 14);
while (tt_string_width(font, tmp_title) > title_width - ICON_PADDING) {
t_l--;
tmp_title[t_l] = '.';
tmp_title[t_l+1] = '.';
@ -1143,7 +1155,8 @@ static void redraw(void) {
gfx_context_t * _tmp = init_graphics_sprite(_tmp_s);
draw_fill(_tmp, rgba(0,0,0,0));
draw_sdf_string(_tmp, 0, 0, s, 16, rgb(0,0,0), SDF_FONT_THIN);
tt_set_size(font, 14);
tt_draw_string(_tmp, font, 0, 14, s, rgb(0,0,0));
blur_context_box(_tmp, 4);
free(_tmp);
@ -1154,18 +1167,18 @@ static void redraw(void) {
if (title_width > MIN_TEXT_WIDTH) {
/* Then draw the window title, with appropriate color */
uint32_t color;
if (j == focused_app) {
/* Current hilighted - title should be a light blue */
draw_sdf_string(ctx, APP_OFFSET + i + 2, TEXT_Y_OFFSET + 2, s, 16, HILIGHT_COLOR, SDF_FONT_THIN);
color = HILIGHT_COLOR;
} else {
if (ad->flags & 1) {
/* Top window should be white */
draw_sdf_string(ctx, APP_OFFSET + i + 2, TEXT_Y_OFFSET + 2, s, 16, FOCUS_COLOR, SDF_FONT_THIN);
color = FOCUS_COLOR;
} else {
/* Otherwise, off white */
draw_sdf_string(ctx, APP_OFFSET + i + 2, TEXT_Y_OFFSET + 2, s, 16, txt_color, SDF_FONT_THIN);
color = txt_color;
}
}
tt_draw_string(ctx, font, APP_OFFSET + i + 2, TEXT_Y_OFFSET + 2 + 14, s, color);
}
/* XXX This keeps track of how far left each window list item is
@ -1489,7 +1502,8 @@ void _menu_draw_MenuEntry_Calendar(gfx_context_t * ctx, struct MenuEntry * self,
/* Go through each and draw with monospace font */
for (int i = 0; i < 9; ++i) {
if (lines[i][0] != 0) {
draw_sdf_string(ctx, 10, self->offset + i * 17, lines[i], 16, rgb(0,0,0), i == 0 ? SDF_FONT_MONO_BOLD : SDF_FONT_MONO);
tt_set_size(i == 0 ? font_mono_bold : font_mono, 13);
tt_draw_string(ctx, i == 0 ? font_mono_bold : font_mono, 10, self->offset + i * 17 + 13, lines[i], rgb(0,0,0));
}
}
}
@ -1502,7 +1516,8 @@ struct MenuEntry * menu_create_calendar(void) {
out->_type = -1; /* Special */
out->height = 16 * 9 + 8;
out->rwidth = draw_sdf_string_width("XX XX XX XX XX XX XX", 16, SDF_FONT_MONO) + 20;
tt_set_size(font_mono, 13);
out->rwidth = tt_string_width(font_mono, "XX XX XX XX XX XX XX") + 20;
out->renderer = _menu_draw_MenuEntry_Calendar;
return out;
}
@ -1522,6 +1537,11 @@ int main (int argc, char ** argv) {
/* Connect to window server */
yctx = yutani_init();
font = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
font_bold = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf");
font_mono = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf");
font_mono_bold = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf");
/* For convenience, store the display size */
width = yctx->display_width;
height = yctx->display_height;

View File

@ -1,171 +0,0 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*
* sdf-demo - SDF font rasterizer demo
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/menu.h>
/* Pointer to graphics memory */
static yutani_t * yctx;
static yutani_window_t * window = NULL;
static gfx_context_t * ctx = NULL;
static int width = 500;
static int height = 500;
static int left = 200;
static int top = 200;
static int size = 16;
static void decors() {
render_decorations(window, ctx, "SDF Demo");
}
void redraw() {
draw_fill(ctx, rgb(255,255,255));
decors();
draw_sdf_string(ctx, 30,30*1, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_THIN);
draw_sdf_string(ctx, 30,30*2, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_THIN);
draw_sdf_string(ctx, 30,30*3, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD);
draw_sdf_string(ctx, 30,30*4, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD);
draw_sdf_string(ctx, 30,30*5, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_OBLIQUE);
draw_sdf_string(ctx, 30,30*6, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_OBLIQUE);
draw_sdf_string(ctx, 30,30*7, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD_OBLIQUE);
draw_sdf_string(ctx, 30,30*8, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD_OBLIQUE);
}
void resize_finish(int w, int h) {
yutani_window_resize_accept(yctx, window, w, h);
reinit_graphics_yutani(ctx, window);
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
width = w - bounds.left_width - bounds.right_width;
height = h - bounds.top_height - bounds.bottom_height;
redraw();
yutani_window_resize_done(yctx, window);
yutani_flip(yctx, window);
}
int main(int argc, char * argv[]) {
yctx = yutani_init();
if (!yctx) {
fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
return 1;
}
init_decorations();
struct decor_bounds bounds;
decor_get_bounds(NULL, &bounds);
window = yutani_window_create(yctx, width + bounds.width, height + bounds.height);
yutani_window_move(yctx, window, left, top);
yutani_window_advertise_icon(yctx, window, "SDF Demo", "sdf");
ctx = init_graphics_yutani(window);
redraw();
yutani_flip(yctx, window);
int playing = 1;
while (playing) {
yutani_msg_t * m = yutani_poll(yctx);
if (m) {
if (menu_process_event(yctx, m)) {
redraw();
continue;
}
switch (m->type) {
case YUTANI_MSG_KEY_EVENT:
{
struct yutani_msg_key_event * ke = (void*)m->data;
if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
playing = 0;
} else if (ke->event.action == KEY_ACTION_DOWN) {
if (size <= 20) {
size += 1;
} else if (size > 20) {
size += 5;
}
if (size > 100) {
size = 1;
}
redraw();
yutani_flip(yctx,window);
}
}
break;
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
{
struct yutani_msg_window_focus_change * wf = (void*)m->data;
yutani_window_t * win = hashmap_get(yctx->windows, (void*)(uintptr_t)wf->wid);
if (win) {
win->focused = wf->focused;
decors();
yutani_flip(yctx, window);
}
}
break;
case YUTANI_MSG_RESIZE_OFFER:
{
struct yutani_msg_window_resize * wr = (void*)m->data;
resize_finish(wr->width, wr->height);
}
break;
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
{
struct yutani_msg_window_mouse_event * me = (void*)m->data;
if (me->wid != window->wid) break;
int result = decor_handle_event(yctx, m);
switch (result) {
case DECOR_CLOSE:
playing = 0;
break;
case DECOR_RIGHT:
/* right click in decoration, show appropriate menu */
decor_show_default_menu(window, window->x + me->new_x, window->y + me->new_y);
break;
default:
/* Other actions */
break;
}
}
break;
case YUTANI_MSG_WINDOW_CLOSE:
case YUTANI_MSG_SESSION_END:
playing = 0;
break;
default:
break;
}
}
free(m);
}
yutani_close(yctx, window);
return 0;
}

View File

@ -8,9 +8,9 @@
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/menu.h>
#include <toaru/button.h>
#include <toaru/text.h>
#include <sys/utsname.h>
@ -30,12 +30,15 @@ static char * icon_path;
static char * title_str;
static char * copyright_str[20] = {NULL};
static void draw_string(int y, const char * string, int font, uint32_t color) {
static struct TT_Font * _tt_font = NULL;
static void draw_string(int y, const char * string, uint32_t color) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
draw_sdf_string(ctx, bounds.left_width + 80, bounds.top_height + 30 + y, string, 16, color, font);
tt_set_size(_tt_font, 13);
tt_draw_string(ctx, _tt_font, bounds.left_width + 80, bounds.top_height + 30 + y + 13, string, color);
}
struct TTKButton _ok = {0};
@ -54,10 +57,10 @@ static void redraw(void) {
if (**copy_str == '-') {
offset += 10;
} else if (**copy_str == '%') {
draw_string(offset, *copy_str+1, SDF_FONT_THIN, rgb(0,0,255));
draw_string(offset, *copy_str+1, rgb(0,0,255));
offset += 20;
} else {
draw_string(offset, *copy_str, SDF_FONT_THIN, rgb(0,0,0));
draw_string(offset, *copy_str, rgb(0,0,0));
offset += 20;
}
}
@ -142,6 +145,8 @@ int main(int argc, char * argv[]) {
struct decor_bounds bounds;
decor_get_bounds(NULL, &bounds);
_tt_font = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
window = yutani_window_create_flags(yctx, width + bounds.width, height + bounds.height, YUTANI_WINDOW_FLAG_DIALOG_ANIMATION);
req_center_x = yctx->display_width / 2;
req_center_y = yctx->display_height / 2;

View File

@ -69,7 +69,6 @@ static void usage(char * argv[]) {
" -x --grid \033[3mMake resizes round to nearest match for character cell size.\033[0m\n"
" -n --no-frame \033[3mDisable decorations.\033[0m\n"
" -g --geometry \033[3mSet requested terminal size WIDTHxHEIGHT\033[0m\n"
" -f --no-ft \033[3mForce disable the freetype backend.\033[0m\n"
"\n"
" This terminal emulator provides basic support for VT220 escapes and\n"
" XTerm extensions, including 256 color support and font effects.\n",
@ -83,7 +82,6 @@ static pid_t child_pid = 0;
static int scale_fonts = 0; /* Whether fonts should be scaled */
static float font_scaling = 1.0; /* How much they should be scaled by */
static float font_gamma = 1.7; /* Gamma to use for SDF library */
static uint16_t term_width = 0; /* Width of the terminal (in cells) */
static uint16_t term_height = 0; /* Height of the terminal (in cells) */
static uint16_t font_size = 16; /* Font size according to SDF library */
@ -109,8 +107,6 @@ static bool cursor_on = 1; /* Whether or not the cursor should be render
static bool _fullscreen = 0; /* Whether or not we are running in fullscreen mode (GUI only) */
static bool _no_frame = 0; /* Whether to disable decorations or not */
static bool _use_aa = 1; /* Whether or not to use best-available anti-aliased renderer */
static bool _have_freetype = 0; /* Whether freetype is available */
static bool _force_no_ft = 0; /* Whether to force disable the freetype backend */
static bool _free_size = 1; /* Disable rounding when resized */
static struct TT_Font * _tt_font_normal = NULL;
@ -118,11 +114,6 @@ static struct TT_Font * _tt_font_bold = NULL;
static struct TT_Font * _tt_font_oblique = NULL;
static struct TT_Font * _tt_font_bold_oblique = NULL;
/** Freetype extension renderer functions */
static void (*freetype_set_font_face)(int face) = NULL;
static void (*freetype_set_font_size)(int size) = NULL;
static void (*freetype_draw_char)(gfx_context_t * ctx, int x, int y, uint32_t fg, uint32_t codepoint) = NULL;
static list_t * images_list = NULL;
static int menu_bar_height = 24;
@ -689,7 +680,7 @@ static void term_write_char(uint32_t val, uint16_t x, uint16_t y, uint32_t fg, u
}
/* Draw glyphs */
if (_use_aa && !_have_freetype) {
if (_use_aa) {
if (val == 0xFFFF) return;
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
@ -715,7 +706,7 @@ static void term_write_char(uint32_t val, uint16_t x, uint16_t y, uint32_t fg, u
_font = _tt_font_oblique;
}
unsigned int glyph = tt_glyph_for_codepoint(_font, val);
tt_set_size(_font, char_offset);
tt_set_size(_font, font_size);
int _x = x;
int _y = y + char_offset;
if (!_no_frame) {
@ -723,44 +714,6 @@ static void term_write_char(uint32_t val, uint16_t x, uint16_t y, uint32_t fg, u
_y += decor_top_height + menu_bar_height;
}
tt_draw_glyph(ctx, _font, _x, _y, glyph, _fg);
} else if (_use_aa && _have_freetype) {
/* Draw using freetype extension */
if (val == 0xFFFF) { return; } /* Unicode, do not redraw here */
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = 0; j < char_width; ++j) {
term_set_point(x+j,y+i,_bg);
}
}
if (flags & ANSI_WIDE) {
for (uint8_t i = 0; i < char_height; ++i) {
for (uint8_t j = char_width; j < 2 * char_width; ++j) {
term_set_point(x+j,y+i,_bg);
}
}
}
if (val < 32 || val == ' ') {
goto _extra_stuff;
}
#define FONT_MONOSPACE 4
#define FONT_MONOSPACE_BOLD 5
#define FONT_MONOSPACE_ITALIC 6
#define FONT_MONOSPACE_BOLD_ITALIC 7
int _font = FONT_MONOSPACE;
if (flags & ANSI_BOLD && flags & ANSI_ITALIC) {
_font = FONT_MONOSPACE_BOLD_ITALIC;
} else if (flags & ANSI_ITALIC) {
_font = FONT_MONOSPACE_ITALIC;
} else if (flags & ANSI_BOLD) {
_font = FONT_MONOSPACE_BOLD;
}
freetype_set_font_face(_font);
freetype_set_font_size(font_size);
if (_no_frame) {
freetype_draw_char(ctx, x, y + char_offset, _fg, val);
} else {
freetype_draw_char(ctx, x + decor_left_width, y + char_offset + decor_top_height + menu_bar_height, _fg, val);
}
} else {
/* Convert other unicode characters. */
if (val > 128) {
@ -1791,10 +1744,10 @@ static term_cell_t * copy_terminal(int old_width, int old_height, term_cell_t *
static void reinit(void) {
/* Figure out character sizes if fonts have changed. */
if (_use_aa && !_have_freetype) {
if (_use_aa) {
char_width = 9;
char_height = 17;
font_size = 16;
font_size = 13;
char_offset = 13;
if (scale_fonts) {
font_size *= font_scaling;
@ -1802,19 +1755,6 @@ static void reinit(void) {
char_width *= font_scaling;
char_offset *= font_scaling;
}
} else if (_use_aa && _have_freetype) {
font_size = 13;
char_height = 17;
char_width = 8;
char_offset = 13;
if (scale_fonts) {
/* Recalculate scaling */
font_size *= font_scaling;
char_height *= font_scaling;
char_width *= font_scaling;
char_offset *= font_scaling;
}
} else {
char_width = 9;
char_height = 20;
@ -2271,9 +2211,6 @@ int main(int argc, char ** argv) {
case 'n':
_no_frame = 1;
break;
case 'f':
_force_no_ft = 1;
break;
case 'F':
_fullscreen = 1;
_no_frame = 1;
@ -2307,16 +2244,6 @@ int main(int argc, char ** argv) {
}
}
if (!_force_no_ft) {
void * freetype = dlopen("libtoaru_ext_freetype_fonts.so", 0);
if (freetype) {
_have_freetype = 1;
freetype_set_font_face = dlsym(freetype, "freetype_set_font_face");
freetype_set_font_size = dlsym(freetype, "freetype_set_font_size");
freetype_draw_char = dlsym(freetype, "freetype_draw_char");
}
}
_tt_font_normal = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf");
_tt_font_bold = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf");
_tt_font_oblique = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf");

View File

@ -11,9 +11,9 @@
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/menu.h>
#include <toaru/button.h>
#include <toaru/text.h>
#include <sys/utsname.h>
@ -42,20 +42,22 @@ static sprite_t package;
static sprite_t logo;
static sprite_t mouse_drag;
static struct TT_Font * _tt_font_thin = NULL;
static struct TT_Font * _tt_font_bold = NULL;
static int page = 0;
static int center(int x, int width) {
return (width - x) / 2;
}
static void draw_string(int y, const char * string, int font, uint32_t color, int size) {
static void draw_string(int y, const char * string, struct TT_Font * font, uint32_t color, int size) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
int text_width = draw_sdf_string_width(string, size, font);
draw_sdf_string(ctx, center(text_width, width), bounds.top_height + 30 + y, string, size, color, font);
tt_set_size(font, size);
tt_draw_string(ctx, font, bounds.left_width + center(tt_string_width(font, string), width), bounds.top_height + 30 + y + size, string, color);
}
struct TTKButton _next_button = {0};
@ -80,13 +82,13 @@ static void redraw(void) {
if (**copy_str == '-') {
offset += 10;
} else if (**copy_str == '%') {
draw_string(offset, *copy_str+1, SDF_FONT_THIN, rgb(0,0,255), 16);
draw_string(offset, *copy_str+1, _tt_font_thin, rgb(0,0,255), 13);
offset += 20;
} else if (**copy_str == '#') {
draw_string(offset, *copy_str+1, SDF_FONT_BOLD, rgb(0,0,0), 23);
draw_string(offset, *copy_str+1, _tt_font_bold, rgb(0,0,0), 20);
offset += 20;
} else {
draw_string(offset, *copy_str, SDF_FONT_THIN, rgb(0,0,0), 16);
draw_string(offset, *copy_str, _tt_font_thin, rgb(0,0,0), 13);
offset += 20;
}
}
@ -311,6 +313,9 @@ int main(int argc, char * argv[]) {
}
init_decorations();
_tt_font_thin = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
_tt_font_bold = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf");
background = yutani_window_create_flags(yctx, yctx->display_width, yctx->display_height,
YUTANI_WINDOW_FLAG_DISALLOW_RESIZE | YUTANI_WINDOW_FLAG_DISALLOW_DRAG |
YUTANI_WINDOW_FLAG_ALT_ANIMATION | YUTANI_WINDOW_FLAG_NO_STEAL_FOCUS);

View File

@ -7,9 +7,11 @@
*/
#include <toaru/graphics.h>
#include <toaru/button.h>
#include <toaru/sdf.h>
#include <toaru/text.h>
#include <toaru/icon_cache.h>
static struct TT_Font * _tt_font_thin = NULL;
void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
if (button->width == 0) {
return;
@ -41,11 +43,15 @@ void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
}
if (button->title[0] != '\033') {
int label_width = draw_sdf_string_width(button->title, 16, SDF_FONT_THIN);
if (!_tt_font_thin) {
_tt_font_thin = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
}
tt_set_size(_tt_font_thin, 13);
int label_width = tt_string_width(_tt_font_thin, button->title);
int centered = (button->width - label_width) / 2;
int centered_y = (button->height - 16) / 2;
draw_sdf_string(ctx, button->x + centered + (hilight == 2), button->y + centered_y + (hilight == 2), button->title, 16, disabled ? rgb(120,120,120) : rgb(0,0,0), SDF_FONT_THIN);
tt_draw_string(ctx, _tt_font_thin, button->x + centered + (hilight == 2), button->y + centered_y + (hilight == 2) + 13, button->title, disabled ? rgb(120,120,120) : rgb(0,0,0));
} else {
sprite_t * icon = icon_get_16(button->title+1);
int centered = button->x + (button->width - icon->width) / 2 + (hilight == 2);

View File

@ -14,7 +14,6 @@
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/text.h>
#define INACTIVE 10
@ -35,12 +34,6 @@ static sprite_t * sprites[20];
#define TEXT_OFFSET ((window->decorator_flags & DECOR_FLAG_TILED) ? 5 : 10)
#define BUTTON_OFFSET ((window->decorator_flags & DECOR_FLAG_TILED) ? 5 : 0)
static int _have_freetype = 0;
static void (*freetype_set_font_face)(int face) = NULL;
static void (*freetype_set_font_size)(int size) = NULL;
static int (*freetype_draw_string)(gfx_context_t * ctx, int x, int y, uint32_t fg, const char * s) = NULL;
static int (*freetype_draw_string_width)(char * s) = NULL;
static void init_sprite(int id, char * path) {
sprites[id] = malloc(sizeof(sprite_t));
load_sprite(sprites[id], path);
@ -148,46 +141,17 @@ static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * c
#define EXTRA_SPACE 120
uint32_t title_color = (decors_active == 0) ? rgb(226,226,226) : rgb(147,147,147);
if (_have_freetype) {
freetype_set_font_face(1); /* regular non-monospace */
freetype_set_font_size(12);
if (freetype_draw_string_width(tmp_title) + EXTRA_SPACE > width) {
while (t_l >= 0 && (freetype_draw_string_width(tmp_title) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0';
t_l--;
}
}
if (*tmp_title) {
int title_offset = (width / 2) - (freetype_draw_string_width(tmp_title) / 2);
freetype_draw_string(ctx, title_offset, TEXT_OFFSET + 14, title_color, tmp_title);
}
} else {
tt_set_size(_tt_font, 12);
if (tt_string_width(_tt_font, tmp_title) + EXTRA_SPACE > width) {
while (t_l >= 0 && (tt_string_width(_tt_font, tmp_title) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0';
t_l--;
}
tt_set_size(_tt_font, 12);
if (tt_string_width(_tt_font, tmp_title) + EXTRA_SPACE > width) {
while (t_l >= 0 && (tt_string_width(_tt_font, tmp_title) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0';
t_l--;
}
}
if (*tmp_title) {
int title_offset = (width / 2) - (tt_string_width(_tt_font, tmp_title) / 2);
tt_draw_string(ctx, _tt_font, title_offset, TEXT_OFFSET + 14, tmp_title, title_color);
}
#if 0
#define TEXT_SIZE 15
if (draw_sdf_string_width(tmp_title, TEXT_SIZE, SDF_FONT_BOLD) + EXTRA_SPACE > width) {
while (t_l >= 0 && (draw_sdf_string_width(tmp_title, TEXT_SIZE, SDF_FONT_BOLD) + EXTRA_SPACE > width)) {
tmp_title[t_l] = '\0';
t_l--;
}
}
if (*tmp_title) {
int title_offset = (width / 2) - (draw_sdf_string_width(tmp_title, TEXT_SIZE, SDF_FONT_BOLD) / 2);
draw_sdf_string(ctx, title_offset, TEXT_OFFSET+2, tmp_title, TEXT_SIZE, title_color, SDF_FONT_BOLD);
}
#endif
if (*tmp_title) {
int title_offset = (width / 2) - (tt_string_width(_tt_font, tmp_title) / 2);
tt_draw_string(ctx, _tt_font, title_offset, TEXT_OFFSET + 14, tmp_title, title_color);
}
free(tmp_title);
@ -243,14 +207,5 @@ void decor_init() {
decor_get_bounds = get_bounds_fancy;
_tt_font = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf");
void * freetype = dlopen("libtoaru_ext_freetype_fonts.so", 0);
if (freetype) {
_have_freetype = 1;
freetype_set_font_face = dlsym(freetype, "freetype_set_font_face");
freetype_set_font_size = dlsym(freetype, "freetype_set_font_size");
freetype_draw_string = dlsym(freetype, "freetype_draw_string");
freetype_draw_string_width = dlsym(freetype, "freetype_draw_string_width");
}
}

View File

@ -17,7 +17,6 @@
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/sdf.h>
#include <toaru/hashmap.h>
#include <toaru/list.h>
#include <toaru/icon_cache.h>
@ -43,25 +42,10 @@ static struct MenuList * hovered_menu = NULL;
int menu_definitely_close(struct MenuList * menu);
/** Freetype extension renderer functions */
static int _have_freetype = 0;
static void (*freetype_set_font_face)(int face) = NULL;
static void (*freetype_set_font_size)(int size) = NULL;
static int (*freetype_draw_string)(gfx_context_t * ctx, int x, int y, uint32_t fg, const char * s) = NULL;
static int (*freetype_draw_string_width)(char * s) = NULL;
__attribute__((constructor))
static void _init_menus(void) {
menu_windows = hashmap_create_int(10);
_tt_font = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
void * freetype = dlopen("libtoaru_ext_freetype_fonts.so", 0);
if (freetype) {
_have_freetype = 1;
freetype_set_font_face = dlsym(freetype, "freetype_set_font_face");
freetype_set_font_size = dlsym(freetype, "freetype_set_font_size");
freetype_draw_string = dlsym(freetype, "freetype_draw_string");
freetype_draw_string_width = dlsym(freetype, "freetype_draw_string_width");
}
}
hashmap_t * menu_get_windows_hash(void) {
@ -69,27 +53,13 @@ hashmap_t * menu_get_windows_hash(void) {
}
static int string_width(const char * s) {
if (_have_freetype) {
freetype_set_font_face(0); /* regular non-monospace */
freetype_set_font_size(13);
return freetype_draw_string_width((char *)s);
} else {
tt_set_size(_tt_font, 13);
return tt_string_width(_tt_font, s);
//return draw_sdf_string_width((char *)s, 16, SDF_FONT_THIN);
}
tt_set_size(_tt_font, 13);
return tt_string_width(_tt_font, s);
}
static int draw_string(gfx_context_t * ctx, int x, int y, uint32_t color, const char * s) {
if (_have_freetype) {
freetype_set_font_face(0); /* regular non-monospace */
freetype_set_font_size(13);
return freetype_draw_string(ctx, x+2, y + 13 /* I think? */, color, s);
} else {
tt_set_size(_tt_font, 13);
return tt_draw_string(ctx, _tt_font, x, y + 13, s, color);
//return draw_sdf_string(ctx, x, y, s, 16, color, SDF_FONT_THIN);
}
tt_set_size(_tt_font, 13);
return tt_draw_string(ctx, _tt_font, x, y + 13, s, color);
}
void _menu_draw_MenuEntry_Normal(gfx_context_t * ctx, struct MenuEntry * self, int offset) {