<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?> 
<!DOCTYPE document SYSTEM "rml_1_0.dtd"> 
<document filename="test_012_simpletoc.pdf"> 
<template pageSize="(595, 842)" leftMargin="72" showBoundary="0">
    
    <pageTemplate id="cover">
    	<pageGraphics>
        	<setFont name="Times-Bold" size="48"/>
        	<drawCentredString x="297" y="516">Table of Contents Example</drawCentredString>
    	</pageGraphics>
    	<frame id="first" x1="72" y1="72" width="451" height="698"/>
    </pageTemplate>
    
    <pageTemplate id="TOC">
		<pageGraphics>
			<doForm name="TOC"/>
		</pageGraphics>
    	<frame id="first" x1="-1" y1="-1" width="0" height="0"/>
    </pageTemplate>
	
    <pageTemplate id="main">
		<pageGraphics>
			<setFont name="Helvetica-BoldOblique" size="18"/>
			<drawRightString x="523" y="800">RML2PDF Test Suite - Test #012 - Tables of Contents</drawRightString>
			<drawRightString x="523" y="20"><evalString default="XXX"><pageNumber/>-2</evalString></drawRightString>
		</pageGraphics>
		<frame id="first" x1="1in" y1="1in" width="6.27in" height="9.69in"/>
	</pageTemplate>
</template>

<stylesheet>
	<initialize>
	<alias id="style.normal" value="style.Normal"/>
	</initialize>

	<paraStyle name="h1"
	fontName="Helvetica-Bold"
	fontSize="12"
	spaceBefore = "0.5 cm"
	/>
	
    <paraStyle name="normal"
	fontName="Helvetica"
	fontSize="10"
    spaceBefore="6"
	/>

    <paraStyle name="code"
	fontName="Courier"
	fontSize="8"
    leftIndent="36"
    textColor="blue"
	/>
    
	<paraStyle name="TOC"
    fontName="Helvetica"
    fontSize="12"
    />
</stylesheet>

<story>

<setNextTemplate name="TOC"/>
<nextFrame/>

