Shared memory fonts library

This commit is contained in:
Kevin Lange 2012-04-17 13:46:46 -05:00
parent 4f2c122af5
commit efcae21316
7 changed files with 42 additions and 77 deletions

View File

@ -31,9 +31,9 @@ clean:
@-rm -f ${EXECUTABLES}
@${ENDRM} "RM" "Cleaned userspace full-toolchain applications."
${FTARGETS}: $(TARGETDIR)% : %.c lib/graphics.o lib/list.o lib/window.o lib/sha2.o lib/decorations.o lib/pthread.o
${FTARGETS}: $(TARGETDIR)% : %.c lib/graphics.o lib/list.o lib/window.o lib/sha2.o lib/decorations.o lib/pthread.o lib/shmemfonts.o
@${BEG} "CC" "$@ $< [w/libs]"
@${CC} ${CFLAGS} ${EXTRAFLAGS} ${FREETYPE_INC} -o $@ $< lib/graphics.o lib/list.o lib/window.o lib/sha2.o lib/decorations.o lib/pthread.o ${LIBM} ${FREETYPE_LIB} ${ERRORS}
@${CC} ${CFLAGS} ${EXTRAFLAGS} ${FREETYPE_INC} -o $@ $< lib/graphics.o lib/list.o lib/window.o lib/sha2.o lib/decorations.o lib/pthread.o lib/shmemfonts.o ${LIBM} ${FREETYPE_LIB} ${ERRORS}
@${END} "CC" "$< [w/libs]"
$(TARGETDIR)%: %.cpp
@ -51,11 +51,6 @@ $(TARGETDIR)%: %.c
@${CC} ${CFLAGS} ${EXTRAFLAGS} -o $@ $< ${ERRORS}
@${END} "CC" "$<"
lib/decorations.o: lib/decorations.c
@${BEG} "CC" "$< [lib]"
@${CC} ${CFLAGS} -c ${EXTRAFLAGS} ${FREETYPE_INC} -o $@ $< ${ERRORS}
@${END} "CC" "$< [lib]"
lib/shmemfonts.o: lib/shmemfonts.c
@${BEG} "CC" "$< [lib]"
@${CC} ${CFLAGS} -c ${EXTRAFLAGS} ${FREETYPE_INC} -o $@ $< ${ERRORS}

View File

