hb-view.c 8.9 KB
Newer Older
B
Behdad Esfahbod 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Copyright (C) 2010  Behdad Esfahbod
 * Copyright (C) 2011  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <glib.h>
#include <hb-glib.h>

#include <cairo-ft.h>
#include <hb-ft.h>

/* Controlled by cmd-line options */
static int margin_t = 10;
static int margin_b = 10;
static int margin_l = 10;
static int margin_r = 10;
static int line_space = 0;
static double font_size = 18;
static const char *fore = "#000000";
53
static const char *back = "#ffffff";
B
Behdad Esfahbod 已提交
54 55 56 57
static const char *text = NULL;
static const char *font_file = NULL;
static const char *out_file = "/dev/stdout";
static const char *language = NULL;
58
static const char *script = NULL;
B
Behdad Esfahbod 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

/* Ugh, global vars.  Ugly, but does the job */
static int width = 0;
static int height = 0;
static cairo_surface_t *surface = NULL;
static cairo_pattern_t *fore_pattern = NULL;
static cairo_pattern_t *back_pattern = NULL;
static FT_Library ft_library;
static FT_Face ft_face;
static cairo_font_face_t *cairo_face;


G_GNUC_NORETURN static void
usage (FILE *f, int status)
{
  fprintf (f, "usage: hb-view [OPTS...] font-file text\n");
  exit (status);
}

G_GNUC_NORETURN static void
version (void)
{
  printf ("hb-view (harfbuzz) %s\n", PACKAGE_VERSION);
  exit (0);
}

static void
parse_opts (int argc, char **argv)
{
  argv[0] = (char *) "hb-view";
  while (1)
    {
      int option_index = 0, c;
      static struct option long_options[] = {
	{"help", 0, 0, 'h'},
	{"version", 0, 0, 'v'},
	{"margin", 1, 0, 'm'},
	{"line-space", 1, 0, 'l'},
	{"font-size", 1, 0, 's'},
	{"foreground", 1, 0, 'F'},
	{"background", 1, 0, 'B'},
	{"language", 1, 0, 'L'},
101
	{"script", 1, 0, 'S'},
B
Behdad Esfahbod 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	{"output", 1, 0, 'o'},
	{0, 0, 0, 0}
      };

      c = getopt_long (argc, argv, "", long_options, &option_index);
      if (c == -1)
	break;

      switch (c)
	{
	case 0:
	  break;
	case 'h':
	  usage (stdout, 0);
	  break;
	case 'v':
	  version ();
	  break;
	case 'l':
	  line_space = atoi (optarg);
	  break;
	case 'm':
	  switch (sscanf (optarg, "%d %d %d %d", &margin_t, &margin_r, &margin_b, &margin_l)) {
	    case 1: margin_r = margin_t;
	    case 2: margin_b = margin_t;
	    case 3: margin_l = margin_r;
	  }
	  break;
	case 's':
	  font_size = strtod (optarg, NULL);
	  break;
	case 'f':
	  font_file = optarg;
	  break;
	case 'F':
	  fore = optarg;
	  break;
	case 'B':
	  back = optarg;
	  break;
	case 't':
	  text = optarg;
	  break;
	case 'L':
	  language = optarg;
	  break;
148 149 150
	case 'S':
	  script = optarg;
	  break;
B
Behdad Esfahbod 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	case 'o':
	  out_file = optarg;
	  break;
	case '?':
	  usage (stdout, 1);
	  break;
	default:
	  break;
	}
    }
  if (optind + 2 != argc)
    usage (stderr, 1);
  font_file = argv[optind++];
  text = argv[optind++];
}



static cairo_glyph_t *
_hb_cr_text_glyphs (cairo_t *cr,
		    const char *text, int len,
		    unsigned int *pnum_glyphs)
{
  cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr);
  FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
  hb_face_t *hb_face = hb_ft_face_create (ft_face, NULL);
  hb_font_t *hb_font = hb_ft_font_create (ft_face, NULL);
  hb_buffer_t *hb_buffer;
  cairo_glyph_t *cairo_glyphs;
  hb_glyph_info_t *hb_glyph;
  hb_glyph_position_t *hb_position;
  unsigned int num_glyphs, i;
  hb_position_t x;

  if (len < 0)
    len = strlen (text);
  hb_buffer = hb_buffer_create (len);

  hb_buffer_set_unicode_funcs (hb_buffer, hb_glib_get_unicode_funcs ());

  hb_buffer_add_utf8 (hb_buffer, text, len, 0, len);
192 193 194 195
  if (script)
    hb_buffer_set_script (hb_buffer, hb_script_from_iso15924_tag (hb_tag_from_string (script)));
  else
    hb_buffer_set_script (hb_buffer, HB_SCRIPT_INVALID);
196
  hb_buffer_set_direction (hb_buffer, HB_DIRECTION_INVALID);
