summaryrefslogtreecommitdiff
path: root/examples/cairoview.c
blob: 8e43a56a8fbdfdc1a9d3535f599a183ba49df6c7 (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
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
/* Pango
 * cairoview.c: Example program to view a UTF-8 encoding file
 *              using PangoCairo to render result
 *
 * Copyright (C) 1999,2004,2005 Red Hat, Inc.
 * 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 "renderdemo.h"
#include "viewer-x.h"

#include <pango/pangocairo.h>
#include <cairo-xlib.h>

static void
render_callback (PangoLayout *layout,
		 int          x,
		 int          y,
		 gpointer     data,
		 gboolean     show_borders)
{
  cairo_t *cr = (cairo_t *)data;

  cairo_move_to (cr, x, y);
  pango_cairo_show_layout (cr, layout);

  if (show_borders)
    {
      PangoRectangle ink, logical;
      double lw = cairo_get_line_width (cr);
      
      pango_layout_get_extents (layout, &ink, &logical);

      cairo_save (cr);
      cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.75);

      cairo_rectangle (cr,
		       (double)logical.x / PANGO_SCALE - lw / 2,
		       (double)logical.y / PANGO_SCALE - lw / 2,
		       (double)logical.width / PANGO_SCALE + lw,
		       (double)logical.height / PANGO_SCALE + lw);
      cairo_stroke (cr);
      
      cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, 0.75);

      cairo_rectangle (cr,
		       (double)ink.x / PANGO_SCALE - lw / 2,
		       (double)ink.y / PANGO_SCALE - lw / 2,
		       (double)ink.width / PANGO_SCALE + lw,
		       (double)ink.height / PANGO_SCALE + lw);
      cairo_stroke (cr);

      cairo_restore (cr);
    }
}

static void
transform_callback (PangoContext *context,
		    PangoMatrix  *matrix,
		    gpointer      data)
{
  cairo_t *cr = (cairo_t *)data;
  cairo_matrix_t cairo_matrix;

  if (matrix)
    {
      cairo_matrix.xx = matrix->xx;
      cairo_matrix.yx = matrix->yx;
      cairo_matrix.xy = matrix->xy;
      cairo_matrix.yy = matrix->yy;
      cairo_matrix.x0 = matrix->x0;
      cairo_matrix.y0 = matrix->y0;
    }
  else
    {
      cairo_matrix_init_identity (&cairo_matrix);
    }

  cairo_set_matrix (cr, &cairo_matrix);
      
  pango_context_set_matrix (context, matrix);
  pango_cairo_update_context (cr, context);
}

void
do_init (Display *display,
	 int screen,
	 int dpi,
	 /* output */
	 PangoContext **context,
	 int *width,
	 int *height)
{
  PangoFontMap *fontmap;
  cairo_t *cr;
  cairo_surface_t *surface;
  fontmap = pango_cairo_font_map_get_default ();
  pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (fontmap), dpi);
  *context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));

  /* This is annoying ... we have to create a temporary surface just to
   * get the extents of the text.
   */
  surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1);
  cr = cairo_create (surface);
  cairo_surface_destroy (surface);
  do_output (*context, NULL, transform_callback, cr, width, height, FALSE);
  cairo_destroy (cr);
}

void
do_render (Display *display,
	   int screen,
	   Window window,
	   Pixmap pixmap,
	   PangoContext *context,
	   int width,
	   int height,
	   gboolean show_borders)
{
  cairo_t *cr;
  cairo_surface_t *surface;

  surface = cairo_xlib_surface_create (display, pixmap,
				       DefaultVisual (display, screen),
				       width, height);

  cr = cairo_create (surface);
  
  transform_callback (context, NULL, cr);

  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_paint (cr);
  
  /* Draw the text in black */
  cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  do_output (context, render_callback, transform_callback, cr, NULL, NULL, show_borders);

#ifdef HAVE_CAIRO_PNG
  if (opt_output && *opt_output)
    {
      cairo_status_t status;

      status = cairo_surface_write_to_png (surface, opt_output);
      if (status != CAIRO_STATUS_SUCCESS)
	g_printerr ("could not save PNG to '%s'\n", opt_output);
    }
#endif

  cairo_surface_destroy (surface);
  cairo_destroy (cr);
}