| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
#ifndef CFFWRITE_SHARE_H |
| 6 |
#define CFFWRITE_SHARE_H |
| 7 |
|
| 8 |
#include "cffwrite.h" |
| 9 |
#include "dynarr.h" |
| 10 |
#include "ctutil.h" |
| 11 |
|
| 12 |
#include "supportexcept.h" |
| 13 |
|
| 14 |
/* --------------------------- Shared Definitions -------------------------- */ |
| 15 |
|
| 16 |
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) |
| 17 |
|
| 18 |
typedef unsigned char OffSize; /* Offset size indicator */ |
| 19 |
typedef long Offset; /* 1-4 byte offset */ |
| 20 |
|
| 21 |
typedef unsigned short SRI; /* String record index */ |
| 22 |
#define SRI_UNDEF 0xffff /* SRI of undefined string */ |
| 23 |
|
| 24 |
typedef unsigned short SID; /* String id */ |
| 25 |
#define SID_SIZE 2 /* SID CFF size */ |
| 26 |
#define SID_UNDEF 0xffff /* SID of undefined string */ |
| 27 |
#define SID_NOTDEF 0 /* SID for .notdef */ |
| 28 |
|
| 29 |
typedef unsigned short GID; /* Glyph id */ |
| 30 |
|
| 31 |
#define OFF_SIZE(o) \ |
| 32 |
((OffSize)(((o) > 0x00ffffff) ? 4 : (((o) > 0x0000ffff) ? 3 : (((o) > 0x000000ff) ? 2 : 1)))) |
| 33 |
|
| 34 |
typedef struct /* INDEX */ |
| 35 |
{ |
| 36 |
unsigned long count; /* Element count */ |
| 37 |
OffSize offSize; /* Offset size */ |
| 38 |
long datasize; /* Data size */ |
| 39 |
unsigned short bias; /* Subr number bias */ |
| 40 |
} INDEX; |
| 41 |
|
| 42 |
/* INDEX macros */ |
| 43 |
#define INDEX_HDR_SIZE (2 + 1) |
| 44 |
#define INDEX2_HDR_SIZE (4 + 1) |
| 45 |
#define INDEX_OFF_SIZE(size) OFF_SIZE((size) + 1) |
| 46 |
#define INDEX_SIZE(items, size) \ |
| 47 |
(((items) == 0) ? 2 : (INDEX_HDR_SIZE + ((items) + 1) * INDEX_OFF_SIZE(size) + (size))) |
| 48 |
#define INDEX2_SIZE(items, size) \ |
| 49 |
(((items) == 0) ? 4 : (INDEX2_HDR_SIZE + ((items) + 1) * INDEX_OFF_SIZE(size) + (size))) |
| 50 |
|
| 51 |
/* Library-wide utility functions */ |
| 52 |
void CTL_CDECL cfwFatal(cfwCtx g, int err_code, char *fmt, ...); |
| 53 |
void CTL_CDECL cfwMessage(cfwCtx g, char *fmt, ...); |
| 54 |
|
| 55 |
void *cfwMemNew(cfwCtx g, size_t size); |
| 56 |
void cfwMemFree(cfwCtx g, void *ptr); |
| 57 |
|
| 58 |
void cfwWrite1(cfwCtx g, unsigned char value); |
| 59 |
void cfwWrite2(cfwCtx g, unsigned short value); |
| 60 |
void cfwWriteN(cfwCtx g, int N, unsigned long value); |
| 61 |
void cfwWrite(cfwCtx g, size_t count, char *buf); |
| 62 |
|
| 63 |
int cfwEncInt(long i, unsigned char *t); |
| 64 |
int cfwEncReal(float r, unsigned char *t); |
| 65 |
|
| 66 |
long cfwSeenGlyph(cfwCtx g, abfGlyphInfo *info, int *result, |
| 67 |
long startNew, long endNew); |
| 68 |
void cfwAddGlyph(cfwCtx g, abfGlyphInfo *info, float hAdv, long length, |
| 69 |
long offset, long seen_index); |
| 70 |
|
| 71 |
/* -------------------------------- Contexts ------------------------------- |
| 72 |
|
| 73 |
The cffwrite library implements a data hiding model based on contexts. A |
| 74 |
context is an instance of data a module or the library uses in order to |
| 75 |
perform its function and is private to the module or library, i.e. not |
| 76 |
visible externally. |
| 77 |
|
| 78 |
The library context (cfwCtx) is public to the modules that make up the |
| 79 |
library (but not to the client) and is defined below. The library context |
| 80 |
contains a structure (called ctx) containing an opaque context pointer for |
| 81 |
each module. |
| 82 |
|
| 83 |
By convention the library context is passed around in a variable called g |
| 84 |
(mnemonic: global) and each module passes its context (<module>Ctx) around |
| 85 |
as h (mnemonic: handle and h follows g in the alphabet). |
| 86 |
|
| 87 |
The only way to manipulate the data in a module context is by calling a |
| 88 |
function (method) from the module. By convention each external function |
| 89 |
provided by a module takes a g pointer as its first argument and internal |
| 90 |
functions typically take an h pointer as their first argument. |
| 91 |
|
| 92 |
A g pointer can be converted to an h pointer using the code: |
| 93 |
|
| 94 |
<module>Ctx h = g->ctx.<module>; |
| 95 |
|
| 96 |
Each module context contains a field called g which is a pointer to the |
| 97 |
global context. Thus, an h pointer can be converted to a g pointer as |
| 98 |
follows: h->g, which permits cross-module calling. |
| 99 |
|
| 100 |
Module contexts are created by calling <module>New(g), prepared for reuse by |
| 101 |
calling <module>reuse(g), and are and destroyed by calling <module>Free(g). |
| 102 |
Similarly, a client creates the library context by calling cfwNew() and |
| 103 |
destroys it by calling cfwFree(). */ |
| 104 |
|
| 105 |
typedef struct controlCtx_ *controlCtx; |
| 106 |
typedef struct charsetCtx_ *charsetCtx; |
| 107 |
typedef struct encodingCtx_ *encodingCtx; |
| 108 |
typedef struct fdselectCtx_ *fdselectCtx; |
| 109 |
typedef struct sindexCtx_ *sindexCtx; |
| 110 |
typedef struct dictCtx_ *dictCtx; |
| 111 |
typedef struct cstrCtx_ *cstrCtx; |
| 112 |
typedef struct subrCtx_ *subrCtx; |
| 113 |
|
| 114 |
/* Library context (the one returned to client) */ |
| 115 |
struct cfwCtx_ { |
| 116 |
long flags; /* Control flags */ |
| 117 |
struct /* Client callbacks */ |
| 118 |
{ |
| 119 |
ctlMemoryCallbacks mem; |
| 120 |
ctlStreamCallbacks stm; |
| 121 |
} cb; |
| 122 |
struct /* Streams */ |
| 123 |
{ |
| 124 |
void *dst; |
| 125 |
void *tmp; |
| 126 |
void *dbg; |
| 127 |
} stm; |
| 128 |
struct /* Temporary stream */ |
| 129 |
{ |
| 130 |
long offset; /* Buffer offset */ |
| 131 |
size_t length; /* Buffer length */ |
| 132 |
char *buf; /* Buffer beginning */ |
| 133 |
char *end; /* Buffer end */ |
| 134 |
char *next; /* Next byte available (buf <= next < end) */ |
| 135 |
} tmp; |
| 136 |
struct /* Service library and module contexts */ |
| 137 |
{ |
| 138 |
dnaCtx dnaSafe; /* longjmp on error */ |
| 139 |
dnaCtx dnaFail; /* Return on error */ |
| 140 |
controlCtx control; |
| 141 |
charsetCtx charset; |
| 142 |
encodingCtx encoding; |
| 143 |
fdselectCtx fdselect; |
| 144 |
sindexCtx sindex; |
| 145 |
dictCtx dict; |
| 146 |
cstrCtx cstr; |
| 147 |
subrCtx subr; |
| 148 |
} ctx; |
| 149 |
struct /* Error handling */ |
| 150 |
{ |
| 151 |
_Exc_Buf env; |
| 152 |
short code; |
| 153 |
} err; |
| 154 |
unsigned long maxNumSubrs; |
| 155 |
struct /* glyph metrics */ |
| 156 |
{ |
| 157 |
struct abfMetricsCtx_ ctx; |
| 158 |
abfGlyphCallbacks cb; |
| 159 |
} glyph_metrics; |
| 160 |
struct /* aggregate font bounding box metrics */ |
| 161 |
{ |
| 162 |
int16_t left; |
| 163 |
int16_t bottom; |
| 164 |
int16_t right; |
| 165 |
int16_t top; |
| 166 |
} font_bbox; |
| 167 |
}; |
| 168 |
|
| 169 |
#endif /* CFFWRITE_SHARE_H */ |