Fix the directory layout and build system.
[gfxprim.git] / include / text / GP_Font.h
blobe84d459cf0ccf4fc707496297625bc2be06cd756
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #ifndef GP_FONT_H
27 #define GP_FONT_H
29 #include <stdint.h>
31 /* The smallest charset, covering only the 7-bit ASCII (0x20 .. 0x7f). */
32 typedef enum GP_Charset {
33 GP_CHARSET_7BIT = 1,
34 } GP_Charset;
36 /* Maximum length of the font name, author, etc. (Note: these values are
37 * used by the on-disc font format.)
39 #define GP_FONT_FAMILY_MAX 63
40 #define GP_FONT_NAME_MAX 63
41 #define GP_FONT_AUTHOR_MAX 63
42 #define GP_FONT_LICENSE_MAX 15
44 /* The current version of the on-disc font format. */
45 #define GP_FONT_FORMAT_VMAJOR 1
46 #define GP_FONT_FORMAT_VMINOR 0
48 /* Magic string starting the on-disc font file. */
49 #define GP_FONT_MAGIC "# gfxprim font file"
52 * Contains font metadata.
54 typedef struct GP_Font {
56 /* Name of the font family. */
57 char family[GP_FONT_NAME_MAX + 1];
59 /* Font name. */
60 char name[GP_FONT_NAME_MAX + 1];
62 /* Name of the font author. */
63 char author[GP_FONT_AUTHOR_MAX + 1];
65 /* Font license (default is "GPL2"). */
66 char license[GP_FONT_LICENSE_MAX + 1];
68 /* Font version (incremented by font author when modifying the font data,
69 * do not confuse with format version).
71 unsigned int version;
73 /* The charset specifies which characters are defined by the font. */
74 uint8_t charset;
77 /* Height of every character in pixels. */
78 uint8_t height;
80 /* Height of the baseline (number of pixels from the bottom). */
81 uint8_t baseline;
84 * Number of bytes for each pixel line in the character data
85 * (typically 1/8 of char_width, rounded upwards).
87 uint8_t bytes_per_line;
89 /* Maximum width of the character bounding box (including empty areas
90 * that are not drawn but cause other characters to shift).
92 uint8_t max_bounding_width;
95 * Array of GP_CharData structures, packed together sequentially
96 * without padding.
98 * Characters are stored in encoding order. The first encoded character
99 * is 0x20 (space). A font must, at a minimum, encode all characters
100 * of the 7-bit ASCII set (0x20 .. 0x7F, inclusive).
102 uint8_t *data;
103 } GP_Font;
105 #define GP_CHECK_FONT(font) do { \
106 GP_CHECK(font->data, "invalid font: NULL font data"); \
107 GP_CHECK(font->height > 0, "invalid font: height == 0"); \
108 GP_CHECK(font->baseline <= font->height, "invalid font: baseline exceeds height"); \
109 GP_CHECK(font->bytes_per_line > 0, "invalid font: bytes_per_line == 0"); \
110 } while(0)
112 /* Data describing a single character. */
113 typedef struct GP_CharData {
115 /* Width of the character in pixels. This is the area that is drawn
116 * onto, but the real area occupied by the character can be different
117 * and is defined by pre_offset and post_offset.
119 uint8_t char_width;
121 /* X offset to be applied to the current position *before*
122 * drawing the character.
124 int8_t pre_offset;
126 /* X offset to be applied to the current position *after*
127 * the character is drawn.
129 int8_t post_offset;
131 /* Character bitmap (size depends on width and height). */
132 uint8_t bitmap[];
134 } GP_CharData;
136 /* The default font, which is hardcoded and always available. */
137 extern struct GP_Font GP_default_console_font;
138 extern struct GP_Font GP_default_proportional_font;
140 /* Returns the number of bytes occupied by the GP_CharData structure
141 * for this font. (Currently, all characters occupy the same space
142 * regardless of proportionality.)
144 unsigned int GP_GetCharDataSize(const GP_Font *font);
146 /* Returns a pointer to the character data (which start by the header)
147 * of the specified character in the font data area.
149 const GP_CharData *GP_GetCharData(const GP_Font *font, int c);
151 /* Returns the overall size (in bytes) occupied by all characters
152 * of the font (the font metadata do not count into this value;
153 * add sizeof(GP_Font) to get the complete size of the font in memory.)
155 unsigned int GP_GetFontDataSize(const GP_Font *font);
157 #include "core/GP_RetCode.h"
159 GP_RetCode GP_FontLoad(GP_Font **font, const char *filename);
160 GP_RetCode GP_FontSave(const GP_Font *font, const char *filename);
162 #endif /* GP_FONT_H */