| 1 |
%-------------------------------------------- |
| 2 |
% $Header: /cvsroot/pgfplots/pgfplots/generic/pgfplots/util/pgfplotsbinary.code.tex,v 1.13 2009/07/21 18:18:48 ludewich Exp $ |
| 3 |
% |
| 4 |
% Package pgfplots |
| 5 |
% |
| 6 |
% Provides a user-friendly interface to create function plots (normal |
| 7 |
% plots, semi-logplots and double-logplots). |
| 8 |
% |
| 9 |
% It is based on Till Tantau's PGF package. |
| 10 |
% |
| 11 |
% Copyright 2007/2008 by Christian Feuersänger. |
| 12 |
% |
| 13 |
% This program is free software: you can redistribute it and/or modify |
| 14 |
% it under the terms of the GNU General Public License as published by |
| 15 |
% the Free Software Foundation, either version 3 of the License, or |
| 16 |
% (at your option) any later version. |
| 17 |
% |
| 18 |
% This program is distributed in the hope that it will be useful, |
| 19 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
% GNU General Public License for more details. |
| 22 |
% |
| 23 |
% You should have received a copy of the GNU General Public License |
| 24 |
% along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 |
% |
| 26 |
%-------------------------------------------- |
| 27 |
|
| 28 |
% This file provides a self-contained package which does only need |
| 29 |
% pgfkeys. |
| 30 |
% |
| 31 |
% It provides a method to convert TeX numbers (integers and |
| 32 |
% dimensions) into binary format (macros with catcode 11 or 12). |
| 33 |
% |
| 34 |
|
| 35 |
\edef\pgfplotsbinaryatcode{\the\catcode`\@ } |
| 36 |
\catcode`\@=11 |
| 37 |
|
| 38 |
|
| 39 |
% Returns a single character, which has the |
| 40 |
% binary ASCII code '#1', with catcode 11. |
| 41 |
% |
| 42 |
% #1 (expands to) a number between 0 and 255 (inclusive). |
| 43 |
% |
| 44 |
% @see \pgfplotsgetchar Note that \pgfplotsgetchar is more powerful, |
| 45 |
% but can't be used inside of \edef (it is not expandable) whereas |
| 46 |
% \pgfplotscharno is. |
| 47 |
\def\pgfplotscharno#1{\csname pgfp@bin@#1\endcsname}% |
| 48 |
\let\pgfplotscharno@bincatcode=\pgfplotscharno |
| 49 |
|
| 50 |
\def\pgfplotscharno@lualatex#1{#1,} |
| 51 |
|
| 52 |
\input pgfplotsbinary.data.code.tex |
| 53 |
|
| 54 |
% Defines the LUA (!) value pgfplotsretval to be a binary string |
| 55 |
% containing the pgfplots binary value #1. |
| 56 |
% |
| 57 |
% #1 a pgfplots binary value collected with \pgfplotscharno. |
| 58 |
% More precisely, it should be a comma-separated sequence of numbers |
| 59 |
% of the form '0,255,2,128,' (can be terminated by comma). It will be |
| 60 |
% converted to the respective binary numbers 0x0, 0xff, 0x02,.. |
| 61 |
% |
| 62 |
% example: |
| 63 |
% \pgfplotsbinarytoluabinary{0, 255,2,128} |
| 64 |
% \directlua{ |
| 65 |
% pdf.immediateobj{"stream", pgfplotsretval,"/DataWithBinaryStream"} |
| 66 |
% } |
| 67 |
\def\pgfplotsbinarytoluabinary#1{% |
| 68 |
% lualatex does not support binary chars as pdftex does - so we have to resort to LUA |
| 69 |
% methods. The idea is to use |
| 70 |
% string.char(1,2,3) which results in a binary string with chars 0x01, 0x02, 0x03 etc. |
| 71 |
% I only need to get the integer numbers. To this end, I patch \pgfplotscharno |
| 72 |
% and create the binary string here: |
| 73 |
\pgfplotsutil@directlua{% |
| 74 |
pgfplotsretval = pgfplotsGetLuaBinaryStringFromCharIndices({#1}); |
| 75 |
}% |
| 76 |
}% |
| 77 |
|
| 78 |
% Defines \pgfplotsretval to be the ASCII character for #1, with |
| 79 |
% catcode 11. |
| 80 |
% |
| 81 |
% #1: either a number between 0 and 255 (inclusive) or a description |
| 82 |
% of the character. |
| 83 |
% |
| 84 |
% Examples: |
| 85 |
% \pgfplotsgetchar{35} |
| 86 |
% \pgfplotsgetchar{`\#} % code for '#' |
| 87 |
% \pgfplotsgetchar{`\^^M} % Newline |
| 88 |
% \pgfplotsgetchar{`\^^ff}% 255 |
| 89 |
% |
| 90 |
% @see \pgfplotscharno |
| 91 |
\def\pgfplotsgetchar#1{% |
| 92 |
\begingroup |
| 93 |
\count0=#1\relax |
| 94 |
\edef\pgfplotsretval{\pgfplotscharno{\the\count0 }}% |
| 95 |
\pgfmath@smuggleone\pgfplotsretval |
| 96 |
\endgroup |
| 97 |
}% |
| 98 |
|
| 99 |
\def\pgfplotsbinary@apphighorderbytes@BIGENDIAN#1{\xdef\pgfplotsbinaryresult{#1\pgfplotsbinaryresult}}% |
| 100 |
\def\pgfplotsbinary@apphighorderbytes@LITTLEENDIAN#1{\xdef\pgfplotsbinaryresult{\pgfplotsbinaryresult#1}}% |
| 101 |
|
| 102 |
\def\pgfplotsbinaryencode@badic@unsigned@PAD@LITTLEENDIAN{% |
| 103 |
% pad with zeros: |
| 104 |
\ifcase\c@pgfplotsbin@byteno |
| 105 |
% ok. |
| 106 |
\or |
| 107 |
% one byte missing. |
| 108 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST}% |
| 109 |
\or |
| 110 |
% two bytes missing. |
| 111 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 112 |
\or |
| 113 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 114 |
\or |
| 115 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 116 |
\or |
| 117 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 118 |
\or |
| 119 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 120 |
\or |
| 121 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 122 |
\or |
| 123 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO@HIGHEST}% |
| 124 |
\else |
| 125 |
\pgfplots@error{Sorry, I can't process byte no \the\c@pgfplotsbin@byteno... you may need to change bytes=\pgfplotsbinary@bytes.}% |
| 126 |
\fi |
| 127 |
}% |
| 128 |
\def\pgfplotsbinaryencode@badic@unsigned@PAD@BIGENDIAN{% |
| 129 |
% pad with zeros: |
| 130 |
\ifcase\c@pgfplotsbin@byteno |
| 131 |
% ok. |
| 132 |
\or |
| 133 |
% one byte missing. |
| 134 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST}% |
| 135 |
\or |
| 136 |
% two bytes missing. |
| 137 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO}% |
| 138 |
\or |
| 139 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 140 |
\or |
| 141 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 142 |
\or |
| 143 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 144 |
\or |
| 145 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 146 |
\or |
| 147 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 148 |
\or |
| 149 |
\pgfplotsbinary@apphighorderbytes{\pgfplotsbinary@ZERO@HIGHEST\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 150 |
\else |
| 151 |
\pgfplots@error{Sorry, I can't process byte no \the\c@pgfplotsbin@byteno... you may need to change bytes=\pgfplotsbinary@bytes.}% |
| 152 |
\fi |
| 153 |
}% |
| 154 |
|
| 155 |
\expandafter\def\csname pgfplotsbinarysetbytes@1\endcsname{% |
| 156 |
\def\pgfplotsbinary@add@signed@largest@absolute{\advance\c@pgfplotsbin@input by 127 }% |
| 157 |
\def\pgfplotsbinaryencodesignedmaplinearly@prepare{% |
| 158 |
% warning: \pgfplotsbinary@bytes is NOT necessarily 1 (ASCII |
| 159 |
% encoding features) |
| 160 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 161 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 162 |
\divide\c@pgfplotsbin@input by16909320 % ~= (2^31-1) / (2^(8*1-1) -1) |
| 163 |
}% |
| 164 |
}% |
| 165 |
\expandafter\def\csname pgfplotsbinarysetbytes@2\endcsname{% |
| 166 |
\def\pgfplotsbinary@add@signed@largest@absolute{\advance\c@pgfplotsbin@input by 32767 }% |
| 167 |
\def\pgfplotsbinaryencodesignedmaplinearly@prepare{% |
| 168 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 169 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 170 |
\divide\c@pgfplotsbin@input by65538 % ~= (2^31-1) / (2^(8*2-1) -1) |
| 171 |
}% |
| 172 |
}% |
| 173 |
\expandafter\def\csname pgfplotsbinarysetbytes@3\endcsname{% |
| 174 |
\def\pgfplotsbinary@add@signed@largest@absolute{\advance\c@pgfplotsbin@input by 8388607 }% |
| 175 |
\def\pgfplotsbinaryencodesignedmaplinearly@prepare{% |
| 176 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 177 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 178 |
\divide\c@pgfplotsbin@input by256 % ~= (2^31-1) / (2^(8*3-1) -1) |
| 179 |
}% |
| 180 |
}% |
| 181 |
\expandafter\def\csname pgfplotsbinarysetbytes@4\endcsname{% |
| 182 |
\def\pgfplotsbinary@add@signed@largest@absolute{% |
| 183 |
\advance\c@pgfplotsbin@input by 2147483647 % this is the *absolute* largest int that TeX can handle. |
| 184 |
}% |
| 185 |
\def\pgfplotsbinaryencodesignedmaplinearly@prepare{% |
| 186 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 187 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 188 |
}% |
| 189 |
}% |
| 190 |
\def\pgfplotsbinarysetbytes@@{% |
| 191 |
\def\pgfplotsbinaryencodesignedmaplinearly@prepare{% |
| 192 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 193 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 194 |
\pgfplots@error{Sorry, but I can't perform \string\pgfplotsbinaryencodesignedmaplinearly\space for bytes=\pgfplotsbinary@bytes\space yet... bytes=4 is the maximum.}% |
| 195 |
}% |
| 196 |
\def\pgfplotsbinary@add@signed@largest@absolute{ |
| 197 |
\advance\c@pgfplotsbin@input by 2147483647 |
| 198 |
}% |
| 199 |
}% |
| 200 |
\expandafter\let\csname pgfplotsbinary@bytes@5\endcsname=\pgfplotsbinarysetbytes@@ |
| 201 |
\expandafter\let\csname pgfplotsbinary@bytes@6\endcsname=\pgfplotsbinarysetbytes@@ |
| 202 |
\expandafter\let\csname pgfplotsbinary@bytes@7\endcsname=\pgfplotsbinarysetbytes@@ |
| 203 |
\expandafter\let\csname pgfplotsbinary@bytes@8\endcsname=\pgfplotsbinarysetbytes@@ |
| 204 |
|
| 205 |
\pgfqkeys{/pgfplots/bin}{% |
| 206 |
% ordering not yet implemented; uses always BIG ENDIAN. |
| 207 |
ordering/.is choice,% |
| 208 |
ordering/big endian/.code={% |
| 209 |
\def\pgfplotsbinary@byteorder{0}% |
| 210 |
\let\pgfplotsbinary@apphighorderbytes=\pgfplotsbinary@apphighorderbytes@BIGENDIAN |
| 211 |
\let\pgfplotsbinaryencode@badic@unsigned@PAD=\pgfplotsbinaryencode@badic@unsigned@PAD@BIGENDIAN |
| 212 |
},% |
| 213 |
ordering/net/.style={/pgfplots/bin/ordering/big endian},% |
| 214 |
ordering/little endian/.code={% |
| 215 |
\def\pgfplotsbinary@byteorder{1}% |
| 216 |
\let\pgfplotsbinary@apphighorderbytes=\pgfplotsbinary@apphighorderbytes@LITTLEENDIAN |
| 217 |
\let\pgfplotsbinaryencode@badic@unsigned@PAD=\pgfplotsbinaryencode@badic@unsigned@PAD@LITTLEENDIAN |
| 218 |
}, |
| 219 |
ordering/big endian,% |
| 220 |
% |
| 221 |
% The standard method - it results in binary encoded numbers. |
| 222 |
binary encoding/.code={% |
| 223 |
\pgfutil@IfUndefined{directlua}{% |
| 224 |
\let\pgfplotscharno=\pgfplotscharno@bincatcode |
| 225 |
}{% |
| 226 |
% Ah - we use LuaTeX! |
| 227 |
% At the time of this writing, LUA does not allow binary output which has been |
| 228 |
% created by means of catcode modifications & TeX string concatenation. |
| 229 |
% binary output in LUA needs to be (re)implemented in LUA (see inline code |
| 230 |
% comments below). |
| 231 |
% |
| 232 |
% There are two possible work-arounds: |
| 233 |
% (a) Base64 encoding |
| 234 |
% (b) binary encoding using special LUA handling. |
| 235 |
% This is what I do. Set the 'encode filter' such that it reinitializes the encoder: |
| 236 |
% we patch \pgfplotscharno with a special routine which collects |
| 237 |
% only the integer indices: |
| 238 |
\let\pgfplotscharno=\pgfplotscharno@lualatex |
| 239 |
% later, the user has to convert this list into a binary lua |
| 240 |
% string before he can use it. See \pgfplotsbinarytoluabinary |
| 241 |
}% |
| 242 |
\edef\pgfplotsbinary@ZERO{\pgfplotscharno0}% |
| 243 |
\edef\pgfplotsbinary@ZERO@LINEARMAP{\pgfplotscharno{128}}% |
| 244 |
\let\pgfplotsbinary@ZERO@HIGHEST=\pgfplotsbinary@ZERO |
| 245 |
\def\pgfplotsbinary@basis{256}% |
| 246 |
\let\pgfplotsbinary@hook=\relax |
| 247 |
\def\pgfplotsbinary@hook@signed@linearmap{% |
| 248 |
\ifnum\c@pgfplotsbin@byteno=0 |
| 249 |
\advance\c@pgfplotsbin@input by128 |
| 250 |
\ifnum\c@pgfplotsbin@input>255 |
| 251 |
\pgfplotsbinary@hook@signed@linearmap@error |
| 252 |
\fi |
| 253 |
\fi |
| 254 |
}% |
| 255 |
\def\pgfplotsbinarysetbytes##1{% |
| 256 |
\pgfutil@ifundefined{pgfplotsbinarysetbytes@##1}{% |
| 257 |
\pgfplots@error{Sorry, I can't write binary output with '##1' bytes yet...}% |
| 258 |
}{% |
| 259 |
\edef\pgfplotsbinary@bytes{##1}% |
| 260 |
\csname pgfplotsbinarysetbytes@##1\endcsname |
| 261 |
}% |
| 262 |
}% |
| 263 |
},% |
| 264 |
% |
| 265 |
% This applies 'binary encoding' and encodes the resulting bytes |
| 266 |
% in Hex. It corresponds to the ASCIIHexEncode in postscript or |
| 267 |
% pdf. |
| 268 |
% Please note that 'bytes' sets the number of binary bytes - the |
| 269 |
% actual encoding length is exactly twice as large. |
| 270 |
ASCIIHexEncode/.code={% |
| 271 |
\let\pgfplotscharno=\pgfplotscharno@bincatcode |
| 272 |
\edef\pgfplotsbinary@ZERO{\pgfplotscharno{48}}% |
| 273 |
\edef\pgfplotsbinary@ZERO@LINEARMAP{\pgfplotscharno{56}}% |
| 274 |
\let\pgfplotsbinary@ZERO@HIGHEST=\pgfplotsbinary@ZERO |
| 275 |
\pgfkeysalso{/pgfplots/bin/ordering/big endian}% |
| 276 |
\def\pgfplotsbinary@basis{16}% |
| 277 |
\def\pgfplotsbinary@hook@hex{% |
| 278 |
\ifnum\c@pgfplotsbin@input<10 |
| 279 |
\advance\c@pgfplotsbin@input by48 |
| 280 |
\else |
| 281 |
\advance\c@pgfplotsbin@input by55 |
| 282 |
\fi |
| 283 |
}% |
| 284 |
\let\pgfplotsbinary@hook=\pgfplotsbinary@hook@hex |
| 285 |
\def\pgfplotsbinary@hook@signed@linearmap{% |
| 286 |
\ifnum\c@pgfplotsbin@byteno=0 |
| 287 |
\advance\c@pgfplotsbin@input by8 |
| 288 |
\ifnum\c@pgfplotsbin@input>16 |
| 289 |
\pgfplotsbinary@hook@signed@linearmap@error |
| 290 |
\fi |
| 291 |
\fi |
| 292 |
\pgfplotsbinary@hook@hex |
| 293 |
}% |
| 294 |
\def\pgfplotsbinarysetbytes##1{% |
| 295 |
\pgfutil@ifundefined{pgfplotsbinarysetbytes@##1}{% |
| 296 |
\pgfplots@error{Sorry, I can't write binary output with '##1' bytes yet...}% |
| 297 |
}{% |
| 298 |
\csname pgfplotsbinarysetbytes@##1\endcsname |
| 299 |
\begingroup |
| 300 |
\count0=##1\relax |
| 301 |
\multiply\count0 by2 |
| 302 |
\xdef\pgfplotsbinary@glob@TMP{\the\count0 }% |
| 303 |
\endgroup |
| 304 |
\let\pgfplotsbinary@bytes=\pgfplotsbinary@glob@TMP |
| 305 |
}% |
| 306 |
}% |
| 307 |
},% |
| 308 |
% |
| 309 |
% |
| 310 |
% This applies 'binary encoding' and encodes the resulting bytes |
| 311 |
% using a base 85 encoding. It corresponds to the ASCII85Encode in postscript or |
| 312 |
% pdf. |
| 313 |
% Handle this method with care - it works just for ONE number, not for a stream of |
| 314 |
% numbers as in pdf. Therefore, it might not be useful at all. |
| 315 |
% Please note that 'bytes' will be ignored; ASCII85Encode assumes |
| 316 |
% 4 binary bytes and uses 5 bytes to encode them. |
| 317 |
% |
| 318 |
% @ATTENTION bytes is ALWAYS 4, regardless of the setting of |
| 319 |
% 'bytes'! |
| 320 |
ASCII85Encode/.code={% |
| 321 |
\let\pgfplotscharno=\pgfplotscharno@bincatcode |
| 322 |
\edef\pgfplotsbinary@ZERO{\pgfplotscharno{33}}% |
| 323 |
\edef\pgfplotsbinary@ZERO@LINEARMAP{\pgfplotscharno{42}}% |
| 324 |
\let\pgfplotsbinary@ZERO@HIGHEST=\pgfplotsbinary@ZERO |
| 325 |
\pgfkeysalso{/pgfplots/bin/ordering/big endian}% |
| 326 |
\edef\pgfplotsbinary@ASCII@specialzero{\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO\pgfplotsbinary@ZERO}% |
| 327 |
\expandafter\def\expandafter\pgfplotsbinaryencode@badic@unsigned@PAD\expandafter{% |
| 328 |
\pgfplotsbinaryencode@badic@unsigned@PAD |
| 329 |
\ifx\pgfplotsbinaryresult\pgfplotsbinary@ASCII@specialzero |
| 330 |
% PDF standard: 0 is encoded as 'z': |
| 331 |
\gdef\pgfplotsbinaryresult{z}% |
| 332 |
\fi |
| 333 |
}% |
| 334 |
\def\pgfplotsbinary@basis{85}% |
| 335 |
\def\pgfplotsbinary@hook{% |
| 336 |
\advance\c@pgfplotsbin@input by33 |
| 337 |
}% |
| 338 |
\def\pgfplotsbinary@hook@signed@linearmap{% |
| 339 |
\advance\c@pgfplotsbin@input by33 |
| 340 |
\ifnum\c@pgfplotsbin@byteno=0 |
| 341 |
\advance\c@pgfplotsbin@input by42 |
| 342 |
\ifnum\c@pgfplotsbin@input>85 |
| 343 |
\pgfplotsbinary@hook@signed@linearmap@error |
| 344 |
\fi |
| 345 |
\fi |
| 346 |
}% |
| 347 |
% I know, that does only work efficiently if bytes=4 for every |
| 348 |
% encoded number. |
| 349 |
\def\pgfplotsbinarysetbytes##1{% |
| 350 |
\def\pgfplotsbinary@bytes{5}% |
| 351 |
\csname pgfplotsbinarysetbytes@4\endcsname |
| 352 |
}% |
| 353 |
\pgfplotsbinarysetbytes4% |
| 354 |
},% |
| 355 |
binary encoding,% |
| 356 |
% |
| 357 |
bytes/.code={\pgfplotsbinarysetbytes{#1}},% |
| 358 |
bytes=4, |
| 359 |
% |
| 360 |
% Irreversibly change to VERBATIM output for debugging: |
| 361 |
debug mode/.code={% |
| 362 |
\let\pgfplotsbinary@apphighorderbytes@ORIG=\pgfplotsbinary@apphighorderbytes |
| 363 |
\def\pgfplotsbinary@ZERO{[Pad-0]}% |
| 364 |
\let\pgfplotsbinary@ZERO@HIGHEST=\pgfplotsbinary@ZERO |
| 365 |
\def\pgfplotsbinary@ZERO@LINEARMAP{[Pad-128]}% |
| 366 |
\def\pgfplotsbinary@apphighorderbytes##1{% |
| 367 |
\pgfutil@ifnextchar\pgfplotscharno{% |
| 368 |
\pgfplotsbinary@apphighorderbytes@DEBUG@csname |
| 369 |
}{% |
| 370 |
\pgfplotsbinary@apphighorderbytes@DEBUG@normal |
| 371 |
}% |
| 372 |
##1\relax |
| 373 |
}% |
| 374 |
},% |
| 375 |
% Write pdf objects in binary form. This does only work with |
| 376 |
% pdftex, and its output is only useful in conjunction with |
| 377 |
% \pdfcompresslevel=0 |
| 378 |
% and a text editor. |
| 379 |
% Usage: |
| 380 |
% \pgfkeys{/pgfplots/bin/debug to pdf={\pgfplotsbinaryencodeunsigned}{1,2,3,...,16}} |
| 381 |
% |
| 382 |
% works only with pdftex |
| 383 |
debug to pdf/.code 2 args={% |
| 384 |
\foreach \num in {#2} {% |
| 385 |
#1{\num}% |
| 386 |
\immediate \pdfobj stream attr { |
| 387 |
/Decimal \num\space |
| 388 |
/Routine (\string#1) |
| 389 |
} {% |
| 390 |
\pgfplotsbinaryresult |
| 391 |
}% |
| 392 |
}% |
| 393 |
},% |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
\def\pgfplotsbinary@hook@signed@linearmap@error{% |
| 398 |
\pgfplots@error{Sorry, there are not enough bytes to store the current number. I tried to write \the\c@pgfplotsbin@input...}% |
| 399 |
}% |
| 400 |
|
| 401 |
\def\pgfplotsbinary@apphighorderbytes@DEBUG@csname\pgfplotscharno#1\relax{% |
| 402 |
\pgfplotsbinary@apphighorderbytes@ORIG{[#1]}% |
| 403 |
}% |
| 404 |
\def\pgfplotsbinary@apphighorderbytes@DEBUG@normal#1\relax{% |
| 405 |
\pgfplotsbinary@apphighorderbytes@ORIG{#1}% |
| 406 |
}% |
| 407 |
|
| 408 |
%\pgfkeys{/pgfplots/bin/debug mode} |
| 409 |
|
| 410 |
|
| 411 |
\countdef\c@pgfplotsbin@input=0 |
| 412 |
\countdef\c@pgfplotsbin@tmpa=1 |
| 413 |
\countdef\c@pgfplotsbin@tmpb=2 |
| 414 |
\countdef\c@pgfplotsbin@byteno=3 |
| 415 |
\countdef\c@pgfplotsbin@basis=4 |
| 416 |
\def\pgfplotsbinaryempty{} |
| 417 |
|
| 418 |
%-------------------------------------------------- |
| 419 |
% input: unsigned int x, b, n; |
| 420 |
% output: unsigned int y[n]; |
| 421 |
% for (i=0; i<n; i++) y[i] = 0; |
| 422 |
% i=0; |
| 423 |
% while (x > 0) |
| 424 |
% { y[i] = x % b; /* entspricht x mod b */ |
| 425 |
% x = x / b; /* ganzzahlige Division */ |
| 426 |
% i++; |
| 427 |
% } |
| 428 |
%-------------------------------------------------- |
| 429 |
% with x = #1 |
| 430 |
% b = basis |
| 431 |
% will store stuff into \pgfplotsbinaryresult in binary format |
| 432 |
% |
| 433 |
% PRECONDITION: |
| 434 |
% - \pgfplotsbinaryresult= empty! |
| 435 |
% - \c@pgfplotsbin@byteno=\pgfplotsbinary@bytes |
| 436 |
\def\pgfplotsbinaryencode@badic@unsigned@{% |
| 437 |
\ifnum\c@pgfplotsbin@input>0 |
| 438 |
\c@pgfplotsbin@tmpa=\c@pgfplotsbin@input |
| 439 |
\divide\c@pgfplotsbin@tmpa by\c@pgfplotsbin@basis\relax |
| 440 |
\c@pgfplotsbin@tmpb=\c@pgfplotsbin@tmpa\relax |
| 441 |
\multiply\c@pgfplotsbin@tmpa by\c@pgfplotsbin@basis\relax |
| 442 |
\advance\c@pgfplotsbin@input by -\c@pgfplotsbin@tmpa\relax |
| 443 |
\advance\c@pgfplotsbin@byteno by-1 |
| 444 |
\pgfplotsbinary@hook% hooks for modifications. |
| 445 |
\pgfplotsbinary@apphighorderbytes{\pgfplotscharno{\the\c@pgfplotsbin@input}}% |
| 446 |
\c@pgfplotsbin@input=\c@pgfplotsbin@tmpb |
| 447 |
%\message{RESULT SO FAR byte no \the\c@pgfplotsbin@byteno: \pgfplotsbinaryresult}% |
| 448 |
\expandafter\pgfplotsbinaryencode@badic@unsigned@ |
| 449 |
\else |
| 450 |
\pgfplotsbinaryencode@badic@unsigned@PAD |
| 451 |
%\message{RESULT SO FAR byte no \the\c@pgfplotsbin@byteno: \pgfplotsbinaryresult}% |
| 452 |
\fi |
| 453 |
}% |
| 454 |
|
| 455 |
|
| 456 |
% Defines \pgfplotsbinaryresult to be the binary representation of an |
| 457 |
% unsigned integer. |
| 458 |
% |
| 459 |
% The representation will use unsigned dual number representation. |
| 460 |
% |
| 461 |
% The assignment to \pgfplotsbinaryresult will be globally. |
| 462 |
% #1: an unsigned integer. It won't be transformed in any way, so make |
| 463 |
% sure it fits into the configured number of bytes. It is an error if |
| 464 |
% the number is too large or too small. Please note that only unsigned |
| 465 |
% numbers are supported with this method. |
| 466 |
% |
| 467 |
% FIXME : fix > 2^30 |
| 468 |
\def\pgfplotsbinaryencodeunsigned#1{% |
| 469 |
\begingroup |
| 470 |
\global\let\pgfplotsbinaryresult=\pgfplotsbinaryempty |
| 471 |
\c@pgfplotsbin@input=#1 % |
| 472 |
\c@pgfplotsbin@byteno=\pgfplotsbinary@bytes\relax |
| 473 |
\c@pgfplotsbin@basis=\pgfplotsbinary@basis\relax |
| 474 |
\pgfplotsbinaryencode@badic@unsigned@% |
| 475 |
\endgroup |
| 476 |
}% |
| 477 |
|
| 478 |
% An implementation for signed integers which maps the signed integer linearly into |
| 479 |
% the unsigned data range before it proceeds. |
| 480 |
% |
| 481 |
% The idea is thus, to first introduce a linear mapping |
| 482 |
% |
| 483 |
% phi : [- smallest_possible, +largest_possible ] -> [0, 256^bytes-1 ] |
| 484 |
% |
| 485 |
% A signed integer in TeX is in [ - (2^31-1), 2^31-1 ]. |
| 486 |
% Thus, we should map |
| 487 |
% |
| 488 |
% phi : [ -(2^31-1), 2^31-1 ] -> [ 0, 2^32-1 ]. |
| 489 |
% |
| 490 |
% A simpler case is to employ the symmetry in TeX's registers and |
| 491 |
% leave one out, i.e. to map to 2^32-2: |
| 492 |
% |
| 493 |
% phi : [ -(2^31-1), 2^31-1 ] -> [ 0, 2^32-2 ]. |
| 494 |
% |
| 495 |
% Then, |
| 496 |
% |
| 497 |
% phi(x) = ( x + 2^31 -1 ) / (2^31-1 + 2^31-1) * (2^32-2) = x+ 2^31-1. |
| 498 |
% |
| 499 |
% The same map phi(x) = x + 2^31 -1 with target space [0, 2^32-1 ] |
| 500 |
% could be realized with the input space [- (2^31-1), 2^31 ]. |
| 501 |
% |
| 502 |
% I am using this encoding procedure, phi(x) = x + 2^31 -1. |
| 503 |
% |
| 504 |
% As a consequence, the binary pattern FF FF FF FF does never occur as |
| 505 |
% result of the mapping. |
| 506 |
% |
| 507 |
% To invert the mapping (i.e. to decode the result), set up the unique |
| 508 |
% linear map |
| 509 |
% |
| 510 |
% psi : [ 0, 2^32-1 ] -> [ -(2^31-1), 2^31 ]. |
| 511 |
% |
| 512 |
% Then, psi( phi(x) ) = x and the decoding procedure is correct. |
| 513 |
% |
| 514 |
% This doesn't need TeX register arithmetics on the whole range. |
| 515 |
% |
| 516 |
% REMARK: the whole operation does also work if bytes<4 (i.e. we have |
| 517 |
% less than 32 bits in the target range). In this case, the mapping is |
| 518 |
% phi : [ -(2^31-1), 2^31-1 ] -> [ 0, 2^{8*bytes}-1 ] |
| 519 |
% and a further, *lossy* quantization still will be applied. The |
| 520 |
% quantization step is an integer division performed in signed number |
| 521 |
% arithmetics (i.e. it is symmetric around 0). |
| 522 |
\def\pgfplotsbinaryencodesignedmaplinearly#1{% |
| 523 |
\begingroup |
| 524 |
\global\let\pgfplotsbinaryresult=\pgfplotsbinaryempty |
| 525 |
\c@pgfplotsbin@input=#1\relax% |
| 526 |
\pgfplotsbinaryencodesignedmaplinearly@prepare |
| 527 |
\ifnum\c@pgfplotsbin@input<0 % |
| 528 |
% compute + 2^31 - 1 |
| 529 |
\pgfplotsbinary@add@signed@largest@absolute |
| 530 |
\else |
| 531 |
% change zero padding such that positive numbers |
| 532 |
% get the EFFECT of + 2^31. |
| 533 |
\let\pgfplotsbinary@ZERO@HIGHEST=\pgfplotsbinary@ZERO@LINEARMAP |
| 534 |
\let\pgfplotsbinary@hook=\pgfplotsbinary@hook@signed@linearmap |
| 535 |
% and compute the -1 explicitly here: |
| 536 |
\advance\c@pgfplotsbin@input by-1 % |
| 537 |
\fi |
| 538 |
\pgfplotsbinaryencode@badic@unsigned@% |
| 539 |
\endgroup |
| 540 |
}% |
| 541 |
|
| 542 |
% Encodes a dimen (like 1pt or \dimen0) in binary form. |
| 543 |
% |
| 544 |
% The encoding works by mapping #1 linearly into the allowed integer |
| 545 |
% range using a quantization technique to respect the (possibly) |
| 546 |
% restricted number of bytes. |
| 547 |
% |
| 548 |
% The implementation is fast and uses only integer arithmetics. |
| 549 |
% It relies on \pgfplotsbinaryencodesignedmaplinearly and a scale. |
| 550 |
% |
| 551 |
% So, what we do is to setup a linear map into binary range with k |
| 552 |
% bytes. The range of a TeX dimen is precisely (in units of pt) |
| 553 |
% [ -(2^30-1) / 2^16, (2^30 -1) / 2^16 ] = [-16383.99998, 16383.99998] |
| 554 |
% |
| 555 |
% Thus, for an input dimen x, we set up the mapping |
| 556 |
% phi(x) = 2^16 * x * 2 |
| 557 |
% which maps |
| 558 |
% phi: [ -(2^30-1) / 2^16, (2^30-1) / 2^16 ] -> [-(2^31 -2), 2^31-2]. |
| 559 |
% |
| 560 |
% I simply use the \pgfplotsbinaryencodesignedmaplinearly to process |
| 561 |
% this further. To simplify the computation, I simply compute |
| 562 |
% phi_signed( phi(x) ), |
| 563 |
% where phi_signed denotes an application of |
| 564 |
% \pgfplotsbinaryencodesignedmaplinearly: |
| 565 |
% phi_signed( y ) = y +2^31 -1, |
| 566 |
% phi_signed( phi(x) ) = 2^16 * 2 * x + 2^31 - 1. |
| 567 |
% This is NOT a linear map to [0,2^32-1] as promised. |
| 568 |
% But, we can setup an inverse transformation PHI (which is linear) anyway |
| 569 |
% such that |
| 570 |
% PHI( phi_signed(phi(x)) ) = x |
| 571 |
% and that's all I want. Do do that, we use the unique linear decoder map |
| 572 |
% PHI : [ 0,2^32-1 ] -> [ -16383.999992, 16384 ]. |
| 573 |
% |
| 574 |
% This is not exactly the input range of before, but using it results |
| 575 |
% in a proper decoder. The difference is due to the non-unique zero |
| 576 |
% representation in TeX's arithmetics. |
| 577 |
% |
| 578 |
% |
| 579 |
% REMARK: the whole operation does also work if bytes<4 (i.e. we have |
| 580 |
% less than 32 bits in the target range). In this case, a further |
| 581 |
% *lossy* quantization step is applied in phi_signed. The inverse |
| 582 |
% transformations are the same, however. See |
| 583 |
% \pgfplotsbinaryencodesignedmaplinearly for details about the |
| 584 |
% quantization step (or try it out). |
| 585 |
% |
| 586 |
% |
| 587 |
%% DEBUG NOTE: This mapping appears to work correctly according to |
| 588 |
%% my tests. |
| 589 |
%% For bc -l test codes: |
| 590 |
%% ibase=16; |
| 591 |
%% -4000 + 809658FA. / (2^20) * 8000 |
| 592 |
\def\pgfplotsbinaryencodedimenmaplinearly#1{% |
| 593 |
\begingroup |
| 594 |
\dimen0=#1\relax |
| 595 |
\c@pgfplotsbin@input=\dimen0 |
| 596 |
\multiply\c@pgfplotsbin@input by2 |
| 597 |
%\message{LOWLEVEL ENCODING '\the\c@pgfplotsbin@input' with linear map}% |
| 598 |
\pgfplotsbinaryencodesignedmaplinearly\c@pgfplotsbin@input |
| 599 |
\endgroup |
| 600 |
}% |
| 601 |
|
| 602 |
\catcode`\@=\pgfplotsbinaryatcode |
| 603 |
\endinput |