1 --- cairo-1.4.12/src/cairo-ft-font.c.orig 2007-11-27 07:20:12.000000000 +0100
2 +++ cairo-1.4.12/src/cairo-ft-font.c 2007-11-28 19:54:09.894901361 +0100
4 #include FT_SYNTHESIS_H
7 +#include FT_LCD_FILTER_H
9 #define DOUBLE_TO_26_6(d) ((FT_F26Dot6)((d) * 64.0))
10 #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
11 #define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0))
12 @@ -702,23 +704,300 @@
13 return CAIRO_STATUS_SUCCESS;
16 -/* Empirically-derived subpixel filtering values thanks to Keith
17 - * Packard and libXft. */
18 -static const int filters[3][3] = {
21 - { 65538*4/7,65538*2/7,65538*1/7 },
23 - { 65536*1/4, 65536*2/4, 65537*1/4 },
25 - { 65538*1/7,65538*2/7,65538*4/7 },
26 +/* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
27 + * into a different format. For example, we want to convert a
28 + * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
29 + * ARGB or ABGR bitmap.
31 + * this function prepares a target descriptor for this operation.
33 + * input :: target bitmap descriptor. The function will set its
34 + * 'width', 'rows' and 'pitch' fields, and only these
36 + * slot :: the glyph slot containing the source bitmap. this
37 + * function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
39 + * mode :: the requested final rendering mode. supported values are
40 + * MONO, NORMAL (i.e. gray), LCD and LCD_V
42 + * the function returns the size in bytes of the corresponding buffer,
43 + * it's up to the caller to allocate the corresponding memory block
44 + * before calling _fill_xrender_bitmap
46 + * it also returns -1 in case of error (e.g. incompatible arguments,
47 + * like trying to convert a gray bitmap into a monochrome one)
50 +_compute_xrender_bitmap_size( FT_Bitmap* target,
52 + FT_Render_Mode mode )
55 + int width, height, pitch;
57 + if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
60 + // compute the size of the final bitmap
61 + ftbit = &slot->bitmap;
63 + width = ftbit->width;
64 + height = ftbit->rows;
65 + pitch = (width+3) & ~3;
67 + switch ( ftbit->pixel_mode )
69 + case FT_PIXEL_MODE_MONO:
70 + if ( mode == FT_RENDER_MODE_MONO )
72 + pitch = (((width+31) & ~31) >> 3);
77 + case FT_PIXEL_MODE_GRAY:
78 + if ( mode == FT_RENDER_MODE_LCD ||
79 + mode == FT_RENDER_MODE_LCD_V )
81 + /* each pixel is replicated into a 32-bit ARGB value */
86 + case FT_PIXEL_MODE_LCD:
87 + if ( mode != FT_RENDER_MODE_LCD )
90 + /* horz pixel triplets are packed into 32-bit ARGB values */
95 + case FT_PIXEL_MODE_LCD_V:
96 + if ( mode != FT_RENDER_MODE_LCD_V )
99 + /* vert pixel triplets are packed into 32-bit ARGB values */
104 + default: /* unsupported source format */
108 + target->width = width;
109 + target->rows = height;
110 + target->pitch = pitch;
111 + target->buffer = NULL;
113 + return pitch * height;
116 +/* this functions converts the glyph bitmap found in a FT_GlyphSlot
117 + * into a different format (see _compute_xrender_bitmap_size)
119 + * you should call this function after _compute_xrender_bitmap_size
121 + * target :: target bitmap descriptor. Note that its 'buffer' pointer
122 + * must point to memory allocated by the caller
124 + * slot :: the glyph slot containing the source bitmap
126 + * mode :: the requested final rendering mode
128 + * bgr :: boolean, set if BGR or VBGR pixel ordering is needed
131 +_fill_xrender_bitmap( FT_Bitmap* target,
133 + FT_Render_Mode mode,
136 + FT_Bitmap* ftbit = &slot->bitmap;
137 + unsigned char* srcLine = ftbit->buffer;
138 + unsigned char* dstLine = target->buffer;
139 + int src_pitch = ftbit->pitch;
140 + int width = target->width;
141 + int height = target->rows;
142 + int pitch = target->pitch;
146 + subpixel = ( mode == FT_RENDER_MODE_LCD ||
147 + mode == FT_RENDER_MODE_LCD_V );
149 + if ( src_pitch < 0 )
150 + srcLine -= src_pitch*(ftbit->rows-1);
152 + target->pixel_mode = ftbit->pixel_mode;
154 + switch ( ftbit->pixel_mode )
156 + case FT_PIXEL_MODE_MONO:
157 + if ( subpixel ) /* convert mono to ARGB32 values */
159 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
163 + for ( x = 0; x < width; x++ )
165 + if ( srcLine[(x >> 3)] & (0x80 >> (x & 7)) )
166 + ((unsigned int*)dstLine)[x] = 0xffffffffU;
169 + target->pixel_mode = FT_PIXEL_MODE_LCD;
171 + else if ( mode == FT_RENDER_MODE_NORMAL ) /* convert mono to 8-bit gray */
173 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
177 + for ( x = 0; x < width; x++ )
179 + if ( srcLine[(x >> 3)] & (0x80 >> (x & 7)) )
183 + target->pixel_mode = FT_PIXEL_MODE_GRAY;
185 + else /* copy mono to mono */
187 + int bytes = (width+7) >> 3;
189 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
190 + memcpy( dstLine, srcLine, bytes );
194 + case FT_PIXEL_MODE_GRAY:
195 + if ( subpixel ) /* convert gray to ARGB32 values */
197 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
200 + unsigned int* dst = (unsigned int*)dstLine;
202 + for ( x = 0; x < width; x++ )
204 + unsigned int pix = srcLine[x];
207 + pix |= (pix << 16);
212 + target->pixel_mode = FT_PIXEL_MODE_LCD;
214 + else /* copy gray into gray */
216 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
217 + memcpy( dstLine, srcLine, width );
221 + case FT_PIXEL_MODE_LCD:
224 + /* convert horizontal RGB into ARGB32 */
225 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
228 + unsigned char* src = srcLine;
229 + unsigned int* dst = (unsigned int*)dstLine;
231 + for ( x = 0; x < width; x++, src += 3 )
235 + pix = ((unsigned int)src[0] << 16) |
236 + ((unsigned int)src[1] << 8) |
237 + ((unsigned int)src[2] ) |
238 + ((unsigned int)src[1] << 24) ;
246 + /* convert horizontal BGR into ARGB32 */
247 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
250 + unsigned char* src = srcLine;
251 + unsigned int* dst = (unsigned int*)dstLine;
253 + for ( x = 0; x < width; x++, src += 3 )
257 + pix = ((unsigned int)src[2] << 16) |
258 + ((unsigned int)src[1] << 8) |
259 + ((unsigned int)src[0] ) |
260 + ((unsigned int)src[1] << 24) ;
268 + default: /* FT_PIXEL_MODE_LCD_V */
269 + /* convert vertical RGB into ARGB32 */
272 + for ( h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch )
275 + unsigned char* src = srcLine;
276 + unsigned int* dst = (unsigned int*)dstLine;
278 + for ( x = 0; x < width; x++, src += 1 )
282 + pix = ((unsigned int)src[0] << 16) |
283 + ((unsigned int)src[src_pitch] << 8) |
284 + ((unsigned int)src[src_pitch*2] ) |
287 + pix = ((unsigned int)src[0] << 16) |
288 + ((unsigned int)src[src_pitch] << 8) |
289 + ((unsigned int)src[src_pitch*2] ) |
290 + ((unsigned int)src[src_pitch] << 24) ;
292 - { 65538*9/13,65538*3/13,65538*1/13 },
294 - { 65538*1/6, 65538*4/6, 65538*1/6 },
296 - { 65538*1/13,65538*3/13,65538*9/13 },
304 + for ( h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch )
307 + unsigned char* src = srcLine;
308 + unsigned int* dst = (unsigned int*)dstLine;
310 + for ( x = 0; x < width; x++, src += 1 )
314 + pix = ((unsigned int)src[src_pitch*2] << 16) |
315 + ((unsigned int)src[src_pitch] << 8) |
316 + ((unsigned int)src[0] ) |
317 + ((unsigned int)src[src_pitch] << 24) ;
327 /* Fills in val->image with an image surface created from @bitmap
330 int width, height, stride;
332 int format = CAIRO_FORMAT_A8;
333 - cairo_bool_t subpixel = FALSE;
334 + cairo_image_surface_t *image;
336 width = bitmap->width;
337 height = bitmap->rows;
343 #ifndef WORDS_BIGENDIAN
345 unsigned char *d = data;
346 @@ -784,17 +1062,15 @@
351 format = CAIRO_FORMAT_A1;
354 case FT_PIXEL_MODE_LCD:
355 case FT_PIXEL_MODE_LCD_V:
356 case FT_PIXEL_MODE_GRAY:
357 - switch (font_options->antialias) {
358 - case CAIRO_ANTIALIAS_DEFAULT:
359 - case CAIRO_ANTIALIAS_GRAY:
360 - case CAIRO_ANTIALIAS_NONE:
362 + if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL)
364 stride = bitmap->pitch;
366 data = bitmap->buffer;
367 @@ -807,105 +1083,16 @@
368 memcpy (data, bitmap->buffer, stride * height);
370 format = CAIRO_FORMAT_A8;
372 - case CAIRO_ANTIALIAS_SUBPIXEL: {
374 - unsigned char *in_line, *out_line, *in;
376 - unsigned int red, green, blue;
380 - unsigned char *data_rgba;
381 - unsigned int width_rgba, stride_rgba;
385 - switch (font_options->subpixel_order) {
386 - case CAIRO_SUBPIXEL_ORDER_DEFAULT:
387 - case CAIRO_SUBPIXEL_ORDER_RGB:
388 - case CAIRO_SUBPIXEL_ORDER_BGR:
393 - case CAIRO_SUBPIXEL_ORDER_VRGB:
394 - case CAIRO_SUBPIXEL_ORDER_VBGR:
400 - * Filter the glyph to soften the color fringes
402 - width_rgba = width;
403 - stride = bitmap->pitch;
404 - stride_rgba = (width_rgba * 4 + 3) & ~3;
405 - data_rgba = calloc (stride_rgba, height);
406 - if (data_rgba == NULL) {
408 - free (bitmap->buffer);
409 - _cairo_error (CAIRO_STATUS_NO_MEMORY);
410 - return CAIRO_STATUS_NO_MEMORY;
413 + // if we get there, the data from the source bitmap
414 + // really comes from _fill_xrender_bitmap, and is
415 + // made of 32-bit ARGB or ABGR values
416 + assert(own_buffer != 0);
417 + assert(bitmap->pixel_mode != FT_PIXEL_MODE_GRAY);
420 - switch (font_options->subpixel_order) {
421 - case CAIRO_SUBPIXEL_ORDER_VRGB:
423 - case CAIRO_SUBPIXEL_ORDER_DEFAULT:
424 - case CAIRO_SUBPIXEL_ORDER_RGB:
430 - case CAIRO_SUBPIXEL_ORDER_VBGR:
432 - case CAIRO_SUBPIXEL_ORDER_BGR:
438 - in_line = bitmap->buffer;
439 - out_line = data_rgba;
440 - for (y = 0; y < height; y++)
443 - out = (unsigned int *) out_line;
444 - in_line += stride * vmul;
445 - out_line += stride_rgba;
446 - for (x = 0; x < width * hmul; x += hmul)
448 - red = green = blue = 0;
450 - for (s = 0; s < 3; s++)
452 - red += filters[rf][s]*in[x+o];
453 - green += filters[gf][s]*in[x+o];
454 - blue += filters[bf][s]*in[x+o];
458 - green = green / 65536;
459 - blue = blue / 65536;
460 - *out++ = (green << 24) | (red << 16) | (green << 8) | blue;
464 - /* Images here are stored in native format. The
465 - * backend must convert to its own format as needed
469 - free (bitmap->buffer);
471 - stride = stride_rgba;
472 + data = bitmap->buffer;
473 + stride = bitmap->pitch;
474 format = CAIRO_FORMAT_ARGB32;
480 case FT_PIXEL_MODE_GRAY2:
481 @@ -918,19 +1105,20 @@
482 return CAIRO_STATUS_NO_MEMORY;
485 - *surface = (cairo_image_surface_t *)
487 + *surface = image = (cairo_image_surface_t *)
488 cairo_image_surface_create_for_data (data,
490 width, height, stride);
491 - if ((*surface)->base.status) {
492 + if (image->base.status) {
494 return CAIRO_STATUS_NO_MEMORY;
498 - pixman_image_set_component_alpha ((*surface)->pixman_image, TRUE);
499 + if (font_options->antialias == CAIRO_ANTIALIAS_SUBPIXEL)
500 + pixman_image_set_component_alpha (image->pixman_image, TRUE);
502 - _cairo_image_surface_assume_ownership_of_data ((*surface));
503 + _cairo_image_surface_assume_ownership_of_data (image);
505 return CAIRO_STATUS_SUCCESS;
507 @@ -955,16 +1143,44 @@
508 cairo_font_options_t *font_options,
509 cairo_image_surface_t **surface)
511 + int rgba = FC_RGBA_UNKNOWN;
512 FT_GlyphSlot glyphslot = face->glyph;
513 FT_Outline *outline = &glyphslot->outline;
519 unsigned int width, height, stride;
520 - cairo_bool_t subpixel = FALSE;
521 + cairo_format_t format;
522 cairo_status_t status;
524 + FT_Library library = glyphslot->library;
525 + FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
527 + switch (font_options->antialias)
529 + case CAIRO_ANTIALIAS_NONE:
530 + render_mode = FT_RENDER_MODE_MONO;
533 + case CAIRO_ANTIALIAS_SUBPIXEL:
534 + switch (font_options->subpixel_order)
536 + case CAIRO_SUBPIXEL_ORDER_DEFAULT:
537 + case CAIRO_SUBPIXEL_ORDER_RGB:
538 + case CAIRO_SUBPIXEL_ORDER_BGR:
539 + render_mode = FT_RENDER_MODE_LCD;
542 + case CAIRO_SUBPIXEL_ORDER_VRGB:
543 + case CAIRO_SUBPIXEL_ORDER_VBGR:
544 + render_mode = FT_RENDER_MODE_LCD_V;
549 + case CAIRO_ANTIALIAS_DEFAULT:
550 + case CAIRO_ANTIALIAS_GRAY:
551 + render_mode = FT_RENDER_MODE_NORMAL;
554 FT_Outline_Get_CBox (outline, &cbox);
556 @@ -975,20 +1191,19 @@
558 width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
559 height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
560 - stride = (width * hmul + 3) & ~3;
561 + stride = (width + 3) & ~3;
563 if (width * height == 0) {
564 - cairo_format_t format;
565 /* Looks like fb handles zero-sized images just fine */
566 - switch (font_options->antialias) {
567 - case CAIRO_ANTIALIAS_NONE:
568 + switch (render_mode)
570 + case FT_RENDER_MODE_MONO:
571 format = CAIRO_FORMAT_A1;
573 - case CAIRO_ANTIALIAS_SUBPIXEL:
574 - format= CAIRO_FORMAT_ARGB32;
575 + case FT_RENDER_MODE_LCD:
576 + case FT_RENDER_MODE_LCD_V:
577 + format = CAIRO_FORMAT_ARGB32;
579 - case CAIRO_ANTIALIAS_DEFAULT:
580 - case CAIRO_ANTIALIAS_GRAY:
582 format = CAIRO_FORMAT_A8;
584 @@ -1000,75 +1215,70 @@
585 return CAIRO_STATUS_NO_MEMORY;
588 - matrix.xx = matrix.yy = 0x10000L;
589 - matrix.xy = matrix.yx = 0;
592 - switch (font_options->antialias) {
593 - case CAIRO_ANTIALIAS_NONE:
594 - bitmap.pixel_mode = FT_PIXEL_MODE_MONO;
595 - bitmap.num_grays = 1;
596 - stride = ((width + 31) & -32) >> 3;
597 + switch (render_mode)
599 + case FT_RENDER_MODE_LCD:
600 + if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR ) {
601 + rgba = FC_RGBA_BGR;
603 + rgba = FC_RGBA_RGB;
606 - case CAIRO_ANTIALIAS_DEFAULT:
607 - case CAIRO_ANTIALIAS_GRAY:
608 - bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
609 - bitmap.num_grays = 256;
610 - stride = (width + 3) & -4;
611 + case FT_RENDER_MODE_LCD_V:
612 + if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR ) {
613 + rgba = FC_RGBA_VBGR;
615 + rgba = FC_RGBA_VRGB;
618 - case CAIRO_ANTIALIAS_SUBPIXEL:
619 - switch (font_options->subpixel_order) {
620 - case CAIRO_SUBPIXEL_ORDER_RGB:
621 - case CAIRO_SUBPIXEL_ORDER_BGR:
622 - case CAIRO_SUBPIXEL_ORDER_DEFAULT:
628 - case CAIRO_SUBPIXEL_ORDER_VRGB:
629 - case CAIRO_SUBPIXEL_ORDER_VBGR:
635 - FT_Outline_Transform (outline, &matrix);
637 - bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
638 - bitmap.num_grays = 256;
639 - stride = (width * hmul + 3) & -4;
642 - bitmap.pitch = stride;
643 - bitmap.width = width * hmul;
644 - bitmap.rows = height * vmul;
645 - bitmap.buffer = calloc (stride, bitmap.rows);
646 + FT_Library_SetLcdFilter( library, FT_LCD_FILTER_DEFAULT );
648 + fterror = FT_Render_Glyph( face->glyph, render_mode );
650 - if (bitmap.buffer == NULL) {
651 + FT_Library_SetLcdFilter( library, FT_LCD_FILTER_NONE );
653 + if (fterror != 0) {
654 _cairo_error (CAIRO_STATUS_NO_MEMORY);
655 return CAIRO_STATUS_NO_MEMORY;
658 - FT_Outline_Translate (outline, -cbox.xMin*hmul, -cbox.yMin*vmul);
659 + bitmap_size = _compute_xrender_bitmap_size( &bitmap,
662 + if ( bitmap_size < 0 ) {
663 + _cairo_error (CAIRO_STATUS_NO_MEMORY);
664 + return CAIRO_STATUS_NO_MEMORY;
667 - if (FT_Outline_Get_Bitmap (glyphslot->library, outline, &bitmap) != 0) {
668 + bitmap.buffer = calloc(1, bitmap_size);
669 + if (bitmap.buffer == NULL) {
670 free (bitmap.buffer);
671 _cairo_error (CAIRO_STATUS_NO_MEMORY);
672 return CAIRO_STATUS_NO_MEMORY;
675 + _fill_xrender_bitmap( &bitmap, face->glyph, render_mode,
676 + (rgba == FC_RGBA_BGR || rgba == FC_RGBA_VBGR) );
678 + // NOTE: _get_bitmap_surface will free bitmap.buffer if there is an error
679 status = _get_bitmap_surface (&bitmap, TRUE, font_options, surface);
685 * Note: the font's coordinate system is upside down from ours, so the
686 * Y coordinate of the control box needs to be negated.
688 cairo_surface_set_device_offset (&(*surface)->base,
689 - floor ((double) cbox.xMin / 64.0),
690 - floor (-(double) cbox.yMax / 64.0));
691 + (double) glyphslot->bitmap_left,
692 + (double)-glyphslot->bitmap_top);
695 return CAIRO_STATUS_SUCCESS;
697 @@ -1442,11 +1652,11 @@
698 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
699 case CAIRO_SUBPIXEL_ORDER_RGB:
700 case CAIRO_SUBPIXEL_ORDER_BGR:
701 - load_target |= FT_LOAD_TARGET_LCD;
702 + load_target = FT_LOAD_TARGET_LCD;
704 case CAIRO_SUBPIXEL_ORDER_VRGB:
705 case CAIRO_SUBPIXEL_ORDER_VBGR:
706 - load_target |= FT_LOAD_TARGET_LCD_V;
707 + load_target = FT_LOAD_TARGET_LCD_V;