face-options.hh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. struct face_options_t
  30. {
  31. ~face_options_t ()
  32. {
  33. g_free (font_file);
  34. }
  35. void set_face (hb_face_t *face_)
  36. { face = face_; }
  37. void add_options (option_parser_t *parser);
  38. void post_parse (GError **error);
  39. static struct cache_t
  40. {
  41. ~cache_t ()
  42. {
  43. g_free (font_path);
  44. hb_blob_destroy (blob);
  45. hb_face_destroy (face);
  46. }
  47. char *font_path = nullptr;
  48. hb_blob_t *blob = nullptr;
  49. unsigned face_index = (unsigned) -1;
  50. hb_face_t *face = nullptr;
  51. } cache;
  52. char *font_file = nullptr;
  53. unsigned face_index = 0;
  54. hb_blob_t *blob = nullptr;
  55. hb_face_t *face = nullptr;
  56. };
  57. face_options_t::cache_t face_options_t::cache {};
  58. void
  59. face_options_t::post_parse (GError **error)
  60. {
  61. if (!font_file)
  62. {
  63. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  64. "No font file set");
  65. return;
  66. }
  67. assert (font_file);
  68. const char *font_path = font_file;
  69. if (0 == strcmp (font_path, "-"))
  70. {
  71. #if defined(_WIN32) || defined(__CYGWIN__)
  72. setmode (fileno (stdin), O_BINARY);
  73. font_path = "STDIN";
  74. #else
  75. font_path = "/dev/stdin";
  76. #endif
  77. }
  78. if (!cache.font_path || 0 != strcmp (cache.font_path, font_path))
  79. {
  80. hb_blob_destroy (cache.blob);
  81. cache.blob = hb_blob_create_from_file_or_fail (font_path);
  82. free ((char *) cache.font_path);
  83. cache.font_path = g_strdup (font_path);
  84. if (!cache.blob)
  85. {
  86. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  87. "%s: Failed reading file", font_path);
  88. return;
  89. }
  90. hb_face_destroy (cache.face);
  91. cache.face = nullptr;
  92. cache.face_index = (unsigned) -1;
  93. }
  94. if (cache.face_index != face_index)
  95. {
  96. hb_face_destroy (cache.face);
  97. cache.face = hb_face_create (cache.blob, face_index);
  98. cache.face_index = face_index;
  99. }
  100. blob = cache.blob;
  101. face = cache.face;
  102. }
  103. void
  104. face_options_t::add_options (option_parser_t *parser)
  105. {
  106. GOptionEntry entries[] =
  107. {
  108. {"font-file", 0, 0, G_OPTION_ARG_STRING, &this->font_file, "Set font file-name", "filename"},
  109. {"face-index", 0, 0, G_OPTION_ARG_INT, &this->face_index, "Set face index (default: 0)", "index"},
  110. {nullptr}
  111. };
  112. parser->add_group (entries,
  113. "face",
  114. "Font-face options:",
  115. "Options for the font face",
  116. this);
  117. }
  118. #endif