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