viewer-render.c 26.3 KB
Newer Older
1
/* viewer-render.c: Common code for rendering in viewers
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Copyright (C) 1999, 2004 Red Hat Software
 * Copyright (C) 2001 Sun Microsystems
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
21
#include "config.h"
22
23
24
25
26
27
28
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

29
#include <glib.h>
30
#include <glib/gprintf.h>
31
32
#include <pango/pango.h>

33
#include "viewer-render.h"
34

35
gboolean opt_display = TRUE;
36
int opt_dpi = 96;
37
gboolean opt_pixels = FALSE;
38
gboolean opt_pango_units = FALSE;
39
const char *opt_font = "";
40
41
gboolean opt_header = FALSE;
const char *opt_output = NULL;
42
43
44
45
int opt_margin_t = 10;
int opt_margin_r = 10;
int opt_margin_b = 10;
int opt_margin_l = 10;
46
47
int opt_markup = FALSE;
gboolean opt_rtl = FALSE;
48
double opt_rotate = 0;
49
50
51
52
gboolean opt_auto_dir = TRUE;
const char *opt_text = NULL;
gboolean opt_waterfall = FALSE;
int opt_width = -1;
53
int opt_height = -1;
54
int opt_indent = 0;
55
int opt_spacing = 0;
56
double opt_line_spacing = -1.0;
Mathias Hasselmann's avatar
Mathias Hasselmann committed
57
gboolean opt_justify = 0;
Matthias Clasen's avatar
Matthias Clasen committed
58
gboolean opt_justify_last_line = 0;
59
int opt_runs = 1;
60
PangoAlignment opt_align = PANGO_ALIGN_LEFT;
61
PangoEllipsizeMode opt_ellipsize = PANGO_ELLIPSIZE_NONE;
62
PangoGravity opt_gravity = PANGO_GRAVITY_SOUTH;
63
PangoGravityHint opt_gravity_hint = PANGO_GRAVITY_HINT_NATURAL;
64
HintMode opt_hinting = HINT_DEFAULT;
Matthias Clasen's avatar
Matthias Clasen committed
65
66
67
HintMetrics opt_hint_metrics = HINT_METRICS_DEFAULT;
SubpixelOrder opt_subpixel_order = SUBPIXEL_DEFAULT;
Antialias opt_antialias = ANTIALIAS_DEFAULT;
68
gboolean opt_subpixel_positions = FALSE;
69
70
PangoWrapMode opt_wrap = PANGO_WRAP_WORD_CHAR;
gboolean opt_wrap_set = FALSE;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
71
static const char *opt_pangorc = NULL; /* Unused */
72
const PangoViewer *opt_viewer = NULL;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
73
const char *opt_language = NULL;
74
gboolean opt_single_par = FALSE;
75
76
77
78
79
PangoColor opt_fg_color = {0, 0, 0};
guint16 opt_fg_alpha = 65535;
gboolean opt_bg_set = FALSE;
PangoColor opt_bg_color = {65535, 65535, 65535};
guint16 opt_bg_alpha = 65535;
80
81
82
83
84
85
86
87

/* Text (or markup) to render */
static char *text;

void
fail (const char *format, ...)
{
  const char *msg;
88

89
90
91
  va_list vap;
  va_start (vap, format);
  msg = g_strdup_vprintf (format, vap);
92
  g_printerr ("%s: %s\n", g_get_prgname (), msg);
93

94
95
96
97
98
99
100
101
102
  exit (1);
}

