Structure Analysis and Application of RTF Files

Source: Internet
Author: User

RTF is a very popular file structure. Many text editors support it and VB and other development tools.

It even provides the richtxtbox control. Programmers who write common tools should consider

Add the read/write function of the RTF file to the file, so that you need to have a full understanding of the structure of the RTF. And now

The most important means of publishing information is www.

What the sequencer should consider. Although this function is already available in word, it cannot be applied to your customers.

Said: "First Use my program to save it as RTF, and then use Word ......".

 

The Structure Analysis and Application of the RTF file are discussed below.

 

1. analyze the structure of the RTF File

 

The structure of the RTF is not complex, but the content is numerous. This article cannot describe them one by one.

(If you want to read the detailed RTF document, you can search for it on the internet or contact the author .)

Each RTF file is a text file, which is formatted by the RTF reader during display. At the beginning of the file is

{\ RTF, which is an essential symbol of an RTF file. The RTF reader judges a file based on it.

Whether it is in the RTF format. Then there is the file header and body. The file header includes the font table, file table, and color table.

Several data structures. The font and table style in the body are formatted Based on the file header information.

Each table is enclosed in a pair of braces and contains many commands starting with the character. For example,

A color table is as follows:

 

{\ Colortbl; \ red0 \ green0 \ blue0; \ red0 \ green0 \ blue255; \ red0 \ green255 \ blue255; \ red0 \ green255 \ blue0; \ red255 \ green0 \ blue255; \ red255 \ green0 \ blue0; \ red255 \ green255 \ blue0; \ red255 \ green255 \ blue255; \ red0 \ green0 \ blue128; \ red0 \ green128 \ blue128; \ red0 \ green128 \ blue0; \ red128 \ green0 \ blue128; \ red128 \ green0 \ blue0; \ red128 \ green128 \ blue0; \ red128 \ green128 \ blue128; \ red192 \ green192 \ blue192 ;}

 

Start with \ colortbl to indicate the color table in braces, followed by \ red0 \ green0 \ blue0,

A color is registered. The red, green, and blue components of this color are all 0. Other tables. After the file header is

Body, which consists of layout formatting commands, text, and various special commands. Only special commands are available.

It is enclosed in braces, and the layout formatting commands and text are "open", so as to put the text and life

Separation. There is a "}" at the end of the file, which corresponds to the first. In the entire file, "}"

It must correspond. This format is the basis of the RTF reader and converter algorithm.

 

 

The special feature of the RTF format is that some characters have special meanings in the command.

When they appear as text, they need to add a "\" before them. For example, "\" must represent

\. In fact, this form is common in most programming languages.

 

Ii. Algorithm Analysis

 

Although the algorithms introduced in this section are designed for read/write of RTF, they are also common for general file filters.

Method, suitable for conversion between files of various formats. Specifically, all files are converted into one type.

Intermediate format, and then display or convert as required. There is a principle that must be followed: the program is required

You must be able to filter out unrecognized formats. Various files have their special formats, which are inevitable during the conversion process.

There will be a loss of format, which should be considered in the algorithm. For formatted text such as RTF

For files, the most important thing is to correctly display or convert the file size, color, Font, and other styles.

Therefore, a data structure should be used in the program to store the information. This structure is called

In the intermediate format, how do you set the rules for the listener. The flowchart is as follows:

 

Iii. Difficulties

 

We encountered many problems during the development process, two of which were particularly interesting.

 

The first problem is the representation of Chinese characters. In RTF, Chinese characters are represented by commands:

"\ 'Internal code ". The inner code is the inner code of the Chinese character machine. However, note that the RTF file is a text file with an internal code.

It is stored in ASCII code and must be converted into numbers before it can be used. For example, "electronics and computers"

The format in RTF is:

 

\ 'B5 \ 'e7 \ 'd7 \ 'd3 \ 'd3 \ 'EB \ 'b5 \ 'e7 \ 'c4 \ 'd4

 

The second is the image problem, which is also the focus of this article. The image in RTF exists in two ways:

One method is direct embedding, starting with {\ PICT; the second method is embedding as an OLE object.

{\ Object. When the RTF processor can directly use OLE, the RTF file provides Ole data; no

Then, the file directly provides the image data, starting with {\ result. The most common image format

Is a Metafile containing DIB bitmap. This format is not described in the SDK, and

In RTF, it is stored in the compressed form, so it is difficult to convert it. We have adopted a comparison

New Method: Read the meta file data and save it as a file. Then, use getmetafile and

The playmetafile function prints the image in the file on a memory DC, and finally uses the image capture method

It is saved as a bitmap file. The following are specific procedures:

 

Voidwmftobmp (intwidth, intheight, char * Metafile)

 

{

 

// Width indicates the image width, height indicates the image height, and * Metafile indicates the object name.

 

Static hmetafile hmf;

 

Charnum [4];

 

File * FH;

 

HDC hmemdc;

 

Hbitmap hbmp, hold;

 

Bitmapfileheader HDR;

 

Lpbitmapinfoheader lpbmpih;

 

Hglobal Hg;

 

  

 

Bitmapcount ++;

 

If (bitmapcount = 3)

 

{

 

Length = 128;

 

Height = 132;

 

}

 

If (bitmapcount = 1)

 

{

 

Width = 80;

 

Height = 50;

 

}

 

Tostring10 (bitmapcount, num );

 

// Function for converting a string into a number

 

Lstrcpy (BMP file, filepath );

 

Lstrcat (BMP file, num );

 

Lstrcpy (giffile, BMP file );

 

Lstrcat (BMP file, ". BMP ");

 

Lstrcat (giffile, ". GIF ");

 

Hmf = getmetafile (Metafile );

 

Hdisplaydc = createdc ("display", null );

 

Hmemdc = createcompatibledc (hdisplaydc );

 

Hg = globalalloc (ghnd, sizeof (bitmapinfoheader ));

 

Lpbmpih = (lpbitmapinfoheader) globallock (Hg );

 

Lpbmpih-> bisize = sizeof (bitmapinfoheader );

 

Lpbmpih-> biwidth = width;

 

Lpbmpih-> biheight = height;

 

Lpbmpih-> biplanes = 1;

 

Lpbmpih-> bibitcount = 8;

 

Lpbmpih-> bicompression = 0;

 

Hbmp = createcompatiblebitmap (hdisplaydc, width, height );

 

Globalunlock (Hg );

 

Globalfree (Hg );

 

Hold = SelectObject (hmemdc, hbmp );

 

Setmapmode (hmemdc, mm_anisotropic );

 

Setdomainworgex (hmemdc, null );

 

Setviewportextex (hmemdc, width, height, null );

 

Playmetafile (hmemdc, hmf );

 

Hbmp = SelectObject (hmemdc, hold );

 

If (FH = fopen (BMP file, "W + B") = NULL)

 

{

 

Return;

 

}

 

HDR. bftype = 0x4d42;

 

HDR. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad) + width * height;

 

HDR. bfreserved1 = 0;

 

HDR. bfreserved2 = 0;

 

HDR. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad );

 

Fwrite (lpstr) & HDR, sizeof (bitmapfileheader), 1, FH );

 

Hg = globalalloc (ghnd, sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad ));

 

Lpbmpih = (lpbitmapinfoheader) globallock (Hg );

 

Lpbmpih-> bisize = sizeof (bitmapinfoheader );

 

Lpbmpih-> biwidth = width;

 

Lpbmpih-> biheight = height;

 

Lpbmpih-> biplanes = 1;

 

Lpbmpih-> bibitcount = 8;

 

Lpbmpih-> bicompression = 0;

 

Lpbmpih-> bisizeimage = width * height;

 

Lpbmpih-> biclrused = 256;

 

Lpbmpih-> biclrimportant = 0;

 

J = getdibits (hmemdc, hbmp, 0, height, null, (bitmapinfo *) lpbmpih, dib_rgb_colors );

 

Lpbmpih-> bisize = sizeof (bitmapinfoheader );

 

Fwrite (lpbmpih, sizeof (bitmapinfoheader) + 256 * sizeof (rgbquad), 1, FH );

 

Getdibits (hmemdc, hbmp, 0, height, (lpstr) filebuffer, (bitmapinfo *) lpbmpih, dib_rgb_cols );

 

Fwrite (filebuffer, width * height, 1, FH );

 

Globalunlock (Hg );

 

Globalfree (Hg );

 

Fclose (FH );

 

Deletedc (hmemdc );

 

Deletedc (hdisplaydc );

 

Deleteobject (hbmp );

 

Deleteobject (hold );

 

Deletemetafile (hmf );

 

} // End function wmftobmp ()

 

Another advantage of using this method is that when you develop the RTF reader from the bottom layer

You need to change the playmetafile parameter from the memory DC to the screen DC to display the image.

 

Iv. Extensions of the RTF Format

 

Finally, we will discuss the extensions of the RTF format. The RTF format should be unified as a standard,

In some cases, expansion is necessary. The most obvious example is Microsoft's word, which has its own unique

. If you want to make your software technically advantageous, you can also create a new

Command. For example, if you want to support DHTML in the software, you can embed

Commands such as {\ DHTML or {\ Java. Since the RTF reader has the function of filtering unknown commands

This will not affect the versatility of the RTF file.

 

 

Structure Analysis and Application of RTF Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.