| 1 |
% This file is part of HINT |
| 2 |
% Copyright 2017-2026 Martin Ruckert, Hochschule Muenchen, Lothstrasse 64, 80336 Muenchen |
| 3 |
% |
| 4 |
% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
% of this software and associated documentation files (the "Software"), to deal |
| 6 |
% in the Software without restriction, including without limitation the rights |
| 7 |
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
% copies of the Software, and to permit persons to whom the Software is |
| 9 |
% furnished to do so, subject to the following conditions: |
| 10 |
% |
| 11 |
% The above copyright notice and this permission notice shall be |
| 12 |
% included in all copies or substantial portions of the Software. |
| 13 |
% |
| 14 |
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 |
% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT |
| 19 |
% OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
% THE SOFTWARE. |
| 21 |
% |
| 22 |
% Except as contained in this notice, the name of the copyright holders shall |
| 23 |
% not be used in advertising or otherwise to promote the sale, use or other |
| 24 |
% dealings in this Software without prior written authorization from the |
| 25 |
% copyright holders. |
| 26 |
|
| 27 |
\input btxmac.tex |
| 28 |
\input hintmac.tex |
| 29 |
|
| 30 |
%% defining how to display certain C identifiers |
| 31 |
|
| 32 |
@s int8_t int |
| 33 |
@s uint8_t int |
| 34 |
@s int16_t int |
| 35 |
@s uint16_t int |
| 36 |
@s uint32_t int |
| 37 |
@s int32_t int |
| 38 |
@s uint64_t int |
| 39 |
@s bool int |
| 40 |
|
| 41 |
%% defining hyphenation patterns |
| 42 |
\hyphenation{pa-ra-me-ter} |
| 43 |
|
| 44 |
@ |
| 45 |
|
| 46 |
\makeindex |
| 47 |
\maketoc |
| 48 |
\makecode |
| 49 |
%\makefigindex |
| 50 |
\titletrue |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
\null |
| 55 |
|
| 56 |
\font\largetitlefont=cmssbx10 scaled\magstep4 |
| 57 |
\font\Largetitlefont=cmssbx10 at 40pt |
| 58 |
\font\hugetitlefont=cmssbx10 at 48pt |
| 59 |
\font\smalltitlefontit=cmbxti10 scaled\magstep3 |
| 60 |
\font\smalltitlefont=cmssbx10 scaled\magstep3 |
| 61 |
|
| 62 |
%halftitle |
| 63 |
\def\raggedleft{\leftskip=0pt plus 5em\parfillskip=0pt |
| 64 |
\spaceskip=.3333em \xspaceskip=0.5em \emergencystretch=1em\relax |
| 65 |
\hyphenpenalty=1000\exhyphenpenalty=1000\pretolerance=10000\linepenalty=5000 |
| 66 |
} |
| 67 |
\hbox{} |
| 68 |
\vskip 0pt plus 1fill |
| 69 |
{ \baselineskip=60pt |
| 70 |
\hugetitlefont\hfill HINT:\par |
| 71 |
\Largetitlefont\raggedleft The File Format\par |
| 72 |
} |
| 73 |
\vskip 0pt plus 5fill |
| 74 |
\eject |
| 75 |
% verso of half title |
| 76 |
\titletrue |
| 77 |
\null |
| 78 |
\vfill |
| 79 |
\eject |
| 80 |
|
| 81 |
% title |
| 82 |
\titletrue |
| 83 |
\hbox{} |
| 84 |
\vskip 0pt plus 1fill |
| 85 |
{ |
| 86 |
\baselineskip=1cm\parindent=0pt |
| 87 |
{\largetitlefont\raggedright HINT: The File Format}\par |
| 88 |
\leftline{\smalltitlefont Version 1.4} |
| 89 |
\vskip 10pt plus 0.5fill |
| 90 |
\leftline{\smalltitlefont Reflowable} |
| 91 |
\vskip-3pt |
| 92 |
\leftline{\smalltitlefont Output} |
| 93 |
\vskip-3pt |
| 94 |
\leftline{\smalltitlefont for \TeX} |
| 95 |
\vskip 10pt plus 0.5fill |
| 96 |
\hskip 0pt plus 2fill{\it F\"ur meine Mutter}\hskip 0pt plus 0.5fill\hbox{} |
| 97 |
\bigskip |
| 98 |
\vskip 10pt plus 3fill |
| 99 |
\raggedright\baselineskip=12pt |
| 100 |
{\bf MARTIN RUCKERT} \ {\it Munich University of Applied Sciences}\par |
| 101 |
\bigskip |
| 102 |
\leftline{Second edition} |
| 103 |
\bigskip |
| 104 |
% \leftline{\bf Eigendruck im Selbstverlag} |
| 105 |
% \bigskip |
| 106 |
} |
| 107 |
\eject |
| 108 |
|
| 109 |
% verso of title |
| 110 |
% copyright page (ii) |
| 111 |
\titletrue |
| 112 |
\begingroup |
| 113 |
\figrm |
| 114 |
\parindent=0pt |
| 115 |
%\null |
| 116 |
{\raggedright\advance\rightskip 3.5pc |
| 117 |
The author has taken care in the preparation of this book, |
| 118 |
but makes no expressed or implied warranty of any kind and assumes no |
| 119 |
responsibility for errors or omissions. No liability is assumed for |
| 120 |
incidental or consequential damages in connection with or arising out |
| 121 |
of the use of the information or programs contained herein. |
| 122 |
|
| 123 |
\bigskip |
| 124 |
{\figtt\obeylines\obeyspaces\baselineskip=11pt |
| 125 |
Ruckert, Martin. |
| 126 |
HINT: The File Format |
| 127 |
Includes index. |
| 128 |
ISBN 979-854992684-4 |
| 129 |
} |
| 130 |
\bigskip |
| 131 |
|
| 132 |
{\raggedright\advance\rightskip 3.5pc |
| 133 |
\def\:{\discretionary{}{}{}} |
| 134 |
Internet page {\tt http:\://hint.\:userweb.\:mwn.\:de/\:hint/\:format.html} |
| 135 |
may contain current information about this book, downloadable software, |
| 136 |
and news. |
| 137 |
|
| 138 |
\vfill |
| 139 |
Copyright $\copyright$ 2019, 2021 by Martin Ruckert |
| 140 |
\smallskip |
| 141 |
All rights reserved. |
| 142 |
Printed by Kindle Direct Publishing. |
| 143 |
This publication is protected by copyright, and permission must be |
| 144 |
obtained prior to any prohibited reproduction, storage in |
| 145 |
a~retrieval system, or transmission in any form or by any means, electronic, |
| 146 |
mechanical, photocopying, recording, or likewise. |
| 147 |
To obtain permission to use material from this work, please submit a written |
| 148 |
request to Martin Ruckert, |
| 149 |
Hochschule M\"unchen, |
| 150 |
Fakult\"at f\"ur Informatik und Mathematik, |
| 151 |
Lothstrasse 64, |
| 152 |
80335 M\"unchen, |
| 153 |
Germany. |
| 154 |
\medskip |
| 155 |
{\tt ruckert\:@@cs.hm.edu} |
| 156 |
\medskip |
| 157 |
ISBN-13: 979-854992684-4\par |
| 158 |
\medskip |
| 159 |
First printing: August 2019\par |
| 160 |
Second edition: August 2021\par |
| 161 |
\medskip |
| 162 |
Last commit: Mon Jan 19 09:58:39 2026 |
| 163 |
\par |
| 164 |
} |
| 165 |
} |
| 166 |
\endgroup |
| 167 |
|
| 168 |
|
| 169 |
\frontmatter |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
\plainsection{Preface} |
| 174 |
Late in summer 2017, with my new \CEE\ based {\tt cweb} implementation |
| 175 |
of \TeX\cite{Knuth:tex} in hand\cite{MR:webtocweb}\cite{MR:tug38}\cite{MR:web2w}, I started to write |
| 176 |
the first prototype of the \HINT\ viewer. I basically made two copies |
| 177 |
of \TeX: In the first copy, I replaced the |build_page| procedure by |
| 178 |
an output routine which used more or less the printing routines |
| 179 |
already available in \TeX. This was the beginning of the |
| 180 |
\HINT\ file format. |
| 181 |
In the second copy, I replaced \TeX's main loop by an input routine |
| 182 |
that would feed the \HINT\ file more or less directly to \TeX's |
| 183 |
|build_page| procedure. And after replacing \TeX's |ship_out| |
| 184 |
procedure by a modified rendering routine of a dvi viewer that I had |
| 185 |
written earlier for my experiments with \TeX's Computer Modern |
| 186 |
fonts\cite{MR:tug37}, I had my first running \HINT\ viewer. My |
| 187 |
sabbatical during the following Fall term gave me time for ``rapid |
| 188 |
prototyping'' various features that I considered necessary for |
| 189 |
reflowable \TeX\ output\cite{MR:tug39}. |
| 190 |
|
| 191 |
The textual output format derived from the original \TeX\ debugging |
| 192 |
routines proved to be insufficient when I implemented a ``page up'' |
| 193 |
button because it did not support reading the page content |
| 194 |
``backwards''. As a consequence, I developed a compact binary file |
| 195 |
format that could be parsed easily in both directions. The \HINT\ |
| 196 |
short file format was born. I stopped an initial attempt at |
| 197 |
eliminating the old textual format because it was so much nicer when |
| 198 |
debugging. Instead, I converted the long textual format into the short |
| 199 |
binary format as a preliminary step in the viewer. This was not a long |
| 200 |
term solution. When opening a big file, as produced from a 1000 |
| 201 |
pages \TeX\ file, the parsing took several seconds before the first |
| 202 |
page would appear on screen. This delay, observed on a fast desktop |
| 203 |
PC, is barley tolerable, and the delay one would expect on a low-cost, |
| 204 |
low-power, mobile device seemed prohibitive. The consequence is |
| 205 |
simple: The viewer will need an input file in the short format; and to |
| 206 |
support debugging (or editing), separate programs are needed to |
| 207 |
translate the short format into the long format and back again. But |
| 208 |
for the moment, I did not bother to implement any of this but |
| 209 |
continued with unrestricted experimentation. |
| 210 |
|
| 211 |
With the beginning of the Spring term 2018, I stopped further |
| 212 |
experiments with the \HINT\ viewer and decided that I had to write |
| 213 |
down a clean design of the \HINT\ file format. Or of both file |
| 214 |
formats? Professors are supposed to do research, and hence I tried an |
| 215 |
experiment: Instead of writing down a traditional language |
| 216 |
specification, I decided to stick with the ``literate programming'' |
| 217 |
paradigm\cite{Knuth:lp} and write the present book. It describes and implements |
| 218 |
the \.{stretch} and \.{shrink} programs translating one file format |
| 219 |
into the other. As a side effect, it contains the underlying language |
| 220 |
specification. Whether this experiment is a success as a language |
| 221 |
specification remains to be seen, and you should see for yourself. But |
| 222 |
the only important measure for the value of a scientific experiment is |
| 223 |
how much you can learn form it---and I learned a lot. |
| 224 |
|
| 225 |
The whole project turned out to be much more difficult than I had |
| 226 |
expected. Early on, I decided that I would use a recursive descent |
| 227 |
parser for the short format and an LR($k$) parser for the long |
| 228 |
format. Of course, I would use {\tt lex}/{\tt flex} and {\tt yacc}/{\tt bison} |
| 229 |
to generate the LR($k$) parser, and so I had to extend the {\tt |
| 230 |
cweb} tools\cite{Knuth:cweb} to support the corresponding source files. |
| 231 |
|
| 232 |
About in mid May, after writing down about 100 pages, the first |
| 233 |
problems emerged that could not be resolved with my current |
| 234 |
approach. I had started to describe font definitions containing |
| 235 |
definitions of the interword glue and the default hyphen, and the |
| 236 |
declarative style of my exposition started to conflict with the |
| 237 |
sequential demands of writing an output file. So it was time for a |
| 238 |
first complete redesign. Two more passes over the whole book were |
| 239 |
necessary to find the concepts and the structure that would allow me |
| 240 |
to go forward and complete the book as you see it now. |
| 241 |
|
| 242 |
While rewriting was on its way, many ``nice ideas'' were pruned from |
| 243 |
the book. For example, the initial idea of optimizing the \HINT\ file |
| 244 |
while translating it was first reduced to just gathering statistics |
| 245 |
and then disappeared completely. The added code and complexity was |
| 246 |
just too distracting. |
| 247 |
|
| 248 |
What you see before you is still a snapshot of the \HINT\ file format |
| 249 |
because its development is still under way. We will know what |
| 250 |
features are needed for a reflowable \TeX\ file format only after many |
| 251 |
people have started using the format. To use the format, the end-user |
| 252 |
will need implementations, and the implementer will need a language |
| 253 |
specification. The present book is the first step in an attempt to |
| 254 |
solve this ``chicken or egg'' dilemma. |
| 255 |
|
| 256 |
|
| 257 |
\vskip 1cm |
| 258 |
\noindent {\it M\"unchen\hfil\break |
| 259 |
August 20, 2019 \hfill Martin Ruckert} |
| 260 |
|
| 261 |
|
| 262 |
\tableofcontent |
| 263 |
%\thefigindex |
| 264 |
|
| 265 |
|
| 266 |
\mainmatter |
| 267 |
|
| 268 |
\section{Introduction}\label{intro} |
| 269 |
This book defines a file format for reflowable text. |
| 270 |
Actually it describes two file formats: a long format |
| 271 |
that optimizes readability for human beings, and |
| 272 |
a short format that optimizes readability for machines |
| 273 |
and the use of storage space. Both formats use the concept of nodes and lists of |
| 274 |
nodes to describe the file content. Programs that process these nodes |
| 275 |
will likely want to convert the compressed binary representation of a |
| 276 |
node---the short format---or the lengthy textual representation of a |
| 277 |
node---the long format---into a convenient internal representation. |
| 278 |
So most of what follows is just a description of these nodes: their short format, |
| 279 |
their long format and sometimes their internal representation. |
| 280 |
Where as the description of the long and short external format is part |
| 281 |
of the file specification, the description of the internal representation |
| 282 |
is just informational. Different internal representations can be chosen |
| 283 |
based on the individual needs of the program. |
| 284 |
|
| 285 |
While defining the format, I illustrate the processing of long and short format |
| 286 |
files by implementing two utilities: \.{shrink} and \.{stretch}. |
| 287 |
\.{shrink} converts the long format into the short format and \.{stretch} |
| 288 |
goes the other way. |
| 289 |
|
| 290 |
There is also a prototype viewer for this |
| 291 |
file format and a special version of \TeX\cite{DK:texbook} to produce output |
| 292 |
in this format. Both are not described here; a survey describing |
| 293 |
them can be found in \cite{MR:tug39}. |
| 294 |
|
| 295 |
\subsection{Glyphs} |
| 296 |
Let's start with a simple and very common kind of node: a node describing |
| 297 |
a character. |
| 298 |
Because we describe a format that is used to display text, |
| 299 |
we are not so much interested in the |
| 300 |
character itself but we are interested in the specific glyph\index{glyph}. |
| 301 |
In typography, a glyph is a unique mark to be placed on the page representing |
| 302 |
a character. For example the glyph representing the character `a' can have |
| 303 |
many forms among them `{\it a\/}', `{\bf a}', or `{\tenss a}'. |
| 304 |
Such glyphs come in collections, called fonts, representing every character |
| 305 |
of the alphabet in a consistent way. |
| 306 |
|
| 307 |
The long format of a node describing the glyph `a' |
| 308 |
might look like this:`` \.{<glyph} \.{97} \.{*1>}''. |
| 309 |
Here ``\.{97}'' is the character code which |
| 310 |
happens to be the ASCII code of the letter `a' and ``{\tt *1}'' is a font reference |
| 311 |
that stands for ``Computer Modern Roman 10pt''. |
| 312 |
Reference numbers, as you can see, |
| 313 |
start with an asterisk reminiscent of references in the \CEE\ programming language. |
| 314 |
The asterisk enables us to distinguish between ordinary numbers like ``\.{1}'' and references like ``{\tt *1}''. |
| 315 |
|
| 316 |
To make this node more readable, we will see in section~\secref{chars} that it is also |
| 317 |
possible to write `` \.{<glyph 'a' *1 (cmr10)>}''. |
| 318 |
The latter form uses a comment ``\.{(cmr10)}'', enclosed in parentheses, to |
| 319 |
give an indication of what kind of font happens to be font 1, and it uses ``\.{'a'}'', |
| 320 |
the character enclosed in single quotes to denote the ASCII code of `a'. |
| 321 |
But let's keep things simple for now and stick with the decimal notation of the character code. |
| 322 |
|
| 323 |
The rest is common for all nodes: a keyword, here ``\.{glyph}'', and a pair of pointed brackets ``\.{<}\dots\.{>}''. |
| 324 |
|
| 325 |
Internally, we represent a glyph by the font number |
| 326 |
and the character number or character code. |
| 327 |
To store the internal representation of a glyph node, |
| 328 |
we define an appropriate structure type, named after the node with an uppercase first letter. |
| 329 |
@<hint types@>= |
| 330 |
typedef struct {@+ uint32_t c;@+ uint8_t f; @+} Glyph; |
| 331 |
@ |
| 332 |
|
| 333 |
Let us now look at the program \.{shrink} and see how it will convert the long format description |
| 334 |
to the internal representation of the glyph and finally to a short format description. |
| 335 |
|
| 336 |
|
| 337 |
\subsection{Scanning the Long Format} |
| 338 |
First, \.{shrink} reads the input file and extracts a sequence of |
| 339 |
tokens. This is called ``scanning''\index{scanning}. We generate the |
| 340 |
procedure to do the scanning using the program |
| 341 |
\.{flex}\cite{JL:flexbison}\index{flex+{\tt flex}} which is the GNU |
| 342 |
version of the common UNIX tool \.{lex}\cite{JL:lexyacc}\index{lex+{\tt lex}}. |
| 343 |
|
| 344 |
The input to \.{flex} is a list of pattern/\kern -1pt action rules |
| 345 |
where the pattern is a regular expression and the action is a piece of |
| 346 |
\CEE\ code. Most of the time, the \CEE\ code is very simple: it just |
| 347 |
returns the right token\index{token} number to the parser which we |
| 348 |
consider shortly. |
| 349 |
|
| 350 |
The code that defines the tokens will be marked with a line ending in |
| 351 |
``\redsymbol''. This symbol\index{symbol} stands for ``{\it Reading |
| 352 |
the long format\/}''. These code sequences define the syntactical |
| 353 |
elements of the long format and at the same time implement the reading |
| 354 |
process. All sections where that happens are preceded by a similar |
| 355 |
heading and for reference they are conveniently listed together |
| 356 |
starting on page~\pageref{codeindex}. |
| 357 |
|
| 358 |
\codesection{\redsymbol}{Reading the Long Format}\redindex{1}{2}{Glyphs} |
| 359 |
@s START symbol |
| 360 |
@s END symbol |
| 361 |
@s GLYPH symbol |
| 362 |
@s UNSIGNED symbol |
| 363 |
@s REFERENCE symbol |
| 364 |
|
| 365 |
@<symbols@>= |
| 366 |
%token START "<" |
| 367 |
%token END ">" |
| 368 |
%token GLYPH "glyph" |
| 369 |
%token <u> UNSIGNED |
| 370 |
%token <u> REFERENCE |
| 371 |
@ |
| 372 |
You might notice that a small caps font is used for |START|, |END| or |GLYPH|. |
| 373 |
These are ``terminal symbols'' or ``tokens''. |
| 374 |
Next are the scanning rules which define the connection between tokens and their |
| 375 |
textual representation. |
| 376 |
|
| 377 |
@<scanning rules@>= |
| 378 |
::@="<"@> :< SCAN_START; return START; >: |
| 379 |
::@=">"@> :< SCAN_END; return END; >: |
| 380 |
::@=glyph@> :< return GLYPH; >: |
| 381 |
::@=0|[1-9][0-9]*@> :< SCAN_UDEC(yytext); return UNSIGNED; >: |
| 382 |
::@=\*(0|[1-9][0-9]*)@> :< SCAN_UDEC(yytext+1); return REFERENCE; >: |
| 383 |
::@=[[:space:]]@> :< ; >: |
| 384 |
::@=\([^()\n]*[)\n]@> :< ; >: |
| 385 |
@ |
| 386 |
|
| 387 |
As we will see later, the macros starting with |SCAN_|\dots\ are scanning macros. |
| 388 |
Here |SCAN_UDEC| is a macro that converts the decimal representation |
| 389 |
that did match the given pattern to an unsigned integer value; it is explained in |
| 390 |
section~\secref{integers}. |
| 391 |
The macros |SCAN_START| and |SCAN_END| are explained in section~\secref{text}. |
| 392 |
|
| 393 |
|
| 394 |
The action ``{\tt ;}'' is a ``do nothing'' action; here it causes spaces or comments\index{comment} |
| 395 |
to be ignored. Comments start with an opening parenthesis and are terminated by a |
| 396 |
closing parenthesis or the end of line character. |
| 397 |
The pattern ``\.{[\^()\\n]}'' is a negated |
| 398 |
character class that matches all characters except parentheses and the newline |
| 399 |
character. These are not allowed inside comments. For detailed information about |
| 400 |
the patterns used in a \.{flex} program, see the \.{flex} user manual\cite{JL:flexbison}. |
| 401 |
|
| 402 |
\subsection{Parsing the Long Format} |
| 403 |
\label{parse_glyph} |
| 404 |
Next, the tokens produced by the scanner are assembled into larger entities. |
| 405 |
This is called ``parsing''\index{parsing}. |
| 406 |
We generate the procedure to do the parsing using the program \.{bison}\cite{JL:flexbison}\index{bison+{\tt bison}} which is |
| 407 |
the GNU version of the common UNIX tool \.{yacc}\cite{JL:lexyacc}\index{yacc+{\tt yacc}}. |
| 408 |
|
| 409 |
The input to \.{bison} is a list of parsing rules, called a ``grammar''\index{grammar}. |
| 410 |
The rules describe how to build larger entities from smaller entities. |
| 411 |
For a simple glyph node like `` \.{<glyph 97 *1>}'', we need just these rules: |
| 412 |
\codesection{\redsymbol}{Reading the Long Format}%\redindex{1}{2}{Glyphs} |
| 413 |
@s content_node symbol |
| 414 |
@s node symbol |
| 415 |
@s glyph symbol |
| 416 |
@s Glyph int |
| 417 |
@s start symbol |
| 418 |
@<symbols@>= |
| 419 |
%type <u> start |
| 420 |
%type <c> glyph |
| 421 |
@ |
| 422 |
|
| 423 |
@<parsing rules@>=@/ |
| 424 |
glyph: UNSIGNED REFERENCE @/{ $$.c=$1; REF(font_kind,$2); $$.f=$2; }; |
| 425 |
content_node: start GLYPH glyph END { hput_tags($1,hput_glyph(&($3))); }; |
| 426 |
start: START {HPUTNODE; $$=(uint32_t)(hpos++-hstart);} |
| 427 |
@ |
| 428 |
|
| 429 |
You might notice that a slanted font is used for |glyph|, |content_node|, or |start|. |
| 430 |
These are ``nonterminal symbols' and occur on the left hand side of a rule. On the |
| 431 |
right hand side of a rule you find nonterminal symbols, as well as terminal\index{terminal symbol} symbols |
| 432 |
and \CEE\ code enclosed in braces. |
| 433 |
|
| 434 |
Within the \CEE\ code, the expressions |$1| and |$2| refer to the variables on the parse stack |
| 435 |
that are associated with the first and second symbol on the right hand side of the rule. |
| 436 |
In the case of our glyph node, these will be the values 97 and 1, respectively, as produced |
| 437 |
by the macro |SCAN_UDEC|. |
| 438 |
|$$| refers to the variable associated with the left hand side of the rule. |
| 439 |
These variables contain the internal representation of the object in question. |
| 440 |
The type of the variable is specified by a mandatory {\bf token} or optional {\bf type} clause |
| 441 |
when we define the symbol. |
| 442 |
In the above {\bf type} clause for |start| and |glyph| , the identifiers |u| and |c| refer to |
| 443 |
the |union| declaration of the parser (see page~\pageref{union}) |
| 444 |
where we find |uint32_t u| and |Glyph c|. The macro |REF| tests a reference number for |
| 445 |
its valid range. |
| 446 |
|
| 447 |
|
| 448 |
Reading a node is usually split into the following sequence of steps: |
| 449 |
\itemize |
| 450 |
\item Reading the node specification, here a |glyph| |
| 451 |
consisting of an |UNSIGNED| value and a |REFERENCE| value. |
| 452 |
\item Creating the internal representation in the variable |$$| |
| 453 |
based on the values of |$1|, |$2|, \dots\ Here the character |
| 454 |
code field |c| is initialized using the |UNSIGNED| value |
| 455 |
stored in |$1| and the font field |f| is initialized using |
| 456 |
|$2| after checking the reference number for the proper range. |
| 457 |
\item A |content_node| rule explaining that |start| is followed by |GLYPH|, |
| 458 |
the keyword that directs the parser to |glyph|, the |
| 459 |
node specification, and a final |END|. |
| 460 |
\item Parsing |start|, which is defined as the token |START| will assign |
| 461 |
to the corresponding variable |p| on the parse stack the current |
| 462 |
position |hpos| in the output and increments that position |
| 463 |
to make room for the start byte, which we will discuss shortly. |
| 464 |
\item At the end of the |content_node| rule, the \.{shrink} program calls |
| 465 |
a {\it hput\_\dots\/} function, here |hput_glyph|, to write the short |
| 466 |
format of the node as given by its internal representation to the output |
| 467 |
and return the correct tag value. |
| 468 |
\item Finally the |hput_tags| function will add the tag as a start byte and end byte |
| 469 |
to the output stream. |
| 470 |
\enditemize |
| 471 |
|
| 472 |
Now let's see how writing the short format works in detail. |
| 473 |
|
| 474 |
|
| 475 |
\subsection{Writing the Short Format} |
| 476 |
A content node in short form begins with a start\index{start byte} |
| 477 |
byte. It tells us what kind of node it is. To describe the content of |
| 478 |
a short \HINT\ file, 32 different kinds\index{kind} of nodes are |
| 479 |
defined. Hence the kind of a node can be stored in 5 bits and the |
| 480 |
remaining bits of the start byte can be used to contain a 3 bit |
| 481 |
``info''\index{info} value. |
| 482 |
|
| 483 |
We define an enumeration type to give symbolic names to the |
| 484 |
kind-values. The exact numerical values are of no specific |
| 485 |
importance; we will see in section~\secref{text}, however, that the |
| 486 |
assignment chosen below, has certain advantages. |
| 487 |
|
| 488 |
Because the usage of kind-values in content nodes is slightly |
| 489 |
different from the usage in definition nodes, we define alternative |
| 490 |
names for some kind-values. To display readable names instead of |
| 491 |
numerical values when debugging, we define two arrays of strings as |
| 492 |
well. Keeping the definitions consistent is achieved by creating all |
| 493 |
definitions from the same list of identifiers using different |
| 494 |
definitions of the macro |DEF_KIND|. |
| 495 |
|
| 496 |
@<hint basic types@>= |
| 497 |
#define DEF_KIND(C,D,N) @[C##_kind=N@] |
| 498 |
typedef enum {@+@<kinds@>@+,@+ @<alternative kind names@> @+} Kind; |
| 499 |
#undef DEF_KIND |
| 500 |
@ |
| 501 |
|
| 502 |
@<define |content_name| and |definition_name|@>= |
| 503 |
|
| 504 |
#define DEF_KIND(C,D,N) @[#C@] |
| 505 |
const char *content_name[32]=@+{@+@<kinds@>@;@+}@+; |
| 506 |
#undef DEF_KIND@# |
| 507 |
#define DEF_KIND(C,D,N) @[#D@] |
| 508 |
const char *definition_name[0x20]=@+{@+@<kinds@>@;@+}@+; |
| 509 |
#undef DEF_KIND |
| 510 |
@ |
| 511 |
|
| 512 |
@<print |content_name| and |definition_name|@>= |
| 513 |
printf("const char *content_name[32]={"); |
| 514 |
for (k=0; k<= 31;k++) |
| 515 |
{ printf("\"%s\"",content_name[k]); |
| 516 |
if (k<31) printf(", "); |
| 517 |
} |
| 518 |
printf("};\n\n"); |
| 519 |
printf("const char *definition_name[32]={"); |
| 520 |
for (k=0; k<= 31;k++) |
| 521 |
{ printf("\"%s\"",definition_name[k]); |
| 522 |
if (k<31) printf(", "); |
| 523 |
} |
| 524 |
printf("};\n\n"); |
| 525 |
@ |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
\goodbreak |
| 533 |
\index{glyph kind+\\{glyph\_kind}} |
| 534 |
\index{font kind+\\{font\_kind}} |
| 535 |
\index{penalty kind+\\{penalty\_kind}} |
| 536 |
\index{int kind+\\{int\_kind}} |
| 537 |
\index{kern kind+\\{kern\_kind}} |
| 538 |
\index{xdimen kind+\\{xdimen\_kind}} |
| 539 |
\index{ligature kind+\\{ligature\_kind}} |
| 540 |
\index{disc kind+\\{disc\_kind}} |
| 541 |
\index{glue kind+\\{glue\_kind}} |
| 542 |
\index{language kind+\\{language\_kind}} |
| 543 |
\index{rule kind+\\{rule\_kind}} |
| 544 |
\index{image kind+\\{image\_kind}} |
| 545 |
\index{baseline kind+\\{baseline\_kind}} |
| 546 |
\index{dimen kind+\\{dimen\_kind}} |
| 547 |
\index{hbox kind+\\{hbox\_kind}} |
| 548 |
\index{vbox kind+\\{vbox\_kind}} |
| 549 |
\index{par kind+\\{par\_kind}} |
| 550 |
\index{math kind+\\{math\_kind}} |
| 551 |
\index{table kind+\\{table\_kind}} |
| 552 |
\index{item kind+\\{item\_kind}} |
| 553 |
\index{hset kind+\\{hset\_kind}} |
| 554 |
\index{vset kind+\\{vset\_kind}} |
| 555 |
\index{hpack kind+\\{hpack\_kind}} |
| 556 |
\index{vpack kind+\\{vpack\_kind}} |
| 557 |
\index{stream kind+\\{stream\_kind}} |
| 558 |
\index{page kind+\\{page\_kind}} |
| 559 |
\index{range kind+\\{range\_kind}} |
| 560 |
\index{adjust kind+\\{adjust\_kind}} |
| 561 |
\index{param kind+\\{param\_kind}} |
| 562 |
\index{list kind+\\{list\_kind}} |
| 563 |
\label{kinddef} |
| 564 |
@<kinds@>= |
| 565 |
DEF_KIND(l@&ist,l@&ist,0),@/ |
| 566 |
DEF_KIND(p@&aram,p@&aram,1),@/ |
| 567 |
DEF_KIND(r@&ange,r@&ange,2),@/ |
| 568 |
DEF_KIND(x@&dimen,x@&dimen,3),@/ |
| 569 |
DEF_KIND(a@&djust,a@&djust,4),@/ |
| 570 |
DEF_KIND(g@&lyph, f@&ont,5),@/ |
| 571 |
DEF_KIND(k@&ern,d@&imen,6),@/ |
| 572 |
DEF_KIND(g@&lue,g@&lue,7),@/ |
| 573 |
DEF_KIND(l@&igature,l@&igature,8),@/ |
| 574 |
DEF_KIND(d@&isc,d@&isc,9),@/ |
| 575 |
DEF_KIND(l@&anguage,l@&anguage,10),@/ |
| 576 |
DEF_KIND(r@&ule,r@&ule,11),@/ |
| 577 |
DEF_KIND(i@&mage,i@&mage,12),@/ |
| 578 |
DEF_KIND(l@&eaders,l@&eaders,13),@/ |
| 579 |
DEF_KIND(b@&aseline,b@&aseline,14),@/ |
| 580 |
DEF_KIND(h@&b@&ox,h@&b@&ox,15),@/ |
| 581 |
DEF_KIND(v@&b@&ox,v@&b@&ox,16),@/ |
| 582 |
DEF_KIND(p@&ar,p@&ar,17),@/ |
| 583 |
DEF_KIND(m@&ath,m@&ath,18),@/ |
| 584 |
DEF_KIND(t@&able,t@&able,19),@/ |
| 585 |
DEF_KIND(i@&tem,i@&tem,20),@/ |
| 586 |
DEF_KIND(h@&set,h@&set,21),@/ |
| 587 |
DEF_KIND(v@&set,v@&set,22),@/ |
| 588 |
DEF_KIND(h@&pack,h@&pack,23),@/ |
| 589 |
DEF_KIND(v@&pack,v@&pack,24),@/ |
| 590 |
DEF_KIND(s@&tream,s@&tream,25),@/ |
| 591 |
DEF_KIND(p@&age,p@&age,26),@/ |
| 592 |
DEF_KIND(l@&ink,l@&abel,27),@/ |
| 593 |
DEF_KIND(c@&olor,c@&olor,28),@/ |
| 594 |
DEF_KIND(u@&ndefined1,u@&ndefined1,29),@/ |
| 595 |
DEF_KIND(u@&ndefined2,u@&ndefined2,30),@/ |
| 596 |
DEF_KIND(p@&enalty, i@&nt,31) |
| 597 |
@t@> |
| 598 |
@ |
| 599 |
|
| 600 |
For a few kind-values we have |
| 601 |
alternative names; we will use them |
| 602 |
to express different intentions when using them. |
| 603 |
@<alternative kind names@>= |
| 604 |
font_kind=glyph_kind,int_kind=penalty_kind, unknown_kind=penalty_kind, |
| 605 |
dimen_kind=kern_kind, label_kind=link_kind, outline_kind=link_kind@/@t{}@> |
| 606 |
@ |
| 607 |
|
| 608 |
The info\index{info value} values can be used to represent numbers |
| 609 |
in the range 0 to 7; for an example |
| 610 |
see the |hput_glyph| function later in this section. |
| 611 |
Mostly, however, the individual bits are used as flags indicating the presence |
| 612 |
or absence of immediate parameter values. If the info bit is set, it |
| 613 |
means the corresponding parameter is present as an immediate value; if it |
| 614 |
is zero, it means that there is no immediate parameter value present, and |
| 615 |
the node specification will reveal what value to use instead. |
| 616 |
In some cases there is a common default value that can be used, in other |
| 617 |
cases a one byte reference number is used to select a predefined value. |
| 618 |
|
| 619 |
To make the binary |
| 620 |
representation of the info bits more readable, we define an |
| 621 |
enumeration type. |
| 622 |
|
| 623 |
\index{b000+\\{b000}} |
| 624 |
\index{b001+\\{b001}} |
| 625 |
\index{b010+\\{b010}} |
| 626 |
\index{b011+\\{b011}} |
| 627 |
\index{b100+\\{b100}} |
| 628 |
\index{b101+\\{b101}} |
| 629 |
\index{b110+\\{b110}} |
| 630 |
\index{b111+\\{b111}} |
| 631 |
@<hint basic types@>= |
| 632 |
typedef enum {@+ b000=0,b001=1,b010=2,b011=3,b100=4,b101=5,b110=6,b111=7@+ } Info; |
| 633 |
@ |
| 634 |
|
| 635 |
|
| 636 |
After the start byte follows the node content and it is the purpose of |
| 637 |
the start byte to reveal the exact syntax and semantics of the node |
| 638 |
content. Because we want to be able to read the short form of a \HINT\ |
| 639 |
file in forward direction and in backward direction, the start byte is |
| 640 |
duplicated after the content as an end\index{end byte} byte. |
| 641 |
|
| 642 |
|
| 643 |
We store a kind and an info value in one byte and call this a tag. |
| 644 |
@<hint basic types@>= |
| 645 |
typedef uint8_t Tag; |
| 646 |
@ |
| 647 |
|
| 648 |
The following macros are used to assemble and disassemble tags:\index{TAG+\.{TAG}} |
| 649 |
@<hint macros@>= |
| 650 |
#define @[KIND(T)@] (((T)>>3)&0x1F) |
| 651 |
#define @[NAME(T)@] @[content_name[KIND(T)]@] |
| 652 |
#define @[INFO(T)@] ((T)&0x7) |
| 653 |
#define @[TAG(K,I)@] (((K)<<3)|(I)) |
| 654 |
@ |
| 655 |
|
| 656 |
Writing a short format \HINT\ file is implemented by a collection of {\it hput\_\kern 1pt\dots\/} functions; |
| 657 |
they follow most of the time the same schema: |
| 658 |
\itemize |
| 659 |
\item First, we define a variable for |info|. |
| 660 |
\item Then follows the main part of the function body, where we |
| 661 |
decide on the output format, do the actual output and set the |info| value accordingly. |
| 662 |
\item We combine the info value with the kind-value and return the correct tag. |
| 663 |
\item The tag value will be passed to |hput_tags| which generates |
| 664 |
debugging information, if requested, and stores the tag before and after the node content. |
| 665 |
\enditemize |
| 666 |
|
| 667 |
|
| 668 |
After these preparations, we turn our attention again to the |hput_glyph| function. |
| 669 |
The font number in a glyph node is between 0 and 255 and fits nicely in one byte, |
| 670 |
but the character code is more difficult: we want to store the most common character |
| 671 |
codes as a single byte and less frequent codes with two, three, or even four byte. |
| 672 |
Naturally, we use the |info| bits to store the number of bytes needed for the character code. |
| 673 |
|
| 674 |
\codesection{\putsymbol}{Writing the Short Format}\putindex{1}{2}{Glyphs} |
| 675 |
@<put functions@>= |
| 676 |
static uint8_t hput_n(uint32_t n) |
| 677 |
{@+ if (n<=0xFF) @+ |
| 678 |
{@+HPUT8(n);@+ return 1;@+} |
| 679 |
else if (n<=0xFFFF) @+ |
| 680 |
{@+HPUT16(n);@+ return 2;@+} |
| 681 |
else if (n<=0xFFFFFF)@+ |
| 682 |
{@+HPUT24(n);@+ return 3;@+} |
| 683 |
else @+ |
| 684 |
{@+HPUT32(n);@+ return 4;@+} |
| 685 |
} |
| 686 |
|
| 687 |
Tag hput_glyph(Glyph *g) |
| 688 |
{ Info info; |
| 689 |
info = hput_n(g->c); |
| 690 |
HPUT8(g->f);@/ |
| 691 |
return TAG(glyph_kind,info); |
| 692 |
} |
| 693 |
@ |
| 694 |
The |hput_tags| function is called after the node content has been written to the |
| 695 |
stream. It gets a the position of the start byte and the tag. With this information |
| 696 |
it writes the start byte at the given position and the end byte at the current stream position. |
| 697 |
@<put functions@>= |
| 698 |
void hput_tags(uint32_t pos, Tag tag) |
| 699 |
{ DBGTAG(tag,hstart+pos);DBGTAG(tag,hpos); |
| 700 |
HPUTX(1); *(hstart+pos)=*(hpos++)=tag; @+ |
| 701 |
} |
| 702 |
@ |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
The variables |hpos| and |hstart|, the macros |HPUT8|, |HPUT16|, |
| 707 |
|HPUT24|, |HPUT32|, and |HPUTX| are all defined in |
| 708 |
section~\secref{HPUT}; they put 8, 16, 24, or 32 bits into the output |
| 709 |
stream and check for sufficient space in the output buffer. The macro |
| 710 |
|DBGTAG| writes debugging output; its definition is found in |
| 711 |
section~\secref{error_section}. |
| 712 |
|
| 713 |
Now that we have seen the general outline of the \.{shrink} program, |
| 714 |
starting with a long format file and ending with a short format file, |
| 715 |
we will look at the program \.{stretch} that reverses this |
| 716 |
transformation. |
| 717 |
|
| 718 |
|
| 719 |
\subsection{Parsing the Short Format} |
| 720 |
The inverse of writing the short format with a {\it hput\_\kern 1pt\dots\/} function |
| 721 |
is reading the short format with a {\it hget\_\kern 1pt\dots\/} function. |
| 722 |
|
| 723 |
The schema of {\it hget\_\kern 1pt\dots\/} functions reverse the schema of {\it hput\_\kern 1pt\dots\/} functions. |
| 724 |
Here is the code for the initial and final part of a get function: |
| 725 |
|
| 726 |
@<read the start byte |a|@>= |
| 727 |
Tag a,z; /* the start and the end byte*/ |
| 728 |
uint32_t node_pos=(uint32_t)(hpos-hstart); |
| 729 |
if (hpos>=hend) QUIT("Attempt to read a start byte at the end of the section"); |
| 730 |
HGETTAG(a);@/@t{}@> |
| 731 |
@ |
| 732 |
|
| 733 |
@<read and check the end byte |z|@>= |
| 734 |
HGETTAG(z);@+ |
| 735 |
if (a!=z) |
| 736 |
QUIT(@["Tag mismatch [%s,%d]!=[%s,%d] at 0x%x to " SIZE_F "\n"@],@| |
| 737 |
NAME(a),INFO(a),NAME(z),INFO(z),@|node_pos, hpos-hstart-1); |
| 738 |
@ |
| 739 |
|
| 740 |
|
| 741 |
The central routine to parse\index{parsing} the content section of a short format |
| 742 |
file is the function |hget_content_node| which calls |hget_content| to |
| 743 |
do most of the processing. |
| 744 |
|
| 745 |
|hget_content_node| will read a content node in short format and write |
| 746 |
it out in long format: It reads the start\index{start byte} byte |a|, writes the |START| |
| 747 |
token using the function |hwrite_start|, and based on |KIND(a)|, it |
| 748 |
writes the node's keyword found in the |content_name| array. Then it |
| 749 |
calls |hget_content| to read the node's content and write it out. |
| 750 |
Finally it reads the end\index{end byte} byte, checks it against the start byte, and |
| 751 |
finishes up the content node by writing the |END| token using the |
| 752 |
|hwrite_end| function. The function returns the tag byte so that |
| 753 |
the calling function might check that the content node meets its requirements. |
| 754 |
|
| 755 |
|hget_content| uses the start byte |a|, passed as a parameter, to |
| 756 |
branch directly to the reading routine for the given combination of |
| 757 |
kind and info value. The reading routine will read the data and store |
| 758 |
its internal representation in a variable. All that the \.{stretch} |
| 759 |
program needs to do with this internal representation is writing it in |
| 760 |
the long format. As we will see, the call to the proper |
| 761 |
{\it hwrite\_\kern 1pt\dots} function is included as final part of the the |
| 762 |
reading routine (avoiding another switch statement). |
| 763 |
|
| 764 |
|
| 765 |
\codesection{\getsymbol}{Reading the Short Format}\getindex{1}{2}{Content Nodes} |
| 766 |
@<get functions@>= |
| 767 |
static void hget_content(Tag a); |
| 768 |
Tag hget_content_node(void) |
| 769 |
{ @<read the start byte |a|@>@;@+ hwrite_start(); |
| 770 |
if (content_known[KIND(a)]&(1<<INFO(a))) hwritef("%s",content_name[KIND(a)]); |
| 771 |
hget_content(a);@/ |
| 772 |
@<read and check the end byte |z|@>@; hwrite_end(); |
| 773 |
return a; |
| 774 |
} |
| 775 |
|
| 776 |
static void hget_content(Tag a) |
| 777 |
{@+ |
| 778 |
switch (a)@/ |
| 779 |
{@+ |
| 780 |
@<cases to get content@>@;@t\1@>@/ |
| 781 |
default: |
| 782 |
if (!hget_unknown(a)) |
| 783 |
TAGERR(a); |
| 784 |
break;@t\2@>@/ |
| 785 |
} |
| 786 |
} |
| 787 |
@ |
| 788 |
|
| 789 |
We implement the code to read a glyph node in two stages. |
| 790 |
First we define a general reading macro |HGET_GLYPH(I,G)| that reads a glyph node with info value |I| into |
| 791 |
a |Glyph| variable |G|; then we insert this macro |
| 792 |
in the above switch statement for all cases where it applies. |
| 793 |
Knowing the function |hput_glyph|, the macro |HGET_GLYPH| should not be a surprise. |
| 794 |
It reverses |hput_glyph|, storing the glyph node in its internal representation. |
| 795 |
After that, the \.{stretch} program calls |hwrite_glyph| to produce the glyph |
| 796 |
node in long format. |
| 797 |
|
| 798 |
\codesection{\getsymbol}{Reading the Short Format}\getindex{1}{2}{Glyphs} |
| 799 |
@<get macros@>= |
| 800 |
#define @[HGET_N(I,X)@] \ |
| 801 |
if ((I)==1) (X)=HGET8;\ |
| 802 |
else if ((I)==2) HGET16(X);\ |
| 803 |
else if ((I)==3) HGET24(X);\ |
| 804 |
else if ((I)==4) HGET32(X); |
| 805 |
|
| 806 |
#define @[HGET_GLYPH(I,G)@] \ |
| 807 |
HGET_N(I,(G).c); (G).f=HGET8; @+REF_RNG(font_kind,(G).f);@/\ |
| 808 |
hwrite_glyph(&(G));\ |
| 809 |
@ |
| 810 |
|
| 811 |
Note that we allow a glyph to reference a font even before that font is defined. |
| 812 |
This is necessary because fonts usually contain definitions---for example |
| 813 |
the fonts hyphen character---that reference this or other fonts. |
| 814 |
|
| 815 |
|
| 816 |
@<cases to get content@>= |
| 817 |
@t\1\kern1em@>case TAG(glyph_kind,1): @+{@+Glyph g;@+ HGET_GLYPH(1,g);@+}@+break; |
| 818 |
case TAG(glyph_kind,2): @+{@+Glyph g;@+ HGET_GLYPH(2,g);@+}@+break; |
| 819 |
case TAG(glyph_kind,3): @+{@+Glyph g;@+ HGET_GLYPH(3,g);@+}@+break; |
| 820 |
case TAG(glyph_kind,4): @+{@+Glyph g;@+ HGET_GLYPH(4,g);@+}@+break; |
| 821 |
@ |
| 822 |
|
| 823 |
If this two stage method seems strange to you, consider what the \CEE\ compiler will |
| 824 |
do with it. It will expand the |HGET_GLYPH| macro four times inside the switch |
| 825 |
statement. The macro is, however, expanded with a constant |I| value, so the expansion |
| 826 |
of the |if| statement in |HGET_GLYPH(1,g)|, for example, |
| 827 |
will become ``|if (1==1)| \dots\ |else if (1==2)| \dots'' |
| 828 |
and the compiler will have no difficulties eliminating the constant tests and |
| 829 |
the dead branches altogether. This is the most effective use of the switch statement: |
| 830 |
a single jump takes you to a specialized code to handle just the given combination |
| 831 |
of kind and info value. |
| 832 |
|
| 833 |
Last not least, we implement the function |hwrite_glyph| to write a |
| 834 |
glyph node in long form---that is: in a form that is as readable as possible. |
| 835 |
|
| 836 |
\subsection{Writing the Long Format} |
| 837 |
|
| 838 |
The |hwrite_glyph| function inverts the scanning and parsing process we have described |
| 839 |
at the very beginning of this chapter. |
| 840 |
To implement the |hwrite_glyph| function, we use the function |hwrite_charcode| |
| 841 |
to write the character code. |
| 842 |
Besides writing the character code as a decimal number, this function can handle also other |
| 843 |
representations of character codes as fully explained in section~\secref{chars}. |
| 844 |
We split off the writing of the opening and the closing pointed bracket, because |
| 845 |
we will need this function very often and because it will keep track of the |nesting| |
| 846 |
of nodes and indent them accordingly. The |hwrite_range| and |hwrite_label| functions |
| 847 |
used in |hwrite_end| are discussed in section~\secref{range} and~\secref{hwritelabel}. |
| 848 |
|
| 849 |
\codesection{\wrtsymbol}{Writing the Long Format}\wrtindex{1}{2}{Glyphs} |
| 850 |
@<write functions@>= |
| 851 |
int nesting=0; |
| 852 |
void hwrite_nesting(void) |
| 853 |
{ int i; |
| 854 |
hwritec('\n'); |
| 855 |
for (i=0;i<nesting;i++) hwritec(' '); |
| 856 |
} |
| 857 |
|
| 858 |
void hwrite_start(void) |
| 859 |
{ @+hwrite_nesting();@+ hwritec('<');@+ nesting++; |
| 860 |
} |
| 861 |
|
| 862 |
void hwrite_range(void); |
| 863 |
void hwrite_label(void); |
| 864 |
|
| 865 |
void hwrite_end(void) |
| 866 |
{ nesting--; hwritec('>'); |
| 867 |
if (section_no==2) |
| 868 |
{ if (nesting==0) hwrite_range(); |
| 869 |
hwrite_label(); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
void hwrite_comment(char *str) |
| 874 |
{ char c; |
| 875 |
if (str==NULL) return; |
| 876 |
hwritef(" ("); |
| 877 |
while ((c=*str++)!=0) |
| 878 |
if (c=='(' || c==')') hwritec('_'); |
| 879 |
else if (c=='\n') hwritef("\n("); |
| 880 |
else hwritec(c); |
| 881 |
hwritec(')'); |
| 882 |
} |
| 883 |
|
| 884 |
void hwrite_charcode(uint32_t c); |
| 885 |
void hwrite_ref(int n); |
| 886 |
|
| 887 |
void hwrite_glyph(Glyph *g) |
| 888 |
{ char *n=hfont_name[g->f]; |
| 889 |
hwrite_charcode(g->c); |
| 890 |
hwrite_ref(g->f); |
| 891 |
if (n!=NULL) hwrite_comment(n); |
| 892 |
} |
| 893 |
@ |
| 894 |
|
| 895 |
The two primitive operations to write the long format file are defined |
| 896 |
as macros: |
| 897 |
|
| 898 |
@<write macros@>= |
| 899 |
#define @[hwritec(c)@] @[(hout?putc(c,hout):0)@] |
| 900 |
#define @[hwritef(...)@] @[(hout?fprintf(hout,__VA_ARGS__):0)@] |
| 901 |
@ |
| 902 |
|
| 903 |
|
| 904 |
Now that we have completed the round trip of shrinking and stretching |
| 905 |
glyph nodes, we continue the description of the \HINT\ file formats |
| 906 |
in a more systematic way. |
| 907 |
|
| 908 |
|
| 909 |
\section{Data Types}\hascode |
| 910 |
\subsection{Integers} |
| 911 |
\label{integers} |
| 912 |
We have already seen the pattern/\kern -1pt action rule for unsigned decimal\index{decimal number} numbers. It remains |
| 913 |
to define the macro |SCAN_UDEC| which converts a string containing an unsigned\index{unsigned} decimal |
| 914 |
number into an unsigned integer\index{integer}. |
| 915 |
We use the \CEE\ library function | strtoul|: |
| 916 |
|
| 917 |
\readcode |
| 918 |
@<scanning macros@>= |
| 919 |
#define @[SCAN_UDEC(S)@] @[yylval.u=strtoul(S,NULL,10)@] |
| 920 |
@ |
| 921 |
Unsigned integers can be given in hexadecimal\index{hexadecimal} notation as well. |
| 922 |
@<scanning definitions@>= |
| 923 |
::@=HEX@> :< @=[0-9A-F]@> >: |
| 924 |
@ |
| 925 |
|
| 926 |
@<scanning rules@>= |
| 927 |
::@=0x{HEX}+@> :< SCAN_HEX(yytext+2); return UNSIGNED; >: |
| 928 |
@ |
| 929 |
|
| 930 |
Note that the pattern above allows only upper case letters in the |
| 931 |
hexadecimal notation for integers. |
| 932 |
|
| 933 |
@<scanning macros@>= |
| 934 |
#define @[SCAN_HEX(S)@] @[yylval.u=strtoul(S,NULL,16)@] |
| 935 |
@ |
| 936 |
|
| 937 |
Last not least, we add rules for signed\index{signed integer} integers. |
| 938 |
@s SIGNED symbol |
| 939 |
@s number symbol |
| 940 |
@s integer symbol |
| 941 |
|
| 942 |
@<symbols@>= |
| 943 |
%token <i> SIGNED |
| 944 |
%type <i> integer |
| 945 |
@ |
| 946 |
|
| 947 |
@<scanning rules@>= |
| 948 |
::@=[+-](0|[1-9][0-9]*)@> :< SCAN_DEC(yytext); return SIGNED; >: |
| 949 |
@ |
| 950 |
|
| 951 |
@<scanning macros@>= |
| 952 |
#define @[SCAN_DEC(S)@] @[yylval.i=strtol(S,NULL,10)@] |
| 953 |
@ |
| 954 |
|
| 955 |
@<parsing rules@>= |
| 956 |
integer: SIGNED @+| UNSIGNED { RNG("number",$1,0,0x7FFFFFFF);}; |
| 957 |
@ |
| 958 |
|
| 959 |
To preserve the ``signedness'' of an integer also for positive signed integers |
| 960 |
in the long format, we implement the function |hwrite_signed|. |
| 961 |
|
| 962 |
\writecode |
| 963 |
@<write functions@>= |
| 964 |
void hwrite_signed(int32_t i) |
| 965 |
{ if (i<0) hwritef(" -%d",-i); |
| 966 |
else hwritef(" +%d",+i); |
| 967 |
} |
| 968 |
@ |
| 969 |
|
| 970 |
Reading and writing integers in the short format is done directly with the {\tt HPUT} and {\tt HGET} |
| 971 |
macros. |
| 972 |
|
| 973 |
|
| 974 |
\subsection{Strings} |
| 975 |
\label{strings} |
| 976 |
Strings\index{string} are needed in the definition part of a \HINT\ |
| 977 |
file to specify names of objects, and in the long file format, we also use them for file\index{file name} names. |
| 978 |
In the long format, strings are sequences of characters delimited by single quote\index{single quote} characters; |
| 979 |
for example: ``\.{'Hello'}'' or ``\.{'cmr10-600dpi.tfm'}''; in the short format, strings are |
| 980 |
byte sequences terminated by a zero byte. |
| 981 |
Because file names are system dependent, we no not allow arbitrary characters in strings |
| 982 |
but only printable ASCII codes which we can reasonably expect to be available on most operating systems. |
| 983 |
If your file names in a long format \HINT\ file are supposed to be portable, |
| 984 |
you should probably be even more restrictive. For example you should avoid characters like |
| 985 |
``\.{\\}'' or ``\.{/}'' which are used in different ways for directories. |
| 986 |
|
| 987 |
The internal representation of a string is a simple zero terminated \CEE\ string. |
| 988 |
When scanning a string, we copy it to the |str_buffer| keeping track |
| 989 |
of its length in |str_length|. When done, |
| 990 |
we make a copy for permanent storage and return the pointer to the parser. |
| 991 |
To operate on the |str_buffer|, we define a few macros. |
| 992 |
The constant |MAX_STR| determines the maximum size of a string (including the zero byte) to be $2^{10}$ byte. |
| 993 |
This restriction is part of the \HINT\ file format specification. |
| 994 |
|
| 995 |
@<scanning macros@>= |
| 996 |
#define MAX_STR (1<<10) /* $2^{10}$ Byte or 1kByte */ |
| 997 |
static char str_buffer[MAX_STR]; |
| 998 |
static int str_length; |
| 999 |
#define STR_START @[(str_length=0)@] |
| 1000 |
#define @[STR_PUT(C)@] @[(str_buffer[str_length++]=(C))@] |
| 1001 |
#define @[STR_ADD(C)@] @[STR_PUT(C);RNG("String length",str_length,0,MAX_STR-1)@] |
| 1002 |
#define STR_END @[str_buffer[str_length]=0@] |
| 1003 |
#define SCAN_STR @[yylval.s=str_buffer@] |
| 1004 |
@ |
| 1005 |
|
| 1006 |
|
| 1007 |
To scan a string, we switch the scanner to |STR| mode when we find a quote character, |
| 1008 |
then we scan bytes in the range |0x20| to |0x7E|, which is the range of printable ASCII |
| 1009 |
characters, until we find the closing single\index{single quote} quote. |
| 1010 |
Quote characters inside the string are written as two consecutive single quote characters. |
| 1011 |
|
| 1012 |
\readcode |
| 1013 |
@s STRING symbol |
| 1014 |
@s STR symbol |
| 1015 |
@s INITIAL symbol |
| 1016 |
|
| 1017 |
@<scanning definitions@>= |
| 1018 |
%x STR |
| 1019 |
@ |
| 1020 |
|
| 1021 |
@<symbols@>= |
| 1022 |
%token <s> STRING |
| 1023 |
@ |
| 1024 |
|
| 1025 |
@<scanning rules@>= |
| 1026 |
::@='@> :< STR_START; BEGIN(STR); >: |
| 1027 |
<STR>{ |
| 1028 |
::@='@> :< STR_END; SCAN_STR; BEGIN(INITIAL); return STRING; >: |
| 1029 |
::@=''@> :< STR_ADD('\''); >: |
| 1030 |
::@=[\x20-\x7E]@> :< STR_ADD(yytext[0]); >: |
| 1031 |
::@=.@> :< RNG("String character",yytext[0],0x20,0x7E); >: |
| 1032 |
::@=\n@> :< QUIT("Unterminated String in line %d",yylineno); >: |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
@ |
| 1037 |
The function |hwrite_string| reverses this process; it must take care of the quote symbols. |
| 1038 |
\writecode |
| 1039 |
@<write functions@>= |
| 1040 |
void hwrite_string(char *str) |
| 1041 |
{@+hwritec(' '); |
| 1042 |
if (str==NULL) hwritef("''"); |
| 1043 |
else@/ |
| 1044 |
{ hwritec('\''); |
| 1045 |
while (*str!=0)@/ |
| 1046 |
{ @+if (*str=='\'') hwritec('\''); |
| 1047 |
hwritec(*str); |
| 1048 |
str++; |
| 1049 |
} |
| 1050 |
hwritec('\''); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
@ |
| 1056 |
In the short format, a string is just a byte sequence terminated by a zero byte. |
| 1057 |
This makes the function |hput_string|, to write a string, and the macro |HGET_STRING|, |
| 1058 |
to read a string in short format, very simple. Note that after writing an unbounded |
| 1059 |
string to the output buffer, the macro |HPUTNODE| will make sure that there is enough |
| 1060 |
space left to write the remainder of the node. |
| 1061 |
|
| 1062 |
\putcode |
| 1063 |
@<put functions@>= |
| 1064 |
void hput_string(char *str) |
| 1065 |
{ char *s=str; |
| 1066 |
if (s!=NULL) |
| 1067 |
{ do { |
| 1068 |
HPUTX(1); |
| 1069 |
HPUT8(*s); |
| 1070 |
} while (*s++!=0); |
| 1071 |
HPUTNODE; |
| 1072 |
} |
| 1073 |
else HPUT8(0); |
| 1074 |
} |
| 1075 |
@ |
| 1076 |
|
| 1077 |
\getcode |
| 1078 |
@<shared get macros@>= |
| 1079 |
#define @[HGET_STRING(S)@] @[S=(char*)hpos;\ |
| 1080 |
while(hpos<hend && *hpos!=0) { RNG("String character",*hpos,0x20,0x7E); hpos++;}\ |
| 1081 |
hpos++; |
| 1082 |
@ |
| 1083 |
|
| 1084 |
\subsection{Character Codes} |
| 1085 |
\label{chars} |
| 1086 |
We have already seen in the introduction that character\index{character code} codes can be written as decimal numbers |
| 1087 |
and section~\secref{integers} adds the possibility to use hexadecimal numbers as well. |
| 1088 |
|
| 1089 |
It is, however, in most cases more readable if we represent character codes directly |
| 1090 |
using the characters themselves. Writing ``\.{a}'' is just so much better than writing ``\.{97}''. |
| 1091 |
To distinguish the character ``\.{9}'' from the number ``\.{9}'', we use the common technique |
| 1092 |
of enclosing characters within single\index{single quote} quotes. So ``\.{'9'}'' is the character code and |
| 1093 |
``\.{9}'' is the number. |
| 1094 |
Therefore we will define |CHARCODE| tokens and complement the parsing rules of section~\secref{parse_glyph} |
| 1095 |
with the following rule: |
| 1096 |
\readcode |
| 1097 |
@<parsing rules@>= |
| 1098 |
glyph: CHARCODE REFERENCE @|{ $$.c=$1; REF(font_kind,$2); $$.f=$2; }; |
| 1099 |
@ |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
If the character codes are small, we can represent them using |
| 1104 |
ASCII character codes. We do not offer a special notation for very small |
| 1105 |
character codes that map to the non-printable ASCII control codes; for them, the decimal |
| 1106 |
or hexadecimal notation will suffice. |
| 1107 |
For larger character codes, we use the multibyte encoding scheme known from UTF8\index{UTF8} as |
| 1108 |
follows. Given a character code~|c|: |
| 1109 |
|
| 1110 |
\itemize |
| 1111 |
\item |
| 1112 |
Values in the range |0x00| to |0x7f| are encoded as a single byte with a leading bit of 0. |
| 1113 |
|
| 1114 |
@<scanning definitions@>= |
| 1115 |
::@=UTF8_1@> :< @=[\x00-\x7F]@> >: |
| 1116 |
@ |
| 1117 |
@<scanning macros@>= |
| 1118 |
#define @[SCAN_UTF8_1(S)@] @[yylval.u=((S)[0]&0x7F)@] |
| 1119 |
@ |
| 1120 |
|
| 1121 |
|
| 1122 |
\item |
| 1123 |
Values in the range |0x80| to |0x7ff| are encoded in two byte with the first byte |
| 1124 |
having three high bits |110|, indicating a two byte sequence, and the lower five bits equal |
| 1125 |
to the five high bits of |c|. It is followed by a continuation byte having two high bits |10| |
| 1126 |
and the lower six bits |
| 1127 |
equal to the lower six bits of |c|. |
| 1128 |
|
| 1129 |
@<scanning definitions@>= |
| 1130 |
::@=UTF8_2@> :< @=[\xC0-\xDF][\x80-\xBF]@> >: |
| 1131 |
@ |
| 1132 |
|
| 1133 |
@<scanning macros@>= |
| 1134 |
#define @[SCAN_UTF8_2(S)@] @[yylval.u=(((S)[0]&0x1F)<<6)+((S)[1]&0x3F)@] |
| 1135 |
@ |
| 1136 |
|
| 1137 |
\item |
| 1138 |
Values in the range |0x800| to |0xFFFF| are encoded in three byte with the first byte |
| 1139 |
having the high bits |1110| indicating a three byte sequence followed by two continuation bytes. |
| 1140 |
|
| 1141 |
@<scanning definitions@>= |
| 1142 |
::@=UTF8_3@> :< @=[\xE0-\xEF][\x80-\xBF][\x80-\xBF]@> >: |
| 1143 |
@ |
| 1144 |
|
| 1145 |
@<scanning macros@>= |
| 1146 |
#define @[SCAN_UTF8_3(S)@] @[yylval.u=(((S)[0]&0x0F)<<12)+(((S)[1]&0x3F)<<6)+((S)[2]&0x3F)@] |
| 1147 |
@ |
| 1148 |
|
| 1149 |
\item |
| 1150 |
Values in the range |0x1000| to |0x1FFFFF| are encoded in four byte with the first byte |
| 1151 |
having the high bits |11110| indicating a four byte sequence followed by three continuation bytes. |
| 1152 |
|
| 1153 |
@<scanning definitions@>= |
| 1154 |
::@=UTF8_4@> :< @=[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]@> >: |
| 1155 |
@ |
| 1156 |
|
| 1157 |
@<scanning macros@>= |
| 1158 |
#define @[SCAN_UTF8_4(S)@] @[yylval.u=(((S)[0]&0x07)<<18)+(((S)[1]&0x3F)<<12)+@|(((S)[2]&0x3F)<<6)+((S)[3]&0x3F)@] |
| 1159 |
@ |
| 1160 |
|
| 1161 |
\enditemize |
| 1162 |
|
| 1163 |
In the long format file, we enclose a character code in single\index{single quote} quotes, just as we do for strings. |
| 1164 |
This is convenient but it has the downside that we must exercise special care when giving the |
| 1165 |
scanning rules in order |
| 1166 |
not to confuse character codes with strings. Further we must convert character codes back into strings |
| 1167 |
in the rare case where the parser expects a string and gets a character code because the string |
| 1168 |
was only a single character long. |
| 1169 |
|
| 1170 |
Let's start with the first problem: |
| 1171 |
The scanner might confuse a string\index{string} and a character code if the first or second |
| 1172 |
character of the string is a quote character which is written as two consecutive quotes. |
| 1173 |
For example \.{'a''b'} is a string with three characters, ``\.{a}'', |
| 1174 |
``\.{'}'', and ``\.{b}''. Two character codes would need a space to separate |
| 1175 |
them like this: \.{'a' 'b'}. |
| 1176 |
|
| 1177 |
|
| 1178 |
@s CHARCODE symbol |
| 1179 |
@<symbols@>= |
| 1180 |
%token <u> CHARCODE |
| 1181 |
@ |
| 1182 |
|
| 1183 |
@<scanning rules@>= |
| 1184 |
::@='''@> :< STR_START; STR_PUT('\''); BEGIN(STR); >: |
| 1185 |
::@=''''@> :< SCAN_UTF8_1(yytext+1); return CHARCODE; >: |
| 1186 |
::@='[\x20-\x7E]''@> :< STR_START; STR_PUT(yytext[1]); STR_PUT('\''); BEGIN(STR); >: |
| 1187 |
::@='''''@> :< STR_START; STR_PUT('\''); STR_PUT('\''); BEGIN(STR); >: |
| 1188 |
::@='{UTF8_1}'@> :< SCAN_UTF8_1(yytext+1); return CHARCODE; >: |
| 1189 |
::@='{UTF8_2}'@> :< SCAN_UTF8_2(yytext+1); return CHARCODE; >: |
| 1190 |
::@='{UTF8_3}'@> :< SCAN_UTF8_3(yytext+1); return CHARCODE; >: |
| 1191 |
::@='{UTF8_4}'@> :< SCAN_UTF8_4(yytext+1); return CHARCODE; >: |
| 1192 |
@ |
| 1193 |
|
| 1194 |
If needed, the parser can convert character codes back to single character strings. |
| 1195 |
|
| 1196 |
@s string symbol |
| 1197 |
|
| 1198 |
@<symbols@>= |
| 1199 |
%type <s> string |
| 1200 |
@ |
| 1201 |
|
| 1202 |
@<parsing rules@>= |
| 1203 |
string: STRING @+ | CHARCODE { static char s[2]; |
| 1204 |
RNG("String element",$1,0x20,0x7E); |
| 1205 |
s[0]=$1; s[1]=0; $$=s;}; |
| 1206 |
@ |
| 1207 |
|
| 1208 |
|
| 1209 |
The function |hwrite_charcode| will write a character code. While ASCII codes are handled directly, |
| 1210 |
larger character codes are passed to the function |hwrite_utf8|. |
| 1211 |
It returns the number of characters written. |
| 1212 |
|
| 1213 |
\writecode |
| 1214 |
@<write functions@>= |
| 1215 |
int hwrite_utf8(uint32_t c) |
| 1216 |
{@+ if (c<0x80) |
| 1217 |
{ hwritec(c); @+return 1;@+ } |
| 1218 |
else if (c<0x800)@/ |
| 1219 |
{ hwritec(0xC0|(c>>6));@+ hwritec(0x80|(c&0x3F));@+ return 2;@+} |
| 1220 |
else if (c<0x10000)@/ |
| 1221 |
{ hwritec(0xE0|(c>>12)); hwritec(0x80|((c>>6)&0x3F));@+ hwritec(0x80|(c&0x3F)); return 3; } |
| 1222 |
else if (c<0x200000)@/ |
| 1223 |
{ hwritec(0xF0|(c>>18));@+ hwritec(0x80|((c>>12)&0x3F)); |
| 1224 |
hwritec(0x80|((c>>6)&0x3F));@+ hwritec(0x80|(c&0x3F)); return 4;} |
| 1225 |
else |
| 1226 |
RNG("character code",c,0,0x1FFFFF); |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
|
| 1230 |
void hwrite_charcode(uint32_t c) |
| 1231 |
{ @+if (c < 0x20) |
| 1232 |
{ if (option_hex) hwritef(" 0x%02X",c); /* non printable ASCII */ |
| 1233 |
else hwritef(" %u",c); |
| 1234 |
} |
| 1235 |
else if (c=='\'') hwritef(" ''''"); |
| 1236 |
else if (c<=0x7E) hwritef(" \'%c\'",c); /* printable ASCII */ |
| 1237 |
else if (option_utf8) { hwritef(" \'"); @+ hwrite_utf8(c); @+ hwritec('\'');@+} |
| 1238 |
else if (option_hex) hwritef(" 0x%04X",c); |
| 1239 |
else hwritef(" %u",c); |
| 1240 |
} |
| 1241 |
@ |
| 1242 |
|
| 1243 |
\getcode |
| 1244 |
@<shared get functions@>= |
| 1245 |
#define @[HGET_UTF8C(X)@] (X)=HGET8;@+ if ((X&0xC0)!=0x80) \ |
| 1246 |
QUIT(@["UTF8 continuation byte expected at " SIZE_F " got 0x%02X\n"@],hpos-hstart-1,X)@; |
| 1247 |
|
| 1248 |
uint32_t hget_utf8(void) |
| 1249 |
{ uint8_t a; |
| 1250 |
a=HGET8; |
| 1251 |
if (a<0x80) return a; |
| 1252 |
else |
| 1253 |
{ if ((a&0xE0)==0xC0) @/ |
| 1254 |
{ uint8_t b; @+ HGET_UTF8C(b); |
| 1255 |
return ((a&~0xE0)<<6)+(b&~0xC0); |
| 1256 |
} |
| 1257 |
else if ((a&0xF0)==0xE0) @/ |
| 1258 |
{ uint8_t b,c; @+ HGET_UTF8C(b); @+ HGET_UTF8C(c); |
| 1259 |
return ((a&~0xF0)<<12)+((b&~0xC0)<<6)+(c&~0xC0); |
| 1260 |
} |
| 1261 |
else if ((a&0xF8)==0xF0) @/ |
| 1262 |
{ uint8_t b,c,d; @+ HGET_UTF8C(b); @+ HGET_UTF8C(c); @+ HGET_UTF8C(d); |
| 1263 |
return ((a&~0xF8)<<18)@|+ ((b&~0xC0)<<12)+((c&~0xC0)<<6)+(d&~0xC0); |
| 1264 |
} |
| 1265 |
else QUIT("UTF8 byte sequence expected"); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
@ |
| 1269 |
\putcode |
| 1270 |
@<put functions@>= |
| 1271 |
void hput_utf8(uint32_t c) |
| 1272 |
{ @+HPUTX(4); |
| 1273 |
if (c<0x80) |
| 1274 |
HPUT8(c); |
| 1275 |
else if (c<0x800) |
| 1276 |
{ HPUT8(0xC0|(c>>6));@+ HPUT8(0x80|(c&0x3F));@+ } |
| 1277 |
else if (c<0x10000)@/ |
| 1278 |
{ HPUT8(0xE0|(c>>12)); HPUT8(0x80|((c>>6)&0x3F));@+ HPUT8(0x80|(c&0x3F)); } |
| 1279 |
else if (c<0x200000)@/ |
| 1280 |
{ HPUT8(0xF0|(c>>18));@+ HPUT8(0x80|((c>>12)&0x3F)); |
| 1281 |
HPUT8(0x80|((c>>6)&0x3F));@+ HPUT8(0x80|(c&0x3F)); } |
| 1282 |
else |
| 1283 |
RNG("character code",c,0,0x1FFFFF); |
| 1284 |
} |
| 1285 |
@ |
| 1286 |
|
| 1287 |
\subsection{Floating Point Numbers} |
| 1288 |
You know a floating point numbers\index{floating point number} when |
| 1289 |
you see it because it features a radix\index{radix point} point. The |
| 1290 |
optional exponent\index{exponent} allows you to ``float'' the point. |
| 1291 |
|
| 1292 |
\readcode |
| 1293 |
@s FPNUM symbol |
| 1294 |
@s number symbol |
| 1295 |
|
| 1296 |
@<symbols@>= |
| 1297 |
%token <f> FPNUM |
| 1298 |
%type <f> number |
| 1299 |
@ |
| 1300 |
@<scanning rules@>= |
| 1301 |
::@=[+-]?[0-9]+\.[0-9]+(e[+-]?[0-9])?@> :< SCAN_DECFLOAT; return FPNUM; >: |
| 1302 |
@ |
| 1303 |
|
| 1304 |
The layout of floating point variables of type |double| |
| 1305 |
or |float| typically follows the IEEE754\index{IEEE754} |
| 1306 |
standard\cite{IEEE754-1985}\cite{IEEE754-2008}. |
| 1307 |
We use the following definitions: |
| 1308 |
|
| 1309 |
\index{float32 t+\&{float32\_t}} |
| 1310 |
\index{float64 t+\&{float64\_t}} |
| 1311 |
|
| 1312 |
@<hint basic types@>= |
| 1313 |
|
| 1314 |
#define FLT_M_BITS 23 |
| 1315 |
#define FLT_E_BITS 8 |
| 1316 |
#define FLT_EXCESS 127 |
| 1317 |
|
| 1318 |
#define DBL_M_BITS 52 |
| 1319 |
#define DBL_E_BITS 11 |
| 1320 |
#define DBL_EXCESS 1023 |
| 1321 |
|
| 1322 |
@ |
| 1323 |
|
| 1324 |
@s float32_t int |
| 1325 |
@s float64_t int |
| 1326 |
|
| 1327 |
We expect a variable of type |float64_t| to have a binary |
| 1328 |
representation using 64 bit. The most significant bit is the sign |
| 1329 |
bit, then follow $|DBL_E_BITS|=11$ bits for the |
| 1330 |
exponent\index{exponent}, and $|DBL_M_BITS|=52$ bits for the |
| 1331 |
mantissa\index{mantissa}. The sign\index{sign bit} bit is 1 for a |
| 1332 |
negative number and 0 for a positive number. A floating point number |
| 1333 |
is stored in normalized\index{normalization} form which means that the |
| 1334 |
mantissa is shifted such that it has exactly 52+1 bit not counting |
| 1335 |
leading zeros. The leading bit is then always 1 and there is no need |
| 1336 |
to store it. So 52 bits suffice. To store the exponent, the excess |
| 1337 |
$q=1023$ is added and the result is stored as an unsigned 11 bit |
| 1338 |
number. For example if we regard the exponent bits and the mantissa |
| 1339 |
bits as unsigned binary numbers $e$ and $m$ then the absolute value of |
| 1340 |
such a floating point number can be expressed as |
| 1341 |
$(1+m*2^{-52})\cdot2^{e-1023}$. We make similar assumptions about |
| 1342 |
variables of type |float32_t| using the constants as defined above. |
| 1343 |
|
| 1344 |
To convert the decimal representation of a floating point number to |
| 1345 |
binary values of type |float64_t|, we use a \CEE\ library function. |
| 1346 |
|
| 1347 |
@<scanning macros@>= |
| 1348 |
#define SCAN_DECFLOAT @[yylval.f=atof(yytext)@] |
| 1349 |
@ |
| 1350 |
|
| 1351 |
When the parser expects a floating point number and gets an integer number, |
| 1352 |
it converts it. So whenever in the long format a floating point number is expected, |
| 1353 |
an integer number will do as well. |
| 1354 |
|
| 1355 |
@<parsing rules@>= |
| 1356 |
number: UNSIGNED {$$=(float64_t)$1; } | SIGNED {$$=(float64_t)$1; } | FPNUM; |
| 1357 |
@ |
| 1358 |
|
| 1359 |
Unfortunately the decimal representation is not optimal for floating |
| 1360 |
point numbers since even simple numbers in decimal notation like $0.1$ |
| 1361 |
do not have an exact representation as a binary floating point number. |
| 1362 |
So if we want a notation that allows an exact representation of binary |
| 1363 |
floating point numbers, we must use a hexadecimal\index{hexadecimal} |
| 1364 |
representation. Hexadecimal floating point numbers start with an |
| 1365 |
optional sign, then as usual the two characters ``{\tt 0x}'', then |
| 1366 |
follows a sequence of hex digits, a radix point, more hex digits, and |
| 1367 |
an optional exponent. The optional exponent starts with the character |
| 1368 |
``{\tt x}'', followed by an optional sign, and some more hex |
| 1369 |
digits. The hexadecimal exponent is given as a base 16 number and it |
| 1370 |
is interpreted as an exponent with the base 16. As an example an |
| 1371 |
exponent of ``{\tt x10}'', would multiply the mantissa by $16^{16}$. |
| 1372 |
In other words it would shift any mantissa 16 hexadecimal digits to |
| 1373 |
the left. Here are the exact rules: |
| 1374 |
|
| 1375 |
@<scanning rules@>= |
| 1376 |
::@=[+-]?0x{HEX}+\.{HEX}+(x[+-]?{HEX}+)?@> :< SCAN_HEXFLOAT; return FPNUM; >: |
| 1377 |
@ |
| 1378 |
|
| 1379 |
@<scanning macros@>= |
| 1380 |
#define SCAN_HEXFLOAT @[yylval.f=xtof(yytext)@] |
| 1381 |
@ |
| 1382 |
There is no function in the \CEE\ library for hexadecimal floating point notation |
| 1383 |
so we have to write our own conversion routine. |
| 1384 |
The function |xtof| converts a string matching the above regular expression to |
| 1385 |
its binary representation. Its outline is very simple: |
| 1386 |
|
| 1387 |
@<scanning functions@>= |
| 1388 |
|
| 1389 |
float64_t xtof(char *x) |
| 1390 |
{ int sign, digits, exp; |
| 1391 |
uint64_t mantissa=0; |
| 1392 |
DBG(DBGFLOAT,"converting %s:\n",x); |
| 1393 |
@<read the optional sign@>@; |
| 1394 |
x=x+2; /* skip ``\.{0x}'' */ |
| 1395 |
@<read the mantissa@>@; |
| 1396 |
@<normalize the mantissa@>@; |
| 1397 |
@<read the optional exponent@>@; |
| 1398 |
@<return the binary representation@>@; |
| 1399 |
} |
| 1400 |
@ |
| 1401 |
|
| 1402 |
Now the pieces: |
| 1403 |
|
| 1404 |
@<read the optional sign@>= |
| 1405 |
if (*x=='-') { sign=-1;@+ x++;@+ } |
| 1406 |
else if (*x=='+') { sign=+1;@+ x++;@+ } |
| 1407 |
else @+sign=+1; |
| 1408 |
DBG(DBGFLOAT,"\tsign=%d\n",sign); |
| 1409 |
@ |
| 1410 |
|
| 1411 |
When we read the mantissa, we use the temporary variable |mantissa|, keep track |
| 1412 |
of the number of digits, and adjust the exponent while reading the fractional part. |
| 1413 |
@<read the mantissa@>= |
| 1414 |
digits=0; |
| 1415 |
while (*x=='0') x++; /*ignore leading zeros*/ |
| 1416 |
while (*x!='.')@/ |
| 1417 |
{ mantissa=mantissa<<4; |
| 1418 |
if (*x<'A') mantissa=mantissa+*x-'0'; |
| 1419 |
else mantissa=mantissa+*x-'A'+10; |
| 1420 |
x++; |
| 1421 |
digits++; |
| 1422 |
} |
| 1423 |
x++; /* skip ``\.{.}'' */ |
| 1424 |
exp=0; |
| 1425 |
while (*x!=0 && *x!='x')@/ |
| 1426 |
{ mantissa=mantissa<<4; |
| 1427 |
exp=exp-4; |
| 1428 |
if (*x<'A') mantissa=mantissa+*x-'0'; |
| 1429 |
else mantissa=mantissa+*x-'A'+10; |
| 1430 |
x++; |
| 1431 |
digits++; |
| 1432 |
} |
| 1433 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 ", exp=%d\n",@|digits,mantissa,exp); |
| 1434 |
@ |
| 1435 |
|
| 1436 |
To normalize the mantissa, first we shift it to place exactly one nonzero hexadecimal |
| 1437 |
digit to the left of the radix point. Then we shift it right bit-wise until there is |
| 1438 |
just a single 1 bit to the left of the radix point. |
| 1439 |
To compensate for the shifting, we adjust the exponent accordingly. |
| 1440 |
Finally we remove the most significant bit because it is |
| 1441 |
not stored. |
| 1442 |
|
| 1443 |
@<normalize the mantissa@>= |
| 1444 |
if (mantissa==0) return 0.0; |
| 1445 |
{ int s; |
| 1446 |
s = digits-DBL_M_BITS/4; |
| 1447 |
if (s>1) |
| 1448 |
mantissa=mantissa>>(4*(s-1)); |
| 1449 |
else if (s<1) |
| 1450 |
mantissa=mantissa<<(4*(1-s)); |
| 1451 |
exp=exp+4*(digits-1); |
| 1452 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 ", exp=%d\n",@|digits,mantissa,exp); |
| 1453 |
while ((mantissa>>DBL_M_BITS)>1)@/ { mantissa=mantissa>>1; @+ exp++;@+ } |
| 1454 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 ", exp=%d\n",@|digits,mantissa,exp); |
| 1455 |
mantissa=mantissa&~((uint64_t)1<<DBL_M_BITS); |
| 1456 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 ", exp=%d\n",@|digits,mantissa,exp); |
| 1457 |
} |
| 1458 |
@ |
| 1459 |
|
| 1460 |
In the printed representation, |
| 1461 |
the exponent is an exponent with base 16. For example, an exponent of 2 shifts |
| 1462 |
the hexadecimal mantissa two hexadecimal digits to the left, which corresponds to a |
| 1463 |
multiplication by ${16}^2$. |
| 1464 |
|
| 1465 |
@<read the optional exponent@>= |
| 1466 |
if (*x=='x')@/ |
| 1467 |
{ int s; |
| 1468 |
x++; /* skip the ``\.{x}'' */ |
| 1469 |
if (*x=='-') {s=-1;@+x++;@+} |
| 1470 |
else if (*x=='+') {s=+1;@+x++;@+} |
| 1471 |
else s=+1; |
| 1472 |
DBG(DBGFLOAT,"\texpsign=%d\n",s); |
| 1473 |
DBG(DBGFLOAT,"\texp=%d\n",exp); |
| 1474 |
while (*x!=0 ) |
| 1475 |
{ if (*x<'A') exp=exp+4*s*(*x-'0'); |
| 1476 |
else exp=exp+4*s*(*x-'A'+10); |
| 1477 |
x++; |
| 1478 |
DBG(DBGFLOAT,"\texp=%d\n",exp); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
RNG("Floating point exponent",@|exp,-DBL_EXCESS,DBL_EXCESS); |
| 1482 |
@ |
| 1483 |
|
| 1484 |
To assemble the binary representation, we use a |union| of a |float64_t| and |uint64_t|. |
| 1485 |
|
| 1486 |
|
| 1487 |
@<return the binary representation@>= |
| 1488 |
{ union {@+float64_t d; @+uint64_t bits; @+} u; |
| 1489 |
if (sign<0) sign=1;@+ else@+ sign=0; /* the sign bit */ |
| 1490 |
exp=exp+DBL_EXCESS; /* the exponent bits */ |
| 1491 |
u.bits=((uint64_t)sign<<63)@/ |
| 1492 |
| ((uint64_t)exp<<DBL_M_BITS) | mantissa; |
| 1493 |
DBG(DBGFLOAT," return %f\n",u.d); |
| 1494 |
return u.d; |
| 1495 |
} |
| 1496 |
@ |
| 1497 |
|
| 1498 |
The inverse function is |hwrite_float64|. It strives to print floating point numbers |
| 1499 |
as readable as possible. So numbers without fractional part are written as integers. |
| 1500 |
Numbers that can be represented exactly in decimal notation are represented in |
| 1501 |
decimal notation. All other values are written as hexadecimal floating point numbers. |
| 1502 |
We avoid an exponent if it can be avoided by using up to |MAX_HEX_DIGITS|. |
| 1503 |
For the use with extended dimensions, floating point numbers should be printed as a suffix: |
| 1504 |
without a leading space and with a mandatory sign. |
| 1505 |
|
| 1506 |
\writecode |
| 1507 |
@<write functions@>= |
| 1508 |
#define MAX_HEX_DIGITS 12 |
| 1509 |
void hwrite_float64(float64_t d, bool suffix) |
| 1510 |
{ uint64_t bits, mantissa; |
| 1511 |
int exp, digits; |
| 1512 |
if (!suffix) hwritec(' '); |
| 1513 |
else if (d>=0) hwritec('+'); |
| 1514 |
if (floor(d)==d) |
| 1515 |
{ hwritef("%d",(int)d);@+ return;@+} |
| 1516 |
if (floor(10000.0*d)==10000.0*d) |
| 1517 |
{ hwritef("%g",d); @+return;@+} |
| 1518 |
DBG(DBGFLOAT,"Writing hexadecimal float %f\n",d); |
| 1519 |
if (d<0) { hwritec('-');@+ d=-d;@+} |
| 1520 |
hwritef("0x"); |
| 1521 |
@<extract mantissa and exponent@>@; |
| 1522 |
if (exp>MAX_HEX_DIGITS) |
| 1523 |
@<write large numbers@>@; |
| 1524 |
else if (exp>=0) @<write medium numbers@>@; |
| 1525 |
else @<write small numbers@>@; |
| 1526 |
} |
| 1527 |
@ |
| 1528 |
|
| 1529 |
The extraction just reverses the creation of the binary representation. |
| 1530 |
|
| 1531 |
@<extract mantissa and exponent@>= |
| 1532 |
{ union {@+float64_t d; @+ uint64_t bits; @+} u; |
| 1533 |
u.d=d; @+ bits=u.bits; |
| 1534 |
} |
| 1535 |
mantissa= bits&(((uint64_t)1<<DBL_M_BITS)-1); |
| 1536 |
mantissa=mantissa+((uint64_t)1<<DBL_M_BITS); |
| 1537 |
exp= ((bits>>DBL_M_BITS)&((1<<DBL_E_BITS)-1))-DBL_EXCESS; |
| 1538 |
digits=DBL_M_BITS+1; |
| 1539 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 " binary exp=%d\n",@|digits,mantissa,exp); |
| 1540 |
@ |
| 1541 |
|
| 1542 |
After we have obtained the binary exponent, |
| 1543 |
we round it down, and convert it to a hexadecimal |
| 1544 |
exponent. |
| 1545 |
@<extract mantissa and exponent@>= |
| 1546 |
{ int r; |
| 1547 |
if (exp>=0) |
| 1548 |
{ r= exp%4; |
| 1549 |
if (r>0) |
| 1550 |
{ mantissa=mantissa<<r; @+exp=exp-r; @+digits=digits+r; @+} |
| 1551 |
} |
| 1552 |
else |
| 1553 |
{ r=(-exp)%4; |
| 1554 |
if (r>0) |
| 1555 |
{ mantissa=mantissa>>r; @+exp=exp+r; @+digits=digits-r;@+} |
| 1556 |
} |
| 1557 |
} |
| 1558 |
exp=exp/4; |
| 1559 |
DBG(DBGFLOAT,"\tdigits=%d mantissa=0x%" PRIx64 " hex exp=%d\n",@|digits,mantissa,exp); |
| 1560 |
@ |
| 1561 |
|
| 1562 |
In preparation for writing, |
| 1563 |
we shift the mantissa to the left so that the leftmost hexadecimal |
| 1564 |
digit of it will occupy the 4 leftmost bits of the variable |bits| . |
| 1565 |
|
| 1566 |
@<extract mantissa and exponent@>= |
| 1567 |
mantissa=mantissa<<(64-DBL_M_BITS-4); /* move leading digit to leftmost nibble */ |
| 1568 |
@ |
| 1569 |
|
| 1570 |
If the exponent is larger than |MAX_HEX_DIGITS|, we need to |
| 1571 |
use an exponent even if the mantissa uses only a few digits. |
| 1572 |
When we use an exponent, we always write exactly one digit preceding the radix point. |
| 1573 |
|
| 1574 |
@<write large numbers@>= |
| 1575 |
{ DBG(DBGFLOAT,"writing large number\n"); |
| 1576 |
hwritef("%X.",(uint8_t)(mantissa>>60)); |
| 1577 |
mantissa=mantissa<<4; |
| 1578 |
do { |
| 1579 |
hwritef("%X",(uint8_t)(mantissa>>DBL_M_BITS)&0xF); |
| 1580 |
mantissa=mantissa<<4; |
| 1581 |
} while (mantissa!=0); |
| 1582 |
hwritef("x+%X",exp); |
| 1583 |
} |
| 1584 |
@ |
| 1585 |
If the exponent is small and non negative, we can write the |
| 1586 |
number without an exponent by writing the radix point at the |
| 1587 |
appropriate place. |
| 1588 |
@<write medium numbers@>= |
| 1589 |
{ DBG(DBGFLOAT,"writing medium number\n"); |
| 1590 |
do { |
| 1591 |
hwritef("%X",(uint8_t)(mantissa>>60)); |
| 1592 |
mantissa=mantissa<<4; |
| 1593 |
if (exp--==0) hwritec('.'); |
| 1594 |
} while (mantissa!=0 || exp>=-1); |
| 1595 |
} |
| 1596 |
@ |
| 1597 |
Last non least, we write numbers that would require additional zeros after the |
| 1598 |
radix point with an exponent, because it keeps the mantissa shorter. |
| 1599 |
@<write small numbers@>= |
| 1600 |
{ DBG(DBGFLOAT,"writing small number\n"); |
| 1601 |
hwritef("%X.",(uint8_t)(mantissa>>60)); |
| 1602 |
mantissa=mantissa<<4; |
| 1603 |
do { |
| 1604 |
hwritef("%X",(uint8_t)(mantissa>>60)); |
| 1605 |
mantissa=mantissa<<4; |
| 1606 |
} while (mantissa!=0); |
| 1607 |
hwritef("x-%X",-exp); |
| 1608 |
} |
| 1609 |
@ |
| 1610 |
|
| 1611 |
Compared to the complications of long format floating point numbers, |
| 1612 |
the short format is very simple because we just use the binary representation. |
| 1613 |
Since 32 bit floating point numbers offer sufficient precision we use only |
| 1614 |
the |float32_t| type. |
| 1615 |
It is however not possible to just write |HPUT32(d)| for a |float32_t| variable |d| |
| 1616 |
or |HPUT32((uint32_t)d)| because in the \CEE\ language this would imply |
| 1617 |
rounding the floating point number to the nearest integer. |
| 1618 |
But we have seen how to convert floating point values to bit pattern before. |
| 1619 |
|
| 1620 |
@<put functions@>= |
| 1621 |
void hput_float32(float32_t d) |
| 1622 |
{ union {@+float32_t d; @+ uint32_t bits; @+} u; |
| 1623 |
u.d=d; @+ HPUT32(u.bits); |
| 1624 |
} |
| 1625 |
@ |
| 1626 |
|
| 1627 |
@<shared get functions@>= |
| 1628 |
float32_t hget_float32(void) |
| 1629 |
{ union {@+float32_t d; @+ uint32_t bits; @+} u; |
| 1630 |
HGET32(u.bits); |
| 1631 |
return u.d; |
| 1632 |
} |
| 1633 |
@ |
| 1634 |
|
| 1635 |
\subsection{Fixed Point Numbers} |
| 1636 |
\TeX\ internally represents most real numbers as fixed\index{fixed |
| 1637 |
point number} point numbers or ``scaled integers''\index{scaled |
| 1638 |
integer}. The type {\bf Scaled} is defined as a signed 32 bit |
| 1639 |
integer, but we consider it as a fixed point number with the binary |
| 1640 |
radix point just in the middle with sixteen bits before and sixteen |
| 1641 |
bits after it. To convert an integer into a scaled number, we |
| 1642 |
multiply it by |ONE|; to convert a floating point number into a scaled |
| 1643 |
number, we multiply it by |ONE| and |ROUND| the result to the nearest |
| 1644 |
integer; to convert a scaled number to a floating point number we |
| 1645 |
divide it by |(float64_t)ONE|. |
| 1646 |
|
| 1647 |
\noindent |
| 1648 |
@<hint basic types@>= |
| 1649 |
typedef int32_t Scaled; |
| 1650 |
#define ONE ((Scaled)(1<<16)) |
| 1651 |
@ |
| 1652 |
|
| 1653 |
@<hint macros@>= |
| 1654 |
#define ROUND(X) ((int)((X)>=0.0?floor((X)+0.5):ceil((X)-0.5))) |
| 1655 |
@ |
| 1656 |
|
| 1657 |
\writecode |
| 1658 |
@<write functions@>= |
| 1659 |
void hwrite_scaled(Scaled x) |
| 1660 |
{ hwrite_float64(x/(float64_t)ONE, false); |
| 1661 |
} |
| 1662 |
@ |
| 1663 |
|
| 1664 |
\subsection{Dimensions} |
| 1665 |
In the long format, |
| 1666 |
the dimensions\index{dimension} of characters, boxes, and other things can be given |
| 1667 |
in three units: \.{pt}, \.{in}, and \.{mm}. |
| 1668 |
|
| 1669 |
\readcode |
| 1670 |
@s PT symbol |
| 1671 |
@s MM symbol |
| 1672 |
@s INCH symbol |
| 1673 |
@s dimension symbol |
| 1674 |
@s DIMEN symbol |
| 1675 |
@<symbols@>= |
| 1676 |
%token DIMEN "dimen" |
| 1677 |
%token PT "pt" |
| 1678 |
%token MM "mm" |
| 1679 |
%token INCH "in" |
| 1680 |
%type <d> dimension |
| 1681 |
@ |
| 1682 |
|
| 1683 |
@<scanning rules@>= |
| 1684 |
::@=dimen@> :< return DIMEN; >: |
| 1685 |
::@=pt@> :< return PT; >: |
| 1686 |
::@=mm@> :< return MM; >: |
| 1687 |
::@=in@> :< return INCH; >: |
| 1688 |
@ |
| 1689 |
|
| 1690 |
The unit \.{pt} is a printers point\index{point}\index{pt+{\tt pt}}. |
| 1691 |
The unit ``\.{in}'' stands for inches\index{inch}\index{in+{\tt in}} and we have $1\.{in}= 72.27\,\.{pt}$. |
| 1692 |
The unit ``\.{mm}'' stands for millimeter\index{millimeter}\index{mm+{\tt mm}} and we have $1\.{in}= 25.4\,\.{mm}$. |
| 1693 |
|
| 1694 |
The definition of a printers\index{printers point} point given above follows the definition used in |
| 1695 |
\TeX\ which is slightly larger than the official definition of a printer's |
| 1696 |
point which was defined to equal exactly 0.013837\.{in} by the American Typefounders |
| 1697 |
Association in~1886\cite{DK:texbook}. |
| 1698 |
|
| 1699 |
We follow the tradition of \TeX\ and |
| 1700 |
store dimensions as ``scaled points''\index{scaled point} that is a dimension of $d$ points is |
| 1701 |
stored as $d\cdot2^{16}$ rounded to the nearest integer. |
| 1702 |
The maximum absolute value of a dimension is $(2^{30}-1)$ scaled points. |
| 1703 |
|
| 1704 |
@<hint basic types@>= |
| 1705 |
typedef Scaled Dimen; |
| 1706 |
#define MAX_DIMEN ((Dimen)(0x3FFFFFFF)) |
| 1707 |
@ |
| 1708 |
|
| 1709 |
@<parsing rules@>= |
| 1710 |
dimension: number PT @|{$$=ROUND($1*ONE); RNG("Dimension",$$,-MAX_DIMEN,MAX_DIMEN); } |
| 1711 |
| number INCH @|{$$=ROUND($1*ONE*72.27); RNG("Dimension",$$,-MAX_DIMEN,MAX_DIMEN);@+} |
| 1712 |
| number MM @|{$$=ROUND($1*ONE*(72.27/25.4)); RNG("Dimension",$$,-MAX_DIMEN,MAX_DIMEN);@+}; |
| 1713 |
@ |
| 1714 |
|
| 1715 |
When \.{stretch} is writing dimensions in the long format, |
| 1716 |
for simplicity it always uses the unit ``\.{pt}''. |
| 1717 |
\writecode |
| 1718 |
@<write functions@>= |
| 1719 |
void hwrite_dimension(Dimen x) |
| 1720 |
{ hwrite_scaled(x); |
| 1721 |
hwritef("pt"); |
| 1722 |
} |
| 1723 |
@ |
| 1724 |
|
| 1725 |
In the short format, dimensions are stored as 32 bit scaled point values without conversion. |
| 1726 |
\getcode |
| 1727 |
@<get functions@>= |
| 1728 |
void hget_dimen(Tag a) |
| 1729 |
{ if (INFO(a)==b000) |
| 1730 |
{uint8_t r; r=HGET8; REF(dimen_kind,r); hwrite_ref(r);} |
| 1731 |
else |
| 1732 |
{ uint32_t d; HGET32(d); hwrite_dimension(d); } |
| 1733 |
} |
| 1734 |
@ |
| 1735 |
|
| 1736 |
\putcode |
| 1737 |
@<put functions@>= |
| 1738 |
|
| 1739 |
Tag hput_dimen(Dimen d) |
| 1740 |
{ HPUT32(d); |
| 1741 |
return TAG(dimen_kind, b001); |
| 1742 |
} |
| 1743 |
@ |
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
\subsection{Extended Dimensions}\index{extended dimension}\index{hsize+{\tt hsize}}\index{vsize+{\tt vsize}} |
| 1748 |
The dimension that is probably used most frequently in a \TeX\ file is {\tt hsize}: |
| 1749 |
the ho\-ri\-zon\-tal size of a line of text. Common are also assignments |
| 1750 |
like \.{\\hsize=0.5\\hsize} \.{\\advance\\hsize by -10pt}, for example to |
| 1751 |
get two columns with lines almost half as wide as usual, leaving a small gap |
| 1752 |
between left and right column. Similar considerations apply to {\tt vsize}. |
| 1753 |
|
| 1754 |
Because we aim at a reflowable format for \TeX\ output, we have to postpone |
| 1755 |
such computations until the values of \.{hsize} and \.{vsize} are known in the viewer. |
| 1756 |
Until then, we do symbolic computations on linear functions\index{linear function} of \.{hsize} and \.{vsize}. |
| 1757 |
We call such a linear function $w+h\cdot\.{hsize}+v\cdot\.{vsize}$ |
| 1758 |
an extended dimension and represent it by the three numbers $w$, $h$, and $v$. |
| 1759 |
|
| 1760 |
@<hint basic types@>= |
| 1761 |
typedef struct {@+ |
| 1762 |
Dimen w; @+ float32_t h, v; @+ |
| 1763 |
} Xdimen; |
| 1764 |
@ |
| 1765 |
Since very often a component of an extended dimension is zero, we |
| 1766 |
store in the short format only the nonzero components and use the |
| 1767 |
info bits to mark them: |b100| implies $|w|\ne0$, |
| 1768 |
|b010| implies $|h|\ne 0$, and |b001| implies $|v|\ne 0$. |
| 1769 |
|
| 1770 |
\readcode |
| 1771 |
@s XDIMEN symbol |
| 1772 |
@s xdimen symbol |
| 1773 |
@s xdimen_node symbol |
| 1774 |
@s H symbol |
| 1775 |
@s V symbol |
| 1776 |
@<symbols@>= |
| 1777 |
%token XDIMEN "xdimen" |
| 1778 |
%token H "h" |
| 1779 |
%token V "v" |
| 1780 |
%type <xd> xdimen |
| 1781 |
@ |
| 1782 |
@<scanning rules@>= |
| 1783 |
::@=xdimen@> :< return XDIMEN; >: |
| 1784 |
::@=h@> :< return H; >: |
| 1785 |
::@=v@> :< return V; >: |
| 1786 |
@ |
| 1787 |
|
| 1788 |
|
| 1789 |
@<parsing rules@>= |
| 1790 |
xdimen: dimension number H number V { $$.w=$1; @+$$.h=$2; @+$$.v=$4; } |
| 1791 |
| dimension number H { $$.w=$1; @+$$.h=$2; @+$$.v=0.0; } |
| 1792 |
| dimension number V { $$.w=$1; @+$$.h=0.0; @+$$.v=$2; } |
| 1793 |
| dimension { $$.w=$1; @+$$.h=0.0; @+$$.v=0.0; }; |
| 1794 |
|
| 1795 |
xdimen_node: start XDIMEN xdimen END { hput_tags($1,hput_xdimen(&($3))); }; |
| 1796 |
@ |
| 1797 |
|
| 1798 |
\writecode |
| 1799 |
@<write functions@>= |
| 1800 |
void hwrite_xdimen(Xdimen *x) |
| 1801 |
{ hwrite_dimension(x->w); |
| 1802 |
if (x->h!=0.0) {hwrite_float64(x->h,true); @+hwritec('h');@+} |
| 1803 |
if (x->v!=0.0) {hwrite_float64(x->v,true); @+hwritec('v');@+} |
| 1804 |
} |
| 1805 |
|
| 1806 |
void hwrite_xdimen_node(Xdimen *x) |
| 1807 |
{ hwrite_start(); hwritef("xdimen"); hwrite_xdimen(x); hwrite_end();} |
| 1808 |
@ |
| 1809 |
|
| 1810 |
\getcode |
| 1811 |
|
| 1812 |
@<get macros@>= |
| 1813 |
#define @[HGET_XDIMEN(I,X)@] \ |
| 1814 |
if((I)&b100) HGET32((X).w);@+ else (X).w=0;\ |
| 1815 |
if((I)&b010) (X).h=hget_float32(); @+ else (X).h=0.0;\ |
| 1816 |
if((I)&b001) (X).v=hget_float32(); @+else (X).v=0.0; |
| 1817 |
@ |
| 1818 |
|
| 1819 |
@<get functions@>= |
| 1820 |
void hget_xdimen(Tag a, Xdimen *x) |
| 1821 |
{ switch(a) |
| 1822 |
{ |
| 1823 |
case TAG(xdimen_kind,b001): HGET_XDIMEN(b001,*x);@+break; |
| 1824 |
case TAG(xdimen_kind,b010): HGET_XDIMEN(b010,*x);@+break; |
| 1825 |
case TAG(xdimen_kind,b011): HGET_XDIMEN(b011,*x);@+break; |
| 1826 |
case TAG(xdimen_kind,b100): HGET_XDIMEN(b100,*x);@+break; |
| 1827 |
case TAG(xdimen_kind,b101): HGET_XDIMEN(b101,*x);@+break; |
| 1828 |
case TAG(xdimen_kind,b110): HGET_XDIMEN(b110,*x);@+break; |
| 1829 |
case TAG(xdimen_kind,b111): HGET_XDIMEN(b111,*x);@+break; |
| 1830 |
default: |
| 1831 |
QUIT("Extent expected got [%s,%d]",NAME(a),INFO(a)); |
| 1832 |
} |
| 1833 |
} |
| 1834 |
@ |
| 1835 |
|
| 1836 |
Note that the info value |b000|, usually indicating a reference, |
| 1837 |
is not supported for extended dimensions. |
| 1838 |
Most nodes that need an extended dimension offer the opportunity to give |
| 1839 |
a reference directly without the start and end byte. |
| 1840 |
An exception is the glue node, |
| 1841 |
but glue nodes that need an extended width are rare. |
| 1842 |
|
| 1843 |
@<get functions@>= |
| 1844 |
void hget_xdimen_node(Xdimen *x) |
| 1845 |
{ @<read the start byte |a|@>@; |
| 1846 |
if (KIND(a)==xdimen_kind) |
| 1847 |
hget_xdimen(a,x); |
| 1848 |
else |
| 1849 |
QUIT("Extent expected at 0x%x got %s",node_pos,NAME(a)); |
| 1850 |
@<read and check the end byte |z|@>@; |
| 1851 |
} |
| 1852 |
@ |
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
\putcode |
| 1857 |
@<put functions@>= |
| 1858 |
Tag hput_xdimen(Xdimen *x) |
| 1859 |
{ Info info=b000; |
| 1860 |
if (x->w==0 && x->h==0.0 && x->v==0.0){ HPUT32(0); @+info|=b100; @+} |
| 1861 |
else |
| 1862 |
{ if (x->w!=0) { HPUT32(x->w); @+info|=b100; @+} |
| 1863 |
if (x->h!=0.0) { hput_float32(x->h); @+info|=b010; @+} |
| 1864 |
if (x->v!=0.0) { hput_float32(x->v); @+info|=b001; @+} |
| 1865 |
} |
| 1866 |
return TAG(xdimen_kind,info); |
| 1867 |
} |
| 1868 |
void hput_xdimen_node(Xdimen *x) |
| 1869 |
{ uint32_t p=hpos++-hstart; |
| 1870 |
hput_tags(p, hput_xdimen(x)); |
| 1871 |
} |
| 1872 |
|
| 1873 |
|
| 1874 |
@ |
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
\subsection{Stretch and Shrink}\label{stretch} |
| 1879 |
In section~\secref{glue}, we will consider glue\index{glue} which |
| 1880 |
is something that can stretch and shrink. |
| 1881 |
The stretchability\index{stretchability} and shrinkability\index{shrinkability} of the |
| 1882 |
glue can be given in ``\.{pt}'' like a dimension, |
| 1883 |
but there are three more units: \.{fil}, \.{fill}, and \.{filll}. |
| 1884 |
A glue with a stretchability of $1\,\hbox{\tt fil}$ will stretch infinitely more |
| 1885 |
than a glue with a stretchability of $1\,\hbox{\tt pt}$. So if you stretch both glues |
| 1886 |
together, the first glue will do all the stretching and the latter will not stretch |
| 1887 |
at all. The ``\.{fil}'' glue has simply a higher order of infinity. |
| 1888 |
You might guess that ``\.{fill}'' glue and ``\.{filll}'' glue have even higher |
| 1889 |
orders of infinite stretchability. |
| 1890 |
The order of infinity is 0 for \.{pt}, 1 for \.{fil}, 2 for \.{fill}, and 3 for \.{filll}. |
| 1891 |
|
| 1892 |
The internal representation of a stretch is a variable of type |Stretch|. |
| 1893 |
It stores the floating point value and the order of infinity separate as a |float64_t| and a |uint8_t|. |
| 1894 |
|
| 1895 |
|
| 1896 |
The short format tries to be space efficient and because it is not necessary to give the |
| 1897 |
stretchability with a precision exceeding about six decimal digits, |
| 1898 |
we use a single 32 bit floating point value. |
| 1899 |
To write a |float32_t| value and an order value as one 32 bit value, |
| 1900 |
we round the two lowest bit of the |float32_t| variable to zero |
| 1901 |
using ``round to even'' and store the order of infinity in these bits. |
| 1902 |
We define a union type \&{Stch} to simplify conversion. |
| 1903 |
|
| 1904 |
@<hint basic types@>= |
| 1905 |
typedef enum { @+ normal_o=0, fil_o=1, fill_o=2, filll_o=3@+} Order; |
| 1906 |
typedef struct {@+ float64_t f;@+ Order o; @+} Stretch; |
| 1907 |
typedef union {@+float32_t f; @+ uint32_t u; @+} Stch; |
| 1908 |
@ |
| 1909 |
|
| 1910 |
\putcode |
| 1911 |
@<put functions@>= |
| 1912 |
void hput_stretch(Stretch *s) |
| 1913 |
{ uint32_t mantissa, lowbits, sign, exponent; |
| 1914 |
Stch st; |
| 1915 |
st.f=s->f; |
| 1916 |
DBG(DBGFLOAT,"joining %f->%f(0x%X),%d:",s->f,st.f,st.u,s->o); |
| 1917 |
mantissa = st.u &(((uint32_t)1<<FLT_M_BITS)-1); |
| 1918 |
lowbits = mantissa&0x7; /* lowest 3 bits */ |
| 1919 |
exponent=(st.u>>FLT_M_BITS)&(((uint32_t)1<<FLT_E_BITS)-1); |
| 1920 |
sign=st.u & ((uint32_t)1<<(FLT_E_BITS+FLT_M_BITS)); |
| 1921 |
DBG(DBGFLOAT,"s=%d e=0x%x m=0x%x",sign, exponent, mantissa); |
| 1922 |
switch (lowbits) /* round to even */ |
| 1923 |
{ @+case 0: break; /* no change */ |
| 1924 |
case 1: mantissa = mantissa -1; @+break;/* round down */ |
| 1925 |
case 2: mantissa = mantissa -2; @+break;/* round down to even */ |
| 1926 |
case 3: mantissa = mantissa +1; @+break; /* round up */ |
| 1927 |
case 4: break; /* no change */ |
| 1928 |
case 5: mantissa = mantissa -1; @+break;/* round down */ |
| 1929 |
case 6: mantissa = mantissa +1; /* round up to even, fall through */ |
| 1930 |
case 7: mantissa = mantissa +1; /* round up to even */ |
| 1931 |
if (mantissa >= ((uint32_t)1<<FLT_M_BITS))@/ |
| 1932 |
{exponent++; /* adjust exponent */ |
| 1933 |
RNG("Float32 exponent",exponent,1,2*FLT_EXCESS); |
| 1934 |
@+mantissa=mantissa>>1; |
| 1935 |
} |
| 1936 |
break; |
| 1937 |
} |
| 1938 |
DBG(DBGFLOAT," round s=%d e=0x%x m=0x%x",sign, exponent, mantissa); |
| 1939 |
st.u=sign| (exponent<<FLT_M_BITS) | mantissa | s->o; |
| 1940 |
DBG(DBGFLOAT,"float %f hex 0x%x\n",st.f, st.u); |
| 1941 |
HPUT32(st.u); |
| 1942 |
} |
| 1943 |
@ |
| 1944 |
|
| 1945 |
\getcode |
| 1946 |
@<get macros@>= |
| 1947 |
#define @[HGET_STRETCH(S)@] { Stch st; @+ HGET32(st.u);@+ S.o=st.u&3; st.u&=~3; S.f=st.f; @+} |
| 1948 |
@ |
| 1949 |
|
| 1950 |
\readcode |
| 1951 |
@s FIL symbol |
| 1952 |
@s FILL symbol |
| 1953 |
@s FILLL symbol |
| 1954 |
@s order symbol |
| 1955 |
|
| 1956 |
@<symbols@>= |
| 1957 |
%token FIL "fil" |
| 1958 |
%token FILL "fill" |
| 1959 |
%token FILLL "filll" |
| 1960 |
%type <st> stretch |
| 1961 |
%type <o> order |
| 1962 |
@ |
| 1963 |
|
| 1964 |
@<scanning rules@>= |
| 1965 |
::@=fil@> :< return FIL; >: |
| 1966 |
::@=fill@> :< return FILL; >: |
| 1967 |
::@=filll@> :< return FILLL; >: |
| 1968 |
@ |
| 1969 |
|
| 1970 |
@s stretch symbol |
| 1971 |
@s Stretch int |
| 1972 |
@<parsing rules@>= |
| 1973 |
|
| 1974 |
order: PT {$$=normal_o;} | FIL {$$=fil_o;} @+| FILL {$$=fill_o;} @+| FILLL {$$=filll_o;}; |
| 1975 |
|
| 1976 |
stretch: number order { $$.f=$1; $$.o=$2; }; |
| 1977 |
@ |
| 1978 |
|
| 1979 |
\writecode |
| 1980 |
|
| 1981 |
@<write functions@>= |
| 1982 |
void hwrite_order(Order o) |
| 1983 |
{ switch (o) |
| 1984 |
{ case normal_o: hwritef("pt"); @+break; |
| 1985 |
case fil_o: hwritef("fil"); @+break; |
| 1986 |
case fill_o: hwritef("fill"); @+break; |
| 1987 |
case filll_o: hwritef("filll"); @+break; |
| 1988 |
default: QUIT("Illegal order %d",o); @+ break; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
void hwrite_stretch(Stretch *s) |
| 1993 |
{ hwrite_float64(s->f, false); |
| 1994 |
hwrite_order(s->o); |
| 1995 |
} |
| 1996 |
@ |
| 1997 |
|
| 1998 |
\section{Simple Nodes}\hascode |
| 1999 |
\label{simple} |
| 2000 |
\subsection{Penalties} |
| 2001 |
Penalties\index{penalty} are very simple nodes. They specify the cost of breaking a |
| 2002 |
line or page at the present position. For the internal representation |
| 2003 |
we use an |int32_t|. The full range of integers is, however, not |
| 2004 |
used. Instead penalties must be between -20000 and +20000. |
| 2005 |
(\TeX\ specifies a range of -10000 to +10000, but plain \TeX\ uses the value -20000 |
| 2006 |
when it defines the supereject control sequence.) |
| 2007 |
The more general node is called an integer node; |
| 2008 |
it shares the same kind-value |int_kind=penalty_kind| |
| 2009 |
but allows the full range of values. |
| 2010 |
The info value of a penalty node is 1 or 2 and indicates the number of bytes |
| 2011 |
used to store the integer. The info value 3 can be used for general |
| 2012 |
integers (see section~\secref{definitions}) that need four byte of storage. |
| 2013 |
|
| 2014 |
\readcode |
| 2015 |
@s PENALTY symbol |
| 2016 |
@s INTEGER symbol |
| 2017 |
@s penalty symbol |
| 2018 |
@<symbols@>= |
| 2019 |
%token PENALTY "penalty" |
| 2020 |
%token INTEGER "int" |
| 2021 |
%type <i> penalty |
| 2022 |
@ |
| 2023 |
|
| 2024 |
@<scanning rules@>= |
| 2025 |
::@=penalty@> :< return PENALTY; >: |
| 2026 |
::@=int@> :< return INTEGER; >: |
| 2027 |
@ |
| 2028 |
|
| 2029 |
@<parsing rules@>= |
| 2030 |
penalty: integer {RNG("Penalty",$1,-20000,+20000);$$=$1;}; |
| 2031 |
content_node: start PENALTY penalty END { hput_tags($1,hput_int($3));@+}; |
| 2032 |
@ |
| 2033 |
|
| 2034 |
\getcode |
| 2035 |
@<cases to get content@>= |
| 2036 |
@t\1\kern1em@>case TAG(penalty_kind,1): @+{int32_t p;@+ HGET_PENALTY(1,p);@+} @+break; |
| 2037 |
case TAG(penalty_kind,2): @+{int32_t p;@+ HGET_PENALTY(2,p);@+} @+break; |
| 2038 |
case TAG(penalty_kind,3): @+{int32_t p;@+ HGET_PENALTY(3,p);@+} @+break; |
| 2039 |
@ |
| 2040 |
|
| 2041 |
@<get macros@>= |
| 2042 |
#define @[HGET_PENALTY(I,P)@] \ |
| 2043 |
if (I==1) {int8_t n=HGET8; @+P=n;@+ } \ |
| 2044 |
else if (I==2) {int16_t n;@+ HGET16(n);@+RNG("Penalty",n,-20000,+20000); @+ P=n; @+}\ |
| 2045 |
else if (I==3) {int32_t n;@+ HGET32(n);@+RNG("Penalty",n,-20000,+20000); @+ P=n; @+}\ |
| 2046 |
hwrite_signed(P); |
| 2047 |
@ |
| 2048 |
|
| 2049 |
\putcode |
| 2050 |
@<put functions@>= |
| 2051 |
Tag hput_int(int32_t n) |
| 2052 |
{ Info info; |
| 2053 |
if (n>=0) @/ |
| 2054 |
{ @+if (n<0x80) { @+HPUT8(n); @+info=1;@+ } |
| 2055 |
else if (n<0x8000) {@+ HPUT16(n);@+ info=2;@+ } |
| 2056 |
else {@+ HPUT32(n);@+ info=3;@+ } |
| 2057 |
} |
| 2058 |
else@/ |
| 2059 |
{@+ if (n>=-0x80) {@+ HPUT8(n);@+ info=1;@+ } |
| 2060 |
else if (n>=-0x8000) {@+ HPUT16(n);@+ info=2;@+ } |
| 2061 |
else {@+ HPUT32(n);@+ info=3;@+ } |
| 2062 |
} |
| 2063 |
return TAG(int_kind,info); |
| 2064 |
} |
| 2065 |
@ |
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
\subsection{Languages} |
| 2071 |
To render a \HINT\ file on screen, information about the language is not necessary. |
| 2072 |
Knowing the language is, however, very important for language translation and |
| 2073 |
text to speech conversion which makes texts accessible to the visually-impaired. |
| 2074 |
For this reason, \HINT\ offers the opportunity to add this information |
| 2075 |
and encourages authors to supply this information. |
| 2076 |
|
| 2077 |
Language information by itself is not sufficient to decode text. It must be supplemented |
| 2078 |
by information about the character encoding (see section~\secref{fonts}). |
| 2079 |
|
| 2080 |
To represent language information, the world wide web has set universally |
| 2081 |
accepted standards. The Internet Engineering Task Force IETF has defined tags for identifying |
| 2082 |
languages\cite{rfc5646}: short strings like ``en'' for English |
| 2083 |
or ``de'' for Deutsch, and longer ones like ``sl-IT-nedis'', for the specific variant of |
| 2084 |
the Nadiza dialect of Slovenian that is spoken in Italy. |
| 2085 |
We assume that any \HINT\ file |
| 2086 |
will contain only a small number of different languages and all language nodes can be |
| 2087 |
encoded using a reference to a predefined node from the |
| 2088 |
definition section (see section~\secref{reference}). |
| 2089 |
In the definition section, a language node will just |
| 2090 |
contain the language tag as given in~\cite{iana:language} (see section~\secref{definitions}). |
| 2091 |
|
| 2092 |
\readcode |
| 2093 |
@s LANGUAGE symbol |
| 2094 |
@s language symbol |
| 2095 |
|
| 2096 |
@<symbols@>= |
| 2097 |
%token LANGUAGE "language" |
| 2098 |
@ |
| 2099 |
|
| 2100 |
@<scanning rules@>= |
| 2101 |
::@=language@> :< return LANGUAGE; >: |
| 2102 |
@ |
| 2103 |
|
| 2104 |
When encoding language nodes in the short format, |
| 2105 |
we use the info value |b000| for language nodes in the definition section |
| 2106 |
and for language nodes in the content section that contain just a one-byte |
| 2107 |
reference (see section~\secref{reference}). |
| 2108 |
We use the info value |1| to |7| as a shorthand for |
| 2109 |
the references {\tt *0} and {\tt *6} to the predefined language nodes. |
| 2110 |
|
| 2111 |
|
| 2112 |
\goodbreak |
| 2113 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 2114 |
@<cases to get content@>= |
| 2115 |
@t\1\kern1em@>case TAG(language_kind,1): REF(language_kind,0); @+hwrite_ref(0); @+break; |
| 2116 |
case TAG(language_kind,2): REF(language_kind,1); @+hwrite_ref(1); @+break; |
| 2117 |
case TAG(language_kind,3): REF(language_kind,2); @+hwrite_ref(2); @+break; |
| 2118 |
case TAG(language_kind,4): REF(language_kind,3); @+hwrite_ref(3); @+break; |
| 2119 |
case TAG(language_kind,5): REF(language_kind,4); @+hwrite_ref(4); @+break; |
| 2120 |
case TAG(language_kind,6): REF(language_kind,5); @+hwrite_ref(5); @+break; |
| 2121 |
case TAG(language_kind,7): REF(language_kind,6); @+hwrite_ref(6); @+break; |
| 2122 |
@ |
| 2123 |
|
| 2124 |
\putcode |
| 2125 |
@<put functions@>= |
| 2126 |
Tag hput_language(uint8_t n) |
| 2127 |
{ if (n<7) return TAG(language_kind,n+1); |
| 2128 |
HPUT8(n); return TAG(language_kind,0); |
| 2129 |
} |
| 2130 |
@ |
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
\subsection{Rules} |
| 2135 |
Rules\index{rule} are simply black rectangles having a height, a depth, and a |
| 2136 |
width. All of these dimensions can also be negative but a rule will |
| 2137 |
not be visible unless its width is positive and its height plus depth |
| 2138 |
is positive. |
| 2139 |
|
| 2140 |
As a specialty, rules can have ``running dimensions''\index{running dimension}. If any of the |
| 2141 |
three dimensions is a running dimension, its actual value will be |
| 2142 |
determined by running the rule up to the boundary of the innermost |
| 2143 |
enclosing box. The width is never running in an horizontal\index{horizontal list} list; the |
| 2144 |
height and depth are never running in a vertical\index{vertical list} list. In the long |
| 2145 |
format, we use a vertical bar ``{\tt \VB}'' or a horizontal bar |
| 2146 |
``{\tt \_}'' (underscore character) to indicate a running |
| 2147 |
dimension. Of course the vertical bar is meant to indicate a running |
| 2148 |
height or depth while the horizontal bar stands for a running |
| 2149 |
width. The parser, however, makes no distinction between the two and |
| 2150 |
you can use either of them. In the short format, we follow \TeX\ and |
| 2151 |
implement a running dimension by using the special value |
| 2152 |
$-2^{30}=|0xC0000000|$. |
| 2153 |
|
| 2154 |
|
| 2155 |
@<hint macros@>= |
| 2156 |
#define RUNNING_DIMEN 0xC0000000 |
| 2157 |
@ |
| 2158 |
|
| 2159 |
It could have been possible to allow extended dimensions in a rule node, |
| 2160 |
but in most circumstances, the mechanism of running dimensions is sufficient |
| 2161 |
and simpler to use. If a rule is needed that requires an extended dimension as |
| 2162 |
its length, it is always possible to put it inside a suitable box and use a |
| 2163 |
running dimension. |
| 2164 |
|
| 2165 |
|
| 2166 |
To make the short format encoding more compact, the first info bit |
| 2167 |
|b100| will be zero to indicate a running height, bit |b010| will be |
| 2168 |
zero to indicate a running depth, and bit |b001| will be zero to |
| 2169 |
indicate a running width. |
| 2170 |
|
| 2171 |
Because leaders\index{leaders} (see section~\secref{leaders}) may contain a rule |
| 2172 |
node, we also provide functions to read and write a complete rule |
| 2173 |
node. While parsing the symbol ``{\sl rule\/}'' will just initialize a variable of type |
| 2174 |
\&{Rule} (the writing is done with a separate routine), |
| 2175 |
parsing a {\sl rule\_node\/} will always include writing it. |
| 2176 |
|
| 2177 |
% Currently no predefined rules. |
| 2178 |
%Further, a {\sl rule\_node} will permit the |
| 2179 |
%use of a predefined rule (see section~\secref{reference}), |
| 2180 |
|
| 2181 |
|
| 2182 |
@<hint types@>= |
| 2183 |
typedef struct {@+ |
| 2184 |
Dimen h,d,w; @+ |
| 2185 |
} Rule; |
| 2186 |
@ |
| 2187 |
|
| 2188 |
\readcode |
| 2189 |
@s RULE symbol |
| 2190 |
@s RUNNING symbol |
| 2191 |
@s rule_dimension symbol |
| 2192 |
@s rule symbol |
| 2193 |
@s rule_node symbol |
| 2194 |
@<symbols@>= |
| 2195 |
%token RULE "rule" |
| 2196 |
%token RUNNING "|" |
| 2197 |
%type <d> rule_dimension |
| 2198 |
%type <r> rule |
| 2199 |
@ |
| 2200 |
|
| 2201 |
@<scanning rules@>= |
| 2202 |
::@=rule@> :< return RULE; >: |
| 2203 |
::@="|"@> :< return RUNNING; >: |
| 2204 |
::@="_"@> :< return RUNNING; >: |
| 2205 |
@ |
| 2206 |
|
| 2207 |
@<parsing rules@>= |
| 2208 |
rule_dimension: dimension@+ | RUNNING {$$=RUNNING_DIMEN;}; |
| 2209 |
rule: rule_dimension rule_dimension rule_dimension @/ |
| 2210 |
{ $$.h=$1; @+ $$.d=$2; @+ $$.w=$3; |
| 2211 |
if ($3==RUNNING_DIMEN && ($1==RUNNING_DIMEN || $2==RUNNING_DIMEN)) |
| 2212 |
QUIT("Incompatible running dimensions 0x%x 0x%x 0x%x",@|$1,$2,$3); }; |
| 2213 |
rule_node: start RULE rule END { hput_tags($1,hput_rule(&($3))); }; |
| 2214 |
content_node: rule_node; |
| 2215 |
@ |
| 2216 |
|
| 2217 |
\writecode |
| 2218 |
@<write functions@>= |
| 2219 |
static void hwrite_rule_dimension(Dimen d, char c) |
| 2220 |
{ @+if (d==RUNNING_DIMEN) hwritef(" %c",c); |
| 2221 |
else hwrite_dimension(d); |
| 2222 |
} |
| 2223 |
|
| 2224 |
void hwrite_rule(Rule *r) |
| 2225 |
{ @+hwrite_rule_dimension(r->h,'|'); |
| 2226 |
hwrite_rule_dimension(r->d,'|'); |
| 2227 |
hwrite_rule_dimension(r->w,'_'); |
| 2228 |
} |
| 2229 |
@ |
| 2230 |
\getcode |
| 2231 |
@<cases to get content@>= |
| 2232 |
@t\1\kern1em@> |
| 2233 |
case TAG(rule_kind,b011): {Rule r;@+ HGET_RULE(b011,r); @+hwrite_rule(&(r));@+ } @+break; |
| 2234 |
case TAG(rule_kind,b101): {Rule r;@+ HGET_RULE(b101,r); @+hwrite_rule(&(r));@+ } @+break; |
| 2235 |
case TAG(rule_kind,b001): {Rule r;@+ HGET_RULE(b001,r); @+hwrite_rule(&(r));@+ } @+break; |
| 2236 |
case TAG(rule_kind,b110): {Rule r;@+ HGET_RULE(b110,r); @+hwrite_rule(&(r));@+ } @+break; |
| 2237 |
case TAG(rule_kind,b111): {Rule r;@+ HGET_RULE(b111,r); @+hwrite_rule(&(r));@+ } @+break; |
| 2238 |
@ |
| 2239 |
|
| 2240 |
@<get macros@>= |
| 2241 |
#define @[HGET_RULE(I,R)@]@/\ |
| 2242 |
if ((I)&b100) HGET32((R).h); @+else (R).h=RUNNING_DIMEN;\ |
| 2243 |
if ((I)&b010) HGET32((R).d); @+else (R).d=RUNNING_DIMEN;\ |
| 2244 |
if ((I)&b001) HGET32((R).w); @+else (R).w=RUNNING_DIMEN; |
| 2245 |
@ |
| 2246 |
|
| 2247 |
@<get functions@>= |
| 2248 |
void hget_rule_node(void) |
| 2249 |
{ @<read the start byte |a|@>@; |
| 2250 |
if (KIND(a)==rule_kind) @/ |
| 2251 |
{ @+Rule r; @+HGET_RULE(INFO(a),r); @/ |
| 2252 |
hwrite_start();@+ hwritef("rule"); @+hwrite_rule(&r); @+hwrite_end(); |
| 2253 |
} |
| 2254 |
else |
| 2255 |
QUIT("Rule expected at 0x%x got %s",node_pos,NAME(a)); |
| 2256 |
@<read and check the end byte |z|@>@; |
| 2257 |
} |
| 2258 |
@ |
| 2259 |
|
| 2260 |
\putcode |
| 2261 |
@<put functions@>= |
| 2262 |
Tag hput_rule(Rule *r) |
| 2263 |
{ Info info=b000; |
| 2264 |
if (r->h!=RUNNING_DIMEN) { HPUT32(r->h); @+info|=b100; @+} |
| 2265 |
if (r->d!=RUNNING_DIMEN) { HPUT32(r->d); @+info|=b010; @+} |
| 2266 |
if (r->w!=RUNNING_DIMEN) { HPUT32(r->w); @+info|=b001; @+} |
| 2267 |
return TAG(rule_kind,info); |
| 2268 |
} |
| 2269 |
@ |
| 2270 |
|
| 2271 |
|
| 2272 |
\subsection{Kerns} |
| 2273 |
A kern\index{kern} is a bit of white space with a certain length. If the kern is part of a |
| 2274 |
horizontal list, the length is measured in the horizontal direction, |
| 2275 |
if it is part of a vertical list, it is measured in the vertical |
| 2276 |
direction. The length of a kern is mostly given as a dimension |
| 2277 |
but provisions are made to use extended dimensions as well. |
| 2278 |
|
| 2279 |
The typical |
| 2280 |
use of a kern is its insertion between two characters to make the natural |
| 2281 |
distance between them a bit wider or smaller. In the latter case, the kern |
| 2282 |
has a negative length. The typographic optimization just described is called |
| 2283 |
``kerning'' and has given the kern node its name. |
| 2284 |
Kerns inserted from font information or math mode calculations are normal kerns, |
| 2285 |
while kerns inserted from \TeX's {\tt \BS kern} or {\tt \BS/} |
| 2286 |
commands are explicit kerns. |
| 2287 |
Kern nodes do not disappear at a line break unless they are explicit\index{explicit kern}. |
| 2288 |
|
| 2289 |
In the long format, explicit kerns are marked with an ``!'' sign |
| 2290 |
and in the short format with the |b100| info bit. |
| 2291 |
The two low order info bits are: 0 for a reference to a dimension, 1 for a reference to |
| 2292 |
an extended dimension, 2 for an immediate dimension, and 3 for an immediate extended dimension node. |
| 2293 |
To distinguish in the long format between a reference to a dimension and a reference to an extended dimension, |
| 2294 |
the latter is prefixed with the keyword ``{\tt xdimen}'' (see section~\secref{reference}). |
| 2295 |
|
| 2296 |
@<hint types@>= |
| 2297 |
typedef struct {@+ |
| 2298 |
bool x;@+ |
| 2299 |
Xdimen d;@+ |
| 2300 |
} Kern; |
| 2301 |
@ |
| 2302 |
|
| 2303 |
\readcode |
| 2304 |
@s KERN symbol |
| 2305 |
@s EXPLICIT symbol |
| 2306 |
@s kern symbol |
| 2307 |
@s explicit symbol |
| 2308 |
@<symbols@>= |
| 2309 |
%token KERN "kern" |
| 2310 |
%token EXPLICIT "!" |
| 2311 |
%type <b> explicit |
| 2312 |
%type <kt> kern |
| 2313 |
@ |
| 2314 |
|
| 2315 |
@<scanning rules@>= |
| 2316 |
::@=kern@> :< return KERN; >: |
| 2317 |
::@=!@> :< return EXPLICIT; >: |
| 2318 |
@ |
| 2319 |
|
| 2320 |
@<parsing rules@>= |
| 2321 |
explicit: {$$=false;} @+| EXPLICIT {$$=true;}; |
| 2322 |
kern: explicit xdimen {$$.x=$1; $$.d=$2;}; |
| 2323 |
content_node: start KERN kern END { hput_tags($1,hput_kern(&($3)));} |
| 2324 |
@ |
| 2325 |
|
| 2326 |
\writecode |
| 2327 |
@<write functions@>= |
| 2328 |
void hwrite_explicit(bool x) |
| 2329 |
{ @+if (x) hwritef(" !"); @+} |
| 2330 |
|
| 2331 |
void hwrite_kern(Kern *k) |
| 2332 |
{ @+hwrite_explicit(k->x); |
| 2333 |
if (k->d.h==0.0 && k->d.v==0.0 && k->d.w==0) hwrite_ref(zero_dimen_no); |
| 2334 |
else hwrite_xdimen(&(k->d)); |
| 2335 |
} |
| 2336 |
@ |
| 2337 |
|
| 2338 |
|
| 2339 |
\getcode |
| 2340 |
@<cases to get content@>= |
| 2341 |
@t\1\kern1em@> |
| 2342 |
case TAG(kern_kind,b010): @+ {@+Kern k; @+HGET_KERN(b010,k);@+ } @+break; |
| 2343 |
case TAG(kern_kind,b011): @+ {@+Kern k; @+HGET_KERN(b011,k);@+ } @+break; |
| 2344 |
case TAG(kern_kind,b110): @+ {@+Kern k; @+HGET_KERN(b110,k);@+ } @+break; |
| 2345 |
case TAG(kern_kind,b111): @+ {@+Kern k; @+HGET_KERN(b111,k);@+ } @+break; |
| 2346 |
@ |
| 2347 |
|
| 2348 |
@<get macros@>= |
| 2349 |
#define @[HGET_KERN(I,K)@] \ |
| 2350 |
K.x=(I)&b100;\ |
| 2351 |
if (((I)&b011)==2) {HGET32(K.d.w);@+ K.d.h=K.d.v=0.0;@+}\ |
| 2352 |
else if (((I)&b011)==3) hget_xdimen_node(&(K.d));\ |
| 2353 |
hwrite_kern(&k); |
| 2354 |
@ |
| 2355 |
|
| 2356 |
\putcode |
| 2357 |
@<put functions@>= |
| 2358 |
Tag hput_kern(Kern *k) |
| 2359 |
{ Info info; |
| 2360 |
if (k->x) info=b100; @+else info=b000; |
| 2361 |
if (k->d.h==0.0 && k->d.v==0.0) |
| 2362 |
{ if (k->d.w==0) HPUT8(zero_dimen_no); |
| 2363 |
else {HPUT32(k->d.w); info=info|2;@+} |
| 2364 |
} |
| 2365 |
else {hput_xdimen_node(&(k->d));info=info|3;@+} |
| 2366 |
return TAG(kern_kind,info); |
| 2367 |
} |
| 2368 |
@ |
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
\subsection{Glue}\label{glue} |
| 2373 |
|
| 2374 |
%Glue considerations |
| 2375 |
|
| 2376 |
%So what are the cases: |
| 2377 |
%\itemize |
| 2378 |
%\item reference to a dimen (common) |
| 2379 |
%\item reference to a xdimen |
| 2380 |
%\item reference to a dimen plus and minus |
| 2381 |
%\item reference to a xdimen plus and minus |
| 2382 |
%\item reference to a dimen plus |
| 2383 |
%\item reference to a xdimen plus |
| 2384 |
%\item reference to a dimen minus |
| 2385 |
%\item reference to a xdimen minus |
| 2386 |
%\item dimen |
| 2387 |
%\item xdimen |
| 2388 |
%\item dimen plus and minus |
| 2389 |
%\item xdimen plus and minus (covers all other cases) |
| 2390 |
%\item dimen plus |
| 2391 |
%\item xdimen plus |
| 2392 |
%\item dimen minus |
| 2393 |
%\item xdimen minus |
| 2394 |
%\item plus and minus |
| 2395 |
%\item plus |
| 2396 |
%\item minus |
| 2397 |
%\item zero glue (rare, can be replaced by a reference to the zero glue) |
| 2398 |
%\item reference to a predefined glue (common) |
| 2399 |
%\enditemize |
| 2400 |
%This is a total of 21 cases. Can we use the info bits to specify 7 common |
| 2401 |
%cases and one catch all? First the use of an extended dimension in a glue |
| 2402 |
%is probably not very common. More typically is the use of a fill glue |
| 2403 |
%that extends to the boundaries of the enclosing box. |
| 2404 |
|
| 2405 |
%Here is the statistics for ctex: |
| 2406 |
%total 58937 glue entries |
| 2407 |
%total 49 defined glues (so 200 still available) |
| 2408 |
%There are three font specific glues defined for each font used in texts. |
| 2409 |
%The explicit glue nodes are the following: |
| 2410 |
%\itemize |
| 2411 |
%\item 35\% is predefined zero glue |
| 2412 |
%\item 30\% are 39 other predefined glue most of them less than 1% |
| 2413 |
%\item 8\% (4839) is one glue with 25pt pure stretch with order 0 |
| 2414 |
%\item 25\% (14746) is one glue with 100pt stretch and 10pt shrink with order 0 |
| 2415 |
%\item 2\% (1096) is one glue with 10pt no stretch and shrink |
| 2416 |
%\item 0\% (13) are 7 different glues with no stretch and shrink |
| 2417 |
%\item 0\% (3) different glues with width!=0 and some stretch of order 0 |
| 2418 |
%\item 0\% (27) 20 different glues with stretch and shrink |
| 2419 |
%\enditemize |
| 2420 |
|
| 2421 |
%Some more glue with 1fil is insider 55 leaders |
| 2422 |
%one vset has an extent 1 no stretch and shrink |
| 2423 |
%56 hset specify an extent 2 and 1 fil stretch |
| 2424 |
|
| 2425 |
|
| 2426 |
We have seen in section~\secref{stretch} how to deal with |
| 2427 |
stretchability\index{stretchability} and |
| 2428 |
shrinkability\index{shrinkability} and we will need this now. |
| 2429 |
Glue\index{glue} has a natural width---which in general can be an |
| 2430 |
extended dimension---and in addition it can stretch and shrink. It |
| 2431 |
might have been possible to allow an extended dimension also for the |
| 2432 |
stretch\-ability or shrink\-ability of a glue, but this seems of |
| 2433 |
little practical relevance and so simplicity won over generality. |
| 2434 |
Even with that restriction, it is an understatement to regard glue |
| 2435 |
nodes as "simple" nodes. |
| 2436 |
%, and we could equally well list them in |
| 2437 |
%section~\secref{composite} as composite nodes. |
| 2438 |
|
| 2439 |
To use the info bits in the short format wisely, I collected some |
| 2440 |
statistical data using the \TeX book as an example. It turns out that |
| 2441 |
about 99\% of all the 58937 glue nodes (not counting the interword |
| 2442 |
glues used inside texts) could be covered with only 43 predefined |
| 2443 |
glues. So this is by far the most common case; we reserve the info |
| 2444 |
value |b000| to cover it and postpone the description of such glue |
| 2445 |
nodes until we describe references in section~\secref{reference}. |
| 2446 |
|
| 2447 |
We expect the remaining cases to contribute not too much to the file |
| 2448 |
size, and hence, simplicity is a more important aspect than efficiency |
| 2449 |
when allocating the remaining info values. |
| 2450 |
|
| 2451 |
Looking at the glues in more detail, we find that the most common |
| 2452 |
cases are those where either one, two, or all three glue components |
| 2453 |
are zero. We use the two lowest bits to indicate the presence of a |
| 2454 |
nonzero stretchability or shrinkability and reserve the info values |
| 2455 |
|b001|, |b010|, and |b011| for those cases where the width of the glue |
| 2456 |
is zero. The zero glue, where all components are zero, is defined as |
| 2457 |
a fixed, predefined glue instead of reserving a special info value for |
| 2458 |
it. The cost of one extra byte when encoding it seems not too high a |
| 2459 |
price to pay. After reserving the info value |b111| for the most |
| 2460 |
general case of a glue, we have only three more info values left: |
| 2461 |
|b100|, |b101|, and |b110|. Keeping things simple implies using the |
| 2462 |
two lowest info bits---as before---to indicate a nonzero |
| 2463 |
stretchability or shrinkability. For the width, three choices remain: |
| 2464 |
using a reference to a dimension, using a reference to an extended |
| 2465 |
dimension, or using an immediate value. Since references to glues are |
| 2466 |
already supported, an immediate width seems best for glues that are |
| 2467 |
not frequently reused, avoiding the overhead of references. |
| 2468 |
|
| 2469 |
% It also makes parsing simpler because we avoid the confusion |
| 2470 |
% between references to dimensions |
| 2471 |
% and references to glues and references to extended dimensions. |
| 2472 |
|
| 2473 |
Here is a summary of the info bits and the implied layout |
| 2474 |
of glue nodes in the short format: |
| 2475 |
\itemize |
| 2476 |
\item |b000|: reference to a predefined glue |
| 2477 |
\item |b001|: zero width and nonzero shrinkability |
| 2478 |
\item |b010|: zero width and nonzero stretchability |
| 2479 |
\item |b011|: zero width and nonzero stretchability and shrinkability |
| 2480 |
\item |b100|: nonzero width |
| 2481 |
\item |b101|: nonzero width and nonzero shrinkability |
| 2482 |
\item |b110|: nonzero width and nonzero stretchability |
| 2483 |
\item |b111|: extended dimension and nonzero stretchability and shrinkability |
| 2484 |
\enditemize |
| 2485 |
|
| 2486 |
|
| 2487 |
@<hint basic types@>= |
| 2488 |
typedef struct {@+ |
| 2489 |
Xdimen w; @+ |
| 2490 |
Stretch p, m;@+ |
| 2491 |
} Glue; |
| 2492 |
@ |
| 2493 |
|
| 2494 |
|
| 2495 |
To test for a zero glue, |
| 2496 |
we implement a macro: |
| 2497 |
@<hint macros@>= |
| 2498 |
#define @[ZERO_GLUE(G)@] ((G).w.w==0 && (G).w.h==0.0 && (G).w.v==0.0 && (G).p.f==0.0 && (G).m.f==0.0) |
| 2499 |
@ |
| 2500 |
|
| 2501 |
Because other nodes (leaders, baselines, and fonts) |
| 2502 |
contain glue nodes as parameters, we provide functions |
| 2503 |
to read and write a complete glue node in the same way as we did |
| 2504 |
for rule nodes. |
| 2505 |
Further, such an internal {\sl glue\_node\/} has the special property that |
| 2506 |
in the short format a node for the zero glue might be omitted entirely. |
| 2507 |
|
| 2508 |
\readcode |
| 2509 |
@s GLUE symbol |
| 2510 |
@s glue symbol |
| 2511 |
@s glue_node symbol |
| 2512 |
@s PLUS symbol |
| 2513 |
@s MINUS symbol |
| 2514 |
@s plus symbol |
| 2515 |
@s minus symbol |
| 2516 |
|
| 2517 |
@<symbols@>= |
| 2518 |
%token GLUE "glue" |
| 2519 |
%token PLUS "plus" |
| 2520 |
%token MINUS "minus" |
| 2521 |
%type <g> glue |
| 2522 |
%type <b> glue_node |
| 2523 |
%type <st> plus minus |
| 2524 |
@ |
| 2525 |
|
| 2526 |
@<scanning rules@>= |
| 2527 |
::@=glue@> :< return GLUE; >: |
| 2528 |
::@=plus@> :< return PLUS; >: |
| 2529 |
::@=minus@> :< return MINUS; >: |
| 2530 |
@ |
| 2531 |
|
| 2532 |
@<parsing rules@>= |
| 2533 |
plus: { $$.f=0.0; $$.o=0; } | PLUS stretch {$$=$2;}; |
| 2534 |
minus: { $$.f=0.0; $$.o=0; } | MINUS stretch {$$=$2;}; |
| 2535 |
glue: xdimen plus minus {$$.w=$1; $$.p=$2; $$.m=$3; }; |
| 2536 |
content_node: start GLUE glue END {if (ZERO_GLUE($3)) {HPUT8(zero_skip_no); |
| 2537 |
hput_tags($1,TAG(glue_kind,0)); } else hput_tags($1,hput_glue(&($3))); }; |
| 2538 |
glue_node: start GLUE glue END @/ |
| 2539 |
{@+ if (ZERO_GLUE($3)) { hpos--; $$=false;@+}@/ |
| 2540 |
else { hput_tags($1,hput_glue(&($3))); $$=true;@+}@+ }; |
| 2541 |
@ |
| 2542 |
|
| 2543 |
\writecode |
| 2544 |
@<write functions@>= |
| 2545 |
void hwrite_plus(Stretch *p) |
| 2546 |
{ @+if (p->f!=0.0) { hwritef(" plus");@+hwrite_stretch(p); @+} |
| 2547 |
} |
| 2548 |
void hwrite_minus(Stretch *m) |
| 2549 |
{@+ if (m->f!=0.0) { hwritef(" minus");@+hwrite_stretch(m); @+} |
| 2550 |
} |
| 2551 |
|
| 2552 |
void hwrite_glue(Glue *g) |
| 2553 |
{ hwrite_xdimen(&(g->w)); @+ |
| 2554 |
hwrite_plus(&g->p); @+hwrite_minus(&g->m); |
| 2555 |
} |
| 2556 |
|
| 2557 |
void hwrite_ref_node(Kind k, uint8_t n); |
| 2558 |
void hwrite_glue_node(Glue *g) |
| 2559 |
{@+ |
| 2560 |
if (ZERO_GLUE(*g)) hwrite_ref_node(glue_kind,zero_skip_no); |
| 2561 |
else @+{ hwrite_start(); @+hwritef("glue"); @+hwrite_glue(g); @+hwrite_end();@+} |
| 2562 |
} |
| 2563 |
@ |
| 2564 |
|
| 2565 |
\getcode |
| 2566 |
@<cases to get content@>= |
| 2567 |
@t\1\kern1em@> |
| 2568 |
case TAG(glue_kind,b001): { Glue g;@+ HGET_GLUE(b001,g);@+ hwrite_glue(&g);@+}@+break; |
| 2569 |
case TAG(glue_kind,b010): { Glue g;@+ HGET_GLUE(b010,g);@+ hwrite_glue(&g);@+}@+break; |
| 2570 |
case TAG(glue_kind,b011): { Glue g;@+ HGET_GLUE(b011,g);@+ hwrite_glue(&g);@+}@+break; |
| 2571 |
case TAG(glue_kind,b100): { Glue g;@+ HGET_GLUE(b100,g);@+ hwrite_glue(&g);@+}@+break; |
| 2572 |
case TAG(glue_kind,b101): { Glue g;@+ HGET_GLUE(b101,g);@+ hwrite_glue(&g);@+}@+break; |
| 2573 |
case TAG(glue_kind,b110): { Glue g;@+ HGET_GLUE(b110,g);@+ hwrite_glue(&g);@+}@+break; |
| 2574 |
case TAG(glue_kind,b111): { Glue g;@+ HGET_GLUE(b111,g);@+ hwrite_glue(&g);@+}@+break; |
| 2575 |
@ |
| 2576 |
|
| 2577 |
@<get macros@>= |
| 2578 |
#define @[HGET_GLUE(I,G)@] {\ |
| 2579 |
if((I)!=b111) { if ((I)&b100) HGET32((G).w.w);@+ else (G).w.w=0;}\ |
| 2580 |
if((I)&b010) HGET_STRETCH((G).p) @+else (G).p.f=0.0, (G).p.o=0;\ |
| 2581 |
if((I)&b001) HGET_STRETCH((G).m) @+else (G).m.f=0.0, (G).m.o=0;\ |
| 2582 |
if((I)==b111) hget_xdimen_node(&((G).w)); else (G).w.h=(G).w.v=0.0;@+} |
| 2583 |
@ |
| 2584 |
|
| 2585 |
The |hget_glue_node| can cope with a glue node that is omitted and |
| 2586 |
will supply a zero glue instead. |
| 2587 |
|
| 2588 |
@<get functions@>= |
| 2589 |
void hget_glue_node(void) |
| 2590 |
{ @<read the start byte |a|@>@; |
| 2591 |
if (KIND(a)!=glue_kind) |
| 2592 |
{@+ hpos--; hwrite_ref_node(glue_kind,zero_skip_no);@+return; @+} |
| 2593 |
if (INFO(a)==b000) |
| 2594 |
{ uint8_t n=HGET8;@+ REF(glue_kind,n);@+hwrite_ref_node(glue_kind,n); @+} |
| 2595 |
else |
| 2596 |
{ @+Glue g; @+HGET_GLUE(INFO(a),g);@+ hwrite_glue_node(&g);@+} |
| 2597 |
@<read and check the end byte |z|@>@; |
| 2598 |
} |
| 2599 |
@ |
| 2600 |
|
| 2601 |
|
| 2602 |
\putcode |
| 2603 |
@<put functions@>= |
| 2604 |
Tag hput_glue(Glue *g) |
| 2605 |
{ Info info=b000; |
| 2606 |
if (ZERO_GLUE(*g)) { HPUT8(zero_skip_no); @+ info=b000; } |
| 2607 |
else if ( (g->w.w==0 && g->w.h==0.0 && g->w.v==0.0)) |
| 2608 |
{ if (g->p.f!=0.0) { hput_stretch(&g->p); @+info|=b010; @+} |
| 2609 |
if (g->m.f!=0.0) { hput_stretch(&g->m); @+info|=b001; @+} |
| 2610 |
} |
| 2611 |
else if ( g->w.h==0.0 && g->w.v==0.0 && (g->p.f==0.0 || g->m.f==0.0)) |
| 2612 |
{ HPUT32(g->w.w); @+ info=b100; |
| 2613 |
if (g->p.f!=0.0) { hput_stretch(&g->p); @+info|=b010; @+} |
| 2614 |
if (g->m.f!=0.0) { hput_stretch(&g->m); @+info|=b001; @+} |
| 2615 |
} |
| 2616 |
else@/ |
| 2617 |
{ hput_stretch(&g->p);@+ hput_stretch(&g->m); |
| 2618 |
hput_xdimen_node(&(g->w)); |
| 2619 |
info=b111; |
| 2620 |
} |
| 2621 |
return TAG(glue_kind,info); |
| 2622 |
} |
| 2623 |
@ |
| 2624 |
|
| 2625 |
\section{Lists}\hascode\label{lists} |
| 2626 |
When a node contains multiple other nodes, we package these nodes into |
| 2627 |
a list\index{list} node. It is important to note that list nodes |
| 2628 |
never occur as individual nodes, they only occur as parts of other |
| 2629 |
nodes. In total, we have three different types of lists: plain lists |
| 2630 |
that use the kind-value |list_kind|, text\index{text} lists that use |
| 2631 |
the kind-value |list_kind| together with the info bit |b100|, |
| 2632 |
and parameter\index{parameter} lists that use the |
| 2633 |
kind-value |param_kind|. A description of the first two types of |
| 2634 |
lists follows here. Parameter lists are described in section~\secref{paramlist}. |
| 2635 |
|
| 2636 |
Because lists are of variable size, it is not possible in the short |
| 2637 |
format to tell from the kind and info bits of a tag byte the size of |
| 2638 |
the list node. So advancing from the beginning of a list node to the |
| 2639 |
next node after the list is not as simple as usual. To solve this |
| 2640 |
problem, we store the size of the list immediately after the start |
| 2641 |
byte and before the end byte. Alternatively we could require programs |
| 2642 |
to traverse the entire list. The latter solution is more compact but |
| 2643 |
inefficient for list with many nodes; our solution will cost some |
| 2644 |
extra bytes, but the amount of extra bytes will only grow |
| 2645 |
logarithmically with the size of the \HINT\ file. It would be |
| 2646 |
possible to allow both methods so that a \HINT\ file could balance |
| 2647 |
size and time trade-offs by making small lists---where the size can be |
| 2648 |
determined easily by reading the entire list---without size |
| 2649 |
information and making large lists with size information so that they |
| 2650 |
can be skipped easily without reading them. But the added complexity |
| 2651 |
seems too high a price to pay. |
| 2652 |
|
| 2653 |
|
| 2654 |
Now consider the problem of reading a content stream starting at an arbitrary |
| 2655 |
position $i$ in the middle of the stream. This situation occurs |
| 2656 |
naturally when resynchronizing\index{resynchronization} a content stream after |
| 2657 |
an error has been detected, but implementing links poses a similar problem. |
| 2658 |
We can inspect the byte at position $i$ and see |
| 2659 |
if it is a valid tag. If yes, we are faced with the problem of |
| 2660 |
verifying that this is not a mere coincidence. |
| 2661 |
So we determine the size $s$ of the node. If the byte in question is a start byte, |
| 2662 |
we should find a matching byte $s$ bytes later in the stream; if it is an end byte, |
| 2663 |
we should find the matching byte $s$ bytes earlier in the stream; if we |
| 2664 |
find no matching byte, this was neither a start nor an end byte. |
| 2665 |
If we find exactly one matching byte, we can be quite confident (error |
| 2666 |
probability 1/256 if assuming equal probability of all byte values) |
| 2667 |
that we have found a tag, and we know whether |
| 2668 |
it is the beginning or the end tag. If we find two matching byte, we |
| 2669 |
have most likely the start or the end of a node, but we do not know which |
| 2670 |
of the two. To find out which of the two possibilities is true |
| 2671 |
or to reduce the probability of an error, we can |
| 2672 |
check the start and end byte of the node immediately preceding a start byte or |
| 2673 |
immediately following an end byte in a similar way. |
| 2674 |
By testing two more byte, this additional check will reduce the error |
| 2675 |
probability further to $1/2^{24}$ (under the same assumption as before). So |
| 2676 |
checking more nodes is rarely necessary. This whole schema |
| 2677 |
would, however, not work if we happen to find a tag byte that indicated |
| 2678 |
either the begin or the end of a list without specifying the size |
| 2679 |
of the list. Sure, we can verify the bytes before and after it to |
| 2680 |
find out whether the byte following it is the begin of a node and the |
| 2681 |
byte preceding it is the end of a node, but we still don't know if the |
| 2682 |
byte itself starts a node list or ends a node list. Even reading along |
| 2683 |
in either direction until finding a matching tag will not answer the |
| 2684 |
question. The situation is better if we specify a |
| 2685 |
size: we can read the suspected size after or before the tag and check if we |
| 2686 |
find a matching tag and size at the position indicated. |
| 2687 |
|
| 2688 |
In the short format, we use the two lower bits of the |info| value to indicate the number of |
| 2689 |
byte used to store the list size: A list with $\hbox{|info&0x3|}=1$ uses 1 byte, |
| 2690 |
with $\hbox{|info&0x3|}=2$ uses 2 byte, and with $\hbox{|info&0x3|}=3$ uses 4 byte. |
| 2691 |
The |info&0x3| value zero is reserved for references to predefined lists. |
| 2692 |
An empty list is always represented using zero as the reference number. |
| 2693 |
General predefined lists are currently implemented only for parameter lists. |
| 2694 |
|
| 2695 |
Storing the list size immediately preceding the end tag creates a new |
| 2696 |
problem: If we try to recover from an error, we might not know the |
| 2697 |
size of the list and searching for the end of a list, we might be |
| 2698 |
unable to tell the difference between the bytes that encode the list |
| 2699 |
size and the start tag of a possible next node. If we parse the |
| 2700 |
content backward, the problem is completely symmetric. |
| 2701 |
|
| 2702 |
To solve the problem, we insert an additional byte immediately before |
| 2703 |
the final size and after the initial size marking the size boundary. |
| 2704 |
We choose the byte values |0xFF|, |0xFE|, and |0xFD| which can |
| 2705 |
not be confused with valid tag bytes and indicate that the size is |
| 2706 |
stored using 1, 2, or 4 byte respectively. Under regular |
| 2707 |
circumstances, these bytes are simply skipped. When searching for the |
| 2708 |
list end (or start) these bytes would correspond to |
| 2709 |
|TAG(penalty_kind,i)| with $7 \ge \hbox{|i|} \ge 5$ and can not be |
| 2710 |
confused with valid penalty nodes which use only the info values 0, 1, |
| 2711 |
and~2. An empty list always uses the info value 0 and the reference value 0. |
| 2712 |
|
| 2713 |
We are a bit lazy when it comes to the internal representation of a list. |
| 2714 |
Since we need the representation as a short format byte sequence anyway, |
| 2715 |
it consists of the position |p| of the start of the byte sequence |
| 2716 |
combined with an integer |s| giving the size of the byte sequence. |
| 2717 |
If the list is empty, |s| is zero. |
| 2718 |
|
| 2719 |
@<hint types@>= |
| 2720 |
typedef struct {@+ |
| 2721 |
Tag t;@+ |
| 2722 |
uint32_t p;@+ |
| 2723 |
uint32_t s;@+ |
| 2724 |
} List; |
| 2725 |
@ |
| 2726 |
|
| 2727 |
The major drawback of this choice of representation is that it ties |
| 2728 |
together the reading of the long format and the writing of the short |
| 2729 |
format; these are no longer independent. |
| 2730 |
So starting with the present section, we have to take the short format |
| 2731 |
representation of a node into account already when we parse the long |
| 2732 |
format representation. |
| 2733 |
|
| 2734 |
In the long format, we may start a list node with an |
| 2735 |
estimate\index{estimate} of the size needed to store the list in the |
| 2736 |
short format. We do not want to require the exact size because this |
| 2737 |
would make editing of long format \HINT\ files almost impossible. Of |
| 2738 |
course this makes it also impossible to derive the exact |s| value of |
| 2739 |
the internal representation from the long format |
| 2740 |
representation. Therefore we start by parsing the estimate of the list |
| 2741 |
size and use it to reserve the necessary number of byte to store the |
| 2742 |
size. Then we parse the |content_list|. As a side effect---and this |
| 2743 |
is an important point---this will write the list content in short |
| 2744 |
format into the output buffer. As mentioned above, whenever a node |
| 2745 |
contains a list, we need to consider this side effect when we give the |
| 2746 |
parsing rules. We will see examples for this in |
| 2747 |
section~\secref{composite}. |
| 2748 |
|
| 2749 |
The function |hput_list| will be called {\it after} the short format |
| 2750 |
of the list is written to the output. Before we pass the internal |
| 2751 |
representation of the list to the |hput_list| |
| 2752 |
function, we update |s| and |p|. Further, we pass the position in the stream where the |
| 2753 |
list size and its boundary mark is supposed to be. |
| 2754 |
Before |hput_list| is called, space for the tag, the size, and the boundary mark |
| 2755 |
is allocated based on the estimate. The function |
| 2756 |
|hsize_bytes| computes the number of byte required to store the list |
| 2757 |
size, and the function |hput_list_size| will later write the list |
| 2758 |
size. If the estimate turns out to be wrong, the list data can be moved |
| 2759 |
to make room for a larger or smaller size field. |
| 2760 |
|
| 2761 |
|
| 2762 |
If the long format does not specify a size estimate, a suitable default must be chosen. |
| 2763 |
A statistical analysis shows |
| 2764 |
% |
| 2765 |
%statistics about list sizes using my old prototype |
| 2766 |
% |
| 2767 |
%name type size_byte list_count total_size |
| 2768 |
%hello.hnt text 1 6 748 |
| 2769 |
% text 2 2 1967 |
| 2770 |
% list 1 65 3245 |
| 2771 |
% list 2 1 352 |
| 2772 |
%web2w.hnt text 1 1043 121925 |
| 2773 |
% text 2 1344 859070 |
| 2774 |
% list 1 19780 725514 |
| 2775 |
% list 2 487 199243 |
| 2776 |
%ctex.hnt text 1 9121 4241128 |
| 2777 |
% text 2 12329 7872687 |
| 2778 |
% text 3 1 75010 |
| 2779 |
% list 1 121557 4600743 |
| 2780 |
% list 2 222 147358 |
| 2781 |
% |
| 2782 |
that most plain lists need only a single byte to store the size; and even the |
| 2783 |
total amount of data contained in these lists exceeds the amount of data stored |
| 2784 |
in longer lists by a factor of about 3. Hence if we do not have an estimate, |
| 2785 |
we reserve only a single byte to store the size of a list. |
| 2786 |
The statistics looks different for lists stored as a text: The number of texts |
| 2787 |
that require two byte for the size is slightly larger than the number of texts that |
| 2788 |
need only one byte, and the total amount of data stored in these texts is larger |
| 2789 |
by a factor of 2 to 7 than the total amount of data found in all other texts. |
| 2790 |
Hence as a default, we reserve two byte to store the size for texts. |
| 2791 |
|
| 2792 |
|
| 2793 |
\subsection{Plain Lists}\label{plainlists} |
| 2794 |
Plain list nodes start and end with a tag of kind |list_kind|. |
| 2795 |
Not uncommon are empty\index{empty list} lists; these can be |
| 2796 |
stored using $|info|=0$ and a reference to the predefined empty list. |
| 2797 |
|
| 2798 |
Writing the long format uses the fact that the function |
| 2799 |
|hget_content_node|, as implemented in the \.{stretch} program, will |
| 2800 |
output the node in the long format. |
| 2801 |
|
| 2802 |
\readcode |
| 2803 |
@s list symbol |
| 2804 |
@s content_list symbol |
| 2805 |
@s estimate symbol |
| 2806 |
@s position symbol |
| 2807 |
|
| 2808 |
@<symbols@>= |
| 2809 |
%type <l> list |
| 2810 |
%type <u> position content_list |
| 2811 |
@ |
| 2812 |
|
| 2813 |
@<parsing rules@>= |
| 2814 |
position: {$$=hpos-hstart;}; |
| 2815 |
content_list: @+ position @+ |
| 2816 |
| content_list content_node; |
| 2817 |
estimate: {hpos+=2; } @+ |
| 2818 |
| UNSIGNED {hpos+=hsize_bytes($1)+1; } ; |
| 2819 |
list: start estimate content_list END @/ |
| 2820 |
{@+$$.t=TAG(list_kind,b010);@+ $$.p=$3; @+ $$.s=(hpos-hstart)-$3; |
| 2821 |
hput_tags($1,hput_list($1+1, &($$)));@+}; |
| 2822 |
@ |
| 2823 |
|
| 2824 |
\writecode |
| 2825 |
@<write functions@>= |
| 2826 |
void hwrite_list(List *l) |
| 2827 |
{ uint32_t h=hpos-hstart, e=hend-hstart; /* save |hpos| and |hend| */ |
| 2828 |
hpos=l->p+hstart;@+ hend=hpos+l->s; |
| 2829 |
if (KIND(l->t)==list_kind) |
| 2830 |
{ if (INFO(l->t)&b100) @<write a text@>@; else @<write a list@>@; } |
| 2831 |
else QUIT("List expected got %s", content_name[KIND(l->t)]); |
| 2832 |
hpos=hstart+h;@+ hend=hstart+e; /* restore |hpos| and |hend| */ |
| 2833 |
} |
| 2834 |
@ |
| 2835 |
|
| 2836 |
@<write a list@>= |
| 2837 |
{@+if (l->s==0) hwritef(" <>");@/ |
| 2838 |
else@/ |
| 2839 |
{@+DBG(DBGNODE,"Write list at 0x%x size=%u\n", l->p, l->s); |
| 2840 |
@+hwrite_start();@+ |
| 2841 |
if (section_no==2) hwrite_label(); |
| 2842 |
if (l->s>0xFF) hwritef("%d",l->s); |
| 2843 |
while(hpos<hend) |
| 2844 |
hget_content_node(); |
| 2845 |
hwrite_end(); |
| 2846 |
} |
| 2847 |
} |
| 2848 |
@ |
| 2849 |
\getcode |
| 2850 |
@<shared get functions@>= |
| 2851 |
void hget_size_boundary(Info info) |
| 2852 |
{ uint32_t n; |
| 2853 |
info=info&0x3; |
| 2854 |
if (info==0) return; |
| 2855 |
n=HGET8; |
| 2856 |
if (n!=0x100-info) QUIT("Non matching boundary byte 0x%x with info value %d at 0x%x", |
| 2857 |
n, info,(uint32_t)(hpos-hstart-1)); |
| 2858 |
} |
| 2859 |
|
| 2860 |
uint32_t hget_list_size(Info info) |
| 2861 |
{ uint32_t n=0; |
| 2862 |
info=info&0x3; |
| 2863 |
if (info==0) return 0; |
| 2864 |
else if (info==1) n=HGET8; |
| 2865 |
else if (info==2) HGET16(n); |
| 2866 |
else if (info==3) HGET32(n); |
| 2867 |
else QUIT("List info %d must be 0, 1, 2, or 3",info); |
| 2868 |
return n; |
| 2869 |
} |
| 2870 |
|
| 2871 |
void hget_list(List *l) |
| 2872 |
{@+if (KIND(*hpos)!=list_kind && KIND(*hpos)!=param_kind) @/ |
| 2873 |
QUIT("List expected at 0x%x", (uint32_t)(hpos-hstart)); |
| 2874 |
else |
| 2875 |
{ |
| 2876 |
@<read the start byte |a|@>@; |
| 2877 |
l->t=a; |
| 2878 |
HGET_LIST(INFO(a),*l); |
| 2879 |
@<read and check the end byte |z|@>@; |
| 2880 |
DBG(DBGNODE,"Get list at 0x%x size=%u\n", l->p, l->s); |
| 2881 |
} |
| 2882 |
} |
| 2883 |
@ |
| 2884 |
|
| 2885 |
If a list has the info value zero, the list is the empty list. |
| 2886 |
Other list references are currently not implemented. |
| 2887 |
|
| 2888 |
@<shared get macros@>= |
| 2889 |
#define @[HGET_LIST(I,L)@] \ |
| 2890 |
if (((I)&0x3)==0) {uint8_t n=HGET8; @+REF_RNG(KIND((L).t),n);@+ (L).s=0;@+}\ |
| 2891 |
else { (L).s=hget_list_size(I); hget_size_boundary(I);\ |
| 2892 |
(L).p=hpos-hstart; \ |
| 2893 |
hpos=hpos+(L).s; hget_size_boundary(I);\ |
| 2894 |
{ uint32_t s=hget_list_size(I); \ |
| 2895 |
if (s!=(L).s) \ |
| 2896 |
QUIT(@["List sizes at 0x%x and " SIZE_F " do not match 0x%x != 0x%x"@],node_pos+1,hpos-hstart-I-1,(L).s,s);}} |
| 2897 |
@ |
| 2898 |
|
| 2899 |
\putcode |
| 2900 |
|
| 2901 |
@<put functions@>= |
| 2902 |
uint8_t hsize_bytes(uint32_t n) |
| 2903 |
{ @+if (n==0) return 0; |
| 2904 |
else if (n<0x100) return 1; |
| 2905 |
else if (n<0x10000) return 2; |
| 2906 |
else return 4; |
| 2907 |
} |
| 2908 |
|
| 2909 |
void hput_list_size(uint32_t n, int i) |
| 2910 |
{ @+if (i==0) return; |
| 2911 |
else if (i==1) HPUT8(n); |
| 2912 |
else if (i==2) HPUT16(n); |
| 2913 |
else HPUT32(n); |
| 2914 |
} |
| 2915 |
|
| 2916 |
Tag hput_list(uint32_t start_pos, List *l) |
| 2917 |
{ @+if (l->s==0) |
| 2918 |
{ hpos=hstart+start_pos; @+ HPUT8(0); @+return TAG(KIND(l->t),INFO(l->t)&b100);@+} |
| 2919 |
else@/ |
| 2920 |
{ uint32_t list_end=hpos-hstart; |
| 2921 |
int i=l->p -start_pos-1; /* number of byte allocated for size */ |
| 2922 |
int j=hsize_bytes(l->s); /* number of byte needed for size */ |
| 2923 |
Info k; |
| 2924 |
if (j==4) k=3; else k=j; |
| 2925 |
DBG(DBGNODE,"Put list at 0x%x size=%u\n", l->p, l->s); |
| 2926 |
if (i>j && l->s> 0x100) j=i; /* avoid moving large lists */ |
| 2927 |
if (i!=j)@/ |
| 2928 |
{ int d= j-i; |
| 2929 |
DBG(DBGNODE,"Moving %u byte by %d\n", l->s,d); |
| 2930 |
if (d>0) HPUTX(d); |
| 2931 |
memmove(hstart+l->p+d,hstart+l->p,l->s); |
| 2932 |
@<adjust label positions after moving a list@>@; |
| 2933 |
l->p=l->p+d;@+ |
| 2934 |
list_end=list_end+d; |
| 2935 |
} |
| 2936 |
hpos=hstart+start_pos; @+ hput_list_size(l->s,j);@+ HPUT8(0x100-k); |
| 2937 |
hpos=hstart+list_end;@+ HPUT8(0x100-k);@+ hput_list_size(l->s,j); |
| 2938 |
return TAG(KIND(l->t),k|(INFO(l->t)&b100)); |
| 2939 |
} |
| 2940 |
} |
| 2941 |
|
| 2942 |
@ |
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
\subsection{Texts}\label{text} |
| 2947 |
A Text\index{text} is a list of nodes with a representation optimized |
| 2948 |
for character nodes. In the long format, a sequence of characters |
| 2949 |
like ``{\tt Hello}'' is written ``\.{<glyph 'H'} \.{*0>} \.{<glyph} \.{'e'} |
| 2950 |
\.{*0>} \.{<glyph 'l' *0>} \.{<glyph 'l' *0>} \.{<glyph 'o' *0>}'', and |
| 2951 |
even in the short format it requires 4 byte per character! As a text, |
| 2952 |
the same sequence is written ``{\tt\,"Hello"\,}'' in the long format and the |
| 2953 |
short format requires usually just 1 byte per character. Indeed |
| 2954 |
except the bytes with values from |0x00| to |0x20|, which are |
| 2955 |
considered control\index{control code} codes, all bytes and all |
| 2956 |
\hbox{UTF-8}\index{UTF8} multibyte sequences are simply considered |
| 2957 |
character\index{character code} codes. They are equivalent to a glyph |
| 2958 |
node in the ``current font''. The current\index{current font} |
| 2959 |
font\index{font} is font number 0 at the beginning of a text and it |
| 2960 |
can be changed using the control codes. We introduce the concept of a |
| 2961 |
``current font'' because we do not expect the font to change too |
| 2962 |
often, and it allows for a more compact representation if we do not |
| 2963 |
store the font with every character code. It has an important |
| 2964 |
disadvantage though: storing only font changes prevents us from |
| 2965 |
parsing a text backwards; we always have to start at the beginning of |
| 2966 |
the text, where the font is known to be font number~0. |
| 2967 |
|
| 2968 |
Defining a second format for encoding lists of nodes adds another |
| 2969 |
difficulty to the problem we had discussed at the beginning of |
| 2970 |
section~\secref{lists}. When we try to recover from an error and start |
| 2971 |
reading a content stream at an arbitrary position, the first thing we |
| 2972 |
need to find out is whether at this position we have the tag byte of |
| 2973 |
an ordinary node or whether we have a position inside a text. |
| 2974 |
|
| 2975 |
Inside a text, character nodes start with a byte in the range |
| 2976 |
|0x21|--|0xF7|. This is a wide range and it overlaps considerably with |
| 2977 |
the range of valid tag bytes. It is however possible to choose the |
| 2978 |
kind-values in such a way that the control codes do not overlap with |
| 2979 |
the valid tag bytes that start a node. |
| 2980 |
For this reason, the values |
| 2981 |
|list_kind==0|, |param_kind==1|, |range_kind==2|, |xdimen_kind==3|, and |
| 2982 |
|adjust_kind==4| were chosen on page~\pageref{kinddef}. Lists, |
| 2983 |
parameter lists, and extended dimensions occur only {\it inside} of |
| 2984 |
content nodes, but are not content nodes in their own right; |
| 2985 |
page ranges occur only in the definition section; so the |
| 2986 |
values |0x00| to |0x1F| are not used as tag bytes of content |
| 2987 |
nodes. The value |0x20| would, as a tag byte, indicate an adjust node |
| 2988 |
(|adjust_kind==4|) with info value zero. Because there are no |
| 2989 |
predefined adjustments, |0x20| is not used as a tag byte either. |
| 2990 |
(An alternative choice would be to use the kind value 4 for paragraph |
| 2991 |
nodes because there are no predefined paragraphs.) |
| 2992 |
|
| 2993 |
The largest byte that starts an UTF8 code is |0xF7|; hence, there are |
| 2994 |
eight possible control codes, from |0xF8| to |0xFF|, available. The |
| 2995 |
first three values |0xF8|, |0xF9|, and |0xFA| are actually used for |
| 2996 |
penalty nodes with info values, 0, 1, and 2. The last three |
| 2997 |
|0xFD|, |0xFE|, and |0xFF| are used as boundary marks for the text |
| 2998 |
size and therefore we can use only |0xFB| and \label{FC}|0xFC| as control codes. |
| 2999 |
|
| 3000 |
In the long format, we do not provide a syntax for specifying a size |
| 3001 |
estimate\index{estimate} as we did for plain lists, because we expect |
| 3002 |
text to be quite short. We allocate two byte for the size and hope |
| 3003 |
that this will prove to be sufficient most of the time. Further, we |
| 3004 |
will disallow the use of non-printable ASCII codes, because these |
| 3005 |
are---by definition---not very readable, and we will give special |
| 3006 |
meaning to some of the printable ASCII codes because we will need a |
| 3007 |
notation for the beginning and ending of a text, for nodes inside a |
| 3008 |
text, and the control codes. |
| 3009 |
|
| 3010 |
Here are the details: |
| 3011 |
\itemize |
| 3012 |
|
| 3013 |
\item In the long format, a text starts and ends with a |
| 3014 |
double\index{double quote} quote character ``{\tt "}''. In the short |
| 3015 |
format, texts are encoded similar to lists setting the info bit |b100|. |
| 3016 |
|
| 3017 |
\item Arbitrary nodes can be embedded inside a text. In the long |
| 3018 |
format, they are enclosed in pointed brackets \.{<} \dots \.{>} as |
| 3019 |
usual. In the short format, an arbitrary node can follow the control |
| 3020 |
code $|txt_node|=|0x1E|$. Because text may occur in nodes, the scanner |
| 3021 |
needs to be able to parse texts nested inside nodes nested inside |
| 3022 |
nodes nested inside texts \dots\ To accomplish this, we use the |
| 3023 |
``stack'' option of \.{flex} and include the pushing and popping of the |
| 3024 |
stack in the macros |SCAN_START| and |SCAN_END|. |
| 3025 |
|
| 3026 |
\item The space\index{space character} character ``\.{\ }'' with ASCII |
| 3027 |
value |0x20| stands in both formats for the font specific interword |
| 3028 |
glue node (control code |txt_glue|). |
| 3029 |
|
| 3030 |
\item The hyphen\index{hyphen character} character ``\.{-}'' in the |
| 3031 |
long format and the control code $|txt_hyphen|=|0x1F|$ in the short |
| 3032 |
format stand for the font specific discretionary hyphenation node. |
| 3033 |
|
| 3034 |
\item In the long format, the backslash\index{backslash} character |
| 3035 |
``\.{\\}'' is used as an escape character. It is used to introduce |
| 3036 |
notations for control codes, as described below, and to access the |
| 3037 |
character codes of those ASCII characters that otherwise carry a |
| 3038 |
special meaning. For example ``{\tt \BS "}'' denotes the character code |
| 3039 |
of the double quote character ``{\tt "}''; and similarly ``\.{\\\\}'', |
| 3040 |
``\.{\\<}'', ``\.{\\>}'', ``\.{\\\ }'', and ``\.{\\-}'' denote the |
| 3041 |
character codes of ``\.{\\}'', ``\.{<}'', ``\.{>}'', ``\.{\ }'', and |
| 3042 |
``\.{-}'' respectively. |
| 3043 |
|
| 3044 |
|
| 3045 |
\item In the long format, a TAB-character (ASCII code |
| 3046 |
|0x09|)\index{tab character} is silently converted to a |
| 3047 |
space\index{space character} character (ASCII code |0x20|); |
| 3048 |
a NL-character\index{newline character} (ASCII code |0x0A|), together |
| 3049 |
with surrounding spaces, TAB-characters, |
| 3050 |
and CR-characters\index{carriage return character} (ASCII code |0x0D|), |
| 3051 |
is silently converted to a single space character. All other ASCII |
| 3052 |
characters in the range |0x00| to |0x1F| are not allowed inside a |
| 3053 |
text. This rule avoids the problems arising from ``invisible'' |
| 3054 |
characters embedded in a text and it allows to break texts into lines, |
| 3055 |
even with indentation\index{indentation}, at word boundaries. |
| 3056 |
|
| 3057 |
To allow breaking a text into lines without inserting spaces, a |
| 3058 |
NL-character together with surrounding spaces, TAB-characters, and |
| 3059 |
CR-characters is completely ignored if the whole group of spaces, |
| 3060 |
TAB-characters, CR-characters, and the NL-character is preceded by a |
| 3061 |
backslash character. |
| 3062 |
|
| 3063 |
For example, the text ``\.{"There\ is\ no\ more\ gas\ in\ the\ |
| 3064 |
tank."}''\hfil\break can be written as \medskip |
| 3065 |
|
| 3066 |
\qquad\vbox{\hsize=0.5\hsize\noindent |
| 3067 |
\.{"There\ is\ }\hfil\break |
| 3068 |
\.{\hbox to 2em {$\rightarrow$\hfill}no more g\\\ \ }\hfil\break |
| 3069 |
\.{\hbox to 2em {$\rightarrow$\hfill}as in the tank."} |
| 3070 |
}\hss |
| 3071 |
|
| 3072 |
To break long lines when writing a long format file, we use the |
| 3073 |
variable |txt_length| to keep track of the approximate length of the |
| 3074 |
current line. |
| 3075 |
|
| 3076 |
\item The control codes $|txt_font|=|0x00|$, |0x01|, |0x02|, \dots, |
| 3077 |
and |0x07| are used to change the current font to font |
| 3078 |
number 0, 1, 2, \dots, and 7. In the long format these control |
| 3079 |
codes are written \.{\\0}, \.{\\1}, \.{\\2}, \dots, and \.{\\7}. |
| 3080 |
|
| 3081 |
\item The control code $|txt_global|=|0x08|$ is followed by a second |
| 3082 |
parameter byte. If the value of the parameter byte is $n$, it will set |
| 3083 |
the current font to font number $n$. In the long format, the two byte |
| 3084 |
sequence is written ``\.{\\F}$n$\.{\\}'' where $n$ is the decimal |
| 3085 |
representation of the font number. |
| 3086 |
|
| 3087 |
|
| 3088 |
\item The control codes |0x09|, |0x0A|, |0x0B|, |0x0C|, |0x0D|, |
| 3089 |
|0x0E|, |0x0F|, and |0x10| are also followed by a second parameter |
| 3090 |
byte. They are used to reference the global definitions of |
| 3091 |
penalty\index{penalty}, kern\index{kern}, ligature\index{ligature}, |
| 3092 |
disc\index{discretionary hyphen}, glue\index{glue}, language\index{language}, |
| 3093 |
rule\index{rule}, and image\index{image} nodes. The parameter byte |
| 3094 |
contains the reference number. For example, the byte sequence |0x09| |
| 3095 |
|0x03| is equivalent to the node \.{<penalty *3>}. |
| 3096 |
In the long format these two-byte sequences are written, |
| 3097 |
``\.{\\P}$n$\.{\\}'' (penalty), |
| 3098 |
``\.{\\K}$n$\.{\\}'' (kern), |
| 3099 |
``\.{\\L}$n$\.{\\}'' (ligature), |
| 3100 |
``\.{\\D}$n$\.{\\}'' (disc), |
| 3101 |
``\.{\\G}$n$\.{\\}'' (glue), |
| 3102 |
``\.{\\S}$n$\.{\\}'' (speak or German ``Sprache''), |
| 3103 |
``\.{\\R}$n$\.{\\}'' (rule), and |
| 3104 |
``\.{\\I}$n$\.{\\}'' (image), where $n$ is the decimal representation |
| 3105 |
of the parameter value. |
| 3106 |
|
| 3107 |
|
| 3108 |
\item The control codes from $|txt_local|=|0x11|$ to |0x1C| are used |
| 3109 |
to reference one of the 12 font specific parameters\index{font |
| 3110 |
parameter}. In the long format they are written ``\.{\\a}'', |
| 3111 |
``\.{\\b}'', ``\.{\\c}'', \dots, ``\.{\\j}'', ``\.{\\k}'',``\.{\\l}''. |
| 3112 |
|
| 3113 |
|
| 3114 |
\item The control code $|txt_cc|=|0x1D|$ is used as a prefix for an |
| 3115 |
arbitrary character code represented as an UTF-8 multibyte sequence. |
| 3116 |
Its main purpose is providing a method for including character codes |
| 3117 |
less or equal to |0x20| which otherwise would be considered control |
| 3118 |
codes. In the long format, the byte sequence is written ``\.{\\C}$n$\.{\\}'' |
| 3119 |
where $n$ is the decimal representation of the character code. |
| 3120 |
|
| 3121 |
|
| 3122 |
\item The control code $|txt_node|=|0x1E|$ is used as a prefix for an |
| 3123 |
arbitrary node in short format. In the long format, it is written |
| 3124 |
``\.{<}'' and is followed by the node content in long format |
| 3125 |
terminated by ``\.{>}''. |
| 3126 |
|
| 3127 |
\item The control code $|txt_hyphen|=|0x1F|$ is used to access the |
| 3128 |
font specific discretionary hyphen\index{hyphen}. In the long format |
| 3129 |
it is simply written as ``\.{-}''. |
| 3130 |
|
| 3131 |
\item The control code $|txt_glue|=|0x20|$ is the space character, it |
| 3132 |
is used to access the font specific interword\index{interword glue} |
| 3133 |
glue. In the long format, we use the space character\index{space |
| 3134 |
character} ``\.{\ }'' as well. |
| 3135 |
|
| 3136 |
\item The control code $|txt_ignore|=|0xFB|$ is ignored, its position |
| 3137 |
can be used in a link to specify a position between two characters. In |
| 3138 |
the long format it is written as ``\.{\\@@}''. |
| 3139 |
|
| 3140 |
\item The control code |0xFC| is currently unused. |
| 3141 |
|
| 3142 |
\enditemize |
| 3143 |
For the control codes, we define an enumeration type |
| 3144 |
and for references, a reference type. |
| 3145 |
@<hint types@>= |
| 3146 |
typedef enum { @+txt_font=0x00, txt_global=0x08, txt_local=0x11, |
| 3147 |
txt_cc=0x1D, txt_node=0x1E, txt_hyphen=0x1F, |
| 3148 |
txt_glue=0x20, txt_ignore=0xFB} Txt; |
| 3149 |
@ |
| 3150 |
|
| 3151 |
\readcode |
| 3152 |
@s TXT symbol |
| 3153 |
@s TXT_START symbol |
| 3154 |
@s TXT_END symbol |
| 3155 |
@s TXT_FONT symbol |
| 3156 |
@s TXT_LOCAL symbol |
| 3157 |
@s TXT_GLOBAL symbol |
| 3158 |
@s TXT_FONT_GLUE symbol |
| 3159 |
@s TXT_FONT_HYPHEN symbol |
| 3160 |
@s TXT_CC symbol |
| 3161 |
@s TXT_IGNORE symbol |
| 3162 |
@s text symbol |
| 3163 |
@<scanning definitions@>= |
| 3164 |
%x TXT |
| 3165 |
@ |
| 3166 |
|
| 3167 |
@<symbols@>= |
| 3168 |
%token TXT_START TXT_END TXT_IGNORE |
| 3169 |
%token TXT_FONT_GLUE TXT_FONT_HYPHEN |
| 3170 |
%token <u> TXT_FONT TXT_LOCAL |
| 3171 |
%token <rf> TXT_GLOBAL |
| 3172 |
%token <u> TXT_CC |
| 3173 |
%type <u> text |
| 3174 |
@ |
| 3175 |
|
| 3176 |
@<scanning rules@>= |
| 3177 |
::@=\"@> :< SCAN_TXT_START; return TXT_START; >: |
| 3178 |
|
| 3179 |
<TXT>{ |
| 3180 |
::@=\"@> :< SCAN_TXT_END; return TXT_END; >: |
| 3181 |
|
| 3182 |
::@="<"@> :< SCAN_START; return START; >: |
| 3183 |
::@=">"@> :< QUIT("> not allowed in text mode");>: |
| 3184 |
|
| 3185 |
::@=\\\\@> :< yylval.u='\\'; return TXT_CC; >: |
| 3186 |
::@=\\\"@> :< yylval.u='"'; return TXT_CC; >: |
| 3187 |
::@=\\"<"@> :< yylval.u='<'; return TXT_CC; >: |
| 3188 |
::@=\\">"@> :< yylval.u='>'; return TXT_CC; >: |
| 3189 |
::@=\\" "@> :< yylval.u=' '; return TXT_CC; >: |
| 3190 |
::@=\\"-"@> :< yylval.u='-'; return TXT_CC; >: |
| 3191 |
::@=\\"@@"@> :< return TXT_IGNORE; >: |
| 3192 |
|
| 3193 |
::@=[ \t\r]*(\n[ \t\r]*)+@> :< return TXT_FONT_GLUE; >: |
| 3194 |
::@=\\[ \t\r]*\n[ \t\r]*@> :< ; >: |
| 3195 |
|
| 3196 |
::@=\\[0-7]@> :< yylval.u=yytext[1]-'0'; return TXT_FONT; >: |
| 3197 |
|
| 3198 |
::@=\\F[0-9]+\\@> :< SCAN_REF(font_kind); return TXT_GLOBAL; >: |
| 3199 |
::@=\\P[0-9]+\\@> :< SCAN_REF(penalty_kind); return TXT_GLOBAL; >: |
| 3200 |
::@=\\K[0-9]+\\@> :< SCAN_REF(kern_kind); return TXT_GLOBAL; >: |
| 3201 |
::@=\\L[0-9]+\\@> :< SCAN_REF(ligature_kind); return TXT_GLOBAL; >: |
| 3202 |
::@=\\D[0-9]+\\@> :< SCAN_REF(disc_kind); return TXT_GLOBAL; >: |
| 3203 |
::@=\\G[0-9]+\\@> :< SCAN_REF(glue_kind); return TXT_GLOBAL; >: |
| 3204 |
::@=\\S[0-9]+\\@> :< SCAN_REF(language_kind); return TXT_GLOBAL; >: |
| 3205 |
::@=\\R[0-9]+\\@> :< SCAN_REF(rule_kind); return TXT_GLOBAL; >: |
| 3206 |
::@=\\I[0-9]+\\@> :< SCAN_REF(image_kind); return TXT_GLOBAL; >: |
| 3207 |
|
| 3208 |
|
| 3209 |
::@=\\C[0-9]+\\@> :< SCAN_UDEC(yytext+2); return TXT_CC; >: |
| 3210 |
|
| 3211 |
::@=\\[a-l]@> :< yylval.u=yytext[1]-'a'; return TXT_LOCAL; >: |
| 3212 |
::@=" "@> :< return TXT_FONT_GLUE; >: |
| 3213 |
::@="-"@> :< return TXT_FONT_HYPHEN; >: |
| 3214 |
|
| 3215 |
::@={UTF8_1}@> :< SCAN_UTF8_1(yytext); return TXT_CC; >: |
| 3216 |
::@={UTF8_2}@> :< SCAN_UTF8_2(yytext); return TXT_CC; >: |
| 3217 |
::@={UTF8_3}@> :< SCAN_UTF8_3(yytext); return TXT_CC; >: |
| 3218 |
::@={UTF8_4}@> :< SCAN_UTF8_4(yytext); return TXT_CC; >: |
| 3219 |
} |
| 3220 |
@ |
| 3221 |
|
| 3222 |
@<scanning macros@>= |
| 3223 |
#define @[SCAN_REF(K)@] @[yylval.rf.k=K;@+ yylval.rf.n=atoi(yytext+2)@;@] |
| 3224 |
static int scan_level=0; |
| 3225 |
#define SCAN_START @[yy_push_state(INITIAL);@+if (1==scan_level++) hpos0=hpos;@] |
| 3226 |
#define SCAN_END @[if (scan_level--) yy_pop_state(); @/else QUIT("Too many '>' in line %d",yylineno)@] |
| 3227 |
#define SCAN_TXT_START @[BEGIN(TXT)@;@] |
| 3228 |
#define SCAN_TXT_END @[BEGIN(INITIAL)@;@] |
| 3229 |
@ |
| 3230 |
@s txt symbol |
| 3231 |
|
| 3232 |
@<parsing rules@>= |
| 3233 |
list: TXT_START position @| |
| 3234 |
{hpos+=4; /* start byte, two size byte, and boundary byte */ } |
| 3235 |
text TXT_END@| |
| 3236 |
{ $$.t=TAG(list_kind,b110);$$.p=$4; $$.s=(hpos-hstart)-$4; |
| 3237 |
hput_tags($2,hput_list($2+1, &($$)));@+}; |
| 3238 |
text: position @+| text txt; |
| 3239 |
|
| 3240 |
txt: TXT_CC { hput_txt_cc($1); } |
| 3241 |
| TXT_FONT { REF(font_kind,$1); hput_txt_font($1); } |
| 3242 |
| TXT_GLOBAL { REF($1.k,$1.n); hput_txt_global(&($1)); } |
| 3243 |
| TXT_LOCAL { RNG("Font parameter",$1,0,11); hput_txt_local($1); } |
| 3244 |
| TXT_FONT_GLUE { HPUTX(1); HPUT8(txt_glue); } |
| 3245 |
| TXT_FONT_HYPHEN { HPUTX(1);HPUT8(txt_hyphen); } |
| 3246 |
| TXT_IGNORE { HPUTX(1);HPUT8(txt_ignore); } |
| 3247 |
| { HPUTX(1); HPUT8(txt_node);} content_node; |
| 3248 |
@ |
| 3249 |
|
| 3250 |
The following function keeps track of the position in the current line. |
| 3251 |
If the line gets too long it will break the text at the next space |
| 3252 |
character. If no suitable space character comes along, |
| 3253 |
the line will be broken after any regular character. |
| 3254 |
|
| 3255 |
\writecode |
| 3256 |
@<write a text@>= |
| 3257 |
{@+if (l->s==0) hwritef(" \"\""); |
| 3258 |
else@/ |
| 3259 |
{ int pos=nesting+20; /* estimate */ |
| 3260 |
hwritef(" \""); |
| 3261 |
while(hpos<hend)@/ |
| 3262 |
{ int i=hget_txt(); |
| 3263 |
if (i<0) |
| 3264 |
{ if (pos++<70) hwritec(' '); |
| 3265 |
else hwrite_nesting(), pos=nesting; |
| 3266 |
} |
| 3267 |
else if (i==1 && pos>=100)@/ |
| 3268 |
{ hwritec('\\'); @+hwrite_nesting(); @+pos=nesting; @+} |
| 3269 |
else |
| 3270 |
pos+=i; |
| 3271 |
} |
| 3272 |
hwritec('"'); |
| 3273 |
} |
| 3274 |
} |
| 3275 |
@ |
| 3276 |
|
| 3277 |
|
| 3278 |
The function returns the number of characters written |
| 3279 |
because this information is needed in |hget_txt| below. |
| 3280 |
|
| 3281 |
@<write functions@>= |
| 3282 |
int hwrite_txt_cc(uint32_t c) |
| 3283 |
{@+ if (c<0x20) |
| 3284 |
return hwritef("\\C%d\\",c); |
| 3285 |
else@+ |
| 3286 |
switch(c) |
| 3287 |
{ case '\\': return hwritef("\\\\"); |
| 3288 |
case '"': return hwritef("\\\""); |
| 3289 |
case '<': return hwritef("\\<"); |
| 3290 |
case '>': return hwritef("\\>"); |
| 3291 |
case ' ': return hwritef("\\ "); |
| 3292 |
case '-': return hwritef("\\-"); |
| 3293 |
default: return option_utf8?hwrite_utf8(c):hwritef("\\C%d\\",c); |
| 3294 |
} |
| 3295 |
} |
| 3296 |
@ |
| 3297 |
|
| 3298 |
\getcode |
| 3299 |
@<get macros@>= |
| 3300 |
#define @[HGET_GREF(K,S)@] {uint8_t n=HGET8;@+ REF(K,n); @+ return hwritef("\\" S "%d\\",n);@+} |
| 3301 |
|
| 3302 |
@ |
| 3303 |
|
| 3304 |
The function |hget_txt| reads a text element and writes it immediately. |
| 3305 |
To enable the insertion of line breaks when writing a text, we need to keep track |
| 3306 |
of the number of characters in the current line. For this purpose |
| 3307 |
the function |hget_txt| returns the number of characters written. |
| 3308 |
It returns $-1$ if a space character needs to be written |
| 3309 |
providing a good opportunity for a break. |
| 3310 |
|
| 3311 |
@<get functions@>= |
| 3312 |
int hget_txt(void) |
| 3313 |
{@+ if (*hpos>=0x80 && *hpos<=0xF7) |
| 3314 |
{ if (option_utf8) |
| 3315 |
return hwrite_utf8(hget_utf8()); |
| 3316 |
else |
| 3317 |
return hwritef("\\C%d\\",hget_utf8()); |
| 3318 |
} |
| 3319 |
else @/ |
| 3320 |
{ uint8_t a; |
| 3321 |
a=HGET8; |
| 3322 |
switch (a) |
| 3323 |
{ case txt_font+0: return hwritef("\\0"); |
| 3324 |
case txt_font+1: return hwritef("\\1"); |
| 3325 |
case txt_font+2: return hwritef("\\2"); |
| 3326 |
case txt_font+3: return hwritef("\\3"); |
| 3327 |
case txt_font+4: return hwritef("\\4"); |
| 3328 |
case txt_font+5: return hwritef("\\5"); |
| 3329 |
case txt_font+6: return hwritef("\\6"); |
| 3330 |
case txt_font+7: return hwritef("\\7"); |
| 3331 |
case txt_global+0: HGET_GREF(font_kind,"F"); |
| 3332 |
case txt_global+1: HGET_GREF(penalty_kind,"P"); |
| 3333 |
case txt_global+2: HGET_GREF(kern_kind,"K"); |
| 3334 |
case txt_global+3: HGET_GREF(ligature_kind,"L"); |
| 3335 |
case txt_global+4: HGET_GREF(disc_kind,"D"); |
| 3336 |
case txt_global+5: HGET_GREF(glue_kind,"G"); |
| 3337 |
case txt_global+6: HGET_GREF(language_kind,"S"); |
| 3338 |
case txt_global+7: HGET_GREF(rule_kind,"R"); |
| 3339 |
case txt_global+8: HGET_GREF(image_kind,"I"); |
| 3340 |
case txt_local+0: return hwritef("\\a"); |
| 3341 |
case txt_local+1: return hwritef("\\b"); |
| 3342 |
case txt_local+2: return hwritef("\\c"); |
| 3343 |
case txt_local+3: return hwritef("\\d"); |
| 3344 |
case txt_local+4: return hwritef("\\e"); |
| 3345 |
case txt_local+5: return hwritef("\\f"); |
| 3346 |
case txt_local+6: return hwritef("\\g"); |
| 3347 |
case txt_local+7: return hwritef("\\h"); |
| 3348 |
case txt_local+8: return hwritef("\\i"); |
| 3349 |
case txt_local+9: return hwritef("\\j"); |
| 3350 |
case txt_local+10: return hwritef("\\k"); |
| 3351 |
case txt_local+11: return hwritef("\\l"); |
| 3352 |
case txt_cc: return hwrite_txt_cc(hget_utf8()); |
| 3353 |
case txt_node: { int i; |
| 3354 |
@<read the start byte |a|@>@; |
| 3355 |
i=hwritef("<"); |
| 3356 |
i+= hwritef("%s",content_name[KIND(a)]);@+ hget_content(a); |
| 3357 |
@<read and check the end byte |z|@>@; |
| 3358 |
hwritec('>');@+ return i+10; /* just an estimate */ |
| 3359 |
} |
| 3360 |
case txt_hyphen: hwritec('-'); @+return 1; |
| 3361 |
case txt_glue: return -1; |
| 3362 |
case '<': return hwritef("\\<"); |
| 3363 |
case '>': return hwritef("\\>"); |
| 3364 |
case '"': return hwritef("\\\""); |
| 3365 |
case '-': return hwritef("\\-"); |
| 3366 |
case txt_ignore: return hwritef("\\@@"); |
| 3367 |
default: hwritec(a); @+return 1; |
| 3368 |
} |
| 3369 |
} |
| 3370 |
} |
| 3371 |
|
| 3372 |
@ |
| 3373 |
|
| 3374 |
|
| 3375 |
\putcode |
| 3376 |
@<put functions@>= |
| 3377 |
|
| 3378 |
void hput_txt_cc(uint32_t c) |
| 3379 |
{ @+ if (c<=0x20) { HPUTX(2); HPUT8(txt_cc);@+ HPUT8(c); @+ } |
| 3380 |
else hput_utf8(c); |
| 3381 |
} |
| 3382 |
|
| 3383 |
void hput_txt_font(uint8_t f) |
| 3384 |
{@+ if (f<8) HPUTX(1),HPUT8(txt_font+f); |
| 3385 |
else QUIT("Use \\F%d\\ instead of \\%d for font %d in a text",f,f,f); |
| 3386 |
} |
| 3387 |
|
| 3388 |
void hput_txt_global(Ref *d) |
| 3389 |
{ @+ HPUTX(2); |
| 3390 |
switch (d->k) |
| 3391 |
{ case font_kind: HPUT8(txt_global+0);@+ break; |
| 3392 |
case penalty_kind: HPUT8(txt_global+1);@+ break; |
| 3393 |
case kern_kind: HPUT8(txt_global+2);@+ break; |
| 3394 |
case ligature_kind: HPUT8(txt_global+3);@+ break; |
| 3395 |
case disc_kind: HPUT8(txt_global+4);@+ break; |
| 3396 |
case glue_kind: HPUT8(txt_global+5);@+ break; |
| 3397 |
case language_kind: HPUT8(txt_global+6);@+ break; |
| 3398 |
case rule_kind: HPUT8(txt_global+7);@+ break; |
| 3399 |
case image_kind: HPUT8(txt_global+8);@+ break; |
| 3400 |
default: QUIT("Kind %s not allowed as a global reference in a text",NAME(d->k)); |
| 3401 |
} |
| 3402 |
HPUT8(d->n); |
| 3403 |
} |
| 3404 |
|
| 3405 |
void hput_txt_local(uint8_t n) |
| 3406 |
{ HPUTX(1); |
| 3407 |
HPUT8(txt_local+n); |
| 3408 |
} |
| 3409 |
@ |
| 3410 |
|
| 3411 |
|
| 3412 |
|
| 3413 |
\section{Composite Nodes}\hascode |
| 3414 |
\label{composite} |
| 3415 |
The nodes that we consider in this section can contain one or more list nodes. |
| 3416 |
When we implement the parsing\index{parsing} routines |
| 3417 |
for composite nodes in the long format, we have to take into account |
| 3418 |
that parsing such a list node will already write the list node |
| 3419 |
to the output. So we split the parsing of composite nodes into several parts |
| 3420 |
and store the parts immediately after parsing them. On the parse stack, we will only |
| 3421 |
keep track of the info value. |
| 3422 |
This new strategy is not as transparent as our previous strategy used for |
| 3423 |
simple nodes where we had a clean separation of reading and writing: |
| 3424 |
reading would store the internal representation of a node and writing the internal |
| 3425 |
representation to output would start only after reading is completed. |
| 3426 |
The new strategy, however, makes it easier to reuse |
| 3427 |
the grammar\index{grammar} rules for the component nodes. |
| 3428 |
|
| 3429 |
Another rule applies to composite nodes: in the short format, the subnodes |
| 3430 |
will come at the end of the node, and especially a list node that contains content nodes |
| 3431 |
comes last. This helps when traversing the content section as we will see in |
| 3432 |
appendix~\secref{fastforward}. |
| 3433 |
|
| 3434 |
\subsection{Boxes}\label{boxnodes} |
| 3435 |
The central structuring elements of \TeX\ are boxes\index{box}. |
| 3436 |
Boxes have a height |h|, a depth |d|, and a width |w|. |
| 3437 |
The shift amount |a| shifts the contents of the box, |
| 3438 |
the glue ratio\index{glue ratio} |r| is a factor applied to the glue inside the box, |
| 3439 |
the glue order |o| is its order of stretchability\index{stretchability}, |
| 3440 |
and the glue sign |s| is $-1$ for shrinking\index{shrinkability}, |
| 3441 |
0 for rigid, and $+1$ for stretching. |
| 3442 |
Most importantly, a box contains a list |l| of content nodes inside the box. |
| 3443 |
|
| 3444 |
|
| 3445 |
@<hint types@>= |
| 3446 |
typedef struct @/{@+ Dimen h,d,w,a;@+ float32_t r;@+ int8_t s,o; @+List l; @+} Box; |
| 3447 |
@ |
| 3448 |
|
| 3449 |
There are two types of boxes: horizontal\index{horizontal box} boxes |
| 3450 |
and vertical\index{vertical box} boxes. |
| 3451 |
The difference between the two is simple: |
| 3452 |
a horizontal box aligns the reference\index{reference point} |
| 3453 |
points of its content nodes horizontally, and a positive shift amount\index{shift amount} |a| |
| 3454 |
shifts the box down; |
| 3455 |
a vertical box aligns\index{alignment} the reference\index{reference point} |
| 3456 |
points vertically, and a positive shift amount |a| shifts the box right. |
| 3457 |
|
| 3458 |
Not all box parameters are used frequently. In the short format, we use the info bits |
| 3459 |
to indicated which of the parameters are used. |
| 3460 |
Where as the width of a horizontal box is most of the time (80\%) nonzero, |
| 3461 |
other parameters are most of the time zero, |
| 3462 |
like the shift amount (99\%) or the glue settings (99.8\%). |
| 3463 |
The depth is zero in about 77\%, the height in about 53\%, |
| 3464 |
and both together are zero in about 47\%. The results for vertical boxes, |
| 3465 |
which constitute about 20\% of all boxes, are similar, |
| 3466 |
except that the depth is zero in about 89\%, |
| 3467 |
but the height and width are almost never zero. |
| 3468 |
For this reason we use bit |b001| to indicate a nonzero depth, |
| 3469 |
bit |b010| for a nonzero shift amount, and |b100| for nonzero glue settings. |
| 3470 |
Glue sign and glue order can be packed as two nibbles in a single byte. |
| 3471 |
% A different use of the info bits for vertical and horizontal boxes is possible, |
| 3472 |
% but does not warrant the added complexity. |
| 3473 |
|
| 3474 |
|
| 3475 |
|
| 3476 |
\goodbreak |
| 3477 |
\readcode |
| 3478 |
@s HBOX symbol |
| 3479 |
@s VBOX symbol |
| 3480 |
@s box symbol |
| 3481 |
@s boxparams symbol |
| 3482 |
@s hbox_node symbol |
| 3483 |
@s vbox_node symbol |
| 3484 |
@s box_dimen symbol |
| 3485 |
@s box_shift symbol |
| 3486 |
@s box_glue_set symbol |
| 3487 |
@<symbols@>= |
| 3488 |
%token HBOX "hbox" |
| 3489 |
%token VBOX "vbox" |
| 3490 |
%token SHIFTED "shifted" |
| 3491 |
%type <info> box box_dimen box_shift box_glue_set |
| 3492 |
|
| 3493 |
@ |
| 3494 |
@<scanning rules@>= |
| 3495 |
::@=hbox@> :< return HBOX; >: |
| 3496 |
::@=vbox@> :< return VBOX; >: |
| 3497 |
::@=shifted@> :< return SHIFTED; >: |
| 3498 |
@ |
| 3499 |
|
| 3500 |
@<parsing rules@>=@/ |
| 3501 |
|
| 3502 |
box_dimen: dimension dimension dimension @/ |
| 3503 |
{$$= hput_box_dimen($1,$2,$3); }; |
| 3504 |
box_shift: {$$=b000;} @+ |
| 3505 |
| SHIFTED dimension {$$=hput_box_shift($2);}; |
| 3506 |
|
| 3507 |
box_glue_set: {$$=b000;} |
| 3508 |
| PLUS stretch { $$=hput_box_glue_set(+1,$2.f,$2.o); } |
| 3509 |
| MINUS stretch { $$=hput_box_glue_set(-1,$2.f,$2.o); }; |
| 3510 |
|
| 3511 |
|
| 3512 |
box: box_dimen box_shift box_glue_set list {$$=$1|$2|$3; }; |
| 3513 |
|
| 3514 |
hbox_node: start HBOX box END { hput_tags($1, TAG(hbox_kind,$3)); }; |
| 3515 |
vbox_node: start VBOX box END { hput_tags($1, TAG(vbox_kind,$3)); }; |
| 3516 |
content_node: hbox_node @+ | vbox_node; |
| 3517 |
@ |
| 3518 |
|
| 3519 |
\writecode |
| 3520 |
@<write functions@>= |
| 3521 |
void hwrite_box(Box *b) |
| 3522 |
{ hwrite_dimension(b->h); |
| 3523 |
hwrite_dimension(b->d); |
| 3524 |
hwrite_dimension(b->w); |
| 3525 |
if (b->a!=0) { hwritef(" shifted"); @+hwrite_dimension(b->a); @+} |
| 3526 |
if (b->r!=0.0 && b->s!=0 )@/ |
| 3527 |
{ @+if (b->s>0) @+hwritef(" plus"); @+else @+hwritef(" minus"); |
| 3528 |
@+hwrite_float64(b->r, false); @+hwrite_order(b->o); |
| 3529 |
} |
| 3530 |
hwrite_list(&(b->l)); |
| 3531 |
} |
| 3532 |
@ |
| 3533 |
|
| 3534 |
\getcode |
| 3535 |
@<cases to get content@>= |
| 3536 |
@t\1\kern1em@> |
| 3537 |
case TAG(hbox_kind,b000): {Box b; @+HGET_BOX(b000,b); @+hwrite_box(&b);@+} @+ break; |
| 3538 |
case TAG(hbox_kind,b001): {Box b; @+HGET_BOX(b001,b); @+hwrite_box(&b);@+} @+ break; |
| 3539 |
case TAG(hbox_kind,b010): {Box b; @+HGET_BOX(b010,b); @+hwrite_box(&b);@+} @+ break; |
| 3540 |
case TAG(hbox_kind,b011): {Box b; @+HGET_BOX(b011,b); @+hwrite_box(&b);@+} @+ break; |
| 3541 |
case TAG(hbox_kind,b100): {Box b; @+HGET_BOX(b100,b); @+hwrite_box(&b);@+} @+ break; |
| 3542 |
case TAG(hbox_kind,b101): {Box b; @+HGET_BOX(b101,b); @+hwrite_box(&b);@+} @+ break; |
| 3543 |
case TAG(hbox_kind,b110): {Box b; @+HGET_BOX(b110,b); @+hwrite_box(&b);@+} @+ break; |
| 3544 |
case TAG(hbox_kind,b111): {Box b; @+HGET_BOX(b111,b); @+hwrite_box(&b);@+} @+ break; |
| 3545 |
case TAG(vbox_kind,b000): {Box b; @+HGET_BOX(b000,b); @+hwrite_box(&b);@+} @+ break; |
| 3546 |
case TAG(vbox_kind,b001): {Box b; @+HGET_BOX(b001,b); @+hwrite_box(&b);@+} @+ break; |
| 3547 |
case TAG(vbox_kind,b010): {Box b; @+HGET_BOX(b010,b); @+hwrite_box(&b);@+} @+ break; |
| 3548 |
case TAG(vbox_kind,b011): {Box b; @+HGET_BOX(b011,b); @+hwrite_box(&b);@+} @+ break; |
| 3549 |
case TAG(vbox_kind,b100): {Box b; @+HGET_BOX(b100,b); @+hwrite_box(&b);@+} @+ break; |
| 3550 |
case TAG(vbox_kind,b101): {Box b; @+HGET_BOX(b101,b); @+hwrite_box(&b);@+} @+ break; |
| 3551 |
case TAG(vbox_kind,b110): {Box b; @+HGET_BOX(b110,b); @+hwrite_box(&b);@+} @+ break; |
| 3552 |
case TAG(vbox_kind,b111): {Box b; @+HGET_BOX(b111,b); @+hwrite_box(&b);@+} @+ break; |
| 3553 |
@ |
| 3554 |
|
| 3555 |
@<get macros@>= |
| 3556 |
#define @[HGET_BOX(I,B)@] \ |
| 3557 |
HGET32(B.h);\ |
| 3558 |
if ((I)&b001) HGET32(B.d); @+ else B.d=0;\ |
| 3559 |
HGET32(B.w);\ |
| 3560 |
if ((I)&b010) HGET32(B.a); @+else B.a=0;\ |
| 3561 |
if ((I)&b100) @/{ B.r=hget_float32();@+ B.s=HGET8; @+ B.o=B.s&0xF; @+B.s=B.s>>4;@+ }\ |
| 3562 |
else { B.r=0.0;@+ B.o=B.s=0;@+ }\ |
| 3563 |
hget_list(&(B.l)); |
| 3564 |
@ |
| 3565 |
|
| 3566 |
@<get functions@>= |
| 3567 |
void hget_hbox_node(void) |
| 3568 |
{ Box b; |
| 3569 |
@<read the start byte |a|@>@; |
| 3570 |
if (KIND(a)!=hbox_kind) QUIT("Hbox expected at 0x%x got %s",node_pos,NAME(a)); |
| 3571 |
HGET_BOX(INFO(a),b);@/ |
| 3572 |
@<read and check the end byte |z|@>@; |
| 3573 |
hwrite_start();@+ |
| 3574 |
hwritef("hbox");@+ |
| 3575 |
hwrite_box(&b);@+ |
| 3576 |
hwrite_end(); |
| 3577 |
} |
| 3578 |
|
| 3579 |
|
| 3580 |
void hget_vbox_node(void) |
| 3581 |
{ Box b; |
| 3582 |
@<read the start byte |a|@>@; |
| 3583 |
if (KIND(a)!=vbox_kind) QUIT("Vbox expected at 0x%x got %s",node_pos,NAME(a)); |
| 3584 |
HGET_BOX(INFO(a),b);@/ |
| 3585 |
@<read and check the end byte |z|@>@; |
| 3586 |
hwrite_start();@+ |
| 3587 |
hwritef("vbox");@+ |
| 3588 |
hwrite_box(&b);@+ |
| 3589 |
hwrite_end(); |
| 3590 |
} |
| 3591 |
@ |
| 3592 |
|
| 3593 |
\putcode |
| 3594 |
@<put functions@>= |
| 3595 |
|
| 3596 |
Info hput_box_dimen(Dimen h, Dimen d, Dimen w) |
| 3597 |
{ Info i; |
| 3598 |
@+HPUT32(h); |
| 3599 |
if (d!=0) { HPUT32(d); @+i=b001;@+ } @+else@+ i=b000; |
| 3600 |
HPUT32(w); |
| 3601 |
return i; |
| 3602 |
} |
| 3603 |
Info hput_box_shift(Dimen a) |
| 3604 |
{ @+if (a!=0) { @+ HPUT32(a); @+return @+ b010;@+} @+ else @+return b000; |
| 3605 |
} |
| 3606 |
|
| 3607 |
Info hput_box_glue_set(int8_t s, float32_t r, Order o) |
| 3608 |
{ @+if (r!=0.0 && s!=0 ) |
| 3609 |
{ hput_float32(r);@+ |
| 3610 |
HPUT8((s<<4)|o);@+ |
| 3611 |
return b100;@+ |
| 3612 |
} |
| 3613 |
else return b000; |
| 3614 |
} |
| 3615 |
|
| 3616 |
@ |
| 3617 |
|
| 3618 |
\subsection{Extended Boxes} |
| 3619 |
Hi\TeX\ produces two kinds of extended\index{extended box} horizontal |
| 3620 |
boxes, |hpack_kind| and |hset_kind|, and the same for vertical boxes |
| 3621 |
using |vpack_kind| and |vset_kind|. Let us focus on horizontal boxes; |
| 3622 |
the handling of vertical boxes is completely parallel. |
| 3623 |
|
| 3624 |
The \\{hpack} procedure of Hi\TeX\ produces an extended box of |hset_kind| |
| 3625 |
either if it is given an extended\index{extended dimension} dimension as its width |
| 3626 |
or if it discovers that the width of its content is an extended |
| 3627 |
dimension. After the final width of the box has been computed in the |
| 3628 |
viewer, it just remains to set the glue; a very simple operation |
| 3629 |
indeed. |
| 3630 |
|
| 3631 |
If the \\{hpack} procedure of Hi\TeX\ can not determine the natural |
| 3632 |
dimensions of the box content because it contains |
| 3633 |
paragraphs\index{paragraph} or other extended boxes, it produces a box |
| 3634 |
of |hpack_kind|. Now the viewer needs to traverse the list of content |
| 3635 |
nodes to determine the natural\index{natural dimension} |
| 3636 |
dimensions. Even the amount of stretchability\index{stretchability} |
| 3637 |
and shrinkability\index{shrinkability} has to be determined in the |
| 3638 |
viewer. For example, the final stretchability of a paragraph with some |
| 3639 |
stretchability in the baseline\index{baseline skip} skip will depend |
| 3640 |
on the number of lines which, in turn, depends on \.{hsize}. It is |
| 3641 |
not possible to merge these traversals of the box content with the |
| 3642 |
traversal necessary when displaying the box. The latter needs to |
| 3643 |
convert glue nodes into positioning instructions which requires a |
| 3644 |
fixed glue\index{glue ratio} ratio. The computation of the glue ratio, |
| 3645 |
however, requires a complete traversal of the content. |
| 3646 |
|
| 3647 |
In the short format of a box node of type |hset_kind|, |vset_kind|, |
| 3648 |
|hpack_kind|, or |vpack_kind|, the info bit |b100| indicates, if set, |
| 3649 |
a complete extended dimension, and if unset, a reference to a |
| 3650 |
predefined extended dimension for the target size; the info bit |b010| |
| 3651 |
indicates a nonzero shift amount. For a box of type |hset_kind| or |
| 3652 |
|vset_kind|, the info bit |b001| indicates, if set, a nonzero depth. |
| 3653 |
For a box of type |hpack_kind| or |vpack_kind|, the info bit |b001| |
| 3654 |
indicates, if set, an additional target size, and if unset, an exact |
| 3655 |
target size. For a box of type |vpack_kind| also the maximum depth |
| 3656 |
is given. If in the long format the maximum depth is omitted, the |
| 3657 |
value |MAX_DIMEN| is used. |
| 3658 |
|
| 3659 |
The reference point of a vertical box is usually the reference point of |
| 3660 |
the last box inside it and multiple vertical boxes are aligned |
| 3661 |
along this common baseline. Occasionaly, however, we want to align |
| 3662 |
vertical boxes using the baselines of their first box. |
| 3663 |
We indicate this alternative setting of the reference point |
| 3664 |
using the keyword {\tt top} in the long form. |
| 3665 |
In the short form, we use the fact the the absolut value of any dimension |
| 3666 |
is less or equal to |MAX_DIMEN| which is equal to |0x3fffffff|. This means |
| 3667 |
that the two most significant bits are always the same. So a vtop node |
| 3668 |
can be marked by toggling the second of these bits. |
| 3669 |
|
| 3670 |
\readcode |
| 3671 |
@s box_options symbol |
| 3672 |
@s vbox_dimen symbol |
| 3673 |
@s hpack symbol |
| 3674 |
@s vpack symbol |
| 3675 |
@s box_goal symbol |
| 3676 |
@s HPACK symbol |
| 3677 |
@s HSET symbol |
| 3678 |
@s VPACK symbol |
| 3679 |
@s VSET symbol |
| 3680 |
@s TO symbol |
| 3681 |
@s ADD symbol |
| 3682 |
@s box_flex symbol |
| 3683 |
@s vxbox_node symbol |
| 3684 |
@s hxbox_node symbol |
| 3685 |
@s DEPTH symbol |
| 3686 |
@s max_depth symbol |
| 3687 |
|
| 3688 |
@<symbols@>= |
| 3689 |
%token HPACK "hpack" |
| 3690 |
%token HSET "hset" |
| 3691 |
%token VPACK "vpack" |
| 3692 |
%token VSET "vset" |
| 3693 |
%token DEPTH "depth" |
| 3694 |
%token ADD "add" |
| 3695 |
%token TO "to" |
| 3696 |
%type <info> box_options box_goal hpack vpack vbox_dimen |
| 3697 |
%type <d> max_depth |
| 3698 |
@ |
| 3699 |
|
| 3700 |
@<scanning rules@>= |
| 3701 |
::@=hpack@> :< return HPACK; >: |
| 3702 |
::@=hset@> :< return HSET; >: |
| 3703 |
::@=vpack@> :< return VPACK; >: |
| 3704 |
::@=vset@> :< return VSET; >: |
| 3705 |
::@=add@> :< return ADD; >: |
| 3706 |
::@=to@> :< return TO; >: |
| 3707 |
::@=depth@> :< return DEPTH; >: |
| 3708 |
@ |
| 3709 |
|
| 3710 |
@<parsing rules@>= |
| 3711 |
box_flex: plus minus { hput_stretch(&($1));hput_stretch(&($2)); }; |
| 3712 |
|
| 3713 |
box_options: box_shift box_flex xdimen_ref list {$$=$1;} |
| 3714 |
| box_shift box_flex xdimen_node list {$$=$1|b100;}; |
| 3715 |
|
| 3716 |
hxbox_node: start HSET box_dimen box_options END { hput_tags($1, TAG(hset_kind,$3|$4)); }; |
| 3717 |
|
| 3718 |
vbox_dimen: box_dimen |
| 3719 |
| TOP dimension dimension dimension @/ |
| 3720 |
{$$= hput_box_dimen($2,$3^0x40000000,$4); }; |
| 3721 |
|
| 3722 |
vxbox_node: start VSET vbox_dimen box_options END { hput_tags($1, TAG(vset_kind,$3|$4)); }; |
| 3723 |
|
| 3724 |
box_goal: TO xdimen_ref {$$=b000;} |
| 3725 |
| ADD xdimen_ref {$$=b001;} |
| 3726 |
| TO xdimen_node {$$=b100;} |
| 3727 |
| ADD xdimen_node {$$=b101;}; |
| 3728 |
|
| 3729 |
hpack: box_shift box_goal list {$$=$2;}; |
| 3730 |
|
| 3731 |
hxbox_node: start HPACK hpack END { hput_tags($1, TAG(hpack_kind,$3)); }; |
| 3732 |
|
| 3733 |
max_depth: {$$=MAX_DIMEN;} | MAX DEPTH dimension { $$=$3; }; |
| 3734 |
|
| 3735 |
vpack: max_depth {HPUT32($1);} box_shift box_goal list {$$= $3|$4;} |
| 3736 |
| TOP max_depth {HPUT32($2^0x40000000);} @/ box_shift box_goal list {$$= $4|$5;}; |
| 3737 |
|
| 3738 |
vxbox_node: start VPACK vpack END { hput_tags($1, TAG(vpack_kind,$3)); }; |
| 3739 |
|
| 3740 |
content_node: vxbox_node | hxbox_node; |
| 3741 |
@ |
| 3742 |
|
| 3743 |
\getcode |
| 3744 |
@<cases to get content@>= |
| 3745 |
@t\1\kern1em@> |
| 3746 |
case TAG(hset_kind,b000): HGET_SET(hset_kind,b000); @+ break; |
| 3747 |
case TAG(hset_kind,b001): HGET_SET(hset_kind,b001); @+ break; |
| 3748 |
case TAG(hset_kind,b010): HGET_SET(hset_kind,b010); @+ break; |
| 3749 |
case TAG(hset_kind,b011): HGET_SET(hset_kind,b011); @+ break; |
| 3750 |
case TAG(hset_kind,b100): HGET_SET(hset_kind,b100); @+ break; |
| 3751 |
case TAG(hset_kind,b101): HGET_SET(hset_kind,b101); @+ break; |
| 3752 |
case TAG(hset_kind,b110): HGET_SET(hset_kind,b110); @+ break; |
| 3753 |
case TAG(hset_kind,b111): HGET_SET(hset_kind,b111); @+ break;@# |
| 3754 |
|
| 3755 |
case TAG(vset_kind,b000): HGET_SET(vset_kind,b000); @+ break; |
| 3756 |
case TAG(vset_kind,b001): HGET_SET(vset_kind,b001); @+ break; |
| 3757 |
case TAG(vset_kind,b010): HGET_SET(vset_kind,b010); @+ break; |
| 3758 |
case TAG(vset_kind,b011): HGET_SET(vset_kind,b011); @+ break; |
| 3759 |
case TAG(vset_kind,b100): HGET_SET(vset_kind,b100); @+ break; |
| 3760 |
case TAG(vset_kind,b101): HGET_SET(vset_kind,b101); @+ break; |
| 3761 |
case TAG(vset_kind,b110): HGET_SET(vset_kind,b110); @+ break; |
| 3762 |
case TAG(vset_kind,b111): HGET_SET(vset_kind,b111); @+ break;@# |
| 3763 |
|
| 3764 |
case TAG(hpack_kind,b000): HGET_PACK(hpack_kind,b000); @+ break; |
| 3765 |
case TAG(hpack_kind,b001): HGET_PACK(hpack_kind,b001); @+ break; |
| 3766 |
case TAG(hpack_kind,b010): HGET_PACK(hpack_kind,b010); @+ break; |
| 3767 |
case TAG(hpack_kind,b011): HGET_PACK(hpack_kind,b011); @+ break; |
| 3768 |
case TAG(hpack_kind,b100): HGET_PACK(hpack_kind,b100); @+ break; |
| 3769 |
case TAG(hpack_kind,b101): HGET_PACK(hpack_kind,b101); @+ break; |
| 3770 |
case TAG(hpack_kind,b110): HGET_PACK(hpack_kind,b110); @+ break; |
| 3771 |
case TAG(hpack_kind,b111): HGET_PACK(hpack_kind,b111); @+ break;@# |
| 3772 |
|
| 3773 |
case TAG(vpack_kind,b000): HGET_PACK(vpack_kind,b000); @+ break; |
| 3774 |
case TAG(vpack_kind,b001): HGET_PACK(vpack_kind,b001); @+ break; |
| 3775 |
case TAG(vpack_kind,b010): HGET_PACK(vpack_kind,b010); @+ break; |
| 3776 |
case TAG(vpack_kind,b011): HGET_PACK(vpack_kind,b011); @+ break; |
| 3777 |
case TAG(vpack_kind,b100): HGET_PACK(vpack_kind,b100); @+ break; |
| 3778 |
case TAG(vpack_kind,b101): HGET_PACK(vpack_kind,b101); @+ break; |
| 3779 |
case TAG(vpack_kind,b110): HGET_PACK(vpack_kind,b110); @+ break; |
| 3780 |
case TAG(vpack_kind,b111): HGET_PACK(vpack_kind,b111); @+ break; |
| 3781 |
@ |
| 3782 |
|
| 3783 |
|
| 3784 |
@<get macros@>= |
| 3785 |
#define @[HGET_SET(K,I)@] @/\ |
| 3786 |
{ Dimen h,d; @+HGET32(h);\ |
| 3787 |
if ((I)&b001) HGET32(d); @+ else d=0;\ |
| 3788 |
if (K==vset_kind && (d>MAX_DIMEN || d<-MAX_DIMEN)) { hwritef(" top"); d^=0x40000000;}\ |
| 3789 |
hwrite_dimension(h); hwrite_dimension(d); @+}\ |
| 3790 |
{ Dimen w; @+HGET32(w); @+hwrite_dimension(w);@+} \ |
| 3791 |
if ((I)&b010) { Dimen a; @+HGET32(a); hwritef(" shifted"); @+hwrite_dimension(a);@+}\ |
| 3792 |
{ Stretch p; @+HGET_STRETCH(p);@+hwrite_plus(&p);@+}\ |
| 3793 |
{ Stretch m; @+HGET_STRETCH(m);@+hwrite_minus(&m);@+}\ |
| 3794 |
if ((I)&b100) {Xdimen x;@+ hget_xdimen_node(&x); @+hwrite_xdimen_node(&x);@+} else HGET_REF(xdimen_kind);\ |
| 3795 |
{ List l; @+hget_list(&l);@+ hwrite_list(&l); @+} |
| 3796 |
@# |
| 3797 |
|
| 3798 |
#define @[HGET_PACK(K,I)@] @/\ |
| 3799 |
if (K==vpack_kind) {@+Dimen d; HGET32(d); \ |
| 3800 |
if (d>MAX_DIMEN || d<-MAX_DIMEN) { hwritef(" top"); d^=0x40000000;}\ |
| 3801 |
if (d!=MAX_DIMEN) {hwritef(" max depth");@+hwrite_dimension(d);}} \ |
| 3802 |
if ((I)&b010)@+{@+Dimen s; HGET32(s); hwritef(" shifted"); @+hwrite_dimension(s); }\ |
| 3803 |
if ((I)&b001) hwritef(" add");@+ else hwritef(" to");\ |
| 3804 |
if ((I)&b100) {Xdimen x;@+ hget_xdimen_node(&x);@+hwrite_xdimen_node(&x);@+}\ |
| 3805 |
else @+HGET_REF(xdimen_kind);\ |
| 3806 |
{ List l; @+hget_list(&l);@+ hwrite_list(&l); @+} |
| 3807 |
@ |
| 3808 |
|
| 3809 |
|
| 3810 |
\subsection{Leaders}\label{leaders} |
| 3811 |
Leaders\index{leaders} are a special type of glue that is best explained by a few |
| 3812 |
examples. |
| 3813 |
Where as ordinary glue fills its designated space with \hfil\ whiteness,\break |
| 3814 |
leaders fill their designated space with either a rule \xleaders\hrule\hfil\ or\break |
| 3815 |
some sort of repeated\leaders\hbox to 15pt{$\hss.\hss$}\hfil content.\break |
| 3816 |
In multiple leaders, the dots\leaders\hbox to 15pt{$\hss.\hss$}\hfil are usually aligned\index{alignment} across lines,\break |
| 3817 |
as in the last\leaders\hbox to 15pt{$\hss.\hss$}\hfil three lines.\break |
| 3818 |
Unless you specify centered\index{centered}\cleaders\hbox to 15pt{$\hss.\hss$}\hfil leaders\break |
| 3819 |
or you specify expanded\index{expanded}\xleaders\hbox to 15pt{$\hss.\hss$}\hfil leaders.\break |
| 3820 |
The former pack the repeated content tight and center |
| 3821 |
the repeated content in the available space, the latter distributes |
| 3822 |
the extra space between all the repeated instances. |
| 3823 |
|
| 3824 |
In the short format, the two lowest info bits store the type |
| 3825 |
of leaders: 1 for aligned, 2 for centered, and 3 for expanded. |
| 3826 |
The |b100| info bit is usually set and only zero in the unlikely |
| 3827 |
case that the glue is zero and therefore not present. |
| 3828 |
|
| 3829 |
\readcode |
| 3830 |
@s LEADERS symbol |
| 3831 |
@s ALIGN symbol |
| 3832 |
@s CENTER symbol |
| 3833 |
@s EXPAND symbol |
| 3834 |
@s leaders symbol |
| 3835 |
@s ltype symbol |
| 3836 |
@<symbols@>= |
| 3837 |
%token LEADERS "leaders" |
| 3838 |
%token ALIGN "align" |
| 3839 |
%token CENTER "center" |
| 3840 |
%token EXPAND "expand" |
| 3841 |
%type <info> leaders |
| 3842 |
%type <info> ltype |
| 3843 |
@ |
| 3844 |
|
| 3845 |
@<scanning rules@>= |
| 3846 |
::@=leaders@> :< return LEADERS; >: |
| 3847 |
::@=align@> :< return ALIGN; >: |
| 3848 |
::@=center@> :< return CENTER; >: |
| 3849 |
::@=expand@> :< return EXPAND; >: |
| 3850 |
@ |
| 3851 |
@<parsing rules@>= |
| 3852 |
ltype: {$$=1;} | ALIGN {$$=1;} @+| CENTER {$$=2;} @+| EXPAND {$$=3;}; |
| 3853 |
leaders: glue_node ltype rule_node {@+if ($1) $$=$2|b100;@+else $$=$2; @+} |
| 3854 |
| glue_node ltype hbox_node {@+if ($1) $$=$2|b100;@+else $$=$2;@+} |
| 3855 |
| glue_node ltype vbox_node {@+if ($1) $$=$2|b100;@+else $$=$2;@+}; |
| 3856 |
content_node: start LEADERS leaders END @| {@+ hput_tags($1, TAG(leaders_kind, $3));} |
| 3857 |
@ |
| 3858 |
|
| 3859 |
\writecode |
| 3860 |
@<write functions@>= |
| 3861 |
void hwrite_leaders_type(int t) |
| 3862 |
{@+ |
| 3863 |
if (t==2) hwritef(" center"); |
| 3864 |
else if (t==3) hwritef(" expand"); |
| 3865 |
} |
| 3866 |
@ |
| 3867 |
|
| 3868 |
\getcode |
| 3869 |
@<cases to get content@>= |
| 3870 |
@t\1\kern1em@> |
| 3871 |
case TAG(leaders_kind,1): @+ HGET_LEADERS(1); @+break; |
| 3872 |
case TAG(leaders_kind,2): @+ HGET_LEADERS(2); @+break; |
| 3873 |
case TAG(leaders_kind,3): @+ HGET_LEADERS(3); @+break; |
| 3874 |
case TAG(leaders_kind,b100|1): @+ HGET_LEADERS(b100|1); @+break; |
| 3875 |
case TAG(leaders_kind,b100|2): @+ HGET_LEADERS(b100|2); @+break; |
| 3876 |
case TAG(leaders_kind,b100|3): @+ HGET_LEADERS(b100|3); @+break; |
| 3877 |
@ |
| 3878 |
@<get macros@>= |
| 3879 |
#define @[HGET_LEADERS(I)@]@/ \ |
| 3880 |
if ((I)&b100) hget_glue_node();\ |
| 3881 |
hwrite_leaders_type((I)&b011);\ |
| 3882 |
if (KIND(*hpos)==rule_kind) hget_rule_node(); \ |
| 3883 |
else if (KIND(*hpos)==hbox_kind) hget_hbox_node(); \ |
| 3884 |
else hget_vbox_node(); |
| 3885 |
@ |
| 3886 |
|
| 3887 |
\subsection{Baseline Skips} |
| 3888 |
Baseline\index{baseline skip} skips are small amounts of glue inserted |
| 3889 |
between two consecutive lines of text. To get nice looking pages, the |
| 3890 |
amount of glue\index{glue} inserted must take into account the depth |
| 3891 |
of the line above the glue and the height of the line below the glue |
| 3892 |
to achieve a constant distance of the baselines. For example, if we |
| 3893 |
have the lines |
| 3894 |
\medskip |
| 3895 |
|
| 3896 |
\qquad\vbox{\hsize=0.5\hsize\noindent |
| 3897 |
``There is no\hfil\break |
| 3898 |
more gas\hfil\break |
| 3899 |
in the tank.'' |
| 3900 |
}\hss |
| 3901 |
|
| 3902 |
\medskip\noindent |
| 3903 |
\TeX\ will insert 7.69446pt of baseline skip between the first and the |
| 3904 |
second line and 3.11111pt of baseline skip between the second and the |
| 3905 |
third line. This is due to the fact that the first line has no |
| 3906 |
descenders, its depth is zero, the second line has no ascenders but |
| 3907 |
the ``g'' descends below the baseline, and the third line has |
| 3908 |
ascenders (``t'', ``h'',\dots) so it is higher than the second line. |
| 3909 |
\TeX's choice of baseline skips ensures that the baselines are exactly |
| 3910 |
12pt apart in both cases. |
| 3911 |
|
| 3912 |
Things get more complicated if the text contains mathematical formulas because then |
| 3913 |
a line can get so high or deep that it is impossible to keep the distance between |
| 3914 |
baselines constant without two adjacent lines touching each other. In such cases, |
| 3915 |
\TeX\ will insert a small minimum line skip glue\index{line skip glue}. |
| 3916 |
|
| 3917 |
For the whole computation, \TeX\ uses three parameters: {\tt base\-line\-skip}, |
| 3918 |
{\tt line\-skip\-limit},\index{line skip limit} and |
| 3919 |
{\tt lineskip}. {\tt baselineskip} is a glue value; its size is the |
| 3920 |
normal distance of two baselines. \TeX\ adjusts the size of the |
| 3921 |
{\tt baselineskip} glue for the height and the depth of the two lines and |
| 3922 |
then checks the result against {\tt lineskiplimit}. If the result is |
| 3923 |
smaller than {\tt lineskiplimit} it will use the {\tt lineskip} glue |
| 3924 |
instead. |
| 3925 |
|
| 3926 |
Because the depth and the height of lines depend on the outcome |
| 3927 |
of the line breaking\index{line breaking} |
| 3928 |
routine, baseline computations must be done in the viewer. |
| 3929 |
The situation gets even more complicated because \TeX\ can manipulate the insertion |
| 3930 |
of baseline skips in various ways. Therefore \HINT\ requires the insertion of |
| 3931 |
baseline nodes wherever the viewer is supposed to perform a baseline skip |
| 3932 |
computation. |
| 3933 |
|
| 3934 |
In the short format of a baseline definition, we store only |
| 3935 |
the nonzero components and use the |
| 3936 |
info bits to mark them: |b100| implies $|bs|\ne0$, |
| 3937 |
|b010| implies $|ls|\ne 0$, and |b001| implies $|lslimit|\ne 0$. |
| 3938 |
If the baseline has only zero components, we put a reference to baseline number 0 |
| 3939 |
in the output. |
| 3940 |
|
| 3941 |
@<hint basic types@>= |
| 3942 |
typedef struct {@+ |
| 3943 |
Glue bs, ls;@+ |
| 3944 |
Dimen lsl;@+ |
| 3945 |
} Baseline; |
| 3946 |
@ |
| 3947 |
|
| 3948 |
|
| 3949 |
|
| 3950 |
\readcode |
| 3951 |
@s BASELINE symbol |
| 3952 |
@s baseline symbol |
| 3953 |
@<symbols@>= |
| 3954 |
%token BASELINE "baseline" |
| 3955 |
%type <info> baseline |
| 3956 |
@ |
| 3957 |
@<scanning rules@>= |
| 3958 |
::@=baseline@> :< return BASELINE; >: |
| 3959 |
@ |
| 3960 |
|
| 3961 |
@<parsing rules@>= |
| 3962 |
baseline: dimension { if ($1!=0) HPUT32($1); } |
| 3963 |
glue_node glue_node @/{ $$=b000; if ($1!=0) $$|=b001; |
| 3964 |
if ($3) $$|=b100; |
| 3965 |
if ($4) $$|=b010; |
| 3966 |
@+}; |
| 3967 |
content_node: start BASELINE baseline END @/ |
| 3968 |
{ @+if ($3==b000) HPUT8(0); @+hput_tags($1,TAG(baseline_kind, $3)); }; |
| 3969 |
@ |
| 3970 |
|
| 3971 |
\getcode |
| 3972 |
@<cases to get content@>= |
| 3973 |
@t\1\kern1em@> |
| 3974 |
case TAG(baseline_kind,b001): { Baseline b;@+ HGET_BASELINE(b001,b);@+ }@+break; |
| 3975 |
case TAG(baseline_kind,b010): { Baseline b;@+ HGET_BASELINE(b010,b);@+ }@+break; |
| 3976 |
case TAG(baseline_kind,b011): { Baseline b;@+ HGET_BASELINE(b011,b);@+ }@+break; |
| 3977 |
case TAG(baseline_kind,b100): { Baseline b;@+ HGET_BASELINE(b100,b);@+ }@+break; |
| 3978 |
case TAG(baseline_kind,b101): { Baseline b;@+ HGET_BASELINE(b101,b);@+ }@+break; |
| 3979 |
case TAG(baseline_kind,b110): { Baseline b;@+ HGET_BASELINE(b110,b);@+ }@+break; |
| 3980 |
case TAG(baseline_kind,b111): { Baseline b;@+ HGET_BASELINE(b111,b);@+ }@+break; |
| 3981 |
@ |
| 3982 |
|
| 3983 |
@<get macros@>= |
| 3984 |
#define @[HGET_BASELINE(I,B)@] \ |
| 3985 |
if((I)&b001) HGET32((B).lsl); @+else B.lsl=0; hwrite_dimension(B.lsl);\ |
| 3986 |
if((I)&b100) hget_glue_node(); \ |
| 3987 |
else {B.bs.p.o=B.bs.m.o=B.bs.w.w=0; @+B.bs.w.h=B.bs.w.v=B.bs.p.f=B.bs.m.f=0.0; @+hwrite_glue_node(&(B.bs));@+}\ |
| 3988 |
if((I)&b010) hget_glue_node(); \ |
| 3989 |
else {B.ls.p.o=B.ls.m.o=B.ls.w.w=0; @+B.ls.w.h=B.ls.w.v=B.ls.p.f=B.ls.m.f=0.0; @+hwrite_glue_node(&(B.ls));@+} |
| 3990 |
@ |
| 3991 |
|
| 3992 |
|
| 3993 |
\putcode |
| 3994 |
@<put functions@>= |
| 3995 |
Tag hput_baseline(Baseline *b) |
| 3996 |
{ Info info=b000; |
| 3997 |
if (!ZERO_GLUE(b->bs)) @+info|=b100; |
| 3998 |
if (!ZERO_GLUE(b->ls)) @+ info|=b010; |
| 3999 |
if (b->lsl!=0) { @+ HPUT32(b->lsl); @+info|=b001; @+} |
| 4000 |
return TAG(baseline_kind,info); |
| 4001 |
} |
| 4002 |
@ |
| 4003 |
|
| 4004 |
|
| 4005 |
|
| 4006 |
\subsection{Ligatures} |
| 4007 |
Ligatures\index{ligature} occur only in horizontal lists. They specify characters |
| 4008 |
that combine the glyphs of several characters into one specialized |
| 4009 |
glyph. For example in the word ``{\it difficult\/}'' the three letters |
| 4010 |
``{\it f{}f{}i\/}'' are combined into the ligature ``{\it ffi\/}''. |
| 4011 |
Hence, a ligature is very similar to a simple glyph node; the |
| 4012 |
characters that got replaced are, however, retained in the ligature |
| 4013 |
because they might be needed for example to support searching. Since |
| 4014 |
ligatures are therefore only specialized list of characters and since |
| 4015 |
we have a very efficient way to store such lists of characters, namely |
| 4016 |
as a |text|, input and output of ligatures is quite simple. |
| 4017 |
|
| 4018 |
The info value zero is reserved for references to a ligature. If the |
| 4019 |
info value is between 1 and 6, it gives the number of bytes used to encode |
| 4020 |
the characters in UTF8. Note that a ligature will always include a |
| 4021 |
glyph byte, so the minimum size is 1. A typical ligature like ``{\it fi\/}'' |
| 4022 |
will need 3 byte: the ligature character ``{\it fi\/}'', and |
| 4023 |
the replacement characters ``f'' and ''i''. More byte might be |
| 4024 |
required if the character codes exceed |0x7F| since we use the UTF8 |
| 4025 |
encoding scheme for larger character codes. If the info value is 7, |
| 4026 |
a full text node follows the font byte. In the long |
| 4027 |
format, we give the font, the character code, and then the replacement |
| 4028 |
characters represented as a text. |
| 4029 |
|
| 4030 |
@<hint types@>= |
| 4031 |
typedef struct{@+uint8_t f; @+List l;@+} Lig; |
| 4032 |
@ |
| 4033 |
|
| 4034 |
\readcode |
| 4035 |
@s ref symbol |
| 4036 |
@s LIGATURE symbol |
| 4037 |
@s ligature symbol |
| 4038 |
@s cc_list symbol |
| 4039 |
@s lig_cc symbol |
| 4040 |
@<symbols@>= |
| 4041 |
%token LIGATURE "ligature" |
| 4042 |
%type <u> lig_cc |
| 4043 |
%type <lg> ligature |
| 4044 |
%type <u> ref |
| 4045 |
@ |
| 4046 |
@<scanning rules@>= |
| 4047 |
::@=ligature@> :< return LIGATURE; >: |
| 4048 |
@ |
| 4049 |
|
| 4050 |
@<parsing rules@>=@/ |
| 4051 |
cc_list:@+ | cc_list TXT_CC { hput_utf8($2); }; |
| 4052 |
lig_cc: UNSIGNED {RNG("UTF-8 code",$1,0,0x1FFFFF);$$=hpos-hstart; hput_utf8($1); }; |
| 4053 |
lig_cc: CHARCODE {$$=hpos-hstart; hput_utf8($1); }; |
| 4054 |
ref: REFERENCE { HPUT8($1); $$=$1; }; |
| 4055 |
ligature: ref { REF(font_kind,$1);} lig_cc TXT_START cc_list TXT_END @/ |
| 4056 |
{ $$.f=$1; $$.l.p=$3; $$.l.s=(hpos-hstart)-$3; |
| 4057 |
RNG("Ligature size",$$.l.s,0,255);}; |
| 4058 |
content_node: start LIGATURE ligature END {hput_tags($1,hput_ligature(&($3)));}; |
| 4059 |
@ |
| 4060 |
|
| 4061 |
\writecode |
| 4062 |
@<write functions@>= |
| 4063 |
void hwrite_ligature(Lig *l) |
| 4064 |
{ uint32_t pos=hpos-hstart; |
| 4065 |
hwrite_ref(l->f); |
| 4066 |
hpos=l->l.p+hstart; |
| 4067 |
hwrite_charcode(hget_utf8()); |
| 4068 |
hwritef(" \""); |
| 4069 |
while (hpos<hstart+l->l.p+l->l.s) |
| 4070 |
hwrite_txt_cc(hget_utf8()); |
| 4071 |
hwritec('"'); |
| 4072 |
hpos=hstart+pos; |
| 4073 |
} |
| 4074 |
@ |
| 4075 |
|
| 4076 |
\getcode |
| 4077 |
@<cases to get content@>= |
| 4078 |
@t\1\kern1em@> |
| 4079 |
case TAG(ligature_kind,1):@+ {Lig l; @+HGET_LIG(1,l);@+} @+break; |
| 4080 |
case TAG(ligature_kind,2):@+ {Lig l; @+HGET_LIG(2,l);@+} @+break; |
| 4081 |
case TAG(ligature_kind,3):@+ {Lig l; @+HGET_LIG(3,l);@+} @+break; |
| 4082 |
case TAG(ligature_kind,4):@+ {Lig l; @+HGET_LIG(4,l);@+} @+break; |
| 4083 |
case TAG(ligature_kind,5):@+ {Lig l; @+HGET_LIG(5,l);@+} @+break; |
| 4084 |
case TAG(ligature_kind,6):@+ {Lig l; @+HGET_LIG(6,l);@+} @+break; |
| 4085 |
case TAG(ligature_kind,7):@+ {Lig l; @+HGET_LIG(7,l);@+} @+break; |
| 4086 |
@ |
| 4087 |
@<get macros@>= |
| 4088 |
#define @[HGET_LIG(I,L)@] @/\ |
| 4089 |
(L).f=HGET8;REF(font_kind,(L).f);\ |
| 4090 |
if ((I)==7) hget_list(&((L).l)); \ |
| 4091 |
else { (L).l.s=(I); (L).l.p=hpos-hstart; @+ hpos+=(L).l.s;} \ |
| 4092 |
hwrite_ligature(&(L)); |
| 4093 |
@ |
| 4094 |
|
| 4095 |
\putcode |
| 4096 |
@<put functions@>= |
| 4097 |
Tag hput_ligature(Lig *l) |
| 4098 |
{ @+if (l->l.s < 7) return TAG(ligature_kind,l->l.s); |
| 4099 |
else@/ |
| 4100 |
{ uint32_t pos=l->l.p; |
| 4101 |
l->l.t=TAG(list_kind,b100); |
| 4102 |
hput_tags(pos,hput_list(pos+1, &(l->l))); |
| 4103 |
return TAG(ligature_kind,7); |
| 4104 |
} |
| 4105 |
} |
| 4106 |
@ |
| 4107 |
|
| 4108 |
|
| 4109 |
\subsection{Discretionary breaks}\label{discbreak}\index{discretionary break} |
| 4110 |
\HINT\ is capable to break lines into paragraphs. It does this |
| 4111 |
primarily at interword spaces but it might also break a line in the |
| 4112 |
middle of a word if it finds a discretionary\index{discretionary break} |
| 4113 |
line break there. These discretionary breaks are usually |
| 4114 |
provided by an automatic hyphenation algorithm but they might be also |
| 4115 |
explicitly\index{explicit} inserted by the author of a |
| 4116 |
document. |
| 4117 |
|
| 4118 |
When a line break occurs at such a discretionary break, the line |
| 4119 |
before the break ends with a |pre_break| list of nodes, the line after |
| 4120 |
the break starts with a |post_break| list of nodes, and the next |
| 4121 |
|replace_count| nodes after the discretionary break will be |
| 4122 |
ignored. Both lists must consist entirely of glyphs\index{glyph}, |
| 4123 |
kerns\index{kern}, boxes\index{box}, rules\index{rule}, or |
| 4124 |
ligatures\index{ligature}. For example, an ordinary discretionary |
| 4125 |
break will have a |pre_break| list containing ``-'', an empty |
| 4126 |
|post_break| list, and a |replace_count| of zero. |
| 4127 |
|
| 4128 |
The long format starts with an optional ``{\tt !}'', indicating an |
| 4129 |
explicit discretionary break, followed by the replace-count. |
| 4130 |
Then comes the pre-break list followed by the post-break list. |
| 4131 |
The replace-count can be omitted if it is zero; |
| 4132 |
an empty post-break list may be omitted as well. |
| 4133 |
Both list may be omitted only if both are empty. |
| 4134 |
|
| 4135 |
In the short format, the three components of a disc node are stored |
| 4136 |
in this order: |replace_count|, |pre_break| list, and |post_break| list. |
| 4137 |
The |b100| bit in the info value indicates the presence of a replace-count, |
| 4138 |
the |b010| bit the presence of a |pre_break| list, |
| 4139 |
and the |b001| bit the presence of a |post_break| list. |
| 4140 |
Since the info value |b000| is reserved for references, at least one |
| 4141 |
of these must be specified; so we represent a node with empty lists |
| 4142 |
and a replace\index{replace count} count of zero using the info value |
| 4143 |
|b100| and a zero byte for the replace count. |
| 4144 |
|
| 4145 |
Replace counts must be in the range 0 to 31; so the short format can |
| 4146 |
set the high bit of the replace count to indicate an explicit\index{explicit} break. |
| 4147 |
|
| 4148 |
@<hint types@>= |
| 4149 |
typedef struct@+ {@+ bool x; @+List p,q;@+ uint8_t r;@+ } Disc; |
| 4150 |
@ |
| 4151 |
|
| 4152 |
|
| 4153 |
\readcode |
| 4154 |
@s DISC symbol |
| 4155 |
@s disc symbol |
| 4156 |
@s disc_node symbol |
| 4157 |
@s replace_count symbol |
| 4158 |
|
| 4159 |
@<symbols@>= |
| 4160 |
%token DISC "disc" |
| 4161 |
%type <dc> disc |
| 4162 |
%type <u> replace_count |
| 4163 |
@ |
| 4164 |
@<scanning rules@>= |
| 4165 |
::@=disc@> :< return DISC; >: |
| 4166 |
@ |
| 4167 |
|
| 4168 |
@<parsing rules@>=@/ |
| 4169 |
replace_count: explicit {@+ if ($1) {$$=0x80; HPUT8(0x80);@+}@+ else $$=0x00;@+} |
| 4170 |
| explicit UNSIGNED { RNG("Replace count",$2,0,31); |
| 4171 |
$$=($2)|(($1)?0x80:0x00); @+ if ($$!=0) HPUT8($$);@+}; |
| 4172 |
disc: replace_count list list { $$.r=$1;$$.p=$2; $$.q=$3; |
| 4173 |
if ($3.s==0) { hpos=hpos-3;@+ if ($2.s==0) hpos=hpos-3; @+}@+} |
| 4174 |
| replace_count list { $$.r=$1;$$.p=$2; if ($2.s==0) hpos=hpos-3;@+ $$.q.s=0; } |
| 4175 |
| replace_count { $$.r=$1;$$.p.s=0; $$.q.s=0; }; |
| 4176 |
|
| 4177 |
|
| 4178 |
disc_node: start DISC disc END |
| 4179 |
{hput_tags($1,hput_disc(&($3)));}; |
| 4180 |
|
| 4181 |
content_node: disc_node; |
| 4182 |
@ |
| 4183 |
|
| 4184 |
\writecode |
| 4185 |
@<write functions@>= |
| 4186 |
void hwrite_disc(Disc *h) |
| 4187 |
{ @+hwrite_explicit(h->x); |
| 4188 |
if (h->r!=0) hwritef(" %d",h->r); |
| 4189 |
if (h->p.s!=0 || h->q.s!=0) hwrite_list(&(h->p)); |
| 4190 |
if (h->q.s!=0) hwrite_list(&(h->q)); |
| 4191 |
} |
| 4192 |
void hwrite_disc_node(Disc *h) |
| 4193 |
{ @+ hwrite_start(); @+hwritef("disc"); @+ hwrite_disc(h); @+hwrite_end();} |
| 4194 |
@ |
| 4195 |
|
| 4196 |
\getcode |
| 4197 |
@<cases to get content@>= |
| 4198 |
@t\1\kern1em@> |
| 4199 |
case TAG(disc_kind,b001): {Disc h; @+HGET_DISC(b001,h);@+ hwrite_disc(&h); @+} @+break; |
| 4200 |
case TAG(disc_kind,b010): {Disc h; @+HGET_DISC(b010,h);@+ hwrite_disc(&h); @+} @+break; |
| 4201 |
case TAG(disc_kind,b011): {Disc h; @+HGET_DISC(b011,h);@+ hwrite_disc(&h); @+} @+break; |
| 4202 |
case TAG(disc_kind,b100): {Disc h; @+HGET_DISC(b100,h);@+ hwrite_disc(&h); @+} @+break; |
| 4203 |
case TAG(disc_kind,b101): {Disc h; @+HGET_DISC(b101,h);@+ hwrite_disc(&h); @+} @+break; |
| 4204 |
case TAG(disc_kind,b110): {Disc h; @+HGET_DISC(b110,h);@+ hwrite_disc(&h); @+} @+break; |
| 4205 |
case TAG(disc_kind,b111): {Disc h; @+HGET_DISC(b111,h);@+ hwrite_disc(&h); @+} @+break; |
| 4206 |
@ |
| 4207 |
|
| 4208 |
@<get macros@>= |
| 4209 |
#define @[HGET_DISC(I,Y)@]\ |
| 4210 |
if ((I)&b100) {uint8_t r=HGET8; (Y).r=r&0x7F; @+ RNG("Replace count",(Y).r,0,31); @+(Y).x=(r&0x80)!=0; @+}\ |
| 4211 |
@+else { (Y).r=0; @+ (Y).x=false;@+}\ |
| 4212 |
if ((I)&b010) hget_list(&((Y).p)); else { (Y).p.p=hpos-hstart; @+(Y).p.s=0; @+(Y).p.t=TAG(list_kind,b000); @+}\ |
| 4213 |
if ((I)&b001) hget_list(&((Y).q)); else { (Y).q.p=hpos-hstart; @+(Y).q.s=0; @+(Y).q.t=TAG(list_kind,b000); @+} |
| 4214 |
@ |
| 4215 |
|
| 4216 |
@<get functions@>= |
| 4217 |
void hget_disc_node(Disc *h) |
| 4218 |
{ @<read the start byte |a|@>@; |
| 4219 |
if (KIND(a)!=disc_kind || INFO(a)==b000) |
| 4220 |
QUIT("Hyphen expected at 0x%x got %s,%d",node_pos,NAME(a),INFO(a)); |
| 4221 |
HGET_DISC(INFO(a),*h); |
| 4222 |
@<read and check the end byte |z|@>@; |
| 4223 |
} |
| 4224 |
@ |
| 4225 |
|
| 4226 |
When |hput_disc| is called, the node is already written to the output, |
| 4227 |
but empty lists might have been deleted, and the info value needs to be determined. |
| 4228 |
Because the info value |b000| is reserved for references, a zero reference |
| 4229 |
count is written to avoid this case. |
| 4230 |
\putcode |
| 4231 |
@<put functions@>= |
| 4232 |
Tag hput_disc(Disc *h) |
| 4233 |
{ Info info=b000; |
| 4234 |
if (h->r!=0) info|=b100; |
| 4235 |
if (h->q.s!=0) info|=b011; |
| 4236 |
else if (h->p.s!=0) info|=b010; |
| 4237 |
if (info==b000) { @+info|=b100; @+HPUT8(0);@+} |
| 4238 |
return TAG(disc_kind,info); |
| 4239 |
} |
| 4240 |
@ |
| 4241 |
\subsection{Paragraphs} |
| 4242 |
The most important procedure that the \HINT\ viewer inherits from \TeX\ is the |
| 4243 |
line breaking routine. If the horizontal size of the paragraph is not known, |
| 4244 |
breaking the paragraph\index{paragraph} into lines must be postponed and this is done by creating |
| 4245 |
a paragraph node. The paragraph node must contain all information that \TeX's |
| 4246 |
line breaking\index{line breaking} algorithm needs to do its job. |
| 4247 |
|
| 4248 |
Besides the horizontal list describing the content of the paragraph and |
| 4249 |
the extended dimension describing the horizontal size, |
| 4250 |
this is the set of parameters that guide the line breaking algorithm: |
| 4251 |
|
| 4252 |
\itemize |
| 4253 |
\item |
| 4254 |
Integer parameters:\hfill\break |
| 4255 |
{\tt pretolerance} (badness tolerance before hyphenation),\hfill\break |
| 4256 |
{\tt tolerance} (badness tolerance after hyphenation),\hfill\break |
| 4257 |
{\tt line\_penalty} (added to the badness of every line, increase to get fewer lines),\hfill\break |
| 4258 |
{\tt hy\-phen\_pe\-nal\-ty} (penalty for break after hyphenation break),\hfill\break |
| 4259 |
{\tt ex\_hy\-phen\_pe\-nal\-ty} (penalty for break after explicit\index{explicit} break),\hfill\break |
| 4260 |
{\tt doub\-le\_hy\-phen\_de\-merits} (demerits for double hyphen break),\hfill\break |
| 4261 |
{\tt final\_hyphen\_de\-me\-rits} (demerits for final hyphen break),\hfill\break |
| 4262 |
{\tt adj\_demerits} (demerits for adjacent incompatible lines),\hfill\break |
| 4263 |
{\tt looseness} (make the paragraph that many lines longer than its optimal size),\hfill\break |
| 4264 |
{\tt inter\_line\_penalty} (additional penalty between lines),\hfill\break |
| 4265 |
{\tt club\_pe\-nal\-ty} (penalty for creating a club line),\hfill\break |
| 4266 |
{\tt widow\_penalty} (penalty for creating a widow line),\hfill\break |
| 4267 |
{\tt display\_widow\_penalty} (ditto, just before a display),\hfill\break |
| 4268 |
{\tt bro\-ken\_pe\-nal\-ty} (penalty for breaking a page at a broken line),\hfill\break |
| 4269 |
{\tt hang\_af\-ter} (start/end hanging indentation at this line). |
| 4270 |
\item |
| 4271 |
Dimension parameters:\hfill\break |
| 4272 |
{\tt line\_skip\_limit} (threshold for {\tt line\_skip} instead of {\tt base\-line\_skip}),\hfill\break |
| 4273 |
{\tt hang\_in\-dent} (amount of hanging indentation),\hfill\break |
| 4274 |
{\tt emergency\_stretch} (stretchability added to every line in the final pass of line breaking). |
| 4275 |
\item |
| 4276 |
Glue parameters:\hfill\break |
| 4277 |
{\tt baseline\_skip} (desired glue between baselines),\hfill\break |
| 4278 |
{\tt line\_skip} (interline glue if {\tt baseline\_skip} is infeasible),\hfill\break |
| 4279 |
{\tt left\_skip} (glue at left of justified lines),\hfill\break |
| 4280 |
{\tt right\_skip} (glue at right of justified lines),\hfill\break |
| 4281 |
{\tt par\_fill\_skip} (glue on last line of paragraph). |
| 4282 |
\enditemize |
| 4283 |
|
| 4284 |
|
| 4285 |
For a detailed explanation of these parameters and how they influence |
| 4286 |
line breaking, you should consult the {\TeX}book\cite{DK:texbook}; |
| 4287 |
\TeX's {\tt parshape} feature is currently not implemented. There are |
| 4288 |
default values for all of these parameters (see section~\secref{defaults}), |
| 4289 |
and therefore it might not be necessary to specify any of them. |
| 4290 |
Any local adjustments are contained in a list of |
| 4291 |
parameters contained in the paragraph node. |
| 4292 |
|
| 4293 |
A further complication arises from displayed\index{displayed formula} formulas |
| 4294 |
that interrupt a paragraph. Such displays are described in the next |
| 4295 |
section. |
| 4296 |
|
| 4297 |
To summarize, a paragraph node in the long format specifies an |
| 4298 |
extended dimension, a parameter list, |
| 4299 |
and a node list. The extended dimension is given either as an |
| 4300 |
|xdimen| node (info bit |b100|) or as a reference; similarly the parameter list |
| 4301 |
can be embedded in the node (info bit |b010|) or again it is given by a reference. |
| 4302 |
|
| 4303 |
|
| 4304 |
\readcode |
| 4305 |
@s PAR symbol |
| 4306 |
@s par symbol |
| 4307 |
@s xdimen_ref symbol |
| 4308 |
@s param_ref symbol |
| 4309 |
@s par_dimen symbol |
| 4310 |
|
| 4311 |
|
| 4312 |
@<symbols@>= |
| 4313 |
%token PAR "par" |
| 4314 |
%type <info> par |
| 4315 |
@ |
| 4316 |
|
| 4317 |
@<scanning rules@>= |
| 4318 |
::@=par@> :< return PAR; >: |
| 4319 |
@ |
| 4320 |
|
| 4321 |
|
| 4322 |
The following parsing rules are slightly more complicated than I would |
| 4323 |
like them to be, but it seems more important to achieve a regular |
| 4324 |
layout of the short format nodes where all sub nodes are located at |
| 4325 |
the end of a node. In this case, I want to put a |param_ref| before |
| 4326 |
an |xdimen| node, but otherwise have the |xdimen_ref| before a |
| 4327 |
|param_list|. The |par_dimen| rule is introduced only to avoid a |
| 4328 |
reduce/reduce conflict in the parser. |
| 4329 |
|
| 4330 |
@<parsing rules@>= |
| 4331 |
par_dimen: xdimen { hput_xdimen_node(&($1)); }; |
| 4332 |
par: xdimen_ref param_ref list {$$=b000;} |
| 4333 |
| xdimen_ref param_list list { $$=b010;} |
| 4334 |
| xdimen param_ref { hput_xdimen_node(&($1)); } list { $$=b100;} |
| 4335 |
| par_dimen param_list list { $$=b110;}; |
| 4336 |
|
| 4337 |
content_node: start PAR par END { hput_tags($1,TAG(par_kind,$3));}; |
| 4338 |
@ |
| 4339 |
|
| 4340 |
\getcode |
| 4341 |
@<cases to get content@>= |
| 4342 |
@t\1\kern1em@> |
| 4343 |
case TAG(par_kind,b000): @+HGET_PAR(b000);@+break; |
| 4344 |
case TAG(par_kind,b010): @+HGET_PAR(b010);@+break; |
| 4345 |
case TAG(par_kind,b100): @+HGET_PAR(b100);@+break; |
| 4346 |
case TAG(par_kind,b110): @+HGET_PAR(b110);@+break; |
| 4347 |
@ |
| 4348 |
|
| 4349 |
@<get macros@>= |
| 4350 |
#define @[HGET_PAR(I)@] @/\ |
| 4351 |
{ uint8_t n;\ |
| 4352 |
if ((I)==b100) {n=HGET8; @+REF(param_kind,n);@+}\ |
| 4353 |
if ((I)&b100) {Xdimen x; @+hget_xdimen_node(&x); @+hwrite_xdimen(&x);@+} else HGET_REF(xdimen_kind);\ |
| 4354 |
if ((I)&b010) { List l; @+hget_param_list(&l); @+hwrite_param_list(&l); @+} \ |
| 4355 |
else if ((I)!=b100) HGET_REF(param_kind)@; else hwrite_ref(n);\ |
| 4356 |
{ List l; @+hget_list(&l);@+ hwrite_list(&l); @+}} |
| 4357 |
@ |
| 4358 |
|
| 4359 |
|
| 4360 |
\subsection{Mathematics}\index{Mathematics}\index{displayed formula} |
| 4361 |
\gdef\subcodetitle{Displayed Math} |
| 4362 |
Being able to handle mathematics\index{mathematics} nicely is one |
| 4363 |
of the primary features of \TeX\ and |
| 4364 |
so you should expect the same from \HINT. |
| 4365 |
We start here with the more complex case---displayed equations---and finish with the |
| 4366 |
simpler case of mathematical formulas that are part of the normal flow of text. |
| 4367 |
|
| 4368 |
Displayed equations occur inside a paragraph\index{paragraph} |
| 4369 |
node. They interrupt normal processing of the paragraph and the |
| 4370 |
paragraph processing is resumed after the display. Positioning of the |
| 4371 |
display depends on several parameters, the shape of the paragraph, and |
| 4372 |
the length of the last line preceding the display. Displayed formulas |
| 4373 |
often feature an equation number which can be placed either left or |
| 4374 |
right of the formula. Also the size of the equation number will |
| 4375 |
influence the placement of the formula. |
| 4376 |
|
| 4377 |
In a \HINT\ file, the parameter list is followed by a list of content |
| 4378 |
nodes, representing the formula, and an optional horizontal box |
| 4379 |
containing the equation number. |
| 4380 |
|
| 4381 |
In the short format, we use the info bit |b100| to indicate the |
| 4382 |
presence of a parameter list (which might be empty---so it's actually the absence of a |
| 4383 |
reference to a parameter list); the info bit |b010| to indicate the presence of |
| 4384 |
a left equation number; and the info bit |b001| for a right |
| 4385 |
equation\index{equation number} number. |
| 4386 |
|
| 4387 |
In the long format, we use ``{\tt eqno}'' or ``{\tt left eqno}'' to indicate presence and |
| 4388 |
placement of the equation number. |
| 4389 |
|
| 4390 |
\readcode |
| 4391 |
@s MATH symbol |
| 4392 |
@s math symbol |
| 4393 |
@<symbols@>= |
| 4394 |
%token MATH "math" |
| 4395 |
%type <info> math |
| 4396 |
@ |
| 4397 |
|
| 4398 |
@<scanning rules@>= |
| 4399 |
::@=math@> :< return MATH; >: |
| 4400 |
@ |
| 4401 |
|
| 4402 |
@<parsing rules@>= |
| 4403 |
math: param_ref list {$$=b000;} |
| 4404 |
| param_ref list hbox_node {$$=b001;} |
| 4405 |
| param_ref hbox_node list {$$=b010;} |
| 4406 |
| param_list list {$$=b100;} |
| 4407 |
| param_list list hbox_node {$$=b101;} |
| 4408 |
| param_list hbox_node list {$$=b110;}; |
| 4409 |
|
| 4410 |
content_node: start MATH math END @/{ hput_tags($1,TAG(math_kind,$3));}; |
| 4411 |
@ |
| 4412 |
|
| 4413 |
\getcode |
| 4414 |
@<cases to get content@>= |
| 4415 |
@t\1\kern1em@> |
| 4416 |
case TAG(math_kind,b000): HGET_MATH(b000); @+ break; |
| 4417 |
case TAG(math_kind,b001): HGET_MATH(b001); @+ break; |
| 4418 |
case TAG(math_kind,b010): HGET_MATH(b010); @+ break; |
| 4419 |
case TAG(math_kind,b100): HGET_MATH(b100); @+ break; |
| 4420 |
case TAG(math_kind,b101): HGET_MATH(b101); @+ break; |
| 4421 |
case TAG(math_kind,b110): HGET_MATH(b110); @+ break; |
| 4422 |
@ |
| 4423 |
|
| 4424 |
@<get macros@>= |
| 4425 |
#define @[HGET_MATH(I)@] \ |
| 4426 |
if ((I)&b100) { List l; @+hget_param_list(&l); @+hwrite_param_list(&l); @+} \ |
| 4427 |
else HGET_REF(param_kind);\ |
| 4428 |
if ((I)&b010) hget_hbox_node(); \ |
| 4429 |
{ List l; @+hget_list(&l);@+ hwrite_list(&l); @+} \ |
| 4430 |
if ((I)&b001) hget_hbox_node(); |
| 4431 |
@ |
| 4432 |
|
| 4433 |
\gdef\subcodetitle{Text Math} |
| 4434 |
Things are much simpler if mathematical formulas are embedded in regular text. |
| 4435 |
Here it is just necessary to mark the beginning and the end of the formula |
| 4436 |
because glue inside a formula is not a possible point for a line break. |
| 4437 |
To break the line within a formula you can insert a penalty node. |
| 4438 |
|
| 4439 |
In the long format, such a simple math node just consists of the keyword ``on'' |
| 4440 |
or ``off''. In the short format, there are two info values still unassigned: |
| 4441 |
we use |b011| for ``off'' and |b111| for ``on''. |
| 4442 |
|
| 4443 |
|
| 4444 |
\readcode |
| 4445 |
@s ON symbol |
| 4446 |
@s OFF symbol |
| 4447 |
@s on_off symbol |
| 4448 |
@<symbols@>= |
| 4449 |
%token ON "on" |
| 4450 |
%token OFF "off" |
| 4451 |
%type <i> on_off |
| 4452 |
@ |
| 4453 |
|
| 4454 |
@<scanning rules@>= |
| 4455 |
::@=on@> :< return ON; >: |
| 4456 |
::@=off@> :< return OFF; >: |
| 4457 |
@ |
| 4458 |
|
| 4459 |
@<parsing rules@>= |
| 4460 |
on_off: ON {$$=1;} | OFF {$$=0;}; |
| 4461 |
math: on_off { $$=b011|($1<<2); }; |
| 4462 |
@ |
| 4463 |
|
| 4464 |
\getcode |
| 4465 |
@<cases to get content@>= |
| 4466 |
@t\1\kern1em@> |
| 4467 |
case TAG(math_kind,b111): hwritef(" on");@+break; |
| 4468 |
case TAG(math_kind,b011): hwritef(" off");@+break; |
| 4469 |
@ |
| 4470 |
|
| 4471 |
Note that \TeX\ allows math nodes to specify a width using the current value of |
| 4472 |
mathsurround. If this width is nonzero, it is equivalent to inserting a |
| 4473 |
kern node before the math on node and after the math off node. |
| 4474 |
|
| 4475 |
\subsection{Adjustments}\label{adjust} |
| 4476 |
An adjustment\index{adjustment} occurs only in paragraphs\index{paragraph}. |
| 4477 |
When the line breaking\index{line breaking} routine finds an adjustment, it inserts |
| 4478 |
the vertical material contained in the adjustment node right after the current line. |
| 4479 |
Adjustments simply contain a list node. |
| 4480 |
|
| 4481 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 4482 |
@s ADJUST symbol |
| 4483 |
@<symbols@>= |
| 4484 |
%token ADJUST "adjust" |
| 4485 |
@ |
| 4486 |
|
| 4487 |
@<scanning rules@>= |
| 4488 |
::@=adjust@> :< return ADJUST; >: |
| 4489 |
@ |
| 4490 |
|
| 4491 |
@<parsing rules@>= |
| 4492 |
content_node: start ADJUST list END { hput_tags($1,TAG(adjust_kind,1));}; |
| 4493 |
@ |
| 4494 |
|
| 4495 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 4496 |
@<cases to get content@>= |
| 4497 |
@t\1\kern1em@> |
| 4498 |
case TAG(adjust_kind,1):@+ { List l;@+hget_list(&l); @+ hwrite_list(&l); @+} @+ break; |
| 4499 |
@ |
| 4500 |
|
| 4501 |
\subsection{Tables}\index{alignment} |
| 4502 |
As long as a table contains no dependencies on \.{hsize} and \.{vsize}, |
| 4503 |
Hi\TeX\ can expand an alignment into a set of nested horizontal and |
| 4504 |
vertical boxes and no special processing is required. |
| 4505 |
As long as only the size of the table itself but neither the tabskip |
| 4506 |
glues nor the table content depends on \.{hsize} or \.{vsize}, the table |
| 4507 |
just needs an outer node of type |hset_kind| or |vset_kind|. If there |
| 4508 |
is non aligned material inside the table that depends on \.{hsize} or |
| 4509 |
\.{vsize}, a vpack or hpack node is still sufficient. |
| 4510 |
|
| 4511 |
While it is reasonable to restrict the tabskip glues to be ordinary |
| 4512 |
glue values without \.{hsize} or \.{vsize} dependencies, it might be |
| 4513 |
desirable to have content in the table that does depend on \.{hsize} or |
| 4514 |
\.{vsize}. For the latter case, we need a special kind of table |
| 4515 |
node. Here is why: |
| 4516 |
|
| 4517 |
As soon as the dimension of an item in the table is an extended |
| 4518 |
dimension, it is no longer possible to compute the maximum natural with |
| 4519 |
of a column, because it is not possible to compare extended dimensions |
| 4520 |
without knowing \.{hsize} and \.{vsize}. Hence the computation of maximum |
| 4521 |
widths needs to be done in the viewer. After knowing the width of the columns, |
| 4522 |
the setting of tabskip glues is easy to compute. |
| 4523 |
|
| 4524 |
To implement these extended tables, we will need a table node that |
| 4525 |
specifies a direction, either horizontal or vertical; a list of |
| 4526 |
tabskip glues, with the provision that the last tabskip glue in the |
| 4527 |
list is repeated as long as necessary; and a list of table content. |
| 4528 |
The table's content is stacked, either vertical or |
| 4529 |
horizontal, orthogonal to the alignment direction of the table. |
| 4530 |
The table's content consists of nonaligned content, for example extra glue |
| 4531 |
or rules, and aligned content. |
| 4532 |
Each element of aligned content |
| 4533 |
is called an outer item and it consist of a list of inner items. |
| 4534 |
For example in a horizontal alignment, each row is an outer item |
| 4535 |
and each table entry in that row is an inner item. |
| 4536 |
An inner item contains a box node (of kind |hbox_kind|, |vbox_kind|, |
| 4537 |
|hset_kind|, |vset_kind|, |hpack_kind|, or |vpack_kind|) followed by |
| 4538 |
an optional span count. |
| 4539 |
|
| 4540 |
The glue of the boxes in the inner items will be reset so that all boxes in the same |
| 4541 |
column reach the same maximum column with. The span counts will be replaced by |
| 4542 |
the appropriate amount of empty boxes and tabskip glues. Finally the |
| 4543 |
glue in the outer item will be set to obtain the desired size |
| 4544 |
of the table. |
| 4545 |
|
| 4546 |
The definitions below specify just a |list| for the list of tabskip glues and a |
| 4547 |
list for the outer table items. |
| 4548 |
This is just for convenience; the first list must contain glue |
| 4549 |
nodes and the second list must contain nonaligned content and inner item nodes. |
| 4550 |
|
| 4551 |
We reuse the |H| and |V| tokens, defined as part of the specification |
| 4552 |
of extended dimensions, to indicate the alignment direction of the |
| 4553 |
table. To tell a reference to an extended dimension from a reference |
| 4554 |
to an ordinary dimension, we prefix the former with an |XDIMEN| token; |
| 4555 |
for the latter, the |DIMEN| token is optional. The scanner will |
| 4556 |
recognize not only ``item'' as an |ITEM| token but also ``row'' and |
| 4557 |
''column''. This allows a more readable notation, for example by |
| 4558 |
marking the outer items as rows and the inner items as columns. |
| 4559 |
|
| 4560 |
In the short format, the |b010| bit is used to mark a vertical table |
| 4561 |
and the |b101| bits indicate how the table size is specified; an outer |
| 4562 |
item node has the info value |b000|, an inner item node with info |
| 4563 |
value |b111| contains an extra byte for the span count, otherwise the |
| 4564 |
info value is equal to the span count. |
| 4565 |
|
| 4566 |
|
| 4567 |
|
| 4568 |
|
| 4569 |
|
| 4570 |
|
| 4571 |
\readcode |
| 4572 |
@s TABLE symbol |
| 4573 |
@s ITEM symbol |
| 4574 |
@s table symbol |
| 4575 |
@s span_count symbol |
| 4576 |
|
| 4577 |
@<symbols@>= |
| 4578 |
%token TABLE "table" |
| 4579 |
%token ITEM "item" |
| 4580 |
%type <info> table span_count |
| 4581 |
@ |
| 4582 |
|
| 4583 |
@<scanning rules@>= |
| 4584 |
::@=table@> :< return TABLE; >: |
| 4585 |
::@=item@> :< return ITEM; >: |
| 4586 |
::@=row@> :< return ITEM; >: |
| 4587 |
::@=column@> :< return ITEM; >: |
| 4588 |
@ |
| 4589 |
|
| 4590 |
@<parsing rules@>= |
| 4591 |
span_count: UNSIGNED { $$=hput_span_count($1); }; |
| 4592 |
content_node: start ITEM content_node END { hput_tags($1,TAG(item_kind,1)); }; |
| 4593 |
content_node: start ITEM span_count content_node END {@+ hput_tags($1,TAG(item_kind,$3));}; |
| 4594 |
content_node: start ITEM list END { hput_tags($1,TAG(item_kind,b000));}; |
| 4595 |
|
| 4596 |
table: H box_goal list list {$$=$2;}; |
| 4597 |
table: V box_goal list list {$$=$2|b010;}; |
| 4598 |
|
| 4599 |
content_node: start TABLE table END { hput_tags($1,TAG(table_kind,$3));}; |
| 4600 |
@ |
| 4601 |
|
| 4602 |
\getcode |
| 4603 |
@<cases to get content@>= |
| 4604 |
@t\1\kern1em@> |
| 4605 |
case TAG(table_kind,b000): @+ HGET_TABLE(b000); @+ break; |
| 4606 |
case TAG(table_kind,b001): @+ HGET_TABLE(b001); @+ break; |
| 4607 |
case TAG(table_kind,b010): @+ HGET_TABLE(b010); @+ break; |
| 4608 |
case TAG(table_kind,b011): @+ HGET_TABLE(b011); @+ break; |
| 4609 |
case TAG(table_kind,b100): @+ HGET_TABLE(b100); @+ break; |
| 4610 |
case TAG(table_kind,b101): @+ HGET_TABLE(b101); @+ break; |
| 4611 |
case TAG(table_kind,b110): @+ HGET_TABLE(b110); @+ break; |
| 4612 |
case TAG(table_kind,b111): @+ HGET_TABLE(b111); @+ break;@# |
| 4613 |
|
| 4614 |
case TAG(item_kind,b000): @+{@+ List l;@+ hget_list(&l);@+ hwrite_list(&l);@+ } @+ break; |
| 4615 |
case TAG(item_kind,b001): hget_content_node(); @+ break; |
| 4616 |
case TAG(item_kind,b010): hwritef(" 2");@+hget_content_node(); @+ break; |
| 4617 |
case TAG(item_kind,b011): hwritef(" 3");@+hget_content_node(); @+ break; |
| 4618 |
case TAG(item_kind,b100): hwritef(" 4");@+hget_content_node(); @+ break; |
| 4619 |
case TAG(item_kind,b101): hwritef(" 5");@+hget_content_node(); @+ break; |
| 4620 |
case TAG(item_kind,b110): hwritef(" 6");@+hget_content_node(); @+ break; |
| 4621 |
case TAG(item_kind,b111): hwritef(" %u",HGET8);@+hget_content_node(); @+ break; |
| 4622 |
@ |
| 4623 |
|
| 4624 |
@<get macros@>= |
| 4625 |
#define @[HGET_TABLE(I)@] \ |
| 4626 |
if(I&b010) hwritef(" v"); @+else hwritef(" h"); \ |
| 4627 |
if ((I)&b001) hwritef(" add");@+ else hwritef(" to");\ |
| 4628 |
if ((I)&b100) {Xdimen x; hget_xdimen_node(&x); @+hwrite_xdimen_node(&x);@+} else HGET_REF(xdimen_kind)@;\ |
| 4629 |
{@+ List l; @+hget_list(&l);@+ hwrite_list(&l);@+ } /* tabskip */ \ |
| 4630 |
{@+ List l; @+hget_list(&l);@+ hwrite_list(&l);@+ } /* items */ |
| 4631 |
@ |
| 4632 |
|
| 4633 |
|
| 4634 |
\putcode |
| 4635 |
@<put functions@>= |
| 4636 |
Info hput_span_count(uint32_t n) |
| 4637 |
{ if (n==0) QUIT("Span count in item must not be zero"); |
| 4638 |
else if (n<7) return n; |
| 4639 |
else if (n>0xFF) QUIT("Span count %d must be less than 255",n); |
| 4640 |
else |
| 4641 |
{ HPUT8(n); return 7; } |
| 4642 |
} |
| 4643 |
@ |
| 4644 |
\section{Extensions}\hascode |
| 4645 |
|
| 4646 |
\subsection{Images} |
| 4647 |
In the first implementation attempt, images behaved pretty much |
| 4648 |
like glue\index{glue}. They could stretch (or shrink) together with |
| 4649 |
the surrounding glue to fill a horizontal or vertical box. While I |
| 4650 |
thought this would be in line with \TeX's concepts, it proved to be a |
| 4651 |
bad decission because images, as opposed to glue, would stretch or |
| 4652 |
shrink horizontally {\it and} vertically at the same time. |
| 4653 |
This would require a two pass algorithm to pack boxes: first to |
| 4654 |
determine the glue setting and a second pass to determine the proper |
| 4655 |
image dimensions. Otherwise incorrect width or height values would |
| 4656 |
propagate all the way through a sequence of nested boxes. Even worse |
| 4657 |
so, this two pass algorithm would be needed in the viewer if images |
| 4658 |
were contained in boxes that had extended dimensions. |
| 4659 |
|
| 4660 |
The new design described below allows images with extended dimensions. |
| 4661 |
This covers the case of stretchable or shrinkable images inside of |
| 4662 |
extended boxes. The given extended dimensions are considered maximum |
| 4663 |
values. The stretching or shrinking of images will always preserve the |
| 4664 |
$\hbox{aspect ratio}=\hbox{width}/\hbox{height}$. |
| 4665 |
|
| 4666 |
For convenience, we allow missing values in the long format, for |
| 4667 |
example the aspect ratio, if they can be determined from the image |
| 4668 |
data. In the short format, the necessary information for a correct |
| 4669 |
layout must be available without using the image data. |
| 4670 |
|
| 4671 |
In the long format, the only required parts of an image node are: the |
| 4672 |
number of the auxiliary section where the image data can be found and |
| 4673 |
the descriptive text which is there to make the document more |
| 4674 |
accessible. The section number is followed by the optional aspect |
| 4675 |
ratio, width, and height of the image. If some of these values are |
| 4676 |
missing, it must be possible to determine them from the image |
| 4677 |
data. The node ends with the description. |
| 4678 |
|
| 4679 |
The short format, starts with the section number of the image data and |
| 4680 |
ends with the description. Missing values for aspect ratio, width, and |
| 4681 |
height are only allowed if they can be recomputed from the image data. |
| 4682 |
A missing width or height is represented by a reference to the zero |
| 4683 |
extended dimension. If the |b100| bit is set, the aspect ratio is |
| 4684 |
present as a 32 bit floating point value followed by extended |
| 4685 |
dimensions for width and height. The info value |b100| indicates a |
| 4686 |
width reference followed by a height reference; the value |b111| |
| 4687 |
indicates a width node followed by a height node; the value |b110| |
| 4688 |
indicates a height reference followed by a width node; and the value |
| 4689 |
|b101| indicates a width reference followed by a height node. The |
| 4690 |
last two rules reflect the requirement that subnodes are always |
| 4691 |
located at the end of a node. |
| 4692 |
|
| 4693 |
The remaining info values are used as follows: |
| 4694 |
The value |b000| is used for a reference to an image. |
| 4695 |
The value |b011| indicates an immediate width and an immediate height. |
| 4696 |
The value |b010| indicates an aspect ratio and an immediate width. |
| 4697 |
The value |b001| indicates an aspect ratio and an immediate height. |
| 4698 |
|
| 4699 |
The following data type stores image information. The width and height |
| 4700 |
are either given as extended dimensions either directly in |w| and |h| |
| 4701 |
or as references in |wr| and |hr|. |
| 4702 |
|
| 4703 |
@<hint types@>= |
| 4704 |
typedef struct {@+ |
| 4705 |
uint16_t n;@+ |
| 4706 |
float32_t a;@+ |
| 4707 |
Xdimen w,h;@+ |
| 4708 |
uint8_t wr,hr;@+ |
| 4709 |
} Image; |
| 4710 |
@ |
| 4711 |
|
| 4712 |
|
| 4713 |
\readcode |
| 4714 |
@s IMAGE symbol |
| 4715 |
@s image symbol |
| 4716 |
@s image_aspect symbol |
| 4717 |
@s image_aspect symbol |
| 4718 |
@s image_width symbol |
| 4719 |
@s image_height symbol |
| 4720 |
@s image_spec symbol |
| 4721 |
@<symbols@>= |
| 4722 |
%token IMAGE "image" |
| 4723 |
%token WIDTH "width" |
| 4724 |
%token HEIGHT "height" |
| 4725 |
%type <xd> image_width image_height |
| 4726 |
%type <f> image_aspect |
| 4727 |
%type <info> image_spec image |
| 4728 |
@ |
| 4729 |
|
| 4730 |
@<scanning rules@>= |
| 4731 |
::@=image@> :< return IMAGE; >: |
| 4732 |
::@=width@> :< return WIDTH; >: |
| 4733 |
::@=height@> :< return HEIGHT; >: |
| 4734 |
@ |
| 4735 |
|
| 4736 |
@<parsing rules@>= |
| 4737 |
image_aspect: number {$$=$1;} | {$$=0.0;}; |
| 4738 |
image_width: WIDTH xdimen { $$=$2;} |
| 4739 |
| { $$=xdimen_defaults[zero_xdimen_no];}; |
| 4740 |
image_height: HEIGHT xdimen { $$=$2; } |
| 4741 |
| { $$=xdimen_defaults[zero_xdimen_no];}; |
| 4742 |
|
| 4743 |
image_spec: UNSIGNED image_aspect image_width image_height |
| 4744 |
{$$=hput_image_spec($1,$2,0,&($3),0,&($4));} |
| 4745 |
| UNSIGNED image_aspect WIDTH REFERENCE image_height |
| 4746 |
{$$=hput_image_spec($1,$2,$4,NULL,0,&($5));} |
| 4747 |
| UNSIGNED image_aspect image_width HEIGHT REFERENCE |
| 4748 |
{$$=hput_image_spec($1,$2,0,&($3),$5,NULL);} |
| 4749 |
| UNSIGNED image_aspect WIDTH REFERENCE HEIGHT REFERENCE |
| 4750 |
{$$=hput_image_spec($1,$2,$4,NULL,$6,NULL);}; |
| 4751 |
|
| 4752 |
image: image_spec list {$$=$1;}; |
| 4753 |
|
| 4754 |
content_node: start IMAGE image END { hput_tags($1,TAG(image_kind,$3));}; |
| 4755 |
@ |
| 4756 |
|
| 4757 |
When a short format file is generated, the image width and height must be |
| 4758 |
determined if necessary from the image file. |
| 4759 |
The following function will write this information into the long format file. |
| 4760 |
Editing the image file at a later time and converting the short format |
| 4761 |
file back to a long format file will preserve the old information. |
| 4762 |
This is not allways a desirable effect. It would be possible to |
| 4763 |
eliminate information about the image size when writing the long format |
| 4764 |
if that information can be derived from the image file. The latter |
| 4765 |
solution might have the disadvantage, that infomation about a |
| 4766 |
desired image size might get lost when editing an image file. |
| 4767 |
|
| 4768 |
\writecode |
| 4769 |
@<write functions@>= |
| 4770 |
void hwrite_image(Image *x) |
| 4771 |
{ RNG("Section number",x->n,3,max_section_no); hwritef(" %u",x->n); |
| 4772 |
if (x->a!=0.0) hwrite_float64(x->a, false); |
| 4773 |
if (x->wr!=0) hwritef(" width *%u",x->wr); |
| 4774 |
else if (x->w.w!=0 ||x->w.h!=0.0 || x->w.v!=0.0) |
| 4775 |
{ hwritef(" width"); hwrite_xdimen(&x->w); } |
| 4776 |
if (x->hr!=0) hwritef(" height *%u",x->hr); |
| 4777 |
else if (x->h.w!=0 || x->h.h!=0.0 || x->h.v!=0.0) |
| 4778 |
{ hwritef(" height"); hwrite_xdimen(&x->h); } |
| 4779 |
} |
| 4780 |
@ |
| 4781 |
|
| 4782 |
\getcode |
| 4783 |
@<cases to get content@>= |
| 4784 |
@t\1\kern1em@> |
| 4785 |
case TAG(image_kind,b001): @+ HGET_IMAGE(b001);@+break; |
| 4786 |
case TAG(image_kind,b010): @+ HGET_IMAGE(b010);@+break; |
| 4787 |
case TAG(image_kind,b011): @+ HGET_IMAGE(b011);@+break; |
| 4788 |
case TAG(image_kind,b100): @+ HGET_IMAGE(b100);@+break; |
| 4789 |
case TAG(image_kind,b101): @+ HGET_IMAGE(b101);@+break; |
| 4790 |
case TAG(image_kind,b110): @+ HGET_IMAGE(b110);@+break; |
| 4791 |
case TAG(image_kind,b111): @+ HGET_IMAGE(b111);@+break; |
| 4792 |
@ |
| 4793 |
|
| 4794 |
@<get macros@>= |
| 4795 |
#define @[HGET_IMAGE(I)@] @/\ |
| 4796 |
{ Image x={0};\ |
| 4797 |
HGET16(x.n);\ |
| 4798 |
if ((I)&b100) { x.a=hget_float32();\ |
| 4799 |
if ((I)==b111) {hget_xdimen_node(&x.w);hget_xdimen_node(&x.h);}\ |
| 4800 |
else if ((I)==b110) {x.hr=HGET8;hget_xdimen_node(&x.w);}\ |
| 4801 |
else if ((I)==b101) {x.wr=HGET8;hget_xdimen_node(&x.h);}\ |
| 4802 |
else {x.wr=HGET8;x.hr=HGET8;}}\ |
| 4803 |
else if((I)==b011) {HGET32(x.w.w);HGET32(x.h.w);} \ |
| 4804 |
else if((I)==b010) { x.a=hget_float32(); HGET32(x.w.w);}\ |
| 4805 |
else if((I)==b001){ x.a=hget_float32(); HGET32(x.h.w);}\ |
| 4806 |
hwrite_image(&x);\ |
| 4807 |
{List d; hget_list(&d);hwrite_list(&d);}}@/ |
| 4808 |
@ |
| 4809 |
|
| 4810 |
\putcode |
| 4811 |
@<put functions@>= |
| 4812 |
@<image functions@>@; |
| 4813 |
Info hput_image_spec(uint32_t n, float32_t a, |
| 4814 |
uint32_t wr, Xdimen *w, uint32_t hr, Xdimen *h) |
| 4815 |
{ HPUT16(n); |
| 4816 |
if (w!=NULL && h!=NULL) |
| 4817 |
{ if (w->h==0.0 && w->v==0.0 && h->h==0.0 && h->v==0.0) |
| 4818 |
return hput_image_dimens(n,a,w->w,h->w); |
| 4819 |
else |
| 4820 |
{ hput_image_aspect(n,a); |
| 4821 |
hput_xdimen_node(w);hput_xdimen_node(h); |
| 4822 |
return b111; |
| 4823 |
} |
| 4824 |
} |
| 4825 |
else if (w!=NULL && h==NULL) |
| 4826 |
{ if (w->h==0.0 && w->v==0.0 && hr==zero_xdimen_no) |
| 4827 |
return hput_image_dimens(n,a,w->w,0); |
| 4828 |
else |
| 4829 |
{ hput_image_aspect(n,a); |
| 4830 |
HPUT8(hr);hput_xdimen_node(w); |
| 4831 |
return b110; |
| 4832 |
} |
| 4833 |
} |
| 4834 |
else if (w==NULL && h!=NULL) |
| 4835 |
{ if (wr==zero_xdimen_no && h->h==0.0 && h->v==0.0) |
| 4836 |
return hput_image_dimens(n,a,0,h->w); |
| 4837 |
else |
| 4838 |
{ hput_image_aspect(n,a); |
| 4839 |
HPUT8(wr);hput_xdimen_node(h); |
| 4840 |
return b101; |
| 4841 |
} |
| 4842 |
} |
| 4843 |
else |
| 4844 |
{ if (wr==zero_xdimen_no && hr==zero_xdimen_no) |
| 4845 |
return hput_image_dimens(n,a,0,0); |
| 4846 |
else |
| 4847 |
{ hput_image_aspect(n,a); |
| 4848 |
HPUT8(wr);HPUT8(hr); |
| 4849 |
return b100; |
| 4850 |
} |
| 4851 |
} |
| 4852 |
} |
| 4853 |
@ |
| 4854 |
|
| 4855 |
|
| 4856 |
If extended dimensions are involved, |
| 4857 |
the long format might very well specify different values than |
| 4858 |
stored in the image. In this case the given dimensions are |
| 4859 |
interpreted as maximum dimensions. If the aspect ratio is missing, |
| 4860 |
we use |hextract_image_dimens| to extract it from the image file. |
| 4861 |
|
| 4862 |
@<image functions@>= |
| 4863 |
static void hput_image_aspect(int n,double a) |
| 4864 |
{ |
| 4865 |
if (a==0.0) {Dimen w,h; hextract_image_dimens(n,&a,&w,&h);} |
| 4866 |
if (a!=0.0) hput_float32(a); |
| 4867 |
else QUIT("Unable to determine aspect ratio of image %s",dir[n].file_name); |
| 4868 |
} |
| 4869 |
@ |
| 4870 |
|
| 4871 |
If no extended dimensions are involved in an image specification, |
| 4872 |
we use |hput_image_dimen|. |
| 4873 |
Because the long format can omit part of the image specification, |
| 4874 |
we use |hextract_image_dimens| to extract information from |
| 4875 |
the image file and merge this information |
| 4876 |
with the data supplied in the long format. |
| 4877 |
|
| 4878 |
@<image functions@>= |
| 4879 |
@<auxiliar image functions@>@; |
| 4880 |
static Info hput_image_dimens(int n,float32_t a, Dimen w, Dimen h) |
| 4881 |
{ Dimen iw,ih; |
| 4882 |
double ia; |
| 4883 |
if (w>0 && h>0) |
| 4884 |
{ HPUT32(w); HPUT32(h); return b011; } |
| 4885 |
else if (a>0 && w>0) |
| 4886 |
{ hput_float32((float32_t)a); HPUT32(w); return b010; } |
| 4887 |
else if (a>0 && h>0) |
| 4888 |
{ hput_float32((float32_t)a); HPUT32(h); return b001; } |
| 4889 |
hextract_image_dimens(n,&ia,&iw,&ih); |
| 4890 |
@<merge stored image dimensions with dimensions given@>@; |
| 4891 |
if (iw>0) |
| 4892 |
{ hput_float32((float32_t)ia); HPUT32(iw); return b010; } |
| 4893 |
else if (ih>0) |
| 4894 |
{ hput_float32((float32_t)ia); HPUT32(ih); return b001; } |
| 4895 |
else |
| 4896 |
{ iw=-iw; ih=-h; /*we accept the default resolution*/ |
| 4897 |
HPUT32(iw); HPUT32(ih); return b011; |
| 4898 |
} |
| 4899 |
} |
| 4900 |
@ |
| 4901 |
|
| 4902 |
|
| 4903 |
If the width, height or aspect ratio is stored in the |
| 4904 |
image file, we can merge this information with the information given in |
| 4905 |
the long format. |
| 4906 |
It is considered an error, if the function |hextract_image_dimens| |
| 4907 |
can not extract the aspect ratio. Absolute width and height values, |
| 4908 |
however, might be missing. If the aspect ratio is computed from the |
| 4909 |
number of horizontal and vertical pixels, |hextract_image_dimens| |
| 4910 |
makes the reasonable assumption that the intended resolution |
| 4911 |
is 72dpi and converts the image dimensions to scaled points. |
| 4912 |
It negates these values to indicate |
| 4913 |
that the resolution is just a guess. This allows other programs |
| 4914 |
to used different default resolutions if desired. |
| 4915 |
|
| 4916 |
@<merge stored image dimensions with dimensions given@>= |
| 4917 |
{ if (ia==0.0) |
| 4918 |
{ if (a!=0.0) ia=a; |
| 4919 |
else if(w!=0 && h!=0) ia=(double)w/(double)h; |
| 4920 |
else QUIT("Unable to determine aspect ratio of image %s",dir[n].file_name); |
| 4921 |
} |
| 4922 |
/* here the aspect ratio |ia| is known */ |
| 4923 |
if (w==0 && h==0) /*neither width nor height specified*/ |
| 4924 |
{ if (ih>0) iw=round(ih * ia); |
| 4925 |
else if (iw>0) ih=round(iw/ia); |
| 4926 |
} |
| 4927 |
else if (h==0) /*width specified*/ |
| 4928 |
{ iw=w;@+ ih=round(w/ia);@+ } |
| 4929 |
else if (w==0) /*height specified*/ |
| 4930 |
{ ih=h;@+ iw=round(h*ia);@+} |
| 4931 |
else /* both specified */ |
| 4932 |
{ ih = h;@+ |
| 4933 |
iw = w;@+ |
| 4934 |
} |
| 4935 |
} |
| 4936 |
@ |
| 4937 |
|
| 4938 |
Before we present the code to extract image dimensions from |
| 4939 |
various types of image files, we define a few macros and |
| 4940 |
variables for the reading these image files. |
| 4941 |
|
| 4942 |
@<auxiliar image functions@>= |
| 4943 |
#define IMG_BUF_MAX 54 |
| 4944 |
#define IMG_HEAD_MAX 2 |
| 4945 |
static unsigned char img_buf[IMG_BUF_MAX]; |
| 4946 |
static size_t img_buf_size; |
| 4947 |
#define @[LittleEndian32(X)@] (img_buf[(X)]+(img_buf[(X)+1]<<8)+\ |
| 4948 |
(img_buf[(X)+2]<<16)+(img_buf[(X)+3]<<24)) |
| 4949 |
|
| 4950 |
#define @[BigEndian16(X)@] (img_buf[(X)+1]+(img_buf[(X)]<<8)) |
| 4951 |
|
| 4952 |
#define @[BigEndian32(X)@] (img_buf[(X)+3]+(img_buf[(X)+2]<<8)+\ |
| 4953 |
(img_buf[(X)+1]<<16)+(img_buf[(X)]<<24)) |
| 4954 |
|
| 4955 |
#define Match2(X,A,B) ((img_buf[(X)]==(A)) && (img_buf[(X)+1]==(B))) |
| 4956 |
#define Match4(X,A,B,C,D) (Match2(X,A,B)&&Match2((X)+2,C,D)) |
| 4957 |
|
| 4958 |
#define @[GET_IMG_BUF(X)@] \ |
| 4959 |
if (img_buf_size<X) \ |
| 4960 |
{ size_t i=fread(img_buf+img_buf_size,1,(X)-img_buf_size,f); \ |
| 4961 |
if (i<0) QUIT("Unable to read image %s",fn); \ |
| 4962 |
else if (i==0) QUIT("Unable to read image header %s",fn); \ |
| 4963 |
else img_buf_size+=i; \ |
| 4964 |
} |
| 4965 |
@ |
| 4966 |
|
| 4967 |
Considering the different image formats, we start with Windows |
| 4968 |
Bitmaps. A Windows bitmap file usually has the extension {\tt .bmp} |
| 4969 |
but the better way to check for a Windows bitmap file ist to examine |
| 4970 |
the first two byte of the file: the ASCII codes for `B' and `M'. Once |
| 4971 |
we have verified the file type, we find the width and height of the |
| 4972 |
bitmap in pixels at offsets |0x12| and |0x16| stored as little-endian |
| 4973 |
32 bit integers. At offsets |0x26| and |0x2A|, we find the horizontal |
| 4974 |
and vertical resolution in pixel per meter stored in the same format. |
| 4975 |
This is sufficient to compute the true width and height of the image |
| 4976 |
in scaled points. |
| 4977 |
|
| 4978 |
The Windows Bitmap format is easy to process but not very |
| 4979 |
efficient. So the support for this format in the \HINT\ format is |
| 4980 |
deprecated and will disappear. You should use one of the formats |
| 4981 |
described next. |
| 4982 |
|
| 4983 |
@<auxiliar image functions@>= |
| 4984 |
static bool get_BMP_info(FILE *f, char *fn, double *a, Dimen *w, Dimen *h) |
| 4985 |
{ double wpx,hpx; |
| 4986 |
double xppm,yppm; |
| 4987 |
GET_IMG_BUF(2); |
| 4988 |
if (!Match2(0,'B','M')) return false; |
| 4989 |
GET_IMG_BUF(0x2E); |
| 4990 |
wpx=(double)LittleEndian32(0x12); /*width in pixel*/ |
| 4991 |
hpx=(double)LittleEndian32(0x16); /*height in pixel*/ |
| 4992 |
xppm=(double)LittleEndian32(0x26); /* horizontal pixel per meter*/ |
| 4993 |
yppm=(double)LittleEndian32(0x2A); /* vertical pixel per meter*/ |
| 4994 |
*w= floor(0.5+ONE*(72.00*1000.0/25.4)*wpx/xppm); |
| 4995 |
*h= floor(0.5+ONE*(72.00*1000.0/25.4)*hpx/yppm); |
| 4996 |
*a = (wpx/xppm)/(hpx/yppm); |
| 4997 |
return true; |
| 4998 |
} |
| 4999 |
@ |
| 5000 |
|
| 5001 |
Now we repeat this process for image files using the Portable Network |
| 5002 |
Graphics file format. This file format is well suited to simple |
| 5003 |
graphics that do not use color gradients. These images usually have |
| 5004 |
the extension {\tt .png} and start with an eight byte signature: |
| 5005 |
|0x89| followed by the ASCII Codes `P', `N', `G', followd by a |
| 5006 |
carriage return (|0x0D| and line feed (|0x0A|), an DOS end-of-file |
| 5007 |
character (|0x1A|) and final line feed (|0x0A|). After the signature |
| 5008 |
follows a list of chunks. The first chunk is the image header chunk. |
| 5009 |
Each chunk starts with the size of the chunk stored as big-endian 32 |
| 5010 |
bit integer, followed by the chunk name stored as four ASCII codes |
| 5011 |
followed by the chunk data and a CRC. The size, as stored in the |
| 5012 |
chunk, does not include the size itself, nor the name, and neither the |
| 5013 |
CRC. The first chunk is the IHDR chunk. The chunk data of the IHDR |
| 5014 |
chunk starts with the width and the height of the image in pixels |
| 5015 |
stored as 32 bit big-endian integers. |
| 5016 |
|
| 5017 |
Finding the image resolution takes some more effort. The image |
| 5018 |
resolution is stored in an optional chunk named ``pHYs'' for the |
| 5019 |
physical pixel dimensions. All we know is that this chunk, if it |
| 5020 |
exists, will appear after the IHDR chunk and before the (required) |
| 5021 |
IDAT chunk. The pHYs chunk contains two 32 bit big-endian integers, |
| 5022 |
giving the horizontal and vertical pixels per unit, and a one byte |
| 5023 |
unit specifier, which is either 0 for an undefined unit or 1 for the |
| 5024 |
meter as unit. With an undefined unit, only the aspect ratio of the |
| 5025 |
pixels and hence the aspect ratio of the image can be determined. |
| 5026 |
It is not uncommon, however, that the resolution in such a case is given |
| 5027 |
as dots per inch. So we decide to assume the latter. |
| 5028 |
|
| 5029 |
If there is resolution can not be determined, we assume a resolution |
| 5030 |
of 72dpi and negate width and height to inform the calling procedure |
| 5031 |
of this arbitrary choice. |
| 5032 |
|
| 5033 |
|
| 5034 |
@<auxiliar image functions@>= |
| 5035 |
static bool get_PNG_info(FILE *f, char *fn, double *a, Dimen *w, Dimen *h) |
| 5036 |
{ int pos, size; |
| 5037 |
double wpx,hpx; /* width and height in pixel */ |
| 5038 |
double xppu,yppu; /* pixel per unit in x and y direction */ |
| 5039 |
int unit; |
| 5040 |
GET_IMG_BUF(24); |
| 5041 |
if (!Match4(0, 0x89, 'P', 'N', 'G') || |
| 5042 |
!Match4(4, 0x0D, 0x0A, 0x1A, 0x0A)) return false; |
| 5043 |
size=BigEndian32(8); |
| 5044 |
if (!Match4(12,'I', 'H', 'D', 'R')) return false; |
| 5045 |
wpx=(double)BigEndian32(16); |
| 5046 |
hpx=(double)BigEndian32(20); |
| 5047 |
pos=20+size; |
| 5048 |
while (true) |
| 5049 |
{ if (fseek(f,pos,SEEK_SET)!=0) return false; |
| 5050 |
img_buf_size=0; |
| 5051 |
GET_IMG_BUF(17); |
| 5052 |
size=BigEndian32(0); |
| 5053 |
if (Match4(4,'p', 'H', 'Y', 's')) /*must occur before IDAT chunk*/ |
| 5054 |
{ xppu =(double)BigEndian32(8); |
| 5055 |
yppu =(double)BigEndian32(12); |
| 5056 |
unit=img_buf[16]; |
| 5057 |
if (unit==0) /* assuming unit is inch */ |
| 5058 |
{ *w=floor(0.5+ONE*72.27*wpx/xppu); |
| 5059 |
*h=floor(0.5+ONE*72.27*hpx/yppu); |
| 5060 |
*a =(wpx/xppu)/(hpx/yppu); |
| 5061 |
return true; |
| 5062 |
} |
| 5063 |
else if (unit==1) /* unit is meter */ |
| 5064 |
{ |
| 5065 |
*w=floor(0.5+ONE*(72.27/0.0254)*wpx/xppu); |
| 5066 |
*h=floor(0.5+ONE*(72.27/0.0254)*hpx/yppu); |
| 5067 |
*a = (wpx/xppu)/(hpx/yppu); |
| 5068 |
return true; |
| 5069 |
} |
| 5070 |
else |
| 5071 |
break; |
| 5072 |
} |
| 5073 |
else if (Match4(4,'I', 'D', 'A', 'T')) |
| 5074 |
break; |
| 5075 |
else |
| 5076 |
pos=pos+12+size; |
| 5077 |
} |
| 5078 |
/*we assume 72dpi and negate the results*/ |
| 5079 |
*w=-floor(0.5+ONE*72.27*wpx/72.0); |
| 5080 |
*h=-floor(0.5+ONE*72.27*hpx/72.0); |
| 5081 |
*a =wpx/hpx; |
| 5082 |
return true; |
| 5083 |
} |
| 5084 |
@ |
| 5085 |
|
| 5086 |
For photographs, the JPEG File Interchange Format (JFIF) is more |
| 5087 |
appropriate. JPEG files come with all sorts of file extensions like |
| 5088 |
{\tt .jpg}, {\tt .jpeg}, or {\tt .jfif}. We check the file siganture: |
| 5089 |
it starts with the the SOI (Start of Image) marker |0xFF|, |0xD8|. |
| 5090 |
|
| 5091 |
Most likely it will be followed by the JIFI-Tag. |
| 5092 |
The JIFI-Tag starts with the segment marker |
| 5093 |
APP0 (|0xFF|, |0xE0|) followed by the 2 byte segment size, followed by |
| 5094 |
the ASCII codes `J', `F', `I', `F' followed by a zero byte. Next is a |
| 5095 |
two byte version number which we do not read. Before the resolution |
| 5096 |
proper there is a resolution unit indicator byte (0 = no units, 1 = |
| 5097 |
dots per inch, 2 = dots per cm) and then comes the horizontal and |
| 5098 |
vertical resolution both as 16 Bit big-endian integers. |
| 5099 |
|
| 5100 |
Instead of the JIFI-Tag, there might as well be a Exif-Tag |
| 5101 |
which starts with the segment marker |
| 5102 |
APP1 (|0xFF|, |0xE1|) followed by the 2 byte segment size. |
| 5103 |
Currently this tag is not decoded. |
| 5104 |
|
| 5105 |
To find the actual width and height, |
| 5106 |
we have to search for a start of frame marker |
| 5107 |
(|0xFF|, |0xC0|+$n$ with $0\le n\le 15$). Which is followed by the 2 |
| 5108 |
byte segment size, the 1 byte sample precission, the 2 byte height and |
| 5109 |
the 2 byte width. |
| 5110 |
|
| 5111 |
If the resolution was given explicitely in the JIFI-Tag, |
| 5112 |
we use it. If there was no such tag or the uint was undefined, |
| 5113 |
we proceed as we did for the PNG file. |
| 5114 |
|
| 5115 |
|
| 5116 |
@<auxiliar image functions@>= |
| 5117 |
static bool get_JPG_info(FILE *f, char *fn, double *a, Dimen *w, Dimen *h) |
| 5118 |
{ int pos, size; |
| 5119 |
double wpx,hpx; |
| 5120 |
double xppu=72.0,yppu=72.0; |
| 5121 |
int unit; |
| 5122 |
GET_IMG_BUF(18); |
| 5123 |
|
| 5124 |
if (!Match2(0, 0xFF,0xD8)) /* SOI Start of Image */ |
| 5125 |
return false; |
| 5126 |
pos=2; |
| 5127 |
while (true) |
| 5128 |
{ if (fseek(f,pos,SEEK_SET)!=0) |
| 5129 |
return false; |
| 5130 |
img_buf_size=0; |
| 5131 |
GET_IMG_BUF(16); |
| 5132 |
if (img_buf[0] != 0xFF) return false; /* Not the start of a segment */ |
| 5133 |
if ( img_buf[1] == 0xE0 && |
| 5134 |
Match4(4,'J', 'F', 'I', 'F')) /* APP0 JFIF Tag */ |
| 5135 |
{ unit=img_buf[11]; |
| 5136 |
xppu=(double)BigEndian16(12); |
| 5137 |
yppu=(double)BigEndian16(14); |
| 5138 |
if (unit==1) |
| 5139 |
; /* allready in dpi */ |
| 5140 |
else if (unit==2) |
| 5141 |
{ xppu=xppu*2.54; /* convert dot per cm to dpi */ |
| 5142 |
yppu=yppu*2.54; |
| 5143 |
} |
| 5144 |
else |
| 5145 |
{ yppu=72.0*yppu/xppu; /* assume 72dpi */ |
| 5146 |
xppu=72.0; |
| 5147 |
} |
| 5148 |
} |
| 5149 |
else if (img_buf[1] == 0xC0 || img_buf[1] == 0xC2) /* SOF Start of Frame */ |
| 5150 |
{ hpx =(double)BigEndian16(5); |
| 5151 |
wpx =(double)BigEndian16(7); |
| 5152 |
*w = floor(0.5+ONE*72.27*wpx/xppu); |
| 5153 |
*h = floor(0.5+ONE*72.27*hpx/yppu); |
| 5154 |
*a = (wpx/xppu)/(hpx/yppu); |
| 5155 |
return true; |
| 5156 |
} |
| 5157 |
else if (img_buf[1] == 0xD9) /* EOI End of Image */ |
| 5158 |
return false; |
| 5159 |
size= BigEndian16(2); |
| 5160 |
pos=pos+2+size; |
| 5161 |
} |
| 5162 |
return false; |
| 5163 |
} |
| 5164 |
@ |
| 5165 |
|
| 5166 |
There is still one image format missing: scalable vector graphics. |
| 5167 |
In the moment, I tend not to include a further image format into |
| 5168 |
the definition of the \HINT\ file format but instead use the |
| 5169 |
PostScript subset that is used for Type 1 fonts to encode |
| 5170 |
vector graphics. Any \HINT\ viewer must support Type 1 |
| 5171 |
PostScript fonts and hence it has already the necessary interpreter. |
| 5172 |
So it seems reasonable to put the burden of converting vector graphics |
| 5173 |
into a Type 1 PostScript font on the generator of \HINT\ files |
| 5174 |
and keep the \HINT\ viewer as small and simple as possible. |
| 5175 |
An alternative which would impose only a slight burden on the \HINT\ file |
| 5176 |
viewer is the use of the rsvg library. |
| 5177 |
|
| 5178 |
After having considered the various types of image files, |
| 5179 |
we now determine width, height and aspect ratio based on |
| 5180 |
such an image file. |
| 5181 |
|
| 5182 |
We combine all the above functions into the |hextract_image_dimens| |
| 5183 |
function. |
| 5184 |
|
| 5185 |
@<image functions@>= |
| 5186 |
void hextract_image_dimens(int n, double *a, Dimen *w, Dimen *h) |
| 5187 |
{ char *fn; |
| 5188 |
FILE *f; |
| 5189 |
*a=0.0; |
| 5190 |
*w=*h=0; |
| 5191 |
fn=dir[n].file_name; |
| 5192 |
f=fopen(fn,"rb"); |
| 5193 |
if (f!=NULL) |
| 5194 |
{ img_buf_size=0; |
| 5195 |
if (!get_BMP_info(f,fn,a,w,h) && |
| 5196 |
!get_PNG_info(f,fn,a,w,h) && |
| 5197 |
!get_JPG_info(f,fn,a,w,h)) |
| 5198 |
DBG(DBGDEF,"Unknown image type %s",fn); |
| 5199 |
fclose(f); |
| 5200 |
DBG(DBGDEF,"image %d: width= %fpt height= %fpt aspect=%f\n", |
| 5201 |
n,*w/(double)ONE,*h/(double)ONE,*a); |
| 5202 |
} |
| 5203 |
} |
| 5204 |
@ |
| 5205 |
|
| 5206 |
\subsection{Positions, Outlines, Links, and Labels}\label{labels} |
| 5207 |
\index{position}\index{outline}\index{link}\index{label} |
| 5208 |
A viewer can usually not display the entire content section of |
| 5209 |
a \HINT\ file. Instead it will display a page of content and will give |
| 5210 |
its user various means to change the page. This might be as simple as |
| 5211 |
a ``page down'' or ``page up'' button (or gesture) and as |
| 5212 |
sophisticated as searching using regular expressions. More |
| 5213 |
traditional ways to navigate the content include the use of a table of |
| 5214 |
content or an index of keywords. All these methods of changing a page |
| 5215 |
have in common that a part of the content that fits nicely in the |
| 5216 |
screen area provided by the output device must be rendered given a |
| 5217 |
position inside the content section. |
| 5218 |
|
| 5219 |
|
| 5220 |
Let's assume that the viewer uses a \HINT\ file in short |
| 5221 |
format---after all that's the format designed for precisely this use. |
| 5222 |
A position inside the content section is then the position of the |
| 5223 |
starting byte of a node. Such a position can be stored as a 32 bit |
| 5224 |
number. Because even the smallest node contains two tag bytes, |
| 5225 |
the position of any node is strictly smaller than the maximum 32 bit |
| 5226 |
number which we can conveniently use as a ``non position''. |
| 5227 |
|
| 5228 |
@<hint macros@>= |
| 5229 |
#define HINT_NO_POS 0xFFFFFFFF |
| 5230 |
@ |
| 5231 |
|
| 5232 |
To render a page starting at a given position is not difficult: |
| 5233 |
We just read content nodes, starting at the given position and feed |
| 5234 |
them to \TeX's page builder until the page is complete. To implement a |
| 5235 |
``clickable'' table of content this is good enough. We store with |
| 5236 |
every entry in the table of content the position of the section |
| 5237 |
header, and when the user clicks the entry, the viewer can display a |
| 5238 |
new page starting exactly with that section header. |
| 5239 |
|
| 5240 |
Things are slightly more complex if we want to implement a ``page |
| 5241 |
down'' button. If we press this button, we want the next page to |
| 5242 |
start exactly where the current page has ended. This is |
| 5243 |
typically in the middle of a paragraph node, and it might even be in |
| 5244 |
the middle of an hyphenated word in that paragraph. Fortunately, |
| 5245 |
paragraph and table nodes are the only nodes that can be broken across page |
| 5246 |
boundaries. But broken paragraph nodes are a common case non the less, |
| 5247 |
and unless we want to search for the enclosing node, we need to |
| 5248 |
augment in this case the primary 32 bit position inside the content |
| 5249 |
section with a secondary position. Most of the |
| 5250 |
time, 16 bit will suffice for this secondary position if we give it |
| 5251 |
relative to the primary position. Further, if the list of nodes forming the |
| 5252 |
paragraph is given as a text, we need to know the current font at the |
| 5253 |
secondary position. Of course, the viewer can find it by scanning the |
| 5254 |
initial part of the text, but when we think of a page down button, the |
| 5255 |
viewer might already know it from rendering the previous page. |
| 5256 |
|
| 5257 |
Similar is the case of a ``page up'' button. Only here we need a page |
| 5258 |
that ends precisely where our current page starts. Possibly even with |
| 5259 |
the initial part of a hyphenated word. Here we need a reverse version |
| 5260 |
of \TeX's page builder that assembles a ``good'' page from the bottom |
| 5261 |
up instead of from the top down. Sure the viewer can cache the start |
| 5262 |
position of the previous page (or the rendering of the entire page) if |
| 5263 |
the reader has reached the current page using the page down |
| 5264 |
button. But this is not possible in all cases. The reader might have |
| 5265 |
reached the current page using the table of content or even an index |
| 5266 |
or a search form. |
| 5267 |
|
| 5268 |
This is the most complex case to consider: a link from an index or a |
| 5269 |
search form to the position of a keyword in the main text. Let's assume |
| 5270 |
someone looks up the word ``M\"unchen''. Should the viewer then |
| 5271 |
generate a page that starts in the middle of a sentence with the word |
| 5272 |
``M\"unchen''? Probably not! We want a page that shows at least the whole sentence if |
| 5273 |
not the whole paragraph. Of course the program that generates the |
| 5274 |
link could specify the position of the start of the paragraph instead |
| 5275 |
of the position of the word. But that will not solve the problem. Just |
| 5276 |
imagine reading the groundbreaking masterpiece of a German philosopher |
| 5277 |
on a small hand-held device: the paragraph will most likely be very |
| 5278 |
long and perhaps only part of the first sentence will fit on the small |
| 5279 |
screen. So the desired keyword might not be found on the page that |
| 5280 |
starts with the beginning of the paragraph; it might not even be on |
| 5281 |
the next or next to next page. Only the viewer can decide what is the |
| 5282 |
best fragment of content to display around the position of the given |
| 5283 |
keyword. |
| 5284 |
|
| 5285 |
To summarize, we need three different ways to render a page for a given position: |
| 5286 |
\itemize |
| 5287 |
\item A page that starts exactly at the given position. |
| 5288 |
\item A page that ends exactly at the given position. |
| 5289 |
\item The ``best'' page that contains the given position somewhere in the middle. |
| 5290 |
\enditemize |
| 5291 |
|
| 5292 |
\noindent |
| 5293 |
A possible way to find the ``best'' page for the latter case |
| 5294 |
could be the following: |
| 5295 |
\itemize |
| 5296 |
\item If the position is inside a paragraph, break the paragraph |
| 5297 |
into lines. One line will contain |
| 5298 |
the given position. Let's call this the destination line. |
| 5299 |
\item If the paragraph will not fit entirely on the page, |
| 5300 |
start the page with the beginning of the |
| 5301 |
paragraph if that will place the destination line on the page, otherwise |
| 5302 |
start with a line in the paragraph that is about half a page |
| 5303 |
before the destination line. |
| 5304 |
\item Else traverse the content list backward for about $2/3$ of the |
| 5305 |
page height and forward for about $2/3$ of the page height, searching |
| 5306 |
for the smallest negative penalty node. Use the penalty node found as |
| 5307 |
either the beginning or ending of the page. |
| 5308 |
\item If there are several equally low negative penalty nodes. Prefer |
| 5309 |
penalties preceding the destination line over penalty nodes following |
| 5310 |
it. A good page start is more important than a good page end. |
| 5311 |
\item If there are are still several equally low negative penalty |
| 5312 |
nodes, choose the one whose distance to the destination line is closest |
| 5313 |
to $1/2$ of the page height. |
| 5314 |
\item If no negative penalty nodes could be found, start the page with |
| 5315 |
the paragraph containing the destination line. |
| 5316 |
\item Once the page start (or end) is found, use \TeX's page builder |
| 5317 |
(or its reverse variant) to complete the page. |
| 5318 |
\enditemize |
| 5319 |
|
| 5320 |
We call content nodes that reference some position inside the content section |
| 5321 |
``link'' nodes. The position that is referenced is called the destination of the link. |
| 5322 |
Link nodes occur always in pairs of an ``start'' link |
| 5323 |
followed by a corresponding ``end'' link that both reference the same position |
| 5324 |
%, the same nesting level, % not sure! |
| 5325 |
and no other link nodes between them. |
| 5326 |
The content between the two will constitute the visible part of the link. |
| 5327 |
|
| 5328 |
To encode a position inside the content section that can be used |
| 5329 |
as the destination of a link node, an other kind of node is needed which |
| 5330 |
we call a ``label''. |
| 5331 |
|
| 5332 |
Links are not the only way to navigate inside a large |
| 5333 |
document. The user interface can also present an ``outline'' |
| 5334 |
of the document that can be used for navigation. |
| 5335 |
An outline node implements an association between a name displayed by the |
| 5336 |
user interface of the \HINT\ viewer and the destination position in the \HINT\ document. |
| 5337 |
|
| 5338 |
It is possible though that outline nodes, link nodes, and label nodes can share |
| 5339 |
the same kind-value and we have |outline_kind==link_kind==label_kind|. |
| 5340 |
To distinguish an outline node from a label node---both occur |
| 5341 |
in the short format definition section---the |b100| info bit is set in an |
| 5342 |
outline node. |
| 5343 |
|
| 5344 |
|
| 5345 |
@<get functions@>= |
| 5346 |
void hget_outline_or_label_def(Info i, uint32_t node_pos) |
| 5347 |
{ @+if (i&b100) |
| 5348 |
@<get and write an outline node@>@; |
| 5349 |
else |
| 5350 |
@<get and store a label node@>@; |
| 5351 |
} |
| 5352 |
@ |
| 5353 |
|
| 5354 |
The next thing we need to implement is a new maximum number |
| 5355 |
for outline nodes. We store this number in the variable |
| 5356 |
|max_outline| and limit it to a 16 bit value. |
| 5357 |
|
| 5358 |
In the short format, the value of |max_outline| is stored with the |
| 5359 |
other maximum values using the kind value |outline_kind==label_kind| and the info |
| 5360 |
value |b100| for single byte and |b101| for a two byte value. |
| 5361 |
|
| 5362 |
\codesection{\getsymbol}{Reading the Short Format}\getindex{1}{7}{Special Maximum Values} |
| 5363 |
@<cases of getting special maximum values@>= |
| 5364 |
@t\1\kern1em@> |
| 5365 |
case TAG(outline_kind,b100): |
| 5366 |
case TAG(outline_kind,b101): max_outline=n; |
| 5367 |
DBG(DBGDEF|DBGLABEL,"max(outline) = %d\n",max_outline); break; |
| 5368 |
@ |
| 5369 |
|
| 5370 |
\codesection{\putsymbol}{Writing the Short Format}\putindex{1}{7}{Special Maximum Values} |
| 5371 |
@<cases of putting special maximum values@>= |
| 5372 |
if (max_outline>-1) |
| 5373 |
{ uint32_t pos=hpos++-hstart; |
| 5374 |
DBG(DBGDEF|DBGLABEL,"max(outline) = %d\n",max_outline); |
| 5375 |
hput_tags(pos,TAG(outline_kind,b100|(hput_n(max_outline)-1))); |
| 5376 |
} |
| 5377 |
@ |
| 5378 |
|
| 5379 |
\codesection{\wrtsymbol}{Writing the Long Format}\wrtindex{1}{7}{Special Maximum Values} |
| 5380 |
@<cases of writing special maximum values@>= |
| 5381 |
@t\1\kern1em@> |
| 5382 |
case label_kind: |
| 5383 |
if (max_ref[label_kind]>-1)@/ |
| 5384 |
{ hwrite_start(); |
| 5385 |
hwritef("label %d",max_ref[label_kind]); |
| 5386 |
hwrite_end();@+ |
| 5387 |
} |
| 5388 |
if (max_outline>-1)@/ |
| 5389 |
{ hwrite_start(); |
| 5390 |
hwritef("outline %d", max_outline); |
| 5391 |
hwrite_end();@+ |
| 5392 |
} |
| 5393 |
break; |
| 5394 |
@ |
| 5395 |
|
| 5396 |
\codesection{\redsymbol}{Reading the Long Format}\redindex{1}{7}{Special Maximum Values} |
| 5397 |
@<parsing rules@>= |
| 5398 |
max_value: OUTLINE UNSIGNED { max_outline=$2; |
| 5399 |
RNG("max outline",max_outline,0, 0xFFFF); |
| 5400 |
DBG(DBGDEF|DBGLABEL,"Setting max outline to %d\n",max_outline); |
| 5401 |
}; |
| 5402 |
@ |
| 5403 |
|
| 5404 |
After having seen the maximum values, we now explain labels, then links, |
| 5405 |
and finally outlines. |
| 5406 |
|
| 5407 |
|
| 5408 |
To store labels, we define a data type |Label| and an array |labels| |
| 5409 |
indexed by the labels reference number. |
| 5410 |
|
| 5411 |
@<hint basic types@>= |
| 5412 |
typedef struct |
| 5413 |
{@+ uint32_t pos; /* position */ |
| 5414 |
uint32_t pos0; /* secondary position */ |
| 5415 |
uint8_t where; /* where on the rendered page */ |
| 5416 |
bool used; /* label used in a link or an outline */ |
| 5417 |
int next; /* reference in a linked list */ |
| 5418 |
uint8_t f; /* font, currently not used */ |
| 5419 |
} Label; |
| 5420 |
@ |
| 5421 |
|
| 5422 |
The |where| field indicates where the label position |
| 5423 |
should be on the rendered page: at the top, |
| 5424 |
at the bottom, or somewhere in the middle. |
| 5425 |
An undefined label has |where| equal to zero. |
| 5426 |
|
| 5427 |
@<hint macros@>= |
| 5428 |
#define LABEL_UNDEF 0 |
| 5429 |
#define LABEL_TOP 1 |
| 5430 |
#define LABEL_BOT 2 |
| 5431 |
#define LABEL_MID 3 |
| 5432 |
@ |
| 5433 |
|
| 5434 |
@<common variables@>= |
| 5435 |
Label *labels=NULL; |
| 5436 |
int first_label=-1; |
| 5437 |
@ |
| 5438 |
The variable |first_label| will be used together with the |next| field of |
| 5439 |
a label to construct a linked list of labels. |
| 5440 |
|
| 5441 |
@<initialize definitions@>= |
| 5442 |
if (max_ref[label_kind]>=0)@/ |
| 5443 |
ALLOCATE(labels,max_ref[label_kind]+1,Label); |
| 5444 |
@ |
| 5445 |
|
| 5446 |
The implementation of labels has to solve the |
| 5447 |
problem of forward links: |
| 5448 |
a link node that references a label |
| 5449 |
that is not yet defined. |
| 5450 |
We solve this problem by |
| 5451 |
keeping all labels in the definition section. |
| 5452 |
So for every label at least a definition is available |
| 5453 |
before we start with the content section and we can fill |
| 5454 |
in the position when the label is found. |
| 5455 |
If we restrict labels to the definition section and |
| 5456 |
do not have an alternative representation, the number of possible references |
| 5457 |
is a hard limit on the number of labels in a document. |
| 5458 |
Therefore label references are allowed to use 16 bit reference numbers. |
| 5459 |
In the short format, |
| 5460 |
the |b001| bit indicates a two byte reference number if set, and a one byte |
| 5461 |
reference number otherwise. |
| 5462 |
|
| 5463 |
In the short format, the complete information about a label is in the definition section. |
| 5464 |
In the long format, this is not possible because we do not have node positions. |
| 5465 |
Therefore we will put label nodes at appropriate points in the content section |
| 5466 |
and compute the label position when writing the short format. |
| 5467 |
|
| 5468 |
\gdef\subcodetitle{Labels} |
| 5469 |
\readcode |
| 5470 |
@s LABEL symbol |
| 5471 |
@s BOT symbol |
| 5472 |
@s MID symbol |
| 5473 |
@s placement symbol |
| 5474 |
|
| 5475 |
@<symbols@>= |
| 5476 |
%token LABEL "label" |
| 5477 |
%token BOT "bot" |
| 5478 |
%token MID "mid" |
| 5479 |
%type <i> placement |
| 5480 |
@ |
| 5481 |
|
| 5482 |
@<scanning rules@>= |
| 5483 |
::@=label@> :< return LABEL; >: |
| 5484 |
::@=bot@> :< return BOT; >: |
| 5485 |
::@=mid@> :< return MID; >: |
| 5486 |
@ |
| 5487 |
|
| 5488 |
A label node specifies the reference number and a placement. |
| 5489 |
|
| 5490 |
@<parsing rules@>= |
| 5491 |
placement: TOP {$$=LABEL_TOP;} | BOT {$$=LABEL_BOT;} | MID {$$=LABEL_MID;} | {$$=LABEL_MID;}; |
| 5492 |
content_node: START LABEL REFERENCE placement END @| |
| 5493 |
{ hset_label($3,$4); @+}; |
| 5494 |
@ |
| 5495 |
|
| 5496 |
|
| 5497 |
After parsing a label, the function |hset_label| is called. |
| 5498 |
|
| 5499 |
@<put functions@>= |
| 5500 |
void hset_label(int n,int w ) |
| 5501 |
{ Label *t; |
| 5502 |
REF_RNG(label_kind,n); |
| 5503 |
t=labels+n;@/ |
| 5504 |
if (t->where!=LABEL_UNDEF) |
| 5505 |
MESSAGE("Duplicate definition of label %d\n",n); |
| 5506 |
t->where=w; |
| 5507 |
t->pos=hpos-hstart; |
| 5508 |
t->pos0=hpos0-hstart; |
| 5509 |
t->next=first_label; first_label=n; |
| 5510 |
} |
| 5511 |
@ |
| 5512 |
|
| 5513 |
|
| 5514 |
The above function will simply store the data obtained in the |labels| array. |
| 5515 |
The generation of the short format output is |
| 5516 |
postponed until the entire content section has been parsed and |
| 5517 |
the positions of all labels are known. |
| 5518 |
|
| 5519 |
One more complication needs to be considered: The |hput_list| function |
| 5520 |
is allowed to move lists in the output stream and if positions |
| 5521 |
inside the list were recorded in a label, these labels need an |
| 5522 |
adjustment. To find out quickly if any labels are affected, |
| 5523 |
the |hset_label| function |
| 5524 |
constructs a linked list of labels starting with the reference number |
| 5525 |
of the most recent label in |first_label| and the |
| 5526 |
reference number of the label preceding label |i| in |labels[i].next|. |
| 5527 |
Because labels are recorded with increasing positions, |
| 5528 |
the list will be sorted with positions decreasing. |
| 5529 |
|
| 5530 |
@<adjust label positions after moving a list@>= |
| 5531 |
{ int i; |
| 5532 |
for (i=first_label;i>=0 && labels[i].pos>=l->p;i=labels[i].next) |
| 5533 |
{ DBG(DBGNODE|DBGLABEL,"Moving label *%d by %d\n", i,d);@/ |
| 5534 |
labels[i].pos+=d; |
| 5535 |
if (labels[i].pos0>=l->p) labels[i].pos0+=d; |
| 5536 |
} |
| 5537 |
} |
| 5538 |
@ |
| 5539 |
|
| 5540 |
|
| 5541 |
The |hwrite_label| function\label{hwritelabel} is the reverse of the above parsing rule. |
| 5542 |
Note that it is different from the |
| 5543 |
usual |hwrite_|\dots\ functions. And we will see shortly why that is so. |
| 5544 |
|
| 5545 |
%see |hwrite_range| |
| 5546 |
\writecode |
| 5547 |
@<write functions@>= |
| 5548 |
void hwrite_label(void) /* called in |hwrite_end| and at the start of a list */ |
| 5549 |
{@+ while (first_label>=0 && (uint32_t)(hpos-hstart)>=labels[first_label].pos)@/ |
| 5550 |
{ Label *t=labels+first_label; |
| 5551 |
DBG(DBGLABEL,"Inserting label *%d\n", first_label); |
| 5552 |
hwrite_start(); |
| 5553 |
hwritef("label *%d",first_label); |
| 5554 |
if (t->where==LABEL_TOP) hwritef(" top"); |
| 5555 |
else if (t->where==LABEL_BOT) hwritef(" bot"); |
| 5556 |
nesting--;hwritec('>'); /* avoid a recursive call to |hwrite_end| */ |
| 5557 |
first_label=labels[first_label].next; |
| 5558 |
} |
| 5559 |
} |
| 5560 |
@ |
| 5561 |
|
| 5562 |
The short format specifies the label positions in the definition section. |
| 5563 |
This is not possible in the long format because there are no ``positions'' |
| 5564 |
in the long format. Therefore long format label nodes must |
| 5565 |
be inserted in the content section just before those nodes |
| 5566 |
that should come after the label. The function |hwrite_label| is called |
| 5567 |
in |hwrite_end|. At that point |hpos| is the position of the next node |
| 5568 |
and it can be compared with the positions of the labels taken from |
| 5569 |
the definition section. |
| 5570 |
Because |hpos| is strictly increasing while reading the content section, |
| 5571 |
the comparison can be made efficient by sorting the labels. |
| 5572 |
The sorting uses the |next| field in the |
| 5573 |
array of |labels| to construct a linked list. After sorting, the value of |
| 5574 |
|first_label| is the index of the label with the smallest position; |
| 5575 |
and for each |i|, the value of |labels[i].next| is the index of |
| 5576 |
the label with the next bigger position. If |labels[i].next| is negative, |
| 5577 |
there is no next bigger position. |
| 5578 |
Currently a simple insertion sort is used. |
| 5579 |
The insertion sort will work well if the labels are already |
| 5580 |
mostly in ascending order. |
| 5581 |
If we expect lots of labels in random order, |
| 5582 |
a more sophisticated sorting algorithm might be appropriate. |
| 5583 |
|
| 5584 |
|
| 5585 |
|
| 5586 |
@<write functions@>= |
| 5587 |
void hsort_labels(void) |
| 5588 |
{ int i; |
| 5589 |
if (max_ref[label_kind]<0) |
| 5590 |
{ first_label=-1; return; @+} /* empty list */ |
| 5591 |
first_label=max_ref[label_kind]; |
| 5592 |
while (first_label>=0 && labels[first_label].where==LABEL_UNDEF) |
| 5593 |
first_label--; |
| 5594 |
if (first_label<0) return; /* no defined labels */ |
| 5595 |
labels[first_label].next=-1; |
| 5596 |
DBG(DBGLABEL,"Sorting %d labels\n",first_label+1); |
| 5597 |
for (i=first_label-1; i>=0; i--) /* insert label |i| */ |
| 5598 |
if (labels[i].where!=LABEL_UNDEF)@/ |
| 5599 |
{ uint32_t pos=labels[i].pos; |
| 5600 |
if (labels[first_label].pos >= pos)@/ |
| 5601 |
{ labels[i].next= first_label; first_label=i;@+ } /* new smallest */ |
| 5602 |
else @/ |
| 5603 |
{ int j; |
| 5604 |
for (j= first_label; |
| 5605 |
labels[j].next>=0 && labels[labels[j].next].pos<pos; |
| 5606 |
j=labels[j].next) continue; |
| 5607 |
labels[i].next=labels[j].next; labels[j].next=i; |
| 5608 |
} |
| 5609 |
} |
| 5610 |
} |
| 5611 |
@ |
| 5612 |
|
| 5613 |
|
| 5614 |
The following code is used to get label information from the |
| 5615 |
definition section and store it in the |labels| array. |
| 5616 |
The |b010| bit indicates the presence of a secondary position for the label. |
| 5617 |
|
| 5618 |
\getcode |
| 5619 |
@<get and store a label node@>= |
| 5620 |
{ Label *t; |
| 5621 |
int n; |
| 5622 |
if (i&b001) HGET16(n); @+else n=HGET8; |
| 5623 |
REF_RNG(label_kind,n); |
| 5624 |
t=labels+n; |
| 5625 |
if (t->where!=LABEL_UNDEF) |
| 5626 |
DBG(DBGLABEL,"Duplicate definition of label %d at 0x%x\n",n, node_pos); |
| 5627 |
HGET32(t->pos); |
| 5628 |
t->where=HGET8; |
| 5629 |
if (t->where==LABEL_UNDEF || t->where>LABEL_MID) |
| 5630 |
DBG(DBGLABEL,"Label %d where value invalid: %d at 0x%x\n",n,t->where,node_pos); |
| 5631 |
if (i&b010) /* secondary position */ |
| 5632 |
{ HGET32(t->pos0); t->f=HGET8;@+} |
| 5633 |
else t->pos0=t->pos; |
| 5634 |
DBG(DBGLABEL,"Defining label %d at 0x%x/0x%x\n",n,t->pos0,t->pos); |
| 5635 |
} |
| 5636 |
@ |
| 5637 |
|
| 5638 |
|
| 5639 |
The function |hput_label| is simply the reverse of the above code. |
| 5640 |
|
| 5641 |
\putcode |
| 5642 |
@<put functions@>= |
| 5643 |
Tag hput_label(int n, Label *l) |
| 5644 |
{ Info i=b000; |
| 5645 |
HPUTX(13); |
| 5646 |
if (n>0xFF) {i|=b001; HPUT16(n);@+}@+ else HPUT8(n); |
| 5647 |
HPUT32(l->pos); |
| 5648 |
HPUT8(l->where); |
| 5649 |
if (l->pos!=l->pos0) |
| 5650 |
{ i|=b010; HPUT32(l->pos0); HPUT8(l->f); @+} |
| 5651 |
DBG(DBGLABEL,"Defining label %d at 0x%x/0x%x\n",n,l->pos0,l->pos); |
| 5652 |
return TAG(label_kind,i); |
| 5653 |
} |
| 5654 |
@ |
| 5655 |
|
| 5656 |
|hput_label_defs| is called by the parser after the entire content |
| 5657 |
section has been processed; it appends the label definitions |
| 5658 |
to the definition section. |
| 5659 |
%Using the fact that the linked list |
| 5660 |
%starting at |first_label| already contains all labels in |
| 5661 |
%order of descending position, we could easily output the |
| 5662 |
%labels in sorted order and reconstruct the sorting while reading |
| 5663 |
%in the labels. The \HINT\ format however does not require |
| 5664 |
%label nodes to be sorted and the |hsort_labels| function |
| 5665 |
%can not be avoided. |
| 5666 |
The outlines are stored after the labels because they reference the labels. |
| 5667 |
@<put functions@>= |
| 5668 |
extern void hput_definitions_end(void); |
| 5669 |
extern Tag hput_outline(Outline *t); |
| 5670 |
void hput_label_defs(void) |
| 5671 |
{ int n; |
| 5672 |
section_no=1; |
| 5673 |
hstart=dir[1].buffer; |
| 5674 |
hend=hstart+ dir[1].bsize; |
| 5675 |
hpos=hstart+dir[1].size;@/ |
| 5676 |
@<output the label definitions@>@; |
| 5677 |
@<output the outline definitions@>@; |
| 5678 |
hput_definitions_end(); |
| 5679 |
} |
| 5680 |
@ |
| 5681 |
|
| 5682 |
@<output the label definitions@>= |
| 5683 |
for (n=0; n<=max_ref[label_kind]; n++)@/ |
| 5684 |
{ Label *l=labels+n; |
| 5685 |
uint32_t pos; |
| 5686 |
if (l->used)@/ |
| 5687 |
{ pos=hpos++-hstart; |
| 5688 |
hput_tags(pos,hput_label(n,l)); |
| 5689 |
if (l->where==LABEL_UNDEF) |
| 5690 |
MESSAGE("WARNING: Label *%d is used but not defined\n",n); |
| 5691 |
else |
| 5692 |
DBG(DBGDEF|DBGLABEL,"Label *%d defined 0x%x\n",n,pos);@/ |
| 5693 |
} |
| 5694 |
else |
| 5695 |
{ if (l->where!=LABEL_UNDEF) |
| 5696 |
{ pos=hpos++-hstart; |
| 5697 |
hput_tags(pos,hput_label(n,l)); |
| 5698 |
DBG(DBGDEF|DBGLABEL,"Label *%d defined but not used 0x%x\n",n,pos);@/ |
| 5699 |
} |
| 5700 |
} |
| 5701 |
} |
| 5702 |
@ |
| 5703 |
|
| 5704 |
Links are simpler than labels. They are found only in the |
| 5705 |
content section and resemble pretty much what we have seen for other |
| 5706 |
content nodes. Let's look at them next. |
| 5707 |
When reading a short format link node, |
| 5708 |
we use again the |b001| info bit to indicate a 16 bit reference |
| 5709 |
number to a label. |
| 5710 |
|
| 5711 |
To help a reader tell a link from ordinary text, links should be |
| 5712 |
visualy different. This is supported in the \HINT\ file format |
| 5713 |
by associating a different color scheme to a link. |
| 5714 |
In the short format, the |b100| bit indicates that a color set reference |
| 5715 |
(see section~\secref{colors}) follows after the label reference. |
| 5716 |
A color reference to 1 in the start node and to |0xFF| in the end node |
| 5717 |
is the default and is omitted. |
| 5718 |
|
| 5719 |
Because color changes are local to the enclosing box or paragraph, |
| 5720 |
a link is local as well. Without further mentioning, here and |
| 5721 |
in the following, when we say ``box'' it also mean ``paragraph''. |
| 5722 |
A link starts with a ``start'' link and ends with either an ``end'' |
| 5723 |
link or the end of the enclosing box. Links must not be nested. |
| 5724 |
It is an error to have two start links in the same box without |
| 5725 |
an end link between them. |
| 5726 |
An application can choose to continue a link in the next box |
| 5727 |
by inserting a copy of the start link node at the begining of |
| 5728 |
the new box. |
| 5729 |
In short: ``end'' links are mandatory when separating two links |
| 5730 |
but optional if they just preceede the end of the box. |
| 5731 |
The |b010| info bit indicates a ``start'' link; |
| 5732 |
otherwise it is an ``end'' link. |
| 5733 |
|
| 5734 |
\gdef\subcodetitle{Links} |
| 5735 |
\getcode |
| 5736 |
@<get macros@>= |
| 5737 |
#define @[HGET_LINK(I)@] @/\ |
| 5738 |
{ int n,c; if (I&b001) HGET16(n);@+ else n=HGET8;\ |
| 5739 |
if (I&b100) c=HGET8; else c=(I&b010)?1:0xFF;\ |
| 5740 |
hwrite_link(n,c,I&b010); @+} |
| 5741 |
@ |
| 5742 |
|
| 5743 |
@<cases to get content@>= |
| 5744 |
@t\1\kern1em@> |
| 5745 |
case TAG(link_kind,b000): @+ HGET_LINK(b000);@+ break; |
| 5746 |
case TAG(link_kind,b001): @+ HGET_LINK(b001);@+ break; |
| 5747 |
case TAG(link_kind,b010): @+ HGET_LINK(b010);@+ break; |
| 5748 |
case TAG(link_kind,b011): @+ HGET_LINK(b011);@+ break; |
| 5749 |
case TAG(link_kind,b100): @+ HGET_LINK(b100);@+ break; |
| 5750 |
case TAG(link_kind,b101): @+ HGET_LINK(b101);@+ break; |
| 5751 |
case TAG(link_kind,b110): @+ HGET_LINK(b110);@+ break; |
| 5752 |
case TAG(link_kind,b111): @+ HGET_LINK(b111);@+ break; |
| 5753 |
@ |
| 5754 |
|
| 5755 |
The function |hput_link| will insert the link in the output stream and return |
| 5756 |
the appropriate tag. |
| 5757 |
|
| 5758 |
\putcode |
| 5759 |
@<put functions@>= |
| 5760 |
Tag hput_link(int n, int c, int on) |
| 5761 |
{ Info i; |
| 5762 |
REF_RNG(label_kind,n); |
| 5763 |
labels[n].used=true; |
| 5764 |
if (on) i=b010;@+ else i=b000; |
| 5765 |
if (n>0xFF) { i|=b001; HPUT16(n);@+} @+else HPUT8(n); |
| 5766 |
if ((on && c!=1) ||(!on && c!=0xFF)) { i|=b100; HPUT8(c); } |
| 5767 |
return TAG(link_kind,i); |
| 5768 |
} |
| 5769 |
@ |
| 5770 |
|
| 5771 |
\readcode |
| 5772 |
@s LINK symbol |
| 5773 |
@<symbols@>= |
| 5774 |
%token LINK "link" |
| 5775 |
@ |
| 5776 |
@<scanning rules@>= |
| 5777 |
::@=link@> :< return LINK; >: |
| 5778 |
@ |
| 5779 |
|
| 5780 |
@<parsing rules@>= |
| 5781 |
content_node:start LINK REFERENCE on_off END |
| 5782 |
{@+ hput_tags($1,hput_link($3,$4?1:0xFF,$4));@+ }; |
| 5783 |
content_node:start LINK REFERENCE on_off REFERENCE END |
| 5784 |
{@+ hput_tags($1,hput_link($3,$5,$4));@+ }; |
| 5785 |
@ |
| 5786 |
|
| 5787 |
\writecode |
| 5788 |
@<write functions@>= |
| 5789 |
void hwrite_link(int n, int c, uint8_t on) |
| 5790 |
{ REF_RNG(label_kind,n); |
| 5791 |
if (labels[n].where==LABEL_UNDEF) |
| 5792 |
MESSAGE("WARNING: Link to an undefined label %d\n",n); |
| 5793 |
hwrite_ref(n); |
| 5794 |
if (on) hwritef(" on"); |
| 5795 |
else hwritef(" off"); |
| 5796 |
if ((on && c!=1)||(!on && c!=0xFF)) |
| 5797 |
{ REF_RNG(color_kind,c); hwrite_ref(c); } |
| 5798 |
} |
| 5799 |
@ |
| 5800 |
|
| 5801 |
Now we look at the |
| 5802 |
outline nodes which are found only in the definition section. |
| 5803 |
Every outline node is associated with a label node, giving the position in the |
| 5804 |
document, and a unique title that should tell the user |
| 5805 |
what to expect when navigating to this position. For example |
| 5806 |
an item with the title ``Table of Content'' should navigate |
| 5807 |
to the page that shows the table of content. |
| 5808 |
The sequence of outline nodes found in the definition section |
| 5809 |
gets a tree structure by assigning to each item a depth level. |
| 5810 |
|
| 5811 |
@<hint types@>= |
| 5812 |
typedef struct {@+ |
| 5813 |
uint8_t *t; /* title */ |
| 5814 |
int s; /* title size */ |
| 5815 |
int d; /* depth */ |
| 5816 |
uint16_t r; /* reference to a label */ |
| 5817 |
} Outline; |
| 5818 |
@ |
| 5819 |
|
| 5820 |
@<shared put variables@>= |
| 5821 |
Outline *outlines; |
| 5822 |
@ |
| 5823 |
|
| 5824 |
@<initialize definitions@>= |
| 5825 |
if (max_outline>=0)@/ |
| 5826 |
ALLOCATE(outlines,max_outline+1,Outline); |
| 5827 |
@ |
| 5828 |
|
| 5829 |
Child items follow their parent item and have a bigger depth level. |
| 5830 |
In the short format, the first item must be a root item, with |
| 5831 |
a depth level of 0. Further, if any item has the depth $d$, then the |
| 5832 |
item following it must have either the same depth $d$ in which |
| 5833 |
case it is a sibling, or the depth $d+1$ in which case it is a child, |
| 5834 |
or a depth $d^\prime$ with $0\le d^\prime<d$ in which case it is a sibling |
| 5835 |
of the latest ancestor with depth $d^\prime$. Because the depth is |
| 5836 |
stored in a single byte, the maximum depth is |0xFF|. |
| 5837 |
|
| 5838 |
In the long format, the depth assignments are more flexible. |
| 5839 |
We allow any signed integer, but insist that the depth |
| 5840 |
assignments can be compressed to depth levels for the |
| 5841 |
short format using the following algorithm: |
| 5842 |
|
| 5843 |
@<compress long format depth levels@>= |
| 5844 |
n=0;@+ |
| 5845 |
while (n<=max_outline) |
| 5846 |
n=hcompress_depth(n,0); |
| 5847 |
@ |
| 5848 |
Outline items must be listed in the order |
| 5849 |
in which they should be displayed. |
| 5850 |
The function |hcompress_depth(n,c)| will compress the subtree starting at |
| 5851 |
|n| with root level |d| to a new tree with the same structure |
| 5852 |
and root level |c|. It returns the outline number of the |
| 5853 |
following subtree. |
| 5854 |
|
| 5855 |
@<put functions@>= |
| 5856 |
int hcompress_depth(int n, int c) |
| 5857 |
{ int d=outlines[n].d; |
| 5858 |
if (c>0xFF) |
| 5859 |
QUIT("Outline %d, depth level %d to %d out of range",n,d,c); |
| 5860 |
while (n<=max_outline) |
| 5861 |
if (outlines[n].d==d) |
| 5862 |
outlines[n++].d=c; |
| 5863 |
else if (outlines[n].d>d) |
| 5864 |
n=hcompress_depth(n,c+1); |
| 5865 |
else break; |
| 5866 |
return n; |
| 5867 |
} |
| 5868 |
@ |
| 5869 |
|
| 5870 |
For an outline node, the |b001| bit indicates a two byte reference to a label. |
| 5871 |
There is no reference number for an outline item itself: |
| 5872 |
it is never referenced anywhere in an \HINT\ file. |
| 5873 |
|
| 5874 |
\gdef\subcodetitle{Outlines} |
| 5875 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 5876 |
|
| 5877 |
@<get and write an outline node@>= |
| 5878 |
{ int r,d; |
| 5879 |
List l; |
| 5880 |
static int outline_no=-1; |
| 5881 |
hwrite_start();@+hwritef("outline"); |
| 5882 |
++outline_no; |
| 5883 |
RNG("outline",outline_no, 0, max_outline); |
| 5884 |
if (i&b001) HGET16(r);@+ else r=HGET8; |
| 5885 |
REF_RNG(link_kind,r); |
| 5886 |
if (labels[r].where==LABEL_UNDEF)@/ |
| 5887 |
MESSAGE("WARNING: Outline with undefined label %d at 0x%x\n",@|r, node_pos); |
| 5888 |
hwritef(" *%d",r);@/ |
| 5889 |
d=HGET8; hwritef(" %d",d);@/ |
| 5890 |
hget_list(&l);hwrite_list(&l);@/ |
| 5891 |
hwrite_end(); |
| 5892 |
} |
| 5893 |
@ |
| 5894 |
|
| 5895 |
When parsing an outline definition in the long format, |
| 5896 |
we parse the outline title as a |list| which will |
| 5897 |
write the representation of the list to the output stream. |
| 5898 |
Writing the outline definitions, however, must be postponed |
| 5899 |
until the label have found their way into the definition |
| 5900 |
section. So we save the list's representation in the |
| 5901 |
outline node for later use and remove it again from the |
| 5902 |
output stream. |
| 5903 |
|
| 5904 |
\readcode |
| 5905 |
@s OUTLINE symbol |
| 5906 |
|
| 5907 |
@<symbols@>= |
| 5908 |
%token OUTLINE "outline" |
| 5909 |
@ |
| 5910 |
|
| 5911 |
@<scanning rules@>= |
| 5912 |
::@=outline@> :< return OUTLINE; >: |
| 5913 |
@ |
| 5914 |
|
| 5915 |
@<parsing rules@>= |
| 5916 |
def_node: START OUTLINE REFERENCE integer position list END { |
| 5917 |
static int outline_no=-1; |
| 5918 |
$$.k=outline_kind; $$.n=$3; |
| 5919 |
if ($6.s==0) QUIT("Outline with empty title in line %d",yylineno); |
| 5920 |
outline_no++; |
| 5921 |
hset_outline(outline_no,$3,$4,$5); |
| 5922 |
}; |
| 5923 |
@ |
| 5924 |
|
| 5925 |
@<put functions@>= |
| 5926 |
void hset_outline(int m, int r, int d, uint32_t pos) |
| 5927 |
{ Outline *t; |
| 5928 |
RNG("Outline",m,0,max_outline); |
| 5929 |
t=outlines+m; |
| 5930 |
REF_RNG(label_kind,r); |
| 5931 |
t->r=r; |
| 5932 |
t->d=d; |
| 5933 |
t->s=hpos-(hstart+pos); |
| 5934 |
hpos=(hstart+pos); |
| 5935 |
ALLOCATE(t->t,t->s,uint8_t); |
| 5936 |
memmove(t->t,hpos,t->s); |
| 5937 |
labels[r].used=true; |
| 5938 |
} |
| 5939 |
@ |
| 5940 |
To output the title, we need to move the list back to the output stream. |
| 5941 |
Before doing so, we allocate space (and make sure there is room left for the |
| 5942 |
end tag of the outline node), and after doing so, we release |
| 5943 |
the memory used to save the title. |
| 5944 |
|
| 5945 |
@<output the title of outline |*t|@>= |
| 5946 |
memmove(hpos,t->t,t->s); |
| 5947 |
hpos=hpos+t->s; |
| 5948 |
free(t->t); |
| 5949 |
@ |
| 5950 |
|
| 5951 |
We output all outline definitions from 0 to |max_outline| and |
| 5952 |
check that every one of them has a title. Thereby we make sure |
| 5953 |
that in the short format |max_outline| matches the number of |
| 5954 |
outline definitions. |
| 5955 |
|
| 5956 |
\putcode |
| 5957 |
@<put functions@>= |
| 5958 |
Tag hput_outline(Outline *t) |
| 5959 |
{ Info i=b100; |
| 5960 |
HPUTX(t->s+4); |
| 5961 |
if (t->r>0xFF) {i|=b001; @+HPUT16(t->r);@+} @+else HPUT8(t->r); |
| 5962 |
labels[t->r].used=true; |
| 5963 |
HPUT8(t->d); |
| 5964 |
@<output the title of outline |*t|@>@; |
| 5965 |
return TAG(outline_kind,i); |
| 5966 |
} |
| 5967 |
@ |
| 5968 |
|
| 5969 |
@<output the outline definitions@>= |
| 5970 |
@<compress long format depth levels@>@; |
| 5971 |
for (n=0;n<=max_outline;n++) |
| 5972 |
{ Outline *t=outlines+n; |
| 5973 |
uint32_t pos; |
| 5974 |
pos=hpos++-hstart; |
| 5975 |
if (t->s==0 || t->t==NULL) |
| 5976 |
QUIT("Definition of outline %d has an empty title",n); |
| 5977 |
DBG(DBGDEF|DBGLABEL,"Outline *%d defined\n",n);@/ |
| 5978 |
hput_tags(pos,hput_outline(t)); |
| 5979 |
} |
| 5980 |
@ |
| 5981 |
|
| 5982 |
\subsection{Colors}\label{colors} |
| 5983 |
This is the third draft of implementing color\index{color} specifications in |
| 5984 |
a \HINT\ file. |
| 5985 |
|
| 5986 |
According to the initial philosophy of a \HINT\ file, a viewer must be |
| 5987 |
capable of rendering a page given just any valid position in the |
| 5988 |
content section without reading the entire file. This makes it |
| 5989 |
impossible to use global information; only the information that is |
| 5990 |
localy available can be used. Given a file position, the viewer will |
| 5991 |
compute a representation of the page, insert it into a page template, |
| 5992 |
and pass it to the renderer. Color will not effect the position of |
| 5993 |
glyphs or rules and so it is sufficient to process the color |
| 5994 |
information when rendering the page. The renderer will, however, |
| 5995 |
render the page always from the top down and from left to right. As a |
| 5996 |
consequence of the rendering order, it is very well possible to work |
| 5997 |
with a color state within the top level boxes. |
| 5998 |
|
| 5999 |
A separate issue is the specification of color changes on the top |
| 6000 |
level. While a vertical list contains no character nodes, a color |
| 6001 |
specification might still affect the background color and the |
| 6002 |
foreground color of rules. Because we still want to avoid the search |
| 6003 |
for color nodes on the top level, we restrict the scope of a color |
| 6004 |
node on the top level. It will extend only to the next possible page |
| 6005 |
break and applications like Hi\TeX\ must repeat a top level color node |
| 6006 |
after every node that could be used as a page break. |
| 6007 |
|
| 6008 |
% Some statisticts |
| 6009 |
% format.hnt 4065407 1103094 byte (compressed), 31802 top level content nodes |
| 6010 |
% Nodes that could be page breaks: |
| 6011 |
% glue nodes 12346 if the preceeding node is a page break |
| 6012 |
% kern nodes 0 if the next node is a glue. |
| 6013 |
% penalty nodes 2193 (unless they are infinity or bigger) |
| 6014 |
% so about 15000 extra color nodes using about 45000 bytes |
| 6015 |
% could be needed. The extra space required is close to 1 percent |
| 6016 |
% not counting compression. |
| 6017 |
% Link nodes 10154 (5077 on and 5077 off nodes) |
| 6018 |
% Having extra color nodes for links is at most 30462 byte. |
| 6019 |
% This is less than 1 percent --- again without accounting for compression. |
| 6020 |
% This could be reduced to 10154 byte if we use the b100 bit |
| 6021 |
% and include the color ref in the link node - which might be |
| 6022 |
% a good idea. |
| 6023 |
|
| 6024 |
The nesting of boxes on a page together with the transparency of colors |
| 6025 |
leads to the problem of stacking several layers of color one on top |
| 6026 |
of the other. |
| 6027 |
Here is an example: An outer box might specify blue as a background color |
| 6028 |
and white as a forground color while an inside box specifies a transparent |
| 6029 |
grey background and a transparent black foreground. |
| 6030 |
Then we expect text in the outer box to have white letters on blue background. |
| 6031 |
Further we want to see the inner box casting a grey shadown on the blue |
| 6032 |
background, resulting in a mix of blue and gray, with black letters on |
| 6033 |
top of it that are not completely black but let the background shine through. |
| 6034 |
|
| 6035 |
To limit the complexity, the \HINT\ file format will allow this stacking |
| 6036 |
of colors only when nesting boxes. But inside a box, there is at any position |
| 6037 |
only one foreground and one background color; a color change inside a box |
| 6038 |
will simply replace the current colors. |
| 6039 |
|
| 6040 |
If an application like Hi\TeX\ wants |
| 6041 |
to implement nesting colors inside a box, it has to implement its own color |
| 6042 |
stack and compute the necessary color mixtures. |
| 6043 |
There is only one exception to this concept: When a new box starts, |
| 6044 |
the current colors will be those of the enclosing box. These colors |
| 6045 |
can be restored after a color change by using the \<color off> command. |
| 6046 |
|
| 6047 |
The limited complexity is necessary to simplifies the spliting of |
| 6048 |
boxes, for example by the line breaking routine. Repeating the last |
| 6049 |
color node before the split just after the split is sufficient. |
| 6050 |
|
| 6051 |
Inside a horizontal list, a background color will extend from |
| 6052 |
top to bottom; inside a vertical list a background color will extend |
| 6053 |
from the left edge to the right edge. |
| 6054 |
If the document does not want to change the background color, |
| 6055 |
a completely transparent color should be used. |
| 6056 |
|
| 6057 |
While the current implementation of searching does not use the |
| 6058 |
background color, a color set will still specify background and foreground |
| 6059 |
for all colors. This is simpler, easier to extend at a later time, |
| 6060 |
and the overhead is small. |
| 6061 |
|
| 6062 |
|
| 6063 |
After these preliminaries let's turn our attention to the design of |
| 6064 |
a suitable color concept. |
| 6065 |
|
| 6066 |
|
| 6067 |
%Before the present implementation of colors, |
| 6068 |
%we used a foreground color and a |
| 6069 |
%background color; then we had a special foreground color for links; |
| 6070 |
%and finaly the implementation of searching used special forground |
| 6071 |
%colors to mark matches and highlight the current search focus. As if |
| 6072 |
%that was not sufficient, viewers could be switched to ``dark mode'' using |
| 6073 |
%a different forground and background color, and of course it was |
| 6074 |
%desirable to adjust the link, mark, and focus color as well. |
| 6075 |
% |
| 6076 |
%In the future, a document designer might want to indicate a link using a |
| 6077 |
%different background color instead or in combination with a special |
| 6078 |
%foreground color. Similar considerations apply to the colors used for |
| 6079 |
%searching. By the way: the interaction of link color with mark or |
| 6080 |
%focus color was an open question; probably mixing the colors might be a |
| 6081 |
%good solution. But there is another feature that sets links appart |
| 6082 |
%from the matching text in a search: The start and end of a link is |
| 6083 |
%already recorded in a \HINT\ file. So an automatic color change might |
| 6084 |
%be called for. |
| 6085 |
|
| 6086 |
%In the first redesign, a color set consisted of eight color pairs for |
| 6087 |
%normal, link, mark, and focus colors in day mode |
| 6088 |
%and four more color pairs for night mode; |
| 6089 |
%each pair specifying a background and a foreground color. |
| 6090 |
% |
| 6091 |
%It turned out that color switching using three bits in a status |
| 6092 |
%byte was inconvenient. Further this solution was mixing two different |
| 6093 |
%causes for a color change: The doument author's requests for a color |
| 6094 |
%change and the user interface's requests for a color change either |
| 6095 |
%by switching between day and night mode or by searching. |
| 6096 |
%While switching between day and night mode requires the author to |
| 6097 |
%supply two complete color schemas, searching just needs to change |
| 6098 |
%the color of glyphs according the the given color schema. |
| 6099 |
%While the color change for links is always known from the document, |
| 6100 |
%the color changes due to searching depend on text input at run time. |
| 6101 |
%So mixing all that together was possibly not the best solution. |
| 6102 |
|
| 6103 |
%The new and second redesign tries to seperate the different concerns. |
| 6104 |
|
| 6105 |
Colors come in sets. |
| 6106 |
A color set supports two modes: day and night mode. |
| 6107 |
In future extensions it might be possible for |
| 6108 |
an author to invent color sets for winter or summer, fall or |
| 6109 |
spring, or any other resonable or unreasonable purpose. |
| 6110 |
For each mode a color set specifies three color styles: |
| 6111 |
one for normal text, one for marked text and one for in-focus text. |
| 6112 |
The switching between different modes and different styles is |
| 6113 |
left to the user interface. |
| 6114 |
|
| 6115 |
We store a color set as an array of 12 words. |
| 6116 |
The first 6 words are for day mode the next 6 byte are for night mode; |
| 6117 |
For each mode we have three color pairs and each pair consists |
| 6118 |
of a forgraound and a backgraund color each stored as an RGBA value. |
| 6119 |
|
| 6120 |
@<hint basic types@>= |
| 6121 |
typedef uint32_t ColorSet[2*3*2]; |
| 6122 |
@ |
| 6123 |
To extract the various sub-arrays, we have the following macros: |
| 6124 |
@<hint macros@>= |
| 6125 |
#define CURCOLOR(M,S,C) ((C)+6*(M)+2*(S)) |
| 6126 |
#define DAY(C) CURCOLOR(0,0,C) |
| 6127 |
#define NIGHT(C) CURCOLOR(1,0,C) |
| 6128 |
#define HIGH(C) CURCOLOR(0,1,C) |
| 6129 |
#define FOCUS(C) CURCOLOR(0,2,C) |
| 6130 |
#define FG(C) ((C)[0]) |
| 6131 |
#define BG(C) ((C)[1]) |
| 6132 |
@ |
| 6133 |
|
| 6134 |
We will allow up to 255 color sets that are stored in the definition |
| 6135 |
section and are referenced in the content section by a single byte. |
| 6136 |
The definition of different color sets and the switching between them |
| 6137 |
is left to the document author. |
| 6138 |
|
| 6139 |
The color set with reference number zero specifies the default colors. |
| 6140 |
At the root of a page template, the default color set is selected |
| 6141 |
and the whole page is filled with the background color for normal text. |
| 6142 |
For links, by default the color set with number one is used. |
| 6143 |
Section ~\secref{colordefault} specifies default values for both |
| 6144 |
color sets; the default colors can be overwritten. |
| 6145 |
The color sets with reference numbers zero and one are not stored in the |
| 6146 |
definition section of a short format file if they are the same as the default values. |
| 6147 |
This makes files not using colors compatible with older versions of the \HINT\ file |
| 6148 |
format. |
| 6149 |
|
| 6150 |
Now we are ready for the implementation. |
| 6151 |
|
| 6152 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 6153 |
|
| 6154 |
@s COLOR symbol |
| 6155 |
@s color symbol |
| 6156 |
@s color_pair symbol |
| 6157 |
@s color_tripple symbol |
| 6158 |
@s color_set symbol |
| 6159 |
@s color_null symbol |
| 6160 |
|
| 6161 |
@<symbols@>= |
| 6162 |
%token COLOR "color" |
| 6163 |
@ |
| 6164 |
|
| 6165 |
@<scanning rules@>= |
| 6166 |
::@=color@> :< return COLOR; >: |
| 6167 |
@ |
| 6168 |
|
| 6169 |
Colors can be specified as a single number, preferably in hexa\-decimal |
| 6170 |
notation, giving the red, green, blue, and alpha value in a single |
| 6171 |
number. For example \.{0xFF0000FF} would be pure red, |
| 6172 |
and \.{0x00FF0080} would be transparent green. |
| 6173 |
Of course even decimal values can be used. A good example is the |
| 6174 |
value \.{0} which is equivalent to but a bit shorter than \.{0x0} or |
| 6175 |
\.{0x00000000} which describes a completely transparent black. |
| 6176 |
It is invisible because the alpha value is zero. |
| 6177 |
|
| 6178 |
Alternatively, colors can be given as a list of three or four numbers |
| 6179 |
enclosed in pointed brackets \.{<} \dots \.{>}. |
| 6180 |
If only three numbers are given, the color is opaque with an alpha |
| 6181 |
value equivalent to \.{0xFF}. Using this format |
| 6182 |
the same colors as before can be written \.{<0xFF 0 0>} (pure red), |
| 6183 |
\.{<0 0xFF 0 0x80>} (transparent green) and \.{<0 0 0 0>} (transparent black). |
| 6184 |
|
| 6185 |
The parser will put the color definition into |colors_n| |
| 6186 |
using the index |colors_i|. As we will see later, the |colors_n| |
| 6187 |
array is initialized with the colors in |colors_0| which in turn |
| 6188 |
is initialized from |color_defaults[0]|. |
| 6189 |
|colors_0| can be changed but only if that change occurs before |
| 6190 |
any other color definition. |
| 6191 |
|
| 6192 |
@<common variables@>= |
| 6193 |
ColorSet colors_0, colors_n; /* default and current color set */ |
| 6194 |
int colors_i; /* current color */ |
| 6195 |
@ |
| 6196 |
|
| 6197 |
@<initialize definitions@>= |
| 6198 |
{ int i; |
| 6199 |
for (i=0;i<sizeof(ColorSet)/4;i++) |
| 6200 |
colors_0[i]=color_defaults[0][i]; |
| 6201 |
} |
| 6202 |
@ |
| 6203 |
|
| 6204 |
@<parsing rules@>=@/ |
| 6205 |
color: START UNSIGNED UNSIGNED UNSIGNED UNSIGNED END@| |
| 6206 |
{ RNG("red",$2,0,0xFF); RNG("green",$3,0,0xFF); |
| 6207 |
RNG("blue",$4,0,0xFF); RNG("alpha",$5,0,0xFF); |
| 6208 |
colors_n[colors_i++]=($2<<24)|($3<<16)|($4<<8)|$5; |
| 6209 |
} |
| 6210 |
| START UNSIGNED UNSIGNED UNSIGNED END@| |
| 6211 |
{ RNG("red",$2,0,0xFF); RNG("green",$3,0,0xFF); |
| 6212 |
RNG("blue",$4,0,0xFF); |
| 6213 |
colors_n[colors_i++]=($2<<24)|($3<<16)|($4<<8)|0xFF; |
| 6214 |
}; |
| 6215 |
color: UNSIGNED { colors_n[colors_i++]=$1;}; |
| 6216 |
@ |
| 6217 |
|
| 6218 |
Colors are always specified in pairs: a foreground color folowed by |
| 6219 |
background color enclosed in pointed brackets \.{<} \dots \.{>} as |
| 6220 |
usual. For convenience, the background color can be omited; |
| 6221 |
in this case a completely transparent background is assumed. |
| 6222 |
|
| 6223 |
@<parsing rules@>=@/ |
| 6224 |
color_pair: START color color END |
| 6225 |
| START color END { colors_n[colors_i++]=0; }; |
| 6226 |
color_unset: { colors_i+=2;}; |
| 6227 |
@ |
| 6228 |
|
| 6229 |
A complete color set consists of six color pairs |
| 6230 |
organized in two |color_tripple|s: |
| 6231 |
the first three pairs for normal, mark, and focus text |
| 6232 |
in day mode are followed by the three pairs in night mode. |
| 6233 |
The |color_tripple| for night mode is optional; and within |
| 6234 |
a |color_tripple| all color pairs except the first one are |
| 6235 |
optional. An omited color is replaced by the |
| 6236 |
corresponding color from the color set zero. To make the replacement |
| 6237 |
process more predictable, the specification of color set zero---if |
| 6238 |
present---must come first. |
| 6239 |
If the default color set itself is redefined, an unspecified |
| 6240 |
color will not change the default color. |
| 6241 |
|
| 6242 |
To be open to future changes, color set definitions in the short format |
| 6243 |
will contain after the reference number the number of color pairs that follow. |
| 6244 |
Currently this value is always six. |
| 6245 |
|
| 6246 |
|
| 6247 |
@<parsing rules@>=@/ |
| 6248 |
color_tripple: START color_pair color_unset color_unset END |
| 6249 |
| START color_pair color_pair color_unset END |
| 6250 |
| START color_pair color_pair color_pair END |
| 6251 |
; |
| 6252 |
|
| 6253 |
color_set: color_tripple color_tripple; |
| 6254 |
color_set: color_tripple color_unset color_unset color_unset; |
| 6255 |
|
| 6256 |
def_node: start COLOR ref { HPUT8(6); color_init(); } color_set END |
| 6257 |
{ DEF($$,color_kind,$3); hput_color_def($1,$3); }; |
| 6258 |
@ |
| 6259 |
|
| 6260 |
@<put functions@>= |
| 6261 |
void color_init(void) |
| 6262 |
{ int i; |
| 6263 |
for (i=0;i<sizeof(ColorSet)/4;i++) colors_n[i]=colors_0[i]; |
| 6264 |
colors_i=0; |
| 6265 |
} |
| 6266 |
|
| 6267 |
static Tag hput_color_set(int n) |
| 6268 |
{ static bool first_color=true; |
| 6269 |
int i; |
| 6270 |
if (n==0) |
| 6271 |
{ if (first_color) |
| 6272 |
for (i=0;i<sizeof(ColorSet)/4;i++) colors_0[i]=colors_n[i]; |
| 6273 |
else |
| 6274 |
QUIT("Redefinition of color set 0 must be the first color definition"); |
| 6275 |
} |
| 6276 |
first_color=false; |
| 6277 |
HPUTX(sizeof(ColorSet)+1); |
| 6278 |
for (i=0;i<sizeof(ColorSet)/4;i++) HPUT32(colors_n[i]); |
| 6279 |
return TAG(color_kind,b000); |
| 6280 |
} |
| 6281 |
@ |
| 6282 |
|
| 6283 |
The |hput_color_def| checks if color sets zero or one need to be written. |
| 6284 |
If not, the function will reset |hpos| to undo the writing of the tag |
| 6285 |
and the number of colors in the set. |
| 6286 |
|
| 6287 |
@<put functions@>= |
| 6288 |
static bool colors_equal(ColorSet a, ColorSet b) |
| 6289 |
{ int i; |
| 6290 |
for (i=0;i<sizeof(ColorSet)/4;i++) |
| 6291 |
if (a[i]!=b[i]) return false; |
| 6292 |
return true; |
| 6293 |
} |
| 6294 |
|
| 6295 |
void hput_color_def(uint32_t pos, int n) |
| 6296 |
{ if ((n==0 && colors_equal(color_defaults[0], colors_n)) || |
| 6297 |
(n==1 && colors_equal(color_defaults[1], colors_n))) |
| 6298 |
{ hpos=hstart+pos; |
| 6299 |
return; |
| 6300 |
} |
| 6301 |
hput_tags(pos,hput_color_set(n)); |
| 6302 |
} |
| 6303 |
@ |
| 6304 |
|
| 6305 |
Compared to the definitions, the content nodes are pretty simple. |
| 6306 |
The special color number |0xFF| is reserved to indicate an |
| 6307 |
\<color off> node in the short format. |
| 6308 |
|
| 6309 |
@<parsing rules@>= |
| 6310 |
content_node: start COLOR ref END |
| 6311 |
{REF_RNG(color_kind,$3); hput_tags($1,TAG(color_kind,b000));}; |
| 6312 |
content_node: start COLOR OFF END |
| 6313 |
{ HPUT8(0xFF); hput_tags($1,TAG(color_kind,b000));}; |
| 6314 |
@ |
| 6315 |
|
| 6316 |
\vbox{\writecode\vskip -\baselineskip\getcode} |
| 6317 |
|
| 6318 |
We contine with the color content nodes: |
| 6319 |
@<cases to get content@>= |
| 6320 |
@t\1\kern1em@> |
| 6321 |
case TAG(color_kind,b000): |
| 6322 |
{ uint8_t n=HGET8;@+ |
| 6323 |
if (n==0xFF) hwritef(" off"); |
| 6324 |
else { REF(color_kind,n); @+hwrite_ref(n);@+} |
| 6325 |
} |
| 6326 |
break; |
| 6327 |
@ |
| 6328 |
|
| 6329 |
And now we turn to the color definitions: |
| 6330 |
|
| 6331 |
@<get functions@>= |
| 6332 |
void hwrite_color_pair(uint32_t f, uint32_t b) |
| 6333 |
{ hwritec('<'); |
| 6334 |
if (f==0) hwritec('0'); else hwritef("0x%08X",f); |
| 6335 |
if (b!=0) hwritef(" 0x%08X",b); |
| 6336 |
hwritec('>'); |
| 6337 |
} |
| 6338 |
|
| 6339 |
void hget_color_set(uint32_t node_pos, ColorSet cs) |
| 6340 |
{ int i,m; |
| 6341 |
for (i=0;i<sizeof(ColorSet)/4;i++) |
| 6342 |
HGET32(cs[i]); |
| 6343 |
for(m=0;m<2;m++) |
| 6344 |
{ uint32_t *c, *d; |
| 6345 |
bool diff_high, diff_focus; |
| 6346 |
if (m==0) |
| 6347 |
{ c=cs; d=color_defaults[0]; } |
| 6348 |
else |
| 6349 |
{ c=NIGHT(cs); d=NIGHT(color_defaults[0]); |
| 6350 |
if (memcmp(c,d,sizeof(ColorSet)/2)==0) |
| 6351 |
return; |
| 6352 |
} |
| 6353 |
hwrite_start(); |
| 6354 |
diff_high=FG(HIGH(c))!=FG(HIGH(d))|| BG(HIGH(c))!=BG(HIGH(d)); |
| 6355 |
diff_focus=FG(FOCUS(c))!=FG(FOCUS(d))||BG(FOCUS(c))!=BG(FOCUS(d)); |
| 6356 |
hwrite_color_pair(FG(c),BG(c)); |
| 6357 |
if (diff_high || diff_focus) |
| 6358 |
{ hwritec(' '); hwrite_color_pair(FG(HIGH(c)),BG(HIGH(c)));} |
| 6359 |
if (diff_focus) |
| 6360 |
{ hwritec(' '); hwrite_color_pair(FG(FOCUS(c)),BG(FOCUS(c)));} |
| 6361 |
hwrite_end(); |
| 6362 |
} |
| 6363 |
} |
| 6364 |
@ |
| 6365 |
|
| 6366 |
@<cases to get definitions for |color_kind|@>= |
| 6367 |
case b000: |
| 6368 |
{ int k; |
| 6369 |
ColorSet c; |
| 6370 |
static bool first_color=true; |
| 6371 |
k=HGET8; |
| 6372 |
if (k<6) |
| 6373 |
QUIT("Definition %d of color set needs 6 color pairs only %d given\n",n,k); |
| 6374 |
hget_color_set(node_pos,c); |
| 6375 |
if (n==0) |
| 6376 |
{ if (!first_color) |
| 6377 |
QUIT("Definition of color set zero must be first"); |
| 6378 |
memcpy(&color_defaults[0],&c,sizeof(ColorSet)); |
| 6379 |
} |
| 6380 |
first_color=false; |
| 6381 |
} |
| 6382 |
break; |
| 6383 |
@ |
| 6384 |
|
| 6385 |
\subsection{Rotation} |
| 6386 |
When it comes to rotation, there is a big difference between printed books and |
| 6387 |
computer displays. For example, if a book contains a table that is rotated |
| 6388 |
to fill a page in landscape mode, the reader can rotate the book and read |
| 6389 |
the table. If you are looking at the same page displayed on a |
| 6390 |
big computer monitor, you will most likely not turn the whole monitor. |
| 6391 |
Instead your viewing application will be able to perform the rotation for you |
| 6392 |
before displaying the page. A smart phone, on the other hand, is |
| 6393 |
easy to turn. But very likely, it will try to be smart and rerenders the |
| 6394 |
content on the display to keep the same orientation. |
| 6395 |
|
| 6396 |
Occasionaly, however, rotation of text is a desirable feature. For example, |
| 6397 |
if a table has lots of tall columns with lenghty column headers. It might be |
| 6398 |
usefull to rotate the column headers in order to keep the column width within |
| 6399 |
reasonable limits. |
| 6400 |
|
| 6401 |
A simple solution therefore would be optional parameters for boxes |
| 6402 |
specifying center and angle for rotating the box. |
| 6403 |
|
| 6404 |
|
| 6405 |
\subsection{Unknown Extensions} |
| 6406 |
Starting with the inclusion in the \TeX\ Live 2022 distribution, the \HINT\ file format |
| 6407 |
became accessible to a wider audience which brought the constant rewrite and upgrade cycle |
| 6408 |
to a sudden halt. Except for bug fixes, pretty much nothing happened for a about a year. |
| 6409 |
When the \TeX\ Live 2023 distribution started to appear on the horizon, one extension |
| 6410 |
that I had on my wish-list already for a long time---the support of \TeX's |
| 6411 |
\\{vtop} boxes---was definitely due for implementation. |
| 6412 |
Adding new tag bytes to the specification of the short file format will, however, |
| 6413 |
invalidate all \HINT\ file viewers and requires everybody to upgrade the viewing |
| 6414 |
application. Because the \HINT\ file format is still in its infancy, more |
| 6415 |
such additions are to be expected and the new version 2.0 file format needs a way to handle |
| 6416 |
such yet unknown extensions gracefuly. For this purpose the definition section |
| 6417 |
now may specify additional entries for the |hnode_size| array. |
| 6418 |
All \HINT\ file viewers starting with version 2.0 will use these entries to skip |
| 6419 |
unknown nodes and display the remaining content of \HINT\ files. |
| 6420 |
|
| 6421 |
\readcode |
| 6422 |
In the long format, unknown nodes, whether in the definition or the content section, |
| 6423 |
start with the keyword \\{unknown}. |
| 6424 |
|
| 6425 |
@s UNKNOWN symbol |
| 6426 |
|
| 6427 |
@<symbols@>= |
| 6428 |
%token UNKNOWN "unknown" |
| 6429 |
@ |
| 6430 |
|
| 6431 |
@<scanning rules@>= |
| 6432 |
::@=unknown@> :< return UNKNOWN; >: |
| 6433 |
@ |
| 6434 |
|
| 6435 |
In the definition section, the keyword is followed by the tag and the length of the initial |
| 6436 |
part of the node (not counting the start byte), after which follows optionaly the number of trailing nodes embedded |
| 6437 |
in the unknown node. There is no need for a maximum value, because the information |
| 6438 |
is stored directly in the |hnode_size| array. |
| 6439 |
|
| 6440 |
@<parsing rules@>= |
| 6441 |
def_node: start UNKNOWN UNSIGNED UNSIGNED END { hput_tags($1,hput_unknown_def($3,$4,0));@+} |
| 6442 |
| start UNKNOWN UNSIGNED UNSIGNED UNSIGNED END { hput_tags($1,hput_unknown_def($3,$4,$5));@+}; |
| 6443 |
@ |
| 6444 |
|
| 6445 |
In the content section, the keyword is followed by the tag value, the remaining byte values |
| 6446 |
belonging to the initial part and the nodes belonging to the trailing part. |
| 6447 |
The end byte, which is equal to the start byte, is omited from the long format. |
| 6448 |
|
| 6449 |
@<symbols@>= |
| 6450 |
%type <u> unknown_bytes |
| 6451 |
%type <u> unknown_nodes |
| 6452 |
@ |
| 6453 |
@<parsing rules@>= |
| 6454 |
content_node: start UNKNOWN UNSIGNED unknown_bytes unknown_nodes END { hput_tags($1,hput_unknown($1,$3,$4,$5));@+} |
| 6455 |
unknown_bytes: {$$=0;} | unknown_bytes UNSIGNED { RNG("byte",$2,0,0xFF); HPUT8($2); $$=$1+1; }; |
| 6456 |
unknown_node: content_node | xdimen_node | list | named_param_list; |
| 6457 |
unknown_nodes: {$$=0;} | unknown_nodes unknown_node { RNG("unknown subnodes",$1,0,3); $$=$1+1; }; |
| 6458 |
@ |
| 6459 |
|
| 6460 |
\putcode |
| 6461 |
In the short format, definitions for unknown nodes are marked with |TAG(unknown_kind,b100)|. |
| 6462 |
This tag is not used elsewhere (see also page \pageref{FC}). |
| 6463 |
We do not check for multiple definitions of the same |
| 6464 |
tag. But only the first of them is considered valid. |
| 6465 |
After the start byte follows the unknown tag and the corresponding entry in the |hnode_size| array. |
| 6466 |
|
| 6467 |
@<put functions@>= |
| 6468 |
uint32_t hput_unknown_def(uint32_t t, uint32_t b, uint32_t n) |
| 6469 |
{ if (n==0) |
| 6470 |
{ RNG("unknown tag",t,TAG(param_kind,7)+1,TAG(int_kind,0)-1); |
| 6471 |
RNG("unknown initial bytes",b,0,0x7F-2); |
| 6472 |
HPUT8(t); |
| 6473 |
HPUT8(b+2); /* adding start and end byte */ |
| 6474 |
if (hnode_size[t]==0) |
| 6475 |
{ hnode_size[t]=NODE_SIZE(b,0); |
| 6476 |
DBG(DBGTAGS,"Defining unknown node size %d,%d for tag 0x%x\n",b,n,t); |
| 6477 |
} |
| 6478 |
} |
| 6479 |
else |
| 6480 |
{ int i; |
| 6481 |
RNG("unknown tag",t,TAG(param_kind,7)+1,TAG(int_kind,0)-1); |
| 6482 |
RNG("unknown initial bytes",b,0,0x1F-1); |
| 6483 |
RNG("unknown trailing nodes",n,1,4); |
| 6484 |
HPUT8(t); |
| 6485 |
i=NODE_SIZE(b,n); |
| 6486 |
HPUT8(i); |
| 6487 |
if (hnode_size[t]==0) |
| 6488 |
{ hnode_size[t]=i; |
| 6489 |
DBG(DBGTAGS,"Defining unknown node size %d,%d for tag 0x%x\n",b,n,t); |
| 6490 |
} |
| 6491 |
} |
| 6492 |
return TAG(unknown_kind,b100); |
| 6493 |
} |
| 6494 |
@ |
| 6495 |
|
| 6496 |
In the content section, the unknown nodes are of course marked with their unknown tag. |
| 6497 |
@<put functions@>= |
| 6498 |
Tag hput_unknown(uint32_t pos,uint32_t t, uint32_t b, uint32_t n) |
| 6499 |
{ int s; |
| 6500 |
RNG("unknown tag",t,TAG(param_kind,7)+1,TAG(int_kind,0)-1); |
| 6501 |
if (n==0) |
| 6502 |
{ RNG("unknown initial bytes",b,0,0x7F-2); |
| 6503 |
s=NODE_SIZE(b,0); |
| 6504 |
} |
| 6505 |
else |
| 6506 |
{ RNG("unknown initial bytes",b,0,0x1F-2); |
| 6507 |
RNG("unknown trailing nodes",n,1,4); |
| 6508 |
s=NODE_SIZE(b,n); |
| 6509 |
} |
| 6510 |
DBG(DBGTAGS,"Adding unknown node size %d,%d tag 0x%x at 0x%x\n",b,n,t,pos); |
| 6511 |
if (hnode_size[t]!=s) |
| 6512 |
QUIT(@["Size %d of unknown node [%s,%d] at " SIZE_F " does not match %d\n"@],s,NAME(t),INFO(t),hpos-hstart,hnode_size[t]); |
| 6513 |
return (Tag)t; |
| 6514 |
} |
| 6515 |
@ |
| 6516 |
|
| 6517 |
\goodbreak |
| 6518 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 6519 |
|
| 6520 |
@<get functions@>= |
| 6521 |
void hget_unknown_def(void) |
| 6522 |
{ Tag t; signed char i, b=0, n=0; |
| 6523 |
t=HGET8; |
| 6524 |
i=HGET8; |
| 6525 |
if (i==0) |
| 6526 |
QUIT("Zero not allowed for unknown node size at 0x%x\n",(uint32_t)(hpos-hstart-2)); |
| 6527 |
hwrite_start(); @+hwritef("unknown"); |
| 6528 |
b=NODE_HEAD(i); n=NODE_TAIL(i); |
| 6529 |
if (n==0) |
| 6530 |
hwritef(" 0x%02X %d",t,b); |
| 6531 |
else |
| 6532 |
hwritef(" 0x%02X %d %d",t,b, n); |
| 6533 |
if (hnode_size[t]==0) |
| 6534 |
{ hnode_size[t]=i; |
| 6535 |
DBG(DBGTAGS,"Defining node size %d,%d for tag 0x%x\n",b,n,t); |
| 6536 |
} |
| 6537 |
hwrite_end(); |
| 6538 |
} |
| 6539 |
@ |
| 6540 |
|
| 6541 |
The |hget_unknown| funktion tries to process a unknown node with the help of |
| 6542 |
an entry in the |hnode_size| array. The definition section can be used to provide |
| 6543 |
this extra information. If successful the function returns 1 else 0. |
| 6544 |
|
| 6545 |
@<get functions@>= |
| 6546 |
int hget_unknown(Tag a) |
| 6547 |
{ int b, n; |
| 6548 |
int8_t s; |
| 6549 |
s = hnode_size[a]; |
| 6550 |
DBG(DBGTAGS,"Trying unknown tag 0x%x at 0x%x\n",a,(uint32_t)(hpos-hstart-1)); |
| 6551 |
if (s==0) return 0; |
| 6552 |
b=NODE_HEAD(s); n=NODE_TAIL(s); |
| 6553 |
DBG(DBGTAGS,"Trying unknown node size %d %d\n",b,n); |
| 6554 |
hwritef("unknown 0x%02X", a); |
| 6555 |
while (b>0) { a=HGET8; hwritef(" 0x%02X",a); b--;} |
| 6556 |
while (n>0) { |
| 6557 |
a=*hpos; |
| 6558 |
if (KIND(a)==xdimen_kind) { Xdimen x; hget_xdimen_node(&x); @+hwrite_xdimen_node(&x); } |
| 6559 |
else if (KIND(a)==param_kind) { List l; @+hget_param_list(&l);@+ hwrite_named_param_list(&l); @+} |
| 6560 |
else if (KIND(a)<=list_kind) { List l; @+hget_list(&l);@+ hwrite_list(&l); @+} |
| 6561 |
else hget_content_node(); |
| 6562 |
n--; } |
| 6563 |
return 1; |
| 6564 |
} |
| 6565 |
@ |
| 6566 |
|
| 6567 |
|
| 6568 |
|
| 6569 |
\section{Replacing \TeX's Page Building Process} |
| 6570 |
|
| 6571 |
\TeX\ uses an output\index{output routine} routine to finalize the page. It uses the accumulated material |
| 6572 |
from the page builder, found in {\tt box255}, attaches headers, footers, and floating material |
| 6573 |
like figures, tables, and footnotes. The latter material is specified by insert nodes |
| 6574 |
while headers and footers are often constructed using mark nodes. |
| 6575 |
Running an output routine requires the full power of the \TeX\ engine and will not be |
| 6576 |
part of the \HINT\ viewer. Therefore, \HINT\ replaces output routines by page templates\index{template}. |
| 6577 |
As \TeX\ can use different output routines for different parts of a book---for example |
| 6578 |
the index might use a different output routine than the main body of text---\HINT\ |
| 6579 |
will allow multiple page templates. To support different output media, the page |
| 6580 |
templates will be named and a suitable user interface may offer the user a selection |
| 6581 |
of possible page layouts. In this way, the page layout remains in the hands of the |
| 6582 |
book designer, and the user has still the opportunity to pick a layout that best fits |
| 6583 |
the display device. |
| 6584 |
|
| 6585 |
\TeX\ uses insertions to describe floating content that is not necessarily displayed |
| 6586 |
where it is specified. Three examples may illustrate this: |
| 6587 |
\itemize |
| 6588 |
\item Footnotes\footnote*{Like this one.} are specified in the middle of the text but are displayed at the |
| 6589 |
bottom of the page. Several |
| 6590 |
footnotes\index{footnote} on the same page are collected and displayed together. The |
| 6591 |
page layout may specify a short rule to separate footnotes from the |
| 6592 |
main text, and if there are many short footnotes, it may use two columns |
| 6593 |
to display them. In extreme cases, the page layout may demand a long |
| 6594 |
footnote to be split and continued on the next page. |
| 6595 |
|
| 6596 |
\item Illustrations\index{illustration} may be displayed exactly where specified if there is enough |
| 6597 |
room on the page, but may move to the top of the page, the bottom of the page, |
| 6598 |
the top of next page, or a separate page at the end of the chapter. |
| 6599 |
|
| 6600 |
\item Margin notes\index{margin note} are displayed in the margin on the same page starting at the top |
| 6601 |
of the margin. |
| 6602 |
\enditemize |
| 6603 |
|
| 6604 |
\HINT\ uses page templates and content streams to achieve similar effects. |
| 6605 |
But before I describe the page building\index{page building} mechanisms of \HINT, let me summarize \TeX's page builder. |
| 6606 |
|
| 6607 |
\TeX's page builder ignores leading glue\index{glue}, kern\index{kern}, and penalty\index{penalty} nodes until the first |
| 6608 |
box\index{box} or rule\index{rule} is encountered; |
| 6609 |
whatsit\index{whatsit node} nodes do not really contribute anything to a page; mark\index{mark node} nodes are recorded for later use. |
| 6610 |
Once the first box, rule, or insert\index{insert node} arrives, \TeX\ makes copies of all parameters |
| 6611 |
that influence the page building process and uses these copies. These parameters |
| 6612 |
are the |page_goal| and the |page_max_depth|. Further, the variables |
| 6613 |
|page_total|, |page_shrink|, |page_stretch|, |page_depth|, |
| 6614 |
and {\it insert\_pe\-nal\-ties\/} are initialized to zero. |
| 6615 |
The top skip\index{top skip} adjustment is made |
| 6616 |
when the first box or rule arrives---possibly after an insert. |
| 6617 |
|
| 6618 |
Now the page builder accumulates material: normal material goes into {\tt box255}\index{box 255} and will change |page_total|, |page_shrink|, |
| 6619 |
|page_stretch|, and |page_depth|. The latter is adjusted so that |
| 6620 |
is does not exceed |page_max_depth|. |
| 6621 |
|
| 6622 |
The handling of inserts\index{insert node} is more complex. |
| 6623 |
\TeX\ creates an insert class using \.{newinsert}. This reserves a number $n$ |
| 6624 |
and four registers: {\tt box\hair$n$} for the inserted material, {\tt count\hair$n$} for the |
| 6625 |
magnification factor $f$, {\tt dimen\hair$n$} for the maximum size per page $d$, and {\tt skip\hair$n$} for the |
| 6626 |
extra space needed on a page if there are any insertions of class $n$. |
| 6627 |
|
| 6628 |
For example plain \TeX\ allocates $n=254$ for footnotes\index{footnote} and sets |
| 6629 |
{\tt count254} to~$1000$, {\tt dimen254} to 8in, and {\tt skip254} to {\tt \BS bigskipamount}. |
| 6630 |
|
| 6631 |
An insertion node will specify the insertion class $n$, some vertical material, |
| 6632 |
its natural height plus depth $x$, a {\it split\-\_top\-\_skip}, a {\it split\-\_max\_depth}, |
| 6633 |
and a {\it floa\-ting\-\_pe\-nal\-ty}. |
| 6634 |
|
| 6635 |
|
| 6636 |
Now assume that an insert node with subtype 254 arrives at the page builder. |
| 6637 |
If this is the first such insert, \TeX\ will decrease the |page_goal| |
| 6638 |
by the width of skip254 and adds its stretchability and shrinkability |
| 6639 |
to the total stretchability and shrinkability of the page. Later, |
| 6640 |
the output routine will add some space and the footnote rule to fill just that |
| 6641 |
much space and add just that much shrinkability and stretchability to the page. |
| 6642 |
Then \TeX\ will normally add the vertical material in the insert node to |
| 6643 |
box254 and decrease the |page_goal| by $x\times f/1000$. |
| 6644 |
|
| 6645 |
Special processing is required if \TeX\ detects that there is not enough space on |
| 6646 |
the current page to accommodate the complete insertion. |
| 6647 |
If already a previous insert did not fit on the page, simply the |floating_penalty| |
| 6648 |
as given in the insert node is added to the total |insert_penalties|. |
| 6649 |
Otherwise \TeX\ will test that the total natural height plus depth of box254 |
| 6650 |
including $x$ does not exceed the maximum size $d$ and that the |
| 6651 |
$|page_total| + |page_depth| + x\times f/1000 - |page_shrink| \le |page_goal|$. |
| 6652 |
If one of these tests fails, the current insertion |
| 6653 |
is split in such a way as to make the size of the remaining insertions just pass the tests |
| 6654 |
just stated. |
| 6655 |
|
| 6656 |
Whenever a glue node, or penalty node, or a kern node that is followed by glue arrives |
| 6657 |
at the page builder, it rates the current position as a possible end of the page based on |
| 6658 |
the shrinkability of the page and the difference between |page_total| and |page_goal|. |
| 6659 |
As the page fills, the page breaks tend to become better and better until the |
| 6660 |
page starts to get overfull and the page breaks get worse and worse until |
| 6661 |
they reach the point where they become |awful_bad|. At that point, |
| 6662 |
the page builder returns to the best page break found so far and fires up the |
| 6663 |
output routine. |
| 6664 |
|
| 6665 |
Let's look next at the problems that show up when implementing a replacement mechanism for \HINT. |
| 6666 |
|
| 6667 |
\enumerate |
| 6668 |
\item |
| 6669 |
An insertion node can not always specify its height $x$ because insertions may contain paragraphs that need |
| 6670 |
to be broken in lines and the height of a paragraph depends in some non obvious way on |
| 6671 |
its width. |
| 6672 |
|
| 6673 |
\item |
| 6674 |
Before the viewer can compute the height $x$, it needs to know the width of the insertion. Just imagine |
| 6675 |
displaying footnotes in two columns or setting notes in the margin. Knowing the width, it |
| 6676 |
can pack the vertical material and derive its height and depth. |
| 6677 |
|
| 6678 |
\item |
| 6679 |
\TeX's plain format provides an insert macro that checks whether there is still space |
| 6680 |
on the current page, and if so, it creates a contribution to the main text body, otherwise it |
| 6681 |
creates a topinsert. Such a decision needs to be postponed to the \HINT\ viewer. |
| 6682 |
|
| 6683 |
\item |
| 6684 |
\HINT\ has no output routines that would specify something like the space and the rule preceding the footnote. |
| 6685 |
|
| 6686 |
\item |
| 6687 |
\TeX's output routines have the ability to inspect the content of the boxes, |
| 6688 |
split them, and distribute the content over the page. |
| 6689 |
For example, the output routine for an index set in two column format might |
| 6690 |
expect a box containing index entries up to a height of $2\times\.{vsize}$. |
| 6691 |
It will split this box in the middle and display the top part in the left |
| 6692 |
column and the bottom part in the right column. With this approach, the |
| 6693 |
last page will show two partly filled columns of about equal size. |
| 6694 |
|
| 6695 |
\item |
| 6696 |
\HINT\ has no mark nodes that could be used to create page headers or footers. |
| 6697 |
Marks, like output routines, contain token lists and need the full \TeX\ interpreter |
| 6698 |
for processing them. Hence, \HINT\ does not support mark nodes. |
| 6699 |
\endenumerate |
| 6700 |
|
| 6701 |
Here now is the solution I have chosen for \HINT: |
| 6702 |
|
| 6703 |
Instead of output routines, \HINT\ will use page templates. |
| 6704 |
Page templates are basically vertical boxes with placeholders marking the |
| 6705 |
positions where the content of the box registers, filled by the page builder, |
| 6706 |
should appear. |
| 6707 |
To output the page, the viewer traverses the page template, |
| 6708 |
replaces the placeholders by the appropriate box content, and |
| 6709 |
sets the glue. Inside the page template, we can use insert nodes to act |
| 6710 |
as placeholders. |
| 6711 |
|
| 6712 |
It is only natural to treat the page's main body, the |
| 6713 |
inserts, and the marks using the same mechanism. We call this |
| 6714 |
mechanism a content stream\index{stream}. |
| 6715 |
Content streams are identified by a stream number in the range 0 to 254; |
| 6716 |
the number 255 is used to indicate an invalid stream number. |
| 6717 |
The stream number 0 is reserved for the main content stream; it is always defined. |
| 6718 |
Besides the main content stream, there are three types of streams: |
| 6719 |
\itemize |
| 6720 |
\item normal streams correspond to \TeX's inserts and accumulate content on the page, |
| 6721 |
\item first\index{first stream} streams correspond to \TeX's first marks and will contain only the first insertion of the page, |
| 6722 |
\item last\index{last stream} streams correspond to \TeX's bottom marks and will contain only the last insertion of the page, and |
| 6723 |
\item top\index{top stream} streams correspond to \TeX's top marks. Top streams are not yet implemented. |
| 6724 |
\enditemize |
| 6725 |
|
| 6726 |
Nodes from the content section are considered contributions to stream 0 except |
| 6727 |
for insert nodes which will specify the stream number explicitly. |
| 6728 |
If the stream is not defined or is not used in the current page template, its content is simply ignored. |
| 6729 |
|
| 6730 |
The page builder needs a mechanism to redirect contributions from one content |
| 6731 |
stream to another content stream based on the availability of space. |
| 6732 |
Hence a \HINT\ content stream can optionally specify a preferred stream number, |
| 6733 |
where content should go if there is still space available, a next stream number, |
| 6734 |
where content should go if the present stream has no more space available, and |
| 6735 |
a split ratio if the content is to be split between these two streams before |
| 6736 |
filling in the template. |
| 6737 |
|
| 6738 |
Various stream parameters govern the treatment of contributions to the stream |
| 6739 |
and the page building process. |
| 6740 |
|
| 6741 |
\itemize |
| 6742 |
\item The magnification factor $f$: Inserting a box of height $h$ to this stream will contribute $h\times f/1000$ |
| 6743 |
to the height of the page under construction. For example, a stream |
| 6744 |
that uses a two column format will have an $f$ value of 500; a stream |
| 6745 |
that specifies notes that will be displayed in the page margin will |
| 6746 |
have an $f$ value of zero. |
| 6747 |
|
| 6748 |
\item The height $h$: The extended dimension $h$ gives the maximum height this |
| 6749 |
stream is allowed to occupy on the current page. |
| 6750 |
To continue the previous example, a stream that will be split into two columns |
| 6751 |
will have $h=2\cdot\.{vsize}$ , and a stream that specifies |
| 6752 |
notes that will be displayed in the page margin will have |
| 6753 |
$h=1\cdot\.{vsize}$. You can restrict the amount of space occupied by |
| 6754 |
footnotes to the bottom quarter by setting the corresponding $h$ value |
| 6755 |
to $h=0.25\cdot\.{vsize}$. |
| 6756 |
|
| 6757 |
\item The depth $d$: The dimension $d$ gives the maximum depth this |
| 6758 |
stream is allowed to have after formatting. |
| 6759 |
|
| 6760 |
\item The width $w$: The extended dimension $w$ gives the width of this stream |
| 6761 |
when formatting its content. For example margin notes |
| 6762 |
should have the width of the margin less some surrounding space. |
| 6763 |
|
| 6764 |
\item The ``before'' list $b$: If there are any contributions to this |
| 6765 |
stream on the current page, the material in list $b$ |
| 6766 |
is inserted {\it before\/} the material from the stream itself. For |
| 6767 |
example, the short line that separates the footnotes from the main |
| 6768 |
page will go, together with some surrounding space, into the list~$b$. |
| 6769 |
|
| 6770 |
\item The top skip glue $g$: This glue is inserted between the material |
| 6771 |
from list $b$ and the first box of the stream, reduced |
| 6772 |
by the height of the first box. Hence it specifies the distance between |
| 6773 |
the material in $b$ and the first baseline of the stream content. |
| 6774 |
|
| 6775 |
\item The ``after'' list $a$: The list $a$ is treated like list $b$ but |
| 6776 |
its material is placed {\it after\/} the material from the stream itself. |
| 6777 |
|
| 6778 |
\item The ``preferred'' stream number $p$: If $p\ne 255$, it is the number of |
| 6779 |
the {\it preferred\/} stream. If stream $p$ has still |
| 6780 |
enough room to accommodate the current contribution, move the |
| 6781 |
contribution to stream $p$, otherwise keep it. For example, you can |
| 6782 |
move an illustration to the main content stream, provided there is |
| 6783 |
still enough space for it on the current page, by setting $p=0$. |
| 6784 |
|
| 6785 |
\item The ``next'' stream number $n$: If $n\ne 255$, it is the number of the |
| 6786 |
{\it next\/} stream. If a contribution can not be |
| 6787 |
accommodated in stream $p$ nor in the current stream, treat it as an |
| 6788 |
insertion to stream $n$. For example, you can move contributions to |
| 6789 |
the next column after the first column is full, or move illustrations |
| 6790 |
to a separate page at the end of the chapter. |
| 6791 |
|
| 6792 |
\item The split ratio\index{split ratio} $r$: If $r$ is positive, both $p$ and $n$ must |
| 6793 |
be valid stream numbers and contents is not immediately moved to stream $p$ or $n$ as described before. |
| 6794 |
Instead the content is kept in the stream itself until the current page is complete. |
| 6795 |
Then, before inserting the streams into the page template, the content of |
| 6796 |
this stream is formatted as a vertical box, the vertical box is |
| 6797 |
split into a top fraction and a bottom fraction in the ratio $r/1000$ |
| 6798 |
for the top and $(1000-r)/1000$ for the bottom, and finally the top |
| 6799 |
fraction is moved to stream $p$ and the bottom fraction to stream |
| 6800 |
$n$. You can use this feature for example to implement footnotes |
| 6801 |
arranged in two columns of about equal size. By collecting all the |
| 6802 |
footnotes in one stream and then splitting the footnotes with $r=500$ |
| 6803 |
before placing them on the page into a right and left column. Even |
| 6804 |
three or more columns can be implemented by cascades of streams using |
| 6805 |
this mechanism. |
| 6806 |
\enditemize |
| 6807 |
|
| 6808 |
\subsection{Stream Definitions} |
| 6809 |
\index{stream} |
| 6810 |
There are four types of streams: normal streams that work like \TeX's inserts; |
| 6811 |
and first, last, and top streams that work like \TeX's marks. |
| 6812 |
For the latter types, the long format uses a matching keyword and the |
| 6813 |
short format the two least significant info bits. All stream definitions |
| 6814 |
start with the stream number. |
| 6815 |
In definitions of normal streams after the number follows in this order |
| 6816 |
\itemize |
| 6817 |
\item the maximum insertion height, |
| 6818 |
\item the magnification factor, and |
| 6819 |
\item information about splitting the stream. |
| 6820 |
It consists of: a preferred stream, a next stream, and a split ratio. |
| 6821 |
An asterisk indicates a missing stream reference, in the |
| 6822 |
short format the stream number 255 serves the same purpose. |
| 6823 |
\enditemize |
| 6824 |
All stream definitions finish with |
| 6825 |
\itemize |
| 6826 |
\item the ``before'' list, |
| 6827 |
\item an extended dimension node specifying the width of the inserted material, |
| 6828 |
\item the top skip glue, |
| 6829 |
\item the ``after'' list, |
| 6830 |
\item and the total height, stretchability, and shrinkability of the material in |
| 6831 |
the ``before'' and ``after'' list. |
| 6832 |
\enditemize |
| 6833 |
|
| 6834 |
A special case is the stream definition for stream 0, the main content stream. |
| 6835 |
None of the above information is necessary for it so it is omitted. |
| 6836 |
Stream definitions, including the definition of stream 0, |
| 6837 |
occur only inside page template definitions\index{template} |
| 6838 |
where they occur twice in two different roles: |
| 6839 |
In the stream definition list, they define properties of the stream |
| 6840 |
and in the template they mark the insertion point (see section~\secref{page}). |
| 6841 |
In the latter case, stream nodes just contain the stream number. |
| 6842 |
Because a template looks like ordinary vertical material, |
| 6843 |
we like to use the same functions for parsing it. |
| 6844 |
But stream definitions are very different from stream content |
| 6845 |
nodes. To solve the problem for the long format, |
| 6846 |
the scanner will return two different tokens |
| 6847 |
when it sees the keyword ``{\tt stream}''. |
| 6848 |
In the definition section, it will return |
| 6849 |
|STREAMDEF| and in the content section |STREAM|. |
| 6850 |
The same problem is solved in the short format |
| 6851 |
by using the |b100| bit to mark a definition. |
| 6852 |
|
| 6853 |
\goodbreak |
| 6854 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 6855 |
|
| 6856 |
@s STREAM symbol |
| 6857 |
@s STREAMDEF symbol |
| 6858 |
@s TOP symbol |
| 6859 |
@s FIRST symbol |
| 6860 |
@s LAST symbol |
| 6861 |
@s NOREFERENCE symbol |
| 6862 |
@s stream_type symbol |
| 6863 |
@s stream_info symbol |
| 6864 |
@s stream_split symbol |
| 6865 |
@s stream_link symbol |
| 6866 |
@s stream_def_node symbol |
| 6867 |
@s stream_ins_node symbol |
| 6868 |
@s stream_ref symbol |
| 6869 |
|
| 6870 |
|
| 6871 |
@<symbols@>= |
| 6872 |
%token STREAM "stream" |
| 6873 |
%token STREAMDEF "stream (definition)" |
| 6874 |
%token FIRST "first" |
| 6875 |
%token LAST "last" |
| 6876 |
%token TOP "top" |
| 6877 |
%token NOREFERENCE "*" |
| 6878 |
%type <info> stream_type |
| 6879 |
%type <u> stream_ref |
| 6880 |
%type <rf> stream_def_node |
| 6881 |
@ |
| 6882 |
|
| 6883 |
@<scanning rules@>= |
| 6884 |
::@=stream@> :< if (section_no==1) return STREAMDEF; else return STREAM;@+ >: |
| 6885 |
::@=first@> :< return FIRST; >: |
| 6886 |
::@=last@> :< return LAST; >: |
| 6887 |
::@=top@> :< return TOP; >: |
| 6888 |
::@=\*@> :< return NOREFERENCE; >: |
| 6889 |
@ |
| 6890 |
|
| 6891 |
@<parsing rules@>= |
| 6892 |
stream_link: ref { REF_RNG(stream_kind,$1); } | NOREFERENCE {HPUT8(255);}; |
| 6893 |
stream_split: stream_link stream_link UNSIGNED @/{RNG("split ratio",$3,0,1000); HPUT16($3);}; |
| 6894 |
stream_info: xdimen_node UNSIGNED @/{RNG("magnification factor",$2,0,1000); HPUT16($2);} stream_split; |
| 6895 |
|
| 6896 |
stream_type: stream_info {$$=0;} |FIRST {$$=1;} @+ | LAST {$$=2;} @+ |TOP {$$=3;} ; |
| 6897 |
|
| 6898 |
stream_def_node: start STREAMDEF ref stream_type @/ |
| 6899 |
list xdimen_node glue_node list glue_node END @/ |
| 6900 |
{@+ DEF($$,stream_kind,$3); @+ hput_tags($1,TAG(stream_kind,$4|b100));}; |
| 6901 |
|
| 6902 |
stream_ins_node: start STREAMDEF ref END@/ |
| 6903 |
{ RNG("Stream insertion",$3,0,max_ref[stream_kind]); hput_tags($1,TAG(stream_kind,b100));}; |
| 6904 |
|
| 6905 |
content_node: stream_def_node @+ | stream_ins_node; |
| 6906 |
@ |
| 6907 |
|
| 6908 |
|
| 6909 |
\goodbreak |
| 6910 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 6911 |
|
| 6912 |
|
| 6913 |
|
| 6914 |
@<get stream information for normal streams@>= |
| 6915 |
{ Xdimen x; |
| 6916 |
uint16_t f,r; |
| 6917 |
uint8_t n; |
| 6918 |
DBG(DBGDEF,"Defining normal stream %d at " SIZE_F "\n",*(hpos-1),hpos-hstart-2); |
| 6919 |
hget_xdimen_node(&x); @+hwrite_xdimen_node(&x); |
| 6920 |
HGET16(f); @+RNG("magnification factor",f,0,1000);@+ hwritef(" %d",f); |
| 6921 |
n=HGET8; if (n==255) hwritef(" *"); else { REF_RNG(stream_kind,n);@+hwrite_ref(n);@+} |
| 6922 |
n=HGET8; if (n==255) hwritef(" *"); else { REF_RNG(stream_kind,n);@+hwrite_ref(n);@+} |
| 6923 |
HGET16(r); RNG("split ratio",r,0,1000); hwritef(" %d",r); |
| 6924 |
} |
| 6925 |
@ |
| 6926 |
|
| 6927 |
@<get functions@>= |
| 6928 |
static bool hget_stream_def(void) |
| 6929 |
{@+ if (KIND(*hpos)!=stream_kind || !(INFO(*hpos)&b100)) |
| 6930 |
return false; |
| 6931 |
else |
| 6932 |
{ Ref df; |
| 6933 |
@<read the start byte |a|@>@; |
| 6934 |
DBG(DBGDEF,"Defining stream %d at " SIZE_F "\n",*hpos,hpos-hstart-1); |
| 6935 |
DEF(df,stream_kind,HGET8); |
| 6936 |
hwrite_start();@+hwritef("stream");@+@+hwrite_ref(df.n); |
| 6937 |
if (df.n>0) |
| 6938 |
{ Xdimen x; @+ List l; |
| 6939 |
if (INFO(a)==b100) @<get stream information for normal streams@>@; |
| 6940 |
else if (INFO(a)==b101) hwritef(" first"); |
| 6941 |
else if(INFO(a)==b110) hwritef(" last"); |
| 6942 |
else if (INFO(a)==b111) hwritef(" top"); |
| 6943 |
hget_list(&l);@+ hwrite_list(&l); |
| 6944 |
hget_xdimen_node(&x); @+hwrite_xdimen_node(&x); |
| 6945 |
hget_glue_node();@+ |
| 6946 |
hget_list(&l);@+ hwrite_list(&l);@+ |
| 6947 |
hget_glue_node(); |
| 6948 |
} |
| 6949 |
@<read and check the end byte |z|@>@; |
| 6950 |
hwrite_end(); |
| 6951 |
return true; |
| 6952 |
} |
| 6953 |
} |
| 6954 |
|
| 6955 |
@ |
| 6956 |
|
| 6957 |
When stream definitions are part of the page template, we call them |
| 6958 |
stream insertion points. |
| 6959 |
They contain only the stream reference and |
| 6960 |
are parsed by the usual content parsing functions. |
| 6961 |
|
| 6962 |
@<cases to get content@>= |
| 6963 |
@t\1\kern1em@> |
| 6964 |
case TAG(stream_kind,b100): {uint8_t n=HGET8;@+ REF_RNG(stream_kind,n); @+hwrite_ref(n); @+ break; @+} |
| 6965 |
@ |
| 6966 |
|
| 6967 |
|
| 6968 |
\subsection{Stream Content} |
| 6969 |
Stream\index{stream} nodes occur in the content section where they |
| 6970 |
must not be inside other nodes except toplevel |
| 6971 |
paragraph\index{paragraph} nodes. A normal stream node contains in this |
| 6972 |
order: the stream reference number, the optional stream parameters, |
| 6973 |
and the stream content. The content is either a vertical box or an |
| 6974 |
extended vertical box. The stream parameters consists of the |
| 6975 |
|floating_penalty|, the |split_max_depth|, and the |
| 6976 |
|split_top_skip|. The parameterlist can be given |
| 6977 |
explicitly or as a reference. |
| 6978 |
|
| 6979 |
In the short format, the info bits |b010| indicate |
| 6980 |
a normal stream content node with an explicit parameter list |
| 6981 |
and the info bits |b000| a normal stream with a parameter list reference. |
| 6982 |
|
| 6983 |
If the info bit |b001| is set, we have a content node of type top, first, |
| 6984 |
or last. In this case, the short format has instead of the parameter list |
| 6985 |
a single byte indicating the type. |
| 6986 |
These types are currently not yet implemented. |
| 6987 |
|
| 6988 |
\goodbreak |
| 6989 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 6990 |
|
| 6991 |
@s stream symbol |
| 6992 |
|
| 6993 |
@<symbols@>= |
| 6994 |
%type <info> stream |
| 6995 |
@ |
| 6996 |
|
| 6997 |
@<parsing rules@>= |
| 6998 |
stream: param_list list {$$=b010;} |
| 6999 |
| param_ref list {$$=b000;}; |
| 7000 |
content_node: start STREAM stream_ref stream END |
| 7001 |
@/{@+hput_tags($1,TAG(stream_kind,$4)); @+}; |
| 7002 |
@ |
| 7003 |
|
| 7004 |
\goodbreak |
| 7005 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 7006 |
|
| 7007 |
@<cases to get content@>= |
| 7008 |
@t\1\kern1em@> |
| 7009 |
case TAG(stream_kind,b000): HGET_STREAM(b000); @+ break; |
| 7010 |
case TAG(stream_kind,b010): HGET_STREAM(b010); @+ break; |
| 7011 |
@ |
| 7012 |
|
| 7013 |
When we read stream numbers, we relax the define before use policy. |
| 7014 |
We just check, that the stream number is in the correct range. |
| 7015 |
\goodbreak |
| 7016 |
@<get macros@>= |
| 7017 |
#define @[HGET_STREAM(I)@] @/\ |
| 7018 |
{uint8_t n=HGET8;@+ REF_RNG(stream_kind,n); @+hwrite_ref(n);@+}\ |
| 7019 |
if ((I)&b010) { List l; @+hget_param_list(&l); @+hwrite_param_list(&l); @+} \ |
| 7020 |
else HGET_REF(param_kind);\ |
| 7021 |
{ List l; @+hget_list(&l);@+ hwrite_list(&l); @+} |
| 7022 |
@ |
| 7023 |
|
| 7024 |
|
| 7025 |
|
| 7026 |
|
| 7027 |
\subsection{Page Template Definitions}\label{page} |
| 7028 |
A \HINT\ file can define multiple page templates\index{template}. Not only |
| 7029 |
might an index demand a different page layout than the main body of text, |
| 7030 |
also the front page or the chapter headings might use their own page templates. |
| 7031 |
Further, the author of a \HINT\ file might define a two column format as |
| 7032 |
an alternative to a single column format to be used if the display area |
| 7033 |
is wide enough. |
| 7034 |
|
| 7035 |
To help in selecting the right page template, page template definitions start with |
| 7036 |
a name and an optional priority\index{priority}; the default priority is 1. |
| 7037 |
The names might appear in a menu from which the user |
| 7038 |
can select a page layout that best fits her taste. |
| 7039 |
Without user interaction, the |
| 7040 |
system can pick the template with the highest priority. Of course, |
| 7041 |
a user interface might provide means to alter priorities. Future |
| 7042 |
versions might include sophisticated feature-vectors that |
| 7043 |
identify templates that are good for large or small displays, |
| 7044 |
landscape or portrait mode, etc \dots |
| 7045 |
|
| 7046 |
After the priority follows a glue node to specify the topskip glue |
| 7047 |
and the dimension of the maximum page depth, |
| 7048 |
an extended dimension to specify the page height and |
| 7049 |
an extended dimension to specify the page width. |
| 7050 |
|
| 7051 |
Then follows the main part of a page template definition: the template. |
| 7052 |
The template consists of a list of vertical material. |
| 7053 |
To construct the page, this list will be placed |
| 7054 |
into a vertical box and the glue will be set. |
| 7055 |
But of course before doing so, the viewer will |
| 7056 |
scan the list and replace all stream insertion points |
| 7057 |
by the appropriate content streams. |
| 7058 |
|
| 7059 |
Let's call the vertical box obtained this way ``the page''. |
| 7060 |
The page will fill the entire display area top to bottom and left to right. |
| 7061 |
It defines not only the appearance of the main body of text |
| 7062 |
but also the margins, the header, and the footer. |
| 7063 |
Because the \.{vsize} and \.{hsize} variables of \TeX\ are used for |
| 7064 |
the vertical and horizontal dimension of the main body of text---they |
| 7065 |
do not include the margins---the page will usually be wider than \.{hsize} |
| 7066 |
and taller than \.{vsize}. The dimensions of the page are part |
| 7067 |
of the page template. The viewer, knowing the actual dimensions |
| 7068 |
of the display area, can derive from them the actual values of \.{hsize} |
| 7069 |
and \.{vsize}. |
| 7070 |
|
| 7071 |
Stream definitions are listed after the template. |
| 7072 |
|
| 7073 |
The page template with number 0 is always defined and has priority 0. |
| 7074 |
It will display just the main content stream. It puts a small margin |
| 7075 |
of $\.{hsize}/8 -4.5\hbox{pt}$ all around it. |
| 7076 |
Given a letter size page, 8.5 inch wide, this formula yields a margin of 1 inch, |
| 7077 |
matching \TeX's plain format. The margin will be positive as long as |
| 7078 |
the page is wider than $1/2$ inch. For narrower pages, there will be no |
| 7079 |
margin at all. In general, the \HINT\ viewer will never set {\tt hsize} larger |
| 7080 |
than the width of the page and {\tt vsize} larger than its height. |
| 7081 |
|
| 7082 |
%8.5 in should give 1 inch margin 2/17 |
| 7083 |
%612pt should give 72pt margin |
| 7084 |
%72pt = 612/8-4.5pt |
| 7085 |
%This would give a positive margin starting at 36pt or 1/2 inch |
| 7086 |
|
| 7087 |
\goodbreak |
| 7088 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 7089 |
|
| 7090 |
@s PAGE symbol |
| 7091 |
@s page_priority symbol |
| 7092 |
@s page symbol |
| 7093 |
@s stream_def_list symbol |
| 7094 |
|
| 7095 |
@<symbols@>= |
| 7096 |
%token PAGE "page" |
| 7097 |
@ |
| 7098 |
|
| 7099 |
@<scanning rules@>= |
| 7100 |
::@=page@> :< return PAGE; >: |
| 7101 |
@ |
| 7102 |
|
| 7103 |
@<parsing rules@>= |
| 7104 |
page_priority: { HPUT8(1); } |
| 7105 |
| UNSIGNED { RNG("page priority",$1,0,255); HPUT8($1); }; |
| 7106 |
|
| 7107 |
stream_def_list: | stream_def_list stream_def_node; |
| 7108 |
|
| 7109 |
page: string { hput_string($1);} page_priority glue_node dimension {@+HPUT32($5);@+} |
| 7110 |
xdimen_node xdimen_node |
| 7111 |
list stream_def_list ; |
| 7112 |
@ |
| 7113 |
|
| 7114 |
\goodbreak |
| 7115 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 7116 |
@<get functions@>= |
| 7117 |
void hget_page(void) |
| 7118 |
{ char *n; uint8_t p; Xdimen x; List l; |
| 7119 |
HGET_STRING(n);@+ hwrite_string(n); |
| 7120 |
p=HGET8; @+ if (p!=1) hwritef(" %d",p); |
| 7121 |
hget_glue_node(); |
| 7122 |
hget_dimen(TAG(dimen_kind,b001)); |
| 7123 |
hget_xdimen_node(&x); @+hwrite_xdimen_node(&x); /* page height */ |
| 7124 |
hget_xdimen_node(&x); @+hwrite_xdimen_node(&x); /* page width */ |
| 7125 |
hget_list(&l);@+ hwrite_list(&l); |
| 7126 |
while (hget_stream_def()) continue; |
| 7127 |
} |
| 7128 |
@ |
| 7129 |
|
| 7130 |
\subsection{Page Ranges}\label{range}\index{page range} |
| 7131 |
Not every template\index{template} is necessarily valid for the entire content |
| 7132 |
section. A page range specifies a start position $a$ and an end |
| 7133 |
position $b$ in the content section and the page template is valid if |
| 7134 |
the start position $p$ of the page is within that range: $a\le p < b$. |
| 7135 |
If paging backward this definition might cause problems because the |
| 7136 |
start position of the page is known only after the page has been |
| 7137 |
build. In this case, the viewer might choose a page template based on |
| 7138 |
the position at the bottom of the page. |
| 7139 |
If it turns out that this ``bottom template'' |
| 7140 |
is no longer valid when the page builder has found the start of the |
| 7141 |
page, the viewer might display the page anyway with the bottom |
| 7142 |
template, it might just display the page with the new ``top |
| 7143 |
template'', or rerun the whole page building process using this time |
| 7144 |
the ``top template''. Neither of these alternatives is guaranteed to |
| 7145 |
produce a perfect result because changing the page template might |
| 7146 |
change the amount of material that fits on the page. A good page |
| 7147 |
template design should take this into account. |
| 7148 |
|
| 7149 |
The representation of page ranges differs significantly for the short |
| 7150 |
format and the long format. The short format will include a list of page |
| 7151 |
ranges in the definition section which consist of a page template number, |
| 7152 |
a start position, and an end position. In the long format, the start |
| 7153 |
and end position of a page |
| 7154 |
range is marked with a page range node switching the availability of a |
| 7155 |
page template on and off. Such a page range node must be a top level node. |
| 7156 |
It is an error, to switch a page template |
| 7157 |
off that was not switched on, or to switch a page template on that was |
| 7158 |
already switched on. It is permissible to omit switching off a page |
| 7159 |
template at the very end of the content section. |
| 7160 |
|
| 7161 |
While we parse a long format \HINT\ file, we store page ranges and generate |
| 7162 |
the short format after reaching the end of the content section. |
| 7163 |
While we parse a short format \HINT\ file, |
| 7164 |
we check at the end of each top level node whether we should insert a |
| 7165 |
page range node into the output. |
| 7166 |
For the \.{shrink} program, it is best |
| 7167 |
to store the start and end positions of all page ranges |
| 7168 |
in an array sorted by the position\footnote*{For a \HINT\ viewer, |
| 7169 |
a data structure which allows fast retrieval of all |
| 7170 |
valid page templates for a given position is needed.}. |
| 7171 |
To check the restrictions on the switching of page templates, we |
| 7172 |
maintain for every page template an index into the range array |
| 7173 |
which identifies the position where the template was switched on. |
| 7174 |
A zero value instead of an index will identify templates that |
| 7175 |
are currently invalid. When switching a range off again, we |
| 7176 |
link the two array entries using this index. These links |
| 7177 |
are useful when producing the range nodes in short format. |
| 7178 |
|
| 7179 |
A range node in short format contains the template number, the |
| 7180 |
start position and the end position. |
| 7181 |
A zero start position |
| 7182 |
is not stored, the info bit |b100| indicates a nonzero start position. |
| 7183 |
An end position equal to |HINT_NO_POS| is not stored, |
| 7184 |
the info bit |b010| indicates a smaller end position. |
| 7185 |
The info bit |b001| indicates that positions are stored using 2 byte |
| 7186 |
otherwise 4 byte are used for the positions. |
| 7187 |
|
| 7188 |
@<hint types@>= |
| 7189 |
typedef |
| 7190 |
struct {@+uint8_t pg; @+uint32_t pos; @+ bool on; @+int link;@+} RangePos; |
| 7191 |
@ |
| 7192 |
|
| 7193 |
@<common variables@>= |
| 7194 |
RangePos *range_pos; |
| 7195 |
int next_range=1, max_range; |
| 7196 |
int *page_on; |
| 7197 |
@ |
| 7198 |
|
| 7199 |
@<initialize definitions@>= |
| 7200 |
ALLOCATE(page_on,max_ref[page_kind]+1,int); |
| 7201 |
ALLOCATE(range_pos,2*(max_ref[range_kind]+1),RangePos); |
| 7202 |
@ |
| 7203 |
|
| 7204 |
@<hint macros@>= |
| 7205 |
#define @[ALLOCATE(R,S,T)@] @/((R)=@[(T *)calloc((S),sizeof(T)),\ |
| 7206 |
(((R)==NULL)?QUIT("Out of memory for " #R):(void)0)) |
| 7207 |
#define @[REALLOCATE(R,S,T)@] @/((R)=@[(T *)realloc((R),(S)*sizeof(T)),\ |
| 7208 |
(((R)==NULL)?QUIT("Out of memory for " #R):(void)0)) |
| 7209 |
@ |
| 7210 |
|
| 7211 |
\readcode |
| 7212 |
@s RANGE symbol |
| 7213 |
@<symbols@>= |
| 7214 |
%token RANGE "range" |
| 7215 |
@ |
| 7216 |
|
| 7217 |
@<scanning rules@>= |
| 7218 |
::@=range@> :< return RANGE; >: |
| 7219 |
@ |
| 7220 |
@<parsing rules@>= |
| 7221 |
|
| 7222 |
content_node: START RANGE REFERENCE ON END @/{ REF(page_kind,$3);hput_range($3,true);} |
| 7223 |
| START RANGE REFERENCE OFF END @/{ REF(page_kind,$3);hput_range($3,false);}; |
| 7224 |
@ |
| 7225 |
|
| 7226 |
|
| 7227 |
\writecode |
| 7228 |
@<write functions@>= |
| 7229 |
void hwrite_range(void) /* called in |hwrite_end| */ |
| 7230 |
{ uint32_t p=hpos-hstart; |
| 7231 |
DBG(DBGRANGE,"Range check at pos 0x%x next at 0x%x\n",p,range_pos[next_range].pos); |
| 7232 |
while (next_range<max_range && range_pos[next_range].pos <= p) |
| 7233 |
{ hwrite_start(); |
| 7234 |
hwritef("range *%d ",range_pos[next_range].pg); |
| 7235 |
if (range_pos[next_range].on) hwritef("on"); else hwritef("off"); |
| 7236 |
nesting--; @+hwritec('>'); /* avoid a recursive call to |hwrite_end| */ |
| 7237 |
next_range++; |
| 7238 |
} |
| 7239 |
} |
| 7240 |
@ |
| 7241 |
|
| 7242 |
\getcode |
| 7243 |
@<get functions@>= |
| 7244 |
void hget_range(Info info, uint8_t pg) |
| 7245 |
{ uint32_t from, to; |
| 7246 |
REF(page_kind,pg); |
| 7247 |
REF(range_kind,(next_range-1)/2); |
| 7248 |
if (info&b100) @+ |
| 7249 |
{ @+ if (info&b001) HGET32(from); @+else HGET16(from); @+} |
| 7250 |
else from=0; |
| 7251 |
if (info&b010) @+ |
| 7252 |
{ @+if (info&b001) HGET32(to); @+else HGET16(to); @+} |
| 7253 |
else to=HINT_NO_POS; |
| 7254 |
range_pos[next_range].pg=pg; |
| 7255 |
range_pos[next_range].on=true; |
| 7256 |
range_pos[next_range].pos=from; |
| 7257 |
DBG(DBGRANGE,"Range *%d from 0x%x\n",pg,from); |
| 7258 |
DBG(DBGRANGE,"Range *%d to 0x%x\n",pg,to); |
| 7259 |
next_range++; |
| 7260 |
if (to!=HINT_NO_POS) @/ |
| 7261 |
{ range_pos[next_range].pg=pg; |
| 7262 |
range_pos[next_range].on=false; |
| 7263 |
range_pos[next_range].pos=to; |
| 7264 |
next_range++; |
| 7265 |
} |
| 7266 |
} |
| 7267 |
@ |
| 7268 |
|
| 7269 |
@<write functions@>= |
| 7270 |
void hsort_ranges(void) /* simple insert sort by position */ |
| 7271 |
{ int i; |
| 7272 |
DBG(DBGRANGE,"Range sorting %d positions\n",next_range-1); |
| 7273 |
for(i=3; i<next_range; i++)@/ |
| 7274 |
{ int j = i-1; |
| 7275 |
if (range_pos[i].pos < range_pos[j].pos) @/ |
| 7276 |
{ RangePos t; |
| 7277 |
t= range_pos[i]; |
| 7278 |
do { |
| 7279 |
range_pos[j+1] = range_pos[j]; |
| 7280 |
j--; |
| 7281 |
} while (range_pos[i].pos < range_pos[j].pos); |
| 7282 |
range_pos[j+1] = t; |
| 7283 |
} |
| 7284 |
} |
| 7285 |
max_range=next_range; @+next_range=1; /* prepare for |hwrite_range| */ |
| 7286 |
} |
| 7287 |
@ |
| 7288 |
|
| 7289 |
\putcode |
| 7290 |
@<put functions@>= |
| 7291 |
void hput_range(uint8_t pg, bool on) |
| 7292 |
{ if (((next_range-1)/2)>max_ref[range_kind]) |
| 7293 |
QUIT("Page range %d > %d",(next_range-1)/2,max_ref[range_kind]); |
| 7294 |
if (on && page_on[pg]!=0) |
| 7295 |
QUIT(@["Template %d is switched on at 0x%x and " SIZE_F@],@| |
| 7296 |
pg, range_pos[page_on[pg]].pos, hpos-hstart); |
| 7297 |
else if (!on && page_on[pg]==0) |
| 7298 |
QUIT(@["Template %d is switched off at " SIZE_F " but was not on"@],@| |
| 7299 |
pg, hpos-hstart); |
| 7300 |
DBG(DBGRANGE,@["Range *%d %s at " SIZE_F "\n"@],pg,on?"on":"off",hpos-hstart); |
| 7301 |
range_pos[next_range].pg=pg; |
| 7302 |
range_pos[next_range].pos=hpos-hstart; |
| 7303 |
range_pos[next_range].on=on; |
| 7304 |
if (on) page_on[pg]=next_range; |
| 7305 |
else @/{ range_pos[next_range].link =page_on[pg]; |
| 7306 |
range_pos[page_on[pg]].link=next_range; |
| 7307 |
page_on[pg]=0; } |
| 7308 |
next_range++; |
| 7309 |
} |
| 7310 |
|
| 7311 |
void hput_range_defs(void) |
| 7312 |
{ int i; |
| 7313 |
section_no=1; |
| 7314 |
hstart=dir[1].buffer; |
| 7315 |
hend=hstart+ dir[1].bsize; |
| 7316 |
hpos=hstart+dir[1].size; |
| 7317 |
for (i=1; i< next_range;i++) |
| 7318 |
if (range_pos[i].on)@/ |
| 7319 |
{ Info info=b000; |
| 7320 |
uint32_t p=hpos++-hstart; |
| 7321 |
uint32_t from, to; |
| 7322 |
HPUT8(range_pos[i].pg); |
| 7323 |
from= range_pos[i].pos; |
| 7324 |
if (range_pos[i].link!=0) to = range_pos[range_pos[i].link].pos; |
| 7325 |
else to=HINT_NO_POS; |
| 7326 |
if (from!=0) @/ |
| 7327 |
{ info=info|b100;@+ if (from>0xFFFF) @+ info=info|b001;@+} |
| 7328 |
if (to!=HINT_NO_POS) @/ |
| 7329 |
{ info=info|b010;@+ if (to>0xFFFF) info=info|b001;@+ } |
| 7330 |
if (info & b100) @/ |
| 7331 |
{ @+if (info & b001) HPUT32(from); @+else HPUT16(from); @+} |
| 7332 |
if (info & b010) @/ |
| 7333 |
{ @+if (info & b001) HPUT32(to); @+else HPUT16(to); @+} |
| 7334 |
DBG(DBGRANGE,"Range *%d from 0x%x to 0x%x\n",@|range_pos[i].pg,from, to); |
| 7335 |
hput_tags(p,TAG(range_kind,info)); |
| 7336 |
} |
| 7337 |
hput_definitions_end(); |
| 7338 |
} |
| 7339 |
@ |
| 7340 |
|
| 7341 |
|
| 7342 |
\section{File Structure}\hascode |
| 7343 |
All \HINT\ files\index{file} start with a banner\index{banner} as |
| 7344 |
described below. After that, they contain three mandatory |
| 7345 |
sections\index{section}: the directory\index{directory section} |
| 7346 |
section, the definition\index{definition section} section, and the |
| 7347 |
content\index{content section} section. Usually, further |
| 7348 |
optional\index{optional section} sections follow. In short format |
| 7349 |
files, these contain auxiliary\index{auxiliary file} files |
| 7350 |
(fonts\index{font}, images\index{image}, \dots) necessary for |
| 7351 |
rendering the content. In long format files, the directory section |
| 7352 |
will simply list the file names of the auxiliary files. |
| 7353 |
|
| 7354 |
|
| 7355 |
|
| 7356 |
\subsection{Banner} |
| 7357 |
All \HINT\ files start with a banner\index{banner}. The banner contains only |
| 7358 |
printable ASCII characters and spaces; |
| 7359 |
its end is marked with a newline character\index{newline character}. |
| 7360 |
The first four byte are the ``magic'' number by which you recognize a \HINT\ |
| 7361 |
file. It consists of the four ASCII codes `{\tt H}', `{\tt I}', `{\tt N}', |
| 7362 |
and `{\tt T}' in the long format and `{\tt h}', `{\tt i}', `{\tt n}', |
| 7363 |
and `{\tt t}' in the short format. Then follows a space, then |
| 7364 |
the version number, a dot, the sub-version number, and another |
| 7365 |
space. Both numbers are encoded as decimal ASCII strings. The |
| 7366 |
remainder of the banner is simply ignored but may be used to contain |
| 7367 |
other useful information about the file. The maximum size of the |
| 7368 |
banner is 256 byte. |
| 7369 |
@<hint macros@>= |
| 7370 |
#define MAX_BANNER 256 |
| 7371 |
@ |
| 7372 |
|
| 7373 |
\goodbreak |
| 7374 |
To check the banner, we have the function |hcheck_banner|; |
| 7375 |
it returns |true| if successful. |
| 7376 |
|
| 7377 |
@<common variables@>= |
| 7378 |
char hbanner[MAX_BANNER+1]; |
| 7379 |
int hbanner_size=0; |
| 7380 |
@ |
| 7381 |
|
| 7382 |
@<function to check the banner@>= |
| 7383 |
|
| 7384 |
bool hcheck_banner(char *magic) |
| 7385 |
{ |
| 7386 |
int v,s; |
| 7387 |
char *t; |
| 7388 |
t=hbanner; |
| 7389 |
if (strncmp(magic,hbanner,4)!=0) |
| 7390 |
{ MESSAGE("This is not a %s file\n",magic); return false; } |
| 7391 |
else t+=4; |
| 7392 |
if(hbanner[hbanner_size-1]!='\n') |
| 7393 |
{ MESSAGE("Banner exceeds maximum size=0x%x\n",MAX_BANNER); return false; } |
| 7394 |
if (*t!=' ') |
| 7395 |
{ MESSAGE("Space expected in banner after %s\n",magic); return false; } |
| 7396 |
else t++; |
| 7397 |
v=strtol(t,&t,10); |
| 7398 |
if (*t!='.') |
| 7399 |
{ MESSAGE("Dot expected in banner after HINT version number\n"); return false; } |
| 7400 |
else t++; |
| 7401 |
s=strtol(t,&t,10); |
| 7402 |
if (v!=HINT_VERSION) |
| 7403 |
{ MESSAGE("Wrong HINT version: got %d.%d, expected %d.%d\n", |
| 7404 |
v,s,HINT_VERSION,HINT_MINOR_VERSION); return false; } |
| 7405 |
#if 0 /* minor versions should be downward compatible */ |
| 7406 |
if (s<HINT_MINOR_VERSION) |
| 7407 |
{ MESSAGE("Outdated HINT minor version: got %d.%d, expected %d.%d\n", |
| 7408 |
v,s,HINT_VERSION,HINT_MINOR_VERSION); } |
| 7409 |
else |
| 7410 |
#endif |
| 7411 |
if (s>HINT_MINOR_VERSION) |
| 7412 |
{ MESSAGE("More recent HINT minor version: got %d.%d, expected %d.%d, update your application\n", |
| 7413 |
v,s,HINT_VERSION,HINT_MINOR_VERSION); } |
| 7414 |
if (*t!=' ' && *t!='\n') |
| 7415 |
{ MESSAGE("Space expected in banner after HINT minor version\n"); return false; } |
| 7416 |
LOG("%s file version " HINT_VERSION_STRING ":%s", magic, t); |
| 7417 |
DBG(DBGDIR,"banner size=0x%x\n",hbanner_size); |
| 7418 |
return true; |
| 7419 |
} |
| 7420 |
@ |
| 7421 |
|
| 7422 |
To read a short format file, we use the macro |HGET8|. It returns a single byte. |
| 7423 |
We read the banner knowing that it ends with a newline character |
| 7424 |
and is at most |MAX_BANNER| byte long. Because this is the first access to a yet unknown file, |
| 7425 |
we are very careful and make sure we do not read past the end of the file. |
| 7426 |
Checking the banner is a separate step. |
| 7427 |
|
| 7428 |
\getcode |
| 7429 |
@<get file functions@>= |
| 7430 |
void hget_banner(void) |
| 7431 |
{ hbanner_size=0; |
| 7432 |
while (hbanner_size<MAX_BANNER && hpos<hend) |
| 7433 |
{ uint8_t c=HGET8; |
| 7434 |
hbanner[hbanner_size++]=c; |
| 7435 |
if (c=='\n') break; |
| 7436 |
} |
| 7437 |
hbanner[hbanner_size]=0; |
| 7438 |
} |
| 7439 |
@ |
| 7440 |
|
| 7441 |
To read a long format file, we use the function |fgetc|. |
| 7442 |
\readcode |
| 7443 |
@<read the banner@>= |
| 7444 |
{ hbanner_size=0; |
| 7445 |
while ( hbanner_size<MAX_BANNER) |
| 7446 |
{ int c=fgetc(hin); |
| 7447 |
if (c==EOF) break; |
| 7448 |
hbanner[hbanner_size++]=c; |
| 7449 |
if (c=='\n') break; |
| 7450 |
} |
| 7451 |
hbanner[hbanner_size]=0; |
| 7452 |
} |
| 7453 |
@ |
| 7454 |
|
| 7455 |
Writing the banner to a short format file is accomplished by calling |
| 7456 |
|hput_banner| with the ``magic'' string |"hint"| as a first argument |
| 7457 |
and a (short) comment as the second argument. |
| 7458 |
\putcode |
| 7459 |
@<function to write the banner@>= |
| 7460 |
|
| 7461 |
static size_t hput_banner(char *magic, char *str) |
| 7462 |
{ size_t s=fprintf(hout,"%s " HINT_VERSION_STRING " %s\n",magic,str); |
| 7463 |
if (s>MAX_BANNER) QUIT("Banner too big"); |
| 7464 |
return s; |
| 7465 |
} |
| 7466 |
@ |
| 7467 |
|
| 7468 |
|
| 7469 |
\writecode |
| 7470 |
Writing the banner of a long format file is essentially the same as for a short |
| 7471 |
format file calling |hput_banner| with |"HINT"| as a first argument. |
| 7472 |
|
| 7473 |
\subsection{Long Format Files}\gdef\subcodetitle{Banner}% |
| 7474 |
|
| 7475 |
After reading and checking the banner, reading a long format file is |
| 7476 |
simply done by calling |yyparse|. The following rule gives the big picture: |
| 7477 |
\readcode |
| 7478 |
@s hint symbol |
| 7479 |
@s content_section symbol |
| 7480 |
|
| 7481 |
@<parsing rules@>= |
| 7482 |
hint: directory_section definition_section content_section ; |
| 7483 |
@ |
| 7484 |
|
| 7485 |
|
| 7486 |
\subsection{Short Format Files}\gdef\subcodetitle{Primitives}% |
| 7487 |
A short format\index{short format} file starts with the banner and continues |
| 7488 |
with a list of sections. Each section has a maximum size |
| 7489 |
of $2^{32}$ byte or 4GByte. This restriction ensures that positions\index{position} |
| 7490 |
inside a section can be stored as 32 bit integers, a feature that |
| 7491 |
we will need only for the so called ``content'' section, but it |
| 7492 |
is also nice for implementers to know in advance what sizes to expect. |
| 7493 |
The big picture is captured by the |put_hint| function: |
| 7494 |
|
| 7495 |
@<put functions@>= |
| 7496 |
static size_t hput_root(void); |
| 7497 |
static size_t hput_section(uint16_t n); |
| 7498 |
static size_t hput_optional_section(int i); |
| 7499 |
|
| 7500 |
size_t hput_hint(char * str) |
| 7501 |
{ size_t s; |
| 7502 |
int i; |
| 7503 |
DBG(DBGBASIC,"Writing hint output %s\n",str); |
| 7504 |
s=hput_banner("hint",str); |
| 7505 |
DBG(DBGDIR,@["Root entry at " SIZE_F "\n"@],s); |
| 7506 |
s+=hput_root(); |
| 7507 |
DBG(DBGDIR,@["Directory section at " SIZE_F "\n"@],s); |
| 7508 |
s+=hput_section(0); |
| 7509 |
DBG(DBGDIR,@["Definition section at " SIZE_F "\n"@],s); |
| 7510 |
s+=hput_section(1); |
| 7511 |
DBG(DBGDIR,@["Content section at " SIZE_F "\n"@],s); |
| 7512 |
s+=hput_section(2); |
| 7513 |
DBG(DBGDIR,@["Auxiliary sections at " SIZE_F "\n"@],s); |
| 7514 |
for (i=3; i<=max_section_no; i++)@/ |
| 7515 |
s+=hput_optional_section(i); |
| 7516 |
DBG(DBGDIR,@["Total number of bytes written " SIZE_F "\n"@],s); |
| 7517 |
return s; |
| 7518 |
} |
| 7519 |
@ |
| 7520 |
|
| 7521 |
|
| 7522 |
When we work on a section, we will have the entire section in |
| 7523 |
memory and use three variables to access it: |hstart| |
| 7524 |
points to the first byte of the section, |hend| points |
| 7525 |
to the byte after the last byte of the section, and |hpos| points to the |
| 7526 |
current position inside the section.\label{hpos} |
| 7527 |
The auxiliary variable |hpos0| contains the |hpos| value of the |
| 7528 |
last content node on nesting level zero. |
| 7529 |
|
| 7530 |
@<common variables@>= |
| 7531 |
uint8_t *hpos=NULL, *hstart=NULL, *hend=NULL, *hpos0=NULL; |
| 7532 |
@ |
| 7533 |
|
| 7534 |
There are two sets of macros that read or write binary data at the current position |
| 7535 |
and advance the stream position accordingly.\label{HPUT}\label{HGET} |
| 7536 |
|
| 7537 |
\getcode |
| 7538 |
@<shared get macros@>= |
| 7539 |
#define HGET_ERROR @/ QUIT(@["HGET overrun in section %d at " SIZE_F "\n"@],@|section_no,hpos-hstart) |
| 7540 |
#define @[HEND@] @[((hpos<=hend)?0:(HGET_ERROR,0))@] |
| 7541 |
|
| 7542 |
#define @[HGET8@] ((hpos<hend)?*(hpos++):(HGET_ERROR,0)) |
| 7543 |
#define @[HGET16(X)@] ((X)=(hpos[0]<<8)+hpos[1],hpos+=2,HEND) |
| 7544 |
#define @[HGET24(X)@] ((X)=(hpos[0]<<16)+(hpos[1]<<8)+hpos[2],hpos+=3,HEND) |
| 7545 |
#define @[HGET32(X)@] ((X)=(hpos[0]<<24)+(hpos[1]<<16)+(hpos[2]<<8)+hpos[3],hpos+=4,HEND) |
| 7546 |
#define @[HGETTAG(A)@] @[A=HGET8,DBGTAG(A,hpos-1)@] |
| 7547 |
@ |
| 7548 |
|
| 7549 |
\putcode |
| 7550 |
@<put functions@>= |
| 7551 |
void hput_error(void) |
| 7552 |
{@+if (hpos<hend) return; |
| 7553 |
QUIT(@["HPUT overrun section %d pos=" SIZE_F "\n"@],@|section_no,hpos-hstart); |
| 7554 |
} |
| 7555 |
@ |
| 7556 |
|
| 7557 |
@<put macros@>= |
| 7558 |
extern void hput_error(void); |
| 7559 |
#define @[HPUT8(X)@] (hput_error(),*(hpos++)=(X)) |
| 7560 |
#define @[HPUT16(X)@] (HPUT8(((X)>>8)&0xFF),HPUT8((X)&0xFF)) |
| 7561 |
#define @[HPUT24(X)@] (HPUT8(((X)>>16)&0xFF),HPUT8(((X)>>8)&0xFF),HPUT8((X)&0xFF)) |
| 7562 |
#define @[HPUT32(X)@] (HPUT8(((X)>>24)&0xFF),HPUT8(((X)>>16)&0xFF),HPUT8(((X)>>8)&0xFF),HPUT8((X)&0xFF)) |
| 7563 |
@ |
| 7564 |
|
| 7565 |
The above macros test for buffer overruns\index{buffer overrun}; |
| 7566 |
allocating sufficient buffer space is done separately. |
| 7567 |
|
| 7568 |
Before writing a node, we will insert a test and increase the buffer if necessary. |
| 7569 |
@<put macros@>= |
| 7570 |
void hput_increase_buffer(uint32_t n); |
| 7571 |
#define @[HPUTX(N)@] @[(((hend-hpos) < (N))? hput_increase_buffer(N):(void)0)@] |
| 7572 |
#define HPUTNODE @[HPUTX(MAX_TAG_DISTANCE)@] |
| 7573 |
#define @[HPUTTAG(K,I)@] @|@[(HPUTNODE,@+DBGTAG(TAG(K,I),hpos),@+HPUT8(TAG(K,I)))@] |
| 7574 |
@ |
| 7575 |
|
| 7576 |
Fortunately the only data types that have an unbounded size are |
| 7577 |
strings\index{string} and texts\index{text}. |
| 7578 |
For these we insert specific tests. For all other cases a relatively |
| 7579 |
small upper bound on the maximum distance between two tags can be determined. |
| 7580 |
Currently the maximum distance between tags is 26 byte as can be determined |
| 7581 |
from the |hnode_size| array described in appendix~\secref{fastforward}. |
| 7582 |
The definition below uses a slightly larger value leaving some room |
| 7583 |
for future changes in the design of the short file format. |
| 7584 |
|
| 7585 |
@<hint macros@>= |
| 7586 |
#define MAX_TAG_DISTANCE 32 |
| 7587 |
@ |
| 7588 |
|
| 7589 |
\subsection{Mapping a Short Format File to Memory} |
| 7590 |
In the following, we implement two alternatives to map a file into memory. |
| 7591 |
The first implementation, opens the file, gets its size, allocates memory, |
| 7592 |
and reads the file. The second implementation uses a call to |mmap|. |
| 7593 |
|
| 7594 |
Since modern computers with 64bit hardware have a huge address space, |
| 7595 |
using |mmap| to map the entire file into virtual memory is the most efficient way |
| 7596 |
to access a large file. ``Mapping'' is not the same as ``reading'' and it is |
| 7597 |
not the same as allocating precious memory, all that is done by the |
| 7598 |
operating system when needed. Mapping just reserves addresses. |
| 7599 |
There is one disadvantage of mapping: it typically locks the underlying file |
| 7600 |
and will not allow a separate process to modify it. This prevents using |
| 7601 |
this method for previewing a \HINT\ file while editing and recompiling it. |
| 7602 |
In this case, the first implementation, which has a copy of the file in memory, |
| 7603 |
is the better choice. To select the second implementation, define the macro |USE_MMAP|. |
| 7604 |
|
| 7605 |
The following functions map and unmap a short format input |
| 7606 |
file setting |hin_addr| to its address and |hin_size| to its size. |
| 7607 |
The value |hin_addr==NULL| indicates, that no file is open. |
| 7608 |
The variable |hin_time| is set to the time when the file was last modified. |
| 7609 |
It can be used to detect modifications of the file and reload it.\label{map} |
| 7610 |
|
| 7611 |
@<common variables@>= |
| 7612 |
char *hin_name=NULL; |
| 7613 |
uint64_t hin_size=0; |
| 7614 |
uint8_t *hin_addr=NULL; |
| 7615 |
uint64_t hin_time=0; |
| 7616 |
@ |
| 7617 |
|
| 7618 |
@<map functions@>= |
| 7619 |
#ifndef USE_MMAP |
| 7620 |
void hget_unmap(void) |
| 7621 |
{@+ if (hin_addr!=NULL) free(hin_addr); |
| 7622 |
hin_addr=NULL; |
| 7623 |
hin_size=0; |
| 7624 |
} |
| 7625 |
bool hget_map(void) |
| 7626 |
{ FILE *f; |
| 7627 |
struct stat st; |
| 7628 |
size_t s,t; |
| 7629 |
uint64_t u; |
| 7630 |
f= fopen(hin_name,"rb"); |
| 7631 |
if (f==NULL)@/ |
| 7632 |
{ MESSAGE("Unable to open file: %s\n", hin_name);@+ return false;@+ } |
| 7633 |
if (stat(hin_name,&st)<0) |
| 7634 |
{ MESSAGE("Unable to obtain file size: %s\n", hin_name); |
| 7635 |
fclose(f); |
| 7636 |
return false; |
| 7637 |
} |
| 7638 |
if (st.st_size==0) |
| 7639 |
{ MESSAGE("File %s is empty\n", hin_name); |
| 7640 |
fclose(f); |
| 7641 |
return false; |
| 7642 |
} |
| 7643 |
u=st.st_size; |
| 7644 |
if (hin_addr!=NULL) hget_unmap(); |
| 7645 |
hin_addr=malloc(u); |
| 7646 |
if (hin_addr==NULL) |
| 7647 |
{ MESSAGE("Unable to allocate 0x%"PRIx64" byte for File %s\n", u,hin_name); |
| 7648 |
fclose(f); |
| 7649 |
return 0; |
| 7650 |
} |
| 7651 |
t=0; |
| 7652 |
do{ |
| 7653 |
s=fread(hin_addr+t,1,u,f); |
| 7654 |
if (s<=0) |
| 7655 |
{ MESSAGE("Unable to read file %s\n",hin_name); |
| 7656 |
fclose(f); |
| 7657 |
free(hin_addr); |
| 7658 |
hin_addr=NULL; |
| 7659 |
return false; |
| 7660 |
} |
| 7661 |
t=t+s;@+ |
| 7662 |
u=u-s; |
| 7663 |
} while (u>0); |
| 7664 |
hin_size=st.st_size; |
| 7665 |
hin_time=st.st_mtime; |
| 7666 |
return true; |
| 7667 |
} |
| 7668 |
|
| 7669 |
#else |
| 7670 |
|
| 7671 |
#include <sys/mman.h> |
| 7672 |
|
| 7673 |
void hget_unmap(void) |
| 7674 |
{@+ munmap(hin_addr,hin_size); |
| 7675 |
hin_addr=NULL; |
| 7676 |
hin_size=0; |
| 7677 |
} |
| 7678 |
|
| 7679 |
bool hget_map(void) |
| 7680 |
{ struct stat st; |
| 7681 |
int fd; |
| 7682 |
fd = open(hin_name, O_RDONLY, 0); |
| 7683 |
if (fd<0)@/ |
| 7684 |
{ MESSAGE("Unable to open file %s\n", hin_name);@+ return false;@+ } |
| 7685 |
if (fstat(fd, &st)<0) |
| 7686 |
{ MESSAGE("Unable to get file size\n"); |
| 7687 |
close(fd); |
| 7688 |
return false; |
| 7689 |
} |
| 7690 |
if (st.st_size==0) |
| 7691 |
{ MESSAGE("File %s is empty\n",hin_name); |
| 7692 |
close(fd); |
| 7693 |
return false; |
| 7694 |
} |
| 7695 |
if (hin_addr!=NULL) hget_unmap(); |
| 7696 |
hin_size=st.st_size; |
| 7697 |
hin_time=st.st_mtime; |
| 7698 |
hin_addr= mmap(NULL,hin_size,PROT_READ,MAP_PRIVATE,fd, 0); |
| 7699 |
if (hin_addr==MAP_FAILED) |
| 7700 |
{ close(fd); |
| 7701 |
hin_addr=NULL;hin_size=0; |
| 7702 |
MESSAGE("Unable to map file into memory\n"); |
| 7703 |
return 0; |
| 7704 |
} |
| 7705 |
close(fd); |
| 7706 |
return hin_size; |
| 7707 |
} |
| 7708 |
#endif |
| 7709 |
|
| 7710 |
@ |
| 7711 |
\subsection{Compression} |
| 7712 |
The short file format offers the possibility to store sections in |
| 7713 |
compressed\index{compression} form. We use the {\tt zlib}\index{zlib+{\tt zlib}} compression library\cite{zlib}\cite{RFC1950} |
| 7714 |
to deflate\index{deflate} and inflate\index{inflate} individual sections. When one of the following |
| 7715 |
functions is called, we can get the section buffer, the buffer size |
| 7716 |
and the size actually used from the directory entry. If a section |
| 7717 |
needs to be inflated, its size after decompression is found in the |
| 7718 |
|xsize| field; if a section needs to be deflated, its size after |
| 7719 |
compression will be known after deflating it. |
| 7720 |
|
| 7721 |
@s z_stream int |
| 7722 |
|
| 7723 |
@<get file functions@>= |
| 7724 |
|
| 7725 |
static void hdecompress(uint16_t n) |
| 7726 |
{ z_stream z; /* decompression stream */ |
| 7727 |
uint8_t *buffer; |
| 7728 |
int i; |
| 7729 |
|
| 7730 |
DBG(DBGCOMPRESS,"Decompressing section %d from 0x%x to 0x%x byte\n",@|n, dir[n].size, dir[n].xsize); |
| 7731 |
z.zalloc = (alloc_func)0;@+ |
| 7732 |
z.zfree = (free_func)0;@+ |
| 7733 |
z.opaque = (voidpf)0; |
| 7734 |
z.next_in = hstart; |
| 7735 |
z.avail_in = hend-hstart; |
| 7736 |
if (inflateInit(&z)!=Z_OK) |
| 7737 |
QUIT("Unable to initialize decompression: %s",z.msg); |
| 7738 |
ALLOCATE(buffer,dir[n].xsize+MAX_TAG_DISTANCE,uint8_t); |
| 7739 |
DBG(DBGBUFFER,"Allocating output buffer size=0x%x, margin=0x%x\n",dir[n].xsize,MAX_TAG_DISTANCE); |
| 7740 |
z.next_out = buffer; |
| 7741 |
z.avail_out =dir[n].xsize+MAX_TAG_DISTANCE; |
| 7742 |
i= inflate(&z, Z_FINISH); |
| 7743 |
DBG(DBGCOMPRESS,"in: avail/total=0x%x/0x%lx "@|"out: avail/total=0x%x/0x%lx, return %d;\n",@| |
| 7744 |
z.avail_in,z.total_in, z.avail_out, z.total_out,i); |
| 7745 |
if (i!=Z_STREAM_END) |
| 7746 |
QUIT("Unable to complete decompression: %s",z.msg); |
| 7747 |
if (z.avail_in != 0) |
| 7748 |
QUIT("Decompression missed input data"); |
| 7749 |
if (z.total_out != dir[n].xsize) |
| 7750 |
QUIT("Decompression output size mismatch 0x%lx != 0x%x",z.total_out, dir[n].xsize ); |
| 7751 |
if (inflateEnd(&z)!=Z_OK) |
| 7752 |
QUIT("Unable to finalize decompression: %s",z.msg); |
| 7753 |
dir[n].buffer=buffer; |
| 7754 |
dir[n].bsize=dir[n].xsize; |
| 7755 |
hpos0=hpos=hstart=buffer; |
| 7756 |
hend=hstart+dir[n].xsize; |
| 7757 |
} |
| 7758 |
@ |
| 7759 |
|
| 7760 |
|
| 7761 |
@<put functions@>= |
| 7762 |
static void hcompress(uint16_t n) |
| 7763 |
{ z_stream z; /* compression stream */ |
| 7764 |
uint8_t *buffer; |
| 7765 |
int i; |
| 7766 |
if (dir[n].size==0) { dir[n].xsize=0;@+ return; @+} |
| 7767 |
DBG(DBGCOMPRESS,"Compressing section %d of size 0x%x\n",n, dir[n].size); |
| 7768 |
z.zalloc = (alloc_func)0;@+ |
| 7769 |
z.zfree = (free_func)0;@+ |
| 7770 |
z.opaque = (voidpf)0; |
| 7771 |
if (deflateInit(&z,Z_DEFAULT_COMPRESSION)!=Z_OK) |
| 7772 |
QUIT("Unable to initialize compression: %s",z.msg); |
| 7773 |
ALLOCATE(buffer,dir[n].size+MAX_TAG_DISTANCE,uint8_t); |
| 7774 |
z.next_out = buffer; |
| 7775 |
z.avail_out = dir[n].size+MAX_TAG_DISTANCE; |
| 7776 |
z.next_in = dir[n].buffer; |
| 7777 |
z.avail_in = dir[n].size; |
| 7778 |
i=deflate(&z, Z_FINISH); |
| 7779 |
DBG(DBGCOMPRESS,"deflate in: avail/total=0x%x/0x%lx out: avail/total=0x%x/0x%lx, return %d;\n",@| |
| 7780 |
z.avail_in,z.total_in, z.avail_out, z.total_out,i); |
| 7781 |
if (z.avail_in != 0) |
| 7782 |
QUIT("Compression missed input data"); |
| 7783 |
if (i!=Z_STREAM_END) |
| 7784 |
QUIT("Compression incomplete: %s",z.msg); |
| 7785 |
if (deflateEnd(&z)!=Z_OK) |
| 7786 |
QUIT("Unable to finalize compression: %s",z.msg); |
| 7787 |
DBG(DBGCOMPRESS,"Compressed 0x%lx byte to 0x%lx byte\n",@|z.total_in,z.total_out); |
| 7788 |
free(dir[n].buffer); |
| 7789 |
dir[n].buffer=buffer; |
| 7790 |
dir[n].bsize=dir[n].size+MAX_TAG_DISTANCE; |
| 7791 |
dir[n].xsize=dir[n].size; |
| 7792 |
dir[n].size=z.total_out; |
| 7793 |
} |
| 7794 |
@ |
| 7795 |
|
| 7796 |
|
| 7797 |
|
| 7798 |
\subsection{Reading Short Format Sections} |
| 7799 |
\gdef\subcodetitle{Sections}% |
| 7800 |
|
| 7801 |
After mapping the file at address |hin_addr| access to sections of the |
| 7802 |
file is provided by decompressing them if necessary and |
| 7803 |
setting the three pointers |hpos|, |hstart|, and |
| 7804 |
|hend|. |
| 7805 |
|
| 7806 |
To read sections of a short format input file, we use the function |hget_section|. |
| 7807 |
|
| 7808 |
\getcode |
| 7809 |
%\codesection{\getsymbol}\getindex{1}{3}{Files} |
| 7810 |
|
| 7811 |
@<get file functions@>= |
| 7812 |
void hget_section(uint16_t n) |
| 7813 |
{ DBG(DBGDIR,"Reading section %d\n",n); |
| 7814 |
RNG("Section number",n,0,max_section_no); |
| 7815 |
if (dir[n].buffer!=NULL && dir[n].xsize>0) |
| 7816 |
{ hpos0=hpos=hstart=dir[n].buffer; |
| 7817 |
hend=hstart+dir[n].xsize; |
| 7818 |
} |
| 7819 |
else |
| 7820 |
{ hpos0=hpos=hstart=hin_addr+dir[n].pos; |
| 7821 |
hend=hstart+dir[n].size; |
| 7822 |
if (dir[n].xsize>0) hdecompress(n); |
| 7823 |
} |
| 7824 |
} |
| 7825 |
@ |
| 7826 |
\subsection{Writing Short Format Sections} |
| 7827 |
\gdef\subcodetitle{Sections}% |
| 7828 |
|
| 7829 |
To write a short format file, we allocate for each of the first three sections a |
| 7830 |
suitable buffer\index{buffer}, then fill these buffers, and finally write them |
| 7831 |
out in sequential order. |
| 7832 |
|
| 7833 |
@<put functions@>= |
| 7834 |
#define BUFFER_SIZE 0x400 |
| 7835 |
void new_output_buffers(void) |
| 7836 |
{ dir[0].bsize=dir[1].bsize=dir[2].bsize=BUFFER_SIZE; |
| 7837 |
DBG(DBGBUFFER,"Allocating output buffer size=0x%x, margin=0x%x\n",BUFFER_SIZE,MAX_TAG_DISTANCE); |
| 7838 |
ALLOCATE(dir[0].buffer,dir[0].bsize+MAX_TAG_DISTANCE,uint8_t); |
| 7839 |
ALLOCATE(dir[1].buffer,dir[1].bsize+MAX_TAG_DISTANCE,uint8_t); |
| 7840 |
ALLOCATE(dir[2].buffer,dir[2].bsize+MAX_TAG_DISTANCE,uint8_t); |
| 7841 |
} |
| 7842 |
|
| 7843 |
void hput_increase_buffer(uint32_t n) |
| 7844 |
{ size_t bsize; |
| 7845 |
uint32_t pos, pos0; |
| 7846 |
const double buffer_factor=1.4142136; /* $\sqrt 2$ */ |
| 7847 |
pos=hpos-hstart; pos0=hpos0-hstart; |
| 7848 |
bsize=dir[section_no].bsize*buffer_factor+0.5; |
| 7849 |
if (bsize<pos+n) bsize=pos+n; |
| 7850 |
if (bsize>=HINT_NO_POS) bsize=HINT_NO_POS; |
| 7851 |
if (bsize<pos+n) QUIT(@["Unable to increase buffer size " SIZE_F " by 0x%x byte"@],@|hpos-hstart,n); |
| 7852 |
DBG(DBGBUFFER,@["Reallocating output buffer "@|" for section %d from 0x%x to " SIZE_F " byte\n"@], |
| 7853 |
section_no,dir[section_no].bsize,bsize); |
| 7854 |
REALLOCATE(dir[section_no].buffer,bsize,uint8_t); |
| 7855 |
dir[section_no].bsize=(uint32_t)bsize; |
| 7856 |
hstart=dir[section_no].buffer; |
| 7857 |
hend=hstart+bsize; |
| 7858 |
hpos0=hstart+pos0; hpos=hstart+pos; |
| 7859 |
} |
| 7860 |
|
| 7861 |
static size_t hput_data(uint16_t n, uint8_t *buffer, uint32_t size) |
| 7862 |
{ size_t s; |
| 7863 |
s=fwrite(buffer,1,size,hout); |
| 7864 |
if (s!=size) |
| 7865 |
QUIT(@["short write " SIZE_F " < %d in section %d"@],s,size,n); |
| 7866 |
return s; |
| 7867 |
} |
| 7868 |
|
| 7869 |
static size_t hput_section(uint16_t n) |
| 7870 |
{ return hput_data(n, dir[n].buffer,dir[n].size); |
| 7871 |
} |
| 7872 |
@ |
| 7873 |
|
| 7874 |
|
| 7875 |
|
| 7876 |
|
| 7877 |
\section{Directory Section} |
| 7878 |
A \HINT\ file is subdivided in sections and |
| 7879 |
each section can be identified by its section number. |
| 7880 |
The first three sections, numbered 0, 1, and 2, are mandatory: |
| 7881 |
directory\index{directory section} section, definition section, and content section. |
| 7882 |
The directory section, which we explain now, lists all sections |
| 7883 |
that make up a \HINT\ file. |
| 7884 |
|
| 7885 |
A document will often contain not only plain text but also other media |
| 7886 |
for example illustrations. Illustrations are produced with specialized |
| 7887 |
tools and stored in specialized files. Because a \HINT\ file in short format |
| 7888 |
should be self contained, these special files are embedded in the \HINT\ file |
| 7889 |
as optional sections. |
| 7890 |
Because a \HINT\ file in long format should be readable, these special files |
| 7891 |
are written to disk and only the file names are retained in the directory. |
| 7892 |
Writing special files to disk has also the advantage that you can modify |
| 7893 |
them individually before embedding them in a short format file. |
| 7894 |
|
| 7895 |
|
| 7896 |
\subsection{Directories in Long Format}\gdef\subcodetitle{Directory Section}% |
| 7897 |
The directory\index{directory section} section of a long format \HINT\ file starts |
| 7898 |
with the ``\.{directory}'' keyword; then follows the maximum section number used and |
| 7899 |
a list of directory entries, one for each optional section numbered 3 and above. |
| 7900 |
Each entry consists of the keyword ``\.{section}'' followed by the |
| 7901 |
section number, followed by the file name. |
| 7902 |
The section numbers must be unique and fit into 16 bit. |
| 7903 |
The directory entries must be ordered with strictly increasing section numbers. |
| 7904 |
Keeping section numbers consecutive is recommended because it reduces the |
| 7905 |
memory footprint if directories are stored as arrays indexed by the section |
| 7906 |
number as we will do below. |
| 7907 |
|
| 7908 |
\readcode |
| 7909 |
@s directory_section symbol |
| 7910 |
@s entry_list symbol |
| 7911 |
@s entry symbol |
| 7912 |
@s DIRECTORY symbol |
| 7913 |
@s SECTION symbol |
| 7914 |
|
| 7915 |
@<symbols@>= |
| 7916 |
%token DIRECTORY "directory" |
| 7917 |
%token SECTION "entry" |
| 7918 |
@ |
| 7919 |
|
| 7920 |
@<scanning rules@>= |
| 7921 |
::@=directory@> :< return DIRECTORY; >: |
| 7922 |
::@=section@> :< return SECTION; >: |
| 7923 |
@ |
| 7924 |
|
| 7925 |
@<parsing rules@>= |
| 7926 |
directory_section: START DIRECTORY UNSIGNED @|{new_directory($3+1); new_output_buffers();} entry_list END ; |
| 7927 |
entry_list: @,@+ | entry_list entry; |
| 7928 |
entry: START SECTION UNSIGNED string END @/ |
| 7929 |
{ RNG("Section number",$3,3,max_section_no); hset_entry(&(dir[$3]), $3,0,0,$4);}; |
| 7930 |
@ |
| 7931 |
|
| 7932 |
|
| 7933 |
|
| 7934 |
We use a dynamically allocated array |
| 7935 |
of directory entries to store the directory. |
| 7936 |
|
| 7937 |
@<directory entry type@>= |
| 7938 |
typedef struct { |
| 7939 |
uint64_t pos; |
| 7940 |
uint32_t size, xsize; |
| 7941 |
uint16_t section_no; |
| 7942 |
char *file_name; |
| 7943 |
uint8_t *buffer; |
| 7944 |
uint32_t bsize; |
| 7945 |
} Entry; |
| 7946 |
@ |
| 7947 |
|
| 7948 |
|
| 7949 |
The function |new_directory| allocates the directory. |
| 7950 |
|
| 7951 |
@<directory functions@>= |
| 7952 |
Entry *dir=NULL; |
| 7953 |
uint16_t section_no, max_section_no; |
| 7954 |
void new_directory(uint32_t entries) |
| 7955 |
{ DBG(DBGDIR,"Creating directory with %d entries\n", entries); |
| 7956 |
RNG("Directory entries",entries,3,0x10000); |
| 7957 |
max_section_no=entries-1;@+ |
| 7958 |
ALLOCATE(dir,entries,Entry); |
| 7959 |
dir[0].section_no=0; @+ dir[1].section_no=1; @+ dir[2].section_no=2; |
| 7960 |
} |
| 7961 |
@ |
| 7962 |
|
| 7963 |
The function |hset_entry| fills in the appropriate entry. |
| 7964 |
@<directory functions@>= |
| 7965 |
void hset_entry(Entry *e, uint16_t i, uint32_t size, uint32_t xsize, @|char *file_name) |
| 7966 |
{ e->section_no=i; |
| 7967 |
e->size=size; @+e->xsize=xsize; |
| 7968 |
if (file_name==NULL || *file_name==0) |
| 7969 |
e->file_name=NULL; |
| 7970 |
else |
| 7971 |
e->file_name=strdup(file_name); |
| 7972 |
DBG(DBGDIR,"Creating entry %d: \"%s\" size=0x%x xsize=0x%x\n",@|i,file_name,size,xsize); |
| 7973 |
} |
| 7974 |
@ |
| 7975 |
|
| 7976 |
|
| 7977 |
Writing the auxiliary files depends on the {\tt -a}, {\tt -g} and {\tt -f} |
| 7978 |
options. |
| 7979 |
|
| 7980 |
@<without {\tt -f} skip writing an existing file@>= |
| 7981 |
if ( !option_force && access(aux_name,F_OK)==0) |
| 7982 |
{ MESSAGE("File '%s' exists.\n"@| "To rewrite the file use the -f option.\n", |
| 7983 |
aux_name); |
| 7984 |
continue; |
| 7985 |
} |
| 7986 |
@ |
| 7987 |
|
| 7988 |
The above code uses the |access| function, and we need to make sure it is defined: |
| 7989 |
@<make sure |access| is defined@>= |
| 7990 |
#ifdef WIN32 |
| 7991 |
#include <io.h> |
| 7992 |
#define @[access(N,M)@] @[_access(N, M )@] |
| 7993 |
#define F_OK 0 |
| 7994 |
#else |
| 7995 |
#include <unistd.h> |
| 7996 |
#endif |
| 7997 |
@ |
| 7998 |
|
| 7999 |
With the {\tt -g} option, filenames are considered global, and files |
| 8000 |
are written to the filesystem possibly overwriting the existing files. |
| 8001 |
For example a font embedded in a \HINT\ file might replace a font of |
| 8002 |
the same name in some operating systems font folder. |
| 8003 |
If the \HINT\ file is {\tt shrink}ed on one system and |
| 8004 |
{\tt stretch}ed on another system, this is usually not the desired behavior. |
| 8005 |
Without the {\tt -g} option,\label{absrel} the files will be written in two local directories. |
| 8006 |
The names of these directories are derived from the output file name, |
| 8007 |
replacing the extension ``{\tt .hint}'' with ``{\tt .abs}'' if the original |
| 8008 |
filename contained an absolute path, and replacing it with ``{\tt .rel}'' |
| 8009 |
if the original filename contained a relative path. Inside these directories, |
| 8010 |
the path as given in the filename is retained. |
| 8011 |
When {\tt shrink}ing a \HINT\ file without the {\tt -g} option, |
| 8012 |
the original filenames can be reconstructed. |
| 8013 |
|
| 8014 |
@<compute a local |aux_name|@>= |
| 8015 |
{ char *path=dir[i].file_name; |
| 8016 |
int path_length=(int)strlen(path); |
| 8017 |
@<determine whether |path| is absolute or relative@>@; |
| 8018 |
@<replace links to the parent directory@>@; |
| 8019 |
DBG(DBGDIR,"Replacing auxiliary file name:\n\t%s\n->\t%s\n",path,aux_name); |
| 8020 |
} |
| 8021 |
@ |
| 8022 |
|
| 8023 |
@<determine whether |path| is absolute or relative@>= |
| 8024 |
int aux_length; |
| 8025 |
enum {absolute=0, relative=1} name_type; |
| 8026 |
char *aux_ext[2]={".abs/",".rel/"}; |
| 8027 |
int ext_length=5; |
| 8028 |
aux_length=stem_length+ext_length+path_length; |
| 8029 |
ALLOCATE(aux_name,aux_length+1,char); |
| 8030 |
strcpy(aux_name,stem_name); |
| 8031 |
if (path[0]=='/') |
| 8032 |
{ name_type=absolute; |
| 8033 |
strcpy(aux_name+stem_length,aux_ext[name_type]); |
| 8034 |
strcpy(aux_name+stem_length+ext_length,path+1); |
| 8035 |
} |
| 8036 |
else if (path_length>3 && isalpha(path[0]) && |
| 8037 |
path[1]==':' && path[2]=='/') |
| 8038 |
{ name_type=absolute; |
| 8039 |
strcpy(aux_name+stem_length,aux_ext[name_type]); |
| 8040 |
strcpy(aux_name+stem_length+ext_length,path); |
| 8041 |
aux_name[stem_length+ext_length+1]='_'; |
| 8042 |
} |
| 8043 |
else |
| 8044 |
name_type=relative; |
| 8045 |
@ |
| 8046 |
|
| 8047 |
When the {\tt -g} is not given, auxiliar files are written into |
| 8048 |
special subdirectories. To prevent them from escaping into the global |
| 8049 |
file system, we replace links to the parent direcory ``{\tt ../}'' by |
| 8050 |
``{\tt \_\,\_/}''. |
| 8051 |
|
| 8052 |
@<replace links to the parent directory@>= |
| 8053 |
{ int k; |
| 8054 |
for (k=stem_length+ext_length; k<aux_length-3;k++) |
| 8055 |
if (aux_name[k]=='.'&& aux_name[k+1]=='.'&& aux_name[k+2]=='/') |
| 8056 |
{ aux_name[k]=aux_name[k+1]='_';k=k+2;} |
| 8057 |
} |
| 8058 |
@ |
| 8059 |
|
| 8060 |
It remains to create the directories along the path we might have constructed. |
| 8061 |
@<make sure the path in |aux_name| exists@>= |
| 8062 |
{ char *path_end; |
| 8063 |
path_end=aux_name+1; |
| 8064 |
while (*path_end!=0) |
| 8065 |
{ if(*path_end=='/') |
| 8066 |
{ struct stat s; |
| 8067 |
*path_end=0; |
| 8068 |
if (stat(aux_name,&s)==-1) |
| 8069 |
{ |
| 8070 |
#ifdef WIN32 |
| 8071 |
if (mkdir(aux_name)!=0) |
| 8072 |
#else |
| 8073 |
@t\2\kern-1em@>if (mkdir(aux_name,0777)!=0) |
| 8074 |
#endif |
| 8075 |
QUIT("Unable to create directory %s",aux_name); |
| 8076 |
DBG(DBGDIR,"Creating directory %s\n",aux_name); |
| 8077 |
} else if (!(S_IFDIR&(s.st_mode))) |
| 8078 |
QUIT("Unable to create directory %s, file exists",aux_name); |
| 8079 |
*path_end='/'; |
| 8080 |
} |
| 8081 |
path_end++; |
| 8082 |
} |
| 8083 |
} |
| 8084 |
|
| 8085 |
|
| 8086 |
@ |
| 8087 |
|
| 8088 |
\writecode |
| 8089 |
@<write functions@>= |
| 8090 |
@<make sure |access| is defined@>@; |
| 8091 |
extern char *stem_name; |
| 8092 |
extern int stem_length; |
| 8093 |
|
| 8094 |
void hget_section(uint16_t n); |
| 8095 |
void hwrite_aux_files(void) |
| 8096 |
{ int i; |
| 8097 |
if (!option_aux) return; |
| 8098 |
DBG(DBGBASIC|DBGDIR,"Writing %d aux files\n",max_section_no-2); |
| 8099 |
for (i=3;i<=max_section_no;i++) |
| 8100 |
{ FILE *f; |
| 8101 |
char *aux_name=NULL; |
| 8102 |
if (option_global) |
| 8103 |
aux_name=strdup(dir[i].file_name); |
| 8104 |
else |
| 8105 |
@<compute a local |aux_name|@>@; |
| 8106 |
@<without {\tt -f} skip writing an existing file@>@; |
| 8107 |
@<make sure the path in |aux_name| exists@>@; |
| 8108 |
|
| 8109 |
f=fopen(aux_name,"wb"); |
| 8110 |
if (f==NULL) |
| 8111 |
QUIT("Unable to open file '%s' for writing",aux_name); |
| 8112 |
else |
| 8113 |
{ size_t s; |
| 8114 |
hget_section(i); |
| 8115 |
DBG(DBGDIR,"Writing file %s\n",aux_name); |
| 8116 |
s=fwrite(hstart,1,dir[i].size,f); |
| 8117 |
if (s!=dir[i].size) QUIT("writing file %s",aux_name); |
| 8118 |
fclose(f); |
| 8119 |
} |
| 8120 |
free(aux_name); |
| 8121 |
} |
| 8122 |
} |
| 8123 |
@ |
| 8124 |
|
| 8125 |
We write the directory, and the directory entries |
| 8126 |
in long format using the following functions. |
| 8127 |
@<write functions@>= |
| 8128 |
static void hwrite_entry(int i) |
| 8129 |
{ hwrite_start(); |
| 8130 |
hwritef("section %u",dir[i].section_no);@+ hwrite_string(dir[i].file_name); |
| 8131 |
hwrite_end(); |
| 8132 |
} |
| 8133 |
|
| 8134 |
void hwrite_directory(void) |
| 8135 |
{ int i; |
| 8136 |
if (dir==NULL) QUIT("Directory not allocated"); |
| 8137 |
section_no=0; |
| 8138 |
hwritef("<directory %u", max_section_no);@/ |
| 8139 |
for (i=3;i<=max_section_no;i++) |
| 8140 |
hwrite_entry(i); |
| 8141 |
hwritef("\n>\n"); |
| 8142 |
} |
| 8143 |
@ |
| 8144 |
|
| 8145 |
\subsection{Directories in Short Format} |
| 8146 |
The directory\index{directory section} section of a short format file contains entries |
| 8147 |
for all sections including the directory section itself. After reading the |
| 8148 |
directory section, enough information---position and size---is available to |
| 8149 |
access any section directly. As usual, a directory entry starts and ends with |
| 8150 |
a tag byte. The kind part of an entry's tag is not used; it is always zero. |
| 8151 |
The value $s$ of the two least significant bits of the info part indicate |
| 8152 |
that sizes are stored using $s+1$ byte. The most significant bit of the info |
| 8153 |
part is 1 if the section is stored in compressed\index{compression} form. In this case the size |
| 8154 |
of the section is followed by the size of the section after decompressing it. |
| 8155 |
After the tag byte follows the section number. In the short format file, |
| 8156 |
section numbers must be strictly increasing and consecutive. This is redundant but helps |
| 8157 |
with checking. Then follows the size---or the sizes---of the section. After the size |
| 8158 |
follows the file name terminated by a zero byte. The file name might be an empty |
| 8159 |
string in which case there is just the zero byte. After the zero byte follows |
| 8160 |
a copy of the tag byte. |
| 8161 |
|
| 8162 |
Here is the macro and function to read a directory\index{directory entry} entry: |
| 8163 |
\gdef\subcodetitle{Directory Entries}% |
| 8164 |
\getcode |
| 8165 |
|
| 8166 |
@<shared get macros@>= |
| 8167 |
#define @[HGET_SIZE(I)@] \ |
| 8168 |
if ((I)&b100) { \ |
| 8169 |
if (((I)&b011)==0) s=HGET8,xs=HGET8; \ |
| 8170 |
else if (((I)&b011)==1) HGET16(s),HGET16(xs); \ |
| 8171 |
else if (((I)&b011)==2) HGET24(s),HGET24(xs); \ |
| 8172 |
else if (((I)&b011)==3) HGET32(s),HGET32(xs); \ |
| 8173 |
} \ |
| 8174 |
else { \ |
| 8175 |
if (((I)&b011)==0) s=HGET8; \ |
| 8176 |
else if (((I)&b011)==1) HGET16(s); \ |
| 8177 |
else if (((I)&b011)==2) HGET24(s); \ |
| 8178 |
else if (((I)&b011)==3) HGET32(s); \ |
| 8179 |
} |
| 8180 |
|
| 8181 |
#define @[HGET_ENTRY(I,E)@] \ |
| 8182 |
{ uint16_t i; \ |
| 8183 |
uint32_t s=0,xs=0; \ |
| 8184 |
char *file_name; \ |
| 8185 |
HGET16(i); @+HGET_SIZE(I); @+HGET_STRING(file_name); @/\ |
| 8186 |
hset_entry(&(E),i,s,xs,file_name); \ |
| 8187 |
} |
| 8188 |
@ |
| 8189 |
|
| 8190 |
@<get file functions@>= |
| 8191 |
void hget_entry(Entry *e) |
| 8192 |
{ @<read the start byte |a|@>@; |
| 8193 |
DBG(DBGDIR,"Reading directory entry\n"); |
| 8194 |
switch(a) |
| 8195 |
{ case TAG(0,b000+0): HGET_ENTRY(b000+0,*e);@+ break; |
| 8196 |
case TAG(0,b000+1): HGET_ENTRY(b000+1,*e);@+ break; |
| 8197 |
case TAG(0,b000+2): HGET_ENTRY(b000+2,*e);@+ break; |
| 8198 |
case TAG(0,b000+3): HGET_ENTRY(b000+3,*e);@+ break; |
| 8199 |
case TAG(0,b100+0): HGET_ENTRY(b100+0,*e);@+ break; |
| 8200 |
case TAG(0,b100+1): HGET_ENTRY(b100+1,*e);@+ break; |
| 8201 |
case TAG(0,b100+2): HGET_ENTRY(b100+2,*e);@+ break; |
| 8202 |
case TAG(0,b100+3): HGET_ENTRY(b100+3,*e);@+ break; |
| 8203 |
default: TAGERR(a); @+ break; |
| 8204 |
} |
| 8205 |
@<read and check the end byte |z|@>@; |
| 8206 |
} |
| 8207 |
@ |
| 8208 |
|
| 8209 |
Because the first entry in the directory section describes the |
| 8210 |
directory section itself, we can not check its info bits in advance to determine |
| 8211 |
whether it is compressed or not. Therefore the directory section |
| 8212 |
starts with a root entry, which is always uncompressed. It describes |
| 8213 |
the remainder of the directory which follows. |
| 8214 |
There are two differences between the root entry and a normal entry: |
| 8215 |
it starts with the maximum section number instead of the section number zero, |
| 8216 |
and we set its position to the position of the |
| 8217 |
entry for section 1 (which might already be compressed). |
| 8218 |
The name of the directory section must be the empty string. |
| 8219 |
|
| 8220 |
\gdef\subcodetitle{Directory Section}% |
| 8221 |
\getcode |
| 8222 |
@<get file functions@>= |
| 8223 |
static void hget_root(Entry *root) |
| 8224 |
{ DBG(DBGDIR,"Root entry at " SIZE_F "\n",hpos-hstart); |
| 8225 |
hget_entry(root); |
| 8226 |
root->pos=hpos-hstart; |
| 8227 |
max_section_no=root->section_no; |
| 8228 |
root->section_no=0; |
| 8229 |
if (max_section_no<2) QUIT("Sections 0, 1, and 2 are mandatory"); |
| 8230 |
} |
| 8231 |
|
| 8232 |
void hget_directory(void) |
| 8233 |
{ int i; |
| 8234 |
Entry root={0}; |
| 8235 |
hget_root(&root); |
| 8236 |
DBG(DBGDIR,"Directory\n"); |
| 8237 |
new_directory(max_section_no+1); |
| 8238 |
dir[0]=root; |
| 8239 |
DBG(DBGDIR,"Directory entry 1 at 0x%"PRIx64"\n",dir[0].pos); |
| 8240 |
hget_section(0); |
| 8241 |
for (i=1;i<=max_section_no;i++)@/ |
| 8242 |
{ hget_entry(&(dir[i]));@+ |
| 8243 |
dir[i].pos=dir[i-1].pos +dir[i-1].size;@+ |
| 8244 |
DBG(DBGDIR,"Section %d at 0x%"PRIx64"\n",i,dir[i].pos); |
| 8245 |
} |
| 8246 |
} |
| 8247 |
|
| 8248 |
void hclear_dir(void) |
| 8249 |
{ int i; |
| 8250 |
if (dir==NULL) return; |
| 8251 |
for (i=0;i<3;i++) /* currently the only compressed sections */ |
| 8252 |
if (dir[i].xsize>0 && dir[i].buffer!=NULL) free(dir[i].buffer); |
| 8253 |
free(dir); dir=NULL; |
| 8254 |
} |
| 8255 |
|
| 8256 |
@ |
| 8257 |
|
| 8258 |
Armed with these preparations, we can put the directory into the \HINT\ file. |
| 8259 |
|
| 8260 |
\gdef\subcodetitle{Directory Section}% |
| 8261 |
\putcode |
| 8262 |
@<put functions@>= |
| 8263 |
static void hput_entry(Entry *e) |
| 8264 |
{ Info b; |
| 8265 |
if (e->size<0x100 && e->xsize<0x100) b=b000; |
| 8266 |
else if (e->size<0x10000 &&e->xsize<0x10000) b=b001; |
| 8267 |
else if (e->size<0x1000000 &&e->xsize<0x1000000) b=b010; |
| 8268 |
else b=b011; |
| 8269 |
if (e->xsize!=0) b =b|b100; |
| 8270 |
DBG(DBGTAGS,"Directory entry no=%d size=0x%x xsize=0x%x\n",e->section_no, e->size, e->xsize); |
| 8271 |
HPUTTAG(0,b);@/ |
| 8272 |
HPUT16(e->section_no); |
| 8273 |
switch (b) { |
| 8274 |
case b000: HPUT8(e->size);@+break; |
| 8275 |
case b001: HPUT16(e->size);@+break; |
| 8276 |
case b010: HPUT24(e->size);@+break; |
| 8277 |
case b011: HPUT32(e->size);@+break; |
| 8278 |
case b100: HPUT8(e->size);@+HPUT8(e->xsize);@+break; |
| 8279 |
case b101: HPUT16(e->size);@+HPUT16(e->xsize);@+break; |
| 8280 |
case b110: HPUT24(e->size);@+HPUT24(e->xsize);@+break; |
| 8281 |
case b111: HPUT32(e->size);@+HPUT32(e->xsize);@+break; |
| 8282 |
default: QUIT("Can't happen");@+ break; |
| 8283 |
} |
| 8284 |
hput_string(e->file_name);@/ |
| 8285 |
DBGTAG(TAG(0,b),hpos);@+HPUT8(TAG(0,b)); |
| 8286 |
} |
| 8287 |
|
| 8288 |
static void hput_directory_start(void) |
| 8289 |
{ DBG(DBGDIR,"Directory Section\n"); |
| 8290 |
section_no=0; |
| 8291 |
hpos=hstart=dir[0].buffer; |
| 8292 |
hend=hstart+dir[0].bsize; |
| 8293 |
} |
| 8294 |
static void hput_directory_end(void) |
| 8295 |
{ dir[0].size=hpos-hstart; |
| 8296 |
DBG(DBGDIR,"End Directory Section size=0x%x\n",dir[0].size); |
| 8297 |
} |
| 8298 |
|
| 8299 |
static size_t hput_root(void) |
| 8300 |
{ uint8_t buffer[MAX_TAG_DISTANCE]; |
| 8301 |
size_t s; |
| 8302 |
hpos=hstart=buffer; |
| 8303 |
hend=hstart+MAX_TAG_DISTANCE; |
| 8304 |
dir[0].section_no=max_section_no; |
| 8305 |
hput_entry(&dir[0]); |
| 8306 |
s=hput_data(0, hstart,hpos-hstart); |
| 8307 |
DBG(DBGDIR,@["Writing root size=" SIZE_F "\n"@],s); |
| 8308 |
return s; |
| 8309 |
} |
| 8310 |
|
| 8311 |
extern int option_compress; |
| 8312 |
static char **aux_names; |
| 8313 |
void hput_directory(void) |
| 8314 |
{ int i; |
| 8315 |
@<update the file sizes of optional sections@>@; |
| 8316 |
|
| 8317 |
if (option_compress) { hcompress(1); @+hcompress(2); @+} |
| 8318 |
hput_directory_start(); |
| 8319 |
for (i=1; i<=max_section_no; i++) |
| 8320 |
{ dir[i].pos=dir[i-1].pos+dir[i-1].size; |
| 8321 |
DBG(DBGDIR,"writing entry %u at 0x%" PRIx64 "\n",i, dir[i].pos); |
| 8322 |
hput_entry(&dir[i]); |
| 8323 |
} |
| 8324 |
hput_directory_end(); |
| 8325 |
if (option_compress) hcompress(0); |
| 8326 |
} |
| 8327 |
|
| 8328 |
@ |
| 8329 |
|
| 8330 |
Now let us look at the optional sections described in the directory |
| 8331 |
entries 3 and above. Where these files are found depends on the {\tt |
| 8332 |
-g} and {\tt -a} options. |
| 8333 |
|
| 8334 |
With the {\tt -g} option given, only the file names as given in the |
| 8335 |
directory entries are used. With the {\tt -a} option given, the file |
| 8336 |
names are translated to filenames in the {|hin_name|\tt .abs} and |
| 8337 |
{|hin_name|\tt .rel} directories, as described in |
| 8338 |
section~\secref{absrel}. If neither the {\tt -a} nor the {\tt -g} |
| 8339 |
option is given, {\tt shrink} first tries the translated filename and |
| 8340 |
then the global filename before it gives up. |
| 8341 |
|
| 8342 |
When the \.{shrink} program writes the directory section in the short |
| 8343 |
format, it needs to know the sizes of all the sections---including the |
| 8344 |
optional sections. These sizes are not provided in the long format |
| 8345 |
because it is safer and more convenient to let the machine figure out |
| 8346 |
the file sizes\index{file size}. But before we can determine the |
| 8347 |
size, we need to determine the file. |
| 8348 |
|
| 8349 |
@<update the file sizes of optional sections@>= |
| 8350 |
{ int i; |
| 8351 |
ALLOCATE(aux_names,max_section_no+1,char *); |
| 8352 |
for (i=3;i<=max_section_no;i++) |
| 8353 |
if (dir[i].size==0) |
| 8354 |
{ struct stat s; |
| 8355 |
if (!option_global) |
| 8356 |
{ char * aux_name=NULL; |
| 8357 |
@<compute a local |aux_name|@>@; |
| 8358 |
if (stat(aux_name,&s)==0) |
| 8359 |
aux_names[i]=aux_name; |
| 8360 |
else |
| 8361 |
{ if (option_aux) QUIT("Unable to find file '%s'",aux_name); |
| 8362 |
free(aux_name); aux_name=NULL; |
| 8363 |
} |
| 8364 |
} |
| 8365 |
if ((aux_names[i]==NULL && !option_aux) || option_global) |
| 8366 |
{ if (stat(dir[i].file_name,&s)!=0) |
| 8367 |
QUIT("Unable to find file '%s'",dir[i].file_name); |
| 8368 |
} |
| 8369 |
dir[i].size=s.st_size; |
| 8370 |
dir[i].xsize=0; |
| 8371 |
DBG(DBGDIR,"section %i: found file %s size %u\n",i,aux_names[i]?aux_names[i]:dir[i].file_name, dir[i].size); |
| 8372 |
} |
| 8373 |
} |
| 8374 |
@ |
| 8375 |
|
| 8376 |
@<rewrite the file names of optional sections@>= |
| 8377 |
{ int i; |
| 8378 |
for (i=3;i<=max_section_no;i++) |
| 8379 |
if (aux_names[i]!=NULL) |
| 8380 |
{ free(dir[i].file_name); |
| 8381 |
dir[i].file_name=aux_names[i]; |
| 8382 |
aux_names[i]=NULL; |
| 8383 |
} |
| 8384 |
} |
| 8385 |
@ |
| 8386 |
|
| 8387 |
|
| 8388 |
The computation of the sizes of the mandatory sections will be |
| 8389 |
explained later. |
| 8390 |
|
| 8391 |
|
| 8392 |
\gdef\subcodetitle{Optional Sections}% |
| 8393 |
To conclude this section, here is the function that adds the files that |
| 8394 |
are described in the directory entries 3 and above to a \HINT\ file in short format. |
| 8395 |
|
| 8396 |
\putcode |
| 8397 |
@<put functions@>= |
| 8398 |
static size_t hput_optional_section(int i) |
| 8399 |
{ size_t fsize; |
| 8400 |
if (dir[i].xsize!=0) @/ |
| 8401 |
DBG(DBGDIR,"Compressing of auxiliary files currently not supported"); |
| 8402 |
if (dir[i].buffer==NULL) |
| 8403 |
@<append section |i| from file@>@; |
| 8404 |
else |
| 8405 |
@<append section |i| from |buffer|@>@; |
| 8406 |
if (fsize!=dir[i].size) |
| 8407 |
QUIT(@["Size written " SIZE_F " does not match section %d size %u"@], |
| 8408 |
@|fsize, i, dir[i].size); |
| 8409 |
return fsize; |
| 8410 |
} |
| 8411 |
@ |
| 8412 |
|
| 8413 |
If the content of the auxiliar section |i| was not loaded into |dir[i].buffer| |
| 8414 |
earlier, the file i loaded from disk and appended to the output. |
| 8415 |
|
| 8416 |
@<append section |i| from file@>= |
| 8417 |
{ FILE *f; |
| 8418 |
char *file_name=dir[i].file_name; |
| 8419 |
DBG(DBGDIR,"Adding file %d: %s\n",dir[i].section_no,file_name); |
| 8420 |
f=fopen(file_name,"rb"); |
| 8421 |
if (f==NULL) QUIT("Unable to read section %d, file %s", |
| 8422 |
dir[i].section_no,file_name); |
| 8423 |
fsize=0; |
| 8424 |
while (!feof(f))@/ |
| 8425 |
{ size_t s,t; |
| 8426 |
char buffer[1<<13]; /* 8kByte */ |
| 8427 |
s=fread(buffer,1,1<<13,f);@/ |
| 8428 |
t=fwrite(buffer,1,s,hout); |
| 8429 |
if (s!=t) QUIT("Unable to write file %s",file_name); |
| 8430 |
fsize=fsize+t; |
| 8431 |
} |
| 8432 |
fclose(f); |
| 8433 |
} |
| 8434 |
@ |
| 8435 |
|
| 8436 |
In the simpler case, that the file was loaded already into the buffer., |
| 8437 |
the buffer is appended to the output. |
| 8438 |
|
| 8439 |
@<append section |i| from |buffer|@>= |
| 8440 |
{ fsize=fwrite(dir[i].buffer,1,dir[i].size,hout); |
| 8441 |
} |
| 8442 |
@ |
| 8443 |
|
| 8444 |
\section{Definition Section}\index{definition section} |
| 8445 |
\label{defsection} |
| 8446 |
In a typical \HINT\ file, there are many things that are used over and over again. |
| 8447 |
For example the interword glue of a specific font or the indentation of |
| 8448 |
the first line of a paragraph. The definition section contains this information so that |
| 8449 |
it can be referenced in the content section by a simple reference number. |
| 8450 |
In addition there are a few parameters that guide the routines of \TeX. |
| 8451 |
An example is the ``above display skip'', which controls the amount of white space |
| 8452 |
inserted above a displayed equation, or the ``hyphen penalty'' that tells \TeX\ |
| 8453 |
the ``\ae sthetic cost'' of ending a line with a hyphenated word. These parameters |
| 8454 |
also get their values in the definition section as explained in section~\secref{defaults}. |
| 8455 |
|
| 8456 |
|
| 8457 |
The most simple way to store these definitions is to store them in an array indexed by the |
| 8458 |
reference numbers. |
| 8459 |
To simplify the dynamic allocation of these arrays, the list of definitions |
| 8460 |
will always start with the list of maximum\index{maximum values} values: a list that contains |
| 8461 |
for each node type the maximum reference number used. |
| 8462 |
|
| 8463 |
In the long format, the definition section starts with the keyword \.{definitions}, |
| 8464 |
followed by the list of maximum values, |
| 8465 |
followed by the definitions proper. |
| 8466 |
|
| 8467 |
When writing the short format, we start by positioning the output stream at the beginning of |
| 8468 |
the definition buffer and we end with recording the size of the definition section |
| 8469 |
in the directory. |
| 8470 |
|
| 8471 |
\readcode |
| 8472 |
@s definition_section symbol |
| 8473 |
@s definition_list symbol |
| 8474 |
@s definition symbol |
| 8475 |
@s DEFINITIONS symbol |
| 8476 |
|
| 8477 |
@<symbols@>= |
| 8478 |
%token DEFINITIONS "definitions" |
| 8479 |
@ |
| 8480 |
|
| 8481 |
@<scanning rules@>= |
| 8482 |
::@=definitions@> :< return DEFINITIONS; >: |
| 8483 |
@ |
| 8484 |
|
| 8485 |
@<parsing rules@>= |
| 8486 |
definition_section: START DEFINITIONS { hput_definitions_start();}@/ |
| 8487 |
max_definitions definition_list @/ |
| 8488 |
END {hput_definitions_end();}; |
| 8489 |
definition_list: @+ | definition_list def_node; |
| 8490 |
@ |
| 8491 |
|
| 8492 |
\writecode |
| 8493 |
@<write functions@>= |
| 8494 |
void hwrite_definitions_start(void) |
| 8495 |
{ section_no=1; @+hwritef("<definitions"); |
| 8496 |
} |
| 8497 |
|
| 8498 |
void hwrite_definitions_end(void) |
| 8499 |
{ hwritef("\n>\n"); |
| 8500 |
} |
| 8501 |
@ |
| 8502 |
|
| 8503 |
|
| 8504 |
|
| 8505 |
@<get functions@>= |
| 8506 |
void hget_definition_section(void) |
| 8507 |
{ DBG(DBGBASIC|DBGDEF,"Definitions\n"); |
| 8508 |
hget_section(1); |
| 8509 |
hwrite_definitions_start(); |
| 8510 |
DBG(DBGDEF,"List of maximum values\n"); |
| 8511 |
hget_max_definitions(); |
| 8512 |
@<initialize definitions@>@; |
| 8513 |
hwrite_max_definitions(); |
| 8514 |
DBG(DBGDEF,"List of definitions\n"); |
| 8515 |
while (hpos<hend) |
| 8516 |
hget_def_node(); |
| 8517 |
hwrite_definitions_end(); |
| 8518 |
} |
| 8519 |
@ |
| 8520 |
|
| 8521 |
\putcode |
| 8522 |
@<put functions@>= |
| 8523 |
void hput_definitions_start(void) |
| 8524 |
{ DBG(DBGDEF,"Definition Section\n"); |
| 8525 |
section_no=1; |
| 8526 |
hpos=hstart=dir[1].buffer; |
| 8527 |
hend=hstart+dir[1].bsize; |
| 8528 |
} |
| 8529 |
void hput_definitions_end(void) |
| 8530 |
{ dir[1].size=hpos-hstart; |
| 8531 |
DBG(DBGDEF,"End Definition Section size=0x%x\n",dir[1].size); |
| 8532 |
} |
| 8533 |
@ |
| 8534 |
\gdef\codetitle{Definitions} |
| 8535 |
\hascode |
| 8536 |
\subsection{Maximum Values}\index{maximum values} |
| 8537 |
To help implementations allocating the right amount of memory for the |
| 8538 |
definitions, the definition section starts with a list of maximum |
| 8539 |
values. For each kind of node, we store the maximum valid reference |
| 8540 |
number in the array |max_ref| which is indexed by the kind-values. |
| 8541 |
For a reference number |n| and kind-value $k$ we have |
| 8542 |
$0\le n\le |max_ref[k]|$. |
| 8543 |
To make sure that a hint file without any definitions |
| 8544 |
will work, some definitions have default values. |
| 8545 |
The initialization of default and maximum values is described |
| 8546 |
in section~\secref{defaults}. The maximum |
| 8547 |
reference number that has a default value is stored in the array |
| 8548 |
|max_default|. |
| 8549 |
We have $-1 \le |max_default[k]| \le |max_ref[k]| < 2^{16}$, |
| 8550 |
and for most $k$ even $|max_ref[k]| < 2^{8}$. |
| 8551 |
Specifying maximum values that are lower than the |
| 8552 |
default\index{default value} values is not allowed in the short |
| 8553 |
format; in the long format, lower values are silently ignored. Some |
| 8554 |
default values are permanently fixed; for example the zero glue with |
| 8555 |
reference number |zero_skip_no| must never change. The array |
| 8556 |
|max_fixed| stores the maximum reference number that has a fixed value for a |
| 8557 |
given kind. Definitions with reference numbers less or equal than the |
| 8558 |
corresponding |max_fixed[k]| number are disallowed. Usually we have |
| 8559 |
$-1 \le |max_fixed[k]| \le |max_default[k]| $, but if for a kind-value |
| 8560 |
$k$ no definitions, and hence no maximum values are allowed, we set |
| 8561 |
$|max_fixed[k]|=|0x10000|>|max_default[k]| $. |
| 8562 |
|
| 8563 |
|
| 8564 |
We use the |max_ref| array whenever we find a |
| 8565 |
reference number in the input to check if it is within the proper range. |
| 8566 |
|
| 8567 |
@<debug macros@>= |
| 8568 |
#define @[REF_RNG(K,N)@] if ((int)(N)>max_ref[K]) QUIT("Reference %d to %s out of range [0 - %d]",\ |
| 8569 |
(N),definition_name[K],max_ref[K]) |
| 8570 |
@ |
| 8571 |
|
| 8572 |
In the long format file, the list of maximum values starts with |
| 8573 |
``\.{<max }'', then follow pairs of keywords and numbers like |
| 8574 |
``\.{<glue 57>}'', and it ends with ``\.{>}''. In the short format, |
| 8575 |
we start the list of maximums with a |list_kind| tag and end it with |
| 8576 |
a |list_kind| tag. Each maximum value is preceded and followed by a |
| 8577 |
tag byte with the appropriate kind-value. The info value has its |b001| bit |
| 8578 |
cleared if the maximum value is in the range 0 to |0xFF| and fits into a |
| 8579 |
single byte; the info value hast its |b001| bit set if it fits into two byte. |
| 8580 |
Currently only the |label_kind| may need to use two byte. |
| 8581 |
@<debug macros@>= |
| 8582 |
#define MAX_REF(K) ((K)==label_kind?0xFFFF:0xFF) |
| 8583 |
@ |
| 8584 |
|
| 8585 |
Other info values are reserved for future extensions. |
| 8586 |
After reading the maximum values, we initialize the data structures for |
| 8587 |
the definitions. |
| 8588 |
|
| 8589 |
|
| 8590 |
\readcode |
| 8591 |
@s max_list symbol |
| 8592 |
@s max_value symbol |
| 8593 |
@s max_definitions symbol |
| 8594 |
@s MAX symbol |
| 8595 |
@<symbols@>= |
| 8596 |
%token MAX "max" |
| 8597 |
@ |
| 8598 |
|
| 8599 |
@<scanning rules@>= |
| 8600 |
::@=max@> :< return MAX; >: |
| 8601 |
@ |
| 8602 |
@<parsing rules@>= |
| 8603 |
max_definitions: START MAX max_list END @| |
| 8604 |
{ @<initialize definitions@>@;@+ hput_max_definitions(); }; |
| 8605 |
|
| 8606 |
max_list:@+ | max_list START max_value END; |
| 8607 |
|
| 8608 |
max_value: FONT UNSIGNED { hset_max(font_kind,$2); } |
| 8609 |
| INTEGER UNSIGNED { hset_max(int_kind,$2); } |
| 8610 |
| DIMEN UNSIGNED { hset_max(dimen_kind,$2); } |
| 8611 |
| LIGATURE UNSIGNED { hset_max(ligature_kind,$2); } |
| 8612 |
| DISC UNSIGNED { hset_max(disc_kind,$2); } |
| 8613 |
| GLUE UNSIGNED { hset_max(glue_kind,$2); } |
| 8614 |
| LANGUAGE UNSIGNED { hset_max(language_kind,$2); } |
| 8615 |
| RULE UNSIGNED { hset_max(rule_kind,$2); } |
| 8616 |
| IMAGE UNSIGNED { hset_max(image_kind,$2); } |
| 8617 |
| LEADERS UNSIGNED { hset_max(leaders_kind,$2); } |
| 8618 |
| BASELINE UNSIGNED { hset_max(baseline_kind,$2); } |
| 8619 |
| XDIMEN UNSIGNED { hset_max(xdimen_kind,$2); } |
| 8620 |
| PARAM UNSIGNED { hset_max(param_kind,$2); } |
| 8621 |
| STREAMDEF UNSIGNED { hset_max(stream_kind,$2); } |
| 8622 |
| PAGE UNSIGNED { hset_max(page_kind,$2); } |
| 8623 |
| RANGE UNSIGNED { hset_max(range_kind,$2); } |
| 8624 |
| LABEL UNSIGNED { hset_max(label_kind,$2); } |
| 8625 |
| COLOR UNSIGNED { hset_max(color_kind,$2); }; |
| 8626 |
|
| 8627 |
@ |
| 8628 |
|
| 8629 |
@<parsing functions@>= |
| 8630 |
void hset_max(Kind k, int n) |
| 8631 |
{ DBG(DBGDEF,"Setting max %s to %d\n",definition_name[k],n); |
| 8632 |
RNG("Maximum",n,max_fixed[k]+1,MAX_REF(k)); |
| 8633 |
if (n>max_ref[k]) |
| 8634 |
max_ref[k]=n; |
| 8635 |
} |
| 8636 |
@ |
| 8637 |
|
| 8638 |
\writecode |
| 8639 |
@<write functions@>= |
| 8640 |
void hwrite_max_definitions(void) |
| 8641 |
{ Kind k; |
| 8642 |
hwrite_start();@+ |
| 8643 |
hwritef("max"); |
| 8644 |
for (k=0; k<32;k++) |
| 8645 |
if (max_ref[k]>max_default[k])@/ |
| 8646 |
{@+ switch (k) |
| 8647 |
{ @<cases of writing special maximum values@>@; |
| 8648 |
default: |
| 8649 |
hwrite_start(); |
| 8650 |
hwritef("%s %d",definition_name[k], max_ref[k]); |
| 8651 |
hwrite_end(); |
| 8652 |
break; |
| 8653 |
} |
| 8654 |
} |
| 8655 |
hwrite_end(); |
| 8656 |
} |
| 8657 |
@ |
| 8658 |
|
| 8659 |
\getcode |
| 8660 |
@<get file functions@>= |
| 8661 |
void hget_max_definitions(void) |
| 8662 |
{ Kind k; |
| 8663 |
@<read the start byte |a|@>@; |
| 8664 |
if (a!=TAG(list_kind,0)) QUIT("Start of maximum list expected"); |
| 8665 |
for(k= 0;k<32;k++)max_ref[k]= max_default[k]; max_outline=-1; |
| 8666 |
while (true) @/ |
| 8667 |
{ int n; |
| 8668 |
if (hpos>=hend) QUIT("Unexpected end of maximum list"); |
| 8669 |
node_pos=hpos-hstart; |
| 8670 |
HGETTAG(a);@+ |
| 8671 |
k=KIND(a);@+ |
| 8672 |
if (k==list_kind) break; |
| 8673 |
if (INFO(a)&b001) HGET16(n); @+else n=HGET8; |
| 8674 |
switch (a) |
| 8675 |
{ @<cases of getting special maximum values@>@; |
| 8676 |
default: |
| 8677 |
if (max_fixed[k]>max_default[k]) |
| 8678 |
MESSAGE("Maximum value for kind %s not supported\n",definition_name[k]); else |
| 8679 |
{ RNG("Maximum number",n,max_default[k],MAX_REF(k)); |
| 8680 |
max_ref[k]=n; |
| 8681 |
DBG(DBGDEF,"max(%s) = %d\n",definition_name[k],max_ref[k]); |
| 8682 |
} |
| 8683 |
break; |
| 8684 |
} |
| 8685 |
@<read and check the end byte |z|@>@; |
| 8686 |
} |
| 8687 |
if (INFO(a)!=0) QUIT("End of maximum list with info %d", INFO(a)); |
| 8688 |
DBG(DBGDEF,"Getting Max Definitions END\n"); |
| 8689 |
} |
| 8690 |
@ |
| 8691 |
|
| 8692 |
\putcode |
| 8693 |
|
| 8694 |
@<put functions@>= |
| 8695 |
void hput_max_definitions(void) |
| 8696 |
{ Kind k; |
| 8697 |
DBG(DBGDEF,"Writing Max Definitions\n"); |
| 8698 |
HPUTTAG(list_kind,0); |
| 8699 |
for (k=0; k<32; k++) |
| 8700 |
if (max_ref[k]>max_default[k]) |
| 8701 |
{ uint32_t pos=hpos++-hstart; |
| 8702 |
DBG(DBGDEF,"max(%s) = %d\n",definition_name[k],max_ref[k]); |
| 8703 |
hput_tags(pos,TAG(k,hput_n(max_ref[k])-1)); |
| 8704 |
} |
| 8705 |
@<cases of putting special maximum values@>@; |
| 8706 |
HPUTTAG(list_kind,0); |
| 8707 |
DBG(DBGDEF,"Writing Max Definitions End\n"); |
| 8708 |
} |
| 8709 |
@ |
| 8710 |
|
| 8711 |
|
| 8712 |
\subsection{Definitions}\label{definitions} |
| 8713 |
A definition\index{definition section} associates a reference number |
| 8714 |
with a content node. Here is an example: A glue definition associates |
| 8715 |
a glue number, for example 71, with a glue specification. In the long |
| 8716 |
format this might look like ``{\tt \.{<}glue *71 4pt plus 5pt minus |
| 8717 |
0.5pt\.{>}}'' which makes glue number 71 refer to a 4pt glue with a |
| 8718 |
stretchability of 5pt and a shrinkability of 0.5pt. |
| 8719 |
Such a glue definition differs from a normal glue node just by an extra |
| 8720 |
byte value immediately following the keyword respectively start byte. |
| 8721 |
|
| 8722 |
Whenever we need this glue in the content section, we can say |
| 8723 |
``{\tt \.{<}glue *71\.{>}}''. Because we restrict the number of glue definitions |
| 8724 |
to at most 256, a single byte is sufficient to |
| 8725 |
store the reference number. The \.{shrink} and \.{stretch} programs |
| 8726 |
will, however, not bother to store glue definitions. Instead they will |
| 8727 |
write them in the new format immediately to the output. |
| 8728 |
|
| 8729 |
The parser will handle definitions in any order, but the order is relevant |
| 8730 |
if a definition references another definition, and of course, |
| 8731 |
it never does any harm to present definitions in a systematic way. |
| 8732 |
|
| 8733 |
As a rule, the definition of a reference must always precede the |
| 8734 |
use of that reference. While this is always the case for |
| 8735 |
references in the content section, it restricts the use of |
| 8736 |
references inside the definition section. |
| 8737 |
|
| 8738 |
The definitions for integers, dimensions, extended dimensions, |
| 8739 |
languages, rules, ligatures, and images are ``simple''. |
| 8740 |
They never contain references and so it is always possible to list them first. |
| 8741 |
The definition of glues may contain extended dimensions, |
| 8742 |
the definitions of baselines may reference glue nodes, and |
| 8743 |
the definitions of parameter lists contain definitions of integers, dimensions, |
| 8744 |
and glues. So these definitions should follow in this order. |
| 8745 |
|
| 8746 |
The definitions of leaders and discretionary breaks allow boxes. |
| 8747 |
While these boxes are usually |
| 8748 |
quite simple, they may contain arbitrary references---including again |
| 8749 |
references to leaders and discretionary breaks. So, at least in principle, |
| 8750 |
they might impose complex (or even unsatisfiable) restrictions |
| 8751 |
on the order of those definitions. |
| 8752 |
|
| 8753 |
The definitions of fonts contain not only ``simple'' definitions |
| 8754 |
but also the definitions of interword glues and hyphens |
| 8755 |
introducing additional ordering restrictions. |
| 8756 |
The definition of hyphens regularly contain glyphs which in turn reference |
| 8757 |
a font---typically the font that just gets defined. |
| 8758 |
Therefore we relax the define before use policy for glyphs: |
| 8759 |
Glyphs may reference a font before the font is defined. |
| 8760 |
|
| 8761 |
The definitions of page templates contain lists of arbitrary content |
| 8762 |
nodes, and while the boxes inside leaders or discretionary breaks tend to be simple, |
| 8763 |
the content of page templates is often quite complex. |
| 8764 |
Page templates are probably the source of most ordering restrictions. |
| 8765 |
Placing page templates towards the end of the list of definitions |
| 8766 |
might be a good idea. |
| 8767 |
% |
| 8768 |
A special case are stream definitions. These occur only as part of |
| 8769 |
the corresponding page template definition and are listed at its end. |
| 8770 |
So references to them will occur in the page template always before their |
| 8771 |
definition. |
| 8772 |
% |
| 8773 |
Finally, the definitions of page ranges always reference a page template |
| 8774 |
and they should come after the page template definitions. |
| 8775 |
For technical reasons explained in section~\secref{labels}, |
| 8776 |
definitions of labels and outlines come last. |
| 8777 |
|
| 8778 |
To avoid complex dependencies, an application can always choose not to |
| 8779 |
use references in the definition section. There are only three types of |
| 8780 |
nodes where references can not be avoided: fonts are referenced in glyph nodes, |
| 8781 |
labels are referenced in outlines, |
| 8782 |
and languages are referenced in boxes or page templates. |
| 8783 |
Possible ordering restrictions can be satisfied if languages are defined early. |
| 8784 |
To check the define before use policy, we use an array of bitvectors, |
| 8785 |
but we limit checking to the first 256 references. |
| 8786 |
We have for every reference number $|N|<256$ and every kind |K| a single |
| 8787 |
bit which is set if and only if the corresponding reference is defined. |
| 8788 |
|
| 8789 |
@<definition checks@>= |
| 8790 |
uint32_t definition_bits[0x100/32][32]={{0}}; |
| 8791 |
|
| 8792 |
#define @[SET_DBIT(N,K)@] ((N)>0xFF?1:(definition_bits[N/32][K]|=(1<<((N)&(32-1))))) |
| 8793 |
#define @[GET_DBIT(N,K)@] ((N)>0xFF?1:((definition_bits[N/32][K]>>((N)&(32-1)))&1)) |
| 8794 |
#define @[DEF(D,K,N)@] (D).k=K;@+ (D).n=(N);@+SET_DBIT((D).n,(D).k);\ |
| 8795 |
DBG(DBGDEF,"Defining %s %d\n",definition_name[(D).k],(D).n);\ |
| 8796 |
RNG("Definition",(D).n,max_fixed[(D).k]+1,max_ref[(D).k]); |
| 8797 |
#define @[REF(K,N)@] REF_RNG(K,N);if(!GET_DBIT(N,K)) \ |
| 8798 |
QUIT("Reference %d to %s before definition",(N),definition_name[K]) |
| 8799 |
@ |
| 8800 |
|
| 8801 |
@<initialize definitions@>= |
| 8802 |
definition_bits[0][list_kind]=(1<<(MAX_LIST_DEFAULT+1))-1; |
| 8803 |
definition_bits[0][param_kind]=(1<<(MAX_LIST_DEFAULT+1))-1; |
| 8804 |
definition_bits[0][int_kind]=(1<<(MAX_INT_DEFAULT+1))-1; |
| 8805 |
definition_bits[0][dimen_kind]=(1<<(MAX_DIMEN_DEFAULT+1))-1; |
| 8806 |
definition_bits[0][xdimen_kind]=(1<<(MAX_XDIMEN_DEFAULT+1))-1; |
| 8807 |
definition_bits[0][glue_kind]=(1<<(MAX_GLUE_DEFAULT+1))-1; |
| 8808 |
definition_bits[0][baseline_kind]=(1<<(MAX_BASELINE_DEFAULT+1))-1; |
| 8809 |
definition_bits[0][page_kind]=(1<<(MAX_PAGE_DEFAULT+1))-1; |
| 8810 |
definition_bits[0][stream_kind]=(1<<(MAX_STREAM_DEFAULT+1))-1; |
| 8811 |
definition_bits[0][range_kind]=(1<<(MAX_RANGE_DEFAULT+1))-1; |
| 8812 |
definition_bits[0][color_kind]=(1<<(MAX_COLOR_DEFAULT+1))-1; |
| 8813 |
@ |
| 8814 |
|
| 8815 |
\goodbreak |
| 8816 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 8817 |
|
| 8818 |
|
| 8819 |
@s font symbol |
| 8820 |
@<symbols@>= |
| 8821 |
|
| 8822 |
%type <rf> def_node |
| 8823 |
@ |
| 8824 |
|
| 8825 |
@<parsing rules@>= |
| 8826 |
def_node: |
| 8827 |
start FONT ref font END @| { DEF($$,font_kind,$3);@+ hput_tags($1,$4);@+} |
| 8828 |
| start INTEGER ref integer END @| { DEF($$,int_kind,$3);@+ hput_tags($1,hput_int($4));@+} |
| 8829 |
| start DIMEN ref dimension END @| { DEF($$,dimen_kind,$3);@+ hput_tags($1,hput_dimen($4));} |
| 8830 |
| start LANGUAGE ref string END @| { DEF($$,language_kind,$3);@+hput_string($4); hput_tags($1,TAG(language_kind,0));} |
| 8831 |
| start GLUE ref glue END @| { DEF($$,glue_kind,$3);@+ hput_tags($1,hput_glue(&($4)));} |
| 8832 |
| start XDIMEN ref xdimen END @| { DEF($$,xdimen_kind,$3);@+ hput_tags($1,hput_xdimen(&($4)));} |
| 8833 |
| start RULE ref rule END @| { DEF($$,rule_kind,$3);@+ hput_tags($1,hput_rule(&($4)));} |
| 8834 |
| start LEADERS ref leaders END @| { DEF($$,leaders_kind,$3);@+ hput_tags($1,TAG(leaders_kind, $4));} |
| 8835 |
| start BASELINE ref baseline END @| { DEF($$,baseline_kind,$3);@+hput_tags($1,TAG(baseline_kind, $4));@+} |
| 8836 |
| start LIGATURE ref ligature END @| { DEF($$,ligature_kind,$3);@+hput_tags($1,hput_ligature(&($4)));} |
| 8837 |
| start DISC ref disc END @| { DEF($$,disc_kind,$3);@+ hput_tags($1,hput_disc(&($4)));} |
| 8838 |
| start IMAGE ref image END @| { DEF($$,image_kind,$3);@+ hput_tags($1,TAG(image_kind,$4));} |
| 8839 |
| start PARAM ref parameters END @| { DEF($$,param_kind,$3);@+ hput_tags($1,hput_list($1+2,&($4)));} |
| 8840 |
| start PAGE ref page END @| { DEF($$,page_kind,$3);@+ hput_tags($1,TAG(page_kind,0));}; |
| 8841 |
@ |
| 8842 |
|
| 8843 |
There are a few cases where one wants to define a reference by a reference. |
| 8844 |
For example, a \HINT\ file may want to set the {\tt parfillskip} glue to zero. |
| 8845 |
While there are multiple ways to define the zero glue, the canonical way is a reference |
| 8846 |
using the |zero_glue_no|. All these cases have in common that the reference to be defined |
| 8847 |
is one of the default references and the defining reference is one of the fixed references. |
| 8848 |
We add a few parsing rules and a testing macro for those cases where the number |
| 8849 |
of default definitions is greater than the number of fixed definitions. |
| 8850 |
|
| 8851 |
@<definition checks@>= |
| 8852 |
#define @[DEF_REF(D,K,M,N)@] DEF(D,K,M);\ |
| 8853 |
if ((int)(M)>max_default[K]) QUIT("Defining non default reference %d for %s",M,definition_name[K]); \ |
| 8854 |
if ((int)(N)>max_fixed[K]) QUIT("Defining reference %d for %s by non fixed reference %d",M,definition_name[K],N); |
| 8855 |
@ |
| 8856 |
|
| 8857 |
@<parsing rules@>= |
| 8858 |
def_node: |
| 8859 |
start INTEGER ref ref END @/{DEF_REF($$,int_kind,$3,$4); hput_tags($1,TAG(int_kind,0)); } |
| 8860 |
| start DIMEN ref ref END @/{DEF_REF($$,dimen_kind,$3,$4); hput_tags($1,TAG(dimen_kind,0)); } |
| 8861 |
| start GLUE ref ref END @/{DEF_REF($$,glue_kind,$3,$4); hput_tags($1,TAG(glue_kind,0)); }; |
| 8862 |
@ |
| 8863 |
|
| 8864 |
|
| 8865 |
|
| 8866 |
|
| 8867 |
\goodbreak |
| 8868 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 8869 |
|
| 8870 |
@<get functions@>= |
| 8871 |
void hget_definition(int n, Tag a, uint32_t node_pos) |
| 8872 |
{@+ switch(KIND(a)) |
| 8873 |
{ case font_kind: hget_font_def(INFO(a),n);@+ break; |
| 8874 |
case param_kind: |
| 8875 |
{@+ List l; l.t=a; @+HGET_LIST(INFO(a),l); @+hwrite_parameters(&l); @+ break;@+} |
| 8876 |
case page_kind: hget_page(); @+break; |
| 8877 |
case dimen_kind: hget_dimen(a); @+break; |
| 8878 |
case xdimen_kind: |
| 8879 |
{@+ Xdimen x; @+hget_xdimen(a,&x); @+hwrite_xdimen(&x); @+break;@+ } |
| 8880 |
case language_kind: |
| 8881 |
if (INFO(a)!=b000) |
| 8882 |
QUIT("Info value of language definition must be zero"); |
| 8883 |
else |
| 8884 |
{ char *n; HGET_STRING(n);@+ hwrite_string(n); } |
| 8885 |
break; |
| 8886 |
case color_kind: |
| 8887 |
switch (INFO(a)) |
| 8888 |
{ @<cases to get definitions for |color_kind|@>@; |
| 8889 |
default: |
| 8890 |
QUIT("Undefined tag %d for color_kind definition at 0x%x",INFO(a),node_pos); |
| 8891 |
} |
| 8892 |
break; |
| 8893 |
default: |
| 8894 |
hget_content(a); @+break; |
| 8895 |
} |
| 8896 |
} |
| 8897 |
|
| 8898 |
|
| 8899 |
void hget_def_node() |
| 8900 |
{ Kind k; |
| 8901 |
|
| 8902 |
@<read the start byte |a|@>@; |
| 8903 |
k=KIND(a); |
| 8904 |
if (k==unknown_kind && INFO(a)==b100) |
| 8905 |
hget_unknown_def(); |
| 8906 |
else if (k==label_kind) |
| 8907 |
hget_outline_or_label_def(INFO(a),node_pos); |
| 8908 |
else |
| 8909 |
{ int n; |
| 8910 |
n=HGET8; |
| 8911 |
if (k!=range_kind) REF_RNG(k,n); |
| 8912 |
SET_DBIT(n,k); |
| 8913 |
if (k==range_kind) |
| 8914 |
hget_range(INFO(a),n); |
| 8915 |
else |
| 8916 |
{ hwrite_start(); @+hwritef("%s *%d",definition_name[k],n); |
| 8917 |
hget_definition(n,a,node_pos); |
| 8918 |
hwrite_end(); |
| 8919 |
} |
| 8920 |
if(n>max_ref[k] || n <= max_fixed[k]) |
| 8921 |
QUIT("Definition %d for %s out of range [%d - %d]",@| |
| 8922 |
n, definition_name[k],max_fixed[k]+1,max_ref[k]); |
| 8923 |
if (max_fixed[k]>max_default[k]) |
| 8924 |
QUIT("Definitions for kind %s not supported", definition_name[k]); |
| 8925 |
} |
| 8926 |
@<read and check the end byte |z|@>@; |
| 8927 |
} |
| 8928 |
@ |
| 8929 |
|
| 8930 |
|
| 8931 |
|
| 8932 |
\subsection{Parameter Lists}\label{paramlist}\index{parameter list} |
| 8933 |
Because the content section is a ``stateless'' list of nodes, the |
| 8934 |
definitions we see in the definition section can never change. It is |
| 8935 |
however necessary to make occasionally local modifications of some of |
| 8936 |
these definitions, because some definitions are parameters of the |
| 8937 |
algorithms borrowed from \TeX. Nodes that need such modifications, for |
| 8938 |
example the paragraph nodes that are passed to \TeX's line breaking |
| 8939 |
algorithm, contain a list of local definitions called parameters. |
| 8940 |
Typically sets of related parameters are needed. To facilitate a |
| 8941 |
simple reference to such a set of parameters, we allow predefined |
| 8942 |
parameter lists that can be referenced by a single number. The |
| 8943 |
parameters of \TeX's routines are quite basic---integers\index{integer}, |
| 8944 |
dimensions\index{dimension}, and glues\index{glue}---and all |
| 8945 |
of them have default values. |
| 8946 |
Therefore we restrict the definitions in parameter lists to such |
| 8947 |
basic definitions. |
| 8948 |
|
| 8949 |
@<parsing functions@>= |
| 8950 |
void check_param_def(Ref *df) |
| 8951 |
{ if(df->k!=int_kind && df->k!=dimen_kind && @| df->k!=glue_kind) |
| 8952 |
QUIT("Kind %s not allowed in parameter list", definition_name[df->k]); |
| 8953 |
if(df->n<=max_fixed[df->k] || max_default[df->k]<df->n) |
| 8954 |
QUIT("Parameter %d for %s not allowed in parameter list", df->n, definition_name[df->k]); |
| 8955 |
} |
| 8956 |
@ |
| 8957 |
|
| 8958 |
The definitions below repeat the definitions we have seen for lists in section~\secref{plainlists} |
| 8959 |
with small modifications. For example we use the kind-value |param_kind|. An empty parameter list |
| 8960 |
is omitted in the long format as well as in the short format. |
| 8961 |
|
| 8962 |
\goodbreak |
| 8963 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 8964 |
|
| 8965 |
@s PARAM symbol |
| 8966 |
@s def_list symbol |
| 8967 |
@s parameters_node symbol |
| 8968 |
@s def_node symbol |
| 8969 |
@s parameters symbol |
| 8970 |
@s named_param_list symbol |
| 8971 |
@s param_list symbol |
| 8972 |
|
| 8973 |
@<symbols@>= |
| 8974 |
%token PARAM "param" |
| 8975 |
%type <u> def_list |
| 8976 |
%type <l> parameters |
| 8977 |
@ |
| 8978 |
|
| 8979 |
@<scanning rules@>= |
| 8980 |
::@=param@> :< return PARAM; >: |
| 8981 |
@ |
| 8982 |
@<parsing rules@>= |
| 8983 |
def_list: position @+ |
| 8984 |
| def_list def_node {check_param_def(&($2));}; |
| 8985 |
parameters: estimate def_list { $$.p=$2; $$.t=TAG(param_kind,b001); $$.s=(hpos-hstart)-$2;}; |
| 8986 |
@ |
| 8987 |
|
| 8988 |
Using a parsing rule like |
| 8989 |
``\nts{param\_list}: \nts{start} \ts{PARAM} \nts{parameters} \ts{END}'', |
| 8990 |
an empty parameter list will be written as ``\.{<param>}''. |
| 8991 |
This looks ugly and seems like unnecessary syntax because |
| 8992 |
the parser knows anyway that a parameter list will come next. |
| 8993 |
Therefore the keyword can be omited except in definitions and in unknown nodes. |
| 8994 |
|
| 8995 |
|
| 8996 |
@<parsing rules@>= |
| 8997 |
named_param_list: start PARAM parameters END @/ |
| 8998 |
{ @+ hput_tags($1,hput_list($1+1,&($3)));@+}; |
| 8999 |
param_list: named_param_list | start parameters END @/ |
| 9000 |
{ @+ hput_tags($1,hput_list($1+1,&($2)));@+}; |
| 9001 |
@ |
| 9002 |
|
| 9003 |
\writecode |
| 9004 |
@<write functions@>= |
| 9005 |
void hwrite_parameters(List *l) |
| 9006 |
{ uint32_t h=hpos-hstart, e=hend-hstart; /* save |hpos| and |hend| */ |
| 9007 |
hpos=l->p+hstart;@+ hend=hpos+l->s; |
| 9008 |
if (l->s>0xFF) hwritef(" %d",l->s); |
| 9009 |
while(hpos<hend) hget_def_node(); |
| 9010 |
hpos=hstart+h;@+ hend=hstart+e; /* restore |hpos| and |hend| */ |
| 9011 |
} |
| 9012 |
void hwrite_param_list(List *l) |
| 9013 |
{ hwrite_start();@+ |
| 9014 |
hwrite_parameters(l); |
| 9015 |
hwrite_end(); |
| 9016 |
} |
| 9017 |
|
| 9018 |
void hwrite_named_param_list(List *l) |
| 9019 |
{ hwrite_start();@+ |
| 9020 |
hwritef("param"); |
| 9021 |
hwrite_parameters(l); |
| 9022 |
hwrite_end(); |
| 9023 |
} |
| 9024 |
@ |
| 9025 |
|
| 9026 |
\getcode |
| 9027 |
@<get functions@>= |
| 9028 |
void hget_param_list(List *l) |
| 9029 |
{ @+if (KIND(*hpos)!=param_kind) @/ |
| 9030 |
QUIT("Parameter list expected at 0x%x", (uint32_t)(hpos-hstart)); |
| 9031 |
else hget_list(l); |
| 9032 |
} |
| 9033 |
@ |
| 9034 |
|
| 9035 |
|
| 9036 |
|
| 9037 |
\subsection{Fonts}\label{fonts} |
| 9038 |
Another definition that has no corresponding content node is the |
| 9039 |
font\index{font} definition. Fonts by themselves do not constitute |
| 9040 |
content, instead they are used in glyph\index{glyph} nodes. |
| 9041 |
Further, fonts are never directly embedded in a content node; in a content node, a |
| 9042 |
font is always specified by its font number. This limits the number of |
| 9043 |
fonts that can be used in a \HINT\ file to at most 256. |
| 9044 |
|
| 9045 |
A long format font definition starts with the keyword ``\.{font}'' and |
| 9046 |
is followed by the font number, as usual prefixed by an asterisk. Then |
| 9047 |
comes the font specification with the font name, the font size, |
| 9048 |
the section number of the \TeX\ font metric file, and the |
| 9049 |
section number of the file containing the glyphs for the font. |
| 9050 |
The \HINT\ format supports \.{.pk} files, the traditional font format |
| 9051 |
for \TeX, and the more modern PostScript Type 1 fonts, |
| 9052 |
TrueType fonts, and OpenType fonts. |
| 9053 |
|
| 9054 |
Starting with version 2.2, there is new support for TrueType and OpenType |
| 9055 |
fonts while \.{.pk} fonts are considered deprecated. |
| 9056 |
The previously mandatory \.{.tfm} file is no longer required for |
| 9057 |
TrueType and OpenType fonts. Instead, the hint viewer is required to |
| 9058 |
extract the necessary font metrics directly from the font files. |
| 9059 |
|
| 9060 |
In a \HINT\ file, text is represented as a sequence of numbers called |
| 9061 |
character codes. \HINT\ files use the UTF-8 character encoding |
| 9062 |
scheme (CES) to map these numbers to their representation as byte |
| 9063 |
sequences. For example the number ``|0xE4|'' is encoded as the byte |
| 9064 |
sequence ``|0xC3| |0xA4|''. The same number |0xE4| now can represent |
| 9065 |
different characters depending on the coded character set (CCS). For |
| 9066 |
example in the common ISO-8859-1 (Latin 1) encoding the number |0xE4| |
| 9067 |
is the umlaut ``\"a'' where as in the ISO-8859-7 (Latin/Greek) it is |
| 9068 |
the Greek letter ``$\delta$'' and in the EBCDIC encoding, used on IBM |
| 9069 |
mainframes, it is the upper case letter ``U''. |
| 9070 |
|
| 9071 |
The character encoding is |
| 9072 |
irrelevant for rendering a \HINT\ file as long as the character codes |
| 9073 |
in the glyph nodes are consistent with the character codes used in the font |
| 9074 |
file, but the character encoding is necessary for all programs that |
| 9075 |
need to ``understand'' the content of the \HINT\ file. For example |
| 9076 |
programs that want to translate a \HINT\ document to a different language, |
| 9077 |
or for text-to-speech conversion. |
| 9078 |
|
| 9079 |
For FreeType and OpenType fonts, the \HINT\ viewer is required to support |
| 9080 |
only two character encodings: |FT_ENCODING_ADOBE_CUSTOM|, used for the |
| 9081 |
traditional \TeX\ encoding schema of fonts; |
| 9082 |
and |FT_ENCODING_UNICODE| used for TrueType and OpenType fonts that do not have |
| 9083 |
a \.{.tfm} file along with them. To be precise: a font that has no \.{.tfm} |
| 9084 |
file along with it must be encoded in Unicode. That is a glyph node |
| 9085 |
will specify the unicode value of the desired character, which is then |
| 9086 |
translated to the glyph number using |FT_ENCODING_UNICODE|. |
| 9087 |
For ligature nodes, the node will speciy the glyph number directly without |
| 9088 |
a need to translate it further, but the replacement list of the ligature |
| 9089 |
will contain the unicode values of the replacement characters. |
| 9090 |
|
| 9091 |
In the short format we use the info value |b000| for a font with \.{.tfm} file |
| 9092 |
and the info value |b001| for a font without \.{.tfm} file. |
| 9093 |
|
| 9094 |
The Internet Engineering Task Force IETF has established a character set |
| 9095 |
registry\cite{ietf:charset-mib} that defines an enumeration of all |
| 9096 |
registered coded character sets\cite{iana:charset-mib}. The coded |
| 9097 |
character set numbers are in the range 1--2999. |
| 9098 |
This encoding number, as given in~\cite{iana:charset}, |
| 9099 |
might be one possibility for specifying the font encoding as |
| 9100 |
part of a font definition. But none such addition is planed at the moment. |
| 9101 |
|
| 9102 |
Currently, it is only required that a font specifies |
| 9103 |
an interword glue and a default discretionary break. After that comes |
| 9104 |
a list of up to 12 font specific parameters. |
| 9105 |
|
| 9106 |
The font size specifies the desired ``at size''\index{font at size} |
| 9107 |
which might be different from the ``design size''\index{font design size} |
| 9108 |
of the font as stored in the \.{.tfm} file. |
| 9109 |
|
| 9110 |
In the short format, the font specification is given in the same order |
| 9111 |
as in the long format. |
| 9112 |
|
| 9113 |
Our internal representation of a font just stores the font name |
| 9114 |
because in the long format we add the font name as a comment to glyph |
| 9115 |
nodes. |
| 9116 |
|
| 9117 |
|
| 9118 |
@<common variables@>= |
| 9119 |
char **hfont_name; /* dynamically allocated array of font names */ |
| 9120 |
@ |
| 9121 |
|
| 9122 |
@<hint basic types@>= |
| 9123 |
#define MAX_FONT_PARAMS 11 |
| 9124 |
@ |
| 9125 |
|
| 9126 |
@<initialize definitions@>= |
| 9127 |
ALLOCATE(hfont_name,max_ref[font_kind]+1,char *); |
| 9128 |
@ |
| 9129 |
|
| 9130 |
|
| 9131 |
|
| 9132 |
\readcode |
| 9133 |
@s FONT symbol |
| 9134 |
@s fref symbol |
| 9135 |
@s font_param_list symbol |
| 9136 |
@s font_param symbol |
| 9137 |
@s font_head symbol |
| 9138 |
|
| 9139 |
@<symbols@>= |
| 9140 |
%token FONT "font" |
| 9141 |
%type <info> font font_head |
| 9142 |
@ |
| 9143 |
|
| 9144 |
@<scanning rules@>= |
| 9145 |
::@=font@> :< return FONT; >: |
| 9146 |
@ |
| 9147 |
|
| 9148 |
Note that we set the definition bit early because the definition of font |f| |
| 9149 |
might involve glyphs that reference font |f| (or other fonts). |
| 9150 |
|
| 9151 |
@<parsing rules@>=@/ |
| 9152 |
|
| 9153 |
font: font_head font_param_list; |
| 9154 |
|
| 9155 |
font_head: string dimension UNSIGNED UNSIGNED @/ |
| 9156 |
{uint8_t f=$<u>@&0; SET_DBIT(f,font_kind); @+hfont_name[f]=strdup($1); $$=hput_font_head(f,hfont_name[f],$2,$3,$4);}; |
| 9157 |
font_head: string dimension UNSIGNED @/ |
| 9158 |
{uint8_t f=$<u>@&0; SET_DBIT(f,font_kind); @+hfont_name[f]=strdup($1); $$=hput_font_head(f,hfont_name[f],$2,-1,$3);}; |
| 9159 |
|
| 9160 |
|
| 9161 |
|
| 9162 |
font_param_list: glue_node disc_node @+ | font_param_list font_param ; |
| 9163 |
|
| 9164 |
font_param: @/ |
| 9165 |
start PENALTY fref penalty END { hput_tags($1,hput_int($4));} |
| 9166 |
| start KERN fref kern END { hput_tags($1,hput_kern(&($4)));} |
| 9167 |
| start LIGATURE fref ligature END { hput_tags($1,hput_ligature(&($4)));} |
| 9168 |
| start DISC fref disc END { hput_tags($1,hput_disc(&($4)));} |
| 9169 |
| start GLUE fref glue END { hput_tags($1,hput_glue(&($4)));} |
| 9170 |
| start LANGUAGE fref string END { hput_string($4);hput_tags($1,TAG(language_kind,0));} |
| 9171 |
| start RULE fref rule END { hput_tags($1,hput_rule(&($4)));} |
| 9172 |
| start IMAGE fref image END { hput_tags($1,TAG(image_kind,$4));}; |
| 9173 |
|
| 9174 |
fref: ref @/{ RNG("Font parameter",$1,0,MAX_FONT_PARAMS); }; |
| 9175 |
@ |
| 9176 |
|
| 9177 |
\goodbreak |
| 9178 |
\vbox{\getcode\vskip -\baselineskip\writecode} |
| 9179 |
|
| 9180 |
@<get functions@>= |
| 9181 |
static void hget_font_params(void) |
| 9182 |
{ Disc h; |
| 9183 |
hget_glue_node(); |
| 9184 |
hget_disc_node(&(h));@+ hwrite_disc_node(&h); |
| 9185 |
DBG(DBGDEF,"Start font parameters\n"); |
| 9186 |
while (KIND(*hpos)!=font_kind)@/ |
| 9187 |
{ Ref df; |
| 9188 |
@<read the start byte |a|@>@; |
| 9189 |
df.k=KIND(a); |
| 9190 |
df.n=HGET8; |
| 9191 |
DBG(DBGDEF,"Reading font parameter %d: %s\n",df.n, definition_name[df.k]); |
| 9192 |
if (df.k!=penalty_kind && df.k!=kern_kind && df.k!=ligature_kind && @| |
| 9193 |
df.k!=disc_kind && df.k!=glue_kind && df.k!=language_kind && @| df.k!=rule_kind && df.k!=image_kind) |
| 9194 |
QUIT("Font parameter %d has invalid type %s",df.n, content_name[df.n]); |
| 9195 |
RNG("Font parameter",df.n,0,MAX_FONT_PARAMS); |
| 9196 |
hwrite_start(); @+ hwritef("%s *%d",content_name[KIND(a)],df.n); |
| 9197 |
hget_definition(df.n,a,node_pos); |
| 9198 |
hwrite_end(); |
| 9199 |
@<read and check the end byte |z|@>@; |
| 9200 |
} |
| 9201 |
DBG(DBGDEF,"End font parameters\n"); |
| 9202 |
} |
| 9203 |
|
| 9204 |
|
| 9205 |
void hget_font_def(Info i, uint8_t f) |
| 9206 |
{ char *n; @+Dimen s=0;@+uint16_t m=0,y; |
| 9207 |
HGET_STRING(n);@+ hwrite_string(n);@+ hfont_name[f]=strdup(n); |
| 9208 |
HGET32(s); @+ hwrite_dimension(s); |
| 9209 |
DBG(DBGDEF,"Font %s size 0x%x\n", n, s); |
| 9210 |
if (i==b000) { HGET16(m); @+RNG("Font metrics",m,3,max_section_no); } |
| 9211 |
HGET16(y); @+RNG("Font glyphs",y,3,max_section_no); |
| 9212 |
if (i==b000) hwritef(" %d",m); |
| 9213 |
hwritef(" %d",y); |
| 9214 |
hget_font_params(); |
| 9215 |
DBG(DBGDEF,"End font definition\n"); |
| 9216 |
} |
| 9217 |
@ |
| 9218 |
|
| 9219 |
\putcode |
| 9220 |
@<put functions@>= |
| 9221 |
Tag hput_font_head(uint8_t f, char *n, Dimen s, @| int m, uint16_t y) |
| 9222 |
{ Info i; |
| 9223 |
|
| 9224 |
DBG(DBGDEF,"Defining font %d (%s) size 0x%x\n", f, n, s); |
| 9225 |
hput_string(n); |
| 9226 |
HPUT32(s);@+ |
| 9227 |
if (m>=0) {i=b000; HPUT16(m);} else i=b001; |
| 9228 |
HPUT16(y); |
| 9229 |
return TAG(font_kind,i); |
| 9230 |
} |
| 9231 |
@ |
| 9232 |
|
| 9233 |
|
| 9234 |
|
| 9235 |
\subsection{References}\label{reference} |
| 9236 |
We have seen how to make definitions, now let's see how to |
| 9237 |
reference\index{reference} them. In the long form, we can simply |
| 9238 |
write the reference number, after the keyword like this: |
| 9239 |
``{\tt \.{<}glue *17\.{>}}''. |
| 9240 |
The asterisk\index{asterisk} is necessary to keep apart, |
| 9241 |
for example, a penalty with value 50, |
| 9242 |
written ``{\tt \.{<}penalty 50\.{>}}'', |
| 9243 |
from a penalty referencing the integer |
| 9244 |
definition number 50, written ``{\tt \.{<}penalty *50\.{>}}''. |
| 9245 |
|
| 9246 |
@<hint types@>= |
| 9247 |
typedef struct { @+Kind k; @+int n; @+} Ref; |
| 9248 |
@ |
| 9249 |
|
| 9250 |
\goodbreak |
| 9251 |
\vbox{\readcode\vskip -\baselineskip\putcode} |
| 9252 |
|
| 9253 |
@<parsing rules@>= |
| 9254 |
xdimen_ref: ref { REF(xdimen_kind,$1);}; |
| 9255 |
param_ref: ref { REF(param_kind,$1); }; |
| 9256 |
stream_ref: ref { REF_RNG(stream_kind,$1); }; |
| 9257 |
|
| 9258 |
|
| 9259 |
content_node: |
| 9260 |
start PENALTY ref END @/{ REF(penalty_kind,$3); @+ hput_tags($1,TAG(penalty_kind,0)); } |
| 9261 |
|start KERN explicit ref END @/ |
| 9262 |
{ REF(dimen_kind,$4); @+ hput_tags($1,TAG(kern_kind,($3)?b100:b000)); } |
| 9263 |
|start KERN explicit XDIMEN ref END @/ |
| 9264 |
{ REF(xdimen_kind,$5); @+hput_tags($1,TAG(kern_kind,($3)?b101:b001)); } |
| 9265 |
|start GLUE ref END @/{ REF(glue_kind,$3); @+ hput_tags($1,TAG(glue_kind,0)); } |
| 9266 |
|start LIGATURE ref END @/{ REF(ligature_kind,$3); @+ hput_tags($1,TAG(ligature_kind,0)); } |
| 9267 |
|start DISC ref END @/{ REF(disc_kind,$3); @+ hput_tags($1,TAG(disc_kind,0)); } |
| 9268 |
|start RULE ref END @/{ REF(rule_kind,$3); @+ hput_tags($1,TAG(rule_kind,0)); } |
| 9269 |
|start IMAGE ref END @/{ REF(image_kind,$3);@+ hput_tags($1,TAG(image_kind,0)); } |
| 9270 |
|start LEADERS ref END @/{ REF(leaders_kind,$3); @+ hput_tags($1,TAG(leaders_kind,0)); } |
| 9271 |
|start BASELINE ref END @/{ REF(baseline_kind,$3);@+ hput_tags($1,TAG(baseline_kind,0)); } |
| 9272 |
|start LANGUAGE REFERENCE END @/{ REF(language_kind,$3);@+ hput_tags($1,hput_language($3)); }; |
| 9273 |
|
| 9274 |
glue_node: start GLUE ref END @/{ REF(glue_kind,$3); |
| 9275 |
if ($3==zero_skip_no) { hpos=hpos-2; $$=false;@+ } |
| 9276 |
else {hput_tags($1,TAG(glue_kind,0)); $$=true;@t\2@>@+}}; |
| 9277 |
|
| 9278 |
@ |
| 9279 |
|
| 9280 |
\getcode |
| 9281 |
@<cases to get content@>= |
| 9282 |
@t\1\kern1em@> |
| 9283 |
case TAG(penalty_kind,0): HGET_REF(penalty_kind); @+break; |
| 9284 |
case TAG(kern_kind,b000): HGET_REF(dimen_kind); @+break; |
| 9285 |
case TAG(kern_kind,b100): hwritef(" !"); @+HGET_REF(dimen_kind); @+break; |
| 9286 |
case TAG(kern_kind,b001): @| hwritef(" xdimen");@+ HGET_REF(xdimen_kind); @+break; |
| 9287 |
case TAG(kern_kind,b101): @| hwritef(" ! xdimen");@+ HGET_REF(xdimen_kind); @+break; |
| 9288 |
case TAG(ligature_kind,0): HGET_REF(ligature_kind); @+break; |
| 9289 |
case TAG(disc_kind,0): HGET_REF(disc_kind); @+break; |
| 9290 |
case TAG(glue_kind,0): HGET_REF(glue_kind); @+break; |
| 9291 |
case TAG(language_kind,b000): HGET_REF(language_kind); @+break; |
| 9292 |
case TAG(rule_kind,0): HGET_REF(rule_kind); @+break; |
| 9293 |
case TAG(image_kind,0): HGET_REF(image_kind); @+break; |
| 9294 |
case TAG(leaders_kind,0): HGET_REF(leaders_kind); @+break; |
| 9295 |
case TAG(baseline_kind,0): HGET_REF(baseline_kind); @+break; |
| 9296 |
@ |
| 9297 |
|
| 9298 |
@<get macros@>= |
| 9299 |
#define @[HGET_REF(K)@] {uint8_t n=HGET8;@+ REF(K,n); @+hwrite_ref(n);@+} |
| 9300 |
@ |
| 9301 |
\writecode |
| 9302 |
@<write functions@>= |
| 9303 |
void hwrite_ref(int n) |
| 9304 |
{hwritef(" *%d",n);@+} |
| 9305 |
|
| 9306 |
void hwrite_ref_node(Kind k, uint8_t n) |
| 9307 |
{ hwrite_start(); @+hwritef("%s",content_name[k]);@+ hwrite_ref(n); @+hwrite_end();} |
| 9308 |
@ |
| 9309 |
|
| 9310 |
|
| 9311 |
|
| 9312 |
\section{Defaults}\label{defaults}\index{default value} |
| 9313 |
Several of the predefined values found in the definition section are used |
| 9314 |
as parameters for the routines borrowed from \TeX\ to display the content |
| 9315 |
of a \HINT\ file. These values must be defined, but it is inconvenient if |
| 9316 |
the same standard definitions need to be placed in each and every \HINT\ file. |
| 9317 |
Therefore we specify in this chapter reasonable default values. |
| 9318 |
As a consequence, even a \HINT\ file without any definitions should |
| 9319 |
produce sensible results when displayed. |
| 9320 |
|
| 9321 |
The definitions that have default values are integers, dimensions, |
| 9322 |
extended dimensions, glues, baselines, labels, page templates, |
| 9323 |
streams, and page ranges. |
| 9324 |
Each of these defaults has its own subsection below. |
| 9325 |
Actually the defaults for extended dimensions, baselines, and labels |
| 9326 |
are not needed by \TeX's routines, but it is nice to have default |
| 9327 |
values for the extended dimensions that represent |
| 9328 |
\.{hsize}, \.{vsize}, a zero baseline skip, and a label for the table |
| 9329 |
of content. |
| 9330 |
|
| 9331 |
The array |max_default| contains for each kind-value the maximum number of |
| 9332 |
the default values. The function |hset_max| is used to initialize them. |
| 9333 |
|
| 9334 |
The programs \.{shrink} and \.{stretch} actually do not use the defaults, |
| 9335 |
but it would be possible to suppress definitions if the defined value |
| 9336 |
is the same as the default value. |
| 9337 |
% |
| 9338 |
We start by setting |max_default[k]==-1|, meaning no defaults, |
| 9339 |
and |max_fixed[k]==0x10000|, meaning no definitions. |
| 9340 |
The following subsections will then overwrite these values for |
| 9341 |
all kinds of definitions that have defaults. |
| 9342 |
It remains to reset |max_fixed| to $-1$ for all those kinds |
| 9343 |
that have no defaults but allow definitions. |
| 9344 |
|
| 9345 |
@<take care of variables without defaults@>= |
| 9346 |
for (k=0; k<32; k++) max_default[k]=-1,max_fixed[k]=0x10000; |
| 9347 |
@/@t}$\hangindent=1em${@>max_fixed[font_kind]= max_fixed[ligature_kind]= max_fixed[disc_kind] |
| 9348 |
@|=max_fixed[language_kind]=max_fixed[rule_kind]= max_fixed[image_kind] |
| 9349 |
@|= max_fixed[leaders_kind]= max_fixed[param_kind]=max_fixed[label_kind]@|= -1; |
| 9350 |
@ |
| 9351 |
|
| 9352 |
|
| 9353 |
\subsection{Integers} |
| 9354 |
Integers\index{integer} are very simple objects, and it might be tempting not to |
| 9355 |
use predefined integers at all. But the \TeX\ typesetting engine, |
| 9356 |
which is used by \HINT, uses many integer parameters to fine tune |
| 9357 |
its operations. As we will see, all these integer parameters have a predefined |
| 9358 |
integer number that refers to an integer definition. |
| 9359 |
|
| 9360 |
Integers and penalties\index{penalty} share the same kind-value. So a penalty node that references |
| 9361 |
one of the predefined penalties, simply contains the integer number as a reference |
| 9362 |
number. |
| 9363 |
|
| 9364 |
The following integer numbers are predefined. |
| 9365 |
The zero integer is fixed with integer number zero. %It is never redefined. |
| 9366 |
The default values are taken from {\tt plain.tex}. |
| 9367 |
|
| 9368 |
@<default names@>= |
| 9369 |
typedef enum {@t}$\hangindent=2em${@> |
| 9370 |
zero_int_no=0, |
| 9371 |
pretolerance_no=1, |
| 9372 |
tolerance_no=2, |
| 9373 |
line_penalty_no=3, |
| 9374 |
hyphen_penalty_no=4, |
| 9375 |
ex_hyphen_penalty_no=5, |
| 9376 |
club_penalty_no=6, |
| 9377 |
widow_penalty_no=7, |
| 9378 |
display_widow_penalty_no=8, |
| 9379 |
broken_penalty_no=9, |
| 9380 |
pre_display_penalty_no=10, |
| 9381 |
post_display_penalty_no=11, |
| 9382 |
inter_line_penalty_no=12, |
| 9383 |
double_hyphen_demerits_no=13, |
| 9384 |
final_hyphen_demerits_no=14, |
| 9385 |
adj_demerits_no=15, |
| 9386 |
looseness_no=16, |
| 9387 |
time_no=17, |
| 9388 |
day_no=18, |
| 9389 |
month_no=19, |
| 9390 |
year_no=20, |
| 9391 |
hang_after_no=21, |
| 9392 |
floating_penalty_no=22 |
| 9393 |
} Int_no; |
| 9394 |
#define MAX_INT_DEFAULT floating_penalty_no |
| 9395 |
@ |
| 9396 |
|
| 9397 |
@<define |int_defaults|@>= |
| 9398 |
max_default[int_kind]=MAX_INT_DEFAULT; |
| 9399 |
max_fixed[int_kind]=zero_int_no; |
| 9400 |
int_defaults[zero_int_no]=0; |
| 9401 |
int_defaults[pretolerance_no]=100; |
| 9402 |
int_defaults[tolerance_no]=200; |
| 9403 |
int_defaults[line_penalty_no]=10; |
| 9404 |
int_defaults[hyphen_penalty_no]=50; |
| 9405 |
int_defaults[ex_hyphen_penalty_no]=50; |
| 9406 |
int_defaults[club_penalty_no]=150; |
| 9407 |
int_defaults[widow_penalty_no]=150; |
| 9408 |
int_defaults[display_widow_penalty_no]=50; |
| 9409 |
int_defaults[broken_penalty_no]=100; |
| 9410 |
int_defaults[pre_display_penalty_no]=10000; |
| 9411 |
int_defaults[post_display_penalty_no]=0; |
| 9412 |
int_defaults[inter_line_penalty_no]=0; |
| 9413 |
int_defaults[double_hyphen_demerits_no]=10000; |
| 9414 |
int_defaults[final_hyphen_demerits_no]=5000; |
| 9415 |
int_defaults[adj_demerits_no]=10000; |
| 9416 |
int_defaults[looseness_no]=0; |
| 9417 |
int_defaults[time_no]=720; |
| 9418 |
int_defaults[day_no]=4; |
| 9419 |
int_defaults[month_no]=7; |
| 9420 |
int_defaults[year_no]=1776; |
| 9421 |
int_defaults[hang_after_no]=1; |
| 9422 |
int_defaults[floating_penalty_no]=20000; |
| 9423 |
@# |
| 9424 |
|
| 9425 |
printf("int32_t int_defaults[MAX_INT_DEFAULT+1]={"); |
| 9426 |
for (i=0; i<= max_default[int_kind];i++)@/ |
| 9427 |
{ printf("%d",int_defaults[i]);@+ |
| 9428 |
if (i<max_default[int_kind]) printf(", ");@+ |
| 9429 |
} |
| 9430 |
printf("};\n\n"); |
| 9431 |
@ |
| 9432 |
|
| 9433 |
\subsection{Dimensions} |
| 9434 |
|
| 9435 |
Notice that there are default values for the two dimensions \.{hsize} and \.{vsize}. |
| 9436 |
These are the ``design sizes'' for the hint file. While it might not be possible |
| 9437 |
to display the \HINT\ file using these values of \.{hsize} and \.{vsize}, |
| 9438 |
these are the author's recommendation for the best ``viewing experience''. |
| 9439 |
|
| 9440 |
\noindent |
| 9441 |
@<default names@>= |
| 9442 |
typedef enum {@t}$\hangindent=2em${@> |
| 9443 |
zero_dimen_no=0, |
| 9444 |
hsize_dimen_no=1, |
| 9445 |
vsize_dimen_no=2, |
| 9446 |
line_skip_limit_no=3, |
| 9447 |
max_depth_no=4, |
| 9448 |
split_max_depth_no=5, |
| 9449 |
hang_indent_no=6, |
| 9450 |
emergency_stretch_no=7, |
| 9451 |
quad_no=8, |
| 9452 |
math_quad_no=9 |
| 9453 |
} Dimen_no; |
| 9454 |
#define MAX_DIMEN_DEFAULT math_quad_no |
| 9455 |
@ |
| 9456 |
|
| 9457 |
@<define |dimen_defaults|@>= |
| 9458 |
max_default[dimen_kind]=MAX_DIMEN_DEFAULT; |
| 9459 |
max_fixed[dimen_kind]=zero_dimen_no;@# |
| 9460 |
dimen_defaults[zero_dimen_no]=0; |
| 9461 |
dimen_defaults[hsize_dimen_no]=(Dimen)(6.5*72.27*ONE); |
| 9462 |
dimen_defaults[vsize_dimen_no]=(Dimen)(8.9*72.27*ONE); |
| 9463 |
dimen_defaults[line_skip_limit_no]=0; |
| 9464 |
dimen_defaults[split_max_depth_no]=(Dimen)(3.5*ONE); |
| 9465 |
dimen_defaults[hang_indent_no]=0; |
| 9466 |
dimen_defaults[emergency_stretch_no]=0; |
| 9467 |
dimen_defaults[quad_no]=10*ONE; |
| 9468 |
dimen_defaults[math_quad_no]=10*ONE;@# |
| 9469 |
|
| 9470 |
printf("Dimen dimen_defaults[MAX_DIMEN_DEFAULT+1]={"); |
| 9471 |
for (i=0; i<= max_default[dimen_kind];i++) |
| 9472 |
{ printf("0x%x",dimen_defaults[i]); |
| 9473 |
if (i<max_default[dimen_kind]) printf(", "); |
| 9474 |
} |
| 9475 |
printf("};\n\n"); |
| 9476 |
@ |
| 9477 |
|
| 9478 |
\subsection{Extended Dimensions} |
| 9479 |
Extended dimensions\index{extended dimension} can be used in a variety of nodes for example |
| 9480 |
kern\index{kern} and box\index{box} nodes. |
| 9481 |
We define three fixed extended dimensions: zero, hsize, and vsize. |
| 9482 |
In contrast to the \.{hsize} and \.{vsize} dimensions defined in the previous |
| 9483 |
section, the extended dimensions defined here are linear functions that always evaluate |
| 9484 |
to the current horizontal and vertical size in the viewer. |
| 9485 |
|
| 9486 |
@<default names@>= |
| 9487 |
typedef enum { |
| 9488 |
zero_xdimen_no=0, |
| 9489 |
hsize_xdimen_no=1, |
| 9490 |
vsize_xdimen_no=2 |
| 9491 |
} Xdimen_no; |
| 9492 |
#define MAX_XDIMEN_DEFAULT vsize_xdimen_no |
| 9493 |
@ |
| 9494 |
|
| 9495 |
@<define |xdimen_defaults|@>= |
| 9496 |
max_default[xdimen_kind]=MAX_XDIMEN_DEFAULT; |
| 9497 |
max_fixed[xdimen_kind]=vsize_xdimen_no;@# |
| 9498 |
|
| 9499 |
printf("Xdimen xdimen_defaults[MAX_XDIMEN_DEFAULT+1]={"@/ |
| 9500 |
"{0x0, 0.0, 0.0}, {0x0, 1.0, 0.0}, {0x0, 0.0, 1.0}"@/ |
| 9501 |
"};\n\n"); |
| 9502 |
@ |
| 9503 |
|
| 9504 |
|
| 9505 |
\subsection{Glue} |
| 9506 |
|
| 9507 |
There are predefined glue\index{glue} numbers that correspond to the skip parameters of \TeX. |
| 9508 |
The default values are taken from {\tt plain.tex}. |
| 9509 |
|
| 9510 |
@<default names@>= |
| 9511 |
typedef enum {@t}$\hangindent=2em${@> |
| 9512 |
zero_skip_no=0, |
| 9513 |
fil_skip_no=1, |
| 9514 |
fill_skip_no=2, |
| 9515 |
line_skip_no=3, |
| 9516 |
baseline_skip_no=4, |
| 9517 |
above_display_skip_no=5, |
| 9518 |
below_display_skip_no=6, |
| 9519 |
above_display_short_skip_no=7, |
| 9520 |
below_display_short_skip_no=8, |
| 9521 |
left_skip_no=9, |
| 9522 |
right_skip_no=10, |
| 9523 |
top_skip_no=11, |
| 9524 |
split_top_skip_no=12, |
| 9525 |
tab_skip_no=13, |
| 9526 |
par_fill_skip_no=14 |
| 9527 |
} Glue_no; |
| 9528 |
#define MAX_GLUE_DEFAULT par_fill_skip_no |
| 9529 |
@ |
| 9530 |
|
| 9531 |
@<define |glue_defaults|@>= |
| 9532 |
max_default[glue_kind]=MAX_GLUE_DEFAULT; |
| 9533 |
max_fixed[glue_kind]=fill_skip_no; |
| 9534 |
|
| 9535 |
glue_defaults[fil_skip_no].p.f=1.0; |
| 9536 |
glue_defaults[fil_skip_no].p.o=fil_o; |
| 9537 |
|
| 9538 |
glue_defaults[fill_skip_no].p.f=1.0; |
| 9539 |
glue_defaults[fill_skip_no].p.o=fill_o;@# |
| 9540 |
|
| 9541 |
glue_defaults[line_skip_no].w.w=1*ONE; |
| 9542 |
glue_defaults[baseline_skip_no].w.w=12*ONE; |
| 9543 |
|
| 9544 |
glue_defaults[above_display_skip_no].w.w=12*ONE; |
| 9545 |
glue_defaults[above_display_skip_no].p.f=3.0; |
| 9546 |
glue_defaults[above_display_skip_no].p.o=normal_o; |
| 9547 |
glue_defaults[above_display_skip_no].m.f=9.0; |
| 9548 |
glue_defaults[above_display_skip_no].m.o=normal_o; |
| 9549 |
|
| 9550 |
glue_defaults[below_display_skip_no].w.w=12*ONE; |
| 9551 |
glue_defaults[below_display_skip_no].p.f=3.0; |
| 9552 |
glue_defaults[below_display_skip_no].p.o=normal_o; |
| 9553 |
glue_defaults[below_display_skip_no].m.f=9.0; |
| 9554 |
glue_defaults[below_display_skip_no].m.o=normal_o; |
| 9555 |
|
| 9556 |
glue_defaults[above_display_short_skip_no].p.f=3.0; |
| 9557 |
glue_defaults[above_display_short_skip_no].p.o=normal_o; |
| 9558 |
|
| 9559 |
glue_defaults[below_display_short_skip_no].w.w=7*ONE; |
| 9560 |
glue_defaults[below_display_short_skip_no].p.f=3.0; |
| 9561 |
glue_defaults[below_display_short_skip_no].p.o=normal_o; |
| 9562 |
glue_defaults[below_display_short_skip_no].m.f=4.0; |
| 9563 |
glue_defaults[below_display_short_skip_no].m.o=normal_o; |
| 9564 |
|
| 9565 |
glue_defaults[top_skip_no].w.w=10*ONE; |
| 9566 |
glue_defaults[split_top_skip_no].w.w=(Dimen)8.5*ONE; |
| 9567 |
|
| 9568 |
glue_defaults[par_fill_skip_no].p.f=1.0; |
| 9569 |
glue_defaults[par_fill_skip_no].p.o=fil_o; |
| 9570 |
|
| 9571 |
#define @[PRINT_GLUE(G)@] \ |
| 9572 |
@[printf("{{0x%x, %f, %f},{%f, %d},{%f, %d}}",\ |
| 9573 |
G.w.w, G.w.h, G.w.v, G.p.f, G.p.o, G.m.f,G.m.o)@]@# |
| 9574 |
|
| 9575 |
printf("Glue glue_defaults[MAX_GLUE_DEFAULT+1]={\n"); |
| 9576 |
for (i=0; i<= max_default[glue_kind];i++)@/ |
| 9577 |
{ PRINT_GLUE(glue_defaults[i]); @+ |
| 9578 |
if (i<max_default[int_kind]) printf(",\n"); |
| 9579 |
} |
| 9580 |
printf("};\n\n"); |
| 9581 |
@ |
| 9582 |
|
| 9583 |
We fix the glue definition with number zero to be the ``zero glue'': a |
| 9584 |
glue with width zero and zero stretchability and shrinkability. Here |
| 9585 |
is the reason: In the short format, the info bits of a glue node |
| 9586 |
indicate which components of a glue are nonzero. Therefore the zero |
| 9587 |
glue should have an info value of zero---which on the other hand is |
| 9588 |
reserved for a reference to a glue definition. Hence, the best way to |
| 9589 |
represent a zero glue is as a predefined glue. |
| 9590 |
|
| 9591 |
|
| 9592 |
\subsection{Baseline Skips} |
| 9593 |
|
| 9594 |
The zero baseline\index{baseline skip} which inserts no baseline skip is predefined. |
| 9595 |
|
| 9596 |
@<default names@>= |
| 9597 |
typedef enum {@+ |
| 9598 |
zero_baseline_no=0@+ |
| 9599 |
} Baseline_no; |
| 9600 |
#define MAX_BASELINE_DEFAULT zero_baseline_no |
| 9601 |
@ |
| 9602 |
@<define |baseline_defaults|@>= |
| 9603 |
max_default[baseline_kind]=MAX_BASELINE_DEFAULT; |
| 9604 |
max_fixed[baseline_kind]=zero_baseline_no;@# |
| 9605 |
{ Baseline z={{{0}}}; |
| 9606 |
printf("Baseline baseline_defaults[MAX_BASELINE_DEFAULT+1]={{"); |
| 9607 |
PRINT_GLUE(z.bs); @+printf(", "); @+PRINT_GLUE(z.ls); printf(", 0x%x}};\n\n",z.lsl); |
| 9608 |
} |
| 9609 |
@ |
| 9610 |
|
| 9611 |
\subsection{Labels} |
| 9612 |
The zero label\index{label} is predefined. It should point to the |
| 9613 |
``home'' position of the document which should be the position |
| 9614 |
where a user can start reading or navigating the document. |
| 9615 |
For a short document this is usually the start of the document, |
| 9616 |
and hence, the default is the first position of the content section. |
| 9617 |
For a larger document, the home position could point to the |
| 9618 |
table of content where a reader will find links to other parts |
| 9619 |
of the document. |
| 9620 |
|
| 9621 |
@<default names@>= |
| 9622 |
typedef enum {@+ |
| 9623 |
zero_label_no=0@+ |
| 9624 |
} Label_no; |
| 9625 |
#define MAX_LABEL_DEFAULT zero_label_no |
| 9626 |
@ |
| 9627 |
@<define |label_defaults|@>= |
| 9628 |
max_default[label_kind]=MAX_LABEL_DEFAULT; |
| 9629 |
printf("Label label_defaults[MAX_LABEL_DEFAULT+1]="@|"{{0,0,LABEL_TOP,true,0,0}};\n\n"); |
| 9630 |
@ |
| 9631 |
|
| 9632 |
|
| 9633 |
\subsection{Streams} |
| 9634 |
The zero stream\index{stream} is predefined for the main content. |
| 9635 |
@<default names@>= |
| 9636 |
typedef enum {@+ |
| 9637 |
zero_stream_no=0@+ |
| 9638 |
} Stream_no; |
| 9639 |
#define MAX_STREAM_DEFAULT zero_stream_no |
| 9640 |
@ |
| 9641 |
|
| 9642 |
@<define stream defaults@>= |
| 9643 |
max_default[stream_kind]=MAX_STREAM_DEFAULT; |
| 9644 |
max_fixed[stream_kind]=zero_stream_no; |
| 9645 |
@ |
| 9646 |
|
| 9647 |
|
| 9648 |
\subsection{Page Templates} |
| 9649 |
|
| 9650 |
The zero page template\index{template} is a predefined, built-in page template. |
| 9651 |
@<default names@>= |
| 9652 |
typedef enum {@+ |
| 9653 |
zero_page_no=0@+ |
| 9654 |
} Page_no; |
| 9655 |
#define MAX_PAGE_DEFAULT zero_page_no |
| 9656 |
@ |
| 9657 |
|
| 9658 |
@<define page defaults@>= |
| 9659 |
max_default[page_kind]=MAX_PAGE_DEFAULT; |
| 9660 |
max_fixed[page_kind]=zero_page_no; |
| 9661 |
@ |
| 9662 |
|
| 9663 |
\subsection{Page Ranges} |
| 9664 |
|
| 9665 |
The page\index{page range} range for the zero page template is |
| 9666 |
the entire content section. |
| 9667 |
|
| 9668 |
@<default names@>= |
| 9669 |
typedef enum {@+ |
| 9670 |
zero_range_no=0@+ |
| 9671 |
} Range_no; |
| 9672 |
#define MAX_RANGE_DEFAULT zero_range_no |
| 9673 |
@ |
| 9674 |
|
| 9675 |
@<define range defaults@>= |
| 9676 |
max_default[range_kind]=MAX_RANGE_DEFAULT; |
| 9677 |
max_fixed[range_kind]=zero_range_no; |
| 9678 |
@ |
| 9679 |
|
| 9680 |
\subsection{List, Texts, and Parameters} |
| 9681 |
@<default names@>= |
| 9682 |
typedef enum {@+ |
| 9683 |
empty_list_no=0@+ |
| 9684 |
} List_no; |
| 9685 |
#define MAX_LIST_DEFAULT empty_list_no |
| 9686 |
@ |
| 9687 |
|
| 9688 |
@<define range defaults@>= |
| 9689 |
max_default[list_kind]=MAX_LIST_DEFAULT; |
| 9690 |
max_fixed[list_kind]=empty_list_no; |
| 9691 |
max_default[param_kind]=MAX_LIST_DEFAULT; |
| 9692 |
max_fixed[param_kind]=empty_list_no; |
| 9693 |
@ |
| 9694 |
|
| 9695 |
\subsection{Colors}\label{colordefault} |
| 9696 |
@<default names@>= |
| 9697 |
typedef enum {@+ |
| 9698 |
zero_color_no=0, link_color_no=1@+ |
| 9699 |
} Color_no; |
| 9700 |
#define MAX_COLOR_DEFAULT link_color_no |
| 9701 |
@ |
| 9702 |
|
| 9703 |
The default colors for day mode are |
| 9704 |
black on white, red on white, and green on white; |
| 9705 |
the links in day mode are blue. |
| 9706 |
In night mode the background becomes black, the normal text white |
| 9707 |
and the other colors become slightly lighter. |
| 9708 |
|
| 9709 |
We store the default color set using an byte array in RGBA format for colors; |
| 9710 |
we combine a pair of colors for foreground and background in an array; |
| 9711 |
we combine three pairs for normal, mark, and focus text in an array; |
| 9712 |
and we define a color set as two such pairs, one for day and one for night mode |
| 9713 |
to define the default colors. |
| 9714 |
|
| 9715 |
|
| 9716 |
@<define |color_defaults|@>= |
| 9717 |
max_default[color_kind]=MAX_COLOR_DEFAULT; |
| 9718 |
max_fixed[color_kind]=-1; |
| 9719 |
printf("ColorSet color_defaults[MAX_COLOR_DEFAULT+1]=\n"@| |
| 9720 |
"{{0x000000FF, 0xFFFFFF00,\n" /* black on white */@| |
| 9721 |
" 0xEE0000FF, 0xFFFFFF00,\n" /* dark red */ |
| 9722 |
" 0x00EE00FF, 0xFFFFFF00,\n" /* dark green */@| |
| 9723 |
" 0xFFFFFFFF, 0x00000000," /* white on black */ |
| 9724 |
" 0xFF1111FF, 0x00000000,\n" /* light red */ |
| 9725 |
" 0x11FF11FF, 0x00000000},\n" /* light green*/@| |
| 9726 |
" {0x0000EEFF, 0xFFFFFF00,\n" /* dark blue on white */ |
| 9727 |
" 0xEE0000FF, 0xFFFFFF00,\n" /* dark red on white */ |
| 9728 |
" 0x00EE00FF, 0xFFFFFF00,\n" /* dark green on white */@| |
| 9729 |
" 0x1111FFFF, 0x00000000,\n" /* light blue on black */ |
| 9730 |
" 0xFF1111FF, 0x00000000,\n" /* light red on black */ |
| 9731 |
" 0x11FF11FF, 0x00000000\n" /* light green on black */ |
| 9732 |
"}};\n\n"); |
| 9733 |
@ |
| 9734 |
|
| 9735 |
|
| 9736 |
\section{Content Section} |
| 9737 |
The content section\index{content section} is just a list of nodes. |
| 9738 |
Within the \.{shrink} program, |
| 9739 |
reading a node in long format will trigger writing the node in short format. |
| 9740 |
Similarly within the \.{stretch} program, reading a node |
| 9741 |
in short form will cause writing it in long format. As a consequence, |
| 9742 |
the main task of writing the content section in long format is accomplished |
| 9743 |
by calling |get_content| and writing it in the short format |
| 9744 |
is accomplished by parsing the |content_list|. |
| 9745 |
|
| 9746 |
%\readcode |
| 9747 |
\codesection{\redsymbol}{Reading the Long Format}\redindex{1}{6}{Content Section} |
| 9748 |
\label{content}% |
| 9749 |
@s CONTENT symbol |
| 9750 |
@<symbols@>= |
| 9751 |
%token CONTENT "content" |
| 9752 |
@ |
| 9753 |
|
| 9754 |
@<scanning rules@>= |
| 9755 |
::@=content@> :< return CONTENT; >: |
| 9756 |
@ |
| 9757 |
|
| 9758 |
|
| 9759 |
@<parsing rules@>= |
| 9760 |
content_section: START CONTENT { hput_content_start(); } @| content_list END @| |
| 9761 |
{ hput_content_end(); hput_range_defs(); hput_label_defs(); }; |
| 9762 |
@ |
| 9763 |
|
| 9764 |
%\writecode |
| 9765 |
\codesection{\wrtsymbol}{Writing the Long Format}\wrtindex{1}{6}{Content Section} |
| 9766 |
|
| 9767 |
@<write functions@>= |
| 9768 |
void hwrite_content_section(void) |
| 9769 |
{ section_no=2; |
| 9770 |
hwritef("<content"); |
| 9771 |
hsort_ranges(); |
| 9772 |
hsort_labels(); |
| 9773 |
hget_content_section(); |
| 9774 |
hwritef("\n>\n"); |
| 9775 |
} |
| 9776 |
@ |
| 9777 |
|
| 9778 |
%\getcode |
| 9779 |
\codesection{\getsymbol}{Reading the Short Format}\getindex{1}{6}{Content Section} |
| 9780 |
@<get functions@>= |
| 9781 |
void hget_content_section() |
| 9782 |
{ DBG(DBGBASIC|DBGDIR,"Content\n"); |
| 9783 |
hget_section(2); |
| 9784 |
hwrite_range(); |
| 9785 |
hwrite_label(); |
| 9786 |
while(hpos<hend) |
| 9787 |
hget_content_node(); |
| 9788 |
} |
| 9789 |
@ |
| 9790 |
|
| 9791 |
%\putcode |
| 9792 |
\codesection{\putsymbol}{Writing the Short Format}\putindex{1}{6}{Content Section} |
| 9793 |
@<put functions@>= |
| 9794 |
void hput_content_start(void) |
| 9795 |
{ DBG(DBGDIR,"Content Section\n"); |
| 9796 |
section_no=2; |
| 9797 |
hpos0=hpos=hstart=dir[2].buffer; |
| 9798 |
hend=hstart+dir[2].bsize; |
| 9799 |
|
| 9800 |
} |
| 9801 |
void hput_content_end(void) |
| 9802 |
{ |
| 9803 |
dir[2].size=hpos-hstart; /* Updating the directory entry */ |
| 9804 |
DBG(DBGDIR,"End Content Section, size=0x%x\n", dir[2].size); |
| 9805 |
} |
| 9806 |
@ |
| 9807 |
|
| 9808 |
|
| 9809 |
\section{Processing the Command Line} |
| 9810 |
The following code explains the command line\index{command line} |
| 9811 |
parameters and options\index{option}\index{debugging}. |
| 9812 |
It tells us what to expect in the rest of this section. |
| 9813 |
{\def\SP{\hskip .5em} |
| 9814 |
@<explain usage@>= |
| 9815 |
fprintf(stdout, |
| 9816 |
"Usage: %s [OPTION]... FILENAME%s\n",prog_name, in_ext);@/ |
| 9817 |
fprintf(stdout,DESCRIPTION); |
| 9818 |
fprintf(stdout, |
| 9819 |
"\nOptions:\n"@/ |
| 9820 |
"\t --help \t display this message\n"@/ |
| 9821 |
"\t --version\t display the HINT version\n"@/ |
| 9822 |
"\t -l \t redirect stderr to a log file\n"@/ |
| 9823 |
#if defined (STRETCH) || defined (SHRINK) |
| 9824 |
"\t -o FILE\t specify an output file name\n"@/ |
| 9825 |
#endif |
| 9826 |
#if defined (STRETCH) |
| 9827 |
"\t -a \t write auxiliary files\n"@/ |
| 9828 |
"\t -g \t do not use localized names (implies -a)\n"@/ |
| 9829 |
"\t -f \t force overwriting existing auxiliary files\n"@/ |
| 9830 |
"\t -u \t enable writing utf8 character codes\n"@/ |
| 9831 |
"\t -x \t enable writing hexadecimal character codes\n"@/ |
| 9832 |
#elif defined (SHRINK) |
| 9833 |
"\t -a \t use only localized names\n"@/ |
| 9834 |
"\t -g \t do not use localized names\n"@/ |
| 9835 |
"\t -c \t enable compression\n"@/ |
| 9836 |
#endif |
| 9837 |
); |
| 9838 |
#ifdef DEBUG |
| 9839 |
fprintf(stdout,"\t -d XXXX \t set debug flag to hexadecimal value XXXX.\n" |
| 9840 |
"\t\t\t OR together these values:\n");@/ |
| 9841 |
fprintf(stdout,"\t\t\t XX=%03X basic debugging\n", DBGBASIC);@/ |
| 9842 |
fprintf(stdout,"\t\t\t XX=%03X tag debugging\n", DBGTAGS);@/ |
| 9843 |
fprintf(stdout,"\t\t\t XX=%03X node debugging\n",DBGNODE);@/ |
| 9844 |
fprintf(stdout,"\t\t\t XX=%03X definition debugging\n", DBGDEF);@/ |
| 9845 |
fprintf(stdout,"\t\t\t XX=%03X directory debugging\n", DBGDIR);@/ |
| 9846 |
fprintf(stdout,"\t\t\t XX=%03X range debugging\n",DBGRANGE);@/ |
| 9847 |
fprintf(stdout,"\t\t\t XX=%03X float debugging\n", DBGFLOAT);@/ |
| 9848 |
fprintf(stdout,"\t\t\t XX=%03X compression debugging\n", DBGCOMPRESS);@/ |
| 9849 |
fprintf(stdout,"\t\t\t XX=%03X buffer debugging\n", DBGBUFFER);@/ |
| 9850 |
fprintf(stdout,"\t\t\t XX=%03X flex debugging\n", DBGFLEX);@/ |
| 9851 |
fprintf(stdout,"\t\t\t XX=%03X bison debugging\n", DBGBISON);@/ |
| 9852 |
fprintf(stdout,"\t\t\t XX=%03X TeX debugging\n", DBGTEX);@/ |
| 9853 |
fprintf(stdout,"\t\t\t XX=%03X Page debugging\n", DBGPAGE);@/ |
| 9854 |
fprintf(stdout,"\t\t\t XX=%03X Font debugging\n", DBGFONT);@/ |
| 9855 |
fprintf(stdout,"\t\t\t XX=%03X Render debugging\n", DBGRENDER);@/ |
| 9856 |
fprintf(stdout,"\t\t\t XX=%03X Label debugging\n", DBGLABEL);@/ |
| 9857 |
#endif |
| 9858 |
@ |
| 9859 |
} |
| 9860 |
We define constants for different debug flags. |
| 9861 |
@<debug constants@>= |
| 9862 |
#define DBGNONE 0x0 |
| 9863 |
#define DBGBASIC 0x1 |
| 9864 |
#define DBGTAGS 0x2 |
| 9865 |
#define DBGNODE 0x4 |
| 9866 |
#define DBGDEF 0x8 |
| 9867 |
#define DBGDIR 0x10 |
| 9868 |
#define DBGRANGE 0x20 |
| 9869 |
#define DBGFLOAT 0x40 |
| 9870 |
#define DBGCOMPRESS 0x80 |
| 9871 |
#define DBGBUFFER 0X100 |
| 9872 |
#define DBGFLEX 0x200 |
| 9873 |
#define DBGBISON 0x400 |
| 9874 |
#define DBGTEX 0x800 |
| 9875 |
#define DBGPAGE 0x1000 |
| 9876 |
#define DBGFONT 0x2000 |
| 9877 |
#define DBGRENDER 0x4000 |
| 9878 |
#define DBGLABEL 0x8000 |
| 9879 |
@ |
| 9880 |
|
| 9881 |
Next we define common variables that are |
| 9882 |
needed in all three programs defined here. |
| 9883 |
|
| 9884 |
@<common variables@>= |
| 9885 |
unsigned int debugflags=DBGNONE; |
| 9886 |
int option_utf8=false; |
| 9887 |
int option_hex=false; |
| 9888 |
int option_force=false; |
| 9889 |
int option_global=false; |
| 9890 |
int option_aux=false; |
| 9891 |
int option_compress=false; |
| 9892 |
char *stem_name=NULL; |
| 9893 |
int stem_length=0; |
| 9894 |
@ |
| 9895 |
The variable |stem_name| contains the name of the input file |
| 9896 |
not including the extension. The space allocated for it |
| 9897 |
is large enough to append an extension with up to five characters. |
| 9898 |
It can be used with the extension {\tt .log} for the log file, |
| 9899 |
with {\tt .hint} or {\tt .hnt} for the output file, |
| 9900 |
and with {\tt .abs} or {\tt .rel} when writing or reading the auxiliary sections. |
| 9901 |
The {\tt stretch} program will overwrite the |stem_name| |
| 9902 |
using the name of the output file if it is set with the {\tt -o} |
| 9903 |
option. |
| 9904 |
|
| 9905 |
|
| 9906 |
Next are the variables that are local in the |main| program. |
| 9907 |
@<local variables in |main|@>= |
| 9908 |
char *prog_name; |
| 9909 |
char *in_ext; |
| 9910 |
char *out_ext; |
| 9911 |
int option_log=false; |
| 9912 |
#ifndef SKIP |
| 9913 |
char *file_name=NULL; |
| 9914 |
int file_name_length=0; |
| 9915 |
#endif |
| 9916 |
@ |
| 9917 |
|
| 9918 |
Processing the command line looks for options and then sets the |
| 9919 |
input file name\index{file name}. For compatibility with |
| 9920 |
GNU standards, the long options {\tt --help} and {\tt --version} |
| 9921 |
are supported in addition to the short options. |
| 9922 |
|
| 9923 |
@<process the command line@>= |
| 9924 |
debugflags=DBGBASIC; |
| 9925 |
prog_name=argv[0]; |
| 9926 |
if (argc < 2) |
| 9927 |
{ fprintf(stderr, |
| 9928 |
"%s: no input file given\n" |
| 9929 |
"Try '%s --help' for more information\n",prog_name, prog_name); |
| 9930 |
exit(1); |
| 9931 |
} |
| 9932 |
argv++; /* skip the program name */ |
| 9933 |
while (*argv!=NULL) |
| 9934 |
{ if ((*argv)[0]=='-') |
| 9935 |
{ char option=(*argv)[1]; |
| 9936 |
switch(option) |
| 9937 |
{ case '-': |
| 9938 |
if (strcmp(*argv,"--version")==0) |
| 9939 |
{ fprintf(stderr,"%s version " HINT_VERSION_STRING "\n",prog_name); |
| 9940 |
exit(0); |
| 9941 |
} |
| 9942 |
else if (strcmp(*argv,"--help")==0) |
| 9943 |
{ @<explain usage@>@; |
| 9944 |
fprintf(stdout,"\nFor further information and reporting bugs see https://hint.userweb.mwn.de/\n"); |
| 9945 |
exit(0); |
| 9946 |
} |
| 9947 |
case 'l': option_log=true; @+break; |
| 9948 |
#if defined (STRETCH) || defined (SHRINK) |
| 9949 |
case 'o': argv++; |
| 9950 |
file_name_length=(int)strlen(*argv); |
| 9951 |
ALLOCATE(file_name,file_name_length+6,char); /*plus extension*/ |
| 9952 |
strcpy(file_name,*argv);@+ break; |
| 9953 |
case 'g': option_global=option_aux=true; @+break; |
| 9954 |
case 'a': option_aux=true; @+break; |
| 9955 |
#endif |
| 9956 |
#if defined (STRETCH) |
| 9957 |
case 'u': option_utf8=true;@+break; |
| 9958 |
case 'x': option_hex=true;@+break; |
| 9959 |
case 'f': option_force=true; @+break; |
| 9960 |
#elif defined (SHRINK) |
| 9961 |
case 'c': option_compress=true; @+break; |
| 9962 |
#endif |
| 9963 |
case 'd': @/ |
| 9964 |
argv++; if (*argv==NULL) |
| 9965 |
{ fprintf(stderr, |
| 9966 |
"%s: option -d expects an argument\n" |
| 9967 |
"Try '%s --help' for more information\n",prog_name, prog_name); |
| 9968 |
exit(1); |
| 9969 |
} |
| 9970 |
debugflags=strtol(*argv,NULL,16); |
| 9971 |
break; |
| 9972 |
default: |
| 9973 |
{ fprintf(stderr, |
| 9974 |
"%s: unrecognized option '%s'\n" |
| 9975 |
"Try '%s --help' for more information\n",prog_name,*argv,prog_name); |
| 9976 |
exit(1); |
| 9977 |
} |
| 9978 |
} |
| 9979 |
} |
| 9980 |
else /* the input file name */ |
| 9981 |
{ int path_length=(int)strlen(*argv); |
| 9982 |
int ext_length=(int)strlen(in_ext); |
| 9983 |
ALLOCATE(hin_name,path_length+ext_length+1,char); |
| 9984 |
strcpy(hin_name,*argv); |
| 9985 |
if (path_length<ext_length |
| 9986 |
|| strncmp(hin_name+path_length-ext_length,in_ext,ext_length)!=0) |
| 9987 |
{ strcat(hin_name,in_ext); |
| 9988 |
path_length+=ext_length; |
| 9989 |
} |
| 9990 |
stem_length=path_length-ext_length; |
| 9991 |
ALLOCATE(stem_name,stem_length+6,char); |
| 9992 |
strncpy(stem_name,hin_name,stem_length); |
| 9993 |
stem_name[stem_length]=0; |
| 9994 |
if (*(argv+1)!=NULL) |
| 9995 |
{ fprintf(stderr, |
| 9996 |
"%s: extra argument after input file name: '%s'\n" |
| 9997 |
"Try '%s --help' for more information\n",prog_name,*(argv+1),prog_name); |
| 9998 |
exit(1); |
| 9999 |
} |
| 10000 |
} |
| 10001 |
argv++; |
| 10002 |
} |
| 10003 |
if (hin_name==NULL) |
| 10004 |
{ fprintf(stderr, |
| 10005 |
"%s: missing input file name\n" |
| 10006 |
"Try '%s --help' for more information\n",prog_name,prog_name); |
| 10007 |
exit(1); |
| 10008 |
} |
| 10009 |
@ |
| 10010 |
|
| 10011 |
After the command line has been processed, three file streams need to be opened: |
| 10012 |
The input file |hin|\index{input file} and the output file |hout|\index{output file}. |
| 10013 |
Further we need a log file |hlog|\index{log file} if debugging is enabled. |
| 10014 |
For technical reasons, the scanner\index{scanning} generated by \.{flex} needs |
| 10015 |
an input file |yyin|\index{input file} which is set to |hin| |
| 10016 |
and an output file |yyout| (which is not used). |
| 10017 |
|
| 10018 |
@<common variables@>= |
| 10019 |
FILE *hin=NULL, *hout=NULL, *hlog=NULL; |
| 10020 |
@ |
| 10021 |
|
| 10022 |
|
| 10023 |
The log file is opened first because |
| 10024 |
this is the place where error messages\index{error message} |
| 10025 |
should go while the other files are opened. |
| 10026 |
It inherits its name from the input file name. |
| 10027 |
|
| 10028 |
@<open the log file@> = |
| 10029 |
if (option_log) |
| 10030 |
{ |
| 10031 |
strcat(stem_name,".log"); |
| 10032 |
hlog=freopen(stem_name,"w",stderr); |
| 10033 |
if (hlog==NULL) |
| 10034 |
{ fprintf(stderr,"Unable to open logfile %s",stem_name); |
| 10035 |
hlog=stderr; |
| 10036 |
} |
| 10037 |
stem_name[stem_length]=0; |
| 10038 |
} |
| 10039 |
else |
| 10040 |
hlog=stderr; |
| 10041 |
@ |
| 10042 |
|
| 10043 |
Once we have established logging, we can try to open the other files. |
| 10044 |
@<open the input file@>= |
| 10045 |
hin=fopen(hin_name,"rb"); |
| 10046 |
if (hin==NULL) QUIT("Unable to open input file %s",hin_name); |
| 10047 |
@ |
| 10048 |
|
| 10049 |
@<open the output file@>= |
| 10050 |
if (file_name!=NULL) |
| 10051 |
{ int ext_length=(int)strlen(out_ext); |
| 10052 |
if (file_name_length<=ext_length |
| 10053 |
|| strncmp(file_name+file_name_length-ext_length,out_ext,ext_length)!=0) |
| 10054 |
{ strcat(file_name,out_ext); file_name_length+=ext_length; } |
| 10055 |
} |
| 10056 |
else |
| 10057 |
{ file_name_length=stem_length+(int)strlen(out_ext); |
| 10058 |
ALLOCATE(file_name,file_name_length+1,char); |
| 10059 |
strcpy(file_name,stem_name);@+ |
| 10060 |
strcpy(file_name+stem_length,out_ext); |
| 10061 |
} |
| 10062 |
{ char *aux_name=file_name; |
| 10063 |
@<make sure the path in |aux_name| exists@>@; |
| 10064 |
aux_name=NULL; |
| 10065 |
} |
| 10066 |
hout=fopen(file_name,"wb"); |
| 10067 |
if (hout==NULL) QUIT("Unable to open output file %s",file_name); |
| 10068 |
@ |
| 10069 |
|
| 10070 |
The {\tt stretch} program will replace the |stem_name| using the stem of the |
| 10071 |
output file. |
| 10072 |
@<determine the |stem_name| from the output |file_name|@>= |
| 10073 |
stem_length=file_name_length-(int)strlen(out_ext); |
| 10074 |
ALLOCATE(stem_name,stem_length+6,char); |
| 10075 |
strncpy(stem_name,file_name,stem_length); |
| 10076 |
stem_name[stem_length]=0; |
| 10077 |
@ |
| 10078 |
|
| 10079 |
At the very end, we will close the files again. |
| 10080 |
@<close the input file@>= |
| 10081 |
if (hin_name!=NULL) free(hin_name); |
| 10082 |
if (hin!=NULL) fclose(hin); |
| 10083 |
@ |
| 10084 |
@<close the output file@>= |
| 10085 |
if (file_name!=NULL) free(file_name); |
| 10086 |
if (hout!=NULL) fclose(hout); |
| 10087 |
@ |
| 10088 |
@<close the log file@>= |
| 10089 |
if (hlog!=NULL) fclose(hlog); |
| 10090 |
if (stem_name!=NULL) free(stem_name); |
| 10091 |
@ |
| 10092 |
|
| 10093 |
|
| 10094 |
|
| 10095 |
\section{Error Handling and Debugging}\label{error_section} |
| 10096 |
There is no good program without good error handling\index{error message}\index{debugging}. |
| 10097 |
To print messages\index{message} or indicate errors, I define the following macros: |
| 10098 |
\index{MESSAGE+\.{MESSAGE}}\index{QUIT+\.{QUIT}} |
| 10099 |
|
| 10100 |
@(hierror.h@>= |
| 10101 |
#ifndef _ERROR_H |
| 10102 |
#define _ERROR_H |
| 10103 |
#include <stdlib.h> |
| 10104 |
#include <stdio.h> |
| 10105 |
extern FILE *hlog; |
| 10106 |
extern uint8_t *hpos, *hstart; |
| 10107 |
#ifndef LOG_PREFIX |
| 10108 |
#define LOG_PREFIX "HINT " |
| 10109 |
#endif |
| 10110 |
#define @[LOG(...)@] @[(fprintf(hlog,LOG_PREFIX __VA_ARGS__),fflush(hlog))@] |
| 10111 |
#define @[MESSAGE(...)@] @[(fprintf(hlog,LOG_PREFIX __VA_ARGS__),fflush(hlog))@] |
| 10112 |
#define @[QUIT(...)@] (MESSAGE("ERROR: " __VA_ARGS__),fprintf(hlog,"\n"),exit(1)) |
| 10113 |
|
| 10114 |
#endif |
| 10115 |
@ |
| 10116 |
|
| 10117 |
|
| 10118 |
The amount of debugging\index{debugging} depends on the debugging flags. |
| 10119 |
For portability, we first define the output specifier for expressions of type |size_t|. |
| 10120 |
\index{DBG+\.{DBG}}\index{SIZE F+\.{SIZE\_F}}\index{DBGTAG+\.{DBGTAG}} |
| 10121 |
\index{RNG+\.{RNG}}\index{TAGERR+\.{TAGERR}} |
| 10122 |
@<debug macros@>= |
| 10123 |
#ifdef WIN32 |
| 10124 |
#define SIZE_F "0x%tx" |
| 10125 |
#else |
| 10126 |
#define SIZE_F "0x%tx" |
| 10127 |
#endif |
| 10128 |
#ifdef DEBUG |
| 10129 |
#define @[DBG(FLAGS,...)@] ((debugflags & (FLAGS))?LOG(__VA_ARGS__):0) |
| 10130 |
#else |
| 10131 |
#define @[DBG(FLAGS,...)@] (void)0 |
| 10132 |
#endif |
| 10133 |
#define @[DBGTAG(A,P)@] @[DBG(DBGTAGS,@["tag [%s,%d] at " SIZE_F "\n"@],@|NAME(A),INFO(A),(P)-hstart)@] |
| 10134 |
|
| 10135 |
#define @[RNG(S,N,A,Z)@] @/\ |
| 10136 |
if ((int)(N)<(int)(A)||(int)(N)>(int)(Z)) QUIT(S@, " %d out of range [%d - %d]",N,A,Z) |
| 10137 |
|
| 10138 |
#define @[TAGERR(A)@] @[QUIT(@["Unknown tag [%s,%d] at " SIZE_F "\n"@],NAME(A),INFO(A),hpos-hstart)@] |
| 10139 |
@ |
| 10140 |
|
| 10141 |
The \.{bison} generated parser will need a function |yyerror| for |
| 10142 |
error reporting. We can define it now: |
| 10143 |
|
| 10144 |
@<parsing functions@>= |
| 10145 |
extern int yylineno; |
| 10146 |
int yyerror(const char *msg) |
| 10147 |
{ QUIT(" in line %d %s",yylineno,msg); |
| 10148 |
return 0; |
| 10149 |
} |
| 10150 |
@ |
| 10151 |
|
| 10152 |
To enable the generation of debugging code \.{bison} needs also the following: |
| 10153 |
@<enable bison debugging@>= |
| 10154 |
#ifdef DEBUG |
| 10155 |
#define YYDEBUG 1 |
| 10156 |
extern int yydebug; |
| 10157 |
#else |
| 10158 |
#define YYDEBUG 0 |
| 10159 |
#endif |
| 10160 |
@ |
| 10161 |
|
| 10162 |
|
| 10163 |
\appendix |
| 10164 |
|
| 10165 |
\section{Traversing Short Format Files}\label{fastforward} |
| 10166 |
For applications like searching or repositioning a file after reloading |
| 10167 |
a possibly changed version of a file, it is useful to have a fast way |
| 10168 |
of getting from one content node to the next. |
| 10169 |
For quite some nodes, it is possible to know the size of the |
| 10170 |
node from the tag. So the fastest way to get to the next node |
| 10171 |
is looking up the node size in a table. |
| 10172 |
|
| 10173 |
Other important nodes, for example hbox, vbox, or par nodes, end with a |
| 10174 |
list node and it is possible to know the size of the node up to the final |
| 10175 |
list. With that knowledge it is possible to skip the initial |
| 10176 |
part of the node, then skip the list, and finally skip the tag byte. |
| 10177 |
The size of the initial part can be stored in the same node size table |
| 10178 |
using negated values. What works for lists, |
| 10179 |
of course, will work for other kinds of nodes as well. |
| 10180 |
So we use the lowest two bits of the values in the size table |
| 10181 |
to store the number of embedded nodes that follow after the initial part. |
| 10182 |
To combine the number of leading bytes and the number of trailing nodes |
| 10183 |
into a single number that encodes both values according to this formula |
| 10184 |
we use the macro |NODE_SIZE|. We can get back both values using the |
| 10185 |
macros |NODE_HEAD| and |NODE_TAIL|. |
| 10186 |
|
| 10187 |
@<hint macros@>= |
| 10188 |
#define @[NODE_SIZE(H,T)@] ((T)==0?(H)+2:-4*((H)+1)+((T)-1)) |
| 10189 |
#define @[NODE_HEAD(N)@] ((N)>0?(N)-2:-((N)>>2)-1) |
| 10190 |
#define @[NODE_TAIL(N)@] ((N)<0?((N)&0x3)+1:0) |
| 10191 |
@ |
| 10192 |
|
| 10193 |
For list nodes neither of these methods works and these nodes can be marked |
| 10194 |
with a zero entry in the node size table. |
| 10195 |
|
| 10196 |
This leads to the following code for a ``fast forward'' function |
| 10197 |
for |hpos|: |
| 10198 |
|
| 10199 |
@<shared skip functions@>= |
| 10200 |
uint32_t hff_list_pos=0, hff_list_size=0; |
| 10201 |
Tag hff_tag; |
| 10202 |
void hff_hpos(void) |
| 10203 |
{ signed char i,b,n; |
| 10204 |
hff_tag=*hpos;@+ |
| 10205 |
DBGTAG(hff_tag,hpos); |
| 10206 |
i= hnode_size[hff_tag]; |
| 10207 |
if (i>0) {hpos=hpos+NODE_HEAD(i)+2; @+return;@+ } |
| 10208 |
else if (i<0) |
| 10209 |
{ n=NODE_TAIL(i);@+ b=NODE_HEAD(i); |
| 10210 |
hpos=hpos+1+b; /* skip initial part */ |
| 10211 |
while (n>0) |
| 10212 |
{ hff_hpos(); @+n--; @+} /* skip trailing nodes */ |
| 10213 |
hpos++;/* skip end byte */ |
| 10214 |
return; |
| 10215 |
} |
| 10216 |
else if (hff_tag <=TAG(param_kind,7)) |
| 10217 |
@<advance |hpos| over a list@>@; |
| 10218 |
TAGERR(hff_tag); |
| 10219 |
} |
| 10220 |
@ |
| 10221 |
|
| 10222 |
|
| 10223 |
We will put the |hnode_size| variable into the {\tt tables.c} file |
| 10224 |
using the following function. We add some comments and |
| 10225 |
split negative values into their components, to make the result more |
| 10226 |
readable. |
| 10227 |
|
| 10228 |
@<print the |hnode_size| variable@>= |
| 10229 |
printf("signed char hnode_size[0x100]= {\n"); |
| 10230 |
for (i=0; i<=0xff; i++)@/ |
| 10231 |
{ signed char s = hnode_size[i]; |
| 10232 |
if (s>=0) printf("%d",s); else printf("-4*%d+%d",-(s>>2),s&3); |
| 10233 |
if (i<0xff) printf(","); else printf("};"); |
| 10234 |
if ((i&0x7)==0x7) printf(" /* %s */\n", content_name[KIND(i)]); |
| 10235 |
} |
| 10236 |
printf("\n\n"); |
| 10237 |
@ |
| 10238 |
|
| 10239 |
When dealing with unknown content nodes, it is convenient to know which |
| 10240 |
nodes are known and which are not. For this purpose the |content_known| |
| 10241 |
array contains one byte for each kind value and each such bytes will |
| 10242 |
indicate using the seven least significant bits for which info values |
| 10243 |
the corresponding nodes are known. |
| 10244 |
|
| 10245 |
@<print the |content_known| variable@>= |
| 10246 |
for (k=0;k<32;k++) |
| 10247 |
for(i=0;i<8;i++) |
| 10248 |
if (hnode_size[TAG(k,i)]!=0) |
| 10249 |
content_known[k]|=(1<<i); |
| 10250 |
printf("uint8_t content_known[32]= {\n"); |
| 10251 |
for (k=0; k<32; k++)@/ |
| 10252 |
{ printf("0x%02X",content_known[k]); |
| 10253 |
if (k<31) printf(","); |
| 10254 |
else printf("};"); |
| 10255 |
printf(" /* %s */\n",content_name[k]); |
| 10256 |
} |
| 10257 |
printf("\n"); |
| 10258 |
@ |
| 10259 |
|
| 10260 |
|
| 10261 |
|
| 10262 |
|
| 10263 |
|
| 10264 |
\subsection{Lists}\index{list}\index{text}\index{parameters} |
| 10265 |
List don't follow the usual schema of nodes. They have a variable size |
| 10266 |
that is stored in the node. We keep position and size in global variables |
| 10267 |
so that the list that ends a node can be conveniently located. |
| 10268 |
|
| 10269 |
@<advance |hpos| over a list@>= |
| 10270 |
switch (INFO(hff_tag)&0x3){ |
| 10271 |
case 0: hff_list_pos=hpos-hstart+1;hff_list_size=0; hpos=hpos+3;@+ return; |
| 10272 |
case 1: hpos++;@+ hff_list_size=HGET8;@+ hff_list_pos=hpos-hstart+1; hpos=hpos+1+hff_list_size+1+1+1;@+ return; |
| 10273 |
case 2: hpos++;@+ HGET16(hff_list_size);@+hff_list_pos=hpos-hstart+1; hpos=hpos+1+hff_list_size+1+2+1;@+ return; |
| 10274 |
case 3: hpos++;@+ HGET32(hff_list_size);@+hff_list_pos=hpos-hstart+1; hpos=hpos+1+hff_list_size+1+4+1;@+ return; |
| 10275 |
default: QUIT(@["List with unknown info [%s,%d] at " SIZE_F "\n"@],NAME(hff_tag),INFO(hff_tag),hpos-hstart); |
| 10276 |
} |
| 10277 |
@ |
| 10278 |
Actually list nodes never occur as content nodes in their own right but only as subnodes of |
| 10279 |
content nodes. |
| 10280 |
|
| 10281 |
Now let's consider the different kinds of nodes. |
| 10282 |
|
| 10283 |
\subsection{Glyphs}\index{glyph} |
| 10284 |
We start with the glyph nodes. All glyph nodes |
| 10285 |
have a start and an end tag, one byte for the font, |
| 10286 |
and depending on the info from 1 to 4 bytes for the character code. |
| 10287 |
|
| 10288 |
@<initialize the |hnode_size| array@>= |
| 10289 |
hnode_size[TAG(glyph_kind,1)] = NODE_SIZE(1+1,0); |
| 10290 |
hnode_size[TAG(glyph_kind,2)] = NODE_SIZE(1+2,0); |
| 10291 |
hnode_size[TAG(glyph_kind,3)] = NODE_SIZE(1+3,0); |
| 10292 |
hnode_size[TAG(glyph_kind,4)] = NODE_SIZE(1+4,0); |
| 10293 |
@ |
| 10294 |
|
| 10295 |
|
| 10296 |
|
| 10297 |
|
| 10298 |
\subsection{Penalties}\index{penalty} |
| 10299 |
Penalty nodes either contain a one byte reference, a one byte number, or a two byte number. |
| 10300 |
|
| 10301 |
@<initialize the |hnode_size| array@>= |
| 10302 |
hnode_size[TAG(penalty_kind,0)] = NODE_SIZE(1,0); |
| 10303 |
hnode_size[TAG(penalty_kind,1)] = NODE_SIZE(1,0); |
| 10304 |
hnode_size[TAG(penalty_kind,2)] = NODE_SIZE(2,0); |
| 10305 |
hnode_size[TAG(penalty_kind,3)] = NODE_SIZE(4,0); |
| 10306 |
@ |
| 10307 |
|
| 10308 |
\subsection{Kerns}\index{kern} |
| 10309 |
Kern nodes can contain a reference (either to a dimension or an extended dimension) |
| 10310 |
followed by either a dimension or an extended dimension node. |
| 10311 |
|
| 10312 |
@<initialize the |hnode_size| array@>= |
| 10313 |
hnode_size[TAG(kern_kind,b000)] = NODE_SIZE(1,0); |
| 10314 |
hnode_size[TAG(kern_kind,b001)] = NODE_SIZE(1,0); |
| 10315 |
hnode_size[TAG(kern_kind,b010)] = NODE_SIZE(4,0); |
| 10316 |
hnode_size[TAG(kern_kind,b011)] = NODE_SIZE(0,1); |
| 10317 |
hnode_size[TAG(kern_kind,b100)] = NODE_SIZE(1,0); |
| 10318 |
hnode_size[TAG(kern_kind,b101)] = NODE_SIZE(1,0); |
| 10319 |
hnode_size[TAG(kern_kind,b110)] = NODE_SIZE(4,0); |
| 10320 |
hnode_size[TAG(kern_kind,b111)] = NODE_SIZE(0,1); |
| 10321 |
@ |
| 10322 |
|
| 10323 |
|
| 10324 |
\subsection{Extended Dimensions}\index{extended dimension} |
| 10325 |
Extended dimensions contain either one two or three 4 byte values depending |
| 10326 |
on the info bits. |
| 10327 |
@<initialize the |hnode_size| array@>= |
| 10328 |
hnode_size[TAG(xdimen_kind,b100)] = NODE_SIZE(4,0); |
| 10329 |
hnode_size[TAG(xdimen_kind,b010)] = NODE_SIZE(4,0); |
| 10330 |
hnode_size[TAG(xdimen_kind,b001)] = NODE_SIZE(4,0); |
| 10331 |
hnode_size[TAG(xdimen_kind,b110)] = NODE_SIZE(4+4,0); |
| 10332 |
hnode_size[TAG(xdimen_kind,b101)] = NODE_SIZE(4+4,0); |
| 10333 |
hnode_size[TAG(xdimen_kind,b011)] = NODE_SIZE(4+4,0); |
| 10334 |
hnode_size[TAG(xdimen_kind,b111)] = NODE_SIZE(4+4+4,0); |
| 10335 |
@ |
| 10336 |
|
| 10337 |
\subsection{Language}\index{language} |
| 10338 |
Language nodes either code the language in the info value or they contain |
| 10339 |
a reference byte. |
| 10340 |
@<initialize the |hnode_size| array@>= |
| 10341 |
hnode_size[TAG(language_kind,b000)] = NODE_SIZE(1,0); |
| 10342 |
hnode_size[TAG(language_kind,1)] = NODE_SIZE(0,0); |
| 10343 |
hnode_size[TAG(language_kind,2)] = NODE_SIZE(0,0); |
| 10344 |
hnode_size[TAG(language_kind,3)] = NODE_SIZE(0,0); |
| 10345 |
hnode_size[TAG(language_kind,4)] = NODE_SIZE(0,0); |
| 10346 |
hnode_size[TAG(language_kind,5)] = NODE_SIZE(0,0); |
| 10347 |
hnode_size[TAG(language_kind,6)] = NODE_SIZE(0,0); |
| 10348 |
hnode_size[TAG(language_kind,7)] = NODE_SIZE(0,0); |
| 10349 |
@ |
| 10350 |
|
| 10351 |
\subsection{Rules}\index{rule} |
| 10352 |
Rules usually contain a reference, otherwise |
| 10353 |
they contain either one, two, or three 4 byte values depending |
| 10354 |
on the info bits. |
| 10355 |
@<initialize the |hnode_size| array@>= |
| 10356 |
hnode_size[TAG(rule_kind,b000)] = NODE_SIZE(1,0); |
| 10357 |
hnode_size[TAG(rule_kind,b100)] = NODE_SIZE(4,0); |
| 10358 |
hnode_size[TAG(rule_kind,b010)] = NODE_SIZE(4,0); |
| 10359 |
hnode_size[TAG(rule_kind,b001)] = NODE_SIZE(4,0); |
| 10360 |
hnode_size[TAG(rule_kind,b110)] = NODE_SIZE(4+4,0); |
| 10361 |
hnode_size[TAG(rule_kind,b101)] = NODE_SIZE(4+4,0); |
| 10362 |
hnode_size[TAG(rule_kind,b011)] = NODE_SIZE(4+4,0); |
| 10363 |
hnode_size[TAG(rule_kind,b111)] = NODE_SIZE(4+4+4,0); |
| 10364 |
@ |
| 10365 |
|
| 10366 |
\subsection{Glue}\index{glue} |
| 10367 |
Glues usually contain a reference or |
| 10368 |
they contain either one two or three 4 byte values depending |
| 10369 |
on the info bits, and possibly even an extended dimension node followed |
| 10370 |
by two 4 byte values. |
| 10371 |
@<initialize the |hnode_size| array@>= |
| 10372 |
hnode_size[TAG(glue_kind,b000)] = NODE_SIZE(1,0); |
| 10373 |
hnode_size[TAG(glue_kind,b100)] = NODE_SIZE(4,0); |
| 10374 |
hnode_size[TAG(glue_kind,b010)] = NODE_SIZE(4,0); |
| 10375 |
hnode_size[TAG(glue_kind,b001)] = NODE_SIZE(4,0); |
| 10376 |
hnode_size[TAG(glue_kind,b110)] = NODE_SIZE(4+4,0); |
| 10377 |
hnode_size[TAG(glue_kind,b101)] = NODE_SIZE(4+4,0); |
| 10378 |
hnode_size[TAG(glue_kind,b011)] = NODE_SIZE(4+4,0); |
| 10379 |
hnode_size[TAG(glue_kind,b111)] = NODE_SIZE(4+4,1); |
| 10380 |
@ |
| 10381 |
|
| 10382 |
|
| 10383 |
\subsection{Boxes}\index{box} |
| 10384 |
The layout of boxes is quite complex and explained in section~\secref{boxnodes}. |
| 10385 |
All boxes contain height and width, some contain a depth, some a shift amount, |
| 10386 |
and some a glue setting together with glue sign and glue order. |
| 10387 |
The last item in a box is a node list. |
| 10388 |
|
| 10389 |
@<initialize the |hnode_size| array@>= |
| 10390 |
hnode_size[TAG(hbox_kind,b000)] = NODE_SIZE(4+4,1); /* tag, height, width*/ |
| 10391 |
hnode_size[TAG(hbox_kind,b001)] = NODE_SIZE(4+4+4,1); /* and depth */ |
| 10392 |
hnode_size[TAG(hbox_kind,b010)] = NODE_SIZE(4+4+4,1); /* or shift */ |
| 10393 |
hnode_size[TAG(hbox_kind,b011)] = NODE_SIZE(4+4+4+4,1); /* or both */ |
| 10394 |
hnode_size[TAG(hbox_kind,b100)] = NODE_SIZE(4+4+5,1); /* and glue setting*/ |
| 10395 |
hnode_size[TAG(hbox_kind,b101)] = NODE_SIZE(4+4+4+5,1); /* and depth */ |
| 10396 |
hnode_size[TAG(hbox_kind,b110)] = NODE_SIZE(4+4+4+5,1); /* or shift */ |
| 10397 |
hnode_size[TAG(hbox_kind,b111)] = NODE_SIZE(4+4+4+4+5,1); /*or both */ |
| 10398 |
hnode_size[TAG(vbox_kind,b000)] = NODE_SIZE(4+4,1); /* same for vbox*/ |
| 10399 |
hnode_size[TAG(vbox_kind,b001)] = NODE_SIZE(4+4+4,1); |
| 10400 |
hnode_size[TAG(vbox_kind,b010)] = NODE_SIZE(4+4+4,1); |
| 10401 |
hnode_size[TAG(vbox_kind,b011)] = NODE_SIZE(4+4+4+4,1); |
| 10402 |
hnode_size[TAG(vbox_kind,b100)] = NODE_SIZE(4+4+5,1); |
| 10403 |
hnode_size[TAG(vbox_kind,b101)] = NODE_SIZE(4+4+4+5,1); |
| 10404 |
hnode_size[TAG(vbox_kind,b110)] = NODE_SIZE(4+4+4+5,1); |
| 10405 |
hnode_size[TAG(vbox_kind,b111)] = NODE_SIZE(4+4+4+4+5,1); |
| 10406 |
@ |
| 10407 |
|
| 10408 |
\subsection{Extended Boxes}\index{extended box} |
| 10409 |
Extended boxes start with height, width, depth, stretch, or shrink components. |
| 10410 |
Then follows an extended dimension either as a reference or a node. |
| 10411 |
The node ends with a list. |
| 10412 |
@<initialize the |hnode_size| array@>= |
| 10413 |
hnode_size[TAG(hset_kind,b000)] = NODE_SIZE(4+4+4+4+1,1); |
| 10414 |
hnode_size[TAG(hset_kind,b001)] = NODE_SIZE(4+4+4+4+4+1,1); |
| 10415 |
hnode_size[TAG(hset_kind,b010)] = NODE_SIZE(4+4+4+4+4+1,1); |
| 10416 |
hnode_size[TAG(hset_kind,b011)] = NODE_SIZE(4+4+4+4+4+4+1,1); |
| 10417 |
hnode_size[TAG(vset_kind,b000)] = NODE_SIZE(4+4+4+4+1,1); |
| 10418 |
hnode_size[TAG(vset_kind,b001)] = NODE_SIZE(4+4+4+4+4+1,1); |
| 10419 |
hnode_size[TAG(vset_kind,b010)] = NODE_SIZE(4+4+4+4+4+1,1); |
| 10420 |
hnode_size[TAG(vset_kind,b011)] = NODE_SIZE(4+4+4+4+4+4+1,1); |
| 10421 |
|
| 10422 |
hnode_size[TAG(hset_kind,b100)] = NODE_SIZE(4+4+4+4,2); |
| 10423 |
hnode_size[TAG(hset_kind,b101)] = NODE_SIZE(4+4+4+4+4,2); |
| 10424 |
hnode_size[TAG(hset_kind,b110)] = NODE_SIZE(4+4+4+4+4,2); |
| 10425 |
hnode_size[TAG(hset_kind,b111)] = NODE_SIZE(4+4+4+4+4+4,2); |
| 10426 |
hnode_size[TAG(vset_kind,b100)] = NODE_SIZE(4+4+4+4,2); |
| 10427 |
hnode_size[TAG(vset_kind,b101)] = NODE_SIZE(4+4+4+4+4,2); |
| 10428 |
hnode_size[TAG(vset_kind,b110)] = NODE_SIZE(4+4+4+4+4,2); |
| 10429 |
hnode_size[TAG(vset_kind,b111)] = NODE_SIZE(4+4+4+4+4+4,2); |
| 10430 |
@ |
| 10431 |
|
| 10432 |
The hpack and vpack nodes start with a shift amount and in case of vpack a depth. |
| 10433 |
Then again an extended dimension and a list. |
| 10434 |
@<initialize the |hnode_size| array@>= |
| 10435 |
hnode_size[TAG(hpack_kind,b000)] = NODE_SIZE(1,1); |
| 10436 |
hnode_size[TAG(hpack_kind,b001)] = NODE_SIZE(1,1); |
| 10437 |
hnode_size[TAG(hpack_kind,b010)] = NODE_SIZE(4+1,1); |
| 10438 |
hnode_size[TAG(hpack_kind,b011)] = NODE_SIZE(4+1,1); |
| 10439 |
hnode_size[TAG(vpack_kind,b000)] = NODE_SIZE(4+1,1); |
| 10440 |
hnode_size[TAG(vpack_kind,b001)] = NODE_SIZE(4+1,1); |
| 10441 |
hnode_size[TAG(vpack_kind,b010)] = NODE_SIZE(4+4+1,1); |
| 10442 |
hnode_size[TAG(vpack_kind,b011)] = NODE_SIZE(4+4+1,1); |
| 10443 |
|
| 10444 |
hnode_size[TAG(hpack_kind,b100)] = NODE_SIZE(0,2); |
| 10445 |
hnode_size[TAG(hpack_kind,b101)] = NODE_SIZE(0,2); |
| 10446 |
hnode_size[TAG(hpack_kind,b110)] = NODE_SIZE(4,2); |
| 10447 |
hnode_size[TAG(hpack_kind,b111)] = NODE_SIZE(4,2); |
| 10448 |
hnode_size[TAG(vpack_kind,b100)] = NODE_SIZE(4,2); |
| 10449 |
hnode_size[TAG(vpack_kind,b101)] = NODE_SIZE(4,2); |
| 10450 |
hnode_size[TAG(vpack_kind,b110)] = NODE_SIZE(4+4,2); |
| 10451 |
hnode_size[TAG(vpack_kind,b111)] = NODE_SIZE(4+4,2); |
| 10452 |
@ |
| 10453 |
|
| 10454 |
\subsection{Leaders}\index{leaders} |
| 10455 |
Most leader nodes will use a reference. |
| 10456 |
Otherwise they contain a glue node followed by a box or rule node. |
| 10457 |
@<initialize the |hnode_size| array@>= |
| 10458 |
hnode_size[TAG(leaders_kind,b000)] = NODE_SIZE(1,0); |
| 10459 |
hnode_size[TAG(leaders_kind,1)] = NODE_SIZE(0,1); |
| 10460 |
hnode_size[TAG(leaders_kind,2)] = NODE_SIZE(0,1); |
| 10461 |
hnode_size[TAG(leaders_kind,3)] = NODE_SIZE(0,1); |
| 10462 |
hnode_size[TAG(leaders_kind,b100|1)] = NODE_SIZE(0,2); |
| 10463 |
hnode_size[TAG(leaders_kind,b100|2)] = NODE_SIZE(0,2); |
| 10464 |
hnode_size[TAG(leaders_kind,b100|3)] = NODE_SIZE(0,2); |
| 10465 |
@ |
| 10466 |
|
| 10467 |
\subsection{Baseline Skips}\index{baseline skip} |
| 10468 |
Here we expect either a reference or two optional glue nodes followed by an optional dimension. |
| 10469 |
@<initialize the |hnode_size| array@>= |
| 10470 |
hnode_size[TAG(baseline_kind,b000)] = NODE_SIZE(1,0); |
| 10471 |
hnode_size[TAG(baseline_kind,b001)] = NODE_SIZE(4,0); |
| 10472 |
hnode_size[TAG(baseline_kind,b010)] = NODE_SIZE(0,1); |
| 10473 |
hnode_size[TAG(baseline_kind,b100)] = NODE_SIZE(0,1); |
| 10474 |
hnode_size[TAG(baseline_kind,b110)] = NODE_SIZE(0,2); |
| 10475 |
|
| 10476 |
hnode_size[TAG(baseline_kind,b011)] = NODE_SIZE(4,1); |
| 10477 |
hnode_size[TAG(baseline_kind,b101)] = NODE_SIZE(4,1); |
| 10478 |
hnode_size[TAG(baseline_kind,b111)] = NODE_SIZE(4,2); |
| 10479 |
@ |
| 10480 |
|
| 10481 |
|
| 10482 |
\subsection{Ligatures}\index{ligature} |
| 10483 |
As usual a reference is possible, otherwise the font is followed by character bytes |
| 10484 |
as given by the info. Only if the info value is 7, the number of character bytes |
| 10485 |
is stored separately. |
| 10486 |
@<initialize the |hnode_size| array@>= |
| 10487 |
hnode_size[TAG(ligature_kind,b000)] = NODE_SIZE(1,0); |
| 10488 |
hnode_size[TAG(ligature_kind,1)] = NODE_SIZE(1+1,0); |
| 10489 |
hnode_size[TAG(ligature_kind,2)] = NODE_SIZE(1+2,0); |
| 10490 |
hnode_size[TAG(ligature_kind,3)] = NODE_SIZE(1+3,0); |
| 10491 |
hnode_size[TAG(ligature_kind,4)] = NODE_SIZE(1+4,0); |
| 10492 |
hnode_size[TAG(ligature_kind,5)] = NODE_SIZE(1+5,0); |
| 10493 |
hnode_size[TAG(ligature_kind,6)] = NODE_SIZE(1+6,0); |
| 10494 |
hnode_size[TAG(ligature_kind,7)] = NODE_SIZE(1,1); |
| 10495 |
@ |
| 10496 |
|
| 10497 |
\subsection{Discretionary breaks}\index{discretionary break} |
| 10498 |
The simple cases here are references, discretionary breaks |
| 10499 |
with empty pre- and post-list, or with a zero line skip limit |
| 10500 |
Otherwise one or two lists are followed by an optional replace count. |
| 10501 |
|
| 10502 |
@<initialize the |hnode_size| array@>= |
| 10503 |
hnode_size[TAG(disc_kind,b000)] = NODE_SIZE(1,0); |
| 10504 |
hnode_size[TAG(disc_kind,b010)] = NODE_SIZE(0,1); |
| 10505 |
hnode_size[TAG(disc_kind,b011)] = NODE_SIZE(0,2); |
| 10506 |
hnode_size[TAG(disc_kind,b100)] = NODE_SIZE(1,0); |
| 10507 |
hnode_size[TAG(disc_kind,b110)] = NODE_SIZE(1,1); |
| 10508 |
hnode_size[TAG(disc_kind,b111)] = NODE_SIZE(1,2); |
| 10509 |
@ |
| 10510 |
|
| 10511 |
\subsection{Paragraphs}\index{paragraph} |
| 10512 |
Paragraph nodes contain an extended dimension, an parameter list and a list. |
| 10513 |
The first two can be given as a reference. |
| 10514 |
@<initialize the |hnode_size| array@>= |
| 10515 |
hnode_size[TAG(par_kind,b000)] = NODE_SIZE(1+1,1); |
| 10516 |
hnode_size[TAG(par_kind,b010)] = NODE_SIZE(1,2); |
| 10517 |
hnode_size[TAG(par_kind,b110)] = NODE_SIZE(0,3); |
| 10518 |
hnode_size[TAG(par_kind,b100)] = NODE_SIZE(1,2); |
| 10519 |
@ |
| 10520 |
|
| 10521 |
\subsection{Mathematics}\index{mathematics}\index{displayed formula} |
| 10522 |
Displayed math needs a parameter list, either as list or as reference |
| 10523 |
followed by an optional left or right equation number and a list. |
| 10524 |
Text math is simpler: the only information is in the info value. |
| 10525 |
@<initialize the |hnode_size| array@>= |
| 10526 |
hnode_size[TAG(math_kind,b000)] = NODE_SIZE(1,1); |
| 10527 |
hnode_size[TAG(math_kind,b001)] = NODE_SIZE(1,2); |
| 10528 |
hnode_size[TAG(math_kind,b010)] = NODE_SIZE(1,2); |
| 10529 |
hnode_size[TAG(math_kind,b100)] = NODE_SIZE(0,2); |
| 10530 |
hnode_size[TAG(math_kind,b101)] = NODE_SIZE(0,3); |
| 10531 |
hnode_size[TAG(math_kind,b110)] = NODE_SIZE(0,3); |
| 10532 |
hnode_size[TAG(math_kind,b111)] = NODE_SIZE(0,0); |
| 10533 |
hnode_size[TAG(math_kind,b011)] = NODE_SIZE(0,0); |
| 10534 |
@ |
| 10535 |
|
| 10536 |
\subsection{Adjustments}\index{adjustment} |
| 10537 |
@<initialize the |hnode_size| array@>= |
| 10538 |
hnode_size[TAG(adjust_kind,1)] = NODE_SIZE(0,1); |
| 10539 |
@ |
| 10540 |
|
| 10541 |
\subsection{Tables}\index{alignment} |
| 10542 |
Tables have an extended dimension either as a node or as a reference followed |
| 10543 |
by two lists. |
| 10544 |
@<initialize the |hnode_size| array@>= |
| 10545 |
hnode_size[TAG(table_kind,b000)] = NODE_SIZE(1,2); |
| 10546 |
hnode_size[TAG(table_kind,b001)] = NODE_SIZE(1,2); |
| 10547 |
hnode_size[TAG(table_kind,b010)] = NODE_SIZE(1,2); |
| 10548 |
hnode_size[TAG(table_kind,b011)] = NODE_SIZE(1,2); |
| 10549 |
hnode_size[TAG(table_kind,b100)] = NODE_SIZE(0,3); |
| 10550 |
hnode_size[TAG(table_kind,b101)] = NODE_SIZE(0,3); |
| 10551 |
hnode_size[TAG(table_kind,b110)] = NODE_SIZE(0,3); |
| 10552 |
hnode_size[TAG(table_kind,b111)] = NODE_SIZE(0,3); |
| 10553 |
@ |
| 10554 |
Outer item nodes are lists of inner item nodes, inner item nodes are box nodes |
| 10555 |
followed by an optional span count. |
| 10556 |
@<initialize the |hnode_size| array@>= |
| 10557 |
hnode_size[TAG(item_kind,b000)] = NODE_SIZE(0,1); /* outer */ |
| 10558 |
hnode_size[TAG(item_kind,1)] = NODE_SIZE(0,1); /* inner */ |
| 10559 |
hnode_size[TAG(item_kind,2)] = NODE_SIZE(0,1); |
| 10560 |
hnode_size[TAG(item_kind,3)] = NODE_SIZE(0,1); |
| 10561 |
hnode_size[TAG(item_kind,4)] = NODE_SIZE(0,1); |
| 10562 |
hnode_size[TAG(item_kind,5)] = NODE_SIZE(0,1); |
| 10563 |
hnode_size[TAG(item_kind,6)] = NODE_SIZE(0,1); |
| 10564 |
hnode_size[TAG(item_kind,7)] = NODE_SIZE(1,1); |
| 10565 |
@ |
| 10566 |
|
| 10567 |
\subsection{Images}\index{image} |
| 10568 |
If not given by a reference, images contain a section reference and optional dimensions and a descriptive list. |
| 10569 |
@<initialize the |hnode_size| array@>= |
| 10570 |
hnode_size[TAG(image_kind,b000)] = NODE_SIZE(1,0); |
| 10571 |
hnode_size[TAG(image_kind,b001)] = NODE_SIZE(2+4+4,1); |
| 10572 |
hnode_size[TAG(image_kind,b010)] = NODE_SIZE(2+4+4,1); |
| 10573 |
hnode_size[TAG(image_kind,b011)] = NODE_SIZE(2+4+4,1); |
| 10574 |
hnode_size[TAG(image_kind,b100)] = NODE_SIZE(2+4+1+1,1); |
| 10575 |
hnode_size[TAG(image_kind,b101)] = NODE_SIZE(2+4+1,2); |
| 10576 |
hnode_size[TAG(image_kind,b110)] = NODE_SIZE(2+4+1,2); |
| 10577 |
hnode_size[TAG(image_kind,b111)] = NODE_SIZE(2+4,3); |
| 10578 |
@ |
| 10579 |
|
| 10580 |
\subsection{Links}\index{link} |
| 10581 |
Links contain either a 2 byte or a 1 byte reference and possibly a color reference. |
| 10582 |
@<initialize the |hnode_size| array@>= |
| 10583 |
hnode_size[TAG(link_kind,b000)] = NODE_SIZE(1,0); |
| 10584 |
hnode_size[TAG(link_kind,b001)] = NODE_SIZE(2,0); |
| 10585 |
hnode_size[TAG(link_kind,b010)] = NODE_SIZE(1,0); |
| 10586 |
hnode_size[TAG(link_kind,b011)] = NODE_SIZE(2,0); |
| 10587 |
hnode_size[TAG(link_kind,b100)] = NODE_SIZE(2,0); |
| 10588 |
hnode_size[TAG(link_kind,b101)] = NODE_SIZE(3,0); |
| 10589 |
hnode_size[TAG(link_kind,b110)] = NODE_SIZE(2,0); |
| 10590 |
hnode_size[TAG(link_kind,b111)] = NODE_SIZE(3,0); |
| 10591 |
@ |
| 10592 |
|
| 10593 |
\subsection{Streams}\index{stream} |
| 10594 |
After the stream reference follows a parameter list, either as reference |
| 10595 |
or as a list, and then a content list. |
| 10596 |
@<initialize the |hnode_size| array@>= |
| 10597 |
hnode_size[TAG(stream_kind,b000)] = NODE_SIZE(1+1,1); |
| 10598 |
hnode_size[TAG(stream_kind,b010)] = NODE_SIZE(1,2); |
| 10599 |
hnode_size[TAG(stream_kind,b100)] = NODE_SIZE(1,0); |
| 10600 |
@ |
| 10601 |
|
| 10602 |
\subsection{Colors}\index{color} |
| 10603 |
@<initialize the |hnode_size| array@>= |
| 10604 |
hnode_size[TAG(color_kind,b000)] = NODE_SIZE(1,0); |
| 10605 |
@ |
| 10606 |
|
| 10607 |
\section{Reading Short Format Files Backwards} |
| 10608 |
This section is not really part of the file format definition, but it |
| 10609 |
illustrates an important property of the content section in short |
| 10610 |
format files: it can be read in both directions. This is important |
| 10611 |
because we want to be able to start at an arbitrary point in the |
| 10612 |
content and from there move pagewise backward. |
| 10613 |
|
| 10614 |
The program {\tt skip}\index{skip+{\tt skip}} described in this |
| 10615 |
section does just that. As wee see in appendix~\secref{skip}, its |
| 10616 |
|main| program is almost the same as the |main| program of the program |
| 10617 |
{\tt stretch} in appendix~\secref{stretchmain}. |
| 10618 |
The major difference is the removal of an output file |
| 10619 |
and the replacement of the call to |hwrite_content_section| by |
| 10620 |
a call to |hteg_content_section|. |
| 10621 |
|
| 10622 |
@<skip functions@>= |
| 10623 |
static void hteg_content_section(void) |
| 10624 |
{ hget_section(2); |
| 10625 |
hpos=hend; |
| 10626 |
while(hpos>hstart) |
| 10627 |
hteg_content_node(); |
| 10628 |
} |
| 10629 |
@ |
| 10630 |
|
| 10631 |
The functions |hteg_content_section| and |hteg_content_node| above are |
| 10632 |
reverse versions of the functions |hget_content_section| and |
| 10633 |
|hget_content_node|. Many such ``reverse functions'' will follow now |
| 10634 |
and we will consistently use the same naming scheme: replacing |
| 10635 |
``{\it get\/}`` by ``{\it teg\/}'' or ``{\tt GET}'' by ``{\tt TEG}''. |
| 10636 |
The {\tt skip} program does not do much input |
| 10637 |
checking; it will just extract enough information from a content node |
| 10638 |
to skip a node and ``advance'' or better ``retreat'' to the previous |
| 10639 |
node. |
| 10640 |
|
| 10641 |
@<skip functions@>= |
| 10642 |
static void hteg_content_node(void) |
| 10643 |
{ @<skip the end byte |z|@>@; |
| 10644 |
hteg_content(z); |
| 10645 |
@<skip and check the start byte |a|@>@; |
| 10646 |
} |
| 10647 |
|
| 10648 |
static void hteg_content(Tag z) |
| 10649 |
{@+ switch (z)@/ |
| 10650 |
{ |
| 10651 |
@<cases to skip content@>@;@t\1@>@/ |
| 10652 |
default: |
| 10653 |
if (!hteg_unknown(z)) |
| 10654 |
TAGERR(z); |
| 10655 |
break;@t\2@>@/ |
| 10656 |
} |
| 10657 |
} |
| 10658 |
@ |
| 10659 |
|
| 10660 |
The code to skip the end\index{end byte} byte |z| and to check the start\index{start byte} byte |a| is used repeatedly. |
| 10661 |
|
| 10662 |
@<skip the end byte |z|@>= |
| 10663 |
Tag a,z; /* the start and the end byte*/ |
| 10664 |
uint32_t node_pos=hpos-hstart; |
| 10665 |
if (hpos<=hstart) return; |
| 10666 |
HTEGTAG(z); |
| 10667 |
@ |
| 10668 |
|
| 10669 |
@<skip and check the start byte |a|@>= |
| 10670 |
HTEGTAG(a); |
| 10671 |
if (a!=z) QUIT(@["Tag mismatch [%s,%d]!=[%s,%d] at " SIZE_F " to 0x%x\n"@],@|NAME(a),INFO(a),NAME(z),INFO(z),@| |
| 10672 |
hpos-hstart,node_pos-1); |
| 10673 |
@ |
| 10674 |
|
| 10675 |
We replace the ``{\tt GET}'' macros by the following ``{\tt TEG}'' macros: |
| 10676 |
|
| 10677 |
@<shared get macros@>= |
| 10678 |
#define @[HBACK(X)@] @[((hpos-(X)<hstart)?(QUIT("HTEG underflow\n"),NULL):(hpos-=(X)))@] |
| 10679 |
|
| 10680 |
#define @[HTEG8@] (HBACK(1),hpos[0]) |
| 10681 |
#define @[HTEG16(X)@] (HBACK(2),(X)=(hpos[0]<<8)+hpos[1]) |
| 10682 |
#define @[HTEG24(X)@] (HBACK(3),(X)=(hpos[0]<<16)+(hpos[1]<<8)+hpos[2]) |
| 10683 |
#define @[HTEG32(X)@] (HBACK(4),(X)=(hpos[0]<<24)+(hpos[1]<<16)+(hpos[2]<<8)+hpos[3]) |
| 10684 |
#define @[HTEGTAG(X)@] @[X=HTEG8,DBGTAG(X,hpos)@] |
| 10685 |
@ |
| 10686 |
|
| 10687 |
Now we review step by step the different kinds of nodes. |
| 10688 |
\subsection{Floating Point Numbers}\index{floating point number} |
| 10689 |
\noindent |
| 10690 |
@<shared skip functions@>= |
| 10691 |
float32_t hteg_float32(void) |
| 10692 |
{ union {@+float32_t d; @+ uint32_t bits; @+} u; |
| 10693 |
HTEG32(u.bits); |
| 10694 |
return u.d; |
| 10695 |
} |
| 10696 |
@ |
| 10697 |
|
| 10698 |
|
| 10699 |
\subsection{Extended Dimensions}\index{extended dimension} |
| 10700 |
\noindent |
| 10701 |
@<skip macros@>= |
| 10702 |
#define @[HTEG_XDIMEN(I,X)@] \ |
| 10703 |
if((I)&b001) HTEG32((X).v); \ |
| 10704 |
if((I)&b010) HTEG32((X).h);\ |
| 10705 |
if((I)&b100) HTEG32((X).w); |
| 10706 |
@ |
| 10707 |
|
| 10708 |
@<skip functions@>= |
| 10709 |
static void hteg_xdimen_node(Xdimen *x) |
| 10710 |
{ @<skip the end byte |z|@>@; |
| 10711 |
switch(z) |
| 10712 |
{ |
| 10713 |
#if 0 |
| 10714 |
/* currently the info value 0 is not supported */ |
| 10715 |
case TAG(xdimen_kind,b000): /* see section~\secref{reference} */ |
| 10716 |
{uint8_t n;@+ n=HTEG8;} @+ break; |
| 10717 |
#endif |
| 10718 |
case TAG(xdimen_kind,b001): HTEG_XDIMEN(b001,*x);@+break; |
| 10719 |
case TAG(xdimen_kind,b010): HTEG_XDIMEN(b010,*x);@+break; |
| 10720 |
case TAG(xdimen_kind,b011): HTEG_XDIMEN(b011,*x);@+break; |
| 10721 |
case TAG(xdimen_kind,b100): HTEG_XDIMEN(b100,*x);@+break; |
| 10722 |
case TAG(xdimen_kind,b101): HTEG_XDIMEN(b101,*x);@+break; |
| 10723 |
case TAG(xdimen_kind,b110): HTEG_XDIMEN(b110,*x);@+break; |
| 10724 |
case TAG(xdimen_kind,b111): HTEG_XDIMEN(b111,*x);@+break; |
| 10725 |
default: |
| 10726 |
QUIT("Extent expected at 0x%x got %s",node_pos,NAME(z)); @+ break; |
| 10727 |
} |
| 10728 |
@<skip and check the start byte |a|@>@; |
| 10729 |
} |
| 10730 |
@ |
| 10731 |
|
| 10732 |
|
| 10733 |
\subsection{Stretch and Shrink}\index{stretchability}\index{shrinkability} |
| 10734 |
\noindent |
| 10735 |
@<skip macros@>= |
| 10736 |
#define @[HTEG_STRETCH(S)@] { Stch st; @+ HTEG32(st.u);@+ S.o=st.u&3;@+ st.u&=~3;@+ S.f=st.f; @+} |
| 10737 |
@ |
| 10738 |
|
| 10739 |
\subsection{Glyphs}\index{glyph} |
| 10740 |
\noindent |
| 10741 |
@<skip macros@>= |
| 10742 |
#define HTEG_GLYPH(I,G) \ |
| 10743 |
(G).f=HTEG8; \ |
| 10744 |
if (I==1) (G).c=HTEG8;\ |
| 10745 |
else if (I==2) HTEG16((G).c);\ |
| 10746 |
else if (I==3) HTEG24((G).c);\ |
| 10747 |
else if (I==4) HTEG32((G).c); |
| 10748 |
@ |
| 10749 |
|
| 10750 |
@<cases to skip content@>= |
| 10751 |
@t\1\kern1em@>case TAG(glyph_kind,1): @+{@+Glyph g;@+ HTEG_GLYPH(1,g);@+}@+break; |
| 10752 |
case TAG(glyph_kind,2): @+{@+Glyph g;@+ HTEG_GLYPH(2,g);@+}@+break; |
| 10753 |
case TAG(glyph_kind,3): @+{@+Glyph g;@+ HTEG_GLYPH(3,g);@+}@+break; |
| 10754 |
case TAG(glyph_kind,4): @+{@+Glyph g;@+ HTEG_GLYPH(4,g);@+}@+break; |
| 10755 |
@ |
| 10756 |
|
| 10757 |
|
| 10758 |
\subsection{Penalties}\index{penalty} |
| 10759 |
\noindent |
| 10760 |
@<skip macros@>= |
| 10761 |
#define @[HTEG_PENALTY(I,P)@] \ |
| 10762 |
if (I==1) {int8_t n; @+n=HTEG8; @+P=n;@+ } \ |
| 10763 |
else if (I==2) {int16_t n;@+ HTEG16(n); @+ P=n; @+}\ |
| 10764 |
else if (I==3) {int32_t n;@+ HTEG32(n); @+ P=n; @+} |
| 10765 |
@ |
| 10766 |
|
| 10767 |
@<cases to skip content@>= |
| 10768 |
@t\1\kern1em@>case TAG(penalty_kind,1): @+{int32_t p;@+ HTEG_PENALTY(1,p);@+} @+break; |
| 10769 |
case TAG(penalty_kind,2): @+{int32_t p;@+ HTEG_PENALTY(2,p);@+} @+break; |
| 10770 |
case TAG(penalty_kind,3): @+{int32_t p;@+ HTEG_PENALTY(2,p);@+} @+break; |
| 10771 |
@ |
| 10772 |
|
| 10773 |
|
| 10774 |
\subsection{Kerns}\index{kern} |
| 10775 |
\noindent |
| 10776 |
@<skip macros@>= |
| 10777 |
#define @[HTEG_KERN(I,X)@] @[if (((I)&b011)==2) HTEG32(X.w); else if (((I)&b011)==3) hteg_xdimen_node(&(X))@] |
| 10778 |
@ |
| 10779 |
|
| 10780 |
@<cases to skip content@>= |
| 10781 |
@t\1\kern1em@>case TAG(kern_kind,b010): @+ {@+Xdimen x; @+HTEG_KERN(b010,x);@+ } @+break; |
| 10782 |
case TAG(kern_kind,b011): @+ {@+Xdimen x; @+HTEG_KERN(b011,x);@+ } @+break; |
| 10783 |
case TAG(kern_kind,b110): @+ {@+Xdimen x; @+HTEG_KERN(b110,x);@+ } @+break; |
| 10784 |
case TAG(kern_kind,b111): @+ {@+Xdimen x; @+HTEG_KERN(b111,x);@+ } @+break; |
| 10785 |
@ |
| 10786 |
|
| 10787 |
\subsection{Language}\index{language} |
| 10788 |
\noindent |
| 10789 |
@<cases to skip content@>= |
| 10790 |
@t\kern1em@>case TAG(language_kind,1): |
| 10791 |
case TAG(language_kind,2): |
| 10792 |
case TAG(language_kind,3): |
| 10793 |
case TAG(language_kind,4): |
| 10794 |
case TAG(language_kind,5): |
| 10795 |
case TAG(language_kind,6): |
| 10796 |
case TAG(language_kind,7):@+break; |
| 10797 |
@ |
| 10798 |
|
| 10799 |
\subsection{Rules}\index{rule} |
| 10800 |
\noindent |
| 10801 |
@<skip macros@>= |
| 10802 |
#define @[HTEG_RULE(I,R)@]@/\ |
| 10803 |
if ((I)&b001) HTEG32((R).w); @+else (R).w=RUNNING_DIMEN;\ |
| 10804 |
if ((I)&b010) HTEG32((R).d); @+else (R).d=RUNNING_DIMEN;\ |
| 10805 |
if ((I)&b100) HTEG32((R).h); @+else (R).h=RUNNING_DIMEN; |
| 10806 |
@ |
| 10807 |
|
| 10808 |
@<cases to skip content@>= |
| 10809 |
@t\1\kern1em@>case TAG(rule_kind,b011): @+ {Rule r;@+ HTEG_RULE(b011,r);@+ }@+ break; |
| 10810 |
case TAG(rule_kind,b101): @+ {Rule r;@+ HTEG_RULE(b101,r);@+ }@+ break; |
| 10811 |
case TAG(rule_kind,b001): @+ {Rule r;@+ HTEG_RULE(b001,r);@+ }@+ break; |
| 10812 |
case TAG(rule_kind,b110): @+ {Rule r;@+ HTEG_RULE(b110,r);@+ }@+ break; |
| 10813 |
case TAG(rule_kind,b111): @+ {Rule r;@+ HTEG_RULE(b111,r);@+ }@+ break; |
| 10814 |
@ |
| 10815 |
|
| 10816 |
@<skip functions@>= |
| 10817 |
static void hteg_rule_node(void) |
| 10818 |
{ @<skip the end byte |z|@>@; |
| 10819 |
if (KIND(z)==rule_kind) { @+Rule r; @+HTEG_RULE(INFO(z),r); @+} |
| 10820 |
else |
| 10821 |
QUIT("Rule expected at 0x%x got %s",node_pos,NAME(z)); |
| 10822 |
@<skip and check the start byte |a|@>@; |
| 10823 |
} |
| 10824 |
@ |
| 10825 |
\subsection{Glue}\index{glue} |
| 10826 |
\noindent |
| 10827 |
@<skip macros@>= |
| 10828 |
#define @[HTEG_GLUE(I,G)@] @/\ |
| 10829 |
if(I==b111) hteg_xdimen_node(&((G).w)); else (G).w.h=(G).w.v=0.0;\ |
| 10830 |
if((I)&b001) HTEG_STRETCH((G).m) @+else (G).m.f=0.0, (G).m.o=0; \ |
| 10831 |
if((I)&b010) HTEG_STRETCH((G).p) @+else (G).p.f=0.0, (G).p.o=0;\ |
| 10832 |
if((I)!=b111) { @+if ((I)&b100) HTEG32((G).w.w);@+ else (G).w.w=0;@+ } |
| 10833 |
@ |
| 10834 |
|
| 10835 |
@<cases to skip content@>= |
| 10836 |
@t\1\kern1em@>case TAG(glue_kind,b001): @+{ Glue g;@+ HTEG_GLUE(b001,g);@+}@+break; |
| 10837 |
case TAG(glue_kind,b010): @+{ Glue g;@+ HTEG_GLUE(b010,g);@+}@+break; |
| 10838 |
case TAG(glue_kind,b011): @+{ Glue g;@+ HTEG_GLUE(b011,g);@+}@+break; |
| 10839 |
case TAG(glue_kind,b100): @+{ Glue g;@+ HTEG_GLUE(b100,g);@+}@+break; |
| 10840 |
case TAG(glue_kind,b101): @+{ Glue g;@+ HTEG_GLUE(b101,g);@+}@+break; |
| 10841 |
case TAG(glue_kind,b110): @+{ Glue g;@+ HTEG_GLUE(b110,g);@+}@+break; |
| 10842 |
case TAG(glue_kind,b111): @+{ Glue g;@+ HTEG_GLUE(b111,g);@+}@+break; |
| 10843 |
@ |
| 10844 |
|
| 10845 |
@<skip functions@>= |
| 10846 |
static void hteg_glue_node(void) |
| 10847 |
{ @<skip the end byte |z|@>@; |
| 10848 |
if (INFO(z)==b000) HTEG_REF(glue_kind); |
| 10849 |
else |
| 10850 |
{ @+Glue g; @+HTEG_GLUE(INFO(z),g);@+} |
| 10851 |
@<skip and check the start byte |a|@>@; |
| 10852 |
} |
| 10853 |
@ |
| 10854 |
|
| 10855 |
\subsection{Boxes}\index{box} |
| 10856 |
\noindent |
| 10857 |
@<skip macros@>= |
| 10858 |
#define @[HTEG_BOX(I,B)@] \ |
| 10859 |
hteg_list(&(B.l));\ |
| 10860 |
if ((I)&b100) @/{ B.s=HTEG8; @+ B.r=hteg_float32();@+ B.o=B.s&0xF; @+B.s=B.s>>4;@+ }\ |
| 10861 |
else { B.r=0.0;@+ B.o=B.s=0;@+ }\ |
| 10862 |
if ((I)&b010) HTEG32(B.a); @+else B.a=0;\ |
| 10863 |
HTEG32(B.w);\ |
| 10864 |
if ((I)&b001) HTEG32(B.d); @+ else B.d=0;\ |
| 10865 |
HTEG32(B.h);\ |
| 10866 |
@ |
| 10867 |
|
| 10868 |
@<cases to skip content@>= |
| 10869 |
@t\1\kern1em@> case TAG(hbox_kind,b000): @+{Box b; @+HTEG_BOX(b000,b);@+} @+ break; |
| 10870 |
case TAG(hbox_kind,b001): @+{Box b; @+HTEG_BOX(b001,b);@+} @+ break; |
| 10871 |
case TAG(hbox_kind,b010): @+{Box b; @+HTEG_BOX(b010,b);@+} @+ break; |
| 10872 |
case TAG(hbox_kind,b011): @+{Box b; @+HTEG_BOX(b011,b);@+} @+ break; |
| 10873 |
case TAG(hbox_kind,b100): @+{Box b; @+HTEG_BOX(b100,b);@+} @+ break; |
| 10874 |
case TAG(hbox_kind,b101): @+{Box b; @+HTEG_BOX(b101,b);@+} @+ break; |
| 10875 |
case TAG(hbox_kind,b110): @+{Box b; @+HTEG_BOX(b110,b);@+} @+ break; |
| 10876 |
case TAG(hbox_kind,b111): @+{Box b; @+HTEG_BOX(b111,b);@+} @+ break; |
| 10877 |
case TAG(vbox_kind,b000): @+{Box b; @+HTEG_BOX(b000,b);@+} @+ break; |
| 10878 |
case TAG(vbox_kind,b001): @+{Box b; @+HTEG_BOX(b001,b);@+} @+ break; |
| 10879 |
case TAG(vbox_kind,b010): @+{Box b; @+HTEG_BOX(b010,b);@+} @+ break; |
| 10880 |
case TAG(vbox_kind,b011): @+{Box b; @+HTEG_BOX(b011,b);@+} @+ break; |
| 10881 |
case TAG(vbox_kind,b100): @+{Box b; @+HTEG_BOX(b100,b);@+} @+ break; |
| 10882 |
case TAG(vbox_kind,b101): @+{Box b; @+HTEG_BOX(b101,b);@+} @+ break; |
| 10883 |
case TAG(vbox_kind,b110): @+{Box b; @+HTEG_BOX(b110,b);@+} @+ break; |
| 10884 |
case TAG(vbox_kind,b111): @+{Box b; @+HTEG_BOX(b111,b);@+} @+ break; |
| 10885 |
@ |
| 10886 |
|
| 10887 |
@<skip functions@>= |
| 10888 |
static void hteg_hbox_node(void) |
| 10889 |
{ Box b; |
| 10890 |
@<skip the end byte |z|@>@; |
| 10891 |
if (KIND(z)!=hbox_kind) QUIT("Hbox expected at 0x%x got %s",node_pos,NAME(z)); |
| 10892 |
HTEG_BOX(INFO(z),b);@/ |
| 10893 |
@<skip and check the start byte |a|@>@; |
| 10894 |
} |
| 10895 |
|
| 10896 |
static void hteg_vbox_node(void) |
| 10897 |
{ Box b; |
| 10898 |
@<skip the end byte |z|@>@; |
| 10899 |
if (KIND(z)!=vbox_kind) QUIT("Vbox expected at 0x%x got %s",node_pos,NAME(z)); |
| 10900 |
HTEG_BOX(INFO(z),b);@/ |
| 10901 |
@<skip and check the start byte |a|@>@; |
| 10902 |
} |
| 10903 |
@ |
| 10904 |
|
| 10905 |
|
| 10906 |
\subsection{Extended Boxes}\index{extended box} |
| 10907 |
\noindent |
| 10908 |
@<skip macros@>= |
| 10909 |
#define @[HTEG_SET(I)@] @/\ |
| 10910 |
{ List l; @+hteg_list(&l); @+} \ |
| 10911 |
if ((I)&b100) {Xdimen x;@+ hteg_xdimen_node(&x); @+} \ |
| 10912 |
else HTEG_REF(xdimen_kind);\ |
| 10913 |
{ Stretch m; @+HTEG_STRETCH(m);@+}\ |
| 10914 |
{ Stretch p; @+HTEG_STRETCH(p);@+}\ |
| 10915 |
if ((I)&b010) { Dimen a; @+HTEG32(a);@+} \ |
| 10916 |
{ Dimen w; @+HTEG32(w);@+} \ |
| 10917 |
{ Dimen d; @+if ((I)&b001) HTEG32(d); @+ else d=0;@+}\ |
| 10918 |
{ Dimen h; @+HTEG32(h);@+} |
| 10919 |
@# |
| 10920 |
|
| 10921 |
#define @[HTEG_PACK(K,I)@] @/\ |
| 10922 |
{ List l; @+hteg_list(&l); @+} \ |
| 10923 |
if ((I)&b100) {Xdimen x; hteg_xdimen_node(&x);@+} @+ else HTEG_REF(xdimen_kind);\ |
| 10924 |
if ((I)&b010) { Dimen d; @+HTEG32(d); @+ }\ |
| 10925 |
if (K==vpack_kind) { Dimen d; @+HTEG32(d); @+ } |
| 10926 |
@ |
| 10927 |
|
| 10928 |
@<cases to skip content@>= |
| 10929 |
@t\1\kern1em@>case TAG(hset_kind,b000): HTEG_SET(b000); @+ break; |
| 10930 |
case TAG(hset_kind,b001): HTEG_SET(b001); @+ break; |
| 10931 |
case TAG(hset_kind,b010): HTEG_SET(b010); @+ break; |
| 10932 |
case TAG(hset_kind,b011): HTEG_SET(b011); @+ break; |
| 10933 |
case TAG(hset_kind,b100): HTEG_SET(b100); @+ break; |
| 10934 |
case TAG(hset_kind,b101): HTEG_SET(b101); @+ break; |
| 10935 |
case TAG(hset_kind,b110): HTEG_SET(b110); @+ break; |
| 10936 |
case TAG(hset_kind,b111): HTEG_SET(b111); @+ break;@# |
| 10937 |
|
| 10938 |
case TAG(vset_kind,b000): HTEG_SET(b000); @+ break; |
| 10939 |
case TAG(vset_kind,b001): HTEG_SET(b001); @+ break; |
| 10940 |
case TAG(vset_kind,b010): HTEG_SET(b010); @+ break; |
| 10941 |
case TAG(vset_kind,b011): HTEG_SET(b011); @+ break; |
| 10942 |
case TAG(vset_kind,b100): HTEG_SET(b100); @+ break; |
| 10943 |
case TAG(vset_kind,b101): HTEG_SET(b101); @+ break; |
| 10944 |
case TAG(vset_kind,b110): HTEG_SET(b110); @+ break; |
| 10945 |
case TAG(vset_kind,b111): HTEG_SET(b111); @+ break;@# |
| 10946 |
|
| 10947 |
case TAG(hpack_kind,b000): HTEG_PACK(hpack_kind,b000); @+ break; |
| 10948 |
case TAG(hpack_kind,b001): HTEG_PACK(hpack_kind,b001); @+ break; |
| 10949 |
case TAG(hpack_kind,b010): HTEG_PACK(hpack_kind,b010); @+ break; |
| 10950 |
case TAG(hpack_kind,b011): HTEG_PACK(hpack_kind,b011); @+ break; |
| 10951 |
case TAG(hpack_kind,b100): HTEG_PACK(hpack_kind,b100); @+ break; |
| 10952 |
case TAG(hpack_kind,b101): HTEG_PACK(hpack_kind,b101); @+ break; |
| 10953 |
case TAG(hpack_kind,b110): HTEG_PACK(hpack_kind,b110); @+ break; |
| 10954 |
case TAG(hpack_kind,b111): HTEG_PACK(hpack_kind,b111); @+ break;@# |
| 10955 |
|
| 10956 |
case TAG(vpack_kind,b000): HTEG_PACK(vpack_kind,b000); @+ break; |
| 10957 |
case TAG(vpack_kind,b001): HTEG_PACK(vpack_kind,b001); @+ break; |
| 10958 |
case TAG(vpack_kind,b010): HTEG_PACK(vpack_kind,b010); @+ break; |
| 10959 |
case TAG(vpack_kind,b011): HTEG_PACK(vpack_kind,b011); @+ break; |
| 10960 |
case TAG(vpack_kind,b100): HTEG_PACK(vpack_kind,b100); @+ break; |
| 10961 |
case TAG(vpack_kind,b101): HTEG_PACK(vpack_kind,b101); @+ break; |
| 10962 |
case TAG(vpack_kind,b110): HTEG_PACK(vpack_kind,b110); @+ break; |
| 10963 |
case TAG(vpack_kind,b111): HTEG_PACK(vpack_kind,b111); @+ break; |
| 10964 |
@ |
| 10965 |
|
| 10966 |
|
| 10967 |
\subsection{Leaders}\index{leaders} |
| 10968 |
\noindent |
| 10969 |
@<skip macros@>= |
| 10970 |
#define @[HTEG_LEADERS(I)@]@/ \ |
| 10971 |
if (KIND(hpos[-1])==rule_kind) hteg_rule_node(); \ |
| 10972 |
else if (KIND(hpos[-1])==hbox_kind) hteg_hbox_node();\ |
| 10973 |
else hteg_vbox_node();\ |
| 10974 |
if ((I)&b100) hteg_glue_node(); |
| 10975 |
@ |
| 10976 |
|
| 10977 |
@<cases to skip content@>= |
| 10978 |
@t\1\kern1em@>case TAG(leaders_kind,1): @+ HTEG_LEADERS(1); @+break; |
| 10979 |
case TAG(leaders_kind,2): @+ HTEG_LEADERS(2); @+break; |
| 10980 |
case TAG(leaders_kind,3): @+ HTEG_LEADERS(3); @+break; |
| 10981 |
case TAG(leaders_kind,b100|1): @+ HTEG_LEADERS(b100|1); @+break; |
| 10982 |
case TAG(leaders_kind,b100|2): @+ HTEG_LEADERS(b100|2); @+break; |
| 10983 |
case TAG(leaders_kind,b100|3): @+ HTEG_LEADERS(b100|3); @+break; |
| 10984 |
@ |
| 10985 |
|
| 10986 |
\subsection{Baseline Skips}\index{baseline skip} |
| 10987 |
\noindent |
| 10988 |
@<skip macros@>= |
| 10989 |
#define @[HTEG_BASELINE(I,B)@] \ |
| 10990 |
if((I)&b010) hteg_glue_node(); \ |
| 10991 |
else {B.ls.p.o=B.ls.m.o=B.ls.w.w=0; @+B.ls.w.h=B.ls.w.v=B.ls.p.f=B.ls.m.f=0.0;@+}\ |
| 10992 |
if((I)&b100) hteg_glue_node(); \ |
| 10993 |
else {B.bs.p.o=B.bs.m.o=B.bs.w.w=0; @+B.bs.w.h=B.bs.w.v=B.bs.p.f=B.bs.m.f=0.0;@+}\ |
| 10994 |
if((I)&b001) HTEG32((B).lsl); @+else B.lsl=0; |
| 10995 |
@ |
| 10996 |
|
| 10997 |
@<cases to skip content@>= |
| 10998 |
@t\1\kern1em@>case TAG(baseline_kind,b001): @+{ Baseline b;@+ HTEG_BASELINE(b001,b);@+ }@+break; |
| 10999 |
case TAG(baseline_kind,b010): @+{ Baseline b;@+ HTEG_BASELINE(b010,b);@+ }@+break; |
| 11000 |
case TAG(baseline_kind,b011): @+{ Baseline b;@+ HTEG_BASELINE(b011,b);@+ }@+break; |
| 11001 |
case TAG(baseline_kind,b100): @+{ Baseline b;@+ HTEG_BASELINE(b100,b);@+ }@+break; |
| 11002 |
case TAG(baseline_kind,b101): @+{ Baseline b;@+ HTEG_BASELINE(b101,b);@+ }@+break; |
| 11003 |
case TAG(baseline_kind,b110): @+{ Baseline b;@+ HTEG_BASELINE(b110,b);@+ }@+break; |
| 11004 |
case TAG(baseline_kind,b111): @+{ Baseline b;@+ HTEG_BASELINE(b111,b);@+ }@+break; |
| 11005 |
@ |
| 11006 |
\subsection{Ligatures}\index{ligature} |
| 11007 |
\noindent |
| 11008 |
@<skip macros@>= |
| 11009 |
#define @[HTEG_LIG(I,L)@] @/\ |
| 11010 |
if ((I)==7) hteg_list(&((L).l)); \ |
| 11011 |
else {(L).l.s=(I); @+hpos-=(L).l.s; @+ (L).l.p=hpos-hstart;@+} \ |
| 11012 |
(L).f=HTEG8; |
| 11013 |
@ |
| 11014 |
|
| 11015 |
@<cases to skip content@>= |
| 11016 |
@t\1\kern1em@>case TAG(ligature_kind,1):@+ {Lig l; @+HTEG_LIG(1,l);@+} @+break; |
| 11017 |
case TAG(ligature_kind,2):@+ {Lig l; @+HTEG_LIG(2,l);@+} @+break; |
| 11018 |
case TAG(ligature_kind,3):@+ {Lig l; @+HTEG_LIG(3,l);@+} @+break; |
| 11019 |
case TAG(ligature_kind,4):@+ {Lig l; @+HTEG_LIG(4,l);@+} @+break; |
| 11020 |
case TAG(ligature_kind,5):@+ {Lig l; @+HTEG_LIG(5,l);@+} @+break; |
| 11021 |
case TAG(ligature_kind,6):@+ {Lig l; @+HTEG_LIG(6,l);@+} @+break; |
| 11022 |
case TAG(ligature_kind,7):@+ {Lig l; @+HTEG_LIG(7,l);@+} @+break; |
| 11023 |
@ |
| 11024 |
|
| 11025 |
|
| 11026 |
\subsection{Discretionary breaks}\index{discretionary breaks} |
| 11027 |
\noindent |
| 11028 |
@<skip macros@>= |
| 11029 |
#define @[HTEG_DISC(I,H)@]\ |
| 11030 |
if ((I)&b001) hteg_list(&((H).q)); else { (H).q.p=hpos-hstart; @+(H).q.s=0; @+(H).q.t=TAG(list_kind,b000); @+}\ |
| 11031 |
if ((I)&b010) hteg_list(&((H).p)); else { (H).p.p=hpos-hstart; @+(H).p.s=0; @+(H).p.t=TAG(list_kind,b000); @+} \ |
| 11032 |
if ((I)&b100) (H).r=HTEG8; @+else (H).r=0; |
| 11033 |
@ |
| 11034 |
@<cases to skip content@>= |
| 11035 |
@t\1\kern1em@>case TAG(disc_kind,b001): @+{Disc h; @+HTEG_DISC(b001,h); @+} @+break; |
| 11036 |
case TAG(disc_kind,b010): @+{Disc h; @+HTEG_DISC(b010,h); @+} @+break; |
| 11037 |
case TAG(disc_kind,b011): @+{Disc h; @+HTEG_DISC(b011,h); @+} @+break; |
| 11038 |
case TAG(disc_kind,b100): @+{Disc h; @+HTEG_DISC(b100,h); @+} @+break; |
| 11039 |
case TAG(disc_kind,b101): @+{Disc h; @+HTEG_DISC(b101,h); @+} @+break; |
| 11040 |
case TAG(disc_kind,b110): @+{Disc h; @+HTEG_DISC(b110,h); @+} @+break; |
| 11041 |
case TAG(disc_kind,b111): @+{Disc h; @+HTEG_DISC(b111,h); @+} @+break; |
| 11042 |
@ |
| 11043 |
|
| 11044 |
|
| 11045 |
\subsection{Paragraphs}\index{paragraph} |
| 11046 |
\noindent |
| 11047 |
@<skip macros@>= |
| 11048 |
#define @[HTEG_PAR(I)@] @/\ |
| 11049 |
{ List l; @+hteg_list(&l); @+} \ |
| 11050 |
if ((I)&b010) { List l; @+hteg_param_list(&l); @+} else if ((I)!=b100) HTEG_REF(param_kind);\ |
| 11051 |
if ((I)&b100) {Xdimen x; @+ hteg_xdimen_node(&x); @+} else HTEG_REF(xdimen_kind);\ |
| 11052 |
if ((I)==b100) HTEG_REF(param_kind); |
| 11053 |
@ |
| 11054 |
|
| 11055 |
@<cases to skip content@>= |
| 11056 |
@t\1\kern1em@>case TAG(par_kind,b000): @+HTEG_PAR(b000);@+break; |
| 11057 |
case TAG(par_kind,b010): @+HTEG_PAR(b010);@+break; |
| 11058 |
case TAG(par_kind,b100): @+HTEG_PAR(b100);@+break; |
| 11059 |
case TAG(par_kind,b110): @+HTEG_PAR(b110);@+break; |
| 11060 |
@ |
| 11061 |
|
| 11062 |
|
| 11063 |
\subsection{Mathematics}\index{mathematics}\index{displayed formula}% |
| 11064 |
\noindent |
| 11065 |
@<skip macros@>= |
| 11066 |
#define @[HTEG_MATH(I)@] \ |
| 11067 |
if ((I)&b001) hteg_hbox_node();\ |
| 11068 |
{ List l; @+hteg_list(&l); @+} \ |
| 11069 |
if ((I)&b010) hteg_hbox_node(); \ |
| 11070 |
if ((I)&b100) { List l; @+hteg_param_list(&l); @+} @+ else HTEG_REF(param_kind); |
| 11071 |
@ |
| 11072 |
|
| 11073 |
@<cases to skip content@>= |
| 11074 |
@t\1\kern1em@>case TAG(math_kind,b000): HTEG_MATH(b000); @+ break; |
| 11075 |
case TAG(math_kind,b001): HTEG_MATH(b001); @+ break; |
| 11076 |
case TAG(math_kind,b010): HTEG_MATH(b010); @+ break; |
| 11077 |
case TAG(math_kind,b100): HTEG_MATH(b100); @+ break; |
| 11078 |
case TAG(math_kind,b101): HTEG_MATH(b101); @+ break; |
| 11079 |
case TAG(math_kind,b110): HTEG_MATH(b110); @+ break; |
| 11080 |
case TAG(math_kind,b011): |
| 11081 |
case TAG(math_kind,b111): @+ break; |
| 11082 |
@ |
| 11083 |
|
| 11084 |
\subsection{Images}\index{image} |
| 11085 |
\noindent |
| 11086 |
@<skip macros@>= |
| 11087 |
#define @[HTEG_IMAGE(I)@] @/\ |
| 11088 |
{ Image x={0}; List d; hteg_list(&d);\ |
| 11089 |
if ((I)&b100) {\ |
| 11090 |
if ((I)==b111) {hteg_xdimen_node(&x.h);hteg_xdimen_node(&x.w);}\ |
| 11091 |
else if ((I)==b110) {hteg_xdimen_node(&x.w);x.hr=HTEG8;}\ |
| 11092 |
else if ((I)==b101) {hteg_xdimen_node(&x.h);x.wr=HTEG8;}\ |
| 11093 |
else {x.hr=HTEG8;x.wr=HTEG8;}\ |
| 11094 |
x.a=hteg_float32();}\ |
| 11095 |
else if((I)==b011) {HTEG32(x.h.w);HTEG32(x.w.w);} \ |
| 11096 |
else if((I)==b010) { HTEG32(x.w.w); x.a=hteg_float32();}\ |
| 11097 |
else if((I)==b001){ HTEG32(x.h.w); x.a=hteg_float32();}\ |
| 11098 |
HTEG16(x.n);} |
| 11099 |
@ |
| 11100 |
|
| 11101 |
@<cases to skip content@>= |
| 11102 |
@t\1\kern1em@> |
| 11103 |
case TAG(image_kind,b001): @+ HTEG_IMAGE(b001);@+break; |
| 11104 |
case TAG(image_kind,b010): @+ HTEG_IMAGE(b010);@+break; |
| 11105 |
case TAG(image_kind,b011): @+ HTEG_IMAGE(b011);@+break; |
| 11106 |
case TAG(image_kind,b100): @+ HTEG_IMAGE(b100);@+break; |
| 11107 |
case TAG(image_kind,b101): @+ HTEG_IMAGE(b101);@+break; |
| 11108 |
case TAG(image_kind,b110): @+ HTEG_IMAGE(b110);@+break; |
| 11109 |
case TAG(image_kind,b111): @+ HTEG_IMAGE(b111);@+break; |
| 11110 |
@ |
| 11111 |
|
| 11112 |
\subsection{Links and Labels} |
| 11113 |
\noindent |
| 11114 |
@<skip macros@>= |
| 11115 |
#define @[HTEG_LINK(I)@] @/\ |
| 11116 |
{ uint16_t n; if (I&b100) n=HTEG8; if (I&b001) HTEG16(n);@+ else n=HTEG8; @+} |
| 11117 |
@ |
| 11118 |
|
| 11119 |
@<cases to skip content@>= |
| 11120 |
@t\1\kern1em@>case TAG(link_kind,b000): @+ HTEG_LINK(b000); @+break; |
| 11121 |
case TAG(link_kind,b001): @+ HTEG_LINK(b001); @+break; |
| 11122 |
case TAG(link_kind,b010): @+ HTEG_LINK(b010); @+break; |
| 11123 |
case TAG(link_kind,b011): @+ HTEG_LINK(b011); @+break; |
| 11124 |
case TAG(link_kind,b100): @+ HTEG_LINK(b100); @+break; |
| 11125 |
case TAG(link_kind,b101): @+ HTEG_LINK(b101); @+break; |
| 11126 |
case TAG(link_kind,b110): @+ HTEG_LINK(b110); @+break; |
| 11127 |
case TAG(link_kind,b111): @+ HTEG_LINK(b111); @+break; |
| 11128 |
@ |
| 11129 |
|
| 11130 |
\subsection{Colors} |
| 11131 |
\noindent |
| 11132 |
@<cases to skip content@>= |
| 11133 |
@t\1\kern1em@> |
| 11134 |
case TAG(color_kind,b000): @+ (void)HTEG8; @+break; |
| 11135 |
@ |
| 11136 |
|
| 11137 |
\subsection{Plain Lists, Texts, and Parameter Lists}\index{list} |
| 11138 |
|
| 11139 |
\noindent |
| 11140 |
@<shared skip functions@>= |
| 11141 |
|
| 11142 |
uint32_t hteg_list_size(Info info) |
| 11143 |
{ uint32_t n=0; |
| 11144 |
info=info&0x3; |
| 11145 |
if (info==0) return 0; |
| 11146 |
else if (info==1) n=HTEG8; |
| 11147 |
else if (info==2) HTEG16(n); |
| 11148 |
else if (info==3) HTEG32(n); |
| 11149 |
else QUIT("List info %d must be 0, 1, 2, or 3",info); |
| 11150 |
return n; |
| 11151 |
} |
| 11152 |
@ |
| 11153 |
|
| 11154 |
@<skip functions@>= |
| 11155 |
static void hteg_size_boundary(Info info) |
| 11156 |
{ uint32_t n; |
| 11157 |
info=info&0x3; |
| 11158 |
if (info==0) return; |
| 11159 |
n=HTEG8; |
| 11160 |
if (n!=0x100-info) QUIT(@["List size boundary byte 0x%x does not match info value %d at " SIZE_F@], |
| 11161 |
n, info,hpos-hstart); |
| 11162 |
} |
| 11163 |
|
| 11164 |
static void hteg_list(List *l) |
| 11165 |
{ @<skip the end byte |z|@>@, |
| 11166 |
@+if (KIND(z)!=list_kind && KIND(z)!=param_kind) @/ |
| 11167 |
QUIT("List expected at 0x%x", (uint32_t)(hpos-hstart)); |
| 11168 |
else if ((INFO(z)&0x3)==0) {HBACK(1); l->s=0;@+} |
| 11169 |
else |
| 11170 |
{ uint32_t s; |
| 11171 |
l->t=z; |
| 11172 |
l->s=hteg_list_size(INFO(z)); |
| 11173 |
hteg_size_boundary(INFO(z)); |
| 11174 |
hpos=hpos-l->s; |
| 11175 |
l->p=hpos-hstart; |
| 11176 |
hteg_size_boundary(INFO(z)); |
| 11177 |
s=hteg_list_size(INFO(z)); |
| 11178 |
if (s!=l->s) QUIT(@["List sizes at " SIZE_F " and 0x%x do not match 0x%x != 0x%x"@], |
| 11179 |
hpos-hstart,node_pos-1,s,l->s); |
| 11180 |
} |
| 11181 |
@<skip and check the start byte |a|@>@; |
| 11182 |
} |
| 11183 |
|
| 11184 |
static void hteg_param_list(List *l) |
| 11185 |
{ @+if (KIND(*(hpos-1))!=param_kind) return; |
| 11186 |
hteg_list(l); |
| 11187 |
} |
| 11188 |
|
| 11189 |
|
| 11190 |
@ |
| 11191 |
|
| 11192 |
\subsection{Adjustments}\index{adjustment} |
| 11193 |
\noindent |
| 11194 |
@<cases to skip content@>= |
| 11195 |
@t\1\kern1em@>case TAG(adjust_kind,b001): @+ { List l; @+hteg_list(&l);@+ } @+ break; |
| 11196 |
@ |
| 11197 |
|
| 11198 |
\subsection{Tables}\index{table} |
| 11199 |
\noindent |
| 11200 |
@<skip macros@>= |
| 11201 |
#define @[HTEG_TABLE(I)@] \ |
| 11202 |
{@+ List l; @+ hteg_list(&l);@+}\ |
| 11203 |
{@+ List l; @+ hteg_list(&l);@+}\ |
| 11204 |
if ((I)&b100) {Xdimen x;@+ hteg_xdimen_node(&x);@+} else HTEG_REF(xdimen_kind)@; |
| 11205 |
@ |
| 11206 |
|
| 11207 |
@<cases to skip content@>= |
| 11208 |
@t\1\kern1em@>case TAG(table_kind,b000): @+ HTEG_TABLE(b000); @+ break; |
| 11209 |
case TAG(table_kind,b001): @+ HTEG_TABLE(b001); @+ break; |
| 11210 |
case TAG(table_kind,b010): @+ HTEG_TABLE(b010); @+ break; |
| 11211 |
case TAG(table_kind,b011): @+ HTEG_TABLE(b011); @+ break; |
| 11212 |
case TAG(table_kind,b100): @+ HTEG_TABLE(b100); @+ break; |
| 11213 |
case TAG(table_kind,b101): @+ HTEG_TABLE(b101); @+ break; |
| 11214 |
case TAG(table_kind,b110): @+ HTEG_TABLE(b110); @+ break; |
| 11215 |
case TAG(table_kind,b111): @+ HTEG_TABLE(b111); @+ break;@# |
| 11216 |
|
| 11217 |
case TAG(item_kind,b000): @+{@+ List l; @+hteg_list(&l);@+ } @+ break; |
| 11218 |
case TAG(item_kind,b001): hteg_content_node(); @+ break; |
| 11219 |
case TAG(item_kind,b010): hteg_content_node(); @+ break; |
| 11220 |
case TAG(item_kind,b011): hteg_content_node(); @+ break; |
| 11221 |
case TAG(item_kind,b100): hteg_content_node(); @+ break; |
| 11222 |
case TAG(item_kind,b101): hteg_content_node(); @+ break; |
| 11223 |
case TAG(item_kind,b110): hteg_content_node(); @+ break; |
| 11224 |
case TAG(item_kind,b111): hteg_content_node(); @+{uint8_t n;@+ n=HTEG8;@+}@+ break; |
| 11225 |
@ |
| 11226 |
|
| 11227 |
|
| 11228 |
|
| 11229 |
\subsection{Stream Nodes}\index{stream} |
| 11230 |
@<skip macros@>= |
| 11231 |
#define @[HTEG_STREAM(I)@] @/\ |
| 11232 |
{ List l; @+hteg_list(&l); @+}\ |
| 11233 |
if ((I)&b010) { List l; @+hteg_param_list(&l); @+} @+ else HTEG_REF(param_kind);\ |
| 11234 |
HTEG_REF(stream_kind); |
| 11235 |
@ |
| 11236 |
|
| 11237 |
@<cases to skip content@>= |
| 11238 |
@t\1\kern1em@>case TAG(stream_kind,b000): HTEG_STREAM(b000); @+ break; |
| 11239 |
case TAG(stream_kind,b010): HTEG_STREAM(b010); @+ break; |
| 11240 |
@ |
| 11241 |
|
| 11242 |
|
| 11243 |
|
| 11244 |
\subsection{References}\index{reference} |
| 11245 |
\noindent |
| 11246 |
@<skip macros@>= |
| 11247 |
#define @[HTEG_REF(K)@] do@+{uint8_t n; @+ n=HTEG8;@+} @+ while (false) |
| 11248 |
@ |
| 11249 |
|
| 11250 |
@<cases to skip content@>= |
| 11251 |
@t\1\kern1em@>case TAG(penalty_kind,0): HTEG_REF(penalty_kind); @+break; |
| 11252 |
case TAG(kern_kind,b000): HTEG_REF(dimen_kind); @+break; |
| 11253 |
case TAG(kern_kind,b100): HTEG_REF(dimen_kind); @+break; |
| 11254 |
case TAG(kern_kind,b001): HTEG_REF(xdimen_kind); @+break; |
| 11255 |
case TAG(kern_kind,b101): HTEG_REF(xdimen_kind); @+break; |
| 11256 |
case TAG(ligature_kind,0): HTEG_REF(ligature_kind); @+break; |
| 11257 |
case TAG(disc_kind,0): HTEG_REF(disc_kind); @+break; |
| 11258 |
case TAG(glue_kind,0): HTEG_REF(glue_kind); @+break; |
| 11259 |
case TAG(language_kind,0): HTEG_REF(language_kind); @+break; |
| 11260 |
case TAG(rule_kind,0): HTEG_REF(rule_kind); @+break; |
| 11261 |
case TAG(image_kind,0): HTEG_REF(image_kind); @+break; |
| 11262 |
case TAG(leaders_kind,0): HTEG_REF(leaders_kind); @+break; |
| 11263 |
case TAG(baseline_kind,0): HTEG_REF(baseline_kind); @+break; |
| 11264 |
@ |
| 11265 |
|
| 11266 |
\subsection{Unknown nodes} |
| 11267 |
@<skip functions@>= |
| 11268 |
static int hteg_unknown(Tag z) |
| 11269 |
{ int b, n; |
| 11270 |
int8_t s; |
| 11271 |
s = hnode_size[z]; |
| 11272 |
DBG(DBGTAGS,"Trying unknown tag 0x%x at 0x%x\n",z,(uint32_t)(hpos-hstart-1)); |
| 11273 |
if (s==0) return 0; |
| 11274 |
b=NODE_HEAD(s); n=NODE_TAIL(s); |
| 11275 |
DBG(DBGTAGS,"Trying unknown node size %d %d\n",b,n); |
| 11276 |
|
| 11277 |
while (n>0) { |
| 11278 |
z=*(hpos-1); |
| 11279 |
if (KIND(z)==xdimen_kind) { Xdimen x; hteg_xdimen_node(&x); } |
| 11280 |
else if (KIND(z)==param_kind) { List l; @+hteg_param_list(&l); @+} |
| 11281 |
else if (KIND(z)<=list_kind) { List l; @+hteg_list(&l); @+} |
| 11282 |
else hteg_content_node(); |
| 11283 |
n--; } |
| 11284 |
while (b>0) { z=HTEG8; b--;} |
| 11285 |
return 1; |
| 11286 |
} |
| 11287 |
@ |
| 11288 |
|
| 11289 |
|
| 11290 |
\section{Code and Header Files}\index{code file}\index{header file} |
| 11291 |
|
| 11292 |
\subsection{{\tt basetypes.h}} |
| 11293 |
To define basic types in a portable way, we create an include file. |
| 11294 |
The macro |_MSC_VER| (Microsoft Visual C Version)\index{Microsoft Visual C} |
| 11295 |
is defined only if using the respective compiler. |
| 11296 |
\index{false+\\{false}}\index{true+\\{true}}\index{bool+\&{bool}} |
| 11297 |
@(hibasetypes.h@>= |
| 11298 |
#ifndef __BASETYPES_H__ |
| 11299 |
#define __BASETYPES_H__ |
| 11300 |
#include <stdlib.h> |
| 11301 |
#include <stdio.h> |
| 11302 |
#ifndef _STDLIB_H |
| 11303 |
#define _STDLIB_H |
| 11304 |
#endif |
| 11305 |
#ifdef _MSC_VER |
| 11306 |
#include <windows.h> |
| 11307 |
#define uint8_t UINT8 |
| 11308 |
#define uint16_t UINT16 |
| 11309 |
#define uint32_t UINT32 |
| 11310 |
#define uint64_t UINT64 |
| 11311 |
#define int8_t INT8 |
| 11312 |
#define int16_t INT16 |
| 11313 |
#define int32_t INT32 |
| 11314 |
#define bool BOOL |
| 11315 |
#define true (0==0) |
| 11316 |
#define false (!true) |
| 11317 |
#define __SIZEOF_FLOAT__ 4 |
| 11318 |
#define __SIZEOF_DOUBLE__ 8 |
| 11319 |
#define PRIx64 "I64x" |
| 11320 |
#pragma @[warning( disable : @[4244@]@t @> @[4996@]@t @> @[4127@])@] |
| 11321 |
#else |
| 11322 |
#include <stdint.h> |
| 11323 |
#include <stdbool.h> |
| 11324 |
#include <inttypes.h> |
| 11325 |
#include <unistd.h> |
| 11326 |
#ifdef WIN32 |
| 11327 |
#include <io.h> |
| 11328 |
#endif |
| 11329 |
#endif |
| 11330 |
typedef float float32_t; |
| 11331 |
typedef double float64_t; |
| 11332 |
#if __SIZEOF_FLOAT__!=4 |
| 11333 |
#error @=float32 type must have size 4@> |
| 11334 |
#endif |
| 11335 |
#if __SIZEOF_DOUBLE__!=8 |
| 11336 |
#error @=float64 type must have size 8@> |
| 11337 |
#endif |
| 11338 |
#define HINT_VERSION 2 |
| 11339 |
#define HINT_MINOR_VERSION 2 |
| 11340 |
#define AS_STR(X) #X |
| 11341 |
#define VERSION_AS_STR(X,Y) AS_STR(X) "." AS_STR(Y) |
| 11342 |
#define HINT_VERSION_STRING VERSION_AS_STR(HINT_VERSION, HINT_MINOR_VERSION) |
| 11343 |
#endif |
| 11344 |
@ |
| 11345 |
|
| 11346 |
\subsection{{\tt format.h}}\index{format.h+{\tt format.h}} |
| 11347 |
The \.{format.h} file contains definitions of types, macros, variables and functions |
| 11348 |
that are needed in other compilation units. |
| 11349 |
|
| 11350 |
@(hiformat.h@>= |
| 11351 |
#ifndef _HFORMAT_H_ |
| 11352 |
#define _HFORMAT_H_ |
| 11353 |
@<debug macros@>@; |
| 11354 |
@<debug constants@>@; |
| 11355 |
@<hint macros@>@; |
| 11356 |
@<hint basic types@>@; |
| 11357 |
@<default names@>@; |
| 11358 |
|
| 11359 |
extern const char *content_name[32]; |
| 11360 |
extern const char *definition_name[32]; |
| 11361 |
extern unsigned int debugflags; |
| 11362 |
extern FILE *hlog; |
| 11363 |
extern int max_fixed[32], max_default[32], max_ref[32], max_outline; |
| 11364 |
extern int32_t int_defaults[MAX_INT_DEFAULT+1]; |
| 11365 |
extern Dimen dimen_defaults[MAX_DIMEN_DEFAULT+1]; |
| 11366 |
extern Xdimen xdimen_defaults[MAX_XDIMEN_DEFAULT+1]; |
| 11367 |
extern Glue glue_defaults[MAX_GLUE_DEFAULT+1]; |
| 11368 |
extern Baseline baseline_defaults[MAX_BASELINE_DEFAULT+1]; |
| 11369 |
extern Label label_defaults[MAX_LABEL_DEFAULT+1]; |
| 11370 |
extern ColorSet color_defaults[MAX_COLOR_DEFAULT+1]; |
| 11371 |
extern signed char hnode_size[0x100]; |
| 11372 |
extern uint8_t content_known[32]; |
| 11373 |
|
| 11374 |
#endif |
| 11375 |
@ |
| 11376 |
\subsection{{\tt tables.c}}\index{tables.c+{\tt tables.c}}\index{mktables.c+{\tt mktables.c}} |
| 11377 |
For maximum flexibility and efficiency, the file {\tt tables.c} |
| 11378 |
is generated by a \CEE\ program. |
| 11379 |
Here is the |main| program of {\tt mktables}: |
| 11380 |
|
| 11381 |
@(himktables.c@>= |
| 11382 |
#include "hibasetypes.h" |
| 11383 |
#include "hiformat.h" |
| 11384 |
@<skip macros@>@; |
| 11385 |
|
| 11386 |
int max_fixed[32], max_default[32]; |
| 11387 |
|
| 11388 |
int32_t int_defaults[MAX_INT_DEFAULT+1]={0}; |
| 11389 |
Dimen dimen_defaults[MAX_DIMEN_DEFAULT+1]={0}; |
| 11390 |
Xdimen xdimen_defaults[MAX_XDIMEN_DEFAULT+1]={{0}}; |
| 11391 |
Glue glue_defaults[MAX_GLUE_DEFAULT+1]={{{0}}}; |
| 11392 |
Baseline baseline_defaults[MAX_BASELINE_DEFAULT+1]={{{{0}}}}; |
| 11393 |
|
| 11394 |
signed char hnode_size[0x100]={0}; |
| 11395 |
uint8_t content_known[32]={0}; |
| 11396 |
@<define |content_name| and |definition_name|@>@; |
| 11397 |
int main(void) |
| 11398 |
{ Kind k; |
| 11399 |
int i; |
| 11400 |
|
| 11401 |
|
| 11402 |
printf("#include \"hibasetypes.h\"\n"@/ |
| 11403 |
"#include \"hiformat.h\"\n\n");@/ |
| 11404 |
|
| 11405 |
@<print |content_name| and |definition_name|@>@; |
| 11406 |
|
| 11407 |
printf("int max_outline=-1;\n\n"); |
| 11408 |
|
| 11409 |
@<take care of variables without defaults@>@; |
| 11410 |
@<define |int_defaults|@>@; |
| 11411 |
@<define |dimen_defaults|@>@; |
| 11412 |
@<define |glue_defaults|@>@; |
| 11413 |
@<define |xdimen_defaults|@>@; |
| 11414 |
@<define |baseline_defaults|@>@; |
| 11415 |
@<define page defaults@>@; |
| 11416 |
@<define stream defaults@>@; |
| 11417 |
@<define range defaults@>@; |
| 11418 |
@<define |label_defaults|@>@; |
| 11419 |
@<define |color_defaults|@>@; |
| 11420 |
@<print defaults@>@; |
| 11421 |
|
| 11422 |
@<initialize the |hnode_size| array@>@; |
| 11423 |
@<print the |hnode_size| variable@>@; |
| 11424 |
@<print the |content_known| variable@>@; |
| 11425 |
return 0; |
| 11426 |
} |
| 11427 |
@ |
| 11428 |
|
| 11429 |
The following code prints the arrays containing the default values. |
| 11430 |
|
| 11431 |
@<print defaults@>= |
| 11432 |
printf("int max_fixed[32]= {"); |
| 11433 |
for (k=0; k<32; k++)@/ |
| 11434 |
{ printf("%d",max_fixed[k]);@+ |
| 11435 |
if (k<31) printf(", ");@+ |
| 11436 |
} |
| 11437 |
printf("};\n\n");@# |
| 11438 |
printf("int max_default[32]= {"); |
| 11439 |
for (k=0; k<32; k++)@/ |
| 11440 |
{ printf("%d",max_default[k]);@+ |
| 11441 |
if (k<31) printf(", ");@+ |
| 11442 |
} |
| 11443 |
printf("};\n\n"); |
| 11444 |
printf("int max_ref[32]= {"); |
| 11445 |
for (k=0; k<32; k++)@/ |
| 11446 |
{ printf("%d",max_default[k]);@+ |
| 11447 |
if (k<31) printf(", ");@+ |
| 11448 |
} |
| 11449 |
printf("};\n\n"); |
| 11450 |
@ |
| 11451 |
|
| 11452 |
|
| 11453 |
\subsection{{\tt get.h}}\index{get.h+{\tt get.h}} |
| 11454 |
The \.{get.h} file contains function prototypes for all the functions |
| 11455 |
that read the short format. |
| 11456 |
|
| 11457 |
@(higet.h@>= |
| 11458 |
@<hint types@>@; |
| 11459 |
@<directory entry type@>@; |
| 11460 |
@<shared get macros@>@; |
| 11461 |
|
| 11462 |
extern Entry *dir; |
| 11463 |
extern uint16_t section_no, max_section_no; |
| 11464 |
extern uint8_t *hpos, *hstart, *hend, *hpos0; |
| 11465 |
extern uint64_t hin_size, hin_time; |
| 11466 |
extern uint8_t *hin_addr; |
| 11467 |
|
| 11468 |
extern Label *labels; |
| 11469 |
extern char *hin_name; |
| 11470 |
extern bool hget_map(void); |
| 11471 |
extern void hget_unmap(void); |
| 11472 |
|
| 11473 |
extern void new_directory(uint32_t entries); |
| 11474 |
extern void hset_entry(Entry *e, uint16_t i, @|uint32_t size, uint32_t xsize, char *file_name); |
| 11475 |
|
| 11476 |
extern void hget_banner(void); |
| 11477 |
extern void hget_section(uint16_t n); |
| 11478 |
extern void hget_entry(Entry *e); |
| 11479 |
extern void hget_directory(void); |
| 11480 |
extern void hclear_dir(void); |
| 11481 |
extern bool hcheck_banner(char *magic); |
| 11482 |
extern int max_range; |
| 11483 |
|
| 11484 |
extern void hget_max_definitions(void); |
| 11485 |
extern uint32_t hget_utf8(void); |
| 11486 |
extern void hget_size_boundary(Info info); |
| 11487 |
extern uint32_t hget_list_size(Info info); |
| 11488 |
extern void hget_list(List *l); |
| 11489 |
extern float32_t hget_float32(void); |
| 11490 |
extern void hff_hpos(void); |
| 11491 |
extern uint32_t hff_list_pos, hff_list_size; |
| 11492 |
extern Tag hff_tag; |
| 11493 |
extern float32_t hteg_float32(void); |
| 11494 |
extern uint32_t hteg_list_size(Info info); |
| 11495 |
|
| 11496 |
/* seems like these are declared static */ |
| 11497 |
#if 0 |
| 11498 |
extern void hteg_list(List *l); |
| 11499 |
|
| 11500 |
extern void hteg_size_boundary(Info info); |
| 11501 |
|
| 11502 |
#endif |
| 11503 |
|
| 11504 |
@ |
| 11505 |
|
| 11506 |
|
| 11507 |
|
| 11508 |
\subsection{{\tt get.c}}\index{get.c+{\tt get.c}} |
| 11509 |
@(higet.c@>= |
| 11510 |
#include "hibasetypes.h" |
| 11511 |
#include <string.h> |
| 11512 |
#include <math.h> |
| 11513 |
#include <zlib.h> |
| 11514 |
#include <sys/types.h> |
| 11515 |
#include <sys/stat.h> |
| 11516 |
#include <fcntl.h> |
| 11517 |
|
| 11518 |
#include "hierror.h" |
| 11519 |
#include "hiformat.h" |
| 11520 |
#include "higet.h" |
| 11521 |
|
| 11522 |
@<common variables@>@; |
| 11523 |
|
| 11524 |
@<map functions@>@; |
| 11525 |
@<function to check the banner@>@; |
| 11526 |
@<directory functions@>@; |
| 11527 |
|
| 11528 |
@<get file functions@>@; |
| 11529 |
@<shared get functions@>@; |
| 11530 |
@<shared skip functions@>@; |
| 11531 |
@ |
| 11532 |
|
| 11533 |
\subsection{{\tt put.h}}\index{put.h+{\tt put.h}} |
| 11534 |
The \.{put.h} file contains function prototypes for all the functions |
| 11535 |
that write the short format. |
| 11536 |
|
| 11537 |
|
| 11538 |
@(hiput.h@>= |
| 11539 |
@<put macros@>@; |
| 11540 |
@<hint macros@>@; |
| 11541 |
@<hint types@>@; |
| 11542 |
@<directory entry type@>@; |
| 11543 |
extern Entry *dir; |
| 11544 |
extern uint16_t section_no, max_section_no; |
| 11545 |
extern uint8_t *hpos, *hstart, *hend, *hpos0; |
| 11546 |
extern int next_range; |
| 11547 |
extern RangePos *range_pos; |
| 11548 |
extern int next_range, max_range; |
| 11549 |
extern int *page_on; |
| 11550 |
extern Label *labels; |
| 11551 |
extern int first_label; |
| 11552 |
extern int max_outline; |
| 11553 |
extern Outline *outlines; |
| 11554 |
|
| 11555 |
|
| 11556 |
extern FILE *hout; |
| 11557 |
extern void new_directory(uint32_t entries); |
| 11558 |
extern void new_output_buffers(void); |
| 11559 |
|
| 11560 |
/* declarations for the parser */ |
| 11561 |
extern void hput_definitions_start(void); |
| 11562 |
extern void hput_definitions_end(void); |
| 11563 |
extern void hput_content_start(void); |
| 11564 |
extern void hput_content_end(void); |
| 11565 |
|
| 11566 |
extern void hset_label(int n,int w); |
| 11567 |
extern Tag hput_link(int n, int c, int on); |
| 11568 |
extern void hset_outline(int m, int r, int d, uint32_t p); |
| 11569 |
extern void hput_label_defs(void); |
| 11570 |
|
| 11571 |
extern void hput_tags(uint32_t pos, Tag tag); |
| 11572 |
extern Tag hput_glyph(Glyph *g); |
| 11573 |
extern Tag hput_xdimen(Xdimen *x); |
| 11574 |
extern Tag hput_int(int32_t p); |
| 11575 |
extern Tag hput_language(uint8_t n); |
| 11576 |
extern Tag hput_rule(Rule *r); |
| 11577 |
extern Tag hput_glue(Glue *g); |
| 11578 |
extern Tag hput_list(uint32_t size_pos, List *y); |
| 11579 |
extern uint8_t hsize_bytes(uint32_t n); |
| 11580 |
extern void hput_txt_cc(uint32_t c); |
| 11581 |
extern void hput_txt_font(uint8_t f); |
| 11582 |
extern void hput_txt_global(Ref *d); |
| 11583 |
extern void hput_txt_local(uint8_t n); |
| 11584 |
extern Info hput_box_dimen(Dimen h, Dimen d, Dimen w); |
| 11585 |
extern Info hput_box_shift(Dimen a); |
| 11586 |
extern Info hput_box_glue_set(int8_t s, float32_t r, Order o); |
| 11587 |
extern void hput_stretch(Stretch *s); |
| 11588 |
extern Tag hput_kern(Kern *k); |
| 11589 |
extern void hput_utf8(uint32_t c); |
| 11590 |
extern Tag hput_ligature(Lig *l); |
| 11591 |
extern Tag hput_disc(Disc *h); |
| 11592 |
extern Info hput_span_count(uint32_t n); |
| 11593 |
extern void hextract_image_dimens(int n, double *a, Dimen *w, Dimen *h); |
| 11594 |
extern Info hput_image_spec(uint32_t n, float32_t a, uint32_t wr, Xdimen *w, uint32_t hr, Xdimen *h); |
| 11595 |
extern int colors_i; |
| 11596 |
extern ColorSet colors_0, colors_n; |
| 11597 |
extern void color_init(void); |
| 11598 |
extern void hput_color_def(uint32_t pos, int n); |
| 11599 |
extern void hput_string(char *str); |
| 11600 |
extern void hput_range(uint8_t pg, bool on); |
| 11601 |
extern void hput_max_definitions(void); |
| 11602 |
extern Tag hput_dimen(Dimen d); |
| 11603 |
extern Tag hput_font_head(uint8_t f, char *n, Dimen s,@| int m, uint16_t y); |
| 11604 |
extern void hput_range_defs(void); |
| 11605 |
extern void hput_xdimen_node(Xdimen *x); |
| 11606 |
extern void hput_directory(void); |
| 11607 |
extern size_t hput_hint(char * str); |
| 11608 |
extern void hput_list_size(uint32_t n, int i); |
| 11609 |
extern uint32_t hput_unknown_def(uint32_t t, uint32_t b, uint32_t n); |
| 11610 |
extern Tag hput_unknown(uint32_t pos,uint32_t t, uint32_t b, uint32_t n); |
| 11611 |
extern int hcompress_depth(int n, int c); |
| 11612 |
@ |
| 11613 |
|
| 11614 |
|
| 11615 |
\subsection{{\tt put.c}}\label{writeshort}\index{put.c+{\tt put.c}} |
| 11616 |
\noindent |
| 11617 |
@(hiput.c@>= |
| 11618 |
#include "hibasetypes.h" |
| 11619 |
#include <string.h> |
| 11620 |
#include <ctype.h> |
| 11621 |
#include <math.h> |
| 11622 |
#include <sys/types.h> |
| 11623 |
#include <sys/stat.h> |
| 11624 |
#include <zlib.h> |
| 11625 |
#include "hierror.h" |
| 11626 |
#include "hiformat.h" |
| 11627 |
#include "hiput.h" |
| 11628 |
|
| 11629 |
@<common variables@>@; |
| 11630 |
@<shared put variables@>@; |
| 11631 |
@<directory functions@>@; |
| 11632 |
@<function to write the banner@>@; |
| 11633 |
@<put functions@>@; |
| 11634 |
@ |
| 11635 |
|
| 11636 |
\subsection{{\tt lexer.l}}\index{lexer.l+{\tt lexer.l}}\index{scanning} |
| 11637 |
The definitions for lex are collected in the file {\tt lexer.l} |
| 11638 |
|
| 11639 |
@(hilexer.l@>= |
| 11640 |
%{ |
| 11641 |
#include "hibasetypes.h" |
| 11642 |
#include "hierror.h" |
| 11643 |
#include "hiformat.h" |
| 11644 |
#include "hiput.h" |
| 11645 |
|
| 11646 |
@<enable bison debugging@>@; |
| 11647 |
#include "hiparser.h" |
| 11648 |
|
| 11649 |
@<scanning macros@>@;@+ |
| 11650 |
@<scanning functions@>@; |
| 11651 |
int yywrap (void )@+{ return 1;@+} |
| 11652 |
#ifdef _MSC_VER |
| 11653 |
#pragma warning( disable : 4267) |
| 11654 |
#endif |
| 11655 |
%} |
| 11656 |
|
| 11657 |
%option yylineno stack batch never-interactive |
| 11658 |
%option debug |
| 11659 |
%option nounistd nounput noinput noyy_top_state |
| 11660 |
|
| 11661 |
@<scanning definitions@>@/ |
| 11662 |
|
| 11663 |
%% |
| 11664 |
|
| 11665 |
@<scanning rules@>@/ |
| 11666 |
::@=[a-z]+@> :< QUIT("Unexpected keyword '%s' in line %d",@|yytext,yylineno); >: |
| 11667 |
::@=.@> :< QUIT("Unexpected character '%c' (0x%02X) in line %d",@|yytext[0]>' '?yytext[0]:' ',yytext[0],yylineno); >: |
| 11668 |
|
| 11669 |
%% |
| 11670 |
@ |
| 11671 |
|
| 11672 |
|
| 11673 |
|
| 11674 |
\subsection{{\tt parser.y}}\index{parser.y+{\tt parser.y}}\index{parsing} |
| 11675 |
|
| 11676 |
The grammar rules for bison are collected in the file {\tt parser.y}. |
| 11677 |
% for the option %token-table use the command line parameter -k |
| 11678 |
|
| 11679 |
|
| 11680 |
@(hiparser.y@>= |
| 11681 |
%{ |
| 11682 |
#include "hibasetypes.h" |
| 11683 |
#include <string.h> |
| 11684 |
#include <math.h> |
| 11685 |
#include "hierror.h" |
| 11686 |
#include "hiformat.h" |
| 11687 |
#include "hiput.h" |
| 11688 |
extern char **hfont_name; /* in common variables */ |
| 11689 |
|
| 11690 |
@<definition checks@>@; |
| 11691 |
|
| 11692 |
extern void hset_entry(Entry *e, uint16_t i, @|uint32_t size, |
| 11693 |
uint32_t xsize, char *file_name); |
| 11694 |
|
| 11695 |
@<enable bison debugging@>@; |
| 11696 |
extern int yylex(void); |
| 11697 |
|
| 11698 |
@<parsing functions@>@; |
| 11699 |
|
| 11700 |
%} |
| 11701 |
|
| 11702 |
|
| 11703 |
@t{\label{union}\index{union}\index{parsing}}@> |
| 11704 |
|
| 11705 |
|
| 11706 |
%union {uint32_t u; @+ int32_t i; @+ char *s; @+ float64_t f; @+ Glyph c; |
| 11707 |
@+ Dimen @+d; Stretch st; @+ Xdimen xd; @+ Kern kt; |
| 11708 |
@+ Rule r; @+ Glue g; @+ @+ Image x; |
| 11709 |
@+ List l; @+ Box h; @+ Disc dc; @+ Lig lg; |
| 11710 |
@+ Ref rf; @+ Info info; @+ Order o; bool@+ b; |
| 11711 |
} |
| 11712 |
|
| 11713 |
@t{}@> |
| 11714 |
|
| 11715 |
%define parse.error verbose |
| 11716 |
%start hint |
| 11717 |
@t@> |
| 11718 |
@<symbols@>@/ |
| 11719 |
%% |
| 11720 |
@<parsing rules@>@; |
| 11721 |
%% |
| 11722 |
@ |
| 11723 |
|
| 11724 |
\subsection{{\tt shrink.c}}\index{shrink.c+{\tt shrink.c}} |
| 11725 |
|
| 11726 |
\.{shrink} is a \CEE\ program translating a \HINT\ file in long format into a \HINT\ file in short format. |
| 11727 |
|
| 11728 |
@(hishrink.c@>= |
| 11729 |
#include "hibasetypes.h" |
| 11730 |
#include <math.h> |
| 11731 |
#include <string.h> |
| 11732 |
#include <ctype.h> |
| 11733 |
#include <sys/types.h> |
| 11734 |
#include <sys/stat.h> |
| 11735 |
#ifdef WIN32 |
| 11736 |
#include <direct.h> |
| 11737 |
#endif |
| 11738 |
#include <zlib.h> |
| 11739 |
|
| 11740 |
#include "hierror.h" |
| 11741 |
#include "hiformat.h" |
| 11742 |
#include "hiput.h" |
| 11743 |
|
| 11744 |
@<enable bison debugging@>@; |
| 11745 |
#include "hiparser.h" |
| 11746 |
|
| 11747 |
extern void yyset_debug(int lex_debug); |
| 11748 |
extern int yylineno; |
| 11749 |
extern FILE *yyin, *yyout; |
| 11750 |
extern int yyparse(void); |
| 11751 |
|
| 11752 |
@<put macros@>@; |
| 11753 |
|
| 11754 |
@<common variables@>@; |
| 11755 |
@<shared put variables@>@; |
| 11756 |
@<function to check the banner@>@; |
| 11757 |
@<directory functions@>@; |
| 11758 |
@<function to write the banner@>@; |
| 11759 |
@<put functions@>@; |
| 11760 |
|
| 11761 |
#define SHRINK |
| 11762 |
#define DESCRIPTION "\nConvert a `long' ASCII HINT file into a `short' binary HINT file.\n" |
| 11763 |
|
| 11764 |
int main(int argc, char *argv[]) |
| 11765 |
{ @<local variables in |main|@>@; |
| 11766 |
in_ext=".hint"; |
| 11767 |
out_ext=".hnt"; |
| 11768 |
@<process the command line@>@; |
| 11769 |
|
| 11770 |
if (debugflags&DBGFLEX) yyset_debug(1); else yyset_debug(0); |
| 11771 |
#if YYDEBUG |
| 11772 |
if (debugflags&DBGBISON) yydebug=1; |
| 11773 |
else yydebug=0; |
| 11774 |
#endif |
| 11775 |
@<open the log file@>@; |
| 11776 |
@<open the input file@>@; |
| 11777 |
@<open the output file@>@; |
| 11778 |
|
| 11779 |
yyin=hin; |
| 11780 |
yyout=hlog; |
| 11781 |
@<read the banner@>@; |
| 11782 |
if (!hcheck_banner("HINT")) QUIT("Invalid banner"); |
| 11783 |
yylineno++; |
| 11784 |
DBG(DBGBISON|DBGFLEX,"Parsing Input\n"); |
| 11785 |
yyparse(); |
| 11786 |
|
| 11787 |
hput_directory(); |
| 11788 |
@<rewrite the file names of optional sections@>@; |
| 11789 |
hput_hint("created by shrink"); |
| 11790 |
|
| 11791 |
@<close the output file@>@; |
| 11792 |
@<close the input file@>@; |
| 11793 |
@<close the log file@>@; |
| 11794 |
return 0; |
| 11795 |
} |
| 11796 |
@ |
| 11797 |
|
| 11798 |
|
| 11799 |
|
| 11800 |
\subsection{{\tt stretch.c}}\label{stretchmain}\index{stretch.c+{\tt stretch.c}} |
| 11801 |
\.{stretch} is a \CEE\ program translating a \HINT\ file in short |
| 11802 |
format into a \HINT\ file in long format. |
| 11803 |
|
| 11804 |
@(histretch.c@>= |
| 11805 |
#include "hibasetypes.h" |
| 11806 |
#include <math.h> |
| 11807 |
#include <string.h> |
| 11808 |
#include <ctype.h> |
| 11809 |
#include <zlib.h> |
| 11810 |
#include <sys/types.h> |
| 11811 |
#include <sys/stat.h> |
| 11812 |
#ifdef WIN32 |
| 11813 |
#include <direct.h> |
| 11814 |
#endif |
| 11815 |
#include <fcntl.h> |
| 11816 |
#include "hierror.h" |
| 11817 |
#include "hiformat.h" |
| 11818 |
#include "higet.h" |
| 11819 |
|
| 11820 |
@<get macros@>@; |
| 11821 |
@<write macros@>@; |
| 11822 |
@<common variables@>@; |
| 11823 |
@<shared put variables@>@; |
| 11824 |
@<map functions@>@; |
| 11825 |
@<function to check the banner@>@; |
| 11826 |
@<function to write the banner@>@; |
| 11827 |
@<directory functions@>@; |
| 11828 |
|
| 11829 |
@<definition checks@>@; |
| 11830 |
@<get function declarations@>@; |
| 11831 |
@<write functions@>@; |
| 11832 |
@<get file functions@>@; |
| 11833 |
@<shared get functions@>@; |
| 11834 |
@<get functions@>@; |
| 11835 |
|
| 11836 |
#define STRETCH |
| 11837 |
#define DESCRIPTION "\nConvert a `short' binary HINT file into a `long' ASCII HINT file.\n" |
| 11838 |
|
| 11839 |
int main(int argc, char *argv[]) |
| 11840 |
{ @<local variables in |main|@>@; |
| 11841 |
|
| 11842 |
in_ext=".hnt"; |
| 11843 |
out_ext=".hint"; |
| 11844 |
@<process the command line@>@; |
| 11845 |
@<open the log file@>@; |
| 11846 |
@<open the output file@>@; |
| 11847 |
@<determine the |stem_name| from the output |file_name|@>@; |
| 11848 |
if (!hget_map()) QUIT("Unable to map the input file"); |
| 11849 |
hpos=hstart=hin_addr; |
| 11850 |
hend=hstart+hin_size; |
| 11851 |
hget_banner(); |
| 11852 |
if (!hcheck_banner("hint")) QUIT("Invalid banner"); |
| 11853 |
hput_banner("HINT","created by stretch"); |
| 11854 |
hget_directory(); |
| 11855 |
hwrite_directory(); |
| 11856 |
hget_definition_section(); |
| 11857 |
hwrite_content_section(); |
| 11858 |
hwrite_aux_files(); |
| 11859 |
hget_unmap(); |
| 11860 |
@<close the output file@>@; |
| 11861 |
DBG(DBGBASIC,"End of Program\n"); |
| 11862 |
@<close the log file@>@; |
| 11863 |
return 0; |
| 11864 |
} |
| 11865 |
@ |
| 11866 |
|
| 11867 |
In the above program, the get functions call the write functions |
| 11868 |
and the write functions call some get functions. This requires |
| 11869 |
function declarations to satisfy the define before use requirement |
| 11870 |
of \CEE. Some of the necessary function declarations are already |
| 11871 |
contained in {\tt get.h}. The remaining declarations are these: |
| 11872 |
|
| 11873 |
@<get function declarations@>= |
| 11874 |
extern void hget_xdimen_node(Xdimen *x); |
| 11875 |
extern void hget_def_node(void); |
| 11876 |
extern void hget_font_def(Info i, uint8_t f); |
| 11877 |
extern void hget_content_section(void); |
| 11878 |
extern Tag hget_content_node(void); |
| 11879 |
extern void hget_glue_node(void); |
| 11880 |
extern void hget_rule_node(void); |
| 11881 |
extern void hget_hbox_node(void); |
| 11882 |
extern void hget_vbox_node(void); |
| 11883 |
extern void hget_param_list(List *l); |
| 11884 |
extern int hget_txt(void); |
| 11885 |
extern int hget_unknown(Tag a); |
| 11886 |
@ |
| 11887 |
|
| 11888 |
|
| 11889 |
\subsection{{\tt skip.c}}\label{skip}\index{skip.c+{\tt skip.c}} |
| 11890 |
\.{skip} is a \CEE\ program reading the content section of a \HINT\ file in short format |
| 11891 |
backwards. |
| 11892 |
|
| 11893 |
@(hiskip.c@>= |
| 11894 |
#include "hibasetypes.h" |
| 11895 |
#include <math.h> |
| 11896 |
#include <string.h> |
| 11897 |
#include <ctype.h> |
| 11898 |
#include <zlib.h> |
| 11899 |
#include <sys/types.h> |
| 11900 |
#include <sys/stat.h> |
| 11901 |
#include <fcntl.h> |
| 11902 |
#include "hierror.h" |
| 11903 |
#include "hiformat.h" |
| 11904 |
#if 1 |
| 11905 |
#include "higet.h" |
| 11906 |
#else |
| 11907 |
@<hint types@>@; |
| 11908 |
@<directory entry type@>@; |
| 11909 |
@<shared get macros@>@; |
| 11910 |
#endif |
| 11911 |
|
| 11912 |
@<get macros@>@; |
| 11913 |
@<write macros@>@; |
| 11914 |
|
| 11915 |
@<common variables@>@; |
| 11916 |
@<shared put variables@>@; |
| 11917 |
@<map functions@>@; |
| 11918 |
@<function to check the banner@>@; |
| 11919 |
@<directory functions@>@; |
| 11920 |
@<shared get macros@>@; |
| 11921 |
@<get file functions@>@; |
| 11922 |
|
| 11923 |
@<skip macros@>@; |
| 11924 |
@<skip function declarations@>@; |
| 11925 |
@<shared skip functions@>@; |
| 11926 |
@<skip functions@>@; |
| 11927 |
|
| 11928 |
@<definition checks@>@; |
| 11929 |
@<get function declarations@>@; |
| 11930 |
@<write functions@>@; |
| 11931 |
@<shared get functions@>@; |
| 11932 |
@<get functions@>@; |
| 11933 |
|
| 11934 |
|
| 11935 |
|
| 11936 |
#define SKIP |
| 11937 |
#define DESCRIPTION "\n This program tests parsing a binary HINT file in reverse direction.\n" |
| 11938 |
|
| 11939 |
int main(int argc, char *argv[]) |
| 11940 |
{ @<local variables in |main|@>@; |
| 11941 |
in_ext=".hnt"; |
| 11942 |
out_ext=".bak"; |
| 11943 |
|
| 11944 |
@<process the command line@>@; |
| 11945 |
@<open the log file@>@; |
| 11946 |
hout=NULL; |
| 11947 |
if (!hget_map()) QUIT("Unable to map the input file"); |
| 11948 |
hpos=hstart=hin_addr; |
| 11949 |
hend=hstart+hin_size; |
| 11950 |
hget_banner(); |
| 11951 |
if (!hcheck_banner("hint")) QUIT("Invalid banner"); |
| 11952 |
hget_directory(); |
| 11953 |
hget_definition_section(); |
| 11954 |
DBG(DBGBASIC,"Skipping Content Section\n"); |
| 11955 |
hteg_content_section(); |
| 11956 |
DBG(DBGBASIC,"Fast forward Content Section\n"); |
| 11957 |
hpos=hstart; |
| 11958 |
while(hpos<hend) |
| 11959 |
{ hff_hpos(); |
| 11960 |
if (KIND(*(hpos-1))==par_kind && KIND(hff_tag)==list_kind && hff_list_size>0 && !(INFO(hff_tag)&b100)) |
| 11961 |
{ uint8_t *p=hpos,*q; |
| 11962 |
DBG(DBGTAGS,"Fast forward list at 0x%x, size %d\n",hff_list_pos,hff_list_size); |
| 11963 |
hpos=hstart+hff_list_pos; |
| 11964 |
q=hpos+hff_list_size; |
| 11965 |
while (hpos<q) |
| 11966 |
hff_hpos(); |
| 11967 |
DBG(DBGTAGS,"Fast forward list end at 0x%x\n",(uint32_t)(hpos-hstart)); |
| 11968 |
hpos=p; |
| 11969 |
DBG(DBGTAGS,"Continue at 0x%x\n",(uint32_t)(hpos-hstart-1)); |
| 11970 |
} |
| 11971 |
} |
| 11972 |
hget_unmap(); |
| 11973 |
@<close the log file@>@; |
| 11974 |
return 0; |
| 11975 |
} |
| 11976 |
@ |
| 11977 |
|
| 11978 |
As we have seen already in the {\tt stretch} program, a few |
| 11979 |
function declarations are necessary to satisfy the define before |
| 11980 |
use requirement of \CEE. |
| 11981 |
|
| 11982 |
@<skip function declarations@>= |
| 11983 |
static void hteg_content_node(void); |
| 11984 |
static void hteg_content(Tag z); |
| 11985 |
static void hteg_xdimen_node(Xdimen *x); |
| 11986 |
static void hteg_list(List *l); |
| 11987 |
static void hteg_param_list(List *l); |
| 11988 |
static void hteg_rule_node(void); |
| 11989 |
static void hteg_hbox_node(void); |
| 11990 |
static void hteg_vbox_node(void); |
| 11991 |
static void hteg_glue_node(void); |
| 11992 |
static int hteg_unknown(Tag z); |
| 11993 |
@ |
| 11994 |
|
| 11995 |
\thecodeindex |
| 11996 |
|
| 11997 |
\crosssections |
| 11998 |
|
| 11999 |
\plainsection{References} |
| 12000 |
|
| 12001 |
{\baselineskip=11pt |
| 12002 |
\def\bfblrm{\small\rm}% |
| 12003 |
\def\bblem{\small\it}% |
| 12004 |
\bibliography{../hint} |
| 12005 |
\bibliographystyle{plain} |
| 12006 |
} |
| 12007 |
|
| 12008 |
\plainsection{Index} |
| 12009 |
{ |
| 12010 |
\def\_{{\tt \UL}} % underline in a string |
| 12011 |
\catcode`\_=\active \let_=\_ % underline is a letter |
| 12012 |
\input format.ind |
| 12013 |
} |
| 12014 |
|
| 12015 |
\write\cont{} % ensure that the contents file isn't empty |
| 12016 |
% \write\cont{\catcode `\noexpand\@=12\relax} % \makeatother |
| 12017 |
\closeout\cont% the contents information has been fully gathered |