|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes the methodology and presents sample code that shows the basics of parsing the Rich Text Format to automatically add colour to text/syntax displayed in a standard BackgroundRecently, I have been working with C# and AIML and I thought I'd write a program that allows easy editing and fast creation of multiple tags within the files. While writing this, I thought it would be nice to be able to automatically add colour to the AIML tags when the file is displayed in a OverviewThere are two basic methods of changing the colour of specific text within a
While method 1 is suitable for when a user edits a file, it is far faster and more of a complete solution to use option 2 and alter the native RTF. It does require learning a little bit about RTF - but learning new stuff is what it's all about, right?! :) Rich Text FormatDetails on RTF can be found here. The way colour works in RTF is that a colour table is defined in the RTF header, and any changes of colour in the text are referenced by the index of a defined colour in the colour table. For example, the colour table for a simple Rich Text File with three colours (Red, Green and Blue) would look like this: {\colortbl;\red255\green0\blue0;\red0\green255\blue0;\red0\green0\blue255;}
The colour table is flagged by the The colour of the text in the bulk of the file is changed using the Parsing the RTFThe simplest way of generating the RTF used to display text in a {
\rtf1\ansi\ansicpg1252\deff0\deflang1033
{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
}
To add a colour table, we have to do a series of //Get RTF from richtextbox
string strRTF = richTextBox1.RTF;
// Search for colour table info. If it exists (it shouldn't,
// but we'll check anyway) remove it and replace with our one
int iCTableStart = strRTF.IndexOf("colortbl;");
if (iCTableStart != -1) //then colortbl exists
{
//find end of colortbl tab by searching
//forward from the colortbl tab itself
int iCTableEnd = strRTF.IndexOf('}', iCTableStart);
//remove the existing colour table
strRTF = strRTF.Remove(iCTableStart, iCTableEnd - iCTableStart);
//now insert new colour table at index of old colortbl tag
strRTF = strRTF.Insert(iCTableStart,
// CHANGE THIS STRING TO ALTER COLOUR TABLE
"colortbl;\\red255\\green0\\blue0;\\red0\\green128\\blue0;
red0\\green0\\blue255;}");
}
//colour table doesn't exist yet, so let's make one
else
{
// find index of start of header
int iRTFLoc = strRTF.IndexOf("\\rtf");
// get index of where we'll insert the colour table
// try finding opening bracket of first property of header first
int iInsertLoc = strRTF.IndexOf('{', iRTFLoc);
// if there is no property, we'll insert colour table
// just before the end bracket of the header
if (iInsertLoc == -1) iInsertLoc = strRTF.IndexOf('}', iRTFLoc) - 1;
// insert the colour table at our chosen location
strRTF = strRTF.Insert(iInsertLoc,
// CHANGE THIS STRING TO ALTER COLOUR TABLE
"{\\colortbl ;\\red128\\green0\\blue0;\\red0\\green128\\blue0;
d0\\green0\\blue255;}");
}
Let's quickly analyse this code segment. The first thing we do is to check whether a colour table already exists with the RTF, by searching for the All that remains for us to do now is to parse the rest of RTF string, and adding /*
* In our colour table we defined:
* cf1 = red
* cf2 = green
* cf3 = blue
* (cf0 = automatic by default)
* */
//Parse the string
for (int i = 0; i < strRTF.Length; i++)
{
//check for correct character
if (strRTF[i] == '<')
{
//add RTF tags after symbol
strRTF = strRTF.Insert(i + 1, "\\cf1 ");
//add RTF before symbol
strRTF = strRTF.Insert(i, "\\cf3 ");
//skip forward past the characters we've just added
//to avoid getting trapped in the loop
i += 6;
}
}
This is probably not the most efficient or fastest method of parsing a In the sample code, I have catered to a couple of other characters so that an AIML file (a sample of which is included in the sample program) is presented as coloured text. Summary and Further WorkThis article has shown you how it is possible to directly access the RTF of a standard History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||