| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2010 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
|
| 24 |
/* |
| 25 |
With these (new) late nodes we operate on write_tokens which happens to be |
| 26 |
equivalent to pdf_literal_data so it is somewhat confusing but cleaning this |
| 27 |
up now is asking for troubles. |
| 28 |
*/ |
| 29 |
|
| 30 |
void pdf_special(PDF pdf, halfword p) |
| 31 |
{ |
| 32 |
int old_setting = selector; |
| 33 |
str_number s; |
| 34 |
halfword h; |
| 35 |
if (subtype(p) == late_special_node) { |
| 36 |
expand_macros_in_tokenlist(pdf_literal_data(p)); |
| 37 |
h = token_link(def_ref); |
| 38 |
} else { |
| 39 |
h = token_link(pdf_literal_data(p)); |
| 40 |
} |
| 41 |
selector = new_string; |
| 42 |
show_token_list(h, null, -1); |
| 43 |
selector = old_setting; |
| 44 |
s = make_string(); |
| 45 |
pdf_literal(pdf, s, scan_special, true); |
| 46 |
flush_str(s); |
| 47 |
if (subtype(p) == late_special_node) { |
| 48 |
flush_list(def_ref); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/*tex |
| 53 |
|
| 54 |
To ship out a \TeX\ box to PDF page description we need to implement |
| 55 |
|hlist_out|, |vlist_out| and |ship_out|, which are equivalent to the \TeX' |
| 56 |
original |hlist_out|, |vlist_out| and |ship_out| resp. But first we need to |
| 57 |
declare some procedures needed in |hlist_out| and |vlist_out|. |
| 58 |
|
| 59 |
*/ |
| 60 |
|
| 61 |
void pdf_out_literal(PDF pdf, halfword p) |
| 62 |
{ |
| 63 |
int old_setting; |
| 64 |
str_number s; |
| 65 |
int t = pdf_literal_type(p); |
| 66 |
halfword h; |
| 67 |
pdfstructure *ps = pdf->pstruct; |
| 68 |
if (t == normal) { |
| 69 |
old_setting = selector; |
| 70 |
if (subtype(p) == pdf_late_literal_node) { |
| 71 |
expand_macros_in_tokenlist(pdf_literal_data(p)); |
| 72 |
h = token_link(def_ref); |
| 73 |
} else { |
| 74 |
h = token_link(pdf_literal_data(p)); |
| 75 |
} |
| 76 |
selector = new_string; |
| 77 |
show_token_list(h, null, -1); |
| 78 |
selector = old_setting; |
| 79 |
s = make_string(); |
| 80 |
pdf_literal(pdf, s, pdf_literal_mode(p), false); |
| 81 |
flush_str(s); |
| 82 |
if (subtype(p) == pdf_late_literal_node) { |
| 83 |
flush_list(def_ref); |
| 84 |
} |
| 85 |
} else if (t == lua_refid_literal) { |
| 86 |
switch (pdf_literal_mode(p)) { |
| 87 |
case set_origin: |
| 88 |
pdf_goto_pagemode(pdf); |
| 89 |
pdf_set_pos(pdf, pdf->posstruct->pos); |
| 90 |
break; |
| 91 |
case direct_page: |
| 92 |
pdf_goto_pagemode(pdf); |
| 93 |
break; |
| 94 |
case direct_text: |
| 95 |
pdf_goto_textmode(pdf); |
| 96 |
break; |
| 97 |
case direct_font: |
| 98 |
pdf_goto_fontmode(pdf); |
| 99 |
break; |
| 100 |
case direct_always: |
| 101 |
pdf_end_string_nl(pdf); |
| 102 |
ps->need_tm = true; |
| 103 |
break; |
| 104 |
case direct_raw: |
| 105 |
pdf_end_string_nl(pdf); |
| 106 |
break; |
| 107 |
default: |
| 108 |
normal_error("pdf backend","bad literal mode"); |
| 109 |
break; |
| 110 |
} |
| 111 |
lua_pdf_literal(pdf, pdf_literal_data(p), 0); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/*tex Test equality of start of strings: */ |
| 116 |
|
| 117 |
static boolean str_in_cstr(str_number s, const char *r, unsigned i) |
| 118 |
{ |
| 119 |
const unsigned char *k, *l; |
| 120 |
if ((unsigned) str_length(s) < i + strlen(r)) |
| 121 |
return false; |
| 122 |
k = (const unsigned char *) r; |
| 123 |
l = str_string(s) + i; |
| 124 |
while ((*l) && (*k)) { |
| 125 |
if (*l++ != *k++) |
| 126 |
return false; |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
void pdf_literal(PDF pdf, str_number s, int literal_mode, boolean warn) |
| 132 |
{ |
| 133 |
unsigned char *ss; |
| 134 |
size_t l; |
| 135 |
/*tex |
| 136 |
The current character code position, initialized to make the compiler |
| 137 |
happy: |
| 138 |
*/ |
| 139 |
pool_pointer j = 0; |
| 140 |
pdfstructure *p = pdf->pstruct; |
| 141 |
if (s >= STRING_OFFSET) { |
| 142 |
/*tex Needed for |out_save|: */ |
| 143 |
j = 0; |
| 144 |
/*tex |
| 145 |
Unfortunately we always go through this when we have vf specials (and |
| 146 |
also via temp strings): |
| 147 |
*/ |
| 148 |
if (literal_mode == scan_special) { |
| 149 |
if (!(str_in_cstr(s, "pdf:", 0) || str_in_cstr(s, "PDF:", 0))) { |
| 150 |
if (warn && ((!(str_in_cstr(s, "src:", 0) || str_in_cstr(s, "SRC:", 0))) || (str_length(s) == 0))) |
| 151 |
tprint_nl("Non-PDF special ignored!"); |
| 152 |
return; |
| 153 |
} |
| 154 |
/*tex |strlen("PDF:")| */ |
| 155 |
j = j + (pool_pointer) 4; |
| 156 |
if (str_in_cstr(s, "direct:", 4)) { |
| 157 |
/*tex |strlen("direct:")| */ |
| 158 |
j = j + (pool_pointer) 7; |
| 159 |
literal_mode = direct_always; |
| 160 |
} else if (str_in_cstr(s, "page:", 4)) { |
| 161 |
/*tex |strlen("page:")| */ |
| 162 |
j = j + (pool_pointer) 5; |
| 163 |
literal_mode = direct_page; |
| 164 |
} else if (str_in_cstr(s, "text:", 4)) { |
| 165 |
/*tex |strlen("text:")| */ |
| 166 |
j = j + (pool_pointer) 5; |
| 167 |
literal_mode = direct_text; |
| 168 |
} else if (str_in_cstr(s, "raw:", 4)) { |
| 169 |
/*tex |strlen("raw:")| */ |
| 170 |
j = j + (pool_pointer) 4; |
| 171 |
literal_mode = direct_raw; |
| 172 |
} else if (str_in_cstr(s, "origin:", 4)) { |
| 173 |
/*tex |strlen("origin:")| */ |
| 174 |
j = j + (pool_pointer) 7; |
| 175 |
literal_mode = set_origin; |
| 176 |
} else { |
| 177 |
literal_mode = set_origin; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
switch (literal_mode) { |
| 182 |
case set_origin: |
| 183 |
pdf_goto_pagemode(pdf); |
| 184 |
pdf_set_pos(pdf, pdf->posstruct->pos); |
| 185 |
break; |
| 186 |
case direct_page: |
| 187 |
pdf_goto_pagemode(pdf); |
| 188 |
break; |
| 189 |
case direct_text: |
| 190 |
pdf_goto_fontmode(pdf); |
| 191 |
/*tex not: |pdf_goto_textmode(pdf);| */ |
| 192 |
break; |
| 193 |
case direct_always: |
| 194 |
pdf_end_string_nl(pdf); |
| 195 |
p->need_tm = true; |
| 196 |
break; |
| 197 |
case direct_raw: |
| 198 |
pdf_end_string_nl(pdf); |
| 199 |
break; |
| 200 |
default: |
| 201 |
normal_error("pdf backend","bad literal mode"); |
| 202 |
break; |
| 203 |
} |
| 204 |
if (s >= STRING_OFFSET) { |
| 205 |
ss = str_string(s); |
| 206 |
l = str_length(s) - (size_t) j; |
| 207 |
pdf_out_block(pdf, (const char *) (ss + j), l); |
| 208 |
} else { |
| 209 |
pdf_out(pdf, s); |
| 210 |
} |
| 211 |
pdf_out(pdf, '\n'); |
| 212 |
} |
| 213 |
|
| 214 |
void pdf_literal_set_mode(PDF pdf, int literal_mode) |
| 215 |
{ |
| 216 |
pdfstructure *p = pdf->pstruct; |
| 217 |
switch (literal_mode) { |
| 218 |
case set_origin: |
| 219 |
pdf_goto_pagemode(pdf); |
| 220 |
pdf_set_pos(pdf, pdf->posstruct->pos); |
| 221 |
break; |
| 222 |
case direct_page: |
| 223 |
pdf_goto_pagemode(pdf); |
| 224 |
break; |
| 225 |
case direct_text: |
| 226 |
pdf_goto_textmode(pdf); |
| 227 |
break; |
| 228 |
case direct_font: |
| 229 |
pdf_goto_fontmode(pdf); |
| 230 |
break; |
| 231 |
case direct_always: |
| 232 |
pdf_end_string_nl(pdf); |
| 233 |
p->need_tm = true; |
| 234 |
break; |
| 235 |
case direct_raw: |
| 236 |
pdf_end_string_nl(pdf); |
| 237 |
break; |
| 238 |
default: |
| 239 |
normal_error("pdf backend","bad literal mode"); |
| 240 |
break; |
| 241 |
} |
| 242 |
} |