beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / pdf / pdfimage.w
blobfd3642438040563a07c1f78882e20fbbfb1c2d14
1 % pdfimage.w
3 % Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
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.
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.
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/>.
20 @ @c
22 #include "ptexlib.h"
24 @ @c
25 void place_img(PDF pdf, image_dict * idict, scaled_whd dim, int transform)
27 float a[6]; /* transformation matrix */
28 float xoff, yoff, tmp;
29 pdfstructure *p = pdf->pstruct;
30 scaledpos pos = pdf->posstruct->pos;
31 int r; /* number of digits after the decimal point */
32 int k;
33 scaledpos tmppos;
34 pdffloat cm[6];
35 int groupref; /* added from web for 1.40.8 */
36 a[0] = a[3] = 1.0e6;
37 a[1] = a[2] = 0;
38 if (img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM
39 || img_type(idict) == IMG_TYPE_PDFSTREAM) {
40 a[0] /= (float) img_xsize(idict);
41 a[3] /= (float) img_ysize(idict);
42 xoff = (float) img_xorig(idict) / (float) img_xsize(idict);
43 yoff = (float) img_yorig(idict) / (float) img_ysize(idict);
44 r = 6;
45 } else {
46 /* added from web for 1.40.8 */
47 if (img_type(idict) == IMG_TYPE_PNG) {
48 groupref = img_group_ref(idict);
49 if ((groupref > 0) && (pdf->img_page_group_val == 0))
50 pdf->img_page_group_val = groupref;
52 /* /added from web */
53 a[0] /= (float) one_hundred_bp;
54 a[3] = a[0];
55 xoff = yoff = 0;
56 r = 4;
58 if ((transform & 7) > 3) { /* mirror cases */
59 a[0] *= -1;
60 xoff *= -1;
62 switch ((transform + img_rotation(idict)) & 3) {
63 case 0: /* no transform */
64 break;
65 case 1: /* rot. 90 deg. (counterclockwise) */
66 a[1] = a[0];
67 a[2] = -a[3];
68 a[3] = a[0] = 0;
69 tmp = yoff;
70 yoff = xoff;
71 xoff = -tmp;
72 break;
73 case 2: /* rot. 180 deg. (counterclockwise) */
74 a[0] *= -1;
75 a[3] *= -1;
76 xoff *= -1;
77 yoff *= -1;
78 break;
79 case 3: /* rot. 270 deg. (counterclockwise) */
80 a[1] = -a[0];
81 a[2] = a[3];
82 a[3] = a[0] = 0;
83 tmp = yoff;
84 yoff = -xoff;
85 xoff = tmp;
86 break;
87 default:;
89 xoff *= (float) dim.wd;
90 yoff *= (float) (dim.ht + dim.dp);
91 a[0] *= (float) dim.wd;
92 a[1] *= (float) (dim.ht + dim.dp);
93 a[2] *= (float) dim.wd;
94 a[3] *= (float) (dim.ht + dim.dp);
95 a[4] = (float) pos.h - xoff;
96 a[5] = (float) pos.v - yoff;
97 k = transform + img_rotation(idict);
98 if ((transform & 7) > 3)
99 k++;
100 switch (k & 3) {
101 case 0: /* no transform */
102 break;
103 case 1: /* rot. 90 deg. (counterclockwise) */
104 a[4] += (float) dim.wd;
105 break;
106 case 2: /* rot. 180 deg. */
107 a[4] += (float) dim.wd;
108 a[5] += (float) (dim.ht + dim.dp);
109 break;
110 case 3: /* rot. 270 deg. */
111 a[5] += (float) (dim.ht + dim.dp);
112 break;
113 default:;
115 /* the following is a kludge, TODO: use pdfpage.c functions */
116 setpdffloat(cm[0], i64round(a[0]), r);
117 setpdffloat(cm[1], i64round(a[1]), r);
118 setpdffloat(cm[2], i64round(a[2]), r);
119 setpdffloat(cm[3], i64round(a[3]), r);
120 tmppos.h = round(a[4]);
121 tmppos.v = round(a[5]);
122 pdf_goto_pagemode(pdf);
123 (void) calc_pdfpos(p, tmppos);
124 cm[4] = p->cm[4];
125 cm[5] = p->cm[5];
126 if (pdf->img_page_group_val == 0)
127 pdf->img_page_group_val = img_group_ref(idict); /* added from web for 1.40.8 */
128 pdf_puts(pdf, "q\n");
129 pdf_print_cm(pdf, cm);
130 pdf_puts(pdf, "/Im");
131 pdf_print_int(pdf, img_index(idict));
132 pdf_print_resname_prefix(pdf);
133 pdf_puts(pdf, " Do\nQ\n");
134 addto_page_resources(pdf, obj_type_ximage, img_objnum(idict));
135 if (img_state(idict) < DICT_OUTIMG)
136 img_state(idict) = DICT_OUTIMG;
139 @ for normal output, see \.{pdflistout.w}
142 void pdf_place_image(PDF pdf, halfword p)
144 scaled_whd dim;
145 image_dict *idict = idict_array[rule_index(p)];
146 dim.wd = width(p);
147 dim.ht = height(p);
148 dim.dp = depth(p);
149 place_img(pdf, idict, dim, rule_transform(p));