Debian Codegraph @

texk/web2c/luatexdir/pdf/pdfimage.c

raw
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 void place_img(PDF pdf, image_dict * idict, scaled_whd dim, int transform)
25 {
26 /*tex A transformation matrix: */
27 double a[6];
28 double xoff, yoff, tmp;
29 pdfstructure *p = pdf->pstruct;
30 scaledpos pos = pdf->posstruct->pos;
31 /*tex The number of digits after the decimal point: */
32 int r;
33 int k;
34 scaledpos tmppos;
35 pdffloat cm[6];
36 int groupref;
37 a[0] = a[3] = 1.0e6;
38 a[1] = a[2] = 0;
39 if (img_type(idict) == IMG_TYPE_PDF || img_type(idict) == IMG_TYPE_PDFMEMSTREAM
40 || img_type(idict) == IMG_TYPE_PDFSTREAM) {
41 a[0] /= (double) img_xsize(idict);
42 a[3] /= (double) img_ysize(idict);
43 xoff = (double) img_xorig(idict) / (double) img_xsize(idict);
44 yoff = (double) img_yorig(idict) / (double) img_ysize(idict);
45 r = 6;
46 } else {
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;
51 }
52 a[0] /= (double) one_hundred_bp;
53 a[3] = a[0];
54 xoff = yoff = 0;
55 r = 4;
56 }
57 if ((transform & 7) > 3) {
58 /*tex Mirror cases: */
59 a[0] *= -1;
60 xoff *= -1;
61 }
62 switch ((transform + img_rotation(idict)) & 3) {
63 case 0:
64 /*tex No transform. */
65 break;
66 case 1:
67 /*tex rotation over 90 degrees (counterclockwise) */
68 a[1] = a[0];
69 a[2] = -a[3];
70 a[3] = a[0] = 0;
71 tmp = yoff;
72 yoff = xoff;
73 xoff = -tmp;
74 break;
75 case 2:
76 /*tex rotation over 180 degrees (counterclockwise) */
77 a[0] *= -1;
78 a[3] *= -1;
79 xoff *= -1;
80 yoff *= -1;
81 break;
82 case 3:
83 /*tex rotation over 270 degrees (counterclockwise) */
84 a[1] = -a[0];
85 a[2] = a[3];
86 a[3] = a[0] = 0;
87 tmp = yoff;
88 yoff = -xoff;
89 xoff = tmp;
90 break;
91 default:;
92 }
93 xoff *= (double) dim.wd;
94 yoff *= (double) (dim.ht + dim.dp);
95 a[0] *= (double) dim.wd;
96 a[1] *= (double) (dim.ht + dim.dp);
97 a[2] *= (double) dim.wd;
98 a[3] *= (double) (dim.ht + dim.dp);
99 a[4] = (double) pos.h - xoff;
100 a[5] = (double) pos.v - yoff;
101 k = transform + img_rotation(idict);
102 if ((transform & 7) > 3)
103 k++;
104 switch (k & 3) {
105 case 0:
106 /*tex No transform */
107 break;
108 case 1:
109 /*tex rotation over 90 degrees (counterclockwise) */
110 a[4] += (double) dim.wd;
111 break;
112 case 2:
113 /*tex rotation over 180 degrees (counterclockwise) */
114 a[4] += (double) dim.wd;
115 a[5] += (double) (dim.ht + dim.dp);
116 break;
117 case 3:
118 /*tex rotation over 270 degrees (counterclockwise) */
119 a[5] += (double) (dim.ht + dim.dp);
120 break;
121 default:;
122 }
123 setpdffloat(cm[0], i64round(a[0]), r);
124 setpdffloat(cm[1], i64round(a[1]), r);
125 setpdffloat(cm[2], i64round(a[2]), r);
126 setpdffloat(cm[3], i64round(a[3]), r);
127 tmppos.h = round(a[4]);
128 tmppos.v = round(a[5]);
129 pdf_goto_pagemode(pdf);
130 (void) calc_pdfpos(p, tmppos);
131 cm[4] = p->cm[4];
132 cm[5] = p->cm[5];
133 if (pdf->img_page_group_val == 0)
134 pdf->img_page_group_val = img_group_ref(idict);
135 pdf_puts(pdf, "q\n");
136 pdf_print_cm(pdf, cm);
137 pdf_puts(pdf, "/Im");
138 pdf_print_int(pdf, img_index(idict));
139 pdf_print_resname_prefix(pdf);
140 pdf_puts(pdf, " Do\nQ\n");
141 addto_page_resources(pdf, obj_type_ximage, img_objnum(idict));
142 if (img_state(idict) < DICT_OUTIMG)
143 img_state(idict) = DICT_OUTIMG;
144 }
145
146 /*tex For normal output see |pdflistout.c|: */
147
148 void pdf_place_image(PDF pdf, halfword p)
149 {
150 scaled_whd dim;
151 image_dict *idict = idict_array[rule_index(p)];
152 dim.wd = width(p);
153 dim.ht = height(p);
154 dim.dp = depth(p);
155 place_img(pdf, idict, dim, rule_transform(p));
156 }