update(add) packaging directory and spec file from OBSTF:Private, OBS
[framework/uifw/harfbuzz.git] / src / hb-view.cc
1 /*
2  * Copyright © 2010  Behdad Esfahbod
3  * Copyright © 2011  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Behdad Esfahbod
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <math.h>
38 #include <locale.h>
39
40 #include <glib.h>
41
42 #include <cairo-ft.h>
43 #include <hb-ft.h>
44
45 HB_BEGIN_DECLS
46
47
48 /* Controlled by cmd-line options */
49 static int margin_t = 10;
50 static int margin_b = 10;
51 static int margin_l = 10;
52 static int margin_r = 10;
53 static int line_space = 0;
54 static int face_index = 0;
55 static double font_size = 18;
56 static const char *fore = "#000000";
57 static const char *back = "#ffffff";
58 static const char *text = NULL;
59 static const char *font_file = NULL;
60 static const char *out_file = "/dev/stdout";
61 static const char *direction = NULL;
62 static const char *script = NULL;
63 static const char *language = NULL;
64 static hb_feature_t *features = NULL;
65 static unsigned int num_features;
66 static hb_bool_t annotate = FALSE;
67 static hb_bool_t debug = FALSE;
68
69 /* Ugh, global vars.  Ugly, but does the job */
70 static int width = 0;
71 static int height = 0;
72 static cairo_surface_t *surface = NULL;
73 static cairo_pattern_t *fore_pattern = NULL;
74 static cairo_pattern_t *back_pattern = NULL;
75 static cairo_font_face_t *cairo_face;
76
77
78 G_GNUC_NORETURN static void
79 usage (FILE *f, int status)
80 {
81   fprintf (f, "usage: hb-view [OPTS...] font-file text\n");
82   exit (status);
83 }
84
85 G_GNUC_NORETURN static void
86 version (void)
87 {
88   printf ("hb-view (harfbuzz) %s\n", HB_VERSION_STRING);
89   exit (0);
90 }
91
92 static void parse_features (char *s);
93
94 static void
95 parse_opts (int argc, char **argv)
96 {
97   argv[0] = (char *) "hb-view";
98   while (1)
99     {
100       int option_index = 0, c;
101       static const struct option long_options[] = {
102         {"annotate", 0, &annotate, TRUE},
103         {"background", 1, 0, 'B'},
104         {"debug", 0, &debug, TRUE},
105         {"direction", 1, 0, 'd'},
106         {"features", 1, 0, 'f'},
107         {"font-size", 1, 0, 's'},
108         {"face-index", 1, 0, 'i'},
109         {"foreground", 1, 0, 'F'},
110         {"help", 0, 0, 'h'},
111         {"language", 1, 0, 'L'},
112         {"line-space", 1, 0, 'l'},
113         {"margin", 1, 0, 'm'},
114         {"output", 1, 0, 'o'},
115         {"script", 1, 0, 'S'},
116         {"version", 0, 0, 'v'},
117         {0, 0, 0, 0}
118       };
119
120       c = getopt_long (argc, argv, "", long_options, &option_index);
121       if (c == -1)
122         break;
123
124       switch (c)
125         {
126         case 0:
127           break;
128         case 'h':
129           usage (stdout, 0);
130           break;
131         case 'v':
132           version ();
133           break;
134         case 'i':
135           face_index = atoi (optarg);
136           break;
137         case 'l':
138           line_space = atoi (optarg);
139           break;
140         case 'm':
141           switch (sscanf (optarg, "%d %d %d %d", &margin_t, &margin_r, &margin_b, &margin_l)) {
142             default: break;
143             case 1: margin_r = margin_t;
144             case 2: margin_b = margin_t;
145             case 3: margin_l = margin_r;
146           }
147           break;
148         case 's':
149           font_size = strtod (optarg, NULL);
150           break;
151         case 'f':
152           parse_features (optarg);
153           break;
154         case 'F':
155           fore = optarg;
156           break;
157         case 'B':
158           back = optarg;
159           break;
160         case 't':
161           text = optarg;
162           break;
163         case 'd':
164           direction = optarg;
165           break;
166         case 'S':
167           script = optarg;
168           break;
169         case 'L':
170           language = optarg;
171           break;
172         case 'o':
173           out_file = optarg;
174           break;
175         case '?':
176           usage (stdout, 1);
177           break;
178         default:
179           break;
180         }
181     }
182   if (optind + 2 != argc)
183     usage (stderr, 1);
184   font_file = argv[optind++];
185   text = argv[optind++];
186 }
187
188
189 static void
190 parse_space (char **pp)
191 {
192   char c;
193 #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
194   while (c = **pp, ISSPACE (c))
195     (*pp)++;
196 #undef ISSPACE
197 }
198
199 static hb_bool_t
200 parse_char (char **pp, char c)
201 {
202   parse_space (pp);
203
204   if (**pp != c)
205     return FALSE;
206
207   (*pp)++;
208   return TRUE;
209 }
210
211 static hb_bool_t
212 parse_uint (char **pp, unsigned int *pv)
213 {
214   char *p = *pp;
215   unsigned int v;
216
217   v = strtol (p, pp, 0);
218
219   if (p == *pp)
220     return FALSE;
221
222   *pv = v;
223   return TRUE;
224 }
225
226
227 static hb_bool_t
228 parse_feature_value_prefix (char **pp, hb_feature_t *feature)
229 {
230   if (parse_char (pp, '-'))
231     feature->value = 0;
232   else {
233     parse_char (pp, '+');
234     feature->value = 1;
235   }
236
237   return TRUE;
238 }
239
240 static hb_bool_t
241 parse_feature_tag (char **pp, hb_feature_t *feature)
242 {
243   char *p = *pp, c;
244
245   parse_space (pp);
246
247 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
248   while (c = **pp, ISALNUM(c))
249     (*pp)++;
250 #undef ISALNUM
251
252   if (p == *pp)
253     return FALSE;
254
255   **pp = '\0';
256   feature->tag = hb_tag_from_string (p);
257   **pp = c;
258
259   return TRUE;
260 }
261
262 static hb_bool_t
263 parse_feature_indices (char **pp, hb_feature_t *feature)
264 {
265   hb_bool_t has_start;
266
267   feature->start = 0;
268   feature->end = (unsigned int) -1;
269
270   if (!parse_char (pp, '['))
271     return TRUE;
272
273   has_start = parse_uint (pp, &feature->start);
274
275   if (parse_char (pp, ':')) {
276     parse_uint (pp, &feature->end);
277   } else {
278     if (has_start)
279       feature->end = feature->start + 1;
280   }
281
282   return parse_char (pp, ']');
283 }
284
285 static hb_bool_t
286 parse_feature_value_postfix (char **pp, hb_feature_t *feature)
287 {
288   return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
289 }
290
291
292 static hb_bool_t
293 parse_one_feature (char **pp, hb_feature_t *feature)
294 {
295   return parse_feature_value_prefix (pp, feature) &&
296          parse_feature_tag (pp, feature) &&
297          parse_feature_indices (pp, feature) &&
298          parse_feature_value_postfix (pp, feature) &&
299          (parse_char (pp, ',') || **pp == '\0');
300 }
301
302 static void
303 skip_one_feature (char **pp)
304 {
305   char *e;
306   e = strchr (*pp, ',');
307   if (e)
308     *pp = e + 1;
309   else
310     *pp = *pp + strlen (*pp);
311 }
312
313 static void parse_features (char *s)
314 {
315   char *p;
316
317   num_features = 0;
318   features = NULL;
319
320   if (!*s)
321     return;
322
323   /* count the features first, so we can allocate memory */
324   p = s;
325   do {
326     num_features++;
327     p = strchr (p, ',');
328     if (p)
329       p++;
330   } while (p);
331
332   features = (hb_feature_t *) calloc (num_features, sizeof (*features));
333
334   /* now do the actual parsing */
335   p = s;
336   num_features = 0;
337   while (*p) {
338     if (parse_one_feature (&p, &features[num_features]))
339       num_features++;
340     else
341       skip_one_feature (&p);
342   }
343 }
344
345
346 static cairo_glyph_t *
347 _hb_cr_text_glyphs (cairo_t *cr,
348                     const char *utf8, int len,
349                     unsigned int *pnum_glyphs)
350 {
351   cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr);
352   FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
353   hb_font_t *hb_font = hb_ft_font_create (ft_face, NULL);
354   hb_buffer_t *hb_buffer;
355   cairo_glyph_t *cairo_glyphs;
356   hb_glyph_info_t *hb_glyph;
357   hb_glyph_position_t *hb_position;
358   unsigned int num_glyphs, i;
359   hb_position_t x, y;
360
361   hb_buffer = hb_buffer_create (0);
362
363   if (direction)
364     hb_buffer_set_direction (hb_buffer, hb_direction_from_string (direction));
365   if (script)
366     hb_buffer_set_script (hb_buffer, hb_script_from_string (script));
367   if (language)
368     hb_buffer_set_language (hb_buffer, hb_language_from_string (language));
369
370   if (len < 0)
371     len = strlen (utf8);
372   hb_buffer_add_utf8 (hb_buffer, utf8, len, 0, len);
373
374   hb_shape (hb_font, hb_buffer, features, num_features);
375
376   num_glyphs = hb_buffer_get_length (hb_buffer);
377   hb_glyph = hb_buffer_get_glyph_infos (hb_buffer, NULL);
378   hb_position = hb_buffer_get_glyph_positions (hb_buffer, NULL);
379   cairo_glyphs = cairo_glyph_allocate (num_glyphs);
380   x = 0;
381   y = 0;
382   for (i = 0; i < num_glyphs; i++)
383     {
384       cairo_glyphs[i].index = hb_glyph->codepoint;
385       cairo_glyphs[i].x = ( hb_position->x_offset + x) * (1./64);
386       cairo_glyphs[i].y = (-hb_position->y_offset + y) * (1./64);
387       x +=  hb_position->x_advance;
388       y += -hb_position->y_advance;
389
390       hb_glyph++;
391       hb_position++;
392     }
393   hb_buffer_destroy (hb_buffer);
394   hb_font_destroy (hb_font);
395   cairo_ft_scaled_font_unlock_face (scaled_font);
396
397   if (pnum_glyphs)
398     *pnum_glyphs = num_glyphs;
399   return cairo_glyphs;
400 }
401
402 static cairo_t *
403 create_context (void)
404 {
405   cairo_t *cr;
406   unsigned int fr, fg, fb, fa, br, bg, bb, ba;
407
408   if (surface)
409     cairo_surface_destroy (surface);
410   if (back_pattern)
411     cairo_pattern_destroy (back_pattern);
412   if (fore_pattern)
413     cairo_pattern_destroy (fore_pattern);
414
415   br = bg = bb = ba = 255;
416   sscanf (back + (*back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
417   fr = fg = fb = 0; fa = 255;
418   sscanf (fore + (*fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
419
420   if (!annotate && ba == 255 && fa == 255 && br == bg && bg == bb && fr == fg && fg == fb) {
421     /* grayscale.  use A8 surface */
422     surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
423     cr = cairo_create (surface);
424     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
425     cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
426     cairo_paint (cr);
427     back_pattern = cairo_pattern_reference (cairo_get_source (cr));
428     cairo_set_source_rgba (cr, 1., 1., 1., fr / 255.);
429     fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
430   } else {
431     /* color.  use (A)RGB surface */
432     if (ba != 255)
433       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
434     else
435       surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
436     cr = cairo_create (surface);
437     cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
438     cairo_paint (cr);
439     back_pattern = cairo_pattern_reference (cairo_get_source (cr));
440     cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
441     fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
442   }
443
444   cairo_set_font_face (cr, cairo_face);
445
446   return cr;
447 }
448
449 static void
450 draw (void)
451 {
452   cairo_t *cr;
453   cairo_font_extents_t font_extents;
454
455   cairo_glyph_t *glyphs = NULL;
456   unsigned int num_glyphs = 0;
457
458   const char *end, *p = text;
459   double x, y;
460
461   cr= create_context ();
462
463   cairo_set_font_size (cr, font_size);
464   cairo_font_extents (cr, &font_extents);
465
466   height = 0;
467   width = 0;
468
469   x = margin_l;
470   y = margin_t;
471
472   do {
473     cairo_text_extents_t extents;
474
475     end = strchr (p, '\n');
476     if (!end)
477       end = p + strlen (p);
478
479     if (p != text)
480         y += line_space;
481
482     if (p != end) {
483       glyphs = _hb_cr_text_glyphs (cr, p, end - p, &num_glyphs);
484
485       cairo_glyph_extents (cr, glyphs, num_glyphs, &extents);
486
487       y += ceil (font_extents.ascent);
488       width = MAX (width, extents.x_advance);
489       cairo_save (cr);
490       cairo_translate (cr, x, y);
491       if (annotate) {
492         unsigned int i;
493         cairo_save (cr);
494
495         /* Draw actual glyph origins */
496         cairo_set_source_rgba (cr, 1., 0., 0., .5);
497         cairo_set_line_width (cr, 5);
498         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
499         for (i = 0; i < num_glyphs; i++) {
500           cairo_move_to (cr, glyphs[i].x, glyphs[i].y);
501           cairo_rel_line_to (cr, 0, 0);
502         }
503         cairo_stroke (cr);
504
505         cairo_restore (cr);
506       }
507       cairo_show_glyphs (cr, glyphs, num_glyphs);
508       cairo_restore (cr);
509       y += ceil (font_extents.height - ceil (font_extents.ascent));
510
511       cairo_glyph_free (glyphs);
512     }
513
514     p = end + 1;
515   } while (*end);
516
517   height = y + margin_b;
518   width += margin_l + margin_r;
519
520   cairo_destroy (cr);
521 }
522
523
524
525 int
526 main (int argc, char **argv)
527 {
528   static FT_Library ft_library;
529   static FT_Face ft_face;
530   cairo_status_t status;
531
532   setlocale (LC_ALL, "");
533
534   parse_opts (argc, argv);
535
536   FT_Init_FreeType (&ft_library);
537   if (FT_New_Face (ft_library, font_file, face_index, &ft_face)) {
538     fprintf (stderr, "Failed to open font file `%s'\n", font_file);
539     exit (1);
540   }
541   cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
542
543   draw ();
544   draw ();
545
546   status = cairo_surface_write_to_png (surface, out_file);
547   if (status != CAIRO_STATUS_SUCCESS) {
548     fprintf (stderr, "Failed to write output file `%s': %s\n",
549              out_file, cairo_status_to_string (status));
550     exit (1);
551   }
552
553   if (debug) {
554     free (features);
555
556     cairo_pattern_destroy (fore_pattern);
557     cairo_pattern_destroy (back_pattern);
558     cairo_surface_destroy (surface);
559     cairo_font_face_destroy (cairo_face);
560     cairo_debug_reset_static_data ();
561
562     FT_Done_Face (ft_face);
563     FT_Done_FreeType (ft_library);
564   }
565
566   return 0;
567 }
568
569
570 HB_END_DECLS