Conversion into RTF

Top  Previous  Next

Examples > Conversion of an Atari text > Conversion into RTF

 

Until now, the characters, which represent the Atari text attributes: underline, bold and italic, simply were ignored, because plain texts cannot contain such attributes. An example for a document, which can contain such attributes, is an RTF-file, which uses the Rich Text Format: RTF.

The project

 

...\Tetra\Examples\Atari\Atari2Rtf.ttp

 

demonstrates, how the Atari text can be transformed into the Rich Text Format. The relevant parts of the RTF specification are immediately used here, without explaining this specification in detail. A good introduction is "RTF Pocket Guide" of Sean M. Burke.

 

http://www.oreilly.com/catalog/rtfpg/

 

The important first chapter of the book as a PDF file can be downloaded there. The original Rich text specification can be found at:

 

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp

 

 

 

According to RTF specification, parts of text with special attributes are enclosed into bracket "{" and "}" and at the beginning of the enclosed text the attribute is written:

 

italic: \i

bold: \b

underline: \ul

 

So the token actions for e.g. italic become to:

 

italic_begin: out << "{\\i ";

italic_end: out << "} ";

 

In RTF the umlauts have to be represented as a backslash followed by an apostroph and the hexadecimal code of the character. So the token action e.g. of ae becomes:

 

ae: out << "\\'e4";

 

Line breaks, which shall be shown in the text-processing program, cannot be written directly into RTF-files. They have to be coded by "\line" or "\par". So an additional token has to be defined, which recognizes line breaks:

 

Name:    EOL

Text:      \r?\n

Action:   out << endl << "\\line ";

 

(By endl a line break is written into the RTF document to make a logical partition into the RTF source.)

 

The whole RTF file must be enclosed into the brackets "{" and "}" and the first bracket must be followed by a header, which specifies the RTF version, the fonts used and the font size. So the begin of a RTF file looks like:

 

{\rtf1\ansi\deff0 {\fonttbl{\f0 Courier New;}}\f0\fs20

 

To write this header, we will use the page for class elements of the TextTransformer for the first time. There you can variables and functions, which then can be used inside of the whole project. For the Atari project three functions are defined:

 

RtfBegin:

 

out        << "{\\rtf1"    // RTF, version 1

    << "\\ansi"     // ANSI character set

    << "\\deff0 ";  // default font #0

 

RtfFont:

 

out        << "{\\fonttbl" // font table

    << "{\\f0 Courier New;}" // font #0

           << "}"; // font table end

out        << "\\f0"    // use font #0

    << "\\fs20";  // font size = 20/2 = 10 points   

 

RtfEnd:

 

out        << "}";

 

RtfBegin and RtfFont could have been combined into a single function too or even written directly into the text of the Atari production. But by the division into these functions, the logical structure of the whole project becomes clearer.

 

The Atari production now is:

 

{{

RtfBegin();

RtfFont();

}}

(

special_char

| textattribute

| SKIP {= out << xState.str(); =}

)+

{{

RtfEnd();

}}

 

 

After you have transformed a text, you have to save the output as an RTF-file. That means, the extension of the file must be "rtf". Now this file can be opened into a word processing program - e.g. WordPad -, which is able to display the RTF format. There you can see for example Benjamin now correctly written in italic

 

JacobBenjaminMeyer

 



This page belongs to the TextTransformer Documentation

Home  Content  German