summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/hyperbar/hyperbar.dtx
blob: 343a703f1f34df259c236fb59f2da92d631bd5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
% \iffalse meta-comment
%
%% Copyright (C) 2018 by Marcel Krüger
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Marcel Krüger
%% 
%
%<*batch>
%<*gobble>
\ifx\jobname\relax\let\documentclass\undefined\fi
\ifx\documentclass\undefined
\csname fi\endcsname
%</gobble>
\input l3docstrip.tex
\keepsilent
\preamble
\endpreamble
\generate{\file{hyperbar.sty}{\from{hyperbar.dtx}{package}}}
\endbatchfile
%</batch>
%<*gobble>
\fi
\expandafter\ifx\csname @currname\endcsname\empty
\csname fi\endcsname
%</gobble>
%<*driver>
\documentclass[full]{l3doc}
\begin{document}
\DocInput{hyperbar.dtx}
\PrintIndex
\PrintChanges
\end{document}
%</driver>
%<*gobble>
\fi
%</gobble>
% \fi
%
% \title{The \pkg{hyperbar} package\thanks{This document
%        corresponds to \pkg{hyperbar}~v0.1, dated~2018/07/04.}}
% \author{Marcel Kr\"uger \\ \href{mailto:tex@2krueger.de}{tex@2krueger.de}}
% 
% \maketitle
%
% \begin{documentation}
% This package extends the \pkg{hyperref} functionality for creating interactive forms to allow adding Barcode form fields supported by some modern PDF readers.
% \section{Usage}
% \begin{function}{\BarcodeField}
%    \begin{syntax}
%      |\BarcodeField|[
%        \meta{type},
%        width=\meta{Barcode width},
%        height=\meta{Barcode height},
%        tsv=\Arg{fields to encode in barcode},\meta{Further parameters}
%      ]{\meta{Some field name}}
%    \end{syntax}
% 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:
% \end{function}
% \begin{description}
% \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.
% \item[symwidth] The horizontal distance between the modules of the barcode in pixel assuming a resolution of $300$ DPI. Default is $6$.
% \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$.
% \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$.
% \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.
% 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.
%
% If you prefer another format, the |calculate| parameter can be used instead to generate the value with customized Javascript code.
% \end{description}
% Currently the only supported backend is pdf\TeX.
% \section{Example}
% \begin{verbatim}
% ...
% \usepackage{hyperbar}
% ...
% \begin{document}
% ...
% \begin{Form}
%   \TextField[name=first,width=3cm,charsize=12pt]{Prename}\\
%   \TextField[name=last,width=3cm,charsize=12pt]{Name}\\
%   \BarcodeField[width=10cm,height=1cm,tsv={first,last}]{BarcodeField}\\
%   \BarcodeField[datamatrix,symwidth=10,width=3cm,height=3cm,tsv={first,last}]{BarcodeField2}\\
%   \BarcodeField[qr,width=3cm,height=3cm,tsv={first,last}]{BarcodeField3}
% \end{Form}
% \end{document}
% \end{verbatim}
% 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.
% \end{documentation}
% \begin{implementation}
% \section{The implementation}
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{hyperbar}
  [2018/07/04 v0.1
  Add barcode form fields to hyperref for pdfTeX]
%</package>
% \fi
%    \begin{macrocode}
\RequirePackage{hyperref}

\def\BarField@SetKeys{\kvsetkeys{BarField}}
\kv@set@family@handler{BarField}{\kv@processor@default{Field}{#1}{#2}}
\newcount\BarFld@barcodetype
\newcount\BarFld@symwidth
\newcount\BarFld@symfactor
\newcount\BarFld@ecc
\BarFld@barcodetype=0
\BarFld@symwidth=6
\BarFld@symfactor=2
\BarFld@ecc=2
\define@key{BarField}{pdf417}[]{%
  \BarFld@barcodetype=0
}
\define@key{BarField}{qr}[]{%
  \BarFld@barcodetype=1
}
\define@key{BarField}{datamatrix}[]{%
  \BarFld@barcodetype=2
}
\define@key{BarField}{symwidth}{%
  \BarFld@symwidth=#1\relax
}
\define@key{BarField}{symfactor}{%
  \BarFld@symfactor=#1\relax
}
\define@key{BarField}{ecc}{%
  \BarFld@ecc=#1\relax
}
\def\qBarcodeFld{\qBarcodeFld}
\def\BarFld@barcode@set@calculate@names#1#2#3,#4{%
  \ifx\qBarcodeFld#4%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\def\Fld@calculate@code{event.value = event.value + " "}%
   \def\Fld@format@code{event.value="#1#3\string\\n#2"+getField("#3").valueAsString;}}%
  {\BarFld@barcode@set@calculate@names
      {#1#3\string\\t}
      {#2"+getField("#3").valueAsString+"\string\\t}%
      #4%
  }%
}
\define@key{Field}{tsv}{%
  \BarFld@barcode@set@calculate@names{}{}#1,\qBarcodeFld
}
\DeclareRobustCommand\BarcodeField{%
  \@ifnextchar[{\@BarcodeField}{\@BarcodeField[]}%
}
%% The following is based on the TextField implementation
%% from the hyperref bundle, file hpdftex.def, with only the
%% barcode specific fields added.
\def\BarField@FlagsBarcode{%
  \HyField@FlagsAnnot{barcode field}%
  \bitsetReset{HyField@Ff}%
  \HyField@UseFlag{Ff}{ReadOnly}%
  \HyField@UseFlag{Ff}{Required}%
  \HyField@UseFlag{Ff}{NoExport}%
  \HyField@UseFlag{Ff}{Multiline}%
  \HyField@UseFlag{Ff}{DoNotSpellCheck}%
  \HyField@UseFlag{Ff}{DoNotScroll}%
  \HyField@PrintFlags{Ff}{text field}%
  \bitsetIsEmpty{HyField@Ff}{%
    \let\Fld@flags\ltx@empty
  }{%
    \edef\Fld@flags{/Ff \bitsetGetDec{HyField@Ff}}%
  }%
}
\def\LayoutBarcodeField#1#2{#2}
\def\MakeBarcodeField#1#2{\vbox to #2{\hbox to #1{\hfill}\vfill}}
%    \end{macrocode}
% The list used as the |\DefaultOptionsofBarcode| are the flags set by Acrobat according to
% Implementation Note E-7 to Adobe Extensions to ISO 32000.
%    \begin{macrocode}
\def\DefaultOptionsofBarcode{readonly,noexport,multiline,donotspellcheck}
\def\BarPDF@BarcodeDict{%
  /Subtype/Widget%
  \Fld@annotflags
  \Fld@pageobjref
  \Fld@annotnames
  /FT/Tx%
  \Fld@flags
  /PMD \the\pdflastobj\space0 R%
  /Q \Fld@align
  /BS<</W \Fld@borderwidth\space /S /\Fld@borderstyle>>%
%    \end{macrocode}
% The |/DataPrep| field is optional with a default value of 0 according to the Adobe Supplement to ISO 32000, Extension Level 3.
% We still explicitly provide it because in reality Acrobat will not render the Barcode otherwise.
%    \begin{macrocode}
  /DataPrep 0%
  \ifcase0\ifnum\Fld@rotation=\z@   \else 1\fi
          \ifx\Fld@bordercolor\relax\else 1\fi
          \ifx\Fld@bcolor\relax     \else 1\fi
          \space
  \else
    /MK<<%
      \ifnum\Fld@rotation=\z@
      \else
        /R \Fld@rotation
      \fi
      \ifx\Fld@bordercolor\relax
      \else
        /BC[\Fld@bordercolor]%
      \fi
      \ifx\Fld@bcolor\relax
      \else
        /BG[\Fld@bcolor]%
      \fi
    >>%
  \fi
  /DA(/Helv \strip@pt\Fld@charsize\space Tf%
      \ifx\Fld@color\@empty\else\space\Fld@color\fi)%
  /DV(\Hy@escapestring{\Fld@default})%
  /V(\Hy@escapestring{\Fld@value})%
  \Fld@additionalactions
}
\def\@BarcodeField[#1]#2{%
  \def\Fld@name{#2}%
  \let\Fld@default\ltx@empty
  \let\Fld@value\@empty
  \def\Fld@width{\DefaultWidthofText}%
  \def\Fld@height{\DefaultHeightofTextMultiline}%
  \begingroup
    \expandafter\BarField@SetKeys\expandafter{%
      \DefaultOptionsofBarcode,#1%
    }%
    \PDFForm@Name
    \BarField@FlagsBarcode
    \ifFld@hidden\def\Fld@width{1sp}\fi
    \ifx\Fld@value\@empty\def\Fld@value{\Fld@default}\fi
    \LayoutBarcodeField{#2}{%
      \leavevmode
      \HyAnn@AbsPageLabel
      \immediate\pdfobj{<<%
        /Type/PaperMetaData%
        /Version 1%
        /Width \strip@pt\dimexpr\Fld@width*65536/\number\dimexpr1in\relax\relax
        /Height \strip@pt\dimexpr\Fld@height*65536/\number\dimexpr1in\relax\relax
        /XSymWidth \the\BarFld@symwidth
        \ifcase\BarFld@barcodetype
          /Symbology/PDF417%
          /XSymHeight \the\numexpr\BarFld@symwidth*\BarFld@symfactor\relax
          /ECC \the\BarFld@ecc
        \or
          /Symbology/QRCode%
          /ECC \the\BarFld@ecc
        \or
          /Symbology/DataMatrix%
        \fi
%    \end{macrocode}
% The |/Resolution| is optional according to the Specification but required in practice like |/DataPrep| above.
% Acrobat also fails with the default value $300$, instead a decimal part is required.
%    \begin{macrocode}
        /Resolution 300.0%
      >>}%
    \Hy@escapeform\BarPDF@BarcodeDict
      \pdfstartlink user {\BarPDF@BarcodeDict}\relax
      \MakeBarcodeField{\Fld@width}{\Fld@height}\pdfendlink
      \HyField@AddToFields
    }%
  \endgroup
}
%    \end{macrocode}
% \end{implementation}