Commit ed913c2151e40eda626d8d2809ed3229aade2941

Joel Klinghed 2010-12-31T16:59:33

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.

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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
diff --git a/ChangeLog b/ChangeLog
index 4f16413..a4fc2c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,29 @@
+2010-12-31  Joel Klinghed  <the_jk@yahoo.com>
+
+	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  <mm.harsha@gmail.com>
 
 	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
diff --git a/builds/unix/configure.raw b/builds/unix/configure.raw
index add41bf..1785c47 100644
--- a/builds/unix/configure.raw
+++ b/builds/unix/configure.raw
@@ -229,6 +229,20 @@ if test x$with_zlib != xno && test -n "$LIBZ"; then
   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
@@ -644,10 +658,12 @@ esac
 
 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)
diff --git a/devel/ftoption.h b/devel/ftoption.h
index 32dd25a..24f72bc 100644
--- a/devel/ftoption.h
+++ b/devel/ftoption.h
@@ -190,6 +190,22 @@ FT_BEGIN_HEADER
 
   /*************************************************************************/
   /*                                                                       */
+  /* 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     */
diff --git a/docs/formats.txt b/docs/formats.txt
index efec660..827c894 100644
--- a/docs/formats.txt
+++ b/docs/formats.txt
@@ -11,8 +11,9 @@ reference document and whether it is supported in FreeType 2.
   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 @@ MAC  ---     PS     TYPE_1  ---        type1    T1_SPEC.pdf
 
 ---  ---     PCF    ---     ---        pcf      X11 [4]
 ---  LZW     PCF    ---     ---        pcf      X11 [4]
+---  BZ2     PCF    ---     ---        pcf      X11 [4]
 
 
 ---  ---     PFR    PFR0    ---        pfr      [2]
diff --git a/include/freetype/config/ftheader.h b/include/freetype/config/ftheader.h
index b63945d..2a7b8c4 100644
--- 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,       */
@@ -427,6 +427,19 @@
   /*************************************************************************
    *
    * @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>
+
+
+  /*************************************************************************
+   *
+   * @macro:
    *   FT_WINFONTS_H
    *
    * @description:
diff --git a/include/freetype/config/ftoption.h b/include/freetype/config/ftoption.h
index 2b46259..1ab6e1e 100644
--- a/include/freetype/config/ftoption.h
+++ b/include/freetype/config/ftoption.h
@@ -190,6 +190,22 @@ FT_BEGIN_HEADER
 
   /*************************************************************************/
   /*                                                                       */
+  /* 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     */
diff --git a/include/freetype/ftbzip2.h b/include/freetype/ftbzip2.h
new file mode 100644
index 0000000..083c356
--- /dev/null
+++ b/include/freetype/ftbzip2.h
@@ -0,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 */
diff --git a/modules.cfg b/modules.cfg
index 4047d7f..8d5b6b5 100644
--- 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,
@@ -122,6 +122,11 @@ AUX_MODULES += gzip
 # 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.
 #
 # AUX_MODULES += otvalid
diff --git a/src/bzip2/Jamfile b/src/bzip2/Jamfile
new file mode 100644
index 0000000..3da986d
--- /dev/null
+++ b/src/bzip2/Jamfile
@@ -0,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
diff --git a/src/bzip2/ftbzip2.c b/src/bzip2/ftbzip2.c
new file mode 100644
index 0000000..1daffb7
--- /dev/null
+++ b/src/bzip2/ftbzip2.c
@@ -0,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 */
diff --git a/src/bzip2/rules.mk b/src/bzip2/rules.mk
new file mode 100644
index 0000000..0ff2628
--- /dev/null
+++ b/src/bzip2/rules.mk
@@ -0,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
diff --git a/src/pcf/pcf.h b/src/pcf/pcf.h
index 1cd56c1..af0ffc3 100644
--- 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_BEGIN_HEADER
   {
     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;
diff --git a/src/pcf/pcfdrivr.c b/src/pcf/pcfdrivr.c
index 6c0aa0f..af2a2d9 100644
--- 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 @@ THE SOFTWARE.
 #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 @@ THE SOFTWARE.
 
     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 @@ THE SOFTWARE.
     {
       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 @@ THE SOFTWARE.
 
 
         /* 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 @@ THE SOFTWARE.
 
 
         /* 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 @@ THE SOFTWARE.
       }
 #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 @@ THE SOFTWARE.
       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;