<setNextTemplate name="main"/>
<nextFrame/>
<namedString id="pageZero"><pageNumber/></namedString>
<para style="h1">Chapter 1: About this page</para>
<namedString id="label1">Chapter 1</namedString>
<namedString id="page1"><evalString default="XXX"><pageNumber/>-<getName id="pageZero"/>+1</evalString></namedString>
	<para style="normal">About these pages: the code behind this pdf should give an example of how to set up a table
    of contents. </para>
    
    
    <para style="normal">There are several ways to do tables of contents.  We make use of two different features:
    <i>names</i>, which let you refer to other page numbers or bits of text and ensure the references are filled in
    as the document is built; and <i>forms</i>, which let you defer drawing things until later on.</para>
    
    <para style="normal">RML is usually generated by another program.  We assume this other program can keep
    track of the headings as it goes through.  So, if you have a book with ten chapters, you will have to
    write out the chapter names ten times in the TOC as well as in each chapter - but RML will sort out
    the page numbers for you.  The source for this example and the comments below should be enough to put
    it together.</para>

    <para style="h1">Names and References</para>

    <para style="normal">The <b>name</b>, <b>getName</b>, <b>namedString</b> and <b>evalString</b> tags are
    all used in combination to set up cross references.  You can give a name to a piece of text, and refer
    to it elsewhere in the document.   If we want to refer to, say, the page on which Chapter 3 starts, we
    would put the following in the heading or some text just after it: </para>

    <para style="code">Blah blah&lt;namedString id="Chapter3Start"&gt;&lt;pageNumber/&gt;&lt;/namedString&gt; blah</para>

    <para style="normal">The <b>pageNumber</b> tag returns the current page number, and the <b>namedString</b> one
    sets up an internal variable which can be referenced ANYWHERE in the document, before or after the definition.
    To display this page number elsewhere in the document, you would place the following inside some other paragraph
    or literal string:</para>

    <para style="code">...see chapter 3 on page &lt;getName id="Chapter3Start"/&gt; for details.</para>

    <para style="normal">You don't have to use names with page numbers - you can use this construct to
    store any text, such as references to chapter names or figures.</para>

    <para style="normal">This cross-reference mechanism is the basis of all indexes, tables of contents,
    cross references and so on. However it has performance implications.  RML willl reformat the whole
    document again and again until all the references are <b>resolved</b>. This means that if you define
    names at the beginning of the document and refer to them later, it can work at full speed (as each
    paragraph's content is fully known when it is drawn).  But if you refer forwards, RML makes two -
    or in some cases three - passes through the document.  Your job will be 2-3 times slower. We'll
    deal with this in a moment.</para>
    
    
    <para style="normal">In real world documents there is another complication.  You might have a
    fancy cover or front matter, and the logical page number 1 used in printing might not actually
    be page 1.  Likewise, you might be doing a batch of customer docs in one RML job.  So, in this
    case we have a more involved expression, and use the <b>evalString</b> tag to work out
    the number we want.  In this example we did this by creating a name for the first page after
    the cover, </para>

    <xpre style="code">...&lt;namedString id="page1"&gt;
    &lt;evalString default="XXX"&gt;
        &lt;pageNumber/&gt;-&lt;getName id="pageZero"/&gt;+1
    &lt;/evalString&gt;
&lt;/namedString&gt;...</xpre>


    <para style="normal">This says 'work out the page number of the cover, add 1 and store that in the variable "page1" for future use'.</para>

    <para style="h1">Forms and Deferred Drawing</para>

    <para style="normal">Forms (or as Adobe calls them, Form XObjects) are graphics drawn outside of the individual
    pages in a PDF file, and which may be used more than once or referred to by name.  Conveniently, we let you
    refer to a form early in a document, and define it later.  If you fail to define it later on, you will get
    an error message on generating the PDF.  This lets us get around the two-pass problem:  we don't actually
    draw the table of contents until the end, when we know all the named page number references and can do it
    in one pass.  The techniques used here is to "draw" a form called TOC in the cover page template, and define it
    at the end of the document.  As noted above, it's the programmer's job to regenerate the chapter titles,
    but the page numbers take care of themselves.</para>
    
    <para style="normal">Ignore the following pseudo-bio babble, it's there for padding</para>

	<para style="h1">Politics of Foramenifera </para>
	<para style="normal">Globorotalia Menardii coils according to water temperature and prevailing political situations. 
    Preferring alkaline rather than acidic environments they are however intuitively left-wing</para>

	<para style="h1">Drosophila Melanogaster </para>
	<para style="normal">How fruit flies changed my life. Blah blah waffle waffle blah blah yawn yawn
    ....................................... </para>

	
<nextFrame/>
<para style="h1">CHAPTER2</para>
<namedString id="label2">Chapter 2</namedString>
<namedString id="page2"><evalString default="XXX"><pageNumber/>-<getName id="pageZero"/>+1</evalString></namedString>

    <para style="h1">Politics of Foramenifera</para>
	<para style="normal">Globorotalia Menardii coils according to water temperature and prevailing political situations. 
    Preferring alkaline rather than acidic environments they are intuitively left-wing</para>

	<para style="h1">Drosophila Melanogaster </para>
	<para style="normal">How fruit flies changed my life. Blah blah waffle waffle blah blah yawn yawn
    ....................................... </para>
<nextFrame/>
	
<para style="h1">CHAPTER3</para>
<namedString id="label3">Chapter 3</namedString>
<namedString id="page3"><evalString default="XXX"><pageNumber/>-<getName id="pageZero"/>+1</evalString></namedString>

<para style="h1">Acrocladium Cuspidatum</para>
	<para style="normal">A meaningful relationship with Moss</para>

	<para style="h1">Rattus rattus</para>
	<para style="normal">So bad they named him twice Blah blah waffle waffle blah blah yawn yawn
    ....................................... </para>
	
	
<nextFrame/>
<para style="h1">Last Chapter</para>
<namedString id="labellast">Last Chapter</namedString>
<namedString id="pagelast"><evalString default="XXX"><pageNumber/>-<getName id="pageZero"/>+1</evalString></namedString>
	<para style="normal">
Blah blah waffle waffle blah blah yawn yawn.......................................
Blah blah waffle waffle blah blah yawn yawn.......................................
Blah blah waffle waffle blah blah yawn yawn.......................................
	</para>

<!--This is where we define the form for the TOC.  Here we did it as a static graphic.  You could also create
a place tag and flow paragraphs into it if you preferred.-->
<illustration width="0" height="0">
	<form name="TOC">
		<setFont name="Times-Bold" size="20"/>
		<fill color="red"/>
		<drawString x="4in" y="9in">Contents</drawString>
		<setFont name="Times-Bold" size="20"/>
		<fill color="black"/>

		<setFont name="Times-Roman" size="18"/>
		<place x="2in" y="8.50in" width="5in" height="0.25in"><para style="TOC"><getName id="label1"/></para></place>
		<place x="2in" y="8.50in" width="5in" height="0.25in"><para style="TOC" alignment="RIGHT"><getName id="page1"/></para></place>

		<place x="2in" y="8.25in" width="5in" height="0.25in"><para style="TOC"><getName id="label2"/></para></place>
		<place x="2in" y="8.25in" width="5in" height="0.25in"><para style="TOC" alignment="RIGHT"><getName id="page2"/></para></place>

		<place x="2in" y="8.00in" width="5in" height="0.25in"><para style="TOC"><getName id="label3"/></para></place>
		<place x="2in" y="8.00in" width="5in" height="0.25in"><para style="TOC" alignment="RIGHT"><getName id="page3"/></para></place>

		<place x="2in" y="7.75in" width="5in" height="0.25in"><para style="TOC"><getName id="labellast"/></para></place>
		<place x="2in" y="7.75in" width="5in" height="0.25in"><para style="TOC" alignment="RIGHT"><getName id="pagelast"/></para></place>

		<setFont name="Times-Roman" size="12"/>
		<drawCentredString x="297" y="4in">(The page numbers in this are dynamically calculated)</drawCentredString>

	</form>
</illustration>
</story>
</document>
