mirror of
https://github.com/nishiowo/nishbox
synced 2025-04-21 20:24:39 +00:00
font
This commit is contained in:
parent
fe12b9de3a
commit
e11186627e
@ -39,6 +39,7 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) {
|
||||
draw->height = 480;
|
||||
draw->running = 0;
|
||||
draw->draw_3d = 0;
|
||||
draw->font = NULL;
|
||||
strcpy(draw->title, title);
|
||||
draw->platform = gf_draw_platform_create(engine, draw);
|
||||
if(draw->platform != NULL) {
|
||||
@ -61,6 +62,8 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) {
|
||||
draw->lookat[1] = 0;
|
||||
draw->lookat[2] = 0;
|
||||
|
||||
draw->font = gf_font_create("font/helvR24.bdf");
|
||||
|
||||
draw->gui = gf_gui_create(engine, draw);
|
||||
if(1) {
|
||||
int w, h, c;
|
||||
@ -68,29 +71,6 @@ gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) {
|
||||
test_texture = gf_texture_create(draw, w, h, d);
|
||||
free(d);
|
||||
}
|
||||
|
||||
regcount = 0;
|
||||
for(i = 0; i < 128; i++) {
|
||||
unsigned char* font = malloc(8 * 8 * 4);
|
||||
int j;
|
||||
/* TODO: check font here */
|
||||
draw->font[i] = 0;
|
||||
if(1) {
|
||||
free(font);
|
||||
continue;
|
||||
}
|
||||
regcount++;
|
||||
for(j = 0; j < 8 * 8; j++) {
|
||||
unsigned char val = 255;
|
||||
font[j * 4 + 0] = val;
|
||||
font[j * 4 + 1] = val;
|
||||
font[j * 4 + 2] = val;
|
||||
font[j * 4 + 3] = val;
|
||||
}
|
||||
draw->font[i] = gf_texture_create(draw, 8, 8, font);
|
||||
free(font);
|
||||
}
|
||||
gf_log_function(engine, "Registered %d glyphs", regcount);
|
||||
} else {
|
||||
gf_draw_destroy(draw);
|
||||
draw = NULL;
|
||||
@ -138,12 +118,6 @@ int gf_draw_step(gf_draw_t* draw) {
|
||||
}
|
||||
|
||||
void gf_draw_destroy(gf_draw_t* draw) {
|
||||
if(draw->running) {
|
||||
int i;
|
||||
for(i = 0; i < 128; i++) {
|
||||
if(draw->font[i] != NULL) gf_texture_destroy(draw->font[i]);
|
||||
}
|
||||
}
|
||||
if(draw->driver != NULL) gf_draw_driver_destroy(draw->driver);
|
||||
if(draw->platform != NULL) gf_draw_platform_destroy(draw->platform);
|
||||
gf_log_function(draw->engine, "Destroyed drawing interface", "");
|
||||
|
@ -1,3 +1,5 @@
|
||||
#define GF_EXPOSE_FONT
|
||||
|
||||
#include <gf_pre.h>
|
||||
|
||||
/* External library */
|
||||
@ -6,5 +8,38 @@
|
||||
#include <gf_font.h>
|
||||
|
||||
/* Engine */
|
||||
#include <gf_log.h>
|
||||
|
||||
/* Standard */
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
gf_font_glyph_t* gf_font_get(gf_font_t* font, int code) {
|
||||
int i;
|
||||
if(code < 0x20) return NULL;
|
||||
for(i = 0; i < font->count; i++) {
|
||||
if(font->glyph[i]->code == code) {
|
||||
return font->glyph[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define gf_stat _stat
|
||||
#else
|
||||
#define gf_stat stat
|
||||
#endif
|
||||
|
||||
gf_font_t* gf_font_create(const char* path) {
|
||||
gf_font_t* font = malloc(sizeof(*font));
|
||||
struct gf_stat s;
|
||||
memset(font, 0, sizeof(*font));
|
||||
if(gf_stat(path, &s) != 0) {
|
||||
free(font);
|
||||
return NULL;
|
||||
}
|
||||
gf_log_function(NULL, "%s: %lu bytes", path, (unsigned long)s.st_size);
|
||||
return font;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#define GF_EXPOSE_DRAW
|
||||
#define GF_EXPOSE_TEXTURE
|
||||
#define GF_EXPOSE_FONT
|
||||
|
||||
#include <gf_pre.h>
|
||||
|
||||
@ -10,6 +11,7 @@
|
||||
|
||||
/* Engine */
|
||||
#include <gf_draw.h>
|
||||
#include <gf_font.h>
|
||||
|
||||
/* Standard */
|
||||
#include <string.h>
|
||||
@ -17,13 +19,20 @@
|
||||
void gf_graphic_fill_rect(gf_draw_t* draw, float x, float y, float w, float h, gf_graphic_color_t color) { gf_graphic_fill_polygon(draw, color, GF_GRAPHIC_2D, 4, x, y, x, y + h, x + w, y + h, x + w, y); }
|
||||
|
||||
void gf_graphic_text(gf_draw_t* draw, float x, float y, float size, const char* text, gf_graphic_color_t color) {
|
||||
int i;
|
||||
int i;
|
||||
double mx = 0;
|
||||
gf_font_glyph_t* glyph;
|
||||
for(i = 0; text[i] != 0; i++) {
|
||||
if(draw->font[text[i]] != NULL) gf_graphic_draw_texture_2d(draw, x + i * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y), y, size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y, size, draw->font[text[i]], color);
|
||||
if(draw->font != NULL && (glyph = gf_font_get(draw->font, text[i])) != NULL) {
|
||||
double fax = 64;
|
||||
double fay = 64;
|
||||
gf_graphic_draw_texture_2d(draw, x + mx, y, size * fax / fay, size, glyph->texture, color);
|
||||
mx += (size * glyph->dwidth[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return (float)strlen(text) * (size * GF_GRAPHIC_FONT_ASPECT_X / GF_GRAPHIC_FONT_ASPECT_Y); }
|
||||
float gf_graphic_text_width(gf_draw_t* draw, float size, const char* text) { return 0; }
|
||||
|
||||
void gf_graphic_draw_texture_2d(gf_draw_t* draw, float x, float y, float w, float h, gf_texture_t* texture, gf_graphic_color_t color) {
|
||||
if(texture != NULL) gf_graphic_draw_texture_polygon(draw, texture, color, GF_GRAPHIC_2D, 4, 0.0, 0.0, x, y, 0.0, 1.0, x, y + h, 1.0, 1.0, x + w, y + h, 1.0, 0.0, x + w, y);
|
||||
|
@ -11,9 +11,27 @@
|
||||
#include <gf_macro.h>
|
||||
|
||||
/* Type */
|
||||
#include <gf_type/font.h>
|
||||
|
||||
/* Engine */
|
||||
|
||||
/* Standard */
|
||||
|
||||
/**
|
||||
* @~english
|
||||
* @brief Get glyph
|
||||
* @param font Font
|
||||
* @param code Character code
|
||||
* @return Glyph
|
||||
*/
|
||||
GF_EXPORT gf_font_glyph_t* gf_font_get(gf_font_t* font, int code);
|
||||
|
||||
/**
|
||||
* @~english
|
||||
* @brief Load font
|
||||
* @param path Path
|
||||
* @return Font
|
||||
*/
|
||||
GF_EXPORT gf_font_t* gf_font_create(const char* path);
|
||||
|
||||
#endif
|
||||
|
@ -19,18 +19,6 @@
|
||||
|
||||
/* Standard */
|
||||
|
||||
/**
|
||||
* @~english
|
||||
* @brief Aspect of font width
|
||||
*/
|
||||
#define GF_GRAPHIC_FONT_ASPECT_X 1
|
||||
|
||||
/**
|
||||
* @~english
|
||||
* @brief Aspect of font height
|
||||
*/
|
||||
#define GF_GRAPHIC_FONT_ASPECT_Y 2
|
||||
|
||||
/**
|
||||
* @~english
|
||||
* @brief Dimension parameter for 2D
|
||||
|
@ -105,6 +105,14 @@
|
||||
#define GF_EXPOSE_SERVER
|
||||
#endif
|
||||
|
||||
#ifndef GF_EXPOSE_FONT
|
||||
/**
|
||||
* @~english
|
||||
* @brief Expose font properties
|
||||
*/
|
||||
#define GF_EXPOSE_FONT
|
||||
#endif
|
||||
|
||||
#ifndef GF_EXPOSE_GRAPHIC
|
||||
/**
|
||||
* @~english
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <gf_type/core.h>
|
||||
#include <gf_type/math.h>
|
||||
#include <gf_type/gui.h>
|
||||
#include <gf_type/font.h>
|
||||
|
||||
/* Standard */
|
||||
|
||||
@ -65,7 +66,7 @@
|
||||
* @brief Window title
|
||||
*
|
||||
* @var gf_draw_t::font
|
||||
* @brief Array of texture of glyphs
|
||||
* @brief Current font
|
||||
*
|
||||
* @var gf_draw_t::light
|
||||
* @brief Light location
|
||||
@ -93,7 +94,7 @@ GF_DECLARE_TYPE(draw, {
|
||||
int running;
|
||||
int draw_3d;
|
||||
char title[128];
|
||||
gf_texture_t* font[128];
|
||||
gf_font_t* font;
|
||||
gf_math_vector_t light;
|
||||
gf_math_vector_t lookat;
|
||||
gf_math_vector_t camera;
|
||||
|
98
engine/include/gf_type/font.h
Normal file
98
engine/include/gf_type/font.h
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @file gf_type/font.h
|
||||
* @~english
|
||||
* @brief Type definitions related to font
|
||||
*/
|
||||
|
||||
#ifndef __GF_TYPE_FONT_H__
|
||||
#define __GF_TYPE_FONT_H__
|
||||
|
||||
#include <gf_pre.h>
|
||||
#include <gf_macro.h>
|
||||
|
||||
#ifdef GF_EXPOSE_FONT
|
||||
/* External library */
|
||||
|
||||
/* Engine */
|
||||
#include <gf_type/texture.h>
|
||||
|
||||
/* Standard */
|
||||
|
||||
/**
|
||||
* @struct gf_font_bbox_t
|
||||
* @~english
|
||||
* @brief Bounding box
|
||||
*
|
||||
* @var gf_font_bbox_t::width
|
||||
* @brief Width
|
||||
*
|
||||
* @var gf_font_bbox_t::height
|
||||
* @brief Height
|
||||
*
|
||||
* @var gf_font_bbox_t::x
|
||||
* @brief X coord
|
||||
*
|
||||
* @var gf_font_bbox_t::y
|
||||
* @brief Y coord
|
||||
*/
|
||||
GF_DECLARE_TYPE(font_bbox, {
|
||||
int width;
|
||||
int height;
|
||||
int x;
|
||||
int y;
|
||||
});
|
||||
|
||||
/**
|
||||
* @struct gf_font_glyph_t
|
||||
* @~english
|
||||
* @brief Glyph
|
||||
*
|
||||
* @var gf_font_glyph_t::code
|
||||
* @brief Character code
|
||||
*
|
||||
* @var gf_font_glyph_t::texture
|
||||
* @brief Texture
|
||||
*
|
||||
* @var gf_font_glyph_t::bpl
|
||||
* @brief Bytes per line
|
||||
*
|
||||
* @var gf_font_glyph_t::bbox
|
||||
* @brief Bounding box
|
||||
*
|
||||
* @var gf_font_glyph_t::dwidth
|
||||
* @brief Device width
|
||||
*/
|
||||
GF_DECLARE_TYPE(font_glyph, {
|
||||
int code;
|
||||
gf_texture_t* texture;
|
||||
int bpl;
|
||||
gf_font_bbox_t bbox;
|
||||
int dwidth[2];
|
||||
});
|
||||
|
||||
/**
|
||||
* @struct gf_font_t
|
||||
* @~english
|
||||
* @brief Font
|
||||
*
|
||||
* @var gf_font_t::glyph
|
||||
* @brief Glyph data
|
||||
*
|
||||
* @var gf_font_t::count
|
||||
* @brief Glyph count
|
||||
*
|
||||
* @var gf_font_t::bbox
|
||||
* @brief Bounding box
|
||||
*/
|
||||
GF_DECLARE_TYPE(font, {
|
||||
gf_font_glyph_t** glyph;
|
||||
int count;
|
||||
gf_font_bbox_t bbox;
|
||||
});
|
||||
#else
|
||||
typedef void gf_font_glyph_t;
|
||||
typedef void gf_font_bbox_t;
|
||||
typedef void gf_font_t;
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user