| 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 |
|
| 6 |
#include "cffwrite_encoding.h" |
| 7 |
|
| 8 |
#include <string.h> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
typedef struct /* Supplementary encoding */ |
| 12 |
{ |
| 13 |
unsigned char code; |
| 14 |
SID sid; |
| 15 |
} SupCode; |
| 16 |
#define SUP_CODE_SIZE (1 + 2) |
| 17 |
|
| 18 |
/* Declarations used for determining sizes and for format documentation */ |
| 19 |
typedef struct { |
| 20 |
unsigned char format; |
| 21 |
unsigned char nCodes; |
| 22 |
unsigned char *code; /* [nCodes] */ |
| 23 |
} Format0; |
| 24 |
#define FORMAT0_SIZE(n) (2 * 1 + (n)*1) |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
unsigned char first; /* First code in range */ |
| 28 |
unsigned char nLeft; /* Number codes left in range (excluding first) */ |
| 29 |
} Range; |
| 30 |
#define RANGE_SIZE (2 * 1) |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
unsigned char format; |
| 34 |
unsigned char nRanges; |
| 35 |
Range *range; /* [nRanges] */ |
| 36 |
} Format1; |
| 37 |
#define FORMAT1_SIZE(n) (2 * 1 + (n)*RANGE_SIZE) |
| 38 |
|
| 39 |
/* Supplemental encodings */ |
| 40 |
typedef struct { |
| 41 |
unsigned char nSups; |
| 42 |
SupCode *supplement; |
| 43 |
} Supplemental; |
| 44 |
#define SUPPLEMENTAL_SIZE(n) (1 + (n)*SUP_CODE_SIZE) |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
dnaDCL(unsigned char, codes); /* Character codes */ |
| 48 |
dnaDCL(SupCode, supcodes); /* Supplementary codes */ |
| 49 |
unsigned char nRanges; /* # code ranges */ |
| 50 |
unsigned char format; /* Selected format */ |
| 51 |
Offset offset; /* Offset relative to first encoding */ |
| 52 |
} Encoding; |
| 53 |
|
| 54 |
/* Predefined encodings */ |
| 55 |
static const unsigned char stdenc[] = { |
| 56 |
#include "stdenc0.h" /* 0 Standard */ |
| 57 |
}; |
| 58 |
static const unsigned char exenc[] = { |
| 59 |
#include "exenc0.h" /* 1 Expert (also used with expert subset fonts) */ |
| 60 |
}; |
| 61 |
|
| 62 |
static const struct { |
| 63 |
short cnt; |
| 64 |
const unsigned char *array; |
| 65 |
} predefs[] = { |
| 66 |
{ARRAY_LEN(stdenc), stdenc}, |
| 67 |
{ARRAY_LEN(exenc), exenc}, |
| 68 |
}; |
| 69 |
#define PREDEF_CNT ARRAY_LEN(predefs) |
| 70 |
|
| 71 |
/* ----------------------------- Module Context ---------------------------- */ |
| 72 |
|
| 73 |
struct encodingCtx_ { |
| 74 |
dnaDCL(Encoding, encodings); |
| 75 |
Encoding *_new; /* Encoding being added */ |
| 76 |
cfwCtx g; /* Package context */ |
| 77 |
}; |
| 78 |
|
| 79 |
/* Initialize encoding. */ |
| 80 |
static void initEncoding(void *ctx, long cnt, Encoding *encoding) { |
| 81 |
cfwCtx g = (cfwCtx)ctx; |
| 82 |
while (cnt--) { |
| 83 |
dnaINIT(g->ctx.dnaSafe, encoding->codes, 256, 256); |
| 84 |
dnaINIT(g->ctx.dnaSafe, encoding->supcodes, 5, 10); |
| 85 |
encoding++; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/* Initialize module. */ |
| 90 |
void cfwEncodingNew(cfwCtx g) { |
| 91 |
encodingCtx h = (encodingCtx)cfwMemNew(g, sizeof(struct encodingCtx_)); |
| 92 |
|
| 93 |
/* Link contexts */ |
| 94 |
h->g = g; |
| 95 |
g->ctx.encoding = h; |
| 96 |
|
| 97 |
dnaINIT(g->ctx.dnaSafe, h->encodings, 1, 10); |
| 98 |
h->encodings.func = initEncoding; |
| 99 |
} |
| 100 |
|
| 101 |
/* Prepare module for reuse. */ |
| 102 |
void cfwEncodingReuse(cfwCtx g) { |
| 103 |
encodingCtx h = g->ctx.encoding; |
| 104 |
h->encodings.cnt = 0; |
| 105 |
} |
| 106 |
|
| 107 |
/* Free resources */ |
| 108 |
void cfwEncodingFree(cfwCtx g) { |
| 109 |
encodingCtx h = g->ctx.encoding; |
| 110 |
int i; |
| 111 |
|
| 112 |
if (h == NULL) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
for (i = 0; i < h->encodings.size; i++) { |
| 117 |
Encoding *encoding = &h->encodings.array[i]; |
| 118 |
dnaFREE(encoding->codes); |
| 119 |
dnaFREE(encoding->supcodes); |
| 120 |
} |
| 121 |
dnaFREE(h->encodings); |
| 122 |
|
| 123 |
cfwMemFree(g, h); |
| 124 |
g->ctx.encoding = NULL; |
| 125 |
} |
| 126 |
|
| 127 |
/* Get pre-defined encoding identified by "id" (beginning at 0). An array of |
| 128 |
"cnt" elements is returned that maps an SID to a character code or 0 if |
| 129 |
unencoded. A return of NULL indicates no encoding is pre-defined for the |
| 130 |
specified id. */ |
| 131 |
const unsigned char *cfwEncodingGetPredef(int id, int *cnt) { |
| 132 |
if (id >= (int)PREDEF_CNT) { |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
*cnt = predefs[id].cnt; |
| 137 |
return predefs[id].array; |
| 138 |
} |
| 139 |
|
| 140 |
/* Begin new encoding. */ |
| 141 |
void cfwEncodingBeg(cfwCtx g) { |
| 142 |
encodingCtx h = g->ctx.encoding; |
| 143 |
h->_new = dnaNEXT(h->encodings); |
| 144 |
h->_new->codes.cnt = 0; |
| 145 |
h->_new->supcodes.cnt = 0; |
| 146 |
} |
| 147 |
|
| 148 |
/* Add code point to encoding. */ |
| 149 |
void cfwEncodingAddCode(cfwCtx g, unsigned char code) { |
| 150 |
encodingCtx h = g->ctx.encoding; |
| 151 |
*dnaNEXT(h->_new->codes) = code; |
| 152 |
} |
| 153 |
|
| 154 |
/* Add supplementary code point to encoding. */ |
| 155 |
void cfwEncodingAddSupCode(cfwCtx g, unsigned char code, SID sid) { |
| 156 |
encodingCtx h = g->ctx.encoding; |
| 157 |
SupCode *sup = dnaNEXT(h->_new->supcodes); |
| 158 |
sup->code = code; |
| 159 |
sup->sid = sid; |
| 160 |
} |
| 161 |
|
| 162 |
/* Compare supplementary encodings. */ |
| 163 |
static int CTL_CDECL cmpSupCodes(const void *first, const void *second) { |
| 164 |
SupCode *a = (SupCode *)first; |
| 165 |
SupCode *b = (SupCode *)second; |
| 166 |
if (a->code < b->code) { |
| 167 |
return -1; |
| 168 |
} else if (a->code > b->code) { |
| 169 |
return 1; |
| 170 |
} else if (a->sid < b->sid) { |
| 171 |
return -1; |
| 172 |
} else if (a->sid > b->sid) { |
| 173 |
return 1; |
| 174 |
} else { |
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/* End code point addition and check for duplicate encoding. */ |
| 180 |
int cfwEncodingEnd(cfwCtx g) { |
| 181 |
encodingCtx h = g->ctx.encoding; |
| 182 |
int i; |
| 183 |
|
| 184 |
/* Sort supplementary encoding by code */ |
| 185 |
if (h->_new->supcodes.cnt > 0) { |
| 186 |
qsort(h->_new->supcodes.array, h->_new->supcodes.cnt, sizeof(SupCode), |
| 187 |
cmpSupCodes); |
| 188 |
} |
| 189 |
|
| 190 |
for (i = 0; i < h->encodings.cnt - 1; i++) { |
| 191 |
Encoding *encoding = &h->encodings.array[i]; |
| 192 |
|
| 193 |
if (h->_new->codes.cnt == encoding->codes.cnt && |
| 194 |
h->_new->supcodes.cnt == encoding->supcodes.cnt && |
| 195 |
memcmp(h->_new->codes.array, |
| 196 |
encoding->codes.array, |
| 197 |
h->_new->codes.cnt) == 0 && |
| 198 |
memcmp(h->_new->supcodes.array, |
| 199 |
encoding->supcodes.array, |
| 200 |
h->_new->supcodes.cnt) == 0) { |
| 201 |
/* Found match; remove new encoding from list */ |
| 202 |
h->encodings.cnt--; |
| 203 |
return PREDEF_CNT + i; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* No match found; leave encoding in list */ |
| 208 |
return PREDEF_CNT + h->encodings.cnt - 1; |
| 209 |
} |
| 210 |
|
| 211 |
/* Fill encodings and return total size of all encodings. */ |
| 212 |
long cfwEncodingFill(cfwCtx g) { |
| 213 |
encodingCtx h = g->ctx.encoding; |
| 214 |
int i; |
| 215 |
Offset offset; |
| 216 |
|
| 217 |
offset = 0; |
| 218 |
for (i = 0; i < h->encodings.cnt; i++) { |
| 219 |
int j; |
| 220 |
Encoding *encoding = &h->encodings.array[i]; |
| 221 |
long size0; |
| 222 |
long size1; |
| 223 |
|
| 224 |
/* Count ranges */ |
| 225 |
encoding->nRanges = 1; |
| 226 |
for (j = 1; j < encoding->codes.cnt; j++) { |
| 227 |
if (encoding->codes.array[j - 1] + 1 != encoding->codes.array[j]) { |
| 228 |
encoding->nRanges++; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
encoding->offset = offset; |
| 233 |
|
| 234 |
/* Save format and offset */ |
| 235 |
size0 = FORMAT0_SIZE(encoding->codes.cnt); |
| 236 |
size1 = FORMAT1_SIZE(encoding->nRanges); |
| 237 |
if (size0 < size1) { |
| 238 |
/* Select format 0 */ |
| 239 |
offset += size0; |
| 240 |
encoding->format = 0; |
| 241 |
} else { |
| 242 |
/* Select format 1 */ |
| 243 |
offset += size1; |
| 244 |
encoding->format = 1; |
| 245 |
} |
| 246 |
if (encoding->supcodes.cnt > 0) { |
| 247 |
encoding->format |= 0x80; |
| 248 |
offset += SUPPLEMENTAL_SIZE(encoding->supcodes.cnt); |
| 249 |
} |
| 250 |
} |
| 251 |
return offset; |
| 252 |
} |
| 253 |
|
| 254 |
/* Write encodings. */ |
| 255 |
void cfwEncodingWrite(cfwCtx g) { |
| 256 |
encodingCtx h = g->ctx.encoding; |
| 257 |
int i; |
| 258 |
|
| 259 |
/* Write non-standard encodings */ |
| 260 |
for (i = 0; i < h->encodings.cnt; i++) { |
| 261 |
int j; |
| 262 |
Encoding *encoding = &h->encodings.array[i]; |
| 263 |
|
| 264 |
cfwWrite1(g, encoding->format); |
| 265 |
switch (encoding->format & 0x7f) { |
| 266 |
case 0: |
| 267 |
/* Write format 0 */ |
| 268 |
cfwWrite1(g, (unsigned char)encoding->codes.cnt); |
| 269 |
cfwWrite(g, encoding->codes.cnt, (char *)encoding->codes.array); |
| 270 |
break; |
| 271 |
|
| 272 |
case 1: { |
| 273 |
/* Write format 1 */ |
| 274 |
unsigned char nLeft = 0; |
| 275 |
|
| 276 |
cfwWrite1(g, encoding->nRanges); |
| 277 |
cfwWrite1(g, encoding->codes.array[0]); |
| 278 |
for (j = 1; j < encoding->codes.cnt; j++) { |
| 279 |
if (encoding->codes.array[j - 1] + 1 != |
| 280 |
encoding->codes.array[j] || |
| 281 |
nLeft == 255) { |
| 282 |
cfwWrite1(g, nLeft); |
| 283 |
cfwWrite1(g, encoding->codes.array[j]); |
| 284 |
nLeft = 0; |
| 285 |
} else { |
| 286 |
nLeft++; |
| 287 |
} |
| 288 |
} |
| 289 |
cfwWrite1(g, nLeft); |
| 290 |
} break; |
| 291 |
} |
| 292 |
if (encoding->format & 0x80) { |
| 293 |
cfwWrite1(g, (unsigned char)encoding->supcodes.cnt); |
| 294 |
for (j = 0; j < encoding->supcodes.cnt; j++) { |
| 295 |
SupCode *supplement = &encoding->supcodes.array[j]; |
| 296 |
cfwWrite1(g, supplement->code); |
| 297 |
cfwWrite2(g, supplement->sid); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/* Get absolute encoding offset. */ |
| 304 |
Offset cfwEncodingGetOffset(cfwCtx g, int iEncoding, Offset base) { |
| 305 |
encodingCtx h = g->ctx.encoding; |
| 306 |
return (iEncoding < (int)PREDEF_CNT) ? iEncoding : base + h->encodings.array[iEncoding - PREDEF_CNT].offset; |
| 307 |
} |