Minor
[profile/ivi/org.tizen.video-player.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 FT_Library ft_library;
76 static FT_Face ft_face;
77 static cairo_font_face_t *cairo_face;
78
79
80 G_GNUC_NORETURN static void
81 usage (FILE *f, int status)
82 {
83   fprintf (f, "usage: hb-view [OPTS...] font-file text\n");
84   exit (status);
85 }
86
87 G_GNUC_NORETURN static void
88 version (void)
89 {
90   printf ("hb-view (harfbuzz) %s\n", HB_VERSION_STRING);
91   exit (0);
92 }
93
94 static void parse_features (char *s);
95
96 static void
97 parse_opts (int argc, char **argv)
98 {
99   argv[0] = (char *) "hb-view";
100   while (1)
101     {
102       int option_index = 0, c;
103       static struct option long_options[] = {
104         {"annotate", 0, &annotate, TRUE},
105         {"background", 1, 0, 'B'},
106         {"debug", 0, &debug, TRUE},
107         {"direction", 1, 0, 'd'},
108         {"features", 1, 0, 'f'},
109         {"font-size", 1, 0, 's'},
110         {"face-index", 1, 0, 'i'},
111         {"foreground", 1, 0, 'F'},
112         {"help", 0, 0, 'h'},
113         {"language", 1, 0, 'L'},
114         {"line-space", 1, 0, 'l'},
115         {"margin", 1, 0, 'm'},
116         {"output", 1, 0, 'o'},
117         {"script", 1, 0, 'S'},
118         {"version", 0, 0, 'v'},
119         {0, 0, 0, 0}
120       };
121
122       c = getopt_long (argc, argv, "", long_options, &option_index);
123       if (c == -1)
124         break;
125
126       switch (c)
127         {
128         case 0:
129           break;
130         case 'h':
131           usage (stdout, 0);
132           break;
133         case 'v':
134           version ();
135           break;
136         case 'i':
137           face_index = atoi (optarg);
138           break;
139         case 'l':
140           line_space = atoi (optarg);
141           break;
142         case 'm':
143           switch (sscanf (optarg, "%d %d %d %d", &margin_t, &margin_r, &margin_b, &margin_l)) {
144             case 1: margin_r = margin_t;
145             case 2: margin_b = margin_t;
146             case 3: margin_l = margin_r;
147           }
148           break;
149         case 's':
150           font_size = strtod (optarg, NULL);
151           break;
152         case 'f':
153           parse_features (optarg);
154           break;
155         case 'F':
156           fore = optarg;
157           break;
158         case 'B':
159           back = optarg;
160           break;
161         case 't':
162           text = optarg;
163           break;
164         case 'd':
165           direction = optarg;
166           break;
167         case 'S':
168           script = optarg;
169           break;
170         case 'L':
171           language = optarg;
172           break;
173         case 'o':
174           out_file = optarg;
175           break;
176         case '?':
177           usage (stdout, 1);
178           break;
179         default:
180           break;
181         }
182     }
183   if (optind + 2 != argc)
184     usage (stderr, 1);
185   font_file = argv[optind++];
186   text = argv[optind++];
187 }
188
189
190 static void
191 parse_space (char **pp)
192 {
193   char c;
194 #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
195   while (c = **pp, ISSPACE (c))
196     (*pp)++;
197 #undef ISSPACE
198 }
199
200 static hb_bool_t
201 parse_char (char **pp, char c)
202 {
203   parse_space (pp);
204
205   if (**pp != c)
206     return FALSE;
207
208   (*pp)++;
209   return TRUE;
210 }
211
212 static hb_bool_t
213 parse_uint (char **pp, unsigned int *pv)
214 {
215   char *p = *pp;
216   unsigned int v;
217
218   v = strtol (p, pp, 0);
219
220   if (p == *pp)
221     return FALSE;
222
223   *pv = v;
224   return TRUE;
225 }
226
227
228 static hb_bool_t
229 parse_feature_value_prefix (char **pp, hb_feature_t *feature)
230 {
231   if (parse_char (pp, '-'))
232     feature->value = 0;
233   else {
234     parse_char (pp, '+');
235     feature->value = 1;
236   }
237
238   return TRUE;
239 }
240
241 static hb_bool_t
242 parse_feature_tag (char **pp, hb_feature_t *feature)
243 {
244   char *p = *pp, c;
245
246   parse_space (pp);
247
248 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
249   while (c = **pp, ISALNUM(c))
250     (*pp)++;
251 #undef ISALNUM
252
253   if (p == *pp)
254     return FALSE;
255
256   **pp = '\0';
257   feature->tag = hb_tag_from_string (p);
258   **pp = c;
259
260   return TRUE;
261 }
262
263 static hb_bool_t
264 parse_feature_indices (char **pp, hb_feature_t *feature)
265 {
266   hb_bool_t has_start;
267
268   feature->start = 0;
269   feature->end = (unsigned int) -1;
270
271   if (!parse_char (pp, '['))
272     return TRUE;
273
274   has_start = parse_uint (pp, &feature->start);
275
276   if (parse_char (pp, ':')) {
277     parse_uint (pp, &feature->end);
278   } else {
279     if (has_start)
280       feature->end = feature->start + 1;
281   }
282
283   return parse_char (pp, ']');
284 }
285
286 static hb_bool_t
287 parse_feature_value_postfix (char **pp, hb_feature_t *feature)
288 {
289   return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
290 }
291
292
293 static hb_bool_t
294 parse_one_feature (char **pp, hb_feature_t *feature)
295 {
296   return parse_feature_value_prefix (pp, feature) &&
297          parse_feature_tag (pp, feature) &&
298          parse_feature_indices (pp, feature) &&
299          parse_feature_value_postfix (pp, feature) &&
300          (parse_char (pp, ',') || **pp == '\0');
301 }
302
303 static void
304 skip_one_feature (char **pp)
305 {
306   char *e;
307   e = strchr (*pp, ',');
308   if (e)
309     *pp = e + 1;
310   else
311     *pp = *pp + strlen (*pp);
312 }
313
314 static void parse_features (char *s)
315 {
316   char *p;
317
318   num_features = 0;
319   features = NULL;
320
321   if (!*s)
322     return;
323
324   /* count the features first, so we can allocate memory */
325   p = s;
326   do {
327     num_features++;
328     p = strchr (p, ',');
329     if (p)
330       p++;
331   } while (p);
332
333   features = (hb_feature_t *) calloc (num_features, sizeof (*features));
334
335   /* now do the actual parsing */
336   p = s;
337   num_features = 0;
338   while (*p) {
339     if (parse_one_feature (&p, &features[num_features]))
340       num_features++;
341     else
342       skip_one_feature (&p);
343   }
344 }
345
346
347 static cairo_glyph_t *
348 _hb_cr_text_glyphs (cairo_t *cr,
349                     const char *text, int len,
350                     unsigned int *pnum_glyphs)
351 {
352   cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr);
353   FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
354   hb_font_t *hb_font = hb_ft_font_create (ft_face, NULL);
355   hb_buffer_t *hb_buffer;
356   cairo_glyph_t *cairo_glyphs;
357   hb_glyph_info_t *hb_glyph;
358   hb_glyph_position_t *hb_position;
359   unsigned int num_glyphs, i;
360   hb_position_t x, y;
361
362   hb_buffer = hb_buffer_create (0);
363
364   if (direction)
365     hb_buffer_set_direction (hb_buffer, hb_direction_from_string (direction));
366   if (script)
367     hb_buffer_set_script (hb_buffer, hb_script_from_string (script));
368   if (language)
369     hb_buffer_set_language (hb_buffer, hb_language_from_string (language));
370
371   if (len < 0)
372     len = strlen (text);
373   hb_buffer_add_utf8 (hb_buffer, text, len, 0, len);
374
375   hb_shape (hb_font, hb_buffer, features, num_features);
376
377   num_glyphs = hb_buffer_get_length (hb_buffer);
378   hb_glyph = hb_buffer_get_glyph_infos (hb_buffer, NULL);
379   hb_position = hb_buffer_get_glyph_positions (hb_buffer, NULL);
380   cairo_glyphs = cairo_glyph_allocate (num_glyphs);
381   x = 0;
382   y = 0;
383   for (i = 0; i < num_glyphs; i++)
384     {
385       cairo_glyphs[i].index = hb_glyph->codepoint;
386       cairo_glyphs[i].x = ( hb_position->x_offset + x) * (1./64);
387       cairo_glyphs[i].y = (-hb_position->y_offset + y) * (1./64);
388       x +=  hb_position->x_advance;
389       y += -hb_position->y_advance;
390
391       hb_glyph++;
392       hb_position++;
393     }
394   hb_buffer_destroy (hb_buffer);
395   hb_font_destroy (hb_font);
396   cairo_ft_scaled_font_unlock_face (scaled_font);
397
398   if (pnum_glyphs)
399     *pnum_glyphs = num_glyphs;
400   return cairo_glyphs;
401 }
402
403 static cairo_t *
404 create_context (void)
405 {
406   cairo_t *cr;
407   unsigned int fr, fg, fb, fa, br, bg, bb, ba;
408
409   if (surface)
410     cairo_surface_destroy (surface);
411   if (back_pattern)
412     cairo_pattern_destroy (back_pattern);
413   if (fore_pattern)
414     cairo_pattern_destroy (fore_pattern);
415
416   br = bg = bb = ba = 255;
417   sscanf (back + (*back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
418   fr = fg = fb = 0; fa = 255;
419   sscanf (fore + (*fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
420
421   if (!annotate && ba == 255 && fa == 255 && br == bg && bg == bb && fr == fg && fg == fb) {
422     /* grayscale.  use A8 surface */
423     surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
424     cr = cairo_create (surface);
425     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
426     cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
427     cairo_paint (cr);
428     back_pattern = cairo_pattern_reference (cairo_get_source (cr));
429     cairo_set_source_rgba (cr, 1., 1., 1., fr / 255.);
430     fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
431   } else {
432     /* color.  use (A)RGB surface */
433     if (ba != 255)
434       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
435     else
436       surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
437     cr = cairo_create (surface);
438     cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
439     cairo_paint (cr);
440     back_pattern = cairo_pattern_reference (cairo_get_source (cr));
441     cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
442     fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
443   }
444
445   cairo_set_font_face (cr, cairo_face);
446
447   return cr;
448 }
449
450 static void
451 draw (void)
452 {
453   cairo_t *cr;
454   cairo_font_extents_t font_extents;
455
456   cairo_glyph_t *glyphs = NULL;
457   unsigned int num_glyphs = 0;
458
459   const char *end, *p = text;
460   double x, y;
461
462   cr= create_context ();
463
464   cairo_set_font_size (cr, font_size);
465   cairo_font_extents (cr, &font_extents);
466
467   height = 0;
468   width = 0;
469
470   x = margin_l;
471   y = margin_t;
472
473   do {
474     cairo_text_extents_t extents;
475
476     end = strchr (p, '\n');
477     if (!end)
478       end = p + strlen (p);
479
480     if (p != text)
481         y += line_space;
482
483     if (p != end) {
484       glyphs = _hb_cr_text_glyphs (cr, p, end - p, &num_glyphs);
485
486       cairo_glyph_extents (cr, glyphs, num_glyphs, &extents);
487
488       y += ceil (font_extents.ascent);
489       width = MAX (width, extents.x_advance);
490       cairo_save (cr);
491       cairo_translate (cr, x, y);
492       if (annotate) {
493         unsigned int i;
494         cairo_save (cr);
495
496         /* Draw actual glyph origins */
497         cairo_set_source_rgba (cr, 1., 0., 0., .5);
498         cairo_set_line_width (cr, 5);
499         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
500         for (i = 0; i < num_glyphs; i++) {
501           cairo_move_to (cr, glyphs[i].x, glyphs[i].y);
502           cairo_rel_line_to (cr, 0, 0);
503         }
504         cairo_stroke (cr);
505
506         cairo_restore (cr);
507       }
508       cairo_show_glyphs (cr, glyphs, num_glyphs);
509       cairo_restore (cr);
510       y += ceil (font_extents.height - ceil (font_extents.ascent));
511
512       cairo_glyph_free (glyphs);
513     }
514
515     p = end + 1;
516   } while (*end);
517
518   height = y + margin_b;
519   width += margin_l + margin_r;
520
521   cairo_destroy (cr);
522 }
523
524
525
526 int
527 main (int argc, char **argv)
528 {
529   cairo_status_t status;
530
531   setlocale (LC_ALL, "");
532
533   parse_opts (argc, argv);
534
535   FT_Init_FreeType (&ft_library);
536   if (FT_New_Face (ft_library, font_file, face_index, &ft_face)) {
537     fprintf (stderr, "Failed to open font file `%s'\n", font_file);
538     exit (1);
539   }
540   cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
541
542   draw ();
543   draw ();
544
545   status = cairo_surface_write_to_png (surface, out_file);
546   if (status != CAIRO_STATUS_SUCCESS) {
547     fprintf (stderr, "Failed to write output file `%s': %s\n",
548              out_file, cairo_status_to_string (status));
549     exit (1);
550   }
551
552   if (debug) {
553     free (features);
554
555     cairo_pattern_destroy (fore_pattern);
556     cairo_pattern_destroy (back_pattern);
557     cairo_surface_destroy (surface);
558     cairo_font_face_destroy (cairo_face);
559     cairo_debug_reset_static_data ();
560
561     FT_Done_Face (ft_face);
562     FT_Done_FreeType (ft_library);
563   }
564
565   return 0;
566 }
567
568
569 HB_END_DECLS