2 * Copyright © 2010 Behdad Esfahbod
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Behdad Esfahbod
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;
69 /* Ugh, global vars. Ugly, but does the job */
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;
80 G_GNUC_NORETURN static void
81 usage (FILE *f, int status)
83 fprintf (f, "usage: hb-view [OPTS...] font-file text\n");
87 G_GNUC_NORETURN static void
90 printf ("hb-view (harfbuzz) %s\n", HB_VERSION_STRING);
94 static void parse_features (char *s);
97 parse_opts (int argc, char **argv)
99 argv[0] = (char *) "hb-view";
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'},
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'},
122 c = getopt_long (argc, argv, "", long_options, &option_index);
137 face_index = atoi (optarg);
140 line_space = atoi (optarg);
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;
150 font_size = strtod (optarg, NULL);
153 parse_features (optarg);
183 if (optind + 2 != argc)
185 font_file = argv[optind++];
186 text = argv[optind++];
191 parse_space (char **pp)
194 #define ISSPACE(c) ((c)==' '||(c)=='\f'||(c)=='\n'||(c)=='\r'||(c)=='\t'||(c)=='\v')
195 while (c = **pp, ISSPACE (c))
201 parse_char (char **pp, char c)
213 parse_uint (char **pp, unsigned int *pv)
218 v = strtol (p, pp, 0);
229 parse_feature_value_prefix (char **pp, hb_feature_t *feature)
231 if (parse_char (pp, '-'))
234 parse_char (pp, '+');
242 parse_feature_tag (char **pp, hb_feature_t *feature)
248 #define ISALNUM(c) (('a' <= (c) && (c) <= 'z') || ('A' <= (c) && (c) <= 'Z') || ('0' <= (c) && (c) <= '9'))
249 while (c = **pp, ISALNUM(c))
257 feature->tag = hb_tag_from_string (p);
264 parse_feature_indices (char **pp, hb_feature_t *feature)
269 feature->end = (unsigned int) -1;
271 if (!parse_char (pp, '['))
274 has_start = parse_uint (pp, &feature->start);
276 if (parse_char (pp, ':')) {
277 parse_uint (pp, &feature->end);
280 feature->end = feature->start + 1;
283 return parse_char (pp, ']');
287 parse_feature_value_postfix (char **pp, hb_feature_t *feature)
289 return !parse_char (pp, '=') || parse_uint (pp, &feature->value);
294 parse_one_feature (char **pp, hb_feature_t *feature)
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');
304 skip_one_feature (char **pp)
307 e = strchr (*pp, ',');
311 *pp = *pp + strlen (*pp);
314 static void parse_features (char *s)
324 /* count the features first, so we can allocate memory */
333 features = (hb_feature_t *) calloc (num_features, sizeof (*features));
335 /* now do the actual parsing */
339 if (parse_one_feature (&p, &features[num_features]))
342 skip_one_feature (&p);
347 static cairo_glyph_t *
348 _hb_cr_text_glyphs (cairo_t *cr,
349 const char *text, int len,
350 unsigned int *pnum_glyphs)
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;
362 hb_buffer = hb_buffer_create (0);
365 hb_buffer_set_direction (hb_buffer, hb_direction_from_string (direction));
367 hb_buffer_set_script (hb_buffer, hb_script_from_string (script));
369 hb_buffer_set_language (hb_buffer, hb_language_from_string (language));
373 hb_buffer_add_utf8 (hb_buffer, text, len, 0, len);
375 hb_shape (hb_font, hb_buffer, features, num_features);
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);
383 for (i = 0; i < num_glyphs; i++)
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;
394 hb_buffer_destroy (hb_buffer);
395 hb_font_destroy (hb_font);
396 cairo_ft_scaled_font_unlock_face (scaled_font);
399 *pnum_glyphs = num_glyphs;
404 create_context (void)
407 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
410 cairo_surface_destroy (surface);
412 cairo_pattern_destroy (back_pattern);
414 cairo_pattern_destroy (fore_pattern);
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);
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.);
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));
432 /* color. use (A)RGB surface */
434 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
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.);
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));
445 cairo_set_font_face (cr, cairo_face);
454 cairo_font_extents_t font_extents;
456 cairo_glyph_t *glyphs = NULL;
457 unsigned int num_glyphs = 0;
459 const char *end, *p = text;
462 cr= create_context ();
464 cairo_set_font_size (cr, font_size);
465 cairo_font_extents (cr, &font_extents);
474 cairo_text_extents_t extents;
476 end = strchr (p, '\n');
478 end = p + strlen (p);
484 glyphs = _hb_cr_text_glyphs (cr, p, end - p, &num_glyphs);
486 cairo_glyph_extents (cr, glyphs, num_glyphs, &extents);
488 y += ceil (font_extents.ascent);
489 width = MAX (width, extents.x_advance);
491 cairo_translate (cr, x, y);
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);
508 cairo_show_glyphs (cr, glyphs, num_glyphs);
510 y += ceil (font_extents.height - ceil (font_extents.ascent));
512 cairo_glyph_free (glyphs);
518 height = y + margin_b;
519 width += margin_l + margin_r;
527 main (int argc, char **argv)
529 cairo_status_t status;
531 setlocale (LC_ALL, "");
533 parse_opts (argc, argv);
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);
540 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
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));
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 ();
561 FT_Done_Face (ft_face);
562 FT_Done_FreeType (ft_library);