1 diff -Naur libXft-2.1.14.orig/src/xftglyphs.c libXft-2.1.14/src/xftglyphs.c
2 --- libXft-2.1.14.orig/src/xftglyphs.c 2009-01-29 21:19:09.000000000 -0200
3 +++ libXft-2.1.14/src/xftglyphs.c 2009-11-01 08:03:09.102347757 -0200
8 -#include <freetype/ftoutln.h>
10 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
11 #include <freetype/ftsynth.h>
14 -static const int filters[3][3] = {
17 -{ 65538*4/7,65538*2/7,65538*1/7 },
19 -{ 65536*1/4, 65536*2/4, 65537*1/4 },
21 -{ 65538*1/7,65538*2/7,65538*4/7 },
22 +#if FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH < 20202
23 +# error "FreeType 2.2.2 or later required to compile this version of libXft"
25 -{ 65538*9/13,65538*3/13,65538*1/13 },
27 -{ 65538*1/6, 65538*4/6, 65538*1/6 },
29 -{ 65538*1/13,65538*3/13,65538*9/13 },
32 +#include FT_OUTLINE_H
33 +#include FT_LCD_FILTER_H
34 +#include FT_SYNTHESIS_H
37 * Validate the memory info for a font
39 font->glyph_memory, glyph_memory);
43 +/* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
44 + * into a different format. For example, we want to convert a
45 + * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
46 + * ARGB or ABGR bitmap.
48 + * this function prepares a target descriptor for this operation.
50 + * input :: target bitmap descriptor. The function will set its
51 + * 'width', 'rows' and 'pitch' fields, and only these
53 + * slot :: the glyph slot containing the source bitmap. this
54 + * function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
56 + * mode :: the requested final rendering mode. supported values are
57 + * MONO, NORMAL (i.e. gray), LCD and LCD_V
59 + * the function returns the size in bytes of the corresponding buffer,
60 + * it's up to the caller to allocate the corresponding memory block
61 + * before calling _fill_xrender_bitmap
63 + * it also returns -1 in case of error (e.g. incompatible arguments,
64 + * like trying to convert a gray bitmap into a monochrome one)
67 +_compute_xrender_bitmap_size( FT_Bitmap* target,
69 + FT_Render_Mode mode )
72 + int width, height, pitch;
74 + if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
77 + // compute the size of the final bitmap
78 + ftbit = &slot->bitmap;
80 + width = ftbit->width;
81 + height = ftbit->rows;
82 + pitch = (width+3) & ~3;
84 + switch ( ftbit->pixel_mode )
86 + case FT_PIXEL_MODE_MONO:
87 + if ( mode == FT_RENDER_MODE_MONO )
89 + pitch = (((width+31) & ~31) >> 3);
94 + case FT_PIXEL_MODE_GRAY:
95 + if ( mode == FT_RENDER_MODE_LCD ||
96 + mode == FT_RENDER_MODE_LCD_V )
98 + /* each pixel is replicated into a 32-bit ARGB value */
103 + case FT_PIXEL_MODE_LCD:
104 + if ( mode != FT_RENDER_MODE_LCD )
107 + /* horz pixel triplets are packed into 32-bit ARGB values */
112 + case FT_PIXEL_MODE_LCD_V:
113 + if ( mode != FT_RENDER_MODE_LCD_V )
116 + /* vert pixel triplets are packed into 32-bit ARGB values */
121 + default: /* unsupported source format */
125 + target->width = width;
126 + target->rows = height;
127 + target->pitch = pitch;
128 + target->buffer = NULL;
130 + return pitch * height;
133 +/* this functions converts the glyph bitmap found in a FT_GlyphSlot
134 + * into a different format (see _compute_xrender_bitmap_size)
136 + * you should call this function after _compute_xrender_bitmap_size
138 + * target :: target bitmap descriptor. Note that its 'buffer' pointer
139 + * must point to memory allocated by the caller
141 + * slot :: the glyph slot containing the source bitmap
143 + * mode :: the requested final rendering mode
145 + * bgr :: boolean, set if BGR or VBGR pixel ordering is needed
148 +_fill_xrender_bitmap( FT_Bitmap* target,
150 + FT_Render_Mode mode,
153 + FT_Bitmap* ftbit = &slot->bitmap;
156 + unsigned char* srcLine = ftbit->buffer;
157 + unsigned char* dstLine = target->buffer;
158 + int src_pitch = ftbit->pitch;
159 + int width = target->width;
160 + int height = target->rows;
161 + int pitch = target->pitch;
165 + subpixel = ( mode == FT_RENDER_MODE_LCD ||
166 + mode == FT_RENDER_MODE_LCD_V );
168 + if ( src_pitch < 0 )
169 + srcLine -= src_pitch*(ftbit->rows-1);
171 + switch ( ftbit->pixel_mode )
173 + case FT_PIXEL_MODE_MONO:
174 + if ( subpixel ) /* convert mono to ARGB32 values */
176 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
180 + for ( x = 0; x < width; x++ )
182 + if ( srcLine[(x >> 3)] & (0x80 >> (x & 7)) )
183 + ((unsigned int*)dstLine)[x] = 0xffffffffU;
187 + else if ( mode == FT_RENDER_MODE_NORMAL ) /* convert mono to 8-bit gray */
189 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
193 + for ( x = 0; x < width; x++ )
195 + if ( srcLine[(x >> 3)] & (0x80 >> (x & 7)) )
200 + else /* copy mono to mono */
202 + int bytes = (width+7) >> 3;
204 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
205 + memcpy( dstLine, srcLine, bytes );
209 + case FT_PIXEL_MODE_GRAY:
210 + if ( subpixel ) /* convert gray to ARGB32 values */
212 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
215 + unsigned int* dst = (unsigned int*)dstLine;
217 + for ( x = 0; x < width; x++ )
219 + unsigned int pix = srcLine[x];
222 + pix |= (pix << 16);
228 + else /* copy gray into gray */
230 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
231 + memcpy( dstLine, srcLine, width );
235 + case FT_PIXEL_MODE_LCD:
238 + /* convert horizontal RGB into ARGB32 */
239 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
242 + unsigned char* src = srcLine;
243 + unsigned int* dst = (unsigned int*)dstLine;
245 + for ( x = 0; x < width; x++, src += 3 )
249 + pix = ((unsigned int)src[0] << 16) |
250 + ((unsigned int)src[1] << 8) |
251 + ((unsigned int)src[2] ) |
252 + ((unsigned int)src[1] << 24) ;
260 + /* convert horizontal BGR into ARGB32 */
261 + for ( h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch )
264 + unsigned char* src = srcLine;
265 + unsigned int* dst = (unsigned int*)dstLine;
267 + for ( x = 0; x < width; x++, src += 3 )
271 + pix = ((unsigned int)src[2] << 16) |
272 + ((unsigned int)src[1] << 8) |
273 + ((unsigned int)src[0] ) |
274 + ((unsigned int)src[1] << 24) ;
282 + default: /* FT_PIXEL_MODE_LCD_V */
283 + /* convert vertical RGB into ARGB32 */
286 + for ( h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch )
289 + unsigned char* src = srcLine;
290 + unsigned int* dst = (unsigned int*)dstLine;
292 + for ( x = 0; x < width; x++, src += 1 )
296 + pix = ((unsigned int)src[0] << 16) |
297 + ((unsigned int)src[src_pitch] << 8) |
298 + ((unsigned int)src[src_pitch*2] ) |
299 + ((unsigned int)src[src_pitch] << 24) ;
307 + for ( h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch )
310 + unsigned char* src = srcLine;
311 + unsigned int* dst = (unsigned int*)dstLine;
313 + for ( x = 0; x < width; x++, src += 1 )
317 + pix = ((unsigned int)src[src_pitch*2] << 16) |
318 + ((unsigned int)src[src_pitch] << 8) |
319 + ((unsigned int)src[0] ) |
320 + ((unsigned int)src[src_pitch] << 24) ;
332 XftFontLoadGlyphs (Display *dpy,
335 unsigned char *bufBitmap = bufLocal;
336 int bufSize = sizeof (bufLocal);
338 - unsigned char bufLocalRgba[4096];
339 - unsigned char *bufBitmapRgba = bufLocalRgba;
340 - int bufSizeRgba = sizeof (bufLocalRgba);
341 - int sizergba, pitchrgba, widthrgba;
344 int left, right, top, bottom;
352 - Bool subpixel = False;
354 + FT_Render_Mode mode = FT_RENDER_MODE_MONO;
358 @@ -110,24 +384,19 @@
362 - matrix.xx = matrix.yy = 0x10000L;
363 - matrix.xy = matrix.yx = 0;
365 if (font->info.antialias)
367 switch (font->info.rgba) {
373 + mode = FT_RENDER_MODE_LCD;
380 + mode = FT_RENDER_MODE_LCD_V;
383 + mode = FT_RENDER_MODE_NORMAL;
388 if (xftg->glyph_memory)
391 + FT_Library_SetLcdFilter( _XftFTlibrary, FT_LCD_FILTER_DEFAULT );
393 error = FT_Load_Glyph (face, glyphindex, font->info.load_flags);
400 * Compute glyph metrics from FreeType information
402 - if(font->info.transform && glyphslot->format != ft_glyph_format_bitmap)
403 + if(font->info.transform && glyphslot->format != FT_GLYPH_FORMAT_BITMAP)
406 * calculate the true width by transforming all four corners.
407 @@ -260,17 +532,14 @@
411 - if (font->info.antialias)
412 - pitch = (width * hmul + 3) & ~3;
414 - pitch = ((width + 31) & ~31) >> 3;
416 - size = pitch * height * vmul;
417 + if ( glyphslot->format != FT_GLYPH_FORMAT_BITMAP )
419 + error = FT_Render_Glyph( face->glyph, mode );
424 - xftg->metrics.width = width;
425 - xftg->metrics.height = height;
426 - xftg->metrics.x = -TRUNC(left);
427 - xftg->metrics.y = TRUNC(top);
428 + FT_Library_SetLcdFilter( _XftFTlibrary, FT_LCD_FILTER_NONE );
430 if (font->info.spacing >= FC_MONO)
432 @@ -310,103 +579,13 @@
433 xftg->metrics.yOff = -TRUNC(ROUND(glyphslot->advance.y));
437 - * If the glyph is relatively large (> 1% of server memory),
438 - * don't send it until necessary
440 - if (!need_bitmaps && size > info->max_glyph_memory / 100)
444 - * Make sure there's enough buffer space for the glyph
446 - if (size > bufSize)
448 - if (bufBitmap != bufLocal)
450 - bufBitmap = (unsigned char *) malloc (size);
455 - memset (bufBitmap, 0, size);
458 - * Rasterize into the local buffer
460 - switch (glyphslot->format) {
461 - case ft_glyph_format_outline:
462 - ftbit.width = width * hmul;
463 - ftbit.rows = height * vmul;
464 - ftbit.pitch = pitch;
465 - if (font->info.antialias)
466 - ftbit.pixel_mode = ft_pixel_mode_grays;
468 - ftbit.pixel_mode = ft_pixel_mode_mono;
470 - ftbit.buffer = bufBitmap;
473 - FT_Outline_Transform (&glyphslot->outline, &matrix);
475 - FT_Outline_Translate ( &glyphslot->outline, -left*hmul, -bottom*vmul );
476 + // compute the size of the final bitmap
477 + ftbit = &glyphslot->bitmap;
479 - FT_Outline_Get_Bitmap( _XftFTlibrary, &glyphslot->outline, &ftbit );
481 - case ft_glyph_format_bitmap:
482 - if (font->info.antialias)
484 - unsigned char *srcLine, *dstLine;
489 - srcLine = glyphslot->bitmap.buffer;
490 - dstLine = bufBitmap;
491 - height = glyphslot->bitmap.rows;
494 - for (x = 0; x < glyphslot->bitmap.width; x++)
496 - /* always MSB bitmaps */
497 - unsigned char a = ((srcLine[x >> 3] & (0x80 >> (x & 7))) ?
501 - for (v = 0; v < vmul; v++)
502 - for (h = 0; h < hmul; h++)
503 - dstLine[v * pitch + x*hmul + h] = a;
508 - dstLine += pitch * vmul;
509 - srcLine += glyphslot->bitmap.pitch;
514 - unsigned char *srcLine, *dstLine;
517 - srcLine = glyphslot->bitmap.buffer;
518 - dstLine = bufBitmap;
519 - h = glyphslot->bitmap.rows;
520 - bytes = (glyphslot->bitmap.width + 7) >> 3;
523 - memcpy (dstLine, srcLine, bytes);
525 - srcLine += glyphslot->bitmap.pitch;
530 - if (XftDebug() & XFT_DBG_GLYPH)
531 - printf ("glyph %d is not in a usable format\n",
535 + width = ftbit->width;
536 + height = ftbit->rows;
537 + pitch = (width+3) & ~3;
539 if (XftDebug() & XFT_DBG_GLYPH)
541 @@ -423,29 +602,72 @@
546 - for (y = 0; y < height * vmul; y++)
547 + line = ftbit->buffer;
549 + if (ftbit->pitch < 0)
550 + line -= ftbit->pitch*(height-1);
552 + for (y = 0; y < height; y++)
554 if (font->info.antialias)
556 - static char den[] = { " .:;=+*#" };
557 - for (x = 0; x < pitch; x++)
558 + static const char den[] = { " .:;=+*#" };
559 + for (x = 0; x < width; x++)
560 printf ("%c", den[line[x] >> 5]);
564 - for (x = 0; x < pitch * 8; x++)
565 + for (x = 0; x < width * 8; x++)
567 printf ("%c", line[x>>3] & (1 << (x & 7)) ? '#' : ' ');
572 + line += ftbit->pitch;
578 + size = _compute_xrender_bitmap_size( &local, glyphslot, mode );
582 + xftg->metrics.width = local.width;
583 + xftg->metrics.height = local.rows;
584 + xftg->metrics.x = - glyphslot->bitmap_left;
585 + xftg->metrics.y = glyphslot->bitmap_top;
588 + * If the glyph is relatively large (> 1% of server memory),
589 + * don't send it until necessary
591 + if (!need_bitmaps && size > info->max_glyph_memory / 100)
595 + * Make sure there's enough buffer space for the glyph
597 + if (size > bufSize)
599 + if (bufBitmap != bufLocal)
601 + bufBitmap = (unsigned char *) malloc (size);
606 + memset (bufBitmap, 0, size);
608 + local.buffer = bufBitmap;
610 + _fill_xrender_bitmap( &local, glyphslot, mode,
611 + (font->info.rgba == FC_RGBA_BGR ||
612 + font->info.rgba == FC_RGBA_VBGR ) );
614 + * Copy or convert into local buffer
618 * Use the glyph index as the wire encoding; it
619 * might be more efficient for some locales to map
620 @@ -455,121 +677,23 @@
622 glyph = (Glyph) glyphindex;
627 - unsigned char *in_line, *out_line, *in;
629 - unsigned int red, green, blue;
635 - * Filter the glyph to soften the color fringes
638 - pitchrgba = (widthrgba * 4 + 3) & ~3;
639 - sizergba = pitchrgba * height;
642 - switch (font->info.rgba) {
659 - if (sizergba > bufSizeRgba)
661 - if (bufBitmapRgba != bufLocalRgba)
662 - free (bufBitmapRgba);
663 - bufBitmapRgba = (unsigned char *) malloc (sizergba);
664 - if (!bufBitmapRgba)
666 - bufSizeRgba = sizergba;
668 - memset (bufBitmapRgba, 0, sizergba);
669 - in_line = bufBitmap;
670 - out_line = bufBitmapRgba;
671 - for (y = 0; y < height; y++)
674 - out = (unsigned int *) out_line;
675 - in_line += pitch * vmul;
676 - out_line += pitchrgba;
677 - for (x = 0; x < width * hmul; x += hmul)
679 - red = green = blue = 0;
681 - for (s = 0; s < 3; s++)
683 - red += filters[rf][s]*in[x+o];
684 - green += filters[gf][s]*in[x+o];
685 - blue += filters[bf][s]*in[x+o];
689 - green = green / 65536;
690 - blue = blue / 65536;
691 - *out++ = (green << 24) | (red << 16) | (green << 8) | blue;
695 - xftg->glyph_memory = sizergba + sizeof (XftGlyph);
696 + xftg->glyph_memory = size + sizeof (XftGlyph);
701 font->glyphset = XRenderCreateGlyphSet (dpy, font->format);
702 - if (ImageByteOrder (dpy) != XftNativeByteOrder ())
703 - XftSwapCARD32 ((CARD32 *) bufBitmapRgba, sizergba >> 2);
704 - XRenderAddGlyphs (dpy, font->glyphset, &glyph,
706 - (char *) bufBitmapRgba, sizergba);
712 - xftg->bitmap = malloc (sizergba);
714 - memcpy (xftg->bitmap, bufBitmapRgba, sizergba);
717 - xftg->bitmap = NULL;
722 - xftg->glyph_memory = size + sizeof (XftGlyph);
726 - * swap bit order around; FreeType is always MSBFirst
728 - if (!font->info.antialias)
729 + if ( mode == FT_RENDER_MODE_MONO )
731 + /* swap bits in each byte */
732 if (BitmapBitOrder (dpy) != MSBFirst)
734 - unsigned char *line;
737 + unsigned char *line = (unsigned char*)bufBitmap;
740 - line = (unsigned char *) bufBitmap;
746 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
747 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
748 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
753 - if (!font->glyphset)
754 - font->glyphset = XRenderCreateGlyphSet (dpy, font->format);
755 + else if ( mode != FT_RENDER_MODE_NORMAL )
757 + /* invert ARGB <=> BGRA */
758 + if (ImageByteOrder (dpy) != XftNativeByteOrder ())
759 + XftSwapCARD32 ((CARD32 *) bufBitmap, size >> 2);
761 XRenderAddGlyphs (dpy, font->glyphset, &glyph,
763 (char *) bufBitmap, size);
770 font->glyph_memory += xftg->glyph_memory;
771 info->glyph_memory += xftg->glyph_memory;
772 if (XftDebug() & XFT_DBG_CACHE)
775 if (bufBitmap != bufLocal)
777 - if (bufBitmapRgba != bufLocalRgba)
778 - free (bufBitmapRgba);
779 XftUnlockFace (&font->public);