font.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* font.c - Font API and font file loader. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/bufio.h>
  20. #include <grub/dl.h>
  21. #include <grub/file.h>
  22. #include <grub/font.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/types.h>
  26. #include <grub/video.h>
  27. #include <grub/bitmap.h>
  28. #include <grub/charset.h>
  29. #include <grub/unicode.h>
  30. #include <grub/fontformat.h>
  31. #include <grub/gfxmenu_view.h>
  32. /* Draw a UTF-8 string of text on the current video render target.
  33. The x coordinate specifies the starting x position for the first character,
  34. while the y coordinate specifies the baseline position.
  35. If the string contains a character that FONT does not contain, then
  36. a glyph from another loaded font may be used instead. */
  37. grub_err_t
  38. grub_font_draw_string (const char *str, grub_font_t font,
  39. grub_video_color_t color,
  40. int left_x, int baseline_y)
  41. {
  42. int x;
  43. grub_uint32_t *logical;
  44. grub_ssize_t logical_len, visual_len;
  45. struct grub_unicode_glyph *visual, *ptr;
  46. grub_err_t err;
  47. logical_len = grub_utf8_to_ucs4_alloc (str, &logical, 0);
  48. if (logical_len < 0)
  49. return grub_errno;
  50. visual_len = grub_bidi_logical_to_visual (logical, logical_len, &visual,
  51. 0, 0, 0, 0, 0, 0, 0);
  52. grub_free (logical);
  53. if (visual_len < 0)
  54. return grub_errno;
  55. err = GRUB_ERR_NONE;
  56. for (ptr = visual, x = left_x; ptr < visual + visual_len; ptr++)
  57. {
  58. struct grub_font_glyph *glyph;
  59. glyph = grub_font_construct_glyph (font, ptr);
  60. if (!glyph)
  61. {
  62. err = grub_errno;
  63. goto out;
  64. }
  65. err = grub_font_draw_glyph (glyph, color, x, baseline_y);
  66. if (err)
  67. goto out;
  68. x += glyph->device_width;
  69. }
  70. out:
  71. for (ptr = visual; ptr < visual + visual_len; ptr++)
  72. grub_unicode_destroy_glyph (ptr);
  73. grub_free (visual);
  74. return err;
  75. }
  76. /* Get the width in pixels of the specified UTF-8 string, when rendered in
  77. in the specified font (but falling back on other fonts for glyphs that
  78. are missing). */
  79. int
  80. grub_font_get_string_width (grub_font_t font, const char *str)
  81. {
  82. int width = 0;
  83. grub_uint32_t *ptr;
  84. grub_ssize_t logical_len;
  85. grub_uint32_t *logical;
  86. logical_len = grub_utf8_to_ucs4_alloc (str, &logical, 0);
  87. if (logical_len < 0)
  88. {
  89. grub_errno = GRUB_ERR_NONE;
  90. return 0;
  91. }
  92. for (ptr = logical; ptr < logical + logical_len;)
  93. {
  94. struct grub_unicode_glyph glyph;
  95. ptr += grub_unicode_aglomerate_comb (ptr,
  96. logical_len - (ptr - logical),
  97. &glyph);
  98. width += grub_font_get_constructed_device_width (font, &glyph);
  99. grub_unicode_destroy_glyph (&glyph);
  100. }
  101. grub_free (logical);
  102. return width;
  103. }