Skip to content
0.2.0-dev Download ZIP
696 lines ·19.6 KB Raw
1/****************************************************************************
2 *
3 * ftsmooth.c
4 *
5 * Anti-aliasing renderer interface (body).
6 *
7 * Copyright (C) 2000-2026 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
15 *
16 */
17
18
19#include <freetype/internal/ftdebug.h>
20#include <freetype/internal/ftobjs.h>
21#include <freetype/ftoutln.h>
22#include "ftsmooth.h"
23#include "ftgrays.h"
24
25#include "ftsmerrs.h"
26
27
28 /* sets render-specific mode */
29 static FT_Error
30 ft_smooth_set_mode( FT_Renderer render,
31 FT_ULong mode_tag,
32 FT_Pointer data )
33 {
34 /* we simply pass it to the raster */
35 return render->clazz->raster_class->raster_set_mode( render->raster,
36 mode_tag,
37 data );
38 }
39
40 /* transform a given glyph image */
41 static FT_Error
42 ft_smooth_transform( FT_Renderer render,
43 FT_GlyphSlot slot,
44 const FT_Matrix* matrix,
45 const FT_Vector* delta )
46 {
47 FT_Error error = FT_Err_Ok;
48
49
50 if ( slot->format != render->glyph_format )
51 {
52 error = FT_THROW( Invalid_Argument );
53 goto Exit;
54 }
55
56 if ( matrix )
57 FT_Outline_Transform( &slot->outline, matrix );
58
59 if ( delta )
60 FT_Outline_Translate( &slot->outline, delta->x, delta->y );
61
62 Exit:
63 return error;
64 }
65
66
67 /* return the glyph's control box */
68 static void
69 ft_smooth_get_cbox( FT_Renderer render,
70 FT_GlyphSlot slot,
71 FT_BBox* cbox )
72 {
73 FT_ZERO( cbox );
74
75 if ( slot->format == render->glyph_format )
76 FT_Outline_Get_CBox( &slot->outline, cbox );
77 }
78
79 typedef struct TOrigin_
80 {
81 unsigned char* origin; /* pixmap origin at the bottom-left */
82 int pitch; /* pitch to go down one row */
83 unsigned char wght[5]; /* filtering weights */
84
85 } TOrigin;
86
87#ifndef FT_CONFIG_OPTION_SUBPIXEL_RENDERING
88
89 /* initialize renderer -- init its raster */
90 static FT_Error
91 ft_smooth_init( FT_Module module ) /* FT_Renderer */
92 {
93 FT_Renderer render = (FT_Renderer)module;
94
95 FT_Vector* sub = render->root.library->lcd_geometry;
96
97
98 /* set up default subpixel geometry for striped RGB panels. */
99 sub[0].x = -21;
100 sub[0].y = 0;
101 sub[1].x = 0;
102 sub[1].y = 0;
103 sub[2].x = 21;
104 sub[2].y = 0;
105
106 render->clazz->raster_class->raster_reset( render->raster, NULL, 0 );
107
108 return 0;
109 }
110
111
112 /* This function writes every third byte in direct rendering mode */
113 static void
114 ft_smooth_lcd_spans( int y,
115 int count,
116 const FT_Span* spans,
117 void* target_ ) /* TOrigin* */
118 {
119 TOrigin* target = (TOrigin*)target_;
120
121 unsigned char* dst_line = target->origin - y * target->pitch;
122 unsigned char* dst;
123 unsigned short w;
124
125
126 for ( ; count--; spans++ )
127 for ( dst = dst_line + (unsigned short)spans->x * 3,
128 w = spans->len; w--; dst += 3 )
129 *dst = spans->coverage;
130 }
131
132
133 static FT_Error
134 ft_smooth_raster_lcd( FT_Renderer render,
135 FT_Outline* outline,
136 FT_Bitmap* bitmap )
137 {
138 FT_Error error = FT_Err_Ok;
139 FT_Vector* sub = render->root.library->lcd_geometry;
140 FT_Pos x, y;
141
142 FT_Raster_Params params;
143 TOrigin target;
144
145
146 /* Render 3 separate coverage bitmaps, shifting the outline. */
147 /* Set up direct rendering to record them on each third byte. */
148 params.source = outline;
149 params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
150 params.gray_spans = ft_smooth_lcd_spans;
151 params.user = &target;
152
153 params.clip_box.xMin = 0;
154 params.clip_box.yMin = 0;
155 params.clip_box.xMax = bitmap->width / 3;
156 params.clip_box.yMax = bitmap->rows;
157
158 if ( bitmap->pitch < 0 )
159 target.origin = bitmap->buffer;
160 else
161 target.origin = bitmap->buffer
162 + ( bitmap->rows - 1 ) * (unsigned int)bitmap->pitch;
163
164 target.pitch = bitmap->pitch;
165
166 FT_Outline_Translate( outline,
167 -sub[0].x,
168 -sub[0].y );
169 error = render->raster_render( render->raster, &params );
170 x = sub[0].x;
171 y = sub[0].y;
172 if ( error )
173 goto Exit;
174
175 target.origin++;
176 FT_Outline_Translate( outline,
177 sub[0].x - sub[1].x,
178 sub[0].y - sub[1].y );
179 error = render->raster_render( render->raster, &params );
180 x = sub[1].x;
181 y = sub[1].y;
182 if ( error )
183 goto Exit;
184
185 target.origin++;
186 FT_Outline_Translate( outline,
187 sub[1].x - sub[2].x,
188 sub[1].y - sub[2].y );
189 error = render->raster_render( render->raster, &params );
190 x = sub[2].x;
191 y = sub[2].y;
192
193 Exit:
194 FT_Outline_Translate( outline, x, y );
195
196 return error;
197 }
198
199
200 static FT_Error
201 ft_smooth_raster_lcdv( FT_Renderer render,
202 FT_Outline* outline,
203 FT_Bitmap* bitmap )
204 {
205 FT_Error error = FT_Err_Ok;
206 int pitch = bitmap->pitch;
207 FT_Vector* sub = render->root.library->lcd_geometry;
208 FT_Pos x, y;
209
210 FT_Raster_Params params;
211
212
213 params.target = bitmap;
214 params.source = outline;
215 params.flags = FT_RASTER_FLAG_AA;
216
217 /* Render 3 separate coverage bitmaps, shifting the outline. */
218 /* Notice that the subpixel geometry vectors are rotated. */
219 /* Triple the pitch to render on each third row. */
220 bitmap->pitch *= 3;
221 bitmap->rows /= 3;
222
223 FT_Outline_Translate( outline,
224 -sub[0].y,
225 sub[0].x );
226 error = render->raster_render( render->raster, &params );
227 x = sub[0].y;
228 y = -sub[0].x;
229 if ( error )
230 goto Exit;
231
232 bitmap->buffer += pitch;
233 FT_Outline_Translate( outline,
234 sub[0].y - sub[1].y,
235 sub[1].x - sub[0].x );
236 error = render->raster_render( render->raster, &params );
237 x = sub[1].y;
238 y = -sub[1].x;
239 bitmap->buffer -= pitch;
240 if ( error )
241 goto Exit;
242
243 bitmap->buffer += 2 * pitch;
244 FT_Outline_Translate( outline,
245 sub[1].y - sub[2].y,
246 sub[2].x - sub[1].x );
247 error = render->raster_render( render->raster, &params );
248 x = sub[2].y;
249 y = -sub[2].x;
250 bitmap->buffer -= 2 * pitch;
251
252 Exit:
253 FT_Outline_Translate( outline, x, y );
254
255 bitmap->pitch /= 3;
256 bitmap->rows *= 3;
257
258 return error;
259 }
260
261#else /* FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
262
263 /* initialize renderer -- init its raster */
264 static FT_Error
265 ft_smooth_init( FT_Module module ) /* FT_Renderer */
266 {
267 FT_Renderer render = (FT_Renderer)module;
268
269
270 /* set up default LCD filtering */
271 FT_Library_SetLcdFilter( render->root.library, FT_LCD_FILTER_DEFAULT );
272
273 render->clazz->raster_class->raster_reset( render->raster, NULL, 0 );
274
275 return 0;
276 }
277
278
279 /* This function applies a horizontal filter in direct rendering mode */
280 static void
281 ft_smooth_lcd_spans( int y,
282 int count,
283 const FT_Span* spans,
284 void* target_ ) /* TOrigin* */
285 {
286 TOrigin* target = (TOrigin*)target_;
287
288 unsigned char* dst_line = target->origin - y * target->pitch - 2;
289 unsigned char* dst;
290 unsigned short w;
291
292
293 for ( ; count--; spans++ )
294 for ( dst = dst_line + (unsigned short)spans->x,
295 w = spans->len; w--; dst++ )
296 {
297 dst[0] += ( spans->coverage * target->wght[0] + 85 ) >> 8;
298 dst[1] += ( spans->coverage * target->wght[1] + 85 ) >> 8;
299 dst[2] += ( spans->coverage * target->wght[2] + 85 ) >> 8;
300 dst[3] += ( spans->coverage * target->wght[3] + 85 ) >> 8;
301 dst[4] += ( spans->coverage * target->wght[4] + 85 ) >> 8;
302 }
303 }
304
305
306 static FT_Error
307 ft_smooth_raster_lcd( FT_Renderer render,
308 FT_Outline* outline,
309 FT_Bitmap* bitmap )
310 {
311 FT_Error error = FT_Err_Ok;
312 FT_Vector* points = outline->points;
313 FT_Vector* points_end = FT_OFFSET( points, outline->n_points );
314 FT_Vector* vec;
315
316 FT_Raster_Params params;
317 TOrigin target;
318
319
320 if ( render->root.library->lcd_weights[2] )
321 {
322 /* Reject outlines that are too wide for 16-bit FT_Span. */
323 /* Other limits are applied upstream with the same error code. */
324 if ( bitmap->width > 0x7FFF )
325 return FT_THROW( Raster_Overflow );
326
327 /* Set up direct rendering for instant filtering. */
328 params.source = outline;
329 params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
330 params.gray_spans = ft_smooth_lcd_spans;
331 params.user = &target;
332
333 params.clip_box.xMin = 0;
334 params.clip_box.yMin = 0;
335 params.clip_box.xMax = bitmap->width;
336 params.clip_box.yMax = bitmap->rows;
337
338 if ( bitmap->pitch < 0 )
339 target.origin = bitmap->buffer;
340 else
341 target.origin = bitmap->buffer
342 + ( bitmap->rows - 1 ) * (unsigned int)bitmap->pitch;
343
344 target.pitch = bitmap->pitch;
345
346 target.wght[0] = render->root.library->lcd_weights[0];
347 target.wght[1] = render->root.library->lcd_weights[1];
348 target.wght[2] = render->root.library->lcd_weights[2];
349 target.wght[3] = render->root.library->lcd_weights[3];
350 target.wght[4] = render->root.library->lcd_weights[4];
351 }
352 else
353 {
354 params.target = bitmap;
355 params.source = outline;
356 params.flags = FT_RASTER_FLAG_AA;
357 }
358
359 /* implode outline */
360 for ( vec = points; vec < points_end; vec++ )
361 vec->x *= 3;
362
363 /* render outline into the bitmap */
364 error = render->raster_render( render->raster, &params );
365
366 /* deflate outline */
367 for ( vec = points; vec < points_end; vec++ )
368 vec->x /= 3;
369
370 return error;
371 }
372
373
374 /* This function applies a vertical filter in direct rendering mode */
375 static void
376 ft_smooth_lcdv_spans( int y,
377 int count,
378 const FT_Span* spans,
379 void* target_ ) /* TOrigin* */
380 {
381 TOrigin* target = (TOrigin*)target_;
382
383 int pitch = target->pitch;
384 unsigned char* dst_line = target->origin - ( y + 2 ) * pitch;
385 unsigned char* dst;
386 unsigned short w;
387
388
389 for ( ; count--; spans++ )
390 for ( dst = dst_line + (unsigned short)spans->x,
391 w = spans->len; w--; dst++ )
392 {
393 dst[ 0] += ( spans->coverage * target->wght[0] + 85 ) >> 8;
394 dst[ pitch] += ( spans->coverage * target->wght[1] + 85 ) >> 8;
395 dst[2 * pitch] += ( spans->coverage * target->wght[2] + 85 ) >> 8;
396 dst[3 * pitch] += ( spans->coverage * target->wght[3] + 85 ) >> 8;
397 dst[4 * pitch] += ( spans->coverage * target->wght[4] + 85 ) >> 8;
398 }
399 }
400
401 static FT_Error
402 ft_smooth_raster_lcdv( FT_Renderer render,
403 FT_Outline* outline,
404 FT_Bitmap* bitmap )
405 {
406 FT_Error error = FT_Err_Ok;
407 FT_Vector* points = outline->points;
408 FT_Vector* points_end = FT_OFFSET( points, outline->n_points );
409 FT_Vector* vec;
410
411 FT_Raster_Params params;
412 TOrigin target;
413
414
415 if ( render->root.library->lcd_weights[2] )
416 {
417 /* Set up direct rendering for instant filtering. */
418 params.source = outline;
419 params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
420 params.gray_spans = ft_smooth_lcdv_spans;
421 params.user = &target;
422
423 params.clip_box.xMin = 0;
424 params.clip_box.yMin = 0;
425 params.clip_box.xMax = bitmap->width;
426 params.clip_box.yMax = bitmap->rows;
427
428 if ( bitmap->pitch < 0 )
429 target.origin = bitmap->buffer;
430 else
431 target.origin = bitmap->buffer
432 + ( bitmap->rows - 1 ) * (unsigned int)bitmap->pitch;
433
434 target.pitch = bitmap->pitch;
435
436 target.wght[0] = render->root.library->lcd_weights[0];
437 target.wght[1] = render->root.library->lcd_weights[1];
438 target.wght[2] = render->root.library->lcd_weights[2];
439 target.wght[3] = render->root.library->lcd_weights[3];
440 target.wght[4] = render->root.library->lcd_weights[4];
441 }
442 else
443 {
444 params.target = bitmap;
445 params.source = outline;
446 params.flags = FT_RASTER_FLAG_AA;
447 }
448
449 /* implode outline */
450 for ( vec = points; vec < points_end; vec++ )
451 vec->y *= 3;
452
453 /* render outline into the bitmap */
454 error = render->raster_render( render->raster, &params );
455
456 /* deflate outline */
457 for ( vec = points; vec < points_end; vec++ )
458 vec->y /= 3;
459
460 return error;
461 }
462
463#endif /* FT_CONFIG_OPTION_SUBPIXEL_RENDERING */
464
465/* Oversampling scale to be used in rendering overlaps */
466#define SCALE ( 1 << 2 )
467
468 /* This function averages inflated spans in direct rendering mode */
469 static void
470 ft_smooth_overlap_spans( int y,
471 int count,
472 const FT_Span* spans,
473 void* target_ )
474 {
475 TOrigin* target = (TOrigin*)target_;
476
477
478 unsigned char* dst = target->origin - ( y / SCALE ) * target->pitch;
479 unsigned int x, i;
480 unsigned int cover, sum;
481
482
483 /* When accumulating the oversampled spans we need to assure that */
484 /* fully covered pixels are equal to 255 and do not overflow. */
485 /* It is important that the SCALE is a power of 2, each subpixel */
486 /* cover can also reach a power of 2 after rounding, and the total */
487 /* is clamped to 255 when it adds up to 256. */
488 for ( ; count--; spans++ )
489 {
490 cover = ( spans->coverage + SCALE * SCALE / 2 ) / ( SCALE * SCALE );
491 for ( x = 0; x < spans->len; x++ )
492 {
493 i = ( (unsigned short)spans->x + x ) / SCALE;
494 sum = dst[i] + cover;
495 dst[i] = (unsigned char)( sum - ( sum >> 8 ) );
496 }
497 }
498 }
499
500
501 static FT_Error
502 ft_smooth_raster_overlap( FT_Renderer render,
503 FT_Outline* outline,
504 FT_Bitmap* bitmap )
505 {
506 FT_Error error = FT_Err_Ok;
507 FT_Vector* points = outline->points;
508 FT_Vector* points_end = FT_OFFSET( points, outline->n_points );
509 FT_Vector* vec;
510
511 FT_Raster_Params params;
512 TOrigin target;
513
514
515 /* Reject outlines that are too wide for 16-bit FT_Span. */
516 /* Other limits are applied upstream with the same error code. */
517 if ( bitmap->width * SCALE > 0x7FFF )
518 return FT_THROW( Raster_Overflow );
519
520 /* Set up direct rendering to average oversampled spans. */
521 params.source = outline;
522 params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
523 params.gray_spans = ft_smooth_overlap_spans;
524 params.user = &target;
525
526 params.clip_box.xMin = 0;
527 params.clip_box.yMin = 0;
528 params.clip_box.xMax = bitmap->width * SCALE;
529 params.clip_box.yMax = bitmap->rows * SCALE;
530
531 if ( bitmap->pitch < 0 )
532 target.origin = bitmap->buffer;
533 else
534 target.origin = bitmap->buffer
535 + ( bitmap->rows - 1 ) * (unsigned int)bitmap->pitch;
536
537 target.pitch = bitmap->pitch;
538
539 /* inflate outline */
540 for ( vec = points; vec < points_end; vec++ )
541 {
542 vec->x *= SCALE;
543 vec->y *= SCALE;
544 }
545
546 /* render outline into the bitmap */
547 error = render->raster_render( render->raster, &params );
548
549 /* deflate outline */
550 for ( vec = points; vec < points_end; vec++ )
551 {
552 vec->x /= SCALE;
553 vec->y /= SCALE;
554 }
555
556 return error;
557 }
558
559#undef SCALE
560
561 static FT_Error
562 ft_smooth_render( FT_Renderer render,
563 FT_GlyphSlot slot,
564 FT_Render_Mode mode,
565 const FT_Vector* origin )
566 {
567 FT_Error error = FT_Err_Ok;
568 FT_Outline* outline = &slot->outline;
569 FT_Bitmap* bitmap = &slot->bitmap;
570 FT_Memory memory = render->root.memory;
571 FT_Pos x_shift = 0;
572 FT_Pos y_shift = 0;
573
574
575 /* check glyph image format */
576 if ( slot->format != render->glyph_format )
577 {
578 error = FT_THROW( Invalid_Argument );
579 goto Exit;
580 }
581
582 /* check mode */
583 if ( mode != FT_RENDER_MODE_NORMAL &&
584 mode != FT_RENDER_MODE_LIGHT &&
585 mode != FT_RENDER_MODE_LCD &&
586 mode != FT_RENDER_MODE_LCD_V )
587 {
588 error = FT_THROW( Cannot_Render_Glyph );
589 goto Exit;
590 }
591
592 /* release old bitmap buffer */
593 if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
594 {
595 FT_FREE( bitmap->buffer );
596 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
597 }
598
599 if ( ft_glyphslot_preset_bitmap( slot, mode, origin ) )
600 {
601 error = FT_THROW( Raster_Overflow );
602 goto Exit;
603 }
604
605 if ( !bitmap->rows || !bitmap->pitch )
606 goto Exit;
607
608 /* allocate new one */
609 if ( FT_ALLOC_MULT( bitmap->buffer, bitmap->rows, bitmap->pitch ) )
610 goto Exit;
611
612 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
613
614 x_shift = 64 * -slot->bitmap_left;
615 y_shift = 64 * -slot->bitmap_top;
616 if ( bitmap->pixel_mode == FT_PIXEL_MODE_LCD_V )
617 y_shift += 64 * (FT_Int)bitmap->rows / 3;
618 else
619 y_shift += 64 * (FT_Int)bitmap->rows;
620
621 /* translate outline to render it into the bitmap */
622 if ( x_shift || y_shift )
623 FT_Outline_Translate( outline, x_shift, y_shift );
624
625 if ( mode == FT_RENDER_MODE_NORMAL ||
626 mode == FT_RENDER_MODE_LIGHT )
627 {
628 if ( outline->flags & FT_OUTLINE_OVERLAP )
629 error = ft_smooth_raster_overlap( render, outline, bitmap );
630 else
631 {
632 FT_Raster_Params params;
633
634
635 params.target = bitmap;
636 params.source = outline;
637 params.flags = FT_RASTER_FLAG_AA;
638
639 error = render->raster_render( render->raster, &params );
640 }
641 }
642 else
643 {
644 if ( mode == FT_RENDER_MODE_LCD )
645 error = ft_smooth_raster_lcd ( render, outline, bitmap );
646 else if ( mode == FT_RENDER_MODE_LCD_V )
647 error = ft_smooth_raster_lcdv( render, outline, bitmap );
648 }
649
650 Exit:
651 if ( !error )
652 {
653 /* everything is fine; the glyph is now officially a bitmap */
654 slot->format = FT_GLYPH_FORMAT_BITMAP;
655 }
656 else if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP )
657 {
658 FT_FREE( bitmap->buffer );
659 slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP;
660 }
661
662 if ( x_shift || y_shift )
663 FT_Outline_Translate( outline, -x_shift, -y_shift );
664
665 return error;
666 }
667
668
669 FT_DEFINE_RENDERER(
670 ft_smooth_renderer_class,
671
672 FT_MODULE_RENDERER,
673 sizeof ( FT_RendererRec ),
674
675 "smooth",
676 0x10000L,
677 0x20000L,
678
679 NULL, /* module specific interface */
680
681 (FT_Module_Constructor)ft_smooth_init, /* module_init */
682 (FT_Module_Destructor) NULL, /* module_done */
683 (FT_Module_Requester) NULL, /* get_interface */
684
685 FT_GLYPH_FORMAT_OUTLINE,
686
687 (FT_Renderer_RenderFunc) ft_smooth_render, /* render_glyph */
688 (FT_Renderer_TransformFunc)ft_smooth_transform, /* transform_glyph */
689 (FT_Renderer_GetCBoxFunc) ft_smooth_get_cbox, /* get_glyph_cbox */
690 (FT_Renderer_SetModeFunc) ft_smooth_set_mode, /* set_mode */
691
692 (FT_Raster_Funcs*)&ft_grays_raster /* raster_class */
693 )
694
695
696/* END */