summaryrefslogtreecommitdiff
path: root/pango/pangoft2-render.c
blob: 90d1ba3a785ae04e59f40d47dff8de13eba700eb (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
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
/* Pango
 * pangoft2-render.c: Rendering routines to FT_Bitmap objects
 *
 * Copyright (C) 2004 Red Hat Software
 * Copyright (C) 2000 Tor Lillqvist
 *
 * 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 <math.h>

#include "pangoft2-private.h"

typedef struct {
  double y;
  double x1;
  double x2;
} Position;

static void
draw_simple_trap (FT_Bitmap *bitmap,
		  Position  *t,
		  Position  *b)
{
  int iy = floor (t->y);
  int x1, x2, x;
  double dy = b->y - t->y;
  guchar *dest;

  if (iy < 0 || iy >= bitmap->rows)
    return;
  dest = bitmap->buffer + iy * bitmap->pitch;

  if (t->x1 < b->x1)
    x1 = floor (t->x1);
  else
    x1 = floor (b->x1);

  if (t->x2 > b->x2)
    x2 = ceil (t->x2);
  else
    x2 = ceil (b->x2);

  x1 = CLAMP (x1, 0, bitmap->width);
  x2 = CLAMP (x2, 0, bitmap->width);

  for (x = x1; x < x2; x++)
    {
      double top_left = MAX (t->x1, x);
      double top_right = MIN (t->x2, x + 1);
      double bottom_left = MAX (b->x1, x);
      double bottom_right = MIN (b->x2, x + 1);
      double c = 0.5 * dy * ((top_right - top_left) + (bottom_right - bottom_left));

      /* When converting to [0,255], we round up. This is intended
       * to prevent the problem of pixels that get divided into
       * multiple slices not being fully black.
       */
      int ic = c * 256;

      dest[x] = MIN (dest[x] + ic, 255);
    }
}

static void
interpolate_position (Position *result,
		      Position *top,
		      Position *bottom,
		      double    val,
		      double    val1,
		      double    val2)
{
  result->y  = (top->y *  (val2 - val) + bottom->y *  (val - val1)) / (val2 - val1);
  result->x1 = (top->x1 * (val2 - val) + bottom->x1 * (val - val1)) / (val2 - val1);
  result->x2 = (top->x2 * (val2 - val) + bottom->x2 * (val - val1)) / (val2 - val1);
}

/* This draws a trapezoid with the parallel sides aligned with
 * the X axis. We do this by subdividing the trapezoid vertically
 * into thin slices (themselves trapezoids) where two edge sides are each
 * contained within a single pixel and then rasterizing each
 * slice. There are frequently multiple slices within a single
 * line so we have to accumulate to get the final result.
 */
static void
draw_trap (FT_Bitmap *bitmap,
	   double     y1,
	   double     x11,
	   double     x21,
	   double     y2,
	   double     x12,
	   double     x22)
{
  Position pos;
  Position t;
  Position b;
  gboolean done = FALSE;

  if (y1 == y2)
    return;

  pos.y = t.y = y1;
  pos.x1 = t.x1 = x11;
  pos.x2 = t.x2 = x21;
  b.y = y2;
  b.x1 = x12;
  b.x2 = x22;

  while (!done)
    {
      Position pos_next;
      double y_next, x1_next, x2_next;
      double ix1, ix2;

      /* The algorithm here is written to emphasize simplicity and
       * numerical stability as opposed to speed.
       *
       * While the end result is slicing up the polygon vertically,
       * conceptually we aren't walking in the X direction, rather we
       * are walking along the edges. When we compute crossing of
       * horizontal pixel boundaries, we use the X coordinate as the
       * interpolating variable, when we compute crossing for vertical
       * pixel boundaries, we use the Y coordinate.
       *
       * This allows us to handle almost exactly horizontal edges without
       * running into difficulties. (Almost exactly horizontal edges
       * come up frequently due to inexactness in computing, say,
       * a 90 degree rotation transformation)
       */

      pos_next = b;
      done = TRUE;

      /* Check for crossing vertical pixel boundaries */
      y_next = floor (pos.y) + 1;
      if (y_next < pos_next.y)
	{
	  interpolate_position (&pos_next, &t, &b,
				y_next, t.y, b.y);
	  pos_next.y = y_next;
	  done = FALSE;
	}

      /* Check left side for crossing horizontal pixel boundaries */
      ix1 = floor (pos.x1);

      if (b.x1 < t.x1)
	{
	  if (ix1 == pos.x1)
	    x1_next = ix1 - 1;
	  else
	    x1_next = ix1;

	  if (x1_next > pos_next.x1)
	    {
	      interpolate_position (&pos_next, &t, &b,
				    x1_next, t.x1, b.x1);
	      pos_next.x1 = x1_next;
	      done = FALSE;
	    }
	}
      else if (b.x1 > t.x1)
	{
	  x1_next = ix1 + 1;

	  if (x1_next < pos_next.x1)
	    {
	      interpolate_position (&pos_next, &t, &b,
				    x1_next, t.x1, b.x1);
	      pos_next.x1 = x1_next;
	      done = FALSE;
	    }
	}

      /* Check right side for crossing horizontal pixel boundaries */
      ix2 = floor (pos.x2);

      if (b.x2 < t.x2)
	{
	  if (ix2 == pos.x2)
	    x2_next = ix2 - 1;
	  else
	    x2_next = ix2;

	  if (x2_next > pos_next.x2)
	    {
	      interpolate_position (&pos_next, &t, &b,
				    x2_next, t.x2, b.x2);
	      pos_next.x2 = x2_next;
	      done = FALSE;
	    }
	}
      else if (x22 > x21)
	{
	  x2_next = ix2 + 1;

	  if (x2_next < pos_next.x2)
	    {
	      interpolate_position (&pos_next, &t, &b,
				    x2_next, t.x2, b.x2);
	      pos_next.x2 = x2_next;
	      done = FALSE;
	    }
	}

      draw_simple_trap (bitmap, &pos, &pos_next);
      pos = pos_next;
    }
}

