shithub: freetype+ttf2subf

Download patch

ref: ed913c2151e40eda626d8d2809ed3229aade2941
parent: 7774ac67ec562add4212930c82e402a7d7cdff8b
author: Joel Klinghed <[email protected]>
date: Fri Dec 31 11:59:33 EST 2010

Add bzip2 compression support to handle *.pcf.bz2 files.

* builds/unix/configure.raw: Test for libbz2 library.

* devel/ftoption.h, include/freetype/config/ftoption.h
(FT_CONFIG_OPTION_USE_BZIP2): Define.
* include/freetype/config/ftheader.h (FT_BZIP2_H): Define.

* include/freetype/ftbzip2.h: New file.

* src/bzip2/*: New files.

* src/pcf/pcf.h: s/gzip_/comp_/.
* src/pcf/pcfdrvr.c: Include FT_BZIP2_H.
s/gzip_/comp_/.
(PCF_Face_Init): Handle bzip2 compressed files.

* docs/formats.txt, modules.cfg: Updated.

git/fs: mount .git/fs: mount/attach disallowed
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,29 @@
+2010-12-31  Joel Klinghed  <[email protected]>
+
+	Add bzip2 compression support to handle *.pcf.bz2 files.
+
+	* builds/unix/configure.raw: Test for libbz2 library.
+
+	* devel/ftoption.h, include/freetype/config/ftoption.h
+	(FT_CONFIG_OPTION_USE_BZIP2): Define.
+	* include/freetype/config/ftheader.h (FT_BZIP2_H): Define.
+
+	* include/freetype/ftbzip2.h: New file.
+
+	* src/bzip2/*: New files.
+
+	* src/pcf/pcf.h: s/gzip_/comp_/.
+	* src/pcf/pcfdrvr.c: Include FT_BZIP2_H.
+	s/gzip_/comp_/.
+	(PCF_Face_Init): Handle bzip2 compressed files.
+
+	* docs/formats.txt, modules.cfg: Updated.
+
 2010-12-25  Harsha  <[email protected]>
 
 	Apply Savannah patch #7422.
 
-	If we encouter a space in a string then the sbit buffer is NULL,
+	If we encounter a space in a string then the sbit buffer is NULL,
 	height and width are 0s.  So the check in ftc_snode_compare will
 	always pass for spaces (comparision with 255).  Here the comments
 	above the condition are proper but the implementation is not.  When
--- a/builds/unix/configure.raw
+++ b/builds/unix/configure.raw
@@ -229,7 +229,21 @@
   SYSTEM_ZLIB=yes
 fi
 
+# check for system libbz2
 
+# don't quote AS_HELP_STRING!
+AC_ARG_WITH([bzip2],
+  AS_HELP_STRING([--without-bzip2],
+                 [do not support bzip2 compressed fonts]))
+if test x$with_bzip2 != xno && test -z "$LIBBZ2"; then
+  AC_CHECK_LIB([bz2], [BZ2_bzDecompress], [AC_CHECK_HEADER([bzlib.h], [LIBBZ2='-lbz2'])])
+fi
+if test x$with_bzip2 != xno && test -n "$LIBBZ2"; then
+  CFLAGS="$CFLAGS -DFT_CONFIG_OPTION_USE_BZIP2"
+  LDFLAGS="$LDFLAGS $LIBBZ2"
+  SYSTEM_BZ2LIB=yes
+fi
+
 # Some options handling SDKs/archs in CFLAGS should be copied
 # to LDFLAGS. Apple TechNote 2137 recommends to include these
 # options in CFLAGS but not in LDFLAGS.
@@ -644,10 +658,12 @@
 
 AC_SUBST([ftmac_c])
 AC_SUBST([LIBZ])
+AC_SUBST([LIBBZ2])
 AC_SUBST([CFLAGS])
 AC_SUBST([LDFLAGS])
 AC_SUBST([FT2_EXTRA_LIBS])
 AC_SUBST([SYSTEM_ZLIB])
+AC_SUBST([SYSTEM_BZ2LIB])
 
 
 LT_INIT(win32-dll)
--- a/devel/ftoption.h
+++ b/devel/ftoption.h
@@ -190,6 +190,22 @@
 
   /*************************************************************************/
   /*                                                                       */