@ -3,6 +3,7 @@
#include "lib/window.h"
#include "lib/graphics.h"
#include "lib/list.h"
#include "lib/shmemfonts.h"
/* XXX TOOLKIT FUNCTIONS */
@ -34,6 +35,7 @@ typedef struct {
ttk_object _super; /* Parent type (Object -> Button) */
char * title; /* Button text */
uint32_t fill_color; /* Fill color */
uint32_t fore_color; /* Text color */
} ttk_button;
void ttk_render_button(void * s) {
@ -48,6 +50,8 @@ void ttk_render_button(void * s) {
draw_line(ctx, self->x, self->x, self->y, self->y + self->height, border_color);
draw_line(ctx, self->x + self->width, self->x + self->width, self->y, self->y + self->height, border_color);
draw_line(ctx, self->x, self->x + self->width, self->y + self->height, self->y + self->height, border_color);
/* button-specific stuff */
draw_string(ctx, self->x + 10, self->y + self->height - 3, ((ttk_button *)self)->fore_color, ((ttk_button * )self)->title);
}
ttk_button * ttk_new_button(char * title, void (*callback)(void *, w_mouse_t *)) {
@ -113,6 +117,7 @@ void setup_ttk(window_t * window) {
ttk_window = window;
ttk_objects = list_create();
mouse_action_callback = ttk_check_click;
init_shmemfonts();
}
uint32_t drawing_color = 0;
@ -149,17 +154,22 @@ int main (int argc, char ** argv) {
ttk_button * button_blue = ttk_new_button("Blue", set_color);
ttk_position((ttk_object *)button_blue, 3, 3, 100, 20);
button_blue->fill_color = rgb(0,0,255);
button_blue->fore_color = rgb(255,255,255);
ttk_button * button_green = ttk_new_button("Grn", set_color);
ttk_button * button_green = ttk_new_button("Green", set_color);
ttk_position((ttk_object *)button_green, 106, 3, 100, 20);
button_green->fill_color = rgb(0,255,0);
button_green->fore_color = rgb(0,0,0);
ttk_button * button_red = ttk_new_button("Red", set_color);
ttk_position((ttk_object *)button_red, 209, 3, 100, 20);
button_red->fill_color = rgb(255,0,0);
button_red->fore_color = rgb(255,255,255);
ttk_button * button_quit = ttk_new_button("Quit", quit_app);
ttk_button * button_quit = ttk_new_button("X", quit_app);
ttk_position((ttk_object *)button_quit, width - 23, 3, 20, 20);
button_quit->fill_color = rgb(255,0,0);
button_quit->fore_color = rgb(255,255,255);
drawing_color = rgb(255,0,0);

View File

@ -62,7 +62,7 @@ void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg) {
}
}
void draw_string(int x, int y, uint32_t fg, char * string) {
static void draw_string(int x, int y, uint32_t fg, char * string) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;
int len = strlen(string);

View File

@ -7,6 +7,7 @@
#include "graphics.h"
#include "window.h"
#include "decorations.h"
#include "shmemfonts.h"
uint32_t decor_top_height = 24;
uint32_t decor_bottom_height = 1;
@ -20,70 +21,9 @@ uint32_t decor_right_width = 1;
#define BACKCOLOR rgb(20,20,20)
#define TEXTCOLOR rgb(255,255,255)
#define SGFX(CTX,x,y,WIDTH) *((uint32_t *)&CTX[((WIDTH) * (y) + (x)) * 4])
#define FONT_SIZE 12
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_CACHE_H
static FT_Library library;
static FT_Face face;
static FT_GlyphSlot slot;
static FT_UInt glyph_index;
static void _loadDejavu() {
char * font;
size_t s = 0;
int error;
font = (char *)syscall_shm_obtain(WINS_SERVER_IDENTIFIER ".fonts.sans-serif.bold", &s);
error = FT_New_Memory_Face(library, font, s, 0, &face);
error = FT_Set_Pixel_Sizes(face, FONT_SIZE, FONT_SIZE);
}
static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, window_t * window, char * ctx) {
int i, j, p, q;
int x_max = x + bitmap->width;
int y_max = y + bitmap->rows;
for (j = y, q = 0; j < y_max; j++, q++) {
for ( i = x, p = 0; i < x_max; i++, p++) {
SGFX(ctx,i,j,window->width) = alpha_blend(SGFX(ctx,i,j,window->width),fg,rgb(bitmap->buffer[q * bitmap->width + p],0,0));
}
}
}
static void draw_string(int x, int y, uint32_t fg, char * string, window_t * window, char * ctx) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;
int len = strlen(string);
int error;
for (i = 0; i < len; ++i) {
FT_UInt glyph_index;
glyph_index = FT_Get_Char_Index( face, string[i]);
error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
if (error) {
printf("Error loading glyph for '%c'\n", string[i]);
continue;
}
slot = (face)->glyph;
if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
error = FT_Render_Glyph((face)->glyph, FT_RENDER_MODE_NORMAL);
if (error) {
printf("Error rendering glyph for '%c'\n", string[i]);
continue;
}
}
draw_char(&slot->bitmap, pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, fg, window, ctx);
pen_x += slot->advance.x >> 6;
pen_y += slot->advance.y >> 6;
}
}
void init_decorations() {
FT_Init_FreeType(&library);
_loadDejavu();
init_shmemfonts();
}
void render_decorations(window_t * window, uint8_t * ctx, char * title) {
@ -96,7 +36,15 @@ void render_decorations(window_t * window, uint8_t * ctx, char * title) {
SGFX(ctx,j,i,window->width) = BACKCOLOR;
}
}
draw_string(TEXT_OFFSET_X,TEXT_OFFSET_Y,TEXTCOLOR,title,window,ctx);
/* Fake context for decorations */
gfx_context_t fake_context;
fake_context.width = window->width;
fake_context.height = window->height;
fake_context.depth = 32;
fake_context.backbuffer = ctx;
draw_string(&fake_context, TEXT_OFFSET_X,TEXT_OFFSET_Y,TEXTCOLOR,title);
for (uint32_t i = 0; i < window->width; ++i) {
SGFX(ctx,i,0,window->width) = BORDERCOLOR;
SGFX(ctx,i,decor_top_height-1,window->width) = BORDERCOLOR;

View File

@ -17,6 +17,7 @@ static FT_Library library;
static FT_Face face; /* perhaps make this an array ? */
static FT_GlyphSlot slot;
static FT_UInt glyph_index;
static int initialized = 0;
#define SGFX(CTX,x,y,WIDTH) *((uint32_t *)&CTX[((WIDTH) * (y) + (x)) * 4])
#define FONT_SIZE 12
@ -33,6 +34,14 @@ static void _loadSansSerif() {
error = FT_Set_Pixel_Sizes(face, FONT_SIZE, FONT_SIZE);
}
void init_shmemfonts() {
if (!initialized) {
FT_Init_FreeType(&library);
_loadSansSerif();
initialized = 1;
}
}
/*
* Draw a character to a context.
*/
@ -42,12 +51,12 @@ static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context
int y_max = y + bitmap->rows;
for (j = y, q = 0; j < y_max; j++, q++) {
for ( i = x, p = 0; i < x_max; i++, p++) {
SGFX(ctx->buffer,i,j,ctx->width) = alpha_blend(SGFX(ctx->buffer,i,j,ctx->width),fg,rgb(bitmap->buffer[q * bitmap->width + p],0,0));
SGFX(ctx->backbuffer,i,j,ctx->width) = alpha_blend(SGFX(ctx->backbuffer,i,j,ctx->width),fg,rgb(bitmap->buffer[q * bitmap->width + p],0,0));
}
}
}
static void draw_string(int x, int y, uint32_t fg, char * string, gfx_context_t * ctx) {
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;
int len = strlen(string);

View File

@ -4,4 +4,7 @@
#include "graphics.h"
#include "window.h"
void init_shmemfonts();
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string);
#endif

View File

@ -65,7 +65,7 @@ void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg) {
}
}
void draw_string(int x, int y, uint32_t fg, char * string) {
static void draw_string(int x, int y, uint32_t fg, char * string) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;
int len = strlen(string);
@ -103,7 +103,7 @@ int wstrlen(uint16_t * s) {
return i;
}
void draw_string_wide(int x, int y, uint32_t fg, uint16_t * string) {
static void draw_string_wide(int x, int y, uint32_t fg, uint16_t * string) {
slot = face->glyph;
int pen_x = x, pen_y = y, i = 0;
int len = wstrlen(string);