summaryrefslogtreecommitdiff
path: root/examples/columns.c
blob: 063127b6d4a88c05a681775d57860b14bdf639b7 (plain)
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
#include <pango/pango.h>
#include <pango/pangocairo.h>

int
main (int argc, char *argv[])
{
  const char *filename;
  PangoContext *context;
  PangoLineBreaker *breaker;
  int margin;
  int x, y, width, height;
  int x0, y0;
  int col;
  PangoLines *lines;
  cairo_surface_t *surface;
  cairo_t *cr;
  char *text;
  gsize length;
  PangoAttrList *attrs;
  GError *error = NULL;

  if (argc != 3)
    {
      g_printerr ("Usage: %s INPUT_FILENAME OUTPUT_FILENAME\n", argv[0]);
      return 1;
    }

  if (!g_file_get_contents (argv[1], &text, &length, &error))
    {
      g_printerr ("%s\n", error->message);
      return 1;
    }

  filename = argv[2];

  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 600, 600);
  cr = cairo_create (surface);
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);
  cairo_set_source_rgb (cr, 0, 0, 0);

  breaker = pango_line_breaker_new (context);

  g_print ("Using %s\n", G_OBJECT_TYPE_NAME (breaker));

  attrs = pango_attr_list_new ();

  pango_line_breaker_add_text (breaker, text, -1, attrs);

  pango_attr_list_unref (attrs);

  lines = pango_lines_new ();

  /* We do 2 columns, followed by 3 columns, to demonstrate changing
   * line width.
   */
  margin = 20;
  x = x0 = margin * PANGO_SCALE;
  y = y0 = margin * PANGO_SCALE;
  width = (300 - 2 * margin) * PANGO_SCALE;
  height = (300 - margin) * PANGO_SCALE;
  col = 0;

  while (!pango_line_breaker_done (breaker))
    {
      PangoLayoutLine *line;
      PangoRectangle ext;

retry:
      line = pango_line_breaker_next_line (breaker,
                                           x, width,
                                           PANGO_WRAP_CHAR,
                                           PANGO_ELLIPSIZE_NONE);

      if (!pango_layout_line_ends_paragraph (line))
        line = pango_layout_line_justify (line, width);

      pango_layout_line_get_trimmed_extents (line, PANGO_LEADING_TRIM_NONE, &ext);

      if (y + ext.height > height)
        {
          gboolean width_changed = FALSE;

          if (col == 0)
            x0 += 300 * PANGO_SCALE;
          else if (col == 1)
            {
              x0 = margin * PANGO_SCALE;
              y0 = (300 + margin) * PANGO_SCALE;
              width = (200 - 2 * margin) * PANGO_SCALE;
              height = (600 - 2 * margin) * PANGO_SCALE;

              width_changed = TRUE;
            }
          else
            x0 += 200 * PANGO_SCALE;

          x = x0;
          y = y0;

          col++;

          if (width_changed &&
              pango_line_breaker_undo_line (breaker, line))
            {
              g_object_unref (line);
              goto retry;
            }
        }

      pango_lines_add_line (lines, line, x, y - ext.y);

      y += ext.height;
    }

  pango_cairo_show_lines (cr, lines);

  cairo_surface_write_to_png (surface, filename);
  g_print ("Output written to %s\n", filename);

  g_object_unref (lines);
  g_object_unref (breaker);

  cairo_surface_destroy (surface);
  cairo_destroy (cr);

  g_object_unref (context);

  g_free (text);

  return 0;
}