Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Shared-memory font management and access library.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_CACHE_H
|
|
|
|
|
|
|
|
#include "graphics.h"
|
|
|
|
#include "shmemfonts.h"
|
|
|
|
|
|
|
|
static FT_Library library;
|
|
|
|
static FT_Face face; /* perhaps make this an array ? */
|
|
|
|
static FT_GlyphSlot slot;
|
|
|
|
static FT_UInt glyph_index;
|
2012-04-17 22:46:46 +04:00
|
|
|
static int initialized = 0;
|
2012-09-13 09:10:10 +04:00
|
|
|
static float opacity = 1.0;
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
|
|
|
|
#define SGFX(CTX,x,y,WIDTH) *((uint32_t *)&CTX[((WIDTH) * (y) + (x)) * 4])
|
|
|
|
#define FONT_SIZE 12
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: take font name as an argument / allow multiple fonts
|
|
|
|
*/
|
|
|
|
static void _loadSansSerif() {
|
|
|
|
char * font;
|
|
|
|
size_t s = 0;
|
|
|
|
int error;
|
2012-04-20 05:21:19 +04:00
|
|
|
font = (char *)syscall_shm_obtain(WINS_SERVER_IDENTIFIER ".fonts.sans-serif", &s);
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
error = FT_New_Memory_Face(library, font, s, 0, &face);
|
|
|
|
error = FT_Set_Pixel_Sizes(face, FONT_SIZE, FONT_SIZE);
|
|
|
|
}
|
|
|
|
|
2012-04-17 22:46:46 +04:00
|
|
|
void init_shmemfonts() {
|
|
|
|
if (!initialized) {
|
|
|
|
FT_Init_FreeType(&library);
|
|
|
|
_loadSansSerif();
|
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 09:10:10 +04:00
|
|
|
void set_font_size(int size) {
|
|
|
|
FT_Set_Pixel_Sizes(face, size, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_text_opacity(float new_opacity) {
|
|
|
|
opacity = new_opacity;
|
|
|
|
}
|
|
|
|
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
/*
|
|
|
|
* Draw a character to a context.
|
|
|
|
*/
|
|
|
|
static void draw_char(FT_Bitmap * bitmap, int x, int y, uint32_t fg, gfx_context_t * 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++) {
|
2012-09-13 09:10:10 +04:00
|
|
|
SGFX(ctx->backbuffer,i,j,ctx->width) = alpha_blend(SGFX(ctx->backbuffer,i,j,ctx->width),fg,rgb(bitmap->buffer[q * bitmap->width + p] * opacity,0,0));
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 22:55:54 +04:00
|
|
|
uint32_t draw_string_width(char * string) {
|
|
|
|
slot = face->glyph;
|
|
|
|
int pen_x = 0, 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;
|
|
|
|
pen_x += slot->advance.x >> 6;
|
|
|
|
}
|
|
|
|
return pen_x;
|
|
|
|
}
|
|
|
|
|
2012-04-17 22:46:46 +04:00
|
|
|
void draw_string(gfx_context_t * ctx, int x, int y, uint32_t fg, char * string) {
|
Context-based graphics library.
All graphics library commands now take a gfx_context_t pointer, which
points to a simple datastructure describing a rendering context (width,
height, depth, total size, front buffer, backbuffer; where backbuffer =
front buffer when not in double-buffering mode, thus we always render to
backbuffer except on a flip). This may have caused a minor speed
reduction, but I don't really care as it's far more important that we
support multiple graphics contexts.
TODO:
- Shared Memory Fonts library (there are a couple of apps that use these
so-called "shmem fonts" on their own; we need a dedicated library for
them)
- Break off "TTK" GUI toolkit into its own library. Since it's just a
callback-based button framework, this shouldn't be too hard right now.
Also, with the previous tick, I'll be able to put labels on controls
and start using text in more places.
2012-04-17 22:21:34 +04:00
|
|
|
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, ctx);
|
|
|
|
pen_x += slot->advance.x >> 6;
|
|
|
|
pen_y += slot->advance.y >> 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|