ftglue.h 4.8 KB
Newer Older
1
/* ftglue.h: Glue code for compiling the OpenType code from
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
 *           FreeType 1 using only the public API of FreeType 2
 *
 * By David Turner, The FreeType Project (www.freetype.org)
 *
 * This code is explicitely put in the public domain
 *
 * ==========================================================================
 *
 * the OpenType parser codes was originally written as an extension to
 * FreeType 1.x. As such, its source code was embedded within the library,
 * and used many internal FreeType functions to deal with memory and
 * stream i/o.
 *
 * When it was 'salvaged' for Pango and Qt, the code was "ported" to FreeType 2,
 * which basically means that some macro tricks were performed in order to
 * directly access FT2 _internal_ functions.
 *
 * these functions were never part of FT2 public API, and _did_ change between
 * various releases. This created chaos for many users: when they upgraded the
 * FreeType library on their system, they couldn't run Gnome anymore since
 * Pango refused to link.
 *
 * Very fortunately, it's possible to completely avoid this problem because
 * the FT_StreamRec and FT_MemoryRec structure types, which describe how
 * memory and stream implementations interface with the rest of the font
 * library, have always been part of the public API, and never changed.
 *
 * What we do thus is re-implement, within the OpenType parser, the few
 * functions that depend on them. This only adds one or two kilobytes of
 * code, and ensures that the parser can work with _any_ version
 * of FreeType installed on your system. How sweet... !
 *
 * Note that we assume that Pango doesn't use any other internal functions
 * from FreeType. It used to in old versions, but this should no longer
 * be the case. (crossing my fingers).
 *
 *  - David Turner
 *  - The FreeType Project  (www.freetype.org)
 *
 * PS: This "glue" code is explicitely put in the public domain
 */
43 44
#ifndef FTGLUE_H
#define FTGLUE_H
45 46 47 48

#include <ft2build.h>
#include FT_FREETYPE_H

49
#include "harfbuzz-open.h"
50
#include "harfbuzz-impl.h"
51

52
HB_BEGIN_HEADER
53 54 55 56


/* utility macros */

57 58 59 60
#ifndef HB_Error
#define HB_Error FT_Error
#endif

61 62 63
#define  SET_ERR(c)   ( (error = (c)) != 0 )

/* stream macros used by the OpenType parser */
64 65 66 67 68 69
#define  FILE_Pos()      _hb_ftglue_stream_pos( stream )
#define  FILE_Seek(pos)  SET_ERR( _hb_ftglue_stream_seek( stream, pos ) )
#define  ACCESS_Frame(size)  SET_ERR( _hb_ftglue_stream_frame_enter( stream, size ) )
#define  FORGET_Frame()      _hb_ftglue_stream_frame_exit( stream )

#define  GET_Byte()      (*stream->cursor++)
70
#define  GET_Short()     (stream->cursor += 2, (HB_Short)( \
71 72 73
				(*(((FT_Byte*)stream->cursor)-2) << 8) | \
				 *(((FT_Byte*)stream->cursor)-1) \
			 ))
74
#define  GET_Long()      (stream->cursor += 4, (HB_Int)( \
75 76 77 78 79
				(*(((FT_Byte*)stream->cursor)-4) << 24) | \
				(*(((FT_Byte*)stream->cursor)-3) << 16) | \
				(*(((FT_Byte*)stream->cursor)-2) << 8) | \
				 *(((FT_Byte*)stream->cursor)-1) \
			 ))
80 81 82


#define  GET_Char()      ((FT_Char)GET_Byte())
83 84
#define  GET_UShort()    ((HB_UShort)GET_Short())
#define  GET_ULong()     ((HB_UInt)GET_Long())
85 86
#define  GET_Tag4()      GET_ULong()

87
HB_INTERNAL HB_Int
88
_hb_ftglue_stream_pos( FT_Stream   stream );
89

90
HB_INTERNAL HB_Error
91
_hb_ftglue_stream_seek( FT_Stream   stream,
92
                    HB_Int     pos );
93

94
HB_INTERNAL HB_Error
95
_hb_ftglue_stream_frame_enter( FT_Stream   stream,
96
                           HB_UInt    size );
97

98
HB_INTERNAL void
99
_hb_ftglue_stream_frame_exit( FT_Stream  stream );
100

101
HB_INTERNAL HB_Error
102
_hb_ftglue_face_goto_table( FT_Face    face,
103
                        HB_UInt   tag,
104 105 106 107
                        FT_Stream  stream );

/* memory macros used by the OpenType parser */
#define  ALLOC(_ptr,_size)   \
108
           ( (_ptr) = _hb_ftglue_alloc( _size, &error ), error != 0 )
109

110 111
#define  REALLOC(_ptr,_newsz)  \
           ( (_ptr) = _hb_ftglue_realloc( (_ptr), (_newsz), &error ), error != 0 )
112 113

#define  FREE(_ptr)                    \
114
  do {                                 \
115 116
    if ( (_ptr) )                      \
    {                                  \
117
      _hb_ftglue_free( _ptr );         \
118 119
      _ptr = NULL;                     \
    }                                  \
120
  } while (0)
121 122 123 124

#define  ALLOC_ARRAY(_ptr,_count,_type)   \
           ALLOC(_ptr,(_count)*sizeof(_type))

125 126
#define  REALLOC_ARRAY(_ptr,_newcnt,_type) \
           REALLOC(_ptr,(_newcnt)*sizeof(_type))
127 128 129 130

#define  MEM_Copy(dest,source,count)   memcpy( (char*)(dest), (const char*)(source), (size_t)(count) )


131
HB_INTERNAL FT_Pointer
132
_hb_ftglue_alloc( HB_UInt   size,
133
		  HB_Error  *perror_ );
134

135
HB_INTERNAL FT_Pointer
136
_hb_ftglue_realloc( FT_Pointer  block,
137
		    HB_UInt    new_size,
138
		    HB_Error   *perror_ );
139

140
HB_INTERNAL void
141
_hb_ftglue_free( FT_Pointer  block );
142

143 144 145
/* abuse these private header/source files */

/* helper func to set a breakpoint on */
146 147
HB_INTERNAL HB_Error
_hb_err (HB_Error code);
148

149
HB_END_HEADER
150

151
#endif /* FTGLUE_H */