+  /* Bzip2-compressed file support.                                        */
+  /*                                                                       */
+  /*   FreeType now handles font files that have been compressed with the  */
+  /*   `bzip2' program.  This is mostly used to parse many of the PCF      */
+  /*   files that come with XFree86.  The implementation uses `libbz2' to  */
+  /*   partially uncompress the file on the fly (see src/bzip2/ftbzip2.c). */
+  /*   Contrary to gzip, bzip2 currently is not included and need to use   */
+  /*   the system available bzip2 implementation.                          */
+  /*                                                                       */
+  /*   Define this macro if you want to enable this `feature'.             */
+  /*                                                                       */
+/* #define FT_CONFIG_OPTION_USE_BZIP2 */
+
+
+  /*************************************************************************/
+  /*                                                                       */
   /* DLL export compilation                                                */
   /*                                                                       */
   /*   When compiling FreeType as a DLL, some systems/compilers need a     */
--- a/docs/formats.txt
+++ b/docs/formats.txt
@@ -11,8 +11,9 @@
   wrapper format:
     The format used  to represent the font data.  In  the table below it
     is used only if the font format differs.  Possible values are `SFNT'
-    (binary), `PS' (a text header, followed by binary or text data), and
-    `LZW' (compressed with either `gzip' or `compress').
+    (binary), `PS' (a text header, followed by binary or text data),
+    `LZW' (compressed with either `gzip' or `compress'), and
+    `BZ2' (compressed with `bzip2`).
 
   font format:
     How the font  is to be accessed, possibly  after converting the file
@@ -113,6 +114,7 @@
 
 ---  ---     PCF    ---     ---        pcf      X11 [4]
 ---  LZW     PCF    ---     ---        pcf      X11 [4]
+---  BZ2     PCF    ---     ---        pcf      X11 [4]
 
 
 ---  ---     PFR    PFR0    ---        pfr      [2]
--- a/include/freetype/config/ftheader.h
+++ b/include/freetype/config/ftheader.h
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    Build macros of the FreeType 2 library.                              */
 /*                                                                         */
-/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by       */
+/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2010 by */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -422,6 +422,19 @@
    *
    */
 #define FT_LZW_H  <freetype/ftlzw.h>
+
+
+  /*************************************************************************
+   *
+   * @macro:
+   *   FT_BZIP2_H
+   *
+   * @description:
+   *   A macro used in #include statements to name the file containing the
+   *   definitions of an API which supports bzip2-compressed files.
+   *
+   */
+#define FT_BZIP2_H  <freetype/ftbzip2.h>
 
 
   /*************************************************************************
--- a/include/freetype/config/ftoption.h
+++ b/include/freetype/config/ftoption.h
@@ -190,6 +190,22 @@
 
   /*************************************************************************/
   /*                                                                       */
+  /* Bzip2-compressed file support.                                        */
+  /*                                                                       */
+  /*   FreeType now handles font files that have been compressed with the  */
+  /*   `bzip2' program.  This is mostly used to parse many of the PCF      */
+  /*   files that come with XFree86.  The implementation uses `libbz2' to  */
+  /*   partially uncompress the file on the fly (see src/bzip2/ftbzip2.c). */
+  /*   Contrary to gzip, bzip2 currently is not included and need to use   */
+  /*   the system available bzip2 implementation.                          */
+  /*                                                                       */
+  /*   Define this macro if you want to enable this `feature'.             */
+  /*                                                                       */
+/* #define FT_CONFIG_OPTION_USE_BZIP2 */
+
+
+  /*************************************************************************/
+  /*                                                                       */
   /* DLL export compilation                                                */
   /*                                                                       */
   /*   When compiling FreeType as a DLL, some systems/compilers need a     */