typedef struct
{
  double x, y;
} Point;

static void
to_device (const PangoMatrix *matrix,
	   double             x,
	   double             y,
	   Point             *result)
{
  result->x = (x * matrix->xx + y * matrix->xy) / PANGO_SCALE + matrix->x0;
  result->y = (x * matrix->yx + y * matrix->yy) / PANGO_SCALE + matrix->y0;
}

static int
compare_points (const void *a,
		const void *b)
{
  const Point *pa = a;
  const Point *pb = b;

  if (pa->y < pb->y)
    return -1;
  else if (pa->y > pb->y)
    return 1;
  else if (pa->x < pb->x)
    return -1;
  else if (pa->x > pb->x)
    return 1;
  else
    return 0;
}

/**
 * _pango_ft2_draw_rect:
 * @bitmap: a #FT_Bitmap
 * @matrix: a #PangoMatrix giving the user to device transformation,
 *          or %NULL for the identity.
 * @x: X coordinate of rectangle, in Pango units in user coordinate system
 * @y: Y coordinate of rectangle, in Pango units in user coordinate system
 * @width: width of rectangle, in Pango units in user coordinate system
 * @height: height of rectangle, in Pango units in user coordinate system
 *
 * Render an axis aligned rectangle in user coordinates onto
 * a bitmap after transformation by the given matrix. Rendering
 * is done anti-aliased.
 **/
void
_pango_ft2_draw_rect (FT_Bitmap          *bitmap,
		      const PangoMatrix  *matrix,
		      int                 x,
		      int                 y,
		      int                 width,
		      int                 height)
{
  static const PangoMatrix identity = PANGO_MATRIX_INIT;
  Point points[4];

  if (!matrix)
    matrix = &identity;

  /* Convert the points to device coordinates, and sort
   * in ascending Y order. (Ordering by X for ties)
   */
  to_device (matrix, x, y, &points[0]);
  to_device (matrix, x + width, y, &points[1]);
  to_device (matrix, x, y + height, &points[2]);
  to_device (matrix, x + width, y + height, &points[3]);

  qsort (points, 4, sizeof (Point), compare_points);

  /* There are essentially three cases. (There is a fourth
   * case where trapezoid B is degenerate and we just have
   * two triangles, but we don't need to handle it separately.)
   *
   *     1            2             3
   *
   *     ______       /\           /\
   *    /     /      /A \         /A \
   *   /  B  /      /____\       /____\
   *  /_____/      /  B  /       \  B  \
   *              /_____/         \_____\
   *              \ C  /           \ C  /
   *               \  /             \  /
   *                \/               \/
   */
  if (points[0].y == points[1].y)
    {
     /* Case 1 (pure shear) */
      draw_trap (bitmap,                                 /* B */
		 points[0].y, points[0].x, points[1].x,
		 points[2].y, points[2].x, points[3].x);
    }
  else if (points[1].x < points[2].x)
    {
      /* Case 2 */
      double tmp_width = ((points[2].x - points[0].x) * (points[1].y - points[0].y)) / (points[2].y - points[0].y);
      double base_width = tmp_width + points[0].x - points[1].x;

      draw_trap (bitmap,                                              /* A */
		 points[0].y, points[0].x, points[0].x,
		 points[1].y, points[1].x, points[1].x + base_width);
      draw_trap (bitmap,
		 points[1].y, points[1].x, points[1].x + base_width,  /* B */
		 points[2].y, points[2].x - base_width, points[2].x);
      draw_trap (bitmap,
		 points[2].y, points[2].x - base_width, points[2].x,  /* C */
		 points[3].y, points[3].x, points[3].x);
    }
  else
    {
      /* case 3 */
      double tmp_width = ((points[0].x - points[2].x) * (points[1].y - points[0].y)) / (points[2].y - points[0].y);
      double base_width = tmp_width + points[1].x - points[0].x;

      draw_trap (bitmap,                                             /* A */
		 points[0].y, points[0].x, points[0].x,
		 points[1].y,  points[1].x - base_width, points[1].x);
      draw_trap (bitmap,
		 points[1].y, points[1].x - base_width, points[1].x, /* B */
		 points[2].y, points[2].x, points[2].x + base_width);
      draw_trap (bitmap,
		 points[2].y, points[2].x, points[2].x + base_width, /* C */
		 points[3].y, points[3].x, points[3].x);
    }
}

