view-options.hh 4.1 KB

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