2N/A/* font.c - Font API and font file loader. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/bufio.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/file.h>
2N/A#include <grub/font.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/types.h>
2N/A#include <grub/video.h>
2N/A#include <grub/bitmap.h>
2N/A#include <grub/charset.h>
2N/A#include <grub/unicode.h>
2N/A#include <grub/fontformat.h>
2N/A#include <grub/gfxmenu_view.h>
2N/A
2N/A/* Draw a UTF-8 string of text on the current video render target.
2N/A The x coordinate specifies the starting x position for the first character,
2N/A while the y coordinate specifies the baseline position.
2N/A If the string contains a character that FONT does not contain, then
2N/A a glyph from another loaded font may be used instead. */
2N/Agrub_err_t
2N/Agrub_font_draw_string (const char *str, grub_font_t font,
2N/A grub_video_color_t color,
2N/A int left_x, int baseline_y)
2N/A{
2N/A int x;
2N/A struct grub_font_glyph *glyph;
2N/A grub_uint32_t *logical;
2N/A grub_ssize_t logical_len, visual_len;
2N/A struct grub_unicode_glyph *visual, *ptr;
2N/A
2N/A logical_len = grub_utf8_to_ucs4_alloc (str, &logical, 0);
2N/A if (logical_len < 0)
2N/A return grub_errno;
2N/A
2N/A visual_len = grub_bidi_logical_to_visual (logical, logical_len, &visual,
2N/A 0, 0, 0);
2N/A grub_free (logical);
2N/A if (visual_len < 0)
2N/A return grub_errno;
2N/A
2N/A for (ptr = visual, x = left_x; ptr < visual + visual_len; ptr++)
2N/A {
2N/A grub_err_t err;
2N/A glyph = grub_font_construct_glyph (font, ptr);
2N/A if (!glyph)
2N/A return grub_errno;
2N/A err = grub_font_draw_glyph (glyph, color, x, baseline_y);
2N/A x += glyph->device_width;
2N/A grub_free (glyph);
2N/A if (err)
2N/A return err;
2N/A }
2N/A
2N/A grub_free (visual);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Get the width in pixels of the specified UTF-8 string, when rendered in
2N/A in the specified font (but falling back on other fonts for glyphs that
2N/A are missing). */
2N/Aint
2N/Agrub_font_get_string_width (grub_font_t font, const char *str)
2N/A{
2N/A int width = 0;
2N/A grub_uint32_t *ptr;
2N/A grub_ssize_t logical_len;
2N/A grub_uint32_t *logical;
2N/A
2N/A logical_len = grub_utf8_to_ucs4_alloc (str, &logical, 0);
2N/A if (logical_len < 0)
2N/A {
2N/A grub_errno = GRUB_ERR_NONE;
2N/A return 0;
2N/A }
2N/A
2N/A for (ptr = logical; ptr < logical + logical_len;)
2N/A {
2N/A struct grub_unicode_glyph glyph;
2N/A
2N/A ptr += grub_unicode_aglomerate_comb (ptr,
2N/A logical_len - (ptr - logical),
2N/A &glyph);
2N/A width += grub_font_get_constructed_device_width (font, &glyph);
2N/A
2N/A grub_free (glyph.combining);
2N/A }
2N/A
2N/A return width;
2N/A}