/* We are drawing an error underline that looks like one of:
 *
 *  /\      /\      /\        /\      /\               -
 * /  \    /  \    /  \      /  \    /  \              |
 * \   \  /\   \  /   /      \   \  /\   \             |
 *  \   \/B \   \/ C /        \   \/B \   \            | height = HEIGHT_SQUARES * square
 *   \ A \  /\ A \  /          \ A \  /\ A \           | 
 *    \   \/  \   \/            \   \/  \   \          |
 *     \  /    \  /              \  /    \  /          |
 *      \/      \/                \/      \/           -
 *
 * |----|
 *   unit_width = (HEIGHT_SQUARES - 1) * square
 *
 * To do this conveniently, we work in a coordinate system where A,B,C
 * are axis aligned rectangles. (If fonts were square, the diagrams
 * would be clearer)
 *
 *             (0,0)    
 *              /\      /\        
 *             /  \    /  \      
 *            /\  /\  /\  /      
 *           /  \/  \/  \/       
 *          /    \  /\  /        
 *      Y axis    \/  \/         
 *                 \  /\          
 *                  \/  \         
 *                       \ X axis
 *
 * Note that the long side in this coordinate system is HEIGHT_SQUARES + 1
 * units long
 *
 * The diagrams above are shown with HEIGHT_SQUARES an integer, but
 * that is actually incidental; the value 2.5 below seems better than
 * either HEIGHT_SQUARES=3 (a little long and skinny) or
 * HEIGHT_SQUARES=2 (a bit short and stubby)
 */
 
#define HEIGHT_SQUARES 2.5

static void
get_total_matrix (PangoMatrix        *total,
		  const PangoMatrix  *global,
		  int                 x,
		  int                 y,
		  int                 square)
{
  PangoMatrix local;
  gdouble scale = 0.5 * square;

  /* The local matrix translates from the axis aligned coordinate system
   * to the original user space coordinate system.
   */
  local.xx = scale;
  local.xy = - scale;
  local.yx = scale;
  local.yy = scale;
  local.x0 = 0;
  local.y0 = 0;

  *total = *global;
  pango_matrix_concat (total, &local);

  total->x0 = (global->xx * x + global->xy * y) / PANGO_SCALE + global->x0;
  total->y0 = (global->yx * x + global->yy * y) / PANGO_SCALE + global->y0;
}

/**
 * _pango_ft2_draw_error_underline:
 * @bitmap: a #FT_Bitmap
 * @matrix: a #PangoMatrix giving the user to device transformation,
 *          or %NULL for the identity.
 * @x: X coordinate of underline, in Pango units in user coordinate system
 * @y: Y coordinate of underline, in Pango units in user coordinate system
 * @width: width of underline, in Pango units in user coordinate system
 * @height: height of underline, in Pango units in user coordinate system
 *
 * Draw a squiggly line that approximately covers the given rectangle
 * in the style of an underline used to indicate a spelling error.
 * (The width of the underline is rounded to an integer number
 * of up/down segments and the resulting rectangle is centered
 * in the original rectangle)
 **/
void
_pango_ft2_draw_error_underline (FT_Bitmap          *bitmap,
				 const PangoMatrix  *matrix,
				 int                 x,
				 int                 y,
				 int                 width,
				 int                 height)
{

  int square = height / HEIGHT_SQUARES;
  int unit_width = (HEIGHT_SQUARES - 1) * square;
  int width_units = (width + unit_width / 2) / unit_width;
  static const PangoMatrix identity = PANGO_MATRIX_INIT;

  x += (width - width_units * unit_width) / 2;
  width = width_units * unit_width;

  if (!matrix)
    matrix = &identity;

  while (TRUE)
    {
      PangoMatrix total;
      get_total_matrix (&total, matrix, x, y, square);

      _pango_ft2_draw_rect (bitmap, &total,              /* A */
			    0,                      0,
			    HEIGHT_SQUARES * 2 - 1, 1);

      if (width_units > 2)
	{
	  _pango_ft2_draw_rect (bitmap, &total,
				HEIGHT_SQUARES * 2 - 2, - (HEIGHT_SQUARES * 2 - 3),
				1,                      HEIGHT_SQUARES * 2 - 3); /* B */
	  width_units -= 2;
	  x += unit_width * 2;
	}
      else if (width_units == 2)
	{
	  _pango_ft2_draw_rect (bitmap, &total,
				HEIGHT_SQUARES * 2 - 2, - (HEIGHT_SQUARES * 2 - 2),
				1,                      HEIGHT_SQUARES * 2 - 2); /* C */
	  break;
	}
      else
	break;
    }
}