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