00001 /***************************************************************************/ 00002 /* */ 00003 /* ftzopen.h */ 00004 /* */ 00005 /* FreeType support for .Z compressed files. */ 00006 /* */ 00007 /* This optional component relies on NetBSD's zopen(). It should mainly */ 00008 /* be used to parse compressed PCF fonts, as found with many X11 server */ 00009 /* distributions. */ 00010 /* */ 00011 /* Copyright 2005, 2006, 2007, 2008 by David Turner. */ 00012 /* */ 00013 /* This file is part of the FreeType project, and may only be used, */ 00014 /* modified, and distributed under the terms of the FreeType project */ 00015 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 00016 /* this file you indicate that you have read the license and */ 00017 /* understand and accept it fully. */ 00018 /* */ 00019 /***************************************************************************/ 00020 00021 #ifndef __FT_ZOPEN_H__ 00022 #define __FT_ZOPEN_H__ 00023 00024 #include <ft2build.h> 00025 #include FT_FREETYPE_H 00026 00027 00028 /* 00029 * This is a complete re-implementation of the LZW file reader, 00030 * since the old one was incredibly badly written, using 00031 * 400 KByte of heap memory before decompressing anything. 00032 * 00033 */ 00034 00035 #define FT_LZW_IN_BUFF_SIZE 64 00036 #define FT_LZW_DEFAULT_STACK_SIZE 64 00037 00038 #define LZW_INIT_BITS 9 00039 #define LZW_MAX_BITS 16 00040 00041 #define LZW_CLEAR 256 00042 #define LZW_FIRST 257 00043 00044 #define LZW_BIT_MASK 0x1f 00045 #define LZW_BLOCK_MASK 0x80 00046 #define LZW_MASK( n ) ( ( 1U << (n) ) - 1U ) 00047 00048 00049 typedef enum FT_LzwPhase_ 00050 { 00051 FT_LZW_PHASE_START = 0, 00052 FT_LZW_PHASE_CODE, 00053 FT_LZW_PHASE_STACK, 00054 FT_LZW_PHASE_EOF 00055 00056 } FT_LzwPhase; 00057 00058 00059 /* 00060 * state of LZW decompressor 00061 * 00062 * small technical note 00063 * -------------------- 00064 * 00065 * We use a few tricks in this implementation that are explained here to 00066 * ease debugging and maintenance. 00067 * 00068 * - First of all, the `prefix' and `suffix' arrays contain the suffix 00069 * and prefix for codes over 256; this means that 00070 * 00071 * prefix_of(code) == state->prefix[code-256] 00072 * suffix_of(code) == state->suffix[code-256] 00073 * 00074 * Each prefix is a 16-bit code, and each suffix an 8-bit byte. 00075 * 00076 * Both arrays are stored in a single memory block, pointed to by 00077 * `state->prefix'. This means that the following equality is always 00078 * true: 00079 * 00080 * state->suffix == (FT_Byte*)(state->prefix + state->prefix_size) 00081 * 00082 * Of course, state->prefix_size is the number of prefix/suffix slots 00083 * in the arrays, corresponding to codes 256..255+prefix_size. 00084 * 00085 * - `free_ent' is the index of the next free entry in the `prefix' 00086 * and `suffix' arrays. This means that the corresponding `next free 00087 * code' is really `256+free_ent'. 00088 * 00089 * Moreover, `max_free' is the maximum value that `free_ent' can reach. 00090 * 00091 * `max_free' corresponds to `(1 << max_bits) - 256'. Note that this 00092 * value is always <= 0xFF00, which means that both `free_ent' and 00093 * `max_free' can be stored in an FT_UInt variable, even on 16-bit 00094 * machines. 00095 * 00096 * If `free_ent == max_free', you cannot add new codes to the 00097 * prefix/suffix table. 00098 * 00099 * - `num_bits' is the current number of code bits, starting at 9 and 00100 * growing each time `free_ent' reaches the value of `free_bits'. The 00101 * latter is computed as follows 00102 * 00103 * if num_bits < max_bits: 00104 * free_bits = (1 << num_bits)-256 00105 * else: 00106 * free_bits = max_free + 1 00107 * 00108 * Since the value of `max_free + 1' can never be reached by 00109 * `free_ent', `num_bits' cannot grow larger than `max_bits'. 00110 */ 00111 00112 typedef struct FT_LzwStateRec_ 00113 { 00114 FT_LzwPhase phase; 00115 FT_Int in_eof; 00116 00117 FT_Byte buf_tab[16]; 00118 FT_Int buf_offset; 00119 FT_Int buf_size; 00120 FT_Bool buf_clear; 00121 FT_Offset buf_total; 00122 00123 FT_UInt max_bits; /* max code bits, from file header */ 00124 FT_Int block_mode; /* block mode flag, from file header */ 00125 FT_UInt max_free; /* (1 << max_bits) - 256 */ 00126 00127 FT_UInt num_bits; /* current code bit number */ 00128 FT_UInt free_ent; /* index of next free entry */ 00129 FT_UInt free_bits; /* if reached by free_ent, increment num_bits */ 00130 FT_UInt old_code; 00131 FT_UInt old_char; 00132 FT_UInt in_code; 00133 00134 FT_UShort* prefix; /* always dynamically allocated / reallocated */ 00135 FT_Byte* suffix; /* suffix = (FT_Byte*)(prefix + prefix_size) */ 00136 FT_UInt prefix_size; /* number of slots in `prefix' or `suffix' */ 00137 00138 FT_Byte* stack; /* character stack */ 00139 FT_UInt stack_top; 00140 FT_Offset stack_size; 00141 FT_Byte stack_0[FT_LZW_DEFAULT_STACK_SIZE]; /* minimize heap alloc */ 00142 00143 FT_Stream source; /* source stream */ 00144 FT_Memory memory; 00145 00146 } FT_LzwStateRec, *FT_LzwState; 00147 00148 00149 FT_LOCAL( void ) 00150 ft_lzwstate_init( FT_LzwState state, 00151 FT_Stream source ); 00152 00153 FT_LOCAL( void ) 00154 ft_lzwstate_done( FT_LzwState state ); 00155 00156 00157 FT_LOCAL( void ) 00158 ft_lzwstate_reset( FT_LzwState state ); 00159 00160 00161 FT_LOCAL( FT_ULong ) 00162 ft_lzwstate_io( FT_LzwState state, 00163 FT_Byte* buffer, 00164 FT_ULong out_size ); 00165 00166 /* */ 00167 00168 #endif /* __FT_ZOPEN_H__ */ 00169 00170 00171 /* END */