| 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 |
* References: |
| 26 |
* |
| 27 |
* PostScript Language Reference Manual, 3rd. ed. (Adobe Systems Inc.) |
| 28 |
* 5.11.4 CMap Dictionaries |
| 29 |
* 5.11.5 FMapType 9 Composite Fonts |
| 30 |
* Building CMap Files for CID-Keyed Fonts, Adobe Technical Note #5099 |
| 31 |
* CID-Keyed Font Technology Overview, Adobe Technical Note #5092 |
| 32 |
* Adobe CMap and CIDFont Files Specification, Adobe Technical Specification #5014 |
| 33 |
* |
| 34 |
* Undefined Character Handling: |
| 35 |
* PLRM 3rd. ed., sec. 5.11.5., "Handling Undefined Characters" |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
# |
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
#include "system.h" |
| 44 |
#include "mem.h" |
| 45 |
#include "error.h" |
| 46 |
#include "dpxutil.h" |
| 47 |
|
| 48 |
#include "cmap_p.h" |
| 49 |
#include "cmap.h" |
| 50 |
|
| 51 |
#include "cmap_write.h" |
| 52 |
|
| 53 |
struct sbuf { |
| 54 |
char *buf; |
| 55 |
char *curptr; |
| 56 |
char *limptr; |
| 57 |
}; |
| 58 |
|
| 59 |
static void cmap_add_stream(FILE *fn, const void *buf, size_t buflen) |
| 60 |
{ |
| 61 |
fwrite(buf, 1, buflen, fn); |
| 62 |
} |
| 63 |
|
| 64 |
static int write_map (mapDef *mtab, int count, |
| 65 |
unsigned char *codestr, int depth, |
| 66 |
int detectranges, struct sbuf *wbuf, FILE *stream); |
| 67 |
|
| 68 |
static int |
| 69 |
block_count (mapDef *mtab, int c) |
| 70 |
{ |
| 71 |
int count = 0, n; |
| 72 |
|
| 73 |
n = mtab[c].len - 1; |
| 74 |
c += 1; |
| 75 |
for (; c < 256; c++) { |
| 76 |
if (LOOKUP_CONTINUE(mtab[c].flag) || |
| 77 |
!MAP_DEFINED(mtab[c].flag) || |
| 78 |
(MAP_TYPE(mtab[c].flag) != MAP_IS_CID && |
| 79 |
MAP_TYPE(mtab[c].flag) != MAP_IS_CODE) || |
| 80 |
mtab[c-1].len != mtab[c].len) |
| 81 |
break; |
| 82 |
else if (!memcmp(mtab[c-1].code, mtab[c].code, n) && |
| 83 |
mtab[c-1].code[n] < 255 && |
| 84 |
mtab[c-1].code[n] + 1 == mtab[c].code[n]) |
| 85 |
count++; |
| 86 |
else { |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return count; |
| 92 |
} |
| 93 |
|
| 94 |
static int |
| 95 |
write_map (mapDef *mtab, int count, |
| 96 |
unsigned char *codestr, int depth, int detectranges, |
| 97 |
struct sbuf *wbuf, FILE *stream) |
| 98 |
{ |
| 99 |
int c, i, block_length; |
| 100 |
mapDef *mtab1; |
| 101 |
/* Must be greater than 1 */ |
| 102 |
#define BLOCK_LEN_MIN 2 |
| 103 |
struct { |
| 104 |
int start, count; |
| 105 |
} blocks[256/BLOCK_LEN_MIN+1]; |
| 106 |
int num_blocks = 0; |
| 107 |
|
| 108 |
for (c = 0; c < 256; c++) { |
| 109 |
codestr[depth] = (unsigned char) (c & 0xff); |
| 110 |
if (LOOKUP_CONTINUE(mtab[c].flag)) { |
| 111 |
mtab1 = mtab[c].next; |
| 112 |
count = write_map(mtab1, count, |
| 113 |
codestr, depth + 1, detectranges, wbuf, stream); |
| 114 |
} else { |
| 115 |
if (MAP_DEFINED(mtab[c].flag)) { |
| 116 |
switch (MAP_TYPE(mtab[c].flag)) { |
| 117 |
case MAP_IS_CID: case MAP_IS_CODE: |
| 118 |
if (detectranges) { |
| 119 |
block_length = block_count(mtab, c); |
| 120 |
if (block_length >= BLOCK_LEN_MIN) { |
| 121 |
blocks[num_blocks].start = c; |
| 122 |
blocks[num_blocks].count = block_length; |
| 123 |
num_blocks++; |
| 124 |
c += block_length; |
| 125 |
} else { |
| 126 |
*(wbuf->curptr)++ = '<'; |
| 127 |
for (i = 0; i <= depth; i++) |
| 128 |
sputx(codestr[i], &(wbuf->curptr), wbuf->limptr); |
| 129 |
*(wbuf->curptr)++ = '>'; |
| 130 |
*(wbuf->curptr)++ = ' '; |
| 131 |
*(wbuf->curptr)++ = '<'; |
| 132 |
for (i = 0; i < mtab[c].len; i++) |
| 133 |
sputx(mtab[c].code[i], &(wbuf->curptr), wbuf->limptr); |
| 134 |
*(wbuf->curptr)++ = '>'; |
| 135 |
*(wbuf->curptr)++ = '\n'; |
| 136 |
count++; |
| 137 |
} |
| 138 |
} else { |
| 139 |
*(wbuf->curptr)++ = '<'; |
| 140 |
for (i = 0; i <= depth; i++) |
| 141 |
sputx(codestr[i], &(wbuf->curptr), wbuf->limptr); |
| 142 |
*(wbuf->curptr)++ = '>'; |
| 143 |
*(wbuf->curptr)++ = ' '; |
| 144 |
*(wbuf->curptr)++ = '<'; |
| 145 |
for (i = 0; i < mtab[c].len; i++) |
| 146 |
sputx(mtab[c].code[i], &(wbuf->curptr), wbuf->limptr); |
| 147 |
*(wbuf->curptr)++ = '>'; |
| 148 |
*(wbuf->curptr)++ = '\n'; |
| 149 |
count++; |
| 150 |
} |
| 151 |
break; |
| 152 |
case MAP_IS_NAME: |
| 153 |
ERROR("%s: Unexpected error...", CMAP_DEBUG_STR); |
| 154 |
break; |
| 155 |
case MAP_IS_NOTDEF: |
| 156 |
break; |
| 157 |
default: |
| 158 |
ERROR("%s: Unknown mapping type: %d", |
| 159 |
CMAP_DEBUG_STR, MAP_TYPE(mtab[c].flag)); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* Flush if necessary */ |
| 165 |
if (count >= 100 || wbuf->curptr >= wbuf->limptr ) { |
| 166 |
char fmt_buf[32]; |
| 167 |
if (count > 100) |
| 168 |
ERROR("Unexpected error....: %d", count); |
| 169 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 170 |
cmap_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 171 |
cmap_add_stream(stream, wbuf->buf, (long) (wbuf->curptr - wbuf->buf)); |
| 172 |
wbuf->curptr = wbuf->buf; |
| 173 |
cmap_add_stream(stream, "endbfchar\n", strlen("endbfchar\n")); |
| 174 |
count = 0; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if (num_blocks > 0) { |
| 179 |
char fmt_buf[32]; |
| 180 |
|
| 181 |
if (count > 0) { |
| 182 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 183 |
cmap_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 184 |
cmap_add_stream(stream, wbuf->buf, (long) (wbuf->curptr - wbuf->buf)); |
| 185 |
wbuf->curptr = wbuf->buf; |
| 186 |
cmap_add_stream(stream, "endbfchar\n", strlen("endbfchar\n")); |
| 187 |
count = 0; |
| 188 |
} |
| 189 |
sprintf(fmt_buf, "%d beginbfrange\n", num_blocks); |
| 190 |
cmap_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 191 |
for (i = 0; i < num_blocks; i++) { |
| 192 |
int j; |
| 193 |
|
| 194 |
c = blocks[i].start; |
| 195 |
*(wbuf->curptr)++ = '<'; |
| 196 |
for (j = 0; j < depth; j++) |
| 197 |
sputx(codestr[j], &(wbuf->curptr), wbuf->limptr); |
| 198 |
sputx((unsigned char)c, &(wbuf->curptr), wbuf->limptr); |
| 199 |
*(wbuf->curptr)++ = '>'; |
| 200 |
*(wbuf->curptr)++ = ' '; |
| 201 |
*(wbuf->curptr)++ = '<'; |
| 202 |
for (j = 0; j < depth; j++) |
| 203 |
sputx(codestr[j], &(wbuf->curptr), wbuf->limptr); |
| 204 |
sputx((unsigned char)(c + blocks[i].count), &(wbuf->curptr), wbuf->limptr); |
| 205 |
*(wbuf->curptr)++ = '>'; |
| 206 |
*(wbuf->curptr)++ = ' '; |
| 207 |
*(wbuf->curptr)++ = '<'; |
| 208 |
for (j = 0; j < mtab[c].len; j++) |
| 209 |
sputx(mtab[c].code[j], &(wbuf->curptr), wbuf->limptr); |
| 210 |
*(wbuf->curptr)++ = '>'; |
| 211 |
*(wbuf->curptr)++ = '\n'; |
| 212 |
} |
| 213 |
cmap_add_stream(stream, wbuf->buf, (long) (wbuf->curptr - wbuf->buf)); |
| 214 |
wbuf->curptr = wbuf->buf; |
| 215 |
cmap_add_stream(stream, "endbfrange\n", strlen("endbfrange\n")); |
| 216 |
} |
| 217 |
|
| 218 |
return count; |
| 219 |
} |
| 220 |
|
| 221 |
#define CMAP_BEGIN "\ |
| 222 |
/CIDInit /ProcSet findresource begin\n\ |
| 223 |
12 dict begin\n\ |
| 224 |
begincmap\n\ |
| 225 |
" |
| 226 |
|
| 227 |
#define CMAP_END "\ |
| 228 |
endcmap\n\ |
| 229 |
CMapName currentdict /CMap defineresource pop\n\ |
| 230 |
end\n\ |
| 231 |
end\n\ |
| 232 |
" |
| 233 |
|
| 234 |
int |
| 235 |
CMap_create_file(const char *map_path, const char *map_name, const char *map_ext, CMap *cmap, int detectranges) |
| 236 |
{ |
| 237 |
FILE *stream; |
| 238 |
char *map_file; |
| 239 |
CIDSysInfo *csi; |
| 240 |
struct sbuf wbuf; |
| 241 |
struct rangeDef *ranges; |
| 242 |
unsigned char *codestr; |
| 243 |
int i, j, count = 0; |
| 244 |
|
| 245 |
if (!cmap || !CMap_is_valid(cmap)) { |
| 246 |
WARN("Invalid CMap"); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
if (cmap->type == CMAP_TYPE_IDENTITY) |
| 251 |
return 0; |
| 252 |
|
| 253 |
csi = CMap_get_CIDSysInfo(cmap); |
| 254 |
if (!csi) { |
| 255 |
csi = (cmap->type != CMAP_TYPE_TO_UNICODE) ? |
| 256 |
&CSI_IDENTITY : &CSI_UNICODE; |
| 257 |
} |
| 258 |
|
| 259 |
map_file = NEW(strlen(map_path) + strlen(map_name) + strlen(map_ext) + 5, char); |
| 260 |
strcpy(map_file,map_path); |
| 261 |
strcat(map_file,"/"); |
| 262 |
strcat(map_file,map_name); |
| 263 |
if (strlen(map_ext) > 0) { |
| 264 |
strcat(map_file, "."); |
| 265 |
strcat(map_file, map_ext); |
| 266 |
} |
| 267 |
stream = fopen(map_file,"w"); |
| 268 |
RELEASE(map_file); |
| 269 |
if ( ! stream ) |
| 270 |
ERROR("Error creating ToUnicode cmap file %s", map_name); |
| 271 |
|
| 272 |
#define WBUF_SIZE 4096 |
| 273 |
wbuf.buf = NEW(WBUF_SIZE, char); |
| 274 |
codestr = NEW(cmap->profile.maxBytesIn, unsigned char); |
| 275 |
memset(codestr, 0, cmap->profile.maxBytesIn); |
| 276 |
|
| 277 |
wbuf.curptr = wbuf.buf; |
| 278 |
wbuf.limptr = wbuf.buf + WBUF_SIZE - |
| 279 |
2 * (cmap->profile.maxBytesIn + cmap->profile.maxBytesOut) + 16; |
| 280 |
|
| 281 |
/* Start CMap */ |
| 282 |
cmap_add_stream(stream, (const void *) CMAP_BEGIN, strlen(CMAP_BEGIN)); |
| 283 |
|
| 284 |
wbuf.curptr += sprintf(wbuf.curptr, "/CMapName /%s def\n", cmap->name); |
| 285 |
wbuf.curptr += sprintf(wbuf.curptr, "/CMapType %d def\n" , cmap->type); |
| 286 |
if (cmap->wmode != 0 && |
| 287 |
cmap->type != CMAP_TYPE_TO_UNICODE) |
| 288 |
wbuf.curptr += sprintf(wbuf.curptr, "/WMode %d def\n", cmap->wmode); |
| 289 |
|
| 290 |
#define CMAP_CSI_FMT "/CIDSystemInfo <<\n\ |
| 291 |
/Registry (%s)\n\ |
| 292 |
/Ordering (%s)\n\ |
| 293 |
/Supplement %d\n\ |
| 294 |
>> def\n" |
| 295 |
wbuf.curptr += sprintf(wbuf.curptr, CMAP_CSI_FMT, |
| 296 |
csi->registry, csi->ordering, csi->supplement); |
| 297 |
cmap_add_stream(stream, wbuf.buf, (long)(wbuf.curptr - wbuf.buf)); |
| 298 |
wbuf.curptr = wbuf.buf; |
| 299 |
|
| 300 |
/* codespacerange */ |
| 301 |
ranges = cmap->codespace.ranges; |
| 302 |
wbuf.curptr += sprintf(wbuf.curptr, |
| 303 |
"%d begincodespacerange\n", cmap->codespace.num); |
| 304 |
for (i = 0; i < cmap->codespace.num; i++) { |
| 305 |
*(wbuf.curptr)++ = '<'; |
| 306 |
for (j = 0; j < ranges[i].dim; j++) { |
| 307 |
sputx(ranges[i].codeLo[j], &(wbuf.curptr), wbuf.limptr); |
| 308 |
} |
| 309 |
*(wbuf.curptr)++ = '>'; |
| 310 |
*(wbuf.curptr)++ = ' '; |
| 311 |
*(wbuf.curptr)++ = '<'; |
| 312 |
for (j = 0; j < ranges[i].dim; j++) { |
| 313 |
sputx(ranges[i].codeHi[j], &(wbuf.curptr), wbuf.limptr); |
| 314 |
} |
| 315 |
*(wbuf.curptr)++ = '>'; |
| 316 |
*(wbuf.curptr)++ = '\n'; |
| 317 |
} |
| 318 |
cmap_add_stream(stream, wbuf.buf, (long)(wbuf.curptr - wbuf.buf)); |
| 319 |
wbuf.curptr = wbuf.buf; |
| 320 |
cmap_add_stream(stream, |
| 321 |
"endcodespacerange\n", strlen("endcodespacerange\n")); |
| 322 |
|
| 323 |
/* CMap body */ |
| 324 |
if (cmap->mapTbl) { |
| 325 |
count = write_map(cmap->mapTbl, |
| 326 |
0, codestr, 0, detectranges, &wbuf, stream); /* Top node */ |
| 327 |
if (count > 0) { /* Flush */ |
| 328 |
char fmt_buf[32]; |
| 329 |
if (count > 100) |
| 330 |
ERROR("Unexpected error....: %d", count); |
| 331 |
sprintf(fmt_buf, "%d beginbfchar\n", count); |
| 332 |
cmap_add_stream(stream, fmt_buf, strlen(fmt_buf)); |
| 333 |
cmap_add_stream(stream, |
| 334 |
wbuf.buf, (long) (wbuf.curptr - wbuf.buf)); |
| 335 |
cmap_add_stream(stream, |
| 336 |
"endbfchar\n", strlen("endbfchar\n")); |
| 337 |
count = 0; |
| 338 |
wbuf.curptr = wbuf.buf; |
| 339 |
} |
| 340 |
} |
| 341 |
/* End CMap */ |
| 342 |
cmap_add_stream(stream, CMAP_END, strlen(CMAP_END)); |
| 343 |
|
| 344 |
RELEASE(codestr); |
| 345 |
RELEASE(wbuf.buf); |
| 346 |
|
| 347 |
return 1; |
| 348 |
} |