static PangoLayout *
make_layout(PangoContext *context,
	    const char   *text,
	    double        size)
{
  static PangoFontDescription *font_description;
103
  PangoAlignment align;
104
105
106
107
108
109
110
111
112
113
  PangoLayout *layout;

  layout = pango_layout_new (context);
  if (opt_markup)
    pango_layout_set_markup (layout, text, -1);
  else
    pango_layout_set_text (layout, text, -1);

  pango_layout_set_auto_dir (layout, opt_auto_dir);
  pango_layout_set_ellipsize (layout, opt_ellipsize);
Mathias Hasselmann's avatar
Mathias Hasselmann committed
114
  pango_layout_set_justify (layout, opt_justify);
Matthias Clasen's avatar
Matthias Clasen committed
115
  pango_layout_set_justify_last_line (layout, opt_justify_last_line);
116
  pango_layout_set_single_paragraph_mode (layout, opt_single_par);
117
  pango_layout_set_wrap (layout, opt_wrap);
118

119
  font_description = pango_font_description_from_string (opt_font);
120
121
  if (size > 0)
    pango_font_description_set_size (font_description, size * PANGO_SCALE);
122

Matthias Clasen's avatar
Matthias Clasen committed
123
  if (opt_width >= 0)
124
125
126
127
128
129
    {
      if (opt_pango_units)
        pango_layout_set_width (layout, opt_width);
      else
        pango_layout_set_width (layout, (opt_width * opt_dpi * PANGO_SCALE + 36) / 72);
    }
130

Matthias Clasen's avatar
Matthias Clasen committed
131
  if (opt_height >= 0)
132
133
134
135
136
137
    {
      if (opt_pango_units)
        pango_layout_set_width (layout, opt_height);
      else
        pango_layout_set_height (layout, (opt_height * opt_dpi * PANGO_SCALE + 36) / 72);
    }
138
139
  else
    pango_layout_set_height (layout, opt_height);
140
141

  if (opt_indent != 0)
142
143
144
145
146
147
    {
      if (opt_pango_units)
        pango_layout_set_indent (layout, opt_indent);
      else
        pango_layout_set_indent (layout, (opt_indent * opt_dpi * PANGO_SCALE + 36) / 72);
    }
148

149
  if (opt_spacing != 0)
150
    {
151
152
153
154
      if (opt_pango_units)
        pango_layout_set_spacing (layout, opt_spacing);
      else
        pango_layout_set_spacing (layout, (opt_spacing * opt_dpi * PANGO_SCALE + 36) / 72);
155
156
157
      pango_layout_set_line_spacing (layout, 0.0);
    }
  if (opt_line_spacing >= 0.0)
158
159
    pango_layout_set_line_spacing (layout, (float)opt_line_spacing);

160
161
162
163
164
165
166
167
168
169
  align = opt_align;
  if (align != PANGO_ALIGN_CENTER &&
      pango_context_get_base_dir (context) != PANGO_DIRECTION_LTR) {
    /* pango reverses left and right if base dir ir rtl.  so we should
     * reverse to cancel that.  unfortunately it also does that for
     * rtl paragraphs, so we cannot really get left/right.  all we get
     * is default/other-side. */
    align = PANGO_ALIGN_LEFT + PANGO_ALIGN_RIGHT - align;
  }
  pango_layout_set_alignment (layout, align);
170

171
172
173
174
175
176
177
178
179
180
  pango_layout_set_font_description (layout, font_description);

  pango_font_description_free (font_description);

  return layout;
}

gchar *
get_options_string (void)
{
181
  PangoFontDescription *font_description = pango_font_description_from_string (opt_font);
182
183
184
185
186
187
188
  gchar *font_name;
  gchar *result;

  if (opt_waterfall)
    pango_font_description_unset_fields (font_description, PANGO_FONT_MASK_SIZE);

  font_name = pango_font_description_to_string (font_description);
189
  result = g_strdup_printf ("%s: %s (%d dpi)", opt_viewer->name, font_name, opt_dpi);
190
191
192
193
194
195
196
  pango_font_description_free (font_description);
  g_free (font_name);

  return result;
}

static void
197
output_body (PangoLayout    *layout,
198
	     RenderCallback  render_cb,
199
	     gpointer        cb_context,
200
201
	     gpointer        cb_data,
	     int            *width,
202
203
	     int            *height,
	     gboolean        supports_matrix)
204
205
206
{
  PangoRectangle logical_rect;
  int size, start_size, end_size, increment;
207
  int x = 0, y = 0;
208

209
210
211
212
  if (!supports_matrix)
    {
      const PangoMatrix* matrix;
      const PangoMatrix identity = PANGO_MATRIX_INIT;
213
      PangoContext *context = pango_layout_get_context (layout);
214
215
216
217
218
219
220
      matrix = pango_context_get_matrix (context);
      if (matrix)
	{
	  x += matrix->x0;
	  y += matrix->y0;
	}
      pango_context_set_matrix (context, &identity);
221
      pango_layout_context_changed (layout);
222
    }
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
  if (opt_waterfall)
    {
      start_size = 8;
      end_size = 48;
      increment = 4;
    }
  else
    {
      start_size = end_size = -1;
      increment = 1;
    }

  *width = 0;
  *height = 0;
238

239
240
  for (size = start_size; size <= end_size; size += increment)
    {
241
242
243
244
245
246
247
248
      if (size > 0)
        {
	  PangoFontDescription *desc = pango_font_description_copy (pango_layout_get_font_description (layout));
	  pango_font_description_set_size (desc, size * PANGO_SCALE);
	  pango_layout_set_font_description (layout, desc);
	  pango_font_description_free (desc);
	}

249
      pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
250
251

      if (render_cb)
252
	(*render_cb) (layout, x, y+*height, cb_context, cb_data);
253

254
255
256
257
258
      *width = MAX (*width, 
		    MAX (logical_rect.x + logical_rect.width,
			 PANGO_PIXELS (pango_layout_get_width (layout))));
      *height +=    MAX (logical_rect.y + logical_rect.height,
			 PANGO_PIXELS (pango_layout_get_height (layout)));
259
260
261
262
263
264
    }
}

