Debian Codegraph @

texmf-dist/source/latex/hyperbar/hyperbar.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% Copyright (C) 2018 by Marcel Krüger
4 %%
5 %% This work may be distributed and/or modified under the
6 %% conditions of the LaTeX Project Public License, either version 1.3
7 %% of this license or (at your option) any later version.
8 %% The latest version of this license is in
9 %% http://www.latex-project.org/lppl.txt
10 %% and version 1.3 or later is part of all distributions of LaTeX
11 %% version 2005/12/01 or later.
12 %%
13 %% This work has the LPPL maintenance status `maintained'.
14 %%
15 %% The Current Maintainer of this work is Marcel Krüger
16 %%
17 %
18 %<*batch>
19 %<*gobble>
20 \ifx\jobname\relax\let\documentclass\undefined\fi
21 \ifx\documentclass\undefined
22 \csname fi\endcsname
23 %</gobble>
24 \input l3docstrip.tex
25 \keepsilent
26 \preamble
27 \endpreamble
28 \generate{\file{hyperbar.sty}{\from{hyperbar.dtx}{package}}}
29 \endbatchfile
30 %</batch>
31 %<*gobble>
32 \fi
33 \expandafter\ifx\csname @currname\endcsname\empty
34 \csname fi\endcsname
35 %</gobble>
36 %<*driver>
37 \documentclass[full]{l3doc}
38 \begin{document}
39 \DocInput{hyperbar.dtx}
40 \PrintIndex
41 \PrintChanges
42 \end{document}
43 %</driver>
44 %<*gobble>
45 \fi
46 %</gobble>
47 % \fi
48 %
49 % \title{The \pkg{hyperbar} package\thanks{This document
50 % corresponds to \pkg{hyperbar}~v0.1, dated~2018/07/04.}}
51 % \author{Marcel Kr\"uger \\ \href{mailto:[email protected]}{[email protected]}}
52 %
53 % \maketitle
54 %
55 % \begin{documentation}
56 % This package extends the \pkg{hyperref} functionality for creating interactive forms to allow adding Barcode form fields supported by some modern PDF readers.
57 % \section{Usage}
58 % \begin{function}{\BarcodeField}
59 % \begin{syntax}
60 % |\BarcodeField|[
61 % \meta{type},
62 % width=\meta{Barcode width},
63 % height=\meta{Barcode height},
64 % tsv=\Arg{fields to encode in barcode},\meta{Further parameters}
65 % ]{\meta{Some field name}}
66 % \end{syntax}
67 % Barcode fields can be used like ordinary form fields as documented in the hyperref manual, section ``PDF and HTML forms''. Especially all parameters documented there can be used in \meta{Further parameters}. There are some additional parameters specific to \cmd\BarcodeField:
68 % \end{function}
69 % \begin{description}
70 % \item[\meta{type}] One of the parameters |qr|, |datamatrix| or |pdf417| can be used to select the format of the generated barcode. If no such parameter is given, a PDF417 barcode is used.
71 % \item[symwidth] The horizontal distance between the modules of the barcode in pixel assuming a resolution of $300$ DPI. Default is $6$.
72 % \item[symfactor] Only used for PDF417. The ratio between the height and the width of the modules. Must be one of the values $1, 2, 3, 4$. Default $4$.
73 % \item[ecc] The level of redundancy added to the data for error correction. Not used for DataMatrix, valid values for QR-Codes are $0, 1, 2, 3$. For PDF417 the valid range are integer values between $0$ and $8$.
74 % \item[tsv] This is used as |tsv={<field1>,<field2>,<...>}| to automatically generate the encoded value of the barcode as a combination of the form fields with names \meta{field1}, \meta{field2}, \dots.
75 % The format is based on the Tab-separated value option of Adobe Acrobat with field names. The generated value contains two lines, the first line are all used field names separated by a tab, the secons line consists of the tab-separated values.
76 %
77 % If you prefer another format, the |calculate| parameter can be used instead to generate the value with customized Javascript code.
78 % \end{description}
79 % Currently the only supported backend is pdf\TeX.
80 % \section{Example}
81 % \begin{verbatim}
82 % ...
83 % \usepackage{hyperbar}
84 % ...
85 % \begin{document}
86 % ...
87 % \begin{Form}
88 % \TextField[name=first,width=3cm,charsize=12pt]{Prename}\\
89 % \TextField[name=last,width=3cm,charsize=12pt]{Name}\\
90 % \BarcodeField[width=10cm,height=1cm,tsv={first,last}]{BarcodeField}\\
91 % \BarcodeField[datamatrix,symwidth=10,width=3cm,height=3cm,tsv={first,last}]{BarcodeField2}\\
92 % \BarcodeField[qr,width=3cm,height=3cm,tsv={first,last}]{BarcodeField3}
93 % \end{Form}
94 % \end{document}
95 % \end{verbatim}
96 % This example creates a simple PDF form. In PDF-Reader supporting Barcode fields three types of barcodes will be visible under the form, which contain the information entered into the fields.
97 % \end{documentation}
98 % \begin{implementation}
99 % \section{The implementation}
100 % \iffalse
101 %<*package>
102 \NeedsTeXFormat{LaTeX2e}
103 \ProvidesPackage{hyperbar}
104 [2018/07/04 v0.1
105 Add barcode form fields to hyperref for pdfTeX]
106 %</package>
107 % \fi
108 % \begin{macrocode}
109 \RequirePackage{hyperref}
110
111 \def\BarField@SetKeys{\kvsetkeys{BarField}}
112 \kv@set@family@handler{BarField}{\kv@processor@default{Field}{#1}{#2}}
113 \newcount\BarFld@barcodetype
114 \newcount\BarFld@symwidth
115 \newcount\BarFld@symfactor
116 \newcount\BarFld@ecc
117 \BarFld@barcodetype=0
118 \BarFld@symwidth=6
119 \BarFld@symfactor=2
120 \BarFld@ecc=2
121 \define@key{BarField}{pdf417}[]{%
122 \BarFld@barcodetype=0
123 }
124 \define@key{BarField}{qr}[]{%
125 \BarFld@barcodetype=1
126 }
127 \define@key{BarField}{datamatrix}[]{%
128 \BarFld@barcodetype=2
129 }
130 \define@key{BarField}{symwidth}{%
131 \BarFld@symwidth=#1\relax
132 }
133 \define@key{BarField}{symfactor}{%
134 \BarFld@symfactor=#1\relax
135 }
136 \define@key{BarField}{ecc}{%
137 \BarFld@ecc=#1\relax
138 }
139 \def\qBarcodeFld{\qBarcodeFld}
140 \def\BarFld@barcode@set@calculate@names#1#2#3,#4{%
141 \ifx\qBarcodeFld#4%
142 \expandafter\@firstoftwo
143 \else
144 \expandafter\@secondoftwo
145 \fi
146 {\def\Fld@calculate@code{event.value = event.value + " "}%
147 \def\Fld@format@code{event.value="#1#3\string\\n#2"+getField("#3").valueAsString;}}%
148 {\BarFld@barcode@set@calculate@names
149 {#1#3\string\\t}
150 {#2"+getField("#3").valueAsString+"\string\\t}%
151 #4%
152 }%
153 }
154 \define@key{Field}{tsv}{%
155 \BarFld@barcode@set@calculate@names{}{}#1,\qBarcodeFld
156 }
157 \DeclareRobustCommand\BarcodeField{%
158 \@ifnextchar[{\@BarcodeField}{\@BarcodeField[]}%
159 }
160 %% The following is based on the TextField implementation
161 %% from the hyperref bundle, file hpdftex.def, with only the
162 %% barcode specific fields added.
163 \def\BarField@FlagsBarcode{%
164 \HyField@FlagsAnnot{barcode field}%
165 \bitsetReset{HyField@Ff}%
166 \HyField@UseFlag{Ff}{ReadOnly}%
167 \HyField@UseFlag{Ff}{Required}%
168 \HyField@UseFlag{Ff}{NoExport}%
169 \HyField@UseFlag{Ff}{Multiline}%
170 \HyField@UseFlag{Ff}{DoNotSpellCheck}%
171 \HyField@UseFlag{Ff}{DoNotScroll}%
172 \HyField@PrintFlags{Ff}{text field}%
173 \bitsetIsEmpty{HyField@Ff}{%
174 \let\Fld@flags\ltx@empty
175 }{%
176 \edef\Fld@flags{/Ff \bitsetGetDec{HyField@Ff}}%
177 }%
178 }
179 \def\LayoutBarcodeField#1#2{#2}
180 \def\MakeBarcodeField#1#2{\vbox to #2{\hbox to #1{\hfill}\vfill}}
181 % \end{macrocode}
182 % The list used as the |\DefaultOptionsofBarcode| are the flags set by Acrobat according to
183 % Implementation Note E-7 to Adobe Extensions to ISO 32000.
184 % \begin{macrocode}
185 \def\DefaultOptionsofBarcode{readonly,noexport,multiline,donotspellcheck}
186 \def\BarPDF@BarcodeDict{%
187 /Subtype/Widget%
188 \Fld@annotflags
189 \Fld@pageobjref
190 \Fld@annotnames
191 /FT/Tx%
192 \Fld@flags
193 /PMD \the\pdflastobj\space0 R%
194 /Q \Fld@align
195 /BS<</W \Fld@borderwidth\space /S /\Fld@borderstyle>>%
196 % \end{macrocode}
197 % The |/DataPrep| field is optional with a default value of 0 according to the Adobe Supplement to ISO 32000, Extension Level 3.
198 % We still explicitly provide it because in reality Acrobat will not render the Barcode otherwise.
199 % \begin{macrocode}
200 /DataPrep 0%
201 \ifcase0\ifnum\Fld@rotation=\z@ \else 1\fi
202 \ifx\Fld@bordercolor\relax\else 1\fi
203 \ifx\Fld@bcolor\relax \else 1\fi
204 \space
205 \else
206 /MK<<%
207 \ifnum\Fld@rotation=\z@
208 \else
209 /R \Fld@rotation
210 \fi
211 \ifx\Fld@bordercolor\relax
212 \else
213 /BC[\Fld@bordercolor]%
214 \fi
215 \ifx\Fld@bcolor\relax
216 \else
217 /BG[\Fld@bcolor]%
218 \fi
219 >>%
220 \fi
221 /DA(/Helv \strip@pt\Fld@charsize\space Tf%
222 \ifx\Fld@color\@empty\else\space\Fld@color\fi)%
223 /DV(\Hy@escapestring{\Fld@default})%
224 /V(\Hy@escapestring{\Fld@value})%
225 \Fld@additionalactions
226 }
227 \def\@BarcodeField[#1]#2{%
228 \def\Fld@name{#2}%
229 \let\Fld@default\ltx@empty
230 \let\Fld@value\@empty
231 \def\Fld@width{\DefaultWidthofText}%
232 \def\Fld@height{\DefaultHeightofTextMultiline}%
233 \begingroup
234 \expandafter\BarField@SetKeys\expandafter{%
235 \DefaultOptionsofBarcode,#1%
236 }%
237 \PDFForm@Name
238 \BarField@FlagsBarcode
239 \ifFld@hidden\def\Fld@width{1sp}\fi
240 \ifx\Fld@value\@empty\def\Fld@value{\Fld@default}\fi
241 \LayoutBarcodeField{#2}{%
242 \leavevmode
243 \HyAnn@AbsPageLabel
244 \immediate\pdfobj{<<%
245 /Type/PaperMetaData%
246 /Version 1%
247 /Width \strip@pt\dimexpr\Fld@width*65536/\number\dimexpr1in\relax\relax
248 /Height \strip@pt\dimexpr\Fld@height*65536/\number\dimexpr1in\relax\relax
249 /XSymWidth \the\BarFld@symwidth
250 \ifcase\BarFld@barcodetype
251 /Symbology/PDF417%
252 /XSymHeight \the\numexpr\BarFld@symwidth*\BarFld@symfactor\relax
253 /ECC \the\BarFld@ecc
254 \or
255 /Symbology/QRCode%
256 /ECC \the\BarFld@ecc
257 \or
258 /Symbology/DataMatrix%
259 \fi
260 % \end{macrocode}
261 % The |/Resolution| is optional according to the Specification but required in practice like |/DataPrep| above.
262 % Acrobat also fails with the default value $300$, instead a decimal part is required.
263 % \begin{macrocode}
264 /Resolution 300.0%
265 >>}%
266 \Hy@escapeform\BarPDF@BarcodeDict
267 \pdfstartlink user {\BarPDF@BarcodeDict}\relax
268 \MakeBarcodeField{\Fld@width}{\Fld@height}\pdfendlink
269 \HyField@AddToFields
270 }%
271 \endgroup
272 }
273 % \end{macrocode}
274 % \end{implementation}