--- /dev/null
+++ b/include/freetype/ftbzip2.h
@@ -1,0 +1,102 @@
+/***************************************************************************/
+/*                                                                         */
+/*  ftbzip2.h                                                              */
+/*                                                                         */
+/*    Bzip2-compressed stream support.                                     */
+/*                                                                         */
+/*  Copyright 2010 by                                                      */
+/*  Joel Klinghed.                                                         */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used,       */
+/*  modified, and distributed under the terms of the FreeType project      */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#ifndef __FTBZIP2_H__
+#define __FTBZIP2_H__
+
+#include <ft2build.h>
+#include FT_FREETYPE_H
+
+#ifdef FREETYPE_H
+#error "freetype.h of FreeType 1 has been loaded!"
+#error "Please fix the directory search order for header files"
+#error "so that freetype.h of FreeType 2 is found first."
+#endif
+
+
+FT_BEGIN_HEADER
+
+  /*************************************************************************/
+  /*                                                                       */
+  /* <Section>                                                             */
+  /*    bzip2                                                              */
+  /*                                                                       */
+  /* <Title>                                                               */
+  /*    BZIP2 Streams                                                      */
+  /*                                                                       */
+  /* <Abstract>                                                            */
+  /*    Using bzip2-compressed font files.                                 */
+  /*                                                                       */
+  /* <Description>                                                         */
+  /*    This section contains the declaration of Bzip2-specific functions. */
+  /*                                                                       */
+  /*************************************************************************/
+
+
+ /************************************************************************
+  *
+  * @function:
+  *   FT_Stream_OpenBzip2
+  *
+  * @description:
+  *   Open a new stream to parse bzip2-compressed font files.  This is
+  *   mainly used to support the compressed `*.pcf.gz' fonts that come
+  *   with XFree86.
+  *
+  * @input:
+  *   stream ::
+  *     The target embedding stream.
+  *
+  *   source ::
+  *     The source stream.
+  *
+  * @return:
+  *   FreeType error code.  0~means success.
+  *
+  * @note:
+  *   The source stream must be opened _before_ calling this function.
+  *
+  *   Calling the internal function `FT_Stream_Close' on the new stream will
+  *   *not* call `FT_Stream_Close' on the source stream.  None of the stream
+  *   objects will be released to the heap.
+  *
+  *   The stream implementation is very basic and resets the decompression
+  *   process each time seeking backwards is needed within the stream.
+  *
+  *   In certain builds of the library, bzip2 compression recognition is
+  *   automatically handled when calling @FT_New_Face or @FT_Open_Face.
+  *   This means that if no font driver is capable of handling the raw
+  *   compressed file, the library will try to open a bzip2 compressed stream
+  *   from it and re-open the face with it.
+  *
+  *   This function may return `FT_Err_Unimplemented_Feature' if your build
+  *   of FreeType was not compiled with bzip2 support.
+  */
+  FT_EXPORT( FT_Error )
+  FT_Stream_OpenBzip2( FT_Stream  stream,
+                       FT_Stream  source );
+
+ /* */
+
+
+FT_END_HEADER
+
+#endif /* __FTBZIP2_H__ */
+
+
+/* END */
--- a/modules.cfg
+++ b/modules.cfg
@@ -1,6 +1,6 @@
 # modules.cfg
 #
-# Copyright 2005, 2006, 2007, 2009 by
+# Copyright 2005, 2006, 2007, 2009, 2010 by
 # David Turner, Robert Wilhelm, and Werner Lemberg.
 #
 # This file is part of the FreeType project, and may only be used, modified,
@@ -121,6 +121,11 @@
 #
 # See include/freetype/ftlzw.h for the API.
 AUX_MODULES += lzw
+
+# Support for streams compressed with bzip2 (files with suffix .bz2).
+#
+# See include/freetype/ftbzip2.h for the API.
+AUX_MODULES += bzip2
 
 # OpenType table validation.  Needs ftotval.c below.
 #