static void
set_transform (PangoContext     *context,
	       TransformCallback transform_cb,
265
	       gpointer          cb_context,
266
267
268
	       gpointer          cb_data,
	       PangoMatrix      *matrix)
{
269
  pango_context_set_matrix (context, matrix);
270
  if (transform_cb)
271
    (*transform_cb) (context, matrix, cb_context, cb_data);
272
273
274
275
276
277
}

void
do_output (PangoContext     *context,
	   RenderCallback    render_cb,
	   TransformCallback transform_cb,
278
	   gpointer          cb_context,
279
280
	   gpointer          cb_data,
	   int              *width_out,
281
	   int              *height_out)
282
283
{
  PangoLayout *layout;
284
  PangoRectangle rect;
285
  PangoMatrix matrix = PANGO_MATRIX_INIT;
286
  PangoMatrix *orig_matrix;
287
  gboolean supports_matrix;
288
  int rotated_width, rotated_height;
289
290
  int x = opt_margin_l;
  int y = opt_margin_t;
291
  int width, height;
292

293
294
295
  width = 0;
  height = 0;

296
297
298
299
300
301
302
303
304
  orig_matrix = pango_matrix_copy (pango_context_get_matrix (context));
  /* If the backend sets an all-zero matrix on the context,
   * means that it doesn't support transformations.
   */
  supports_matrix = !orig_matrix ||
		    (orig_matrix->xx != 0. || orig_matrix->xy != 0. ||
		     orig_matrix->yx != 0. || orig_matrix->yy != 0. ||
		     orig_matrix->x0 != 0. || orig_matrix->y0 != 0.);

305
306
  set_transform (context, transform_cb, cb_context, cb_data, NULL);

Behdad Esfahbod's avatar
Behdad Esfahbod committed
307
308
309
  pango_context_set_language (context,
			      opt_language ? pango_language_from_string (opt_language)
					   : pango_language_get_default ());
310
311
  pango_context_set_base_dir (context,
			      opt_rtl ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
312

313
314
315
  if (opt_header)
    {
      char *options_string = get_options_string ();
316
      pango_context_set_base_gravity (context, PANGO_GRAVITY_SOUTH);
317
      layout = make_layout (context, options_string, 10);
318
      pango_layout_get_extents (layout, NULL, &rect);
319

320
321
      width = MAX (width, PANGO_PIXELS (rect.width));
      height += PANGO_PIXELS (rect.height);
322

323
      if (render_cb)
324
325
	(*render_cb) (layout, x, y, cb_context, cb_data);

326
      y += PANGO_PIXELS (rect.height);
327

328
329
330
331
      g_object_unref (layout);
      g_free (options_string);
    }

332
333
334
335
336
  if (opt_rotate != 0)
    {
      if (supports_matrix)
	pango_matrix_rotate (&matrix, opt_rotate);
      else
337
	g_printerr ("The backend does not support rotated text\n");
338
    }
339

340
  pango_context_set_base_gravity (context, opt_gravity);
341
  pango_context_set_gravity_hint (context, opt_gravity_hint);
342

343
344
  layout = make_layout (context, text, -1);

345
346
  set_transform (context, transform_cb, cb_context, cb_data, &matrix);

347
  output_body (layout,
348
349
350
	       NULL, NULL, NULL,
	       &rotated_width, &rotated_height,
	       supports_matrix);
351

352
353
354
  rect.x = rect.y = 0;
  rect.width = rotated_width;
  rect.height = rotated_height;
355

356
  pango_matrix_transform_pixel_rectangle (&matrix, &rect);
357

358
359
  matrix.x0 = x - rect.x;
  matrix.y0 = y - rect.y;
360

361
362
  set_transform (context, transform_cb, cb_context, cb_data, &matrix);

363
  if (render_cb)
364
    output_body (layout,
365
366
367
		 render_cb, cb_context, cb_data,
		 &rotated_width, &rotated_height,
		 supports_matrix);
368

369
370
  width = MAX (width, rect.width);
  height += rect.height;
371

372
373
  width += opt_margin_l + opt_margin_r;
  height += opt_margin_t + opt_margin_b;
374
375
376
377
378

  if (width_out)
    *width_out = width;
  if (height_out)
    *height_out = height;
379
380
381

  pango_context_set_matrix (context, orig_matrix);
  pango_matrix_free (orig_matrix);
382
  g_object_unref (layout);
383
384
385
}

static gboolean
386
parse_enum (GType       type,
387
388
389
	    int        *value,
	    const char *name,
	    const char *arg,
390
	    gpointer    data G_GNUC_UNUSED,
391
	    GError **error)
392
{
393
394
  char *possible_values = NULL;
  gboolean ret;
395

Matthias Clasen's avatar
Matthias Clasen committed
396
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
397
398
399
400
401
  ret = pango_parse_enum (type,
			  arg,
			  value,
			  FALSE,
			  &possible_values);
Matthias Clasen's avatar
Matthias Clasen committed
402
G_GNUC_END_IGNORE_DEPRECATIONS
403

404
  if (!ret && error)
405
    {
406
      g_set_error(error,
407
		  G_OPTION_ERROR,
408
409
410
411
		  G_OPTION_ERROR_BAD_VALUE,
		  "Argument for %s must be one of %s",
		  name,
		  possible_values);
412
413
    }

414
415
  g_free (possible_values);

416
417
418
  return ret;
}

419
420
421
422
423
424
static gboolean
parse_align (const char *name,
	     const char *arg,
	     gpointer    data,
	     GError **error)
{
425
  return parse_enum (PANGO_TYPE_ALIGNMENT, (int*)(void*)&opt_align,
426
427
428
		     name, arg, data, error);
}

429
430
431
432
433
434
static gboolean
parse_ellipsis (const char *name,
		const char *arg,
		gpointer    data,
		GError **error)
{
435
  return parse_enum (PANGO_TYPE_ELLIPSIZE_MODE, (int*)(void*)&opt_ellipsize,
436
437
438
		     name, arg, data, error);
}

Behdad Esfahbod's avatar
Behdad Esfahbod committed
439
440
441
442
443
444
static gboolean
parse_gravity (const char *name,
	       const char *arg,
	       gpointer    data,
	       GError **error)
{
445
  return parse_enum (PANGO_TYPE_GRAVITY, (int*)(void*)&opt_gravity,
446
		     name, arg, data, error);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
447
448
}

449
450
451
452
453
454
static gboolean
parse_gravity_hint (const char *name,
		    const char *arg,
		    gpointer    data,
		    GError **error)
{
455
  return parse_enum (PANGO_TYPE_GRAVITY_HINT, (int*)(void*)&opt_gravity_hint,
456
		     name, arg, data, error);
457
458
}

459
static gboolean
460
parse_hinting (const char *name G_GNUC_UNUSED,
461
	       const char *arg,
462
	       gpointer    data G_GNUC_UNUSED,
463
464
465
	       GError    **error)
{
  gboolean ret = TRUE;
466

467
468
469
470
  if (strcmp (arg, "none") == 0)
    opt_hinting = HINT_NONE;
  else if (strcmp (arg, "auto") == 0)
    opt_hinting = HINT_AUTO;
Matthias Clasen's avatar
Matthias Clasen committed
471
472
473
474
  else if (strcmp (arg, "slight") == 0)
    opt_hinting = HINT_SLIGHT;
  else if (strcmp (arg, "medium") == 0)
    opt_hinting = HINT_MEDIUM;
475
476
477
478
  else if (strcmp (arg, "full") == 0)
    opt_hinting = HINT_FULL;
  else
    {
479
      g_set_error(error,
480
		  G_OPTION_ERROR,
481
		  G_OPTION_ERROR_BAD_VALUE,
Matthias Clasen's avatar
Matthias Clasen committed
482
		  "Argument for --hinting must be one of none/auto/slight/medium/full");
483
484
      ret = FALSE;
    }
485

486
487
488
  return ret;
}

Matthias Clasen's avatar
Matthias Clasen committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
static gboolean
parse_subpixel_order (const char  *name,
                      const char  *arg,
                      gpointer     data,
                      GError     **error)
{
  gboolean ret = TRUE;

  if (strcmp (arg, "rgb") == 0)
    opt_subpixel_order = SUBPIXEL_RGB;
  else if (strcmp (arg, "bgr") == 0)
    opt_subpixel_order = SUBPIXEL_BGR;
  else if (strcmp (arg, "vrgb") == 0)
    opt_subpixel_order = SUBPIXEL_VRGB;
  else if (strcmp (arg, "vbgr") == 0)
    opt_subpixel_order = SUBPIXEL_VBGR;
  else
    {
      g_set_error (error,
		   G_OPTION_ERROR,
		   G_OPTION_ERROR_BAD_VALUE,
		   "Argument for --subpixel-order must be one of rgb/bgr/vrgb/vbgr");
      ret = FALSE;
    }

  return ret;
}

static gboolean
parse_hint_metrics (const char  *name,
                    const char  *arg,
                    gpointer     data,
                    GError     **error)
{
  gboolean ret = TRUE;

  if (strcmp (arg, "on") == 0)
    opt_hint_metrics = HINT_METRICS_ON;
  else if (strcmp (arg, "off") == 0)
    opt_hint_metrics = HINT_METRICS_OFF;
  else
    {
      g_set_error (error,
		   G_OPTION_ERROR,
		   G_OPTION_ERROR_BAD_VALUE,
		   "Argument for --hint-metrics must be one of on/off");
      ret = FALSE;
    }

  return ret;
}

static gboolean
parse_antialias (const char  *name,
                 const char  *arg,
                 gpointer     data,
                 GError     **error)
{
  gboolean ret = TRUE;

  if (strcmp (arg, "none") == 0)
    opt_antialias = ANTIALIAS_NONE;
  else if (strcmp (arg, "gray") == 0)
    opt_antialias = ANTIALIAS_GRAY;
  else if (strcmp (arg, "subpixel") == 0)
    opt_antialias = ANTIALIAS_SUBPIXEL;
  else
    {
      g_set_error (error,
		   G_OPTION_ERROR,
		   G_OPTION_ERROR_BAD_VALUE,
		   "Argument for --antialias must be one of none/gray/subpixel");
      ret = FALSE;
    }

  return ret;
}
566
567
static gboolean
parse_wrap (const char *name,
568
569
570
	    const char *arg,
	    gpointer    data,
	    GError    **error)
571
{
572
  gboolean ret;
573
  if ((ret = parse_enum (PANGO_TYPE_WRAP_MODE, (int*)(void*)&opt_wrap,
574
			 name, arg, data, error)))
575
576
577
578
579
580
    {
      opt_wrap_set = TRUE;
    }
  return ret;
}

581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
static gboolean
parse_rgba_color (PangoColor *color,
		  guint16    *alpha,
		  const char *name,
		  const char *arg,
		  gpointer    data G_GNUC_UNUSED,
		  GError    **error)
{
  gboolean ret;
  char buf[32];
  int len;

  len = strlen (arg);
  /* handle alpha */
  if (*arg == '#' && (len == 5 || len == 9 || len == 17))
    {
      int width, bits;
      unsigned int a;

      bits = len - 1;
      width = bits >> 2;

      strcpy (buf, arg);
      arg = buf;

      if (!sscanf (buf + len - width, "%x", &a))
        {
	  ret = FALSE;
	  goto err;
	}
      buf[len - width] = '\0';

      a <<= (16 - bits);
      while (bits < 16)
        {
	  a |= (a >> bits);
	  bits *= 2;
	}
      *alpha = a;
    }
  else
    *alpha = 65535;

  ret = pango_color_parse (color, arg);

err:
  if (!ret && error)
    {
      g_set_error(error,
		  G_OPTION_ERROR,
		  G_OPTION_ERROR_BAD_VALUE,
		  "Argument for %s must be a color name like red, or CSS-style #rrggbb / #rrggbbaa",
		  name);
    }

  return ret;
}

static gboolean
parse_foreground (const char *name,
		  const char *arg,
		  gpointer    data,
		  GError **error)
{
  return parse_rgba_color (&opt_fg_color, &opt_fg_alpha,
			   name, arg, data, error);
}

static gboolean
parse_background (const char *name,
		  const char *arg,
		  gpointer    data,
		  GError **error)
{
  opt_bg_set = TRUE;

  if (0 == strcmp ("transparent", arg))
    {
      opt_bg_alpha = 0;
      return TRUE;
    }

  return parse_rgba_color (&opt_bg_color, &opt_bg_alpha,
			   name, arg, data, error);
}

667
668
669
670
671
672
static gboolean
parse_margin (const char *name G_GNUC_UNUSED,
	      const char *arg,
	      gpointer    data G_GNUC_UNUSED,
	      GError    **error)
{
673
  switch (sscanf (arg, "%d%*[ ,]%d%*[ ,]%d%*[ ,]%d", &opt_margin_t, &opt_margin_r, &opt_margin_b, &opt_margin_l))
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
  {
    case 0:
    {
      g_set_error(error,
		  G_OPTION_ERROR,
		  G_OPTION_ERROR_BAD_VALUE,
		  "Argument for --margin must be one to four space-separated numbers");
      return FALSE;
    }
    case 1: opt_margin_r = opt_margin_t;
    case 2: opt_margin_b = opt_margin_t;
    case 3: opt_margin_l = opt_margin_r;
  }
  return TRUE;
}


691
692
693
694
695
static gchar *
backends_to_string (void)
{
  GString *backends = g_string_new (NULL);
  const PangoViewer **viewer;
696

697
698
699
700
701
702
703
  for (viewer = viewers; *viewer; viewer++)
    if ((*viewer)->id)
      {
	g_string_append (backends, (*viewer)->id);
	g_string_append_c (backends, '/');
      }
  g_string_truncate (backends, MAX (0, (gint)backends->len - 1));
704

705
706
707
708
709
710
711
712
713
714
715
716
  return g_string_free(backends,FALSE);
}

static int
backends_get_count (void)
{
  const PangoViewer **viewer;
  int i = 0;

  for (viewer = viewers; *viewer; viewer++)
    if ((*viewer)->id)
      i++;
717

718
719
720
721
722
723
724
725
726
  return i;
}


static gchar *
backend_description (void)
{
 GString *description  = g_string_new("Pango backend to use for rendering ");
 int backends_count = backends_get_count ();
727

728
729
730
731
732
733
734
735
 if (backends_count > 1)
   g_string_append_printf(description,"(default: %s)", (*viewers)->id);
 else if (backends_count == 1)
   g_string_append_printf(description,"(only available: %s)", (*viewers)->id);
 else
   g_string_append_printf(description,"(no backends found!)");

 return g_string_free(description,FALSE);
736

737
738
}

739
static gboolean
740
parse_backend (const char *name G_GNUC_UNUSED,
741
	       const char *arg,
742
	       gpointer    data G_GNUC_UNUSED,
743
744
745
746
747
748
749
750
751
752
753
754
755
	       GError    **error)
{
  gboolean ret = TRUE;
  const PangoViewer **viewer;

  for (viewer = viewers; *viewer; viewer++)
    if (!g_ascii_strcasecmp ((*viewer)->id, arg))
      break;

  if (*viewer)
    opt_viewer = *viewer;
  else
    {
756
      gchar *backends = backends_to_string ();
757

758
      g_set_error(error,
759
		  G_OPTION_ERROR,
760
		  G_OPTION_ERROR_BAD_VALUE,
761
		  "Available --backend options are: %s",
762
		  backends);
763
      g_free(backends);
764
765
766
767
768
769
      ret = FALSE;
    }

  return ret;
}

770

771
static G_GNUC_NORETURN gboolean
772
773
774
775
show_version(const char *name G_GNUC_UNUSED,
	     const char *arg G_GNUC_UNUSED,
	     gpointer    data G_GNUC_UNUSED,
	     GError    **error G_GNUC_UNUSED)
776
{
777
  g_printf("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
778
779

  if (PANGO_VERSION != pango_version())
780
    g_printf("Linked Pango library has a different version: %s\n", pango_version_string ());
781

782
783
784
  exit(0);
}

785
786
787
void
parse_options (int argc, char *argv[])
{
Behdad Esfahbod's avatar
Behdad Esfahbod committed
788
  gchar *backend_options = backends_to_string ();
789
790
  GOptionFlags backend_flag = backends_get_count () > 1 ? 0 : G_OPTION_FLAG_HIDDEN;
  gchar *backend_desc = backend_description ();
791
  GOptionEntry entries[] =
792
793
  {
    {"no-auto-dir",	0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&opt_auto_dir,
794
     "No layout direction according to contents",			NULL},
795
796
    {"backend",		0, backend_flag, G_OPTION_ARG_CALLBACK,		&parse_backend,
     backend_desc,					     backend_options},
797
    {"background",	0, 0, G_OPTION_ARG_CALLBACK,			&parse_background,
798
     "Set the background color",     "red/#rrggbb/#rrggbbaa/transparent"},
799
    {"no-display",	'q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,	&opt_display,
800
     "Do not display (just write to file or whatever)",			NULL},
801
    {"dpi",		0, 0, G_OPTION_ARG_INT,				&opt_dpi,
802
     "Set the resolution",					    "number"},
803
804
    {"align",		0, 0, G_OPTION_ARG_CALLBACK,			&parse_align,
     "Text alignment",				         "left/center/right"},
805
    {"ellipsize",	0, 0, G_OPTION_ARG_CALLBACK,			&parse_ellipsis,
806
     "Ellipsization mode",				  "start/middle/end"},
807
    {"font",		0, 0, G_OPTION_ARG_STRING,			&opt_font,
808
     "Set the font description",			       "description"},
809
    {"foreground",	0, 0, G_OPTION_ARG_CALLBACK,			&parse_foreground,
810
     "Set the text color",		         "red/#rrggbb/#rrggbbaa"},
Behdad Esfahbod's avatar
Behdad Esfahbod committed
811
    {"gravity",		0, 0, G_OPTION_ARG_CALLBACK,			&parse_gravity,
812
813
814
     "Base gravity: glyph rotation",		"south/east/north/west/auto"},
    {"gravity-hint",	0, 0, G_OPTION_ARG_CALLBACK,			&parse_gravity_hint,
     "Gravity hint",				       "natural/strong/line"},
815
    {"header",		0, 0, G_OPTION_ARG_NONE,			&opt_header,
816
     "Display the options in the output",				NULL},
817
818
    {"height",		0, 0, G_OPTION_ARG_INT,				&opt_height,
     "Height in points (positive) or number of lines (negative) for ellipsizing", "+points/-numlines"},
819
    {"hinting",		0, 0, G_OPTION_ARG_CALLBACK,			&parse_hinting,
Matthias Clasen's avatar
Matthias Clasen committed
820
821
822
823
824
     "Hinting style",					    "none/auto/slight/medium/full"},
    {"antialias",       0, 0, G_OPTION_ARG_CALLBACK,                    &parse_antialias,
     "Antialiasing",                                        "none/gray/subpixel"},
    {"hint-metrics",    0, 0, G_OPTION_ARG_CALLBACK,                    &parse_hint_metrics,
     "Hint metrics",                                        "on/off"},
825
826
    { "subpixel-positions", 0, 0, G_OPTION_ARG_NONE,                    &opt_subpixel_positions,
     "Subpixel positioning",                                        NULL},
Matthias Clasen's avatar
Matthias Clasen committed
827
828
    {"subpixel-order",  0, 0, G_OPTION_ARG_CALLBACK,                    &parse_subpixel_order,
     "Subpixel order",                                      "rgb/bgr/vrgb/vbgr"},
829
    {"indent",		0, 0, G_OPTION_ARG_INT,				&opt_indent,
830
     "Width in points to indent paragraphs",			    "points"},
831
832
    {"spacing",		0, 0, G_OPTION_ARG_INT,				&opt_spacing,
     "Spacing in points between lines",			            "points"},
833
834
    {"line-spacing",	0, 0, G_OPTION_ARG_DOUBLE,		        &opt_line_spacing,
     "Spread factor for line height",			            "factor"},
Matthias Clasen's avatar
Matthias Clasen committed
835
836
837
838
    {"justify",         0, 0, G_OPTION_ARG_NONE,                        &opt_justify,
     "Stretch paragraph lines to be justified",                         NULL},
    {"justify-last-line", 0, 0, G_OPTION_ARG_NONE,                      &opt_justify_last_line,
     "Justify the last line of the paragraph",                          NULL},
Behdad Esfahbod's avatar
Behdad Esfahbod committed
839
840
    {"language",	0, 0, G_OPTION_ARG_STRING,			&opt_language,
     "Language to use for font selection",			    "en_US/etc"},
841
842
    {"margin",		0, 0, G_OPTION_ARG_CALLBACK,			&parse_margin,
     "Set the margin on the output in pixels",			    "CSS-style numbers in pixels"},
843
    {"markup",		0, 0, G_OPTION_ARG_NONE,			&opt_markup,
844
     "Interpret text as Pango markup",					NULL},
845
    {"output",		'o', 0, G_OPTION_ARG_STRING,			&opt_output,
846
     "Save rendered image to output file",			      "file"},
Behdad Esfahbod's avatar
Behdad Esfahbod committed
847
    {"pangorc",		0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING,	&opt_pangorc,
Khaled Hosny's avatar
Khaled Hosny committed
848
     "Deprecated",		      "file"},
849
850
    {"pixels",		0, 0, G_OPTION_ARG_NONE,			&opt_pixels,
     "Use pixel units instead of points (sets dpi to 72)",		NULL},
851
852
    {"pango-units",	0, 0, G_OPTION_ARG_NONE,			&opt_pango_units,
     "Use Pango units instead of points",		                NULL},
853
    {"rtl",		0, 0, G_OPTION_ARG_NONE,			&opt_rtl,
854
     "Set base direction to right-to-left",				NULL},
855
    {"rotate",		0, 0, G_OPTION_ARG_DOUBLE,			&opt_rotate,
856
     "Angle at which to rotate results",			   "degrees"},
857
    {"runs",		'n', 0, G_OPTION_ARG_INT,			&opt_runs,
858
     "Run Pango layout engine this many times",			   "integer"},
859
860
    {"single-par",	0, 0, G_OPTION_ARG_NONE,			&opt_single_par,
     "Enable single-paragraph mode",					NULL},
861
    {"text",		't', 0, G_OPTION_ARG_STRING,			&opt_text,
862
     "Text to display (instead of a file)",			    "string"},
863
864
    {"version",		0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &show_version,
     "Show version numbers",						NULL},
865
    {"waterfall",	0, 0, G_OPTION_ARG_NONE,			&opt_waterfall,
866
     "Create a waterfall display",					NULL},
867
    {"width",		'w', 0, G_OPTION_ARG_INT,			&opt_width,
868
     "Width in points to which to wrap lines or ellipsize",	    "points"},
869
    {"wrap",		0, 0, G_OPTION_ARG_CALLBACK,			&parse_wrap,
870
     "Text wrapping mode (needs a width to be set)",   "word/char/word-char"},
871
    {NULL}
872
873
874
875
876
  };
  GError *error = NULL;
  GError *parse_error = NULL;
  GOptionContext *context;
  size_t len;
877
  const PangoViewer **viewer;
878
879
880

  context = g_option_context_new ("- FILE");
  g_option_context_add_main_entries (context, entries, NULL);
881
882
883
884
885
886
887
888
889

  for (viewer = viewers; *viewer; viewer++)
    if ((*viewer)->get_option_group)
      {
        GOptionGroup *group = (*viewer)->get_option_group (*viewer);
	if (group)
	  g_option_context_add_group (context, group);
      }

890
891
892
  if (!g_option_context_parse (context, &argc, &argv, &parse_error))
  {
    if (parse_error != NULL)
893
      fail("%s", parse_error->message);
894
    else
895
      fail("Option parse error");
896
897
898
    exit(1);
  }
  g_option_context_free(context);
899
900
  g_free(backend_options);
  g_free(backend_desc);
901

902
903
904
  if (opt_pixels)
    opt_dpi = 72;

905
906
907
908
  if ((opt_text && argc != 1) || (!opt_text && argc != 2))
    {
      if (opt_text && argc != 1)
	fail ("When specifying --text, no file should be given");
909

910
      g_printerr ("Usage: %s [OPTION...] FILE\n", g_get_prgname ());
911
912
913
      exit (1);
    }

914
915
916
917
918
  /* set up the backend */
  if (!opt_viewer)
    {
      opt_viewer = *viewers;
      if (!opt_viewer)
919
	fail ("No viewer backend found");
920
921
    }

922
923
924
925
926
927
928
929
930
931
932
933
  /* Get the text
   */
  if (opt_text)
    {
      text = g_strdup (opt_text);
      len = strlen (text);
    }
  else
    {
      if (!g_file_get_contents (argv[1], &text, &len, &error))
	fail ("%s\n", error->message);
    }
934

935
  /* Strip one trailing newline
936
   */
937
938
939
940
941
  if (len > 0 && text[len - 1] == '\n')
    len--;
  if (len > 0 && text[len - 1] == '\r')
    len--;
  text[len] = '\0';
942
943
944
945
946
947
948
949
950
951
952
953
954
955

  /* Make sure we have valid markup
   */
  if (opt_markup &&
      !pango_parse_markup (text, -1, 0, NULL, NULL, NULL, &error))
    fail ("Cannot parse input as markup: %s", error->message);
}


void
finalize (void)
{
  g_free (text);
}