Imported Upstream version 1.22.4
[platform/upstream/groff.git] / tmac / pdfpic.tmac
1 .\" pdfpic.tmac
2 .\"
3 .\" Define the PDFPIC macro.
4 .\"
5 .\" When used other than with gropdf, the image is converted to .eps
6 .\" and processing passed over to PSPIC.
7 .\"
8 .\" Usage:
9 .\"
10 .\"   .PDFPIC [-L|-R|-C|-I <indent>] <file> [<width> [<height>]]
11 .\"
12 .\" Requires the poppler-utils package (for pdfinfo and pdftops).
13 .\" Requires running groff in unsafe mode.
14 .
15 .do if d PDFPIC .nx
16 .
17 .nr _C \n(.C
18 .cp 0
19 .
20 .de @abort
21 .  ab [PDFPIC]: \\$* Aborting.
22 ..
23 .de PDFPIC
24 .  if !\\n[.U] \
25 .    @abort Use of \\$0 requires giving groff the -U option.
26 .
27 .  nr convert-pdf 0
28 .  if !'\\*[.T]'pdf' .nr convert-pdf 1
29 .
30 .  nr pdf-offset-mode 0
31 .
32 .  \" left-aligned?
33 .  ie '\\$1'-L' \{\
34 .    nr pdf-offset-mode 1
35 .    if \\n[convert-pdf] .ds pspic-args \\$1 \"
36 .    shift
37 .  \}
38 .  el \{\
39 .    \" right-aligned?
40 .    ie '\\$1'-R' \{\
41 .      nr pdf-offset-mode 2
42 .      if \\n[convert-pdf] .ds pspic-args \\$1 \"
43 .      shift
44 .    \}
45 .    el \{\
46 .      \" indented?
47 .      ie '\\$1'-I' \{\
48 .        nr pdf-offset-mode 3
49 .        nr pdf-offset (m;\\$2)
50 .        if \\n[convert-pdf] .ds pspic-args \\$1 \\$2 \"
51 .        shift 2
52 .      \}
53 .      el \{\
54 .        \" centered is the default
55 .        ie '\\$1'-C' \{\
56 .          if \\n[convert-pdf] .ds pspic-args \\$1 \"
57 .          shift
58 .        \}
59 .        el .nr pdf-offset-mode 0
60 .      \}
61 .    \}
62 .  \}
63 .  br
64 .
65 .  ds is-pdf \\$1
66 .  substring is-pdf -3
67 .  if !'\\*[is-pdf]'pdf' \
68 .    @abort \\$1 at line \\n[.c] is not a PDF file, or lacks a .pdf extension.
69 .
70 .\" if driver is not gropdf, convert image to .eps
71 .  if \\n[convert-pdf] \{\
72 .    ds img-file \\$1
73 .    substring img-file 0 -5
74 .
75 .    sy pdftops -eps \\$1
76 .    shift
77 .
78 .    as pspic-args \\*[img-file].eps \\$*
79 .
80 .    PSPIC \\*[pspic-args]
81 .    return
82 .  \}
83 .
84 .\" get image dimensions
85 .  ec @
86 .  sy pdfinfo @$1 | \
87 grep "Page *size" | \
88 sed -e 's/Page *size: *\\([[:digit:].]*\\) *x *\\([[:digit:].]*\\).*$/\
89 .nr pdf-wid (p;\\1)\\n\
90 .nr pdf-ht  (p;\\2)/' \
91 > /tmp/pdfpic\n[$$]
92 .  so /tmp/pdfpic\n[$$]
93 .  sy rm /tmp/pdfpic\n[$$]
94 .  ec
95 .
96 .  \" if we have a <width> parameter, use it as the final
97 .  \" image width; otherwise we use the image's natural width
98 .  \" or the current line length, whatever is smaller
99 .  ie (\\n[.$] >= 2) \
100 .    nr pdf-deswid (i;\\$2)
101 .  el \
102 .    nr pdf-deswid ((\\n[.l] - \\n[.i]) <? \\n[pdf-wid])
103 .
104 .  \" compute the final image height (with proper rounding),
105 .  \" based on the image's aspect
106 .  nr pdf-desht (\\n[pdf-deswid] * 1000 + (\\n[pdf-wid] / 2) \
107                 / \\n[pdf-wid] * \\n[pdf-ht] \
108                 + 500 / 1000)
109 .
110 .  \" if we have a <height> parameter, use it as the final
111 .  \" image height in case it is smaller than the height
112 .  \" value we have just computed
113 .  if ((\\n[.$] >= 3) & (\\n[pdf-desht] > (i;0\\$3))) \{\
114 .    nr pdf-desht (i;\\$3)
115 .    \" recompute the final image width since we always
116 .    \" keep the correct image aspect
117 .    nr pdf-deswid (\\n[pdf-desht] * 1000 + (\\n[pdf-ht] / 2) \
118                    / \\n[pdf-ht] * \\n[pdf-wid] \
119                    + 500 / 1000)
120 .  \}
121 .
122 .  \" reserve vertical space for image
123 .  ne (\\n[pdf-desht]u + 1v)
124 .
125 .  \" compute image offset w.r.t. the current left margin
126 .  if (\\n[pdf-offset-mode] == 0) \
127 .    nr pdf-offset (\\n[.l] - \\n[.i] - \\n[pdf-deswid] / 2)
128 .  if (\\n[pdf-offset-mode] == 1) \
129 .    nr pdf-offset 0
130 .  if (\\n[pdf-offset-mode] == 2) \
131 .    nr pdf-offset (\\n[.l] - \\n[.i] - \\n[pdf-deswid])
132 .
133 \h'\\n[pdf-offset]u'\
134 \X'pdf: pdfpic \\$1 -L \\n[pdf-deswid]z \\n[pdf-desht]z'
135 .  if !r PDFPIC_NOSPACE \{\
136 .    sy echo ".nr PDFPIC_NOSPACE 0$GROFF_PDFPIC_NOSPACE" > /tmp/x\n[$$]
137 .    so /tmp/x\n[$$]
138 .    sy rm /tmp/x\n[$$]
139 .  \}
140 .  if \\n[PDFPIC_NOSPACE]==0 \{\
141 .    br
142 .    sp \\n[pdf-desht]u
143 .  \}
144 ..
145 .
146 .cp \n[_C]
147 .
148 .\" end of pdfpic.tmac