vaseboot/VasEBoot-core/gfxmenu/font.c

117 lines
3.5 KiB
C

/* font.c - Font API and font file loader. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/bufio.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/file.h>
#include <VasEBoot/font.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/types.h>
#include <VasEBoot/video.h>
#include <VasEBoot/bitmap.h>
#include <VasEBoot/charset.h>
#include <VasEBoot/unicode.h>
#include <VasEBoot/fontformat.h>
#include <VasEBoot/gfxmenu_view.h>
/* Draw a UTF-8 string of text on the current video render target.
The x coordinate specifies the starting x position for the first character,
while the y coordinate specifies the baseline position.
If the string contains a character that FONT does not contain, then
a glyph from another loaded font may be used instead. */
VasEBoot_err_t
VasEBoot_font_draw_string (const char *str, VasEBoot_font_t font,
VasEBoot_video_color_t color,
int left_x, int baseline_y)
{
int x;
VasEBoot_uint32_t *logical;
VasEBoot_ssize_t logical_len, visual_len;
struct VasEBoot_unicode_glyph *visual, *ptr;
VasEBoot_err_t err;
logical_len = VasEBoot_utf8_to_ucs4_alloc (str, &logical, 0);
if (logical_len < 0)
return VasEBoot_errno;
visual_len = VasEBoot_bidi_logical_to_visual (logical, logical_len, &visual,
0, 0, 0, 0, 0, 0, 0);
VasEBoot_free (logical);
if (visual_len < 0)
return VasEBoot_errno;
err = VAS_EBOOT_ERR_NONE;
for (ptr = visual, x = left_x; ptr < visual + visual_len; ptr++)
{
struct VasEBoot_font_glyph *glyph;
glyph = VasEBoot_font_construct_glyph (font, ptr);
if (!glyph)
{
err = VasEBoot_errno;
goto out;
}
err = VasEBoot_font_draw_glyph (glyph, color, x, baseline_y);
if (err)
goto out;
x += glyph->device_width;
}
out:
for (ptr = visual; ptr < visual + visual_len; ptr++)
VasEBoot_unicode_destroy_glyph (ptr);
VasEBoot_free (visual);
return err;
}
/* Get the width in pixels of the specified UTF-8 string, when rendered in
in the specified font (but falling back on other fonts for glyphs that
are missing). */
int
VasEBoot_font_get_string_width (VasEBoot_font_t font, const char *str)
{
int width = 0;
VasEBoot_uint32_t *ptr;
VasEBoot_ssize_t logical_len;
VasEBoot_uint32_t *logical;
logical_len = VasEBoot_utf8_to_ucs4_alloc (str, &logical, 0);
if (logical_len < 0)
{
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
return 0;
}
for (ptr = logical; ptr < logical + logical_len;)
{
struct VasEBoot_unicode_glyph glyph;
ptr += VasEBoot_unicode_aglomerate_comb (ptr,
logical_len - (ptr - logical),
&glyph);
width += VasEBoot_font_get_constructed_device_width (font, &glyph);
VasEBoot_unicode_destroy_glyph (&glyph);
}
VasEBoot_free (logical);
return width;
}