| 1 |
/* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki. |
| 2 |
|
| 3 |
Copyright (C) 2016 by VTeX Ltd (www.vtex.lt), |
| 4 |
the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis. |
| 5 |
|
| 6 |
Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and |
| 7 |
Shunsaku Hirata, the dvipdfmx project team. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* CID-Keyed Font support: |
| 26 |
* |
| 27 |
* Only CFF/OpenType CID-Keyed Font with Type 2 charstrings is supported. |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
|
| 33 |
#include "system.h" |
| 34 |
#include "numbers.h" |
| 35 |
#include "mem.h" |
| 36 |
#include "error.h" |
| 37 |
|
| 38 |
#include "xdvips.h" |
| 39 |
#include "protos.h" |
| 40 |
|
| 41 |
#include "cff_types.h" |
| 42 |
#include "cff_limits.h" |
| 43 |
#include "cff.h" |
| 44 |
#include "cff_dict.h" |
| 45 |
#include "cs_type2.h" |
| 46 |
|
| 47 |
#include "cmap.h" |
| 48 |
#include "cmap_write.h" |
| 49 |
#include "cidtype0.h" |
| 50 |
|
| 51 |
#define c1 52845 |
| 52 |
#define c2 22719 |
| 53 |
|
| 54 |
static int verbose = 0; |
| 55 |
static UT_icd subr_icd = {sizeof(cs_type1subr), NULL, NULL, NULL }; |
| 56 |
|
| 57 |
|
| 58 |
/* These subroutines are code by Adobe for this exact use (from T1_Spec.pdf) */ |
| 59 |
|
| 60 |
/* 3 0 callothersubr pop pop setcurrentpoint return */ |
| 61 |
static const card8 subrs0[] = { 3+139, 0+139, 12, 16, 12, 17, 12, 17, 12, 33, 11 }; |
| 62 |
/* 0 1 callothersubr return */ |
| 63 |
static const card8 subrs1[] = { 0+139, 1+139, 12, 16, 11 }; |
| 64 |
/* 0 2 callothersubr return */ |
| 65 |
static const card8 subrs2[] = { 0+139, 2+139, 12, 16, 11 }; |
| 66 |
/* return */ |
| 67 |
static const card8 subrs3[] = { 11 }; |
| 68 |
/* This one I created myself to do hint substitution */ |
| 69 |
/* <subr number presumed to be on stack> 1 3 callother pop callsubr */ |
| 70 |
static const card8 subrs4[] = { 1+139, 3+139, 12, 16, 12, 17, 10, 11 }; |
| 71 |
|
| 72 |
/* These others from adobe for multiple master */ |
| 73 |
/* They need some fix up before they are used (the stack count depends on the # instances). */ |
| 74 |
/* <n> 14 callothersubr pop return */ |
| 75 |
static const card8 subrs5[] = { 139, 14+139, 12, 16, 12, 17, 11 }; |
| 76 |
/* 2*<n> 15 callothersubr pop pop return */ |
| 77 |
static const card8 subrs6[] = { 139, 15+139, 12, 16, 12, 17, 12, 17, 11 }; |
| 78 |
/* 3*<n> 16 callothersubr pop pop pop return */ |
| 79 |
static const card8 subrs7[] = { 139, 16+139, 12, 16, 12, 17, 12, 17, 12, 17, 11 }; |
| 80 |
/* 4*<n> 17 callothersubr pop pop pop pop return */ |
| 81 |
static const card8 subrs8[] = { 139, 17+139, 12, 16, 12, 17, 12, 17, 12, 17, 12, 17, 11 }; |
| 82 |
/* 6*<n> 18 callothersubr pop pop pop pop pop pop return */ |
| 83 |
static const card8 subrs9[] = { 139, 18+139, 12, 16, 12, 17, 12, 17, 12, 17, 12, 17, 12, 17, 12, 17, 11 }; |
| 84 |
|
| 85 |
|
| 86 |
static const card8 *const subrs[] = { subrs0, subrs1, subrs2, subrs3, subrs4, |
| 87 |
subrs5, subrs6, subrs7, subrs8, subrs9 }; |
| 88 |
static const int subrslens[] = { sizeof(subrs0), sizeof(subrs1), sizeof(subrs2), |
| 89 |
sizeof(subrs3), sizeof(subrs4), sizeof(subrs5), sizeof(subrs6), |
| 90 |
sizeof(subrs7), sizeof(subrs8), sizeof(subrs9) }; |
| 91 |
|
| 92 |
static void initsubrs(UT_array *t1_subrs) { |
| 93 |
int i; |
| 94 |
cs_type1subr subr; |
| 95 |
|
| 96 |
for ( i=0; i<5; ++i ) { |
| 97 |
subr.size = subrslens[i]; |
| 98 |
subr.data = NEW(subrslens[i],card8); |
| 99 |
memcpy(subr.data,subrs[i],subrslens[i]); |
| 100 |
utarray_push_back(t1_subrs,&subr); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/* Encode a string in adobe's format. choose a different set of initial random*/ |
| 105 |
/* bytes every time. (the expected value of leniv is 4. we have some support */ |
| 106 |
/* for values bigger than 5 but not as much as for values <=4) */ |
| 107 |
static void encodestrout(card8 *binary,long *offset,unsigned char *value, int len, int leniv) { |
| 108 |
unsigned short r = 4330; |
| 109 |
long k = *offset; |
| 110 |
unsigned char plain, cypher; |
| 111 |
static unsigned char randombytes[10] = { 0xaa, 0x55, 0x3e, 0x4d, 0x89, 0x98, 'a', '4', 0, 0xff }; |
| 112 |
|
| 113 |
randombytes[0] += 3; |
| 114 |
randombytes[1] += 5; |
| 115 |
randombytes[2] += 7; |
| 116 |
randombytes[3] += 11; |
| 117 |
randombytes[4] += 13; |
| 118 |
|
| 119 |
while ( leniv>0 ) { |
| 120 |
plain = randombytes[leniv--%10]; |
| 121 |
cypher = ( plain ^ (r>>8)); |
| 122 |
r = (cypher + r) * c1 + c2; |
| 123 |
binary[k] = cypher; |
| 124 |
k++; |
| 125 |
} |
| 126 |
while ( len-->0 ) { |
| 127 |
plain = *value++; |
| 128 |
cypher = ( plain ^ (r>>8)); |
| 129 |
r = (cypher + r) * c1 + c2; |
| 130 |
binary[k] = cypher; |
| 131 |
k++; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void dumpt1str(card8 *binary,long *offset,card8 *data, int len, int leniv) { |
| 136 |
long k; |
| 137 |
|
| 138 |
if ( len == 0 ) return; |
| 139 |
k = *offset; |
| 140 |
if ( leniv==-1 ) { |
| 141 |
memcpy(binary + k,data,len); |
| 142 |
k += len; |
| 143 |
} |
| 144 |
else { |
| 145 |
encodestrout(binary,offset,data,len,leniv); |
| 146 |
k += len + leniv; |
| 147 |
} |
| 148 |
*offset = k; |
| 149 |
} |
| 150 |
|
| 151 |
static void dump_index(card8 *binary,long *offset,int size,int val) { |
| 152 |
long k; |
| 153 |
|
| 154 |
if ( size == 0 ) return; |
| 155 |
k = *offset; |
| 156 |
if ( size>=4 ) { |
| 157 |
binary[k] = (val>>24)&0xff; |
| 158 |
k++; |
| 159 |
} |
| 160 |
if ( size>=3 ) { |
| 161 |
binary[k] = (val>>16)&0xff; |
| 162 |
k++; |
| 163 |
} |
| 164 |
if ( size>=2 ) { |
| 165 |
binary[k] = (val>>8)&0xff; |
| 166 |
k++; |
| 167 |
} |
| 168 |
if ( size>=1 ) { /* Yes, size may be 0 for the fd index */ |
| 169 |
binary[k] = (val)&0xff; |
| 170 |
k++; |
| 171 |
} |
| 172 |
*offset = k; |
| 173 |
} |
| 174 |
|
| 175 |
static long |
| 176 |
gencidbinarydata(cff_font *cffont, const UsedMapElem *used_chars, |
| 177 |
UT_array *t1_glyphs, UT_array *t1_subrs, |
| 178 |
struct cidbytes *cidbytes, card8 **bindata) |
| 179 |
{ |
| 180 |
int i,j, gid, leniv, subrtot; |
| 181 |
struct fddata *fd; |
| 182 |
card8 *binary; |
| 183 |
long offset, subrs_len, chrs_len, binary_len, bin_offset; |
| 184 |
int len, glyphscnt; |
| 185 |
cs_type1subr *p; |
| 186 |
|
| 187 |
memset(cidbytes,'\0',sizeof(struct cidbytes)); |
| 188 |
cidbytes->cidcnt = (int)cff_dict_get(cffont->topdict, "CIDCount", 0); |
| 189 |
cidbytes->fdcnt = cffont->num_fds; |
| 190 |
cidbytes->fds = NEW(cidbytes->fdcnt,struct fddata); |
| 191 |
subrtot = 0; |
| 192 |
subrs_len = 0; |
| 193 |
chrs_len = 0; |
| 194 |
leniv = 1; |
| 195 |
for ( i=0; i<cidbytes->fdcnt; ++i ) { |
| 196 |
fd = &cidbytes->fds[i]; |
| 197 |
fd->subrs = t1_subrs; |
| 198 |
if ( cff_dict_known(cffont->privat[i],"lenIV") ) |
| 199 |
fd->leniv = (int)cff_dict_get(cffont->privat[i],"lenIV",0); |
| 200 |
else |
| 201 |
fd->leniv = 1; |
| 202 |
fd->subrcnt = utarray_len(fd->subrs); |
| 203 |
subrtot += fd->subrcnt; |
| 204 |
for ( j=0; j<fd->subrcnt; j++ ) { |
| 205 |
p = (cs_type1subr *)utarray_eltptr(fd->subrs,j); |
| 206 |
subrs_len += p->size; |
| 207 |
} |
| 208 |
if ( fd->leniv > 0 ) |
| 209 |
subrs_len += (fd->subrcnt * fd->leniv); |
| 210 |
} |
| 211 |
glyphscnt = utarray_len(t1_glyphs); |
| 212 |
for ( j=0; j<glyphscnt; j++ ) { |
| 213 |
p = (cs_type1subr *)utarray_eltptr(t1_glyphs,j); |
| 214 |
chrs_len += p->size; |
| 215 |
} |
| 216 |
if ( fd->leniv > 0 ) |
| 217 |
chrs_len += (glyphscnt * fd->leniv); |
| 218 |
|
| 219 |
cidbytes->fdbytes = ( cidbytes->fdcnt==1 )? 0 : |
| 220 |
( cidbytes->fdcnt<256 )? 1 : 2; |
| 221 |
if ( (cidbytes->cidcnt+1)*(cidbytes->fdbytes+3) + /* size of the CID map region */ |
| 222 |
(subrtot+1) * 3 + /* size of the Subr map region */ |
| 223 |
subrs_len + /* size of the subr region */ |
| 224 |
chrs_len < 0x1000000 ) /* size of the charstring region */ /* Are all our offsets less than 3 bytes? */ |
| 225 |
cidbytes->gdbytes = 3; /* Adobe's convention is to use 3, so don't bother checking for anything less */ |
| 226 |
else |
| 227 |
cidbytes->gdbytes = 4; /* but I suppose we might need more... */ |
| 228 |
|
| 229 |
offset = (cidbytes->cidcnt+1)*(cidbytes->fdbytes+cidbytes->gdbytes) + |
| 230 |
(subrtot+1) * cidbytes->gdbytes + subrs_len; |
| 231 |
binary_len = (cidbytes->cidcnt+1)*(cidbytes->fdbytes+cidbytes->gdbytes) + |
| 232 |
(subrtot+1) * cidbytes->gdbytes + subrs_len + chrs_len; |
| 233 |
binary = NEW(binary_len,card8); |
| 234 |
memset(binary,0,binary_len); |
| 235 |
bin_offset = 0; |
| 236 |
gid = 0; |
| 237 |
for ( i=0; i<cidbytes->cidcnt; ++i ) { |
| 238 |
if (IS_USED_CHAR(used_chars, i)) { |
| 239 |
dump_index(binary,&bin_offset,cidbytes->fdbytes,0); |
| 240 |
dump_index(binary,&bin_offset,cidbytes->gdbytes,offset); |
| 241 |
p = (cs_type1subr *)utarray_eltptr(t1_glyphs,gid); |
| 242 |
offset += p->size; |
| 243 |
if ( fd->leniv > 0 ) |
| 244 |
offset += fd->leniv; |
| 245 |
gid++; |
| 246 |
} |
| 247 |
else { |
| 248 |
dump_index(binary,&bin_offset,cidbytes->fdbytes,0); |
| 249 |
dump_index(binary,&bin_offset,cidbytes->gdbytes,offset); |
| 250 |
} |
| 251 |
} |
| 252 |
dump_index(binary,&bin_offset,cidbytes->fdbytes,-1); /* Adobe says undefined */ |
| 253 |
dump_index(binary,&bin_offset,cidbytes->gdbytes,offset); |
| 254 |
|
| 255 |
offset = (cidbytes->cidcnt+1)*(cidbytes->fdbytes+cidbytes->gdbytes) + |
| 256 |
(subrtot+1) * cidbytes->gdbytes; |
| 257 |
for ( i=0; i<cidbytes->fdcnt; ++i ) { |
| 258 |
fd = &cidbytes->fds[i]; |
| 259 |
fd->subrmapoff = bin_offset; |
| 260 |
fd->sdbytes = cidbytes->gdbytes; |
| 261 |
fd->subrcnt = utarray_len(fd->subrs); |
| 262 |
for ( j=0; j<fd->subrcnt; ++j ) { |
| 263 |
dump_index(binary,&bin_offset,fd->sdbytes,offset); |
| 264 |
p = (cs_type1subr *)utarray_eltptr(fd->subrs,j); |
| 265 |
offset += p->size; |
| 266 |
if ( fd->leniv > 0 ) |
| 267 |
offset += fd->leniv; |
| 268 |
} |
| 269 |
} |
| 270 |
dump_index(binary,&bin_offset,cidbytes->gdbytes,offset); |
| 271 |
if ( fd->leniv > 0 ) |
| 272 |
offset += fd->leniv; |
| 273 |
|
| 274 |
for ( i=0; i<cidbytes->fdcnt; ++i ) { |
| 275 |
fd = &cidbytes->fds[i]; |
| 276 |
for ( j=0; j<fd->subrcnt; ++j ) { |
| 277 |
p = (cs_type1subr *)utarray_eltptr(fd->subrs,j); |
| 278 |
dumpt1str(binary,&bin_offset,p->data,p->size,fd->leniv); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
for ( j=0; j<glyphscnt; ++j ) { |
| 283 |
p = (cs_type1subr *)utarray_eltptr(t1_glyphs,j); |
| 284 |
dumpt1str(binary,&bin_offset,p->data,p->size,leniv); |
| 285 |
} |
| 286 |
*bindata = binary; |
| 287 |
|
| 288 |
return( binary_len ); |
| 289 |
} |
| 290 |
|
| 291 |
long |
| 292 |
CIDFont_type0_dofont (const char *PSName, cff_font *cffont, UsedMapElem *used_glyphs, |
| 293 |
struct cidbytes *cidbytes, card8 **bindata) |
| 294 |
{ |
| 295 |
cff_index *idx; |
| 296 |
cff_charsets *charset = NULL; |
| 297 |
cff_fdselect *fdselect = NULL; |
| 298 |
long charstring_len, max_len; |
| 299 |
long size, offset = 0; |
| 300 |
card8 *data; |
| 301 |
card16 num_glyphs = 0, gid; |
| 302 |
long cid; |
| 303 |
card16 cs_count, last_cid = 0; |
| 304 |
int fd, prev_fd; |
| 305 |
unsigned char *CIDToGIDMap = NULL; |
| 306 |
long cid_count; |
| 307 |
UT_array *t1_charstrings, *t1_subrs; |
| 308 |
cs_type1subr *p; |
| 309 |
double default_width, nominal_width; |
| 310 |
|
| 311 |
ASSERT(cffont); |
| 312 |
|
| 313 |
cff_read_charsets(cffont); |
| 314 |
|
| 315 |
if (cff_dict_known(cffont->topdict, "CIDCount")) { |
| 316 |
cid_count = (long) cff_dict_get(cffont->topdict, "CIDCount", 0); |
| 317 |
} else { |
| 318 |
cid_count = CID_MAX + 1; |
| 319 |
} |
| 320 |
|
| 321 |
CIDToGIDMap = NEW(2 * cid_count, unsigned char); |
| 322 |
memset(CIDToGIDMap, 0, 2 * cid_count); |
| 323 |
ADD_TO_USED_CHARS(used_glyphs, 0); /* .notdef */ |
| 324 |
for (cid = 0; cid <= CID_MAX; cid++) { |
| 325 |
if (IS_USED_CHAR(used_glyphs, cid)) { |
| 326 |
gid = cff_charsets_lookup(cffont, (card16)cid); |
| 327 |
if (cid != 0 && gid == 0) { |
| 328 |
// WARN("Glyph for CID %u missing in font \"%s\".", (CID) cid, PSName); |
| 329 |
REMOVE_FROM_USED_CHARS(used_glyphs, cid); |
| 330 |
continue; |
| 331 |
} |
| 332 |
CIDToGIDMap[2*cid] = (gid >> 8) & 0xff; |
| 333 |
CIDToGIDMap[2*cid+1] = gid & 0xff; |
| 334 |
last_cid = cid; |
| 335 |
num_glyphs++; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1); |
| 340 |
|
| 341 |
/* |
| 342 |
* Embed font subset. |
| 343 |
*/ |
| 344 |
cff_read_fdselect(cffont); |
| 345 |
cff_read_fdarray(cffont); |
| 346 |
cff_read_private(cffont); |
| 347 |
|
| 348 |
cff_read_subrs(cffont); |
| 349 |
|
| 350 |
offset = (long) cff_dict_get(cffont->topdict, "CharStrings", 0); |
| 351 |
cff_seek_set(cffont, offset); |
| 352 |
idx = cff_get_index_header(cffont); |
| 353 |
/* offset is now absolute offset ... bad */ |
| 354 |
offset = cff_tell(cffont); |
| 355 |
|
| 356 |
if ((cs_count = idx->count) < 2) { |
| 357 |
ERROR("No valid charstring data found."); |
| 358 |
} |
| 359 |
|
| 360 |
/* New Charsets data */ |
| 361 |
charset = NEW(1, cff_charsets); |
| 362 |
charset->format = 0; |
| 363 |
charset->num_entries = 0; |
| 364 |
charset->data.glyphs = NEW(num_glyphs, s_SID); |
| 365 |
|
| 366 |
/* New FDSelect data */ |
| 367 |
fdselect = NEW(1, cff_fdselect); |
| 368 |
fdselect->format = 3; |
| 369 |
fdselect->num_entries = 0; |
| 370 |
fdselect->data.ranges = NEW(num_glyphs, cff_range3); |
| 371 |
|
| 372 |
/* Convert Charstrings to Type1 */ |
| 373 |
utarray_new(t1_charstrings,&subr_icd); |
| 374 |
utarray_new(t1_subrs,&subr_icd); |
| 375 |
initsubrs(t1_subrs); |
| 376 |
cs_start_conversion(5, true, t1_charstrings, t1_subrs); |
| 377 |
|
| 378 |
charstring_len = 0; |
| 379 |
|
| 380 |
prev_fd = -1; gid = 0; |
| 381 |
data = NEW(CS_STR_LEN_MAX, card8); |
| 382 |
for (cid = 0; cid <= last_cid; cid++) { |
| 383 |
unsigned short gid_org; |
| 384 |
|
| 385 |
if (!IS_USED_CHAR(used_glyphs, cid)) |
| 386 |
continue; |
| 387 |
|
| 388 |
gid_org = (CIDToGIDMap[2*cid] << 8)|(CIDToGIDMap[2*cid+1]); |
| 389 |
if ((size = (idx->offset)[gid_org+1] - (idx->offset)[gid_org]) |
| 390 |
> CS_STR_LEN_MAX) |
| 391 |
ERROR("Charstring too long: gid=%u", gid_org); |
| 392 |
cff_seek(cffont, offset + (idx->offset)[gid_org] - 1); |
| 393 |
cff_read_data(data, size, cffont); |
| 394 |
fd = cff_fdselect_lookup(cffont, gid_org); |
| 395 |
|
| 396 |
if (cffont->privat[fd] && cff_dict_known(cffont->privat[fd], "defaultWidthX")) { |
| 397 |
default_width = (double) cff_dict_get(cffont->privat[fd], "defaultWidthX", 0); |
| 398 |
} else { |
| 399 |
default_width = CFF_DEFAULTWIDTHX_DEFAULT; |
| 400 |
} |
| 401 |
if (cffont->privat[fd] && cff_dict_known(cffont->privat[fd], "nominalWidthX")) { |
| 402 |
nominal_width = (double) cff_dict_get(cffont->privat[fd], "nominalWidthX", 0); |
| 403 |
} else { |
| 404 |
nominal_width = CFF_NOMINALWIDTHX_DEFAULT; |
| 405 |
} |
| 406 |
|
| 407 |
charstring_len += cs_convert_charstring(data, size, cffont->gsubr, (cffont->subrs)[fd], default_width, nominal_width); |
| 408 |
|
| 409 |
if (cid > 0 && gid_org > 0) { |
| 410 |
charset->data.glyphs[charset->num_entries] = cid; |
| 411 |
charset->num_entries += 1; |
| 412 |
} |
| 413 |
if (fd != prev_fd) { |
| 414 |
fdselect->data.ranges[fdselect->num_entries].first = gid; |
| 415 |
fdselect->data.ranges[fdselect->num_entries].fd = fd; |
| 416 |
fdselect->num_entries += 1; |
| 417 |
prev_fd = fd; |
| 418 |
} |
| 419 |
gid++; |
| 420 |
} |
| 421 |
|
| 422 |
cs_end_conversion(); |
| 423 |
|
| 424 |
if (gid != num_glyphs) |
| 425 |
ERROR("Unexpeced error: ?????"); |
| 426 |
RELEASE(data); |
| 427 |
cff_release_index(idx); |
| 428 |
|
| 429 |
RELEASE(CIDToGIDMap); |
| 430 |
|
| 431 |
/* discard old one, set new data */ |
| 432 |
cff_release_charsets(cffont->charsets); |
| 433 |
cffont->charsets = charset; |
| 434 |
cff_release_fdselect(cffont->fdselect); |
| 435 |
cffont->fdselect = fdselect; |
| 436 |
|
| 437 |
/* no Global subr */ |
| 438 |
if (cffont->gsubr) |
| 439 |
cff_release_index(cffont->gsubr); |
| 440 |
cffont->gsubr = cff_new_index(0); |
| 441 |
|
| 442 |
for (fd = 0; fd < cffont->num_fds; fd++) { |
| 443 |
if (cffont->subrs && cffont->subrs[fd]) { |
| 444 |
cff_release_index(cffont->subrs[fd]); |
| 445 |
cffont->subrs[fd] = NULL; |
| 446 |
} |
| 447 |
if (cffont->privat && (cffont->privat)[fd]) { |
| 448 |
cff_dict_remove((cffont->privat)[fd], "Subrs"); /* no Subrs */ |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
max_len = gencidbinarydata(cffont, used_glyphs, t1_charstrings, t1_subrs, cidbytes, bindata); |
| 453 |
|
| 454 |
for(p=(cs_type1subr *)utarray_front(t1_charstrings); p!=NULL; p=(cs_type1subr *)utarray_next(t1_charstrings,p)) { |
| 455 |
if ( p->data ) |
| 456 |
free(p->data); |
| 457 |
} |
| 458 |
utarray_free(t1_charstrings); |
| 459 |
|
| 460 |
for(p=(cs_type1subr *)utarray_front(t1_subrs); p!=NULL; p=(cs_type1subr *)utarray_next(t1_subrs,p)) { |
| 461 |
if ( p->data ) |
| 462 |
free(p->data); |
| 463 |
} |
| 464 |
utarray_free(t1_subrs); |
| 465 |
|
| 466 |
return max_len; |
| 467 |
} |
| 468 |
|
| 469 |
long |
| 470 |
CIDFont_type0_t1cdofont (const char *PSName, cff_font *cffont, UsedMapElem *used_glyphs, |
| 471 |
struct cidbytes *cidbytes, card8 **bindata) |
| 472 |
{ |
| 473 |
cff_index *idx; |
| 474 |
long charstring_len, max_len; |
| 475 |
long size, offset = 0; |
| 476 |
card8 *data; |
| 477 |
card16 num_glyphs, gid, last_cid; |
| 478 |
long i, cid; |
| 479 |
double default_width, nominal_width; |
| 480 |
UT_array *t1_charstrings, *t1_subrs; |
| 481 |
cs_type1subr *p; |
| 482 |
|
| 483 |
ASSERT(cffont); |
| 484 |
|
| 485 |
cff_read_private(cffont); |
| 486 |
cff_read_subrs (cffont); |
| 487 |
|
| 488 |
if (cffont->privat[0] && cff_dict_known(cffont->privat[0], "defaultWidthX")) { |
| 489 |
default_width = (double) cff_dict_get(cffont->privat[0], "defaultWidthX", 0); |
| 490 |
} else { |
| 491 |
default_width = CFF_DEFAULTWIDTHX_DEFAULT; |
| 492 |
} |
| 493 |
if (cffont->privat[0] && cff_dict_known(cffont->privat[0], "nominalWidthX")) { |
| 494 |
nominal_width = (double) cff_dict_get(cffont->privat[0], "nominalWidthX", 0); |
| 495 |
} else { |
| 496 |
nominal_width = CFF_NOMINALWIDTHX_DEFAULT; |
| 497 |
} |
| 498 |
|
| 499 |
num_glyphs = 0; last_cid = 0; |
| 500 |
ADD_TO_USED_CHARS(used_glyphs, 0); /* .notdef */ |
| 501 |
for (i = 0; i < (cffont->num_glyphs + BITS_PER_USED_ELEM - 1) / BITS_PER_USED_ELEM; i++) { |
| 502 |
int c, j; |
| 503 |
|
| 504 |
c = used_glyphs[i]; |
| 505 |
for (j = BITS_PER_USED_ELEM - 1; j >= 0; j--) { |
| 506 |
if (c & (1 << j)) { |
| 507 |
num_glyphs++; |
| 508 |
last_cid = (i + 1) * BITS_PER_USED_ELEM - j - 1; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
{ |
| 514 |
cff_fdselect *fdselect; |
| 515 |
|
| 516 |
fdselect = NEW(1, cff_fdselect); |
| 517 |
fdselect->format = 3; |
| 518 |
fdselect->num_entries = 1; |
| 519 |
fdselect->data.ranges = NEW(1, cff_range3); |
| 520 |
fdselect->data.ranges[0].first = 0; |
| 521 |
fdselect->data.ranges[0].fd = 0; |
| 522 |
cffont->fdselect = fdselect; |
| 523 |
} |
| 524 |
|
| 525 |
{ |
| 526 |
cff_charsets *charset; |
| 527 |
|
| 528 |
charset = NEW(1, cff_charsets); |
| 529 |
charset->format = 0; |
| 530 |
charset->num_entries = num_glyphs-1; |
| 531 |
charset->data.glyphs = NEW(num_glyphs-1, s_SID); |
| 532 |
|
| 533 |
for (gid = 0, cid = 0; cid <= last_cid; cid++) { |
| 534 |
if (IS_USED_CHAR(used_glyphs, cid)) { |
| 535 |
if (gid > 0) |
| 536 |
charset->data.glyphs[gid-1] = cid; |
| 537 |
gid++; |
| 538 |
} |
| 539 |
} |
| 540 |
/* cff_release_charsets(cffont->charsets); */ |
| 541 |
cffont->charsets = charset; |
| 542 |
} |
| 543 |
|
| 544 |
cff_dict_add(cffont->topdict, "CIDCount", 1); |
| 545 |
cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1); |
| 546 |
|
| 547 |
cffont->fdarray = NEW(1, cff_dict *); |
| 548 |
cffont->fdarray[0] = cff_new_dict(); |
| 549 |
cff_dict_add(cffont->fdarray[0], "FontName", 1); |
| 550 |
cff_dict_set(cffont->fdarray[0], "FontName", 0, |
| 551 |
(double) cff_add_string(cffont, PSName, 1)); /* FIXME: Skip XXXXXX+ */ |
| 552 |
cff_dict_add(cffont->fdarray[0], "Private", 2); |
| 553 |
cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0); |
| 554 |
cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0); |
| 555 |
/* FDArray - index offset, not known yet */ |
| 556 |
cff_dict_add(cffont->topdict, "FDArray", 1); |
| 557 |
cff_dict_set(cffont->topdict, "FDArray", 0, 0.0); |
| 558 |
/* FDSelect - offset, not known yet */ |
| 559 |
cff_dict_add(cffont->topdict, "FDSelect", 1); |
| 560 |
cff_dict_set(cffont->topdict, "FDSelect", 0, 0.0); |
| 561 |
|
| 562 |
cff_dict_remove(cffont->topdict, "UniqueID"); |
| 563 |
cff_dict_remove(cffont->topdict, "XUID"); |
| 564 |
cff_dict_remove(cffont->topdict, "Private"); |
| 565 |
cff_dict_remove(cffont->topdict, "Encoding"); |
| 566 |
|
| 567 |
|
| 568 |
/* */ |
| 569 |
offset = (long) cff_dict_get(cffont->topdict, "CharStrings", 0); |
| 570 |
cff_seek_set(cffont, offset); |
| 571 |
idx = cff_get_index_header(cffont); |
| 572 |
/* offset is now absolute offset ... bad */ |
| 573 |
offset = cff_tell(cffont); |
| 574 |
|
| 575 |
if (idx->count < 2) |
| 576 |
ERROR("No valid charstring data found."); |
| 577 |
|
| 578 |
/* Convert Charstrings to Type1 */ |
| 579 |
utarray_new(t1_charstrings,&subr_icd); |
| 580 |
utarray_new(t1_subrs,&subr_icd); |
| 581 |
initsubrs(t1_subrs); |
| 582 |
cs_start_conversion(5, true, t1_charstrings, t1_subrs); |
| 583 |
|
| 584 |
charstring_len = 0; |
| 585 |
|
| 586 |
gid = 0; |
| 587 |
data = NEW(CS_STR_LEN_MAX, card8); |
| 588 |
for (cid = 0; cid <= last_cid; cid++) { |
| 589 |
if (!IS_USED_CHAR(used_glyphs, cid)) |
| 590 |
continue; |
| 591 |
|
| 592 |
if ((size = (idx->offset)[cid+1] - (idx->offset)[cid]) |
| 593 |
> CS_STR_LEN_MAX) |
| 594 |
ERROR("Charstring too long: gid=%u", cid); |
| 595 |
cff_seek(cffont, offset + (idx->offset)[cid] - 1); |
| 596 |
cff_read_data(data, size, cffont); |
| 597 |
|
| 598 |
charstring_len += cs_convert_charstring(data, size, cffont->gsubr, (cffont->subrs)[0], default_width, nominal_width); |
| 599 |
gid++; |
| 600 |
} |
| 601 |
|
| 602 |
cs_end_conversion(); |
| 603 |
|
| 604 |
if (gid != num_glyphs) |
| 605 |
ERROR("Unexpeced error: ?????"); |
| 606 |
RELEASE(data); |
| 607 |
cff_release_index(idx); |
| 608 |
|
| 609 |
/* no Global subr */ |
| 610 |
if (cffont->gsubr) |
| 611 |
cff_release_index(cffont->gsubr); |
| 612 |
cffont->gsubr = cff_new_index(0); |
| 613 |
|
| 614 |
if (cffont->subrs && cffont->subrs[0]) { |
| 615 |
cff_release_index(cffont->subrs[0]); |
| 616 |
cffont->subrs[0] = NULL; |
| 617 |
} |
| 618 |
if (cffont->privat && (cffont->privat)[0]) { |
| 619 |
cff_dict_remove((cffont->privat)[0], "Subrs"); /* no Subrs */ |
| 620 |
} |
| 621 |
|
| 622 |
cff_add_string(cffont, "Adobe", 1); |
| 623 |
cff_add_string(cffont, "Identity", 1); |
| 624 |
|
| 625 |
cff_dict_update(cffont->topdict, cffont); |
| 626 |
cff_dict_update(cffont->privat[0], cffont); |
| 627 |
cff_update_string(cffont); |
| 628 |
|
| 629 |
/* CFF code need to be rewrote... */ |
| 630 |
cff_dict_add(cffont->topdict, "ROS", 3); |
| 631 |
cff_dict_set(cffont->topdict, "ROS", 0, |
| 632 |
(double) cff_get_sid(cffont, "Adobe")); |
| 633 |
cff_dict_set(cffont->topdict, "ROS", 1, |
| 634 |
(double) cff_get_sid(cffont, "Identity")); |
| 635 |
cff_dict_set(cffont->topdict, "ROS", 2, 0.0); |
| 636 |
|
| 637 |
max_len = gencidbinarydata(cffont, used_glyphs, t1_charstrings, t1_subrs, cidbytes, bindata); |
| 638 |
|
| 639 |
for(p=(cs_type1subr *)utarray_front(t1_charstrings); p!=NULL; p=(cs_type1subr *)utarray_next(t1_charstrings,p)) { |
| 640 |
if ( p->data ) |
| 641 |
free(p->data); |
| 642 |
} |
| 643 |
utarray_free(t1_charstrings); |
| 644 |
|
| 645 |
for(p=(cs_type1subr *)utarray_front(t1_subrs); p!=NULL; p=(cs_type1subr *)utarray_next(t1_subrs,p)) { |
| 646 |
if ( p->data ) |
| 647 |
free(p->data); |
| 648 |
} |
| 649 |
utarray_free(t1_subrs); |
| 650 |
|
| 651 |
return max_len; |
| 652 |
} |