B
Behdad Esfahbod 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  hb_buffer_set_language (hb_buffer, hb_language_from_string (language));

  hb_shape (hb_font, hb_face, hb_buffer, NULL, 0);

  num_glyphs = hb_buffer_get_length (hb_buffer);
  hb_glyph = hb_buffer_get_glyph_infos (hb_buffer);
  hb_position = hb_buffer_get_glyph_positions (hb_buffer);
  cairo_glyphs = cairo_glyph_allocate (num_glyphs + 1);
  x = 0;
  for (i = 0; i < num_glyphs; i++)
    {
      cairo_glyphs[i].index = hb_glyph->codepoint;
      cairo_glyphs[i].x = (hb_position->x_offset + x) * (1./64);
      cairo_glyphs[i].y = -(hb_position->y_offset)    * (1./64);
      x += hb_position->x_advance;

      hb_glyph++;
      hb_position++;
    }
  cairo_glyphs[i].x = x * (1./64);
  hb_buffer_destroy (hb_buffer);
  hb_font_destroy (hb_font);
  hb_face_destroy (hb_face);
  cairo_ft_scaled_font_unlock_face (scaled_font);

  if (pnum_glyphs)
    *pnum_glyphs = num_glyphs;
  return cairo_glyphs;
}

static cairo_t *
create_context (void)
{
  cairo_t *cr;
  unsigned int fr, fg, fb, fa, br, bg, bb, ba;

  if (surface)
    cairo_surface_destroy (surface);
  if (back_pattern)
    cairo_pattern_destroy (back_pattern);
  if (fore_pattern)
    cairo_pattern_destroy (fore_pattern);

  br = bg = bb = ba = 255;
  sscanf (back + (*back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
  fr = fg = fb = 0; fa = 255;
  sscanf (fore + (*fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);

  if (ba == 255 && fa == 255 && br == bg && bg == bb && fr == fg && fg == fb) {
    /* grayscale.  use A8 surface */
    surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
    cr = cairo_create (surface);
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
    cairo_paint (cr);
    back_pattern = cairo_pattern_reference (cairo_get_source (cr));
    cairo_set_source_rgba (cr, 1., 1., 1., fr / 255.);
    fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
  } else {
    /* color.  use (A)RGB surface */
    if (ba != 255)
      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    else
      surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
    cr = cairo_create (surface);
    cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
    cairo_paint (cr);
    back_pattern = cairo_pattern_reference (cairo_get_source (cr));
    cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
    fore_pattern = cairo_pattern_reference (cairo_get_source (cr));
  }

  cairo_set_font_face (cr, cairo_face);

  return cr;
}

static void
draw (void)
{
  cairo_t *cr;
  cairo_font_extents_t font_extents;

  cairo_glyph_t *glyphs = NULL;
  unsigned int num_glyphs = 0;

  const char *end, *p = text;
  double x, y;

  cr= create_context ();

  cairo_set_font_size (cr, font_size);
  cairo_font_extents (cr, &font_extents);

  height = 0;
  width = 0;

  x = margin_l;
  y = margin_t;

  do {
    cairo_text_extents_t extents;

    end = strchr (p, '\n');
    if (!end)
      end = p + strlen (p);

    if (p != text)
	y += line_space;

    if (p != end) {
      glyphs = _hb_cr_text_glyphs (cr, p, end - p, &num_glyphs);
      cairo_glyph_extents (cr, glyphs, num_glyphs, &extents);

      y += ceil (font_extents.ascent);
      width = MAX (width, extents.x_advance);
      cairo_save (cr);
      cairo_translate (cr, x, y);
      cairo_show_glyphs (cr, glyphs, num_glyphs);
      cairo_restore (cr);
      y += ceil (font_extents.height - ceil (font_extents.ascent));
      cairo_glyph_free (glyphs);
    }

    p = end + 1;
  } while (*end);

  height = y + margin_b;
  width += margin_l + margin_r;

  cairo_destroy (cr);
}



int
main (int argc, char **argv)
{
335 336
  cairo_status_t status;

B
Behdad Esfahbod 已提交
337 338 339 340 341 342 343 344 345 346 347 348
  parse_opts (argc, argv);

  FT_Init_FreeType (&ft_library);
  if (FT_New_Face (ft_library, font_file, 0, &ft_face)) {
    fprintf (stderr, "Failed to open font file `%s'\n", font_file);
    exit (1);
  }
  cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);

  draw ();
  draw ();

349 350 351 352 353 354
  status = cairo_surface_write_to_png (surface, out_file);
  if (status != CAIRO_STATUS_SUCCESS) {
    fprintf (stderr, "Failed to write output file `%s': %s\n",
	     out_file, cairo_status_to_string (status));
    exit (1);
  }
B
Behdad Esfahbod 已提交
355 356 357

  return 0;
}