| 1 |
/* |
| 2 |
Copyright 1996-2014 Han The Thanh, <[email protected]> |
| 3 |
|
| 4 |
This file is part of pdfTeX. |
| 5 |
|
| 6 |
pdfTeX is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
pdfTeX is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License along |
| 17 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
|
| 22 |
/**********************************************************************/ |
| 23 |
/* All encoding entries go into AVL tree for fast search by name. */ |
| 24 |
|
| 25 |
struct avl_table *fe_tree = NULL; |
| 26 |
|
| 27 |
/* AVL sort fe_entry into fe_tree by name */ |
| 28 |
|
| 29 |
static int comp_fe_entry(const void *pa, const void *pb, void *p) |
| 30 |
{ |
| 31 |
return strcmp(((const fe_entry *) pa)->name, ((const fe_entry *) pb)->name); |
| 32 |
} |
| 33 |
|
| 34 |
static fe_entry *new_fe_entry(void) |
| 35 |
{ |
| 36 |
fe_entry *fe; |
| 37 |
fe = xtalloc(1, fe_entry); |
| 38 |
fe->name = NULL; |
| 39 |
fe->fe_objnum = 0; |
| 40 |
fe->glyph_names = NULL; /* encoding file not yet read in */ |
| 41 |
fe->tx_tree = NULL; |
| 42 |
return fe; |
| 43 |
} |
| 44 |
|
| 45 |
static fe_entry *lookup_fe_entry(char *s) |
| 46 |
{ |
| 47 |
fe_entry fe; |
| 48 |
assert(s != NULL); |
| 49 |
fe.name = s; |
| 50 |
if (fe_tree == NULL) { |
| 51 |
fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator); |
| 52 |
assert(fe_tree != NULL); |
| 53 |
} |
| 54 |
return (fe_entry *) avl_find(fe_tree, &fe); |
| 55 |
} |
| 56 |
|
| 57 |
static void register_fe_entry(fe_entry * fe) |
| 58 |
{ |
| 59 |
void **aa; |
| 60 |
if (fe_tree == NULL) { |
| 61 |
fe_tree = avl_create(comp_fe_entry, NULL, &avl_xallocator); |
| 62 |
assert(fe_tree != NULL); |
| 63 |
} |
| 64 |
assert(fe != NULL); |
| 65 |
assert(fe->name != NULL); |
| 66 |
assert(lookup_fe_entry(fe->name) == NULL); /* encoding not yet registered */ |
| 67 |
aa = avl_probe(fe_tree, fe); |
| 68 |
assert(aa != NULL); |
| 69 |
} |
| 70 |
|
| 71 |
fe_entry *get_fe_entry(char *s) |
| 72 |
{ |
| 73 |
fe_entry *fe; |
| 74 |
char **gl; |
| 75 |
if ((fe = lookup_fe_entry(s)) == NULL && (gl = load_enc_file(s)) != NULL) { |
| 76 |
fe = new_fe_entry(); |
| 77 |
fe->name = s; |
| 78 |
fe->glyph_names = gl; |
| 79 |
register_fe_entry(fe); |
| 80 |
} |
| 81 |
return fe; |
| 82 |
} |
| 83 |
|
| 84 |
/**********************************************************************/ |
| 85 |
|
| 86 |
void epdf_write_enc(char **glyph_names, int fe_objnum) |
| 87 |
{ |
| 88 |
int i, i_old; |
| 89 |
assert(glyph_names != NULL); |
| 90 |
assert(fe_objnum != 0); |
| 91 |
pdfbegindict(fe_objnum, 1); |
| 92 |
pdf_puts("/Type /Encoding\n"); |
| 93 |
pdf_puts("/Differences ["); |
| 94 |
for (i = 0, i_old = -2; i < 256; i++) |
| 95 |
if (glyph_names[i] != notdef) { |
| 96 |
if (i == i_old + 1) /* no gap */ |
| 97 |
pdf_printf("/%s", glyph_names[i]); |
| 98 |
else { |
| 99 |
if (i_old == -2) |
| 100 |
pdf_printf("%i/%s", i, glyph_names[i]); |
| 101 |
else |
| 102 |
pdf_printf(" %i/%s", i, glyph_names[i]); |
| 103 |
} |
| 104 |
i_old = i; |
| 105 |
} |
| 106 |
pdf_puts("]\n"); |
| 107 |
pdfenddict(); |
| 108 |
} |
| 109 |
|
| 110 |
static void write_enc(char **glyph_names, struct avl_table *tx_tree, int fe_objnum) |
| 111 |
{ |
| 112 |
int i_old, *p; |
| 113 |
struct avl_traverser t; |
| 114 |
assert(glyph_names != NULL); |
| 115 |
assert(tx_tree != NULL); |
| 116 |
assert(fe_objnum != 0); |
| 117 |
pdfbegindict(fe_objnum, 1); |
| 118 |
pdf_puts("/Type /Encoding\n"); |
| 119 |
pdf_puts("/Differences ["); |
| 120 |
avl_t_init(&t, tx_tree); |
| 121 |
for (i_old = -2, p = (int *) avl_t_first(&t, tx_tree); p != NULL; |
| 122 |
p = (int *) avl_t_next(&t)) { |
| 123 |
if (*p == i_old + 1) /* no gap */ |
| 124 |
pdf_printf("/%s", glyph_names[*p]); |
| 125 |
else { |
| 126 |
if (i_old == -2) |
| 127 |
pdf_printf("%i/%s", *p, glyph_names[*p]); |
| 128 |
else |
| 129 |
pdf_printf(" %i/%s", *p, glyph_names[*p]); |
| 130 |
} |
| 131 |
i_old = *p; |
| 132 |
} |
| 133 |
pdf_puts("]\n"); |
| 134 |
pdfenddict(); |
| 135 |
} |
| 136 |
|
| 137 |
static void write_fontencoding(fe_entry * fe) |
| 138 |
{ |
| 139 |
assert(fe != NULL); |
| 140 |
write_enc(fe->glyph_names, fe->tx_tree, fe->fe_objnum); |
| 141 |
} |
| 142 |
|
| 143 |
void write_fontencodings(void) |
| 144 |
{ |
| 145 |
fe_entry *fe; |
| 146 |
struct avl_traverser t; |
| 147 |
if (fe_tree == NULL) |
| 148 |
return; |
| 149 |
avl_t_init(&t, fe_tree); |
| 150 |
for (fe = (fe_entry *) avl_t_first(&t, fe_tree); fe != NULL; |
| 151 |
fe = (fe_entry *) avl_t_next(&t)) |
| 152 |
if (fe->fe_objnum != 0) |
| 153 |
write_fontencoding(fe); |
| 154 |
} |
| 155 |
|
| 156 |
/**********************************************************************/ |
| 157 |
/* cleaning up... */ |
| 158 |
|
| 159 |
static void destroy_fe_entry(void *pa, void *pb) |
| 160 |
{ |
| 161 |
fe_entry *p; |
| 162 |
int i; |
| 163 |
p = (fe_entry *) pa; |
| 164 |
xfree(p->name); |
| 165 |
if (p->glyph_names != NULL) |
| 166 |
for (i = 0; i < 256; i++) |
| 167 |
if (p->glyph_names[i] != notdef) |
| 168 |
xfree(p->glyph_names[i]); |
| 169 |
xfree(p->glyph_names); |
| 170 |
xfree(p); |
| 171 |
} |
| 172 |
|
| 173 |
void enc_free(void) |
| 174 |
{ |
| 175 |
if (fe_tree != NULL) |
| 176 |
avl_destroy(fe_tree, destroy_fe_entry); |
| 177 |
fe_tree = NULL; |
| 178 |
} |
| 179 |
|
| 180 |
/**********************************************************************/ |