view-options.hh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 VIEW_OPTIONS_HH
  27. #define VIEW_OPTIONS_HH
  28. #include "options.hh"
  29. #define DEFAULT_MARGIN 16
  30. #define DEFAULT_FORE "#000000"
  31. #define DEFAULT_BACK "#FFFFFF"
  32. struct view_options_t
  33. {
  34. ~view_options_t ()
  35. {
  36. g_free (fore);
  37. g_free (back);
  38. g_free (custom_palette);
  39. }
  40. void add_options (option_parser_t *parser);
  41. char *fore = nullptr;
  42. char *back = nullptr;
  43. unsigned int palette = 0;
  44. char *custom_palette = nullptr;
  45. double line_space = 0;
  46. bool have_font_extents = false;
  47. struct font_extents_t {
  48. double ascent, descent, line_gap;
  49. } font_extents = {0., 0., 0.};
  50. struct margin_t {
  51. double t, r, b, l;
  52. } margin = {DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN};
  53. hb_bool_t show_extents = false;
  54. };
  55. static gboolean
  56. parse_font_extents (const char *name G_GNUC_UNUSED,
  57. const char *arg,
  58. gpointer data,
  59. GError **error G_GNUC_UNUSED)
  60. {
  61. view_options_t *view_opts = (view_options_t *) data;
  62. view_options_t::font_extents_t &e = view_opts->font_extents;
  63. switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf", &e.ascent, &e.descent, &e.line_gap)) {
  64. case 1: HB_FALLTHROUGH;
  65. case 2: HB_FALLTHROUGH;
  66. case 3:
  67. view_opts->have_font_extents = true;
  68. return true;
  69. default:
  70. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  71. "%s argument should be one to three space-separated numbers",
  72. name);
  73. return false;
  74. }
  75. }
  76. static gboolean
  77. parse_margin (const char *name G_GNUC_UNUSED,
  78. const char *arg,
  79. gpointer data,
  80. GError **error G_GNUC_UNUSED)
  81. {
  82. view_options_t *view_opts = (view_options_t *) data;
  83. view_options_t::margin_t &m = view_opts->margin;
  84. switch (sscanf (arg, "%lf%*[ ,]%lf%*[ ,]%lf%*[ ,]%lf", &m.t, &m.r, &m.b, &m.l)) {
  85. case 1: m.r = m.t; HB_FALLTHROUGH;
  86. case 2: m.b = m.t; HB_FALLTHROUGH;
  87. case 3: m.l = m.r; HB_FALLTHROUGH;
  88. case 4: return true;
  89. default:
  90. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
  91. "%s argument should be one to four space-separated numbers",
  92. name);
  93. return false;
  94. }
  95. }
  96. void
  97. view_options_t::add_options (option_parser_t *parser)
  98. {
  99. GOptionEntry entries[] =
  100. {
  101. {"annotate", 0, G_OPTION_FLAG_HIDDEN,
  102. G_OPTION_ARG_NONE, &this->show_extents, "Annotate output rendering", nullptr},
  103. {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"},
  104. {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"},
  105. {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"},
  106. {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"},
  107. {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"},
  108. {"font-extents", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_extents, "Set font ascent/descent/line-gap (default: auto)","one to three numbers"},
  109. {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"},
  110. {"show-extents", 0, 0, G_OPTION_ARG_NONE, &this->show_extents, "Draw glyph extents", nullptr},
  111. {nullptr}
  112. };
  113. parser->add_group (entries,
  114. "view",
  115. "View options:",
  116. "Options for output rendering",
  117. this);
  118. }
  119. #endif