summaryrefslogtreecommitdiff
path: root/examples/viewer-pangoft2.c
blob: 372ccb30c3c9eb2fba0d560f32a9d45b896fbebb (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
/* viewer-pangoft2.c: PangoFT2 viewer backend.
 *
 * 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 <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "renderdemo.h"
#include "viewer.h"

#include <pango/pangoft2.h>

static void
substitute_func (FcPattern *pattern,
		 gpointer   data)
{
  if (opt_hinting != HINT_DEFAULT)
    {
      FcPatternDel (pattern, FC_HINTING);
      FcPatternAddBool (pattern, FC_HINTING, opt_hinting != HINT_NONE);
      
      FcPatternDel (pattern, FC_AUTOHINT);
      FcPatternAddBool (pattern, FC_AUTOHINT, opt_hinting == HINT_AUTO);
    }
}

static gpointer
pangoft2_view_create (const PangoViewer *klass)
{
  PangoFontMap *fontmap;
  fontmap = pango_ft2_font_map_new ();

  pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap), opt_dpi, opt_dpi);
  pango_ft2_font_map_set_default_substitute (PANGO_FT2_FONT_MAP (fontmap), substitute_func, NULL, NULL);

  return fontmap;
}

static void 
pangoft2_view_destroy (gpointer instance)
{
  g_object_unref (instance);
}

static PangoContext *
pangoft2_view_get_context (gpointer instance)
{
  return pango_ft2_font_map_create_context (PANGO_FT2_FONT_MAP (instance));
}

static gpointer
pangoft2_view_create_surface (gpointer instance,
			      int      width,
			      int      height)
{
  FT_Bitmap *bitmap;

  bitmap = g_slice_new (FT_Bitmap);
  bitmap->width = width;
  bitmap->pitch = (bitmap->width + 3) & ~3;
  bitmap->rows = height;
  bitmap->buffer = g_malloc (bitmap->pitch * bitmap->rows);
  bitmap->num_grays = 256;
  bitmap->pixel_mode = ft_pixel_mode_grays;
  memset (bitmap->buffer, 0x00, bitmap->pitch * bitmap->rows);

  return bitmap;
}

static void 
pangoft2_view_destroy_surface (gpointer instance,
			       gpointer surface)
{
  FT_Bitmap *bitmap = (FT_Bitmap *) surface;

  g_free (bitmap->buffer);
  g_slice_free (FT_Bitmap, bitmap);
}

static void
render_callback (PangoLayout *layout,
		 int          x,
		 int          y,
		 gpointer     context,
		 gpointer     state)
{
  pango_ft2_render_layout ((FT_Bitmap *)context,
			   layout,
			   x, y);
}

static void 
pangoft2_view_render (gpointer      instance,
		      gpointer      surface,
		      PangoContext *context,
		      int           width,
		      int           height,
		      gpointer      state)
{
  int pix_idx;
  FT_Bitmap *bitmap = (FT_Bitmap *) surface;

  do_output (context, render_callback, NULL, surface, state, NULL, NULL);

  for (pix_idx=0; pix_idx<bitmap->pitch * bitmap->rows; pix_idx++)
    bitmap->buffer[pix_idx] = 255 - bitmap->buffer[pix_idx];
}

static void
pangoft2_view_save (gpointer instance,
		    gpointer surface,
		    FILE    *stream,
		    int      width,
		    int      height)
{
  int row;
  FT_Bitmap *bitmap = (FT_Bitmap *) surface;

  /* Write it as pgm to output */
  fprintf(stream,
	  "P5\n"
	  "%d %d\n"
	  "255\n", width, height);
  for (row = 0; row < height; row++)
    fwrite(bitmap->buffer + row * bitmap->pitch, 1, width, stream);
}

const PangoViewer pangoft2_viewer = {
  "PangoFT2",
  "ft2",
  ".pgm",
  pangoft2_view_create,
  pangoft2_view_destroy,
  pangoft2_view_get_context,
  pangoft2_view_create_surface,
  pangoft2_view_destroy_surface,
  pangoft2_view_render,
  pangoft2_view_save,
  NULL,
  NULL,
  NULL
};