| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009 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 |
/*tex |
| 25 |
|
| 26 |
We keep a stack for |pdfextension setmatrix|: |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
matrix_entry *matrix_stack = NULL; |
| 31 |
int matrix_stack_size = 0; |
| 32 |
int matrix_stack_used = 0; |
| 33 |
|
| 34 |
boolean matrixused(void) |
| 35 |
{ |
| 36 |
return matrix_stack_used > 0; |
| 37 |
} |
| 38 |
|
| 39 |
static void matrix_stack_room(void) |
| 40 |
{ |
| 41 |
matrix_entry *new_stack; |
| 42 |
if (matrix_stack_used >= matrix_stack_size) { |
| 43 |
matrix_stack_size += STACK_INCREMENT; |
| 44 |
new_stack = xtalloc((unsigned) matrix_stack_size, matrix_entry); |
| 45 |
memcpy((void *) new_stack, (void *) matrix_stack, (unsigned) matrix_stack_used * sizeof(matrix_entry)); |
| 46 |
xfree(matrix_stack); |
| 47 |
matrix_stack = new_stack; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/*tex |
| 52 |
|
| 53 |
The matrix specification has four entries and gets translated to |e| being |
| 54 |
|pos.h and |f| being pos.v. The current active matrix at the top of the |
| 55 |
matrix stack is kept in |M_top|. |
| 56 |
|
| 57 |
The origin of \.{\\pdfextension setmatrix} is the current point. The |
| 58 |
annotation coordinate system is the original page coordinate system. When |
| 59 |
pdfTeX calculates annotation rectangles it does not take into account this |
| 60 |
transformations, it uses the original coordinate system. To get the corrected |
| 61 |
values, first we go back to the origin, perform the transformation and go |
| 62 |
back: |
| 63 |
|
| 64 |
\starttyping |
| 65 |
( 1 0 0 ) ( a b 0 ) ( 1 0 0 ) |
| 66 |
( 0 1 0 ) x ( c d 0 ) x ( 0 1 0 ) x M\_top |
| 67 |
( -e -f 1 ) ( 0 0 1 ) ( e f 1 ) |
| 68 |
|
| 69 |
( 1 0 0 ) ( a b 0 ) |
| 70 |
= ( 0 1 0 ) x ( c d 0 ) x M\_top |
| 71 |
( e f 1 ) ( -e -f 1 ) |
| 72 |
|
| 73 |
( a b 0 ) |
| 74 |
= ( c d 0 ) x M\_top |
| 75 |
( e(1-a)-fc f(1-d)-eb 1 ) |
| 76 |
\stoptyping |
| 77 |
|
| 78 |
*/ |
| 79 |
|
| 80 |
static void pdfsetmatrix(const char *in, scaledpos pos) |
| 81 |
{ |
| 82 |
/*tex |
| 83 |
The argument of |pdfextension setmatrix| starts with |str_pool[in]| |
| 84 |
and ends before |str_pool[pool_ptr]|. |
| 85 |
*/ |
| 86 |
matrix_entry x, *y, *z; |
| 87 |
if (global_shipping_mode == SHIPPING_PAGE) { |
| 88 |
if (sscanf((const char *) in, " %lf %lf %lf %lf ", &x.a, &x.b, &x.c, &x.d) != 4) { |
| 89 |
formatted_warning("pdf backend","unrecognized format of setmatrix: %s", in); |
| 90 |
return; |
| 91 |
} |
| 92 |
/*tex Calculate this transformation matrix. */ |
| 93 |
x.e = (double) pos.h * (1.0 - x.a) - (double) pos.v * x.c; |
| 94 |
x.f = (double) pos.v * (1.0 - x.d) - (double) pos.h * x.b; |
| 95 |
matrix_stack_room(); |
| 96 |
z = &matrix_stack[matrix_stack_used]; |
| 97 |
if (matrix_stack_used > 0) { |
| 98 |
y = &matrix_stack[matrix_stack_used - 1]; |
| 99 |
z->a = x.a * y->a + x.b * y->c; |
| 100 |
z->b = x.a * y->b + x.b * y->d; |
| 101 |
z->c = x.c * y->a + x.d * y->c; |
| 102 |
z->d = x.c * y->b + x.d * y->d; |
| 103 |
z->e = x.e * y->a + x.f * y->c + y->e; |
| 104 |
z->f = x.e * y->b + x.f * y->d + y->f; |
| 105 |
} else { |
| 106 |
z->a = x.a; |
| 107 |
z->b = x.b; |
| 108 |
z->c = x.c; |
| 109 |
z->d = x.d; |
| 110 |
z->e = x.e; |
| 111 |
z->f = x.f; |
| 112 |
} |
| 113 |
matrix_stack_used++; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/*tex |
| 118 |
|
| 119 |
Apply matrix to point (x,y) |
| 120 |
|
| 121 |
\starttyping |
| 122 |
( a b 0 ) |
| 123 |
( x y 1 ) x ( c d 0 ) = ( xa+yc+e xb+yd+f 1 ) |
| 124 |
( e f 1 ) |
| 125 |
\stoptyping |
| 126 |
|
| 127 |
If \.{\\pdfextension setmatrix} wasn't used, then return the value unchanged. |
| 128 |
The return values for matrix tranform functions are: |
| 129 |
|
| 130 |
*/ |
| 131 |
|
| 132 |
static scaled ret_llx; |
| 133 |
static scaled ret_lly; |
| 134 |
static scaled ret_urx; |
| 135 |
static scaled ret_ury; |
| 136 |
|
| 137 |
scaled getllx(void) |
| 138 |
{ |
| 139 |
return ret_llx; |
| 140 |
} |
| 141 |
|
| 142 |
scaled getlly(void) |
| 143 |
{ |
| 144 |
return ret_lly; |
| 145 |
} |
| 146 |
|
| 147 |
scaled geturx(void) |
| 148 |
{ |
| 149 |
return ret_urx; |
| 150 |
} |
| 151 |
|
| 152 |
scaled getury(void) |
| 153 |
{ |
| 154 |
return ret_ury; |
| 155 |
} |
| 156 |
|
| 157 |
static int last_llx; |
| 158 |
static int last_lly; |
| 159 |
static int last_urx; |
| 160 |
static int last_ury; |
| 161 |
|
| 162 |
#define DO_ROUND(x) ((x > 0) ? (x + .5) : (x - .5)) |
| 163 |
#define DO_MIN(a, b) ((a < b) ? a : b) |
| 164 |
#define DO_MAX(a, b) ((a > b) ? a : b) |
| 165 |
|
| 166 |
static void do_matrixtransform(scaled x, scaled y, scaled * retx, scaled * rety) |
| 167 |
{ |
| 168 |
matrix_entry *m = &matrix_stack[matrix_stack_used - 1]; |
| 169 |
double x_old = x; |
| 170 |
double y_old = y; |
| 171 |
double x_new = x_old * m->a + y_old * m->c + m->e; |
| 172 |
double y_new = x_old * m->b + y_old * m->d + m->f; |
| 173 |
*retx = (scaled) DO_ROUND(x_new); |
| 174 |
*rety = (scaled) DO_ROUND(y_new); |
| 175 |
} |
| 176 |
|
| 177 |
void matrixtransformrect(scaled llx, scaled lly, scaled urx, scaled ury) |
| 178 |
{ |
| 179 |
scaled x1, x2, x3, x4, y1, y2, y3, y4; |
| 180 |
if (global_shipping_mode == SHIPPING_PAGE && matrix_stack_used > 0) { |
| 181 |
last_llx = llx; |
| 182 |
last_lly = lly; |
| 183 |
last_urx = urx; |
| 184 |
last_ury = ury; |
| 185 |
do_matrixtransform(llx, lly, &x1, &y1); |
| 186 |
do_matrixtransform(llx, ury, &x2, &y2); |
| 187 |
do_matrixtransform(urx, lly, &x3, &y3); |
| 188 |
do_matrixtransform(urx, ury, &x4, &y4); |
| 189 |
ret_llx = DO_MIN(DO_MIN(x1, x2), DO_MIN(x3, x4)); |
| 190 |
ret_lly = DO_MIN(DO_MIN(y1, y2), DO_MIN(y3, y4)); |
| 191 |
ret_urx = DO_MAX(DO_MAX(x1, x2), DO_MAX(x3, x4)); |
| 192 |
ret_ury = DO_MAX(DO_MAX(y1, y2), DO_MAX(y3, y4)); |
| 193 |
} else { |
| 194 |
ret_llx = llx; |
| 195 |
ret_lly = lly; |
| 196 |
ret_urx = urx; |
| 197 |
ret_ury = ury; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void matrixtransformpoint(scaled x, scaled y) |
| 202 |
{ |
| 203 |
if (global_shipping_mode == SHIPPING_PAGE && matrix_stack_used > 0) { |
| 204 |
do_matrixtransform(x, y, &ret_llx, &ret_lly); |
| 205 |
} else { |
| 206 |
ret_llx = x; |
| 207 |
ret_lly = y; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void matrixrecalculate(scaled urx) |
| 212 |
{ |
| 213 |
matrixtransformrect(last_llx, last_lly, urx, last_ury); |
| 214 |
} |
| 215 |
|
| 216 |
void pdf_out_setmatrix(PDF pdf, halfword p) |
| 217 |
{ |
| 218 |
scaledpos pos = pdf->posstruct->pos; |
| 219 |
int old_setting; |
| 220 |
str_number s; |
| 221 |
old_setting = selector; |
| 222 |
selector = new_string; |
| 223 |
show_token_list(token_link(pdf_setmatrix_data(p)), null, -1); |
| 224 |
pdfsetmatrix((char *) cur_string, pos); |
| 225 |
tprint(" 0 0 cm"); |
| 226 |
selector = old_setting; |
| 227 |
s = make_string(); |
| 228 |
pdf_literal(pdf, s, set_origin, false); |
| 229 |
flush_str(s); |
| 230 |
} |