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