face-options.hh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef FACE_OPTIONS_HH
  27. #define FACE_OPTIONS_HH
  28. #include "options.hh"
  29. #ifdef HAVE_FREETYPE
  30. #include <hb-ft.h>
  31. #endif
  32. #ifdef HAVE_CORETEXT
  33. #include <hb-coretext.h>
  34. #endif
  35. struct face_options_t
  36. {
  37. ~face_options_t ()
  38. {
  39. g_free (face_loader);
  40. g_free (font_file);
  41. }
  42. void set_face (hb_face_t *face_)
  43. { face = face_; }
  44. void add_options (option_parser_t *parser);
  45. void post_parse (GError **error);
  46. static struct cache_t
  47. {
  48. ~cache_t ()
  49. {
  50. g_free (font_path);
  51. hb_face_destroy (face);
  52. }
  53. char *font_path = nullptr;
  54. unsigned face_index = (unsigned) -1;
  55. hb_face_t *face = nullptr;
  56. } cache;
  57. char *font_file = nullptr;
  58. unsigned face_index = 0;
  59. char *face_loader = nullptr;
  60. hb_face_t *face = nullptr;
  61. };
  62. face_options_t::cache_t face_options_t::cache {};
  63. static struct supported_face_loaders_t {
  64. char name[9];
  65. hb_face_t * (*func) (const char *font_file, unsigned face_index);
  66. } supported_face_loaders[] =
  67. {
  68. {"ot", hb_face_create_from_file_or_fail},
  69. #ifdef HAVE_FREETYPE
  70. {"ft", hb_ft_face_create_from_file_or_fail},
  71. #endif
  72. #ifdef HAVE_CORETEXT
  73. {"coretext", hb_coretext_face_create_from_file_or_fail},
  74. #endif
  75. };
  76. void
  77. face_options_t::post_parse (GError **error)
  78. {
  79. if (!font_file)
  80. {
  81. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  82. "No font file set");
  83. return;
  84. }
  85. assert (font_file);
  86. const char *font_path = font_file;
  87. if (0 == strcmp (font_path, "-"))
  88. {
  89. #if defined(_WIN32) || defined(__CYGWIN__)
  90. setmode (fileno (stdin), O_BINARY);
  91. font_path = "STDIN";
  92. #else
  93. font_path = "/dev/stdin";
  94. #endif
  95. }
  96. hb_face_t * (*face_load) (const char *file_name, unsigned face_index) = nullptr;
  97. if (!face_loader)
  98. {
  99. face_load = supported_face_loaders[0].func;
  100. }
  101. else
  102. {
  103. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_face_loaders); i++)
  104. if (0 == g_ascii_strcasecmp (face_loader, supported_face_loaders[i].name))
  105. {
  106. face_load = supported_face_loaders[i].func;
  107. break;
  108. }
  109. if (!face_load)
  110. {
  111. GString *s = g_string_new (nullptr);
  112. for (unsigned int i = 0; i < ARRAY_LENGTH (supported_face_loaders); i++)
  113. {
  114. if (i)
  115. g_string_append_c (s, '/');
  116. g_string_append (s, supported_face_loaders[i].name);
  117. }
  118. g_string_append_c (s, '\n');
  119. char *p = g_string_free (s, FALSE);
  120. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  121. "Unknown face loader `%s'; supported values are: %s; default is %s",
  122. face_loader,
  123. p,
  124. supported_face_loaders[0].name);
  125. free (p);
  126. return;
  127. }
  128. }
  129. if (!cache.font_path ||
  130. 0 != strcmp (cache.font_path, font_path) ||
  131. cache.face_index != face_index)
  132. {
  133. hb_face_destroy (cache.face);
  134. cache.face = face_load (font_path, face_index);
  135. cache.face_index = face_index;
  136. free ((char *) cache.font_path);
  137. cache.font_path = g_strdup (font_path);
  138. if (!cache.face)
  139. {
  140. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  141. "%s: Failed loading font face", font_path);
  142. return;
  143. }
  144. }
  145. face = cache.face;
  146. }
  147. void
  148. face_options_t::add_options (option_parser_t *parser)
  149. {
  150. char *face_loaders_text = nullptr;
  151. {
  152. static_assert ((ARRAY_LENGTH_CONST (supported_face_loaders) > 0),
  153. "No supported face-loaders found.");
  154. GString *s = g_string_new (nullptr);
  155. g_string_printf (s, "Set face loader to use (default: %s)\n\n Supported face loaders are: %s",
  156. supported_face_loaders[0].name,
  157. supported_face_loaders[0].name);
  158. for (unsigned int i = 1; i < ARRAY_LENGTH (supported_face_loaders); i++)
  159. {
  160. g_string_append_c (s, '/');
  161. g_string_append (s, supported_face_loaders[i].name);
  162. }
  163. face_loaders_text = g_string_free (s, FALSE);
  164. parser->free_later (face_loaders_text);
  165. }
  166. GOptionEntry entries[] =
  167. {
  168. {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
  169. {"face-index", 'y', 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
  170. {"face-loader", 0, 0, G_OPTION_ARG_STRING, &this->face_loader, face_loaders_text, "loader"},
  171. {nullptr}
  172. };
  173. parser->add_group (entries,
  174. "face",
  175. "Font-face options:",
  176. "Options for the font face",
  177. this);
  178. }
  179. #endif