#			-*- Tcl -*-
# $Id: script,v 1.2 1998/10/13 08:11:31 tobotras Exp $

require RTF.spec

# Define stylesheet:

rtf:documentFormat {
    LeftMargin		3cm
    RightMargin		2cm
    TopMargin		0.5in
    BottomMargin	0.5in
    FootnoteNumbering	Arabic
    FootnoteRestart	AtPage
    FootnotePlacement	PageBottom
}

# ---------------------------------------------------

rtf:sectStyle theSection "Section style" {
    HeaderPosition	0.5in
    FooterPosition	0.5in
    SectionBreak	Page
}

# ---------------------------------------------------

rtf:paraStyle body "Body Text" {
    Font roman
    FontSize 12pt
    LeftIndent 0pt
    SpaceBefore 6pt
}

rtf:paraStyle heading1 "Heading 1" {
    Font sans
    FontSize 16pt
    Bold 1
    LeftIndent 0pt
    SpaceAfter 5mm
    SpaceBefore 1cm
    TopBorder	thick
}

rtf:paraStyle heading2 "Heading 2" {
    Font sans
    FontSize 14pt
    Bold 1
    LeftIndent 0pt
    SpaceAfter 2mm
    SpaceBefore 5mm
    TopBorder thin
}

rtf:paraStyle heading3 "Heading 3" {
    Font sans
    FontSize 14pt
    LeftIndent 0pt
    SpaceAfter 1mm
    SpaceBefore 3mm
}

rtf:paraStyle title "Title" {
    Font sans
    FontSize 18pt
    Bold 1
    Quadding Center
}

rtf:paraStyle author "Author" {
    Font roman
    FontSize 16pt
    Quadding Center
    SpaceBefore 1cm
    SpaceAfter 2cm
}

rtf:paraStyle date "Date" -basedon author {
    SpaceAfter 5cm
}

rtf:paraStyle abstract "Abstract" {
    Font mono
    FontSize 14pt
    Italic 1
    TopBorder thin
    BottomBorder thin
    SpaceAfter 1cm
    Quadding Center
}

rtf:paraStyle itemize "Itemization" -basedon body {
    LeftIndent 5mm
    SpaceAfter 3mm
}

rtf:paraStyle tag "Tag start" -basedon body {
    Bold 1
}

# ---------------------------------------------------

rtf:charStyle plain "Plain chars" {
    Font roman
    FontSize 12pt
}

rtf:charStyle heading_inline "Inline heading" -basedon plain {
    FontSize 14pt
    Italic 1
}

rtf:charStyle emphasized "Emphasized chars" -basedon plain {
    Italic 1
}

rtf:charStyle strong "Strong chars" -basedon plain {
    Bold 1
}

rtf:charStyle bold "Bold chars" -basedon strong {
}

rtf:charStyle monospaced "Typewriter chars" -basedon plain {
    Font mono
}

# Global vars

set sectN 0
set ssectN 0

# Specify processing for each element type:

specification rtfSpec {
    {element P} {
	rtf para paraStyle body
	before	"[rtfEnv save; rtfEnv set cdataFilter trimSpaces]"
	after	"[rtfEnv restore]"
    }
    {element TITLEPAG} {
	after	"[rtf:pageBreak]"
    }
    {element TITLE in TITLEPAG} {
	rtf para paraStyle title
    }
    {element NAME in AUTHOR in TITLEPAG} {
	rtf para paraStyle author
    }
    {element DATE in TITLEPAG} {
	rtf para paraStyle date
    }
    {element ABSTRACT in TITLEPAG} {
	rtf para paraStyle abstract
	before	"[rtfEnv save; rtfEnv set cdataFilter trimSpaces]"
	after	"[rtfEnv restore]"
    }
    {element SECT} {
	rtf section sectStyle theSection
    }
    {element HEADING in SECT} {
	rtf para paraStyle heading1
	prefix "[sectnumber] "
    }
    {element HEADING in SECT1} {
	rtf para paraStyle heading2
	prefix "[sect1number] "
    }
    {element HEADING in SECT2} {
	rtf para paraStyle heading3
    }
    {element HEADING in "SECT3 SECT4"} {
	rtf phrase charStyle heading_inline
    }
    {element ITEMIZE} {
	rtf para paraStyle itemize
	before		"[doFirstItem]"
	after		"[closeList]"
    }
    {element ENUM} {
	rtf para paraStyle itemize
	before		"[doFirstItem]"
	after		"[closeList]"
    }
    {element LIST} {
	rtf para paraStyle itemize
	before		"[doFirstItem]"
	after		"[closeList]"
    }
    {element ITEM in ITEMIZE} {
	before		"[firstItemLineBreak]"
	prefix		"[itemIndent]$rtfSpecial(Bullet) "
	cdataFilter	trimSpaces
    }
    {element ITEM in ENUM} {
	before		"[firstItemLineBreak]"
	prefix		"[itemIndent][childNumber]. "
	cdataFilter	trimSpaces
    }
    {element ITEM in LIST} {
	prefix		"[itemIndent]"
	suffix		"[rtf:lineBreak]"
	cdataFilter	trimSpaces
    }
    {element EM} {
	rtf phrase charStyle emphasized
    }
    {element TAG} {
	rtf phrase charStyle strong
    }
    {element BF} {
	rtf phrase charStyle bold
    }
    {element TT} {
	rtf phrase charStyle monospaced
    }
    {element FOOTNOTE} {
	before	"[rtf:special FootnoteNumber][rtf:divert Footnote][rtf:special FootnoteNumber] "
	after	"[rtf:undivert]"
    }
}

proc trimSpaces { text } {
    regsub -all -- "\[ \n\t\]+" $text " " var
    return [string trim $var]
}

proc identity { text } { 
    return $text
}

proc doFirstItem {} {
    global firstItem itemOffset
    set firstItem 1
    if { ![info exists itemOffset] } {
	set itemOffset 0
    }
    incr itemOffset
    return ""
}

proc itemIndent {} {
    global itemOffset rtfSpecial
    set ret {}
    for { set i 0 } { $i < $itemOffset } { incr i } {
	append ret $rtfSpecial(Tab)
    }
    return $ret
}

proc closeList {} {
    global itemOffset
    incr itemOffset -1
    return ""
}

proc firstItemLineBreak {} {
    global firstItem
    set tmp $firstItem
    set firstItem 0
    if { $tmp != 1 } {
	return [rtf:lineBreak]
    } else {
	return ""
    }
}

proc sectnumber {} {
    global sectN ssectN
    incr sectN
    set ssectN 0
    return "$sectN"
}

proc sect1number {} {
    global sectN ssectN
    incr ssectN
    return "${sectN}.${ssectN}"
}

# Main routine:
proc main {} {
    rtf:start
    rtf:convert rtfSpec
    rtf:end
}
