BDF font. More...
#include "cgul_common.h"
#include "cgul_bdf_glyph.h"
#include "cgul_exception.h"
#include "cgul_wchar.h"
Typedefs | |
typedef typedefCGUL_BEGIN_C struct cgul_bdf * | cgul_bdf_t |
This class loads a BDF font or merges multiple BDF fonts. Also, you can use cgul_bdf__write_index()
to create an index file. If you then call cgul_bdf__load()
or cgul_bdf__load_explicit()
with the name of the index file, this class will only load the meta data for the font initially. It will then use the index file to load individual glyphs from the BDF file on demand.
There is sample code in the following file that shows how to generate index files:
tests/main_bdf_genbdfi.c
You can use cgul_line_wrapper__wrap_utf8_at_width()
and cgul_line_wrapper__wrap_utf32_at_width()
to wrap lines of text based on the length reported by this class's cgul_bdf__get_utf8_extents()
and cgul_bdf__get_utf32_extents()
.
For an example of how to draw text using this class, see png_wrapper__draw_text()
in the png_wrapper distribution.
typedef typedefCGUL_BEGIN_C struct cgul_bdf* cgul_bdf_t |
Opaque pointer to a cgul_bdf
instance.
CGUL_EXPORT cgul_bdf_t cgul_bdf__new | ( | cgul_exception_t * | cex | ) |
This method creates a new bdf
object. The client is responsible for freeing the object by calling cgul_bdf__delete()
. To load a font or merge multiple fonts into the object that is returned, call cgul_bdf__load()
or cgul_bdf__load_explicit()
. If an error occurs, NULL
is returned, and an exception is thrown.
[in,out] | cex | c-style exception |
cgul_bdf
instance Referenced by cgul_bdf_cxx::cgul_bdf_cxx().
CGUL_EXPORT void cgul_bdf__delete | ( | cgul_bdf_t | bdf | ) |
This method frees all internally allocated resources for the bdf
object. This method invalidates bdf
so it must not be used after this method returns.
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::set_obj(), and cgul_bdf_cxx::~cgul_bdf_cxx().
CGUL_EXPORT void cgul_bdf__load | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const char * | font_list | ||
) |
This method splits font_list
into individual fonts and tries to load and merge each font in turn by repeatedly calling cgul_bdf__load_explicit()
. The end result is that when the client calls cgul_bdf__get_glyph()
, the fonts will be searched in order and the first matching glyph will be returned.
The font_list
can be just a single font or can have multiple fonts separated with the "|" character. A trailing ".bdf" extension in the names of the fonts is optional:
"foo-14|bar-14|baz-14"
When calling cgul_bdf__load_explicit()
, this method first tries to load the font along with its BDF index file by appending an "i" character to the BDF file name. Only if this does not succeed will the method try to load the BDF file without an index. If different behavior is needed, the client should call cbul_bdf__load_explicit()
directly.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | font_list | font list |
Referenced by cgul_bdf_cxx::load().
CGUL_EXPORT void cgul_bdf__load_explicit | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const char * | bdf_file_name, | ||
const char * | index_file_name | ||
) |
This methods loads a BDF font from bdf_file_name
. If a font is already loaded, the new font will be merged into the existing font. To manually unload the existing font first, call cgul_bdf__unload()
.
If an error occurs, an exception is thrown, and bdf
will be in an undefined state. In this case, the client should delete bdf
and start over after correcting the problem.
The index_file_name
parameter is optional. It specifies the BDF index file (which should have been generated earlier using cgul_bdf__write_index()
). The BDF index file contains information on where to find each glyph in the BDF file. All fonts must be loaded with an index file in order for cache expiration to be enabled; otherwise, there would be no good way to reload any arbitrary glyph that expires. (See cgul_bdf__set_cache_size()
).
If index_file_name
is NULL
the entire font will be loaded from file including all of the glyphs. If index_file_name
is not NULL
, only the meta information for the font will be loaded. The glyphs for the font will be loaded on demand by cgul_bdf__get_glyph()
using the index information contained in index_file_name
.
Whether to use an index file or not depends on how bdf
will be used. If bdf
will be used to lookup glyph information in large fonts for characters in random strings, using an index file will probably give the best performance; however, if bdf
needs to process each glyph once like when writing a new BDF file or when writing a BDF index file, loading the BDF without an index file will give the best performance by a wide margin.
The advantages of using an index file is that your program will start much faster and will only load the glyphs that are used thereby saving time and memory. The disadvantages are that the index file can be large and can potentially get out of sync with the BDF file. Also, the first time you try to access a glyph, it has to be loaded from disk which is a relatively slow operation, but subsequent accesses will retrieve the glyph from the cache.
If the DEFAULT_CHAR property is present in the BDF font, the glyph for that character will be used when the user requests a glyph which is not present in the font. If the DEFAULT_CHAR property is missing, this method will generate a glyph that looks like an empty box to use instead.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | bdf_file_name | bdf file name |
[in] | index_file_name | index file name |
Referenced by cgul_bdf_cxx::load_explicit().
CGUL_EXPORT void cgul_bdf__unload | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
This method unloads the font and frees internally allocated resources.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::unload().
CGUL_EXPORT unsigned long int cgul_bdf__get_cache_size | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the current limit on the size of the internal glyph cache. By convention, a cache size of 0
means the cache can grow without limit.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_cache_size().
CGUL_EXPORT void cgul_bdf__set_cache_size | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
unsigned long int | cache_size | ||
) |
This method sets the maximum size of the internal cache that stores glyphs. The cache size is only relevant if index files are specified for each BDF when cgul_bdf__load()
or cgul_bdf__load_explicit()
is called. If index files are specified, it means the glyphs can be reloaded from file later so it is safe to remove them from the internal cache.
By default, cache_size
is set to 0
which, by convention, means glyphs never expire. If the caller sets cache_size
to a positive value, the least-recently used (LRU) glyphs will be expired first.
If glyph expiration is enabled, the client must be careful not to cache glyphs itself because they could expire at any moment. Instead, the client must make sure cache_size
is large enough for the task at hand and must call cgul_bdf__get_glyph()
to get the glyph each time it is used so that bdf
can keep its LRU list up to date so that glyphs can be expired in the correct order.
Calling this method has the potential of cause glyphs to expire immediately.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | cache_size | cache size |
Referenced by cgul_bdf_cxx::set_cache_size().
CGUL_EXPORT void cgul_bdf__get_utf8_extents | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const char * | utf8, | ||
size_t | utf8_length, | ||
int | substitute, | ||
unsigned int * | width, | ||
unsigned int * | height | ||
) |
This method returns the width and height in pixels of the smallest rectangle that can contain the image of the text in the UTF-8 string utf8
. The width and height are returned in width
and height
provided they are not set to NULL
.
No attempt is made by this method to trim white space. Thus, if white space characters appear at the end of a line, their width will be added to the width that is returned. Likewise, if there are multiple trailing line breaks, the extra ones will add to the height that is returned. If this is not the behavior you want, you need to trim utf8
before passing it into this method.
It is also important to understand that the cgul_bdf
class does not draw text. Instead, it loads fonts and makes the information for each glyph available so that drawing libraries you write can draw text. Providing your drawing library spaces each character using the value returned by cgul_bdf_glyph__get_dwidth_x()
and providing your drawing library spaces each line using the value returned by cgul_bdf__get_height()
, this method will return accurate values for width
and height
.
It is also important to understand that the width for each line of text is not simply the sum of the "dwidth_x" properties of each character. It is slightly more complicated than this because some glyphs have pixels that intrude on the character cell to the left or right. If this happens for the first or last character, these extra pixels will be added to the width of those lines.
This method treats the "\n" character as the only character that causes a line break. If you want to treat "\r\n" or "\r" as line breaks, the correct solution is to use cgul_crlf_file
to read in your text because it handles DOS, Mac, and Unix text files allowing you to properly set the standard C new line character for these different formats.
If an error occurs, an exception is thrown. Errors can occur because utf8
has an invalid encoding or because utf8
has a character for which there is no glyph in the bdf
font and substitute
is not set. Errors can also occur if an index file is being used to load glyphs on demand, and an error occurs during the loading process.
You can use cgul_line_wrapper__wrap_utf8_at_width()
and to wrap lines of text based the length reported by this method.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | utf8 | UTF-8 string |
[in] | utf8_length | number of bytes (not characters) in utf8 |
[in] | substitute | whether to substitute missing glyphs with the default |
[out] | width | width of utf8 in pixels |
[out] | height | height of utf8 in pixels |
Referenced by cgul_bdf_cxx::get_utf8_extents().
CGUL_EXPORT void cgul_bdf__get_utf32_extents | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const cgul_wchar_t * | utf32, | ||
size_t | utf32_length, | ||
int | substitute, | ||
unsigned int * | width, | ||
unsigned int * | height | ||
) |
This method returns the width and height in pixels of the smallest rectangle that can contain the image of the text in the UTF-32 string utf32
. The width and height are returned in width
and height
provided they are not set to NULL
.
No attempt is made by this method to trim white space. Thus, if white space characters appear at the end of a line, their width will be added to the width that is returned. Likewise, if there are multiple trailing line breaks, the extra ones will add to the height that is returned. If this is not the behavior you want, you need to trim utf8
before passing it into this method.
It is also important to understand that the cgul_bdf
class does not draw text. Instead, it loads fonts and makes the information for each glyph available so that drawing libraries you write can draw text. Providing your drawing library spaces each character using the value returned by cgul_bdf_glyph__get_dwidth_x()
and providing your drawing library spaces each line using the value returned by cgul_bdf__get_height()
, this method will return accurate values for width
and height
.
It is also important to understand that the width for each line of text is not simply the sum of the "dwidth_x" properties of each character. It is slightly more complicated than this because some glyphs have pixels that intrude on the character cell to the left or right. If this happens for the first or last character, these extra pixels will be added to the width of those lines.
This method treats the "\n" character as the only character that causes a line break. If you want to treat "\r\n" or "\r" as line breaks, the correct solution is to use cgul_crlf_file
to read in your text because it handles DOS, Mac, and Unix text files allowing you to properly set the standard C new line character for these different formats.
If an error occurs, an exception is thrown. Errors can occur because utf8
has an invalid encoding or because utf8
has a character for which there is no glyph in the bdf
font and substitute
is not set. Errors can also occur if an index file is being used to load glyphs on demand, and an error occurs during the loading process.
You can use cgul_line_wrapper__wrap_utf32_at_width()
and to wrap lines of text based the length reported by this method.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | utf32 | UTF-32 string |
[in] | utf32_length | number of characters (not bytes) in utf32 |
[in] | substitute | whether to substitute missing glyphs with the default |
[out] | width | width of utf32 in pixels |
[out] | height | height of utf32 in pixels |
Referenced by cgul_bdf_cxx::get_utf32_extents().
CGUL_EXPORT cgul_bdf_glyph_t cgul_bdf__get_glyph | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
cgul_wchar_t | encoding, | ||
int | substitute | ||
) |
Get the glyph for a particular code point. The code point is called encoding
to match its BDF name. If substitute
is set, the default character glyph is returned when no glyph is present for encoding
; otherwise, NULL
is returned when no glyph is present.
The returned glyph is still owned by bdf
so the client must not attempt to delete it. The client is free however to make a copy using cgul_bdf_glyph__copy()
.
Errors can happen if an index file is being used to load glyphs on demand, and an error occurs during the loading process. If an error occurs, NULL
is returned, and an exception is thrown.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | encoding | code point for the glyph that is returned |
[in] | substitute | whether to substitute missing glyphs with the default |
Referenced by cgul_bdf_cxx::get_glyph().
CGUL_EXPORT void cgul_bdf__shift_right | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Shift each glyph in bdf
the same minimal number of pixels to the right so that no glyph has a negative x-offset. For efficiency, this method requires that all BDF files be loaded without specifying an index file.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::shift_right().
CGUL_EXPORT void cgul_bdf__pad_left | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Force the x-offset of each glyph in bdf
to zero but add padding to the left side of each glyph to compensate so that the glyph will still be drawn in the same location. The caller is responsible for making sure cgul_bdf__shift_right()
is called before calling this method. For efficiency, this method requires that all BDF files be loaded without specifying an index file.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::pad_left().
CGUL_EXPORT void cgul_bdf__write | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const char * | bdf_file_name | ||
) |
This method creates or truncates the file bdf_file_name
and writes the entire font in BDF format to that file. For efficiency, this method requires that all BDF files be loaded without specifying an index file.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | bdf_file_name | bdf file name |
Referenced by cgul_bdf_cxx::write().
CGUL_EXPORT void cgul_bdf__write_file | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
FILE * | f | ||
) |
This method writes the entire font in BDF format to the file f
. For efficiency, this method requires that all BDF files be loaded without specifying an index file.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | f | file |
Referenced by cgul_bdf_cxx::write_file().
CGUL_EXPORT void cgul_bdf__write_index | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
const char * | index_file_name | ||
) |
This method creates or truncates the file index_file_name
and writes the entire BDF index file for the BDF file that is currently loaded. For this method to produce meaningful results only one BDF file can be loaded, and it must not already be loaded using an index file. You can do this by calling cgul_bdf__load_explicit()
with a NULL
value for index_file_name
. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | index_file_name | file name |
Referenced by cgul_bdf_cxx::write_index().
CGUL_EXPORT void cgul_bdf__write_index_file | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf, | ||
FILE * | f | ||
) |
This method writes the entire BDF index file to f
for the BDF file that is currently loaded. f
must be opened in binary mode. For this method to produce meaningful results only one BDF file can be loaded, and it must not already be loaded using an index file. You can do this by calling cgul_bdf__load_explicit()
with a NULL
value for index_file_name
. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
[in] | f | file |
Referenced by cgul_bdf_cxx::write_index_file().
CGUL_EXPORT unsigned int cgul_bdf__get_ascent | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the ascent of the font.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_ascent().
CGUL_EXPORT unsigned int cgul_bdf__get_descent | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the descent of the font.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_descent().
CGUL_EXPORT unsigned int cgul_bdf__get_height | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
This method returns the height of the font. This is simply the sum of the ascent and the descent. The height of the font is typically the value used as the spacing between lines. If overflow is detected when summing the ascent and the descent, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_height().
CGUL_EXPORT const char* cgul_bdf__get_font_list | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the list of fonts that have been loaded and merged into bdf
. The font list has the same format as the font list passed into cgul_bdf__load()
. If no font is currently loaded, an empty string is returned. The caller must not attempt to free the pointer returned by this method.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_font_list().
CGUL_EXPORT const char* cgul_bdf__get_font_name | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the name of the font as given on the FONT line. If no font is currently loaded, NULL
is returned. The caller must not attempt to free the pointer returned by this method.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_font_name().
CGUL_EXPORT unsigned int cgul_bdf__get_font_size | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the size of the font in points.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_font_size().
CGUL_EXPORT unsigned int cgul_bdf__get_dpi_x | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the x-resolution of the font in dots per inch.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_dpi_x().
CGUL_EXPORT unsigned int cgul_bdf__get_dpi_y | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the y-resolution of the font in dots per inch.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_dpi_y().
CGUL_EXPORT unsigned int cgul_bdf__get_bb_width | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the width of the font's bounding box.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_bb_width().
CGUL_EXPORT unsigned int cgul_bdf__get_bb_height | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the height of the font's bounding box.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_bb_height().
CGUL_EXPORT int cgul_bdf__get_bb_x_offset | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the x-offset of the lower-left corner of the font's bounding box.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_bb_x_offset().
CGUL_EXPORT int cgul_bdf__get_bb_y_offset | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the y-offset of the lower-left corner of the font's bounding box.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_bb_y_offset().
CGUL_EXPORT unsigned long int cgul_bdf__get_char_count | ( | cgul_exception_t * | cex, |
cgul_bdf_t | bdf | ||
) |
Get the number of characters in the font. This function throws an exception if more than one BDF file is loaded unless all the BDF files are loaded without index files. The reason for this is that it is hard to know how many characters are in a merged BDF font when index files are being used because the actual merging of glyphs is delayed until cgul_bdf__get_glyph()
is called, and even then, the merging only occurs for the requested encoding.
[in] | cex | c-style exception |
[in] | bdf | cgul_bdf instance |
Referenced by cgul_bdf_cxx::get_char_count().