/* font.c - Font functions. */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008  Free Software Foundation, Inc.
 *
 *  GRUB 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.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/file.h>
#include <grub/misc.h>
#include <grub/dl.h>
#include <grub/normal.h>
#include <grub/types.h>
#include <grub/mm.h>
#include <grub/font_internal.h>

grub_font_t
grub_font_get (const char *font_name)
{
  struct font_node *node;

  for (node = grub_font_list; node; node = node->next)
    {
      grub_font_t font = node->value;
      if (grub_strcmp (font->name, font_name) == 0)
        return font;
    }

  /* If no font by that name is found, return the first font in the list
   * as a fallback. */
  return grub_font_list->value;
}

const char *
grub_font_get_name (grub_font_t font)
{
  return font->name;
}

int
grub_font_get_max_char_width (grub_font_t font)
{
  return font->max_char_width;
}

int
grub_font_get_max_char_height (grub_font_t font)
{
  return font->max_char_height;
}

int
grub_font_get_ascent (grub_font_t font)
{
  return font->ascent;
}

int
grub_font_get_descent (grub_font_t font)
{
  return font->descent;
}

int
grub_font_get_string_width (grub_font_t font, const char *str)
{
  int i;
  int width;
  struct grub_font_glyph *glyph;
  grub_size_t len;

  len = grub_strlen (str);

  for (i = 0, width = 0; i < len; i++)
    {
      glyph = grub_font_get_glyph (font, str[i]);
      width += glyph->device_width;
    }

  return width;
}