--- /dev/null
+++ b/src/bzip2/Jamfile
@@ -1,0 +1,19 @@
+# FreeType 2 src/bzip2 Jamfile
+#
+# Copyright 2010 by
+# Joel Klinghed
+#
+# Based on src/lzw/Jamfile, Copyright 2004, 2006 by
+# David Turner, Robert Wilhelm, and Werner Lemberg.
+#
+# This file is part of the FreeType project, and may only be used, modified,
+# and distributed under the terms of the FreeType project license,
+# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
+# indicate that you have read the license and understand and accept it
+# fully.
+
+SubDir  FT2_TOP $(FT2_SRC_DIR) bzip2 ;
+
+Library  $(FT2_LIB) : ftbzip2.c ;
+
+# end of src/bzip2 Jamfile
--- /dev/null
+++ b/src/bzip2/ftbzip2.c
@@ -1,0 +1,510 @@
+/***************************************************************************/
+/*                                                                         */
+/*  ftbzip2.c                                                              */
+/*                                                                         */
+/*    FreeType support for .bz2 compressed files.                          */
+/*                                                                         */
+/*  This optional component relies on libbz2.  It should mainly be used to */
+/*  parse compressed PCF fonts, as found with many X11 server              */
+/*  distributions.                                                         */
+/*                                                                         */
+/*  Copyright 2010 by                                                      */
+/*  Joel Klinghed.                                                         */
+/*                                                                         */
+/*  Based on src/gzip/ftgzip.c, Copyright 2002 - 2010 by                   */
+/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used,       */
+/*  modified, and distributed under the terms of the FreeType project      */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_MEMORY_H
+#include FT_INTERNAL_STREAM_H
+#include FT_INTERNAL_DEBUG_H
+#include FT_BZIP2_H
+#include FT_CONFIG_STANDARD_LIBRARY_H
+
+
+#include FT_MODULE_ERRORS_H
+
+#undef __FTERRORS_H__
+
+#define FT_ERR_PREFIX  Bzip2_Err_
+#define FT_ERR_BASE    FT_Mod_Err_Bzip2
+
+#include FT_ERRORS_H
+
+
+#ifdef FT_CONFIG_OPTION_USE_BZIP2
+
+#ifdef FT_CONFIG_OPTION_PIC
+#error "bzip2 code does not support PIC yet"
+#endif
+
+#define BZ_NO_STDIO /* Do not need FILE */
+#include <bzlib.h>
+
+
+/***************************************************************************/
+/***************************************************************************/
+/*****                                                                 *****/
+/*****           B Z I P 2   M E M O R Y   M A N A G E M E N T         *****/
+/*****                                                                 *****/
+/***************************************************************************/
+/***************************************************************************/
+
+  /* it is better to use FreeType memory routines instead of raw
+     'malloc/free' */
+
+  typedef void *(* alloc_func)(void*, int, int);
+  typedef void (* free_func)(void*, void*);
+
+  static void*
+  ft_bzip2_alloc( FT_Memory  memory,
+                  int        items,
+                  int        size )
+  {
+    FT_ULong    sz = (FT_ULong)size * items;
+    FT_Error    error;
+    FT_Pointer  p  = NULL;
+
+
+    (void)FT_ALLOC( p, sz );
+    return p;
+  }
+
+
+  static void
+  ft_bzip2_free( FT_Memory  memory,
+                 void*      address )
+  {
+    FT_MEM_FREE( address );
+  }
+
+
+/***************************************************************************/
+/***************************************************************************/
+/*****                                                                 *****/
+/*****              B Z I P 2   F I L E   D E S C R I P T O R          *****/
+/*****                                                                 *****/
+/***************************************************************************/
+/***************************************************************************/
+
+#define FT_BZIP2_BUFFER_SIZE  4096
+
+  typedef struct  FT_BZip2FileRec_
+  {
+    FT_Stream  source;         /* parent/source stream        */
+    FT_Stream  stream;         /* embedding stream            */
+    FT_Memory  memory;         /* memory allocator            */
+    bz_stream  bzstream;       /* bzlib input stream          */
+
+    FT_Byte    input[FT_BZIP2_BUFFER_SIZE];  /* input read buffer  */
+
+    FT_Byte    buffer[FT_BZIP2_BUFFER_SIZE]; /* output buffer      */
+    FT_ULong   pos;                          /* position in output */
+    FT_Byte*   cursor;
+    FT_Byte*   limit;
+
+  } FT_BZip2FileRec, *FT_BZip2File;
+
+
+  /* check and skip .bz2 header - we don't support `transparent' compression */
+  static FT_Error
+  ft_bzip2_check_header( FT_Stream  stream )
+  {
+    FT_Error  error = Bzip2_Err_Ok;
+    FT_Byte   head[4];
+
+
+    if ( FT_STREAM_SEEK( 0 )       ||
+         FT_STREAM_READ( head, 4 ) )
+      goto Exit;
+
+    /* head[0] && head[1] are the magic numbers;    */
+    /* head[2] is the version, and head[3] the blocksize */
+    if ( head[0] != 0x42  ||
+         head[1] != 0x5a  ||
+         head[2] != 0x68  )  /* only support bzip2 (huffman) */
+    {
+      error = Bzip2_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+  Exit:
+    return error;
+  }
+
+
+  static FT_Error
+  ft_bzip2_file_init( FT_BZip2File  zip,
+                      FT_Stream     stream,
+                      FT_Stream     source )
+  {
+    bz_stream*  bzstream = &zip->bzstream;
+    FT_Error    error    = Bzip2_Err_Ok;
+
+
+    zip->stream = stream;
+    zip->source = source;
+    zip->memory = stream->memory;
+
+    zip->limit  = zip->buffer + FT_BZIP2_BUFFER_SIZE;
+    zip->cursor = zip->limit;
+    zip->pos    = 0;
+
+    /* check .bz2 header */
+    {
+      stream = source;
+
+      error = ft_bzip2_check_header( stream );
+      if ( error )
+        goto Exit;
+
+      if ( FT_STREAM_SEEK( 0 ) )
+        goto Exit;
+    }
+
+    /* initialize bzlib */
+    bzstream->bzalloc = (alloc_func)ft_bzip2_alloc;
+    bzstream->bzfree  = (free_func) ft_bzip2_free;
+    bzstream->opaque  = zip->memory;
+
+    bzstream->avail_in = 0;
+    bzstream->next_in  = (char*)zip->buffer;
+
+    if ( BZ2_bzDecompressInit( bzstream, 0, 0 ) != BZ_OK ||
+         bzstream->next_in == NULL                       )
+      error = Bzip2_Err_Invalid_File_Format;
+
+  Exit:
+    return error;
+  }
+
+
+  static void
+  ft_bzip2_file_done( FT_BZip2File  zip )
+  {
+    bz_stream*  bzstream = &zip->bzstream;
+
+
+    BZ2_bzDecompressEnd( bzstream );
+
+    /* clear the rest */
+    bzstream->bzalloc   = NULL;
+    bzstream->bzfree    = NULL;
+    bzstream->opaque    = NULL;
+    bzstream->next_in   = NULL;
+    bzstream->next_out  = NULL;
+    bzstream->avail_in  = 0;
+    bzstream->avail_out = 0;
+
+    zip->memory = NULL;
+    zip->source = NULL;
+    zip->stream = NULL;
+  }
+
+
+  static FT_Error
+  ft_bzip2_file_reset( FT_BZip2File  zip )
+  {
+    FT_Stream  stream = zip->source;
+    FT_Error   error;
+
+
+    if ( !FT_STREAM_SEEK( 0 ) )
+    {
+      bz_stream*  bzstream = &zip->bzstream;
+
+
+      BZ2_bzDecompressEnd( bzstream );
+
+      bzstream->avail_in  = 0;
+      bzstream->next_in   = (char*)zip->input;
+      bzstream->avail_out = 0;
+      bzstream->next_out  = (char*)zip->buffer;
+
+      zip->limit  = zip->buffer + FT_BZIP2_BUFFER_SIZE;
+      zip->cursor = zip->limit;
+      zip->pos    = 0;
+
+      BZ2_bzDecompressInit( bzstream, 0, 0 );
+    }
+
+    return error;
+  }
+
+
+  static FT_Error
+  ft_bzip2_file_fill_input( FT_BZip2File  zip )
+  {
+    bz_stream*  bzstream = &zip->bzstream;
+    FT_Stream   stream    = zip->source;
+    FT_ULong    size;
+
+
+    if ( stream->read )
+    {
+      size = stream->read( stream, stream->pos, zip->input,
+                           FT_BZIP2_BUFFER_SIZE );
+      if ( size == 0 )
+        return Bzip2_Err_Invalid_Stream_Operation;
+    }
+    else
+    {
+      size = stream->size - stream->pos;
+      if ( size > FT_BZIP2_BUFFER_SIZE )
+        size = FT_BZIP2_BUFFER_SIZE;
+
+      if ( size == 0 )
+        return Bzip2_Err_Invalid_Stream_Operation;
+
+      FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
+    }
+    stream->pos += size;
+
+    bzstream->next_in  = (char*)zip->input;
+    bzstream->avail_in = size;
+
+    return Bzip2_Err_Ok;
+  }
+
+
+  static FT_Error
+  ft_bzip2_file_fill_output( FT_BZip2File  zip )
+  {
+    bz_stream*  bzstream = &zip->bzstream;
+    FT_Error    error    = Bzip2_Err_Ok;
+
+
+    zip->cursor         = zip->buffer;
+    bzstream->next_out  = (char*)zip->cursor;
+    bzstream->avail_out = FT_BZIP2_BUFFER_SIZE;
+
+    while ( bzstream->avail_out > 0 )
+    {
+      int  err;
+
+
+      if ( bzstream->avail_in == 0 )
+      {
+        error = ft_bzip2_file_fill_input( zip );
+        if ( error )
+          break;
+      }
+
+      err = BZ2_bzDecompress( bzstream );
+
+      if ( err == BZ_STREAM_END )
+      {
+        zip->limit = (FT_Byte*)bzstream->next_out;
+        if ( zip->limit == zip->cursor )
+          error = Bzip2_Err_Invalid_Stream_Operation;
+        break;
+      }
+      else if ( err != BZ_OK )
+      {
+        error = Bzip2_Err_Invalid_Stream_Operation;
+        break;
+      }
+    }
+
+    return error;
+  }
+
+
+  /* fill output buffer; `count' must be <= FT_BZIP2_BUFFER_SIZE */
+  static FT_Error
+  ft_bzip2_file_skip_output( FT_BZip2File  zip,
+                             FT_ULong      count )
+  {
+    FT_Error  error = Bzip2_Err_Ok;
+    FT_ULong  delta;
+
+
+    for (;;)
+    {
+      delta = (FT_ULong)( zip->limit - zip->cursor );
+      if ( delta >= count )
+        delta = count;
+
+      zip->cursor += delta;
+      zip->pos    += delta;
+
+      count -= delta;
+      if ( count == 0 )
+        break;
+
+      error = ft_bzip2_file_fill_output( zip );
+      if ( error )
+        break;
+    }
+
+    return error;
+  }
+
+
+  static FT_ULong
+  ft_bzip2_file_io( FT_BZip2File  zip,
+                    FT_ULong      pos,
+                    FT_Byte*      buffer,
+                    FT_ULong      count )
+  {
+    FT_ULong  result = 0;
+    FT_Error  error;
+
+
+    /* Reset inflate stream if we're seeking backwards.        */
+    /* Yes, that is not too efficient, but it saves memory :-) */
+    if ( pos < zip->pos )
+    {
+      error = ft_bzip2_file_reset( zip );
+      if ( error )
+        goto Exit;
+    }
+
+    /* skip unwanted bytes */
+    if ( pos > zip->pos )
+    {
+      error = ft_bzip2_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
+      if ( error )
+        goto Exit;
+    }
+
+    if ( count == 0 )
+      goto Exit;
+
+    /* now read the data */
+    for (;;)
+    {
+      FT_ULong  delta;
+
+
+      delta = (FT_ULong)( zip->limit - zip->cursor );
+      if ( delta >= count )
+        delta = count;
+
+      FT_MEM_COPY( buffer, zip->cursor, delta );
+      buffer      += delta;
+      result      += delta;
+      zip->cursor += delta;
+      zip->pos    += delta;
+
+      count -= delta;
+      if ( count == 0 )
+        break;
+
+      error = ft_bzip2_file_fill_output( zip );
+      if ( error )
+        break;
+    }
+
+  Exit:
+    return result;
+  }
+
+
+/***************************************************************************/
+/***************************************************************************/
+/*****                                                                 *****/
+/*****               B Z   E M B E D D I N G   S T R E A M             *****/
+/*****                                                                 *****/
+/***************************************************************************/
+/***************************************************************************/
+
+  static void
+  ft_bzip2_stream_close( FT_Stream  stream )
+  {
+    FT_BZip2File  zip    = (FT_BZip2File)stream->descriptor.pointer;
+    FT_Memory     memory = stream->memory;
+
+
+    if ( zip )
+    {
+      /* finalize bzip file descriptor */
+      ft_bzip2_file_done( zip );
+
+      FT_FREE( zip );
+
+      stream->descriptor.pointer = NULL;
+    }
+  }
+
+
+  static FT_ULong
+  ft_bzip2_stream_io( FT_Stream  stream,
+                      FT_ULong   pos,
+                      FT_Byte*   buffer,
+                      FT_ULong   count )
+  {
+    FT_BZip2File  zip = (FT_BZip2File)stream->descriptor.pointer;
+
+
+    return ft_bzip2_file_io( zip, pos, buffer, count );
+  }
+
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_Stream_OpenBzip2( FT_Stream  stream,
+                       FT_Stream  source )
+  {
+    FT_Error      error;
+    FT_Memory     memory = source->memory;
+    FT_BZip2File  zip;
+
+
+    /*
+     *  check the header right now; this prevents allocating unnecessary
+     *  objects when we don't need them
+     */
+    error = ft_bzip2_check_header( source );
+    if ( error )
+      goto Exit;
+
+    FT_ZERO( stream );
+    stream->memory = memory;
+
+    if ( !FT_QNEW( zip ) )
+    {
+      error = ft_bzip2_file_init( zip, stream, source );
+      if ( error )
+      {
+        FT_FREE( zip );
+        goto Exit;
+      }
+
+      stream->descriptor.pointer = zip;
+    }
+
+    stream->size  = 0x7FFFFFFFL;  /* don't know the real size! */
+    stream->pos   = 0;
+    stream->base  = 0;
+    stream->read  = ft_bzip2_stream_io;
+    stream->close = ft_bzip2_stream_close;
+
+  Exit:
+    return error;
+  }
+
+#else  /* !FT_CONFIG_OPTION_USE_BZIP2 */
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_Stream_OpenBzip2( FT_Stream  stream,
+                       FT_Stream  source )
+  {
+    FT_UNUSED( stream );
+    FT_UNUSED( source );
+
+    return Bzip2_Err_Unimplemented_Feature;
+  }
+
+#endif /* !FT_CONFIG_OPTION_USE_BZIP2 */
+
+
+/* END */
--- /dev/null
+++ b/src/bzip2/rules.mk
@@ -1,0 +1,63 @@
+#
+# FreeType 2 BZIP2 support configuration rules
+#
+
+# Copyright 2010 by
+# Joel Klinghed.
+#
+# Based on src/lzw/rules.mk, Copyright 2004-2006 by
+# Albert Chin-A-Young.
+#
+# This file is part of the FreeType project, and may only be used, modified,
+# and distributed under the terms of the FreeType project license,
+# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
+# indicate that you have read the license and understand and accept it
+# fully.
+
+
+# BZIP2 driver directory
+#
+BZIP2_DIR := $(SRC_DIR)/bzip2
+
+
+# compilation flags for the driver
+#
+BZIP2_COMPILE := $(FT_COMPILE)
+
+
+# BZIP2 support sources (i.e., C files)
+#
+BZIP2_DRV_SRC := $(BZIP2_DIR)/ftbzip2.c
+
+# BZIP2 driver object(s)
+#
+#   BZIP2_DRV_OBJ_M is used during `multi' builds
+#   BZIP2_DRV_OBJ_S is used during `single' builds
+#
+BZIP2_DRV_OBJ_M := $(OBJ_DIR)/ftbzip2.$O
+BZIP2_DRV_OBJ_S := $(OBJ_DIR)/ftbzip2.$O
+
+# BZIP2 support source file for single build
+#
+BZIP2_DRV_SRC_S := $(BZIP2_DIR)/ftbzip2.c
+
+
+# BZIP2 support - single object
+#
+$(BZIP2_DRV_OBJ_S): $(BZIP2_DRV_SRC_S) $(BZIP2_DRV_SRC) $(FREETYPE_H) $(BZIP2_DRV_H)
+	$(BZIP2_COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $(BZIP2_DRV_SRC_S))
+
+
+# BZIP2 support - multiple objects
+#
+$(OBJ_DIR)/%.$O: $(BZIP2_DIR)/%.c $(FREETYPE_H) $(BZIP2_DRV_H)
+	$(BZIP2_COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+
+# update main driver object lists
+#
+DRV_OBJS_S += $(BZIP2_DRV_OBJ_S)
+DRV_OBJS_M += $(BZIP2_DRV_OBJ_M)
+
+
+# EOF
--- a/src/pcf/pcf.h
+++ b/src/pcf/pcf.h
@@ -2,7 +2,7 @@
 
   FreeType font driver for pcf fonts
 
-  Copyright (C) 2000, 2001, 2002, 2003, 2006 by
+  Copyright (C) 2000, 2001, 2002, 2003, 2006, 2010 by
   Francesco Zappa Nardelli
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -136,8 +136,8 @@
   {
     FT_FaceRec     root;
 
-    FT_StreamRec   gzip_stream;
-    FT_Stream      gzip_source;
+    FT_StreamRec   comp_stream;
+    FT_Stream      comp_source;
 
     char*          charset_encoding;
     char*          charset_registry;
--- a/src/pcf/pcfdrivr.c
+++ b/src/pcf/pcfdrivr.c
@@ -2,7 +2,8 @@
 
     FreeType font driver for pcf files
 
-    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 by
+    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009,
+                  2010 by
     Francesco Zappa Nardelli
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -32,6 +33,7 @@
 #include FT_INTERNAL_OBJECTS_H
 #include FT_GZIP_H
 #include FT_LZW_H
+#include FT_BZIP2_H
 #include FT_ERRORS_H
 #include FT_BDF_H
 #include FT_TRUETYPE_IDS_H 
@@ -248,11 +250,11 @@
 
     FT_TRACE4(( "PCF_Face_Done: done face\n" ));
 
-    /* close gzip/LZW stream if any */
-    if ( pcfface->stream == &face->gzip_stream )
+    /* close compressed stream if any */
+    if ( pcfface->stream == &face->comp_stream )
     {
-      FT_Stream_Close( &face->gzip_stream );
-      pcfface->stream = face->gzip_source;
+      FT_Stream_Close( &face->comp_stream );
+      pcfface->stream = face->comp_source;
     }
   }
 
@@ -277,8 +279,9 @@
     {
       PCF_Face_Done( pcfface );
 
-#if defined( FT_CONFIG_OPTION_USE_ZLIB ) || \
-    defined( FT_CONFIG_OPTION_USE_LZW )
+#if defined( FT_CONFIG_OPTION_USE_ZLIB )  || \
+    defined( FT_CONFIG_OPTION_USE_LZW )   || \
+    defined( FT_CONFIG_OPTION_USE_BZIP2 )
 
 #ifdef FT_CONFIG_OPTION_USE_ZLIB
       {
@@ -286,7 +289,7 @@
 
 
         /* this didn't work, try gzip support! */
-        error2 = FT_Stream_OpenGzip( &face->gzip_stream, stream );
+        error2 = FT_Stream_OpenGzip( &face->comp_stream, stream );
         if ( FT_ERROR_BASE( error2 ) == FT_Err_Unimplemented_Feature )
           goto Fail;
 
@@ -301,7 +304,7 @@
 
 
         /* this didn't work, try LZW support! */
-        error3 = FT_Stream_OpenLZW( &face->gzip_stream, stream );
+        error3 = FT_Stream_OpenLZW( &face->comp_stream, stream );
         if ( FT_ERROR_BASE( error3 ) == FT_Err_Unimplemented_Feature )
           goto Fail;
 
@@ -309,11 +312,26 @@
       }
 #endif /* FT_CONFIG_OPTION_USE_LZW */
 
+#ifdef FT_CONFIG_OPTION_USE_BZIP2
       if ( error )
+      {
+        FT_Error  error4;
+
+
+        /* this didn't work, try Bzip2 support! */
+        error4 = FT_Stream_OpenBzip2( &face->comp_stream, stream );
+        if ( FT_ERROR_BASE( error4 ) == FT_Err_Unimplemented_Feature )
+          goto Fail;
+
+        error = error4;
+      }
+#endif /* FT_CONFIG_OPTION_USE_BZIP2 */
+
+      if ( error )
         goto Fail;
 
-      face->gzip_source = stream;
-      pcfface->stream   = &face->gzip_stream;
+      face->comp_source = stream;
+      pcfface->stream   = &face->comp_stream;
 
       stream = pcfface->stream;
 
@@ -321,7 +339,9 @@
       if ( error )
         goto Fail;
 
-#else /* !(FT_CONFIG_OPTION_USE_ZLIB || FT_CONFIG_OPTION_USE_LZW) */
+#else /* !(FT_CONFIG_OPTION_USE_ZLIB ||
+           FT_CONFIG_OPTION_USE_LZW ||
+           FT_CONFIG_OPTION_USE_BZIP2) */
 
       goto Fail;