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
53
54
55
56
57
58
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
101
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
148
149
150
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
192
193
194
195
196
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
/* viewer-render.c: Common code for rendering in viewers
*
* 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.
*/
#include "config.h"
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <pango/pango.h>
#include "viewer-render.h"
gboolean opt_display = TRUE;
int opt_dpi = 96;
gboolean opt_pixels = FALSE;
gboolean opt_pango_units = FALSE;
const char *opt_font = "";
gboolean opt_header = FALSE;
const char *opt_output = NULL;
int opt_margin_t = 10;
int opt_margin_r = 10;
int opt_margin_b = 10;
int opt_margin_l = 10;
int opt_markup = FALSE;
gboolean opt_rtl = FALSE;
double opt_rotate = 0;
gboolean opt_auto_dir = TRUE;
const char *opt_text = NULL;
gboolean opt_waterfall = FALSE;
int opt_width = -1;
int opt_height = -1;
int opt_indent = 0;
int opt_spacing = 0;
double opt_line_spacing = -1.0;
gboolean opt_justify = 0;
gboolean opt_justify_last_line = 0;
int opt_runs = 1;
PangoAlignment opt_align = PANGO_ALIGN_LEFT;
PangoEllipsizeMode opt_ellipsize = PANGO_ELLIPSIZE_NONE;
PangoGravity opt_gravity = PANGO_GRAVITY_SOUTH;
PangoGravityHint opt_gravity_hint = PANGO_GRAVITY_HINT_NATURAL;
HintMode opt_hinting = HINT_DEFAULT;
HintMetrics opt_hint_metrics = HINT_METRICS_DEFAULT;
SubpixelOrder opt_subpixel_order = SUBPIXEL_DEFAULT;
Antialias opt_antialias = ANTIALIAS_DEFAULT;
gboolean opt_subpixel_positions = FALSE;
PangoWrapMode opt_wrap = PANGO_WRAP_WORD_CHAR;
gboolean opt_wrap_set = FALSE;
static const char *opt_pangorc = NULL; /* Unused */
const PangoViewer *opt_viewer = NULL;
const char *opt_language = NULL;
gboolean opt_single_par = FALSE;
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;
gboolean opt_serialized = FALSE;
const char *opt_serialized_output;
const char *file_arg;
/* Text (or markup) to render */
static char *text;
void
fail (const char *format, ...)
{
const char *msg;
va_list vap;
va_start (vap, format);
msg = g_strdup_vprintf (format, vap);
g_printerr ("%s: %s\n", g_get_prgname (), msg);
exit (1);
}
static PangoLayout *
make_layout(PangoContext *context,
const char *text,
double size)
{
static PangoFontDescription *font_description;
PangoAlignment align;
PangoLayout *layout;
if (opt_serialized)
{
char *data;
gsize len;
GBytes *bytes;
GError *error = NULL;
if (!g_file_get_contents (file_arg, &data, &len, &error))
fail ("%s\n", error->message);
bytes = g_bytes_new_take (data, len);
layout = pango_layout_deserialize (context, bytes, PANGO_LAYOUT_DESERIALIZE_CONTEXT, &error);
if (!layout)
fail ("%s\n", error->message);
g_bytes_unref (bytes);
goto out;
}
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);
pango_layout_set_justify (layout, opt_justify);
pango_layout_set_justify_last_line (layout, opt_justify_last_line);
pango_layout_set_single_paragraph_mode (layout, opt_single_par);
pango_layout_set_wrap (layout, opt_wrap);
font_description = pango_font_description_from_string (opt_font);
if (size > 0)
pango_font_description_set_size (font_description, size * PANGO_SCALE);
if (opt_width >= 0)
{
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);
}
if (opt_height >= 0)
{
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);
}
else
pango_layout_set_height (layout, opt_height);
if (opt_indent != 0)
{
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);
}
if (opt_spacing != 0)
{
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);
pango_layout_set_line_spacing (layout, 0.0);
}
if (opt_line_spacing >= 0.0)
pango_layout_set_line_spacing (layout, (float)opt_line_spacing);
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);
pango_layout_set_font_description (layout, font_description);
pango_font_description_free (font_description);
out:
if (opt_serialized_output)
{
GError *error = NULL;
if (!pango_layout_write_to_file (layout,
PANGO_LAYOUT_SERIALIZE_CONTEXT|PANGO_LAYOUT_SERIALIZE_OUTPUT,
opt_serialized_output,
&error))
fail ("%s\n", error->message);
}
return layout;
}
gchar *
get_options_string (void)
{
PangoFontDescription *font_description = pango_font_description_from_string (opt_font);
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);
result = g_strdup_printf ("%s: %s (%d dpi)", opt_viewer->name, font_name, opt_dpi);
pango_font_description_free (font_description);
g_free (font_name);
return result;
}
static void
output_body (PangoLayout *layout,
RenderCallback render_cb,
gpointer cb_context,
gpointer cb_data,
int *width,
int *height,
gboolean supports_matrix)
{
PangoRectangle logical_rect;
int size, start_size, end_size, increment;
int x = 0, y = 0;
if (!supports_matrix)
{
const PangoMatrix* matrix;
const PangoMatrix identity = PANGO_MATRIX_INIT;
PangoContext *context = pango_layout_get_context (layout);
matrix = pango_context_get_matrix (context);
if (matrix)
{
x += matrix->x0;
y += matrix->y0;
}
pango_context_set_matrix (context, &identity);
pango_layout_context_changed (layout);
}
if (opt_waterfall)
{
start_size = 8;
end_size = 48;
increment = 4;
}
else
{
start_size = end_size = -1;
increment = 1;
}
*width = 0;
*height = 0;
for (size = start_size; size <= end_size; size += increment)
{
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);
}
pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
if (render_cb)
(*render_cb) (layout, x, y+*height, cb_context, cb_data);
*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)));
}
}
static void
set_transform (PangoContext *context,
TransformCallback transform_cb,
gpointer cb_context,
gpointer cb_data,
PangoMatrix *matrix)
{
pango_context_set_matrix (context, matrix);
if (transform_cb)
(*transform_cb) (context, matrix, cb_context, cb_data);
}
void
do_output (PangoContext *context,
RenderCallback render_cb,
TransformCallback transform_cb,
gpointer cb_context,
gpointer cb_data,
int *width_out,
int *height_out)
{
PangoLayout *layout;
PangoRectangle rect;
PangoMatrix matrix = PANGO_MATRIX_INIT;
PangoMatrix *orig_matrix;
gboolean supports_matrix;
int rotated_width, rotated_height;
int x = opt_margin_l;
int y = opt_margin_t;
int width, height;
width = 0;
height = 0;
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.);
set_transform (context, transform_cb, cb_context, cb_data, NULL);
pango_context_set_language (context,
opt_language ? pango_language_from_string (opt_language)
: pango_language_get_default ());
pango_context_set_base_dir (context,
opt_rtl ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
if (opt_header)
{
char *options_string = get_options_string ();
pango_context_set_base_gravity (context, PANGO_GRAVITY_SOUTH);
layout = make_layout (context, options_string, 10);
pango_layout_get_extents (layout, NULL, &rect);
width = MAX (width, PANGO_PIXELS (rect.width));
height += PANGO_PIXELS (rect.height);
if (render_cb)
(*render_cb) (layout, x, y, cb_context, cb_data);
y += PANGO_PIXELS (rect.height);
g_object_unref (layout);
g_free (options_string);
}
if (opt_rotate != 0)
{
if (supports_matrix)
pango_matrix_rotate (&matrix, opt_rotate);
else
g_printerr ("The backend does not support rotated text\n");
}
pango_context_set_base_gravity (context, opt_gravity);
pango_context_set_gravity_hint (context, opt_gravity_hint);
layout = make_layout (context, text, -1);
set_transform (context, transform_cb, cb_context, cb_data, &matrix);
output_body (layout,
NULL, NULL, NULL,
&rotated_width, &rotated_height,
supports_matrix);
rect.x = rect.y = 0;
rect.width = rotated_width;
rect.height = rotated_height;
pango_matrix_transform_pixel_rectangle (&matrix, &rect);
matrix.x0 = x - rect.x;
matrix.y0 = y - rect.y;
set_transform (context, transform_cb, cb_context, cb_data, &matrix);
if (render_cb)
output_body (layout,
render_cb, cb_context, cb_data,
&rotated_width, &rotated_height,
supports_matrix);
width = MAX (width, rect.width);
height += rect.height;
width += opt_margin_l + opt_margin_r;
height += opt_margin_t + opt_margin_b;
if (width_out)
*width_out = width;
if (height_out)
*height_out = height;
pango_context_set_matrix (context, orig_matrix);
pango_matrix_free (orig_matrix);
g_object_unref (layout);
}
static gboolean
parse_enum (GType type,
int *value,
const char *name,
const char *arg,
gpointer data G_GNUC_UNUSED,
GError **error)
{
char *possible_values = NULL;
gboolean ret;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
ret = pango_parse_enum (type,
arg,
value,
FALSE,
&possible_values);
G_GNUC_END_IGNORE_DEPRECATIONS
if (!ret && error)
{
g_set_error(error,
G_OPTION_ERROR,
G_OPTION_ERROR_BAD_VALUE,
"Argument for %s must be one of %s",
name,
possible_values);
}
g_free (possible_values);
return ret;
}
static gboolean
parse_align (const char *name,
const char *arg,
gpointer data,
GError **error)
{
return parse_enum (PANGO_TYPE_ALIGNMENT, (int*)(void*)&opt_align,
name, arg, data, error);
}
static gboolean
parse_ellipsis (const char *name,
const char *arg,
gpointer data,
GError **error)
{
return parse_enum (PANGO_TYPE_ELLIPSIZE_MODE, (int*)(void*)&opt_ellipsize,
name, arg, data, error);
}
static gboolean
parse_gravity (const char *name,
const char *arg,
gpointer data,
GError **error)
{
return parse_enum (PANGO_TYPE_GRAVITY, (int*)(void*)&opt_gravity,
name, arg, data, error);
}
static gboolean
parse_gravity_hint (const char *name,
const char *arg,
gpointer data,
GError **error)
{
return parse_enum (PANGO_TYPE_GRAVITY_HINT, (int*)(void*)&opt_gravity_hint,
name, arg, data, error);
}
static gboolean
parse_hinting (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data G_GNUC_UNUSED,
GError **error)
{
gboolean ret = TRUE;
if (strcmp (arg, "none") == 0)
opt_hinting = HINT_NONE;
else if (strcmp (arg, "auto") == 0)
opt_hinting = HINT_AUTO;
else if (strcmp (arg, "slight") == 0)
opt_hinting = HINT_SLIGHT;
else if (strcmp (arg, "medium") == 0)
opt_hinting = HINT_MEDIUM;
else if (strcmp (arg, "full") == 0)
opt_hinting = HINT_FULL;
else
{
g_set_error(error,
G_OPTION_ERROR,
G_OPTION_ERROR_BAD_VALUE,
"Argument for --hinting must be one of none/auto/slight/medium/full");
ret = FALSE;
}
return ret;
}
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;
}
static gboolean
parse_wrap (const char *name,
const char *arg,
gpointer data,
GError **error)
{
gboolean ret;
if ((ret = parse_enum (PANGO_TYPE_WRAP_MODE, (int*)(void*)&opt_wrap,
name, arg, data, error)))
{
opt_wrap_set = TRUE;
}
return ret;
}
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);
}
static gboolean
parse_margin (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data G_GNUC_UNUSED,
GError **error)
{
switch (sscanf (arg, "%d%*[ ,]%d%*[ ,]%d%*[ ,]%d", &opt_margin_t, &opt_margin_r, &opt_margin_b, &opt_margin_l))
{
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;
}
static gchar *
backends_to_string (void)
{
GString *backends = g_string_new (NULL);
const PangoViewer **viewer;
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));
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++;
return i;
}
static gchar *
backend_description (void)
{
GString *description = g_string_new("Pango backend to use for rendering ");
int backends_count = backends_get_count ();
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);
}
static gboolean
parse_backend (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data G_GNUC_UNUSED,
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
{
gchar *backends = backends_to_string ();
g_set_error(error,
G_OPTION_ERROR,
G_OPTION_ERROR_BAD_VALUE,
"Available --backend options are: %s",
backends);
g_free(backends);
ret = FALSE;
}
return ret;
}
static G_GNUC_NORETURN gboolean
show_version(const char *name G_GNUC_UNUSED,
const char *arg G_GNUC_UNUSED,
gpointer data G_GNUC_UNUSED,
GError **error G_GNUC_UNUSED)
{
g_printf("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
if (PANGO_VERSION != pango_version())
g_printf("Linked Pango library has a different version: %s\n", pango_version_string ());
exit(0);
}
void
parse_options (int argc, char *argv[])
{
gchar *backend_options = backends_to_string ();
GOptionFlags backend_flag = backends_get_count () > 1 ? 0 : G_OPTION_FLAG_HIDDEN;
gchar *backend_desc = backend_description ();
GOptionEntry entries[] =
{
{"no-auto-dir", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_auto_dir,
"No layout direction according to contents", NULL},
{"backend", 0, backend_flag, G_OPTION_ARG_CALLBACK, &parse_backend,
backend_desc, backend_options},
{"background", 0, 0, G_OPTION_ARG_CALLBACK, &parse_background,
"Set the background color", "red/#rrggbb/#rrggbbaa/transparent"},
{"no-display", 'q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_display,
"Do not display (just write to file or whatever)", NULL},
{"dpi", 0, 0, G_OPTION_ARG_INT, &opt_dpi,
"Set the resolution", "number"},
{"align", 0, 0, G_OPTION_ARG_CALLBACK, &parse_align,
"Text alignment", "left/center/right"},
{"ellipsize", 0, 0, G_OPTION_ARG_CALLBACK, &parse_ellipsis,
"Ellipsization mode", "start/middle/end"},
{"font", 0, 0, G_OPTION_ARG_STRING, &opt_font,
"Set the font description", "description"},
{"foreground", 0, 0, G_OPTION_ARG_CALLBACK, &parse_foreground,
"Set the text color", "red/#rrggbb/#rrggbbaa"},
{"gravity", 0, 0, G_OPTION_ARG_CALLBACK, &parse_gravity,
"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"},
{"header", 0, 0, G_OPTION_ARG_NONE, &opt_header,
"Display the options in the output", NULL},
{"height", 0, 0, G_OPTION_ARG_INT, &opt_height,
"Height in points (positive) or number of lines (negative) for ellipsizing", "+points/-numlines"},
{"hinting", 0, 0, G_OPTION_ARG_CALLBACK, &parse_hinting,
"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"},
{ "subpixel-positions", 0, 0, G_OPTION_ARG_NONE, &opt_subpixel_positions,
"Subpixel positioning", NULL},
{"subpixel-order", 0, 0, G_OPTION_ARG_CALLBACK, &parse_subpixel_order,
"Subpixel order", "rgb/bgr/vrgb/vbgr"},
{"indent", 0, 0, G_OPTION_ARG_INT, &opt_indent,
"Width in points to indent paragraphs", "points"},
{"spacing", 0, 0, G_OPTION_ARG_INT, &opt_spacing,
"Spacing in points between lines", "points"},
{"line-spacing", 0, 0, G_OPTION_ARG_DOUBLE, &opt_line_spacing,
"Spread factor for line height", "factor"},
{"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},
{"language", 0, 0, G_OPTION_ARG_STRING, &opt_language,
"Language to use for font selection", "en_US/etc"},
{"margin", 0, 0, G_OPTION_ARG_CALLBACK, &parse_margin,
"Set the margin on the output in pixels", "CSS-style numbers in pixels"},
{"markup", 0, 0, G_OPTION_ARG_NONE, &opt_markup,
"Interpret text as Pango markup", NULL},
{"output", 'o', 0, G_OPTION_ARG_STRING, &opt_output,
"Save rendered image to output file", "file"},
{"pangorc", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &opt_pangorc,
"Deprecated", "file"},
{"pixels", 0, 0, G_OPTION_ARG_NONE, &opt_pixels,
"Use pixel units instead of points (sets dpi to 72)", NULL},
{"pango-units", 0, 0, G_OPTION_ARG_NONE, &opt_pango_units,
"Use Pango units instead of points", NULL},
{"rtl", 0, 0, G_OPTION_ARG_NONE, &opt_rtl,
"Set base direction to right-to-left", NULL},
{"rotate", 0, 0, G_OPTION_ARG_DOUBLE, &opt_rotate,
"Angle at which to rotate results", "degrees"},
{"runs", 'n', 0, G_OPTION_ARG_INT, &opt_runs,
"Run Pango layout engine this many times", "integer"},
{"single-par", 0, 0, G_OPTION_ARG_NONE, &opt_single_par,
"Enable single-paragraph mode", NULL},
{"text", 't', 0, G_OPTION_ARG_STRING, &opt_text,
"Text to display (instead of a file)", "string"},
{"version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &show_version,
"Show version numbers", NULL},
{"waterfall", 0, 0, G_OPTION_ARG_NONE, &opt_waterfall,
"Create a waterfall display", NULL},
{"width", 'w', 0, G_OPTION_ARG_INT, &opt_width,
"Width in points to which to wrap lines or ellipsize", "points"},
{"wrap", 0, 0, G_OPTION_ARG_CALLBACK, &parse_wrap,
"Text wrapping mode (needs a width to be set)", "word/char/word-char"},
{"serialized", 0, 0, G_OPTION_ARG_NONE, &opt_serialized,
"Create layout from a serialized file", "FILE"},
{"serialize-to", 0, 0, G_OPTION_ARG_FILENAME, &opt_serialized_output,
"Serialize result to a file", "FILE"},
{NULL}
};
GError *error = NULL;
GError *parse_error = NULL;
GOptionContext *context;
size_t len;
const PangoViewer **viewer;
context = g_option_context_new ("- FILE");
g_option_context_add_main_entries (context, entries, NULL);
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);
}
if (!g_option_context_parse (context, &argc, &argv, &parse_error))
{
if (parse_error != NULL)
fail("%s", parse_error->message);
else
fail("Option parse error");
exit(1);
}
g_option_context_free(context);
g_free(backend_options);
g_free(backend_desc);
if (opt_pixels)
opt_dpi = 72;
if ((opt_text && argc != 1) || (!opt_text && argc != 2))
{
if (opt_text && argc != 1)
fail ("When specifying --text, no file should be given");
g_printerr ("Usage: %s [OPTION...] FILE\n", g_get_prgname ());
exit (1);
}
if (opt_serialized && argc != 2)
{
g_printerr ("Usage: %s [OPTION...] FILE\n", g_get_prgname ());
exit (1);
}
/* set up the backend */
if (!opt_viewer)
{
opt_viewer = *viewers;
if (!opt_viewer)
fail ("No viewer backend found");
}
/* Get the text
*/
if (opt_serialized)
{
file_arg = argv[1];
text = g_strdup ("");
len = 0;
}
else 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);
}
/* Strip one trailing newline
*/
if (len > 0 && text[len - 1] == '\n')
len--;
if (len > 0 && text[len - 1] == '\r')
len--;
text[len] = '\0';
/* 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);
}
|