• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OPTIONS_HH
28 #define OPTIONS_HH
29 
30 #include "hb.hh"
31 
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <math.h>
38 #include <locale.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h> /* for isatty() */
43 #endif
44 #if defined(_WIN32) || defined(__CYGWIN__)
45 #include <io.h> /* for setmode() under Windows */
46 #endif
47 
48 #include <hb.h>
49 #include <hb-ot.h>
50 #include <glib.h>
51 #include <glib/gprintf.h>
52 
53 void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
54 
55 struct option_group_t
56 {
~option_group_toption_group_t57   virtual ~option_group_t () {}
58 
59   virtual void add_options (struct option_parser_t *parser) = 0;
60 
pre_parseoption_group_t61   virtual void pre_parse (GError **error G_GNUC_UNUSED) {}
post_parseoption_group_t62   virtual void post_parse (GError **error G_GNUC_UNUSED) {}
63 };
64 
65 
66 struct option_parser_t
67 {
option_parser_toption_parser_t68   option_parser_t (const char *usage)
69   {
70     memset (this, 0, sizeof (*this));
71     usage_str = usage;
72     context = g_option_context_new (usage);
73     to_free = g_ptr_array_new ();
74 
75     add_main_options ();
76   }
~option_parser_toption_parser_t77   ~option_parser_t ()
78   {
79     g_option_context_free (context);
80     g_ptr_array_foreach (to_free, (GFunc) g_free, nullptr);
81     g_ptr_array_free (to_free, TRUE);
82   }
83 
84   void add_main_options ();
85 
86   void add_group (GOptionEntry   *entries,
87 		  const gchar    *name,
88 		  const gchar    *description,
89 		  const gchar    *help_description,
90 		  option_group_t *option_group);
91 
free_lateroption_parser_t92   void free_later (char *p) {
93     g_ptr_array_add (to_free, p);
94   }
95 
96   void parse (int *argc, char ***argv);
97 
usageoption_parser_t98   G_GNUC_NORETURN void usage () {
99     g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
100     exit (1);
101   }
102 
103   private:
104   const char *usage_str;
105   GOptionContext *context;
106   GPtrArray *to_free;
107 };
108 
109 
110 #define DEFAULT_MARGIN 16
111 #define DEFAULT_FORE "#000000"
112 #define DEFAULT_BACK "#FFFFFF"
113 #define FONT_SIZE_UPEM 0x7FFFFFFF
114 #define FONT_SIZE_NONE 0
115 
116 struct view_options_t : option_group_t
117 {
view_options_tview_options_t118   view_options_t (option_parser_t *parser)
119   {
120     annotate = false;
121     fore = nullptr;
122     back = nullptr;
123     line_space = 0;
124     margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
125 
126     add_options (parser);
127   }
~view_options_tview_options_t128   virtual ~view_options_t ()
129   {
130     g_free (fore);
131     g_free (back);
132   }
133 
134   void add_options (option_parser_t *parser);
135 
136   hb_bool_t annotate;
137   char *fore;
138   char *back;
139   double line_space;
140   struct margin_t {
141     double t, r, b, l;
142   } margin;
143 };
144 
145 
146 struct shape_options_t : option_group_t
147 {
shape_options_tshape_options_t148   shape_options_t (option_parser_t *parser)
149   {
150     direction = language = script = nullptr;
151     bot = eot = preserve_default_ignorables = remove_default_ignorables = false;
152     features = nullptr;
153     num_features = 0;
154     shapers = nullptr;
155     utf8_clusters = false;
156     invisible_glyph = 0;
157     cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
158     normalize_glyphs = false;
159     verify = false;
160     num_iterations = 1;
161 
162     add_options (parser);
163   }
~shape_options_tshape_options_t164   virtual ~shape_options_t ()
165   {
166     g_free (direction);
167     g_free (language);
168     g_free (script);
169     free (features);
170     g_strfreev (shapers);
171   }
172 
173   void add_options (option_parser_t *parser);
174 
setup_buffershape_options_t175   void setup_buffer (hb_buffer_t *buffer)
176   {
177     hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
178     hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
179     hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
180     hb_buffer_set_flags (buffer, (hb_buffer_flags_t)
181 				 (HB_BUFFER_FLAG_DEFAULT |
182 				  (bot ? HB_BUFFER_FLAG_BOT : 0) |
183 				  (eot ? HB_BUFFER_FLAG_EOT : 0) |
184 				  (preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
185 				  (remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
186 				  0));
187     hb_buffer_set_invisible_glyph (buffer, invisible_glyph);
188     hb_buffer_set_cluster_level (buffer, cluster_level);
189     hb_buffer_guess_segment_properties (buffer);
190   }
191 
copy_buffer_propertiesshape_options_t192   static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src)
193   {
194     hb_segment_properties_t props;
195     hb_buffer_get_segment_properties (src, &props);
196     hb_buffer_set_segment_properties (dst, &props);
197     hb_buffer_set_flags (dst, hb_buffer_get_flags (src));
198     hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src));
199   }
200 
populate_buffershape_options_t201   void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
202 			const char *text_before, const char *text_after)
203   {
204     hb_buffer_clear_contents (buffer);
205     if (text_before) {
206       unsigned int len = strlen (text_before);
207       hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
208     }
209     hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
210     if (text_after) {
211       hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
212     }
213 
214     if (!utf8_clusters) {
215       /* Reset cluster values to refer to Unicode character index
216        * instead of UTF-8 index. */
217       unsigned int num_glyphs = hb_buffer_get_length (buffer);
218       hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
219       for (unsigned int i = 0; i < num_glyphs; i++)
220       {
221 	info->cluster = i;
222 	info++;
223       }
224     }
225 
226     setup_buffer (buffer);
227   }
228 
shapeshape_options_t229   hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
230   {
231     hb_buffer_t *text_buffer = nullptr;
232     if (verify)
233     {
234       text_buffer = hb_buffer_create ();
235       hb_buffer_append (text_buffer, buffer, 0, -1);
236     }
237 
238     if (!hb_shape_full (font, buffer, features, num_features, shapers))
239     {
240       if (error)
241         *error = "all shapers failed.";
242       goto fail;
243     }
244 
245     if (normalize_glyphs)
246       hb_buffer_normalize_glyphs (buffer);
247 
248     if (verify && !verify_buffer (buffer, text_buffer, font, error))
249       goto fail;
250 
251     if (text_buffer)
252       hb_buffer_destroy (text_buffer);
253 
254     return true;
255 
256   fail:
257     if (text_buffer)
258       hb_buffer_destroy (text_buffer);
259 
260     return false;
261   }
262 
verify_buffershape_options_t263   bool verify_buffer (hb_buffer_t  *buffer,
264 		      hb_buffer_t  *text_buffer,
265 		      hb_font_t    *font,
266 		      const char  **error=nullptr)
267   {
268     if (!verify_buffer_monotone (buffer, error))
269       return false;
270     if (!verify_buffer_safe_to_break (buffer, text_buffer, font, error))
271       return false;
272     return true;
273   }
274 
verify_buffer_monotoneshape_options_t275   bool verify_buffer_monotone (hb_buffer_t *buffer, const char **error=nullptr)
276   {
277     /* Check that clusters are monotone. */
278     if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES ||
279 	cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
280     {
281       bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
282 
283       unsigned int num_glyphs;
284       hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
285 
286       for (unsigned int i = 1; i < num_glyphs; i++)
287 	if (info[i-1].cluster != info[i].cluster &&
288 	    (info[i-1].cluster < info[i].cluster) != is_forward)
289 	{
290 	  if (error)
291 	    *error = "clusters are not monotone.";
292 	  return false;
293 	}
294     }
295 
296     return true;
297   }
298 
verify_buffer_safe_to_breakshape_options_t299   bool verify_buffer_safe_to_break (hb_buffer_t  *buffer,
300 				    hb_buffer_t  *text_buffer,
301 				    hb_font_t    *font,
302 				    const char  **error=nullptr)
303   {
304     if (cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&
305 	cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
306     {
307       /* Cannot perform this check without monotone clusters.
308        * Then again, unsafe-to-break flag is much harder to use without
309        * monotone clusters. */
310       return true;
311     }
312 
313     /* Check that breaking up shaping at safe-to-break is indeed safe. */
314 
315     hb_buffer_t *fragment = hb_buffer_create ();
316     hb_buffer_t *reconstruction = hb_buffer_create ();
317     copy_buffer_properties (reconstruction, buffer);
318 
319     unsigned int num_glyphs;
320     hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
321 
322     unsigned int num_chars;
323     hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
324 
325     /* Chop text and shape fragments. */
326     bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
327     unsigned int start = 0;
328     unsigned int text_start = forward ? 0 : num_chars;
329     unsigned int text_end = text_start;
330     for (unsigned int end = 1; end < num_glyphs + 1; end++)
331     {
332       if (end < num_glyphs &&
333 	  (info[end].cluster == info[end-1].cluster ||
334 	   info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))
335 	  continue;
336 
337       /* Shape segment corresponding to glyphs start..end. */
338       if (end == num_glyphs)
339       {
340         if (forward)
341 	  text_end = num_chars;
342 	else
343 	  text_start = 0;
344       }
345       else
346       {
347 	if (forward)
348 	{
349 	  unsigned int cluster = info[end].cluster;
350 	  while (text_end < num_chars && text[text_end].cluster < cluster)
351 	    text_end++;
352 	}
353 	else
354 	{
355 	  unsigned int cluster = info[end - 1].cluster;
356 	  while (text_start && text[text_start - 1].cluster >= cluster)
357 	    text_start--;
358 	}
359       }
360       assert (text_start < text_end);
361 
362       if (0)
363 	printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);
364 
365       hb_buffer_clear_contents (fragment);
366       copy_buffer_properties (fragment, buffer);
367 
368       /* TODO: Add pre/post context text. */
369       hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
370       if (0 < text_start)
371         flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
372       if (text_end < num_chars)
373         flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
374       hb_buffer_set_flags (fragment, flags);
375 
376       hb_buffer_append (fragment, text_buffer, text_start, text_end);
377       if (!hb_shape_full (font, fragment, features, num_features, shapers))
378       {
379 	if (error)
380 	  *error = "all shapers failed while shaping fragment.";
381 	hb_buffer_destroy (reconstruction);
382 	hb_buffer_destroy (fragment);
383 	return false;
384       }
385       hb_buffer_append (reconstruction, fragment, 0, -1);
386 
387       start = end;
388       if (forward)
389 	text_start = text_end;
390       else
391 	text_end = text_start;
392     }
393 
394     bool ret = true;
395     hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
396     if (diff)
397     {
398       if (error)
399 	*error = "Safe-to-break test failed.";
400       ret = false;
401 
402       /* Return the reconstructed result instead so it can be inspected. */
403       hb_buffer_set_length (buffer, 0);
404       hb_buffer_append (buffer, reconstruction, 0, -1);
405     }
406 
407     hb_buffer_destroy (reconstruction);
408     hb_buffer_destroy (fragment);
409 
410     return ret;
411   }
412 
shape_closureshape_options_t413   void shape_closure (const char *text, int text_len,
414 		      hb_font_t *font, hb_buffer_t *buffer,
415 		      hb_set_t *glyphs)
416   {
417     hb_buffer_reset (buffer);
418     hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
419     setup_buffer (buffer);
420     hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
421   }
422 
423   /* Buffer properties */
424   char *direction;
425   char *language;
426   char *script;
427 
428   /* Buffer flags */
429   hb_bool_t bot;
430   hb_bool_t eot;
431   hb_bool_t preserve_default_ignorables;
432   hb_bool_t remove_default_ignorables;
433 
434   hb_feature_t *features;
435   unsigned int num_features;
436   char **shapers;
437   hb_bool_t utf8_clusters;
438   hb_codepoint_t invisible_glyph;
439   hb_buffer_cluster_level_t cluster_level;
440   hb_bool_t normalize_glyphs;
441   hb_bool_t verify;
442   unsigned int num_iterations;
443 };
444 
445 
446 struct font_options_t : option_group_t
447 {
font_options_tfont_options_t448   font_options_t (option_parser_t *parser,
449 		  int default_font_size_,
450 		  unsigned int subpixel_bits_)
451   {
452     variations = nullptr;
453     num_variations = 0;
454     default_font_size = default_font_size_;
455     x_ppem = 0;
456     y_ppem = 0;
457     ptem = 0.;
458     subpixel_bits = subpixel_bits_;
459     font_file = nullptr;
460     face_index = 0;
461     font_size_x = font_size_y = default_font_size;
462     font_funcs = nullptr;
463     ft_load_flags = 2;
464 
465     blob = nullptr;
466     font = nullptr;
467 
468     add_options (parser);
469   }
~font_options_tfont_options_t470   virtual ~font_options_t ()
471   {
472     g_free (font_file);
473     free (variations);
474     g_free (font_funcs);
475     hb_font_destroy (font);
476   }
477 
478   void add_options (option_parser_t *parser);
479 
480   hb_font_t *get_font () const;
481 
482   char *font_file;
483   mutable hb_blob_t *blob;
484   int face_index;
485   hb_variation_t *variations;
486   unsigned int num_variations;
487   int default_font_size;
488   int x_ppem;
489   int y_ppem;
490   double ptem;
491   unsigned int subpixel_bits;
492   mutable double font_size_x;
493   mutable double font_size_y;
494   char *font_funcs;
495   int ft_load_flags;
496 
497   private:
498   mutable hb_font_t *font;
499 };
500 
501 
502 struct text_options_t : option_group_t
503 {
text_options_ttext_options_t504   text_options_t (option_parser_t *parser)
505   {
506     text_before = nullptr;
507     text_after = nullptr;
508 
509     text_len = -1;
510     text = nullptr;
511     text_file = nullptr;
512 
513     fp = nullptr;
514     gs = nullptr;
515     line = nullptr;
516     line_len = (unsigned int) -1;
517 
518     add_options (parser);
519   }
~text_options_ttext_options_t520   virtual ~text_options_t ()
521   {
522     g_free (text_before);
523     g_free (text_after);
524     g_free (text);
525     g_free (text_file);
526     if (gs)
527       g_string_free (gs, true);
528     if (fp && fp != stdin)
529       fclose (fp);
530   }
531 
532   void add_options (option_parser_t *parser);
533 
post_parsetext_options_t534   void post_parse (GError **error G_GNUC_UNUSED) {
535     if (text && text_file)
536       g_set_error (error,
537 		   G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
538 		   "Only one of text and text-file can be set");
539   }
540 
541   const char *get_line (unsigned int *len);
542 
543   char *text_before;
544   char *text_after;
545 
546   int text_len;
547   char *text;
548   char *text_file;
549 
550   private:
551   FILE *fp;
552   GString *gs;
553   char *line;
554   unsigned int line_len;
555 };
556 
557 struct output_options_t : option_group_t
558 {
output_options_toutput_options_t559   output_options_t (option_parser_t *parser,
560 		    const char **supported_formats_ = nullptr)
561   {
562     output_file = nullptr;
563     output_format = nullptr;
564     supported_formats = supported_formats_;
565     explicit_output_format = false;
566 
567     fp = nullptr;
568 
569     add_options (parser);
570   }
~output_options_toutput_options_t571   virtual ~output_options_t ()
572   {
573     g_free (output_file);
574     g_free (output_format);
575     if (fp && fp != stdout)
576       fclose (fp);
577   }
578 
579   void add_options (option_parser_t *parser);
580 
post_parseoutput_options_t581   void post_parse (GError **error G_GNUC_UNUSED)
582   {
583     if (output_format)
584       explicit_output_format = true;
585 
586     if (output_file && !output_format) {
587       output_format = strrchr (output_file, '.');
588       if (output_format)
589       {
590 	  output_format++; /* skip the dot */
591 	  output_format = g_strdup (output_format);
592       }
593     }
594 
595     if (output_file && 0 == strcmp (output_file, "-"))
596       output_file = nullptr; /* STDOUT */
597   }
598 
599   FILE *get_file_handle ();
600 
601   char *output_file;
602   char *output_format;
603   const char **supported_formats;
604   bool explicit_output_format;
605 
606   mutable FILE *fp;
607 };
608 
609 struct format_options_t : option_group_t
610 {
format_options_tformat_options_t611   format_options_t (option_parser_t *parser) {
612     show_glyph_names = true;
613     show_positions = true;
614     show_advances = true;
615     show_clusters = true;
616     show_text = false;
617     show_unicode = false;
618     show_line_num = false;
619     show_extents = false;
620     show_flags = false;
621     trace = false;
622 
623     add_options (parser);
624   }
625 
626   void add_options (option_parser_t *parser);
627 
628   void serialize_unicode (hb_buffer_t  *buffer,
629 			  GString      *gs);
630   void serialize_glyphs (hb_buffer_t  *buffer,
631 			 hb_font_t    *font,
632 			 hb_buffer_serialize_format_t format,
633 			 hb_buffer_serialize_flags_t flags,
634 			 GString      *gs);
635   void serialize_line_no (unsigned int  line_no,
636 			  GString      *gs);
637   void serialize_buffer_of_text (hb_buffer_t  *buffer,
638 				 unsigned int  line_no,
639 				 const char   *text,
640 				 unsigned int  text_len,
641 				 hb_font_t    *font,
642 				 GString      *gs);
643   void serialize_message (unsigned int  line_no,
644 			  const char   *type,
645 			  const char   *msg,
646 			  GString      *gs);
647   void serialize_buffer_of_glyphs (hb_buffer_t  *buffer,
648 				   unsigned int  line_no,
649 				   const char   *text,
650 				   unsigned int  text_len,
651 				   hb_font_t    *font,
652 				   hb_buffer_serialize_format_t output_format,
653 				   hb_buffer_serialize_flags_t format_flags,
654 				   GString      *gs);
655 
656 
657   hb_bool_t show_glyph_names;
658   hb_bool_t show_positions;
659   hb_bool_t show_advances;
660   hb_bool_t show_clusters;
661   hb_bool_t show_text;
662   hb_bool_t show_unicode;
663   hb_bool_t show_line_num;
664   hb_bool_t show_extents;
665   hb_bool_t show_flags;
666   hb_bool_t trace;
667 };
668 
669 struct subset_options_t : option_group_t
670 {
subset_options_tsubset_options_t671   subset_options_t (option_parser_t *parser)
672   {
673     keep_layout = false;
674     drop_hints = false;
675     desubroutinize = false;
676 
677     add_options (parser);
678   }
679 
680   void add_options (option_parser_t *parser);
681 
682   hb_bool_t keep_layout;
683   hb_bool_t drop_hints;
684   hb_bool_t desubroutinize;
685 };
686 
687 /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
688 #if defined (_MSC_VER) && (_MSC_VER < 1800)
689 
690 #ifndef FLT_RADIX
691 #define FLT_RADIX 2
692 #endif
693 
scalbn(long double x,int exp)694 __inline long double scalbn (long double x, int exp)
695 {
696   return x * (pow ((long double) FLT_RADIX, exp));
697 }
698 
scalbnf(float x,int exp)699 __inline float scalbnf (float x, int exp)
700 {
701   return x * (pow ((float) FLT_RADIX, exp));
702 }
703 #endif
704 
705 #endif
706