<?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="Helvetica-Bold" size="18"/>
		    <drawString x="35" y="783">RML Example 16: Simpletoc</drawString>
			<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="749" width="72" height="72"/>
    		<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" />
        	<setFont name="Times-Bold" size="48"/>
        	<drawCentredString x="300" y="516">Table of Contents Example</drawCentredString>
    	</pageGraphics>
    	<frame id="first" x1="72" y1="72" width="451" height="698"/>
    </pageTemplate>
    
    <pageTemplate id="TOC">
		<pageGraphics>
		<setFont name="Helvetica-Bold" size="18"/>
		    <drawString x="35" y="783">RML Example 16: Simpletoc</drawString>
			<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="749" width="72" height="72"/>
    		<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" />
			<doForm name="TOC"/>
		</pageGraphics>
    	<frame id="first" x1="-1" y1="-1" width="0" height="0"/>
    </pageTemplate>
	
    <pageTemplate id="main">
		<pageGraphics>
			<setFont name="Helvetica-Bold" size="18"/>
		    <drawString x="35" y="783">RML Example 16: Simpletoc</drawString>
			<image file="logo_no_bar.png" preserveAspectRatio="1" x="488" y="749" width="72" height="72"/>
    		<image file="strapline.png" preserveAspectRatio="1" x="35" y="0" width="525" />
                <setFont  name="Helvetica" size="8pt"/><drawRightString x="523" y="40"><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"
    />
    <paraStyle name="intro"  fontName="Helvetica" fontSize="12" leading="12" spaceAfter="12"/>
</stylesheet>

<story>

<storyPlace x="35" y="660" width="525" height="73" origin="page">
<para style="intro">RML (Report Markup Language) is ReportLab's own language for specifying the appearance of a printed page, which is converted into PDF by the utility rml2pdf.</para>
<hr color="white" thickness="8pt"/>
<para style="intro">These RML samples showcase techniques and features for generating various types of ouput and are distributed within our commercial package as test cases. Each should be self explanatory and stand alone.</para>
<illustration height="3" width="525" align="center">
<fill color= "(0,0.99,0.97,0.0)" />
<rect x="0" y = "-12" width="525" height="3" round="1" fill="1" stroke = "Yes" />
</illustration>
</storyPlace>


<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>
<illustration width="144" height="10" borderStrokeWidth="0.5" borderFillColor="palegreen" borderStrokeColor="darkblue" borderDash="2,1">
	<drawString x="0" y="0">Illustration with a border</drawString></illustration>

<!--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" borderStrokeWidth="0.5" borderFillColor="palegreen" borderStrokeColor="darkblue" borderDash="2,1">
	<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" borderStrokeColor="red" borderFillColor="yellow" borderStrokeWidth="1"><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>
