The text API described in this topic is out of date. If you want to learn about text API represented by the new drawing engine in Graphics Mill, read the Formatted Text topic instead.
Writing text may require the highlighting of a certain area of text, whether it be a phrase or a single word. While Graphics.DrawString or Graphics.DrawText could be used to do this, they are not optimal methods, because of the necessity of breaking text into chunks with the same font settings, and then rendering them separately by calling the Graphics.DrawString or Graphics.DrawText methods. Graphics Mill provides a far more streamlined method for highlighting, called the Graphics.DrawFormattedText. This method allows the completion of this task with ease. This method is quite similar to Graphics.DrawText and accepts the same arguments:
There is only one difference between these methods: the text you want to draw with the Graphics.DrawFormattedText method must be marked up in XML-like style. The following tags and document structure are allowed:
<root>
<para [style="..."]>
[some text]
<span [style="..."]>
[some text]
<span [style="..."]>
[some text]
</span>
</span>
...
<span [style="..."]></span>
</para>
...
<para [style="..."]></para>
</root>
Let's consider it in more detail.
Root is the root tag. There should be only one root tag in the
text argument which incorporates all parts of the text, along with their settings. If you place some text outside the
<root></root>
tags, the Graphics.DrawFormattedText
will throw an ArgumentException.
Para is a paragraph tag. All the text you want to draw should be divided into at least one paragraph
and each paragraph should be enclosed between <para>
and </para>
tags.
Otherwise, the text won't be parsed and drawn. It supports the style attribute (see
below) which allows you to specify some font and color settings of this paragraph.
Span is a tag intended to define a new inline area. You can use it to highlight
some text inside a paragraph. These tags can be embedded in the existing <span></span>
tags.
It supports the style attribute too.
All allowed tags are case insensitive, however, the name of every opening tag must have the same case as the corresponding closing tag.
This means that <SpAn></SpAn>
or <rOOt></rOOt>
pairs will be parsed
successfully, but pairs like <Para></parA>
will cause an error.
The text layout mechanism is based on CSS and uses style attributes for attaching font settings and text parameters for paragraphs and their portions. These attributes accept settings as a formatted string. Its common syntax is shown below:
style="style-parameter1:value1; style-parameter2:value2; ...; style-parameterN:valueN"
The style attribute and its value are case insensitive, i.e. this style setting will be
applied correctly: StyLe="fOnT-faMily: CoUriEr NeW; FoNt-siZe: 18; cOlOr:RgB(100,10,10)"
All supported parameters are described in the following table:
Name | Description | Examples |
---|---|---|
Font Parameters | ||
font-family |
Specifies the typeface name of the font. If font with specified typeface name is not found, system uses a default font (typically "Arial"). |
style="font-family:arial"
|
font-style |
Selects between normal and italic faces within a font family. |
style="font-style:normal"
style="font-style:italic"
|
font-weight |
Selects the weight of the font. The values from |
style="font-weight:normal"
style="font-weight:bold"
style="font-weight:600"
|
font-size |
Specifies the font size. |
style="font-size:12"
|
Color Parameters | ||
color |
Specifies the text color. The numerical RGB specification is used. No keywords (like
|
style="color:rgb(200, 0, 0)"
|
Text Parameters | ||
text-align |
Specifies the text alignment. The following alignment types are supported:
If the justify alignment type is specified, the last line of the text can be aligned using the textAlignLast parameter. This parameter takes one of the following values:
If the textAlignLast parameter is not specified the last line of the text will be aligned along the left edge. |
style="text-align:left"
style="text-align:justify; textAlignLast:right"
|
_line-spacing |
Specifies the space between two adjacent lines of text. |
style="_line-spacing:10px"
style="_line-spacing:-15px"
|
Graphics.DrawFormattedText supports the following XML predeclared entities:
&
- & ampersand;<
- < less than;>
- > greater than.This method also processes service characters in the specific way. It parses the tabulation character (\t
in C#) as a sequence of whitespaces.
The number of whitespaces with which to replace one tabulation character can be specified by the tabSize
parameter.
Graphics.DrawFormattedText, unlike
the Graphics.DrawText method, does not support the
newline character (\n
in C#). To draw a chunk of text from a
new line you have to enclose it in <para></para>
tags.
The following code example draws a birthday postcard with specified text that shown below:
// Background bitmap
using (var bitmap = new Bitmap(@"Images\balloons.jpg"))
using (var graphics = bitmap.GetGraphics())
{
// XML string for drawing
string resultXml;
//define font and color settings
var font = new Font("Tahoma", 14);
var brush = new SolidBrush(RgbColor.Black);
// Mark up the text
resultXml = "<root>"+
"<para style=\"font-size:40;text-align:center;color:rgb(220,0,0);_line-spacing:40px\">" +
"Dear John!" + "</para>" +
"<para style=\"font-size:20;text-align:justify;font-weight:bold;color:rgb(5,70,150);_line-spacing:20px\">" +
"\tFor a happy birthday and a happy, healthy and lucky year. We wish you to live in you own way, " +
"to reach for your own goals, to be the person you want to be. Wishing you happiness on your bithday!" +
"</para>" + "<para style=\"font-size:18;text-align:right;font-style:italic;color:rgb(220,235,250)\">" +
"Your friends" + "</para>" + "</root>";
graphics.DrawFormattedText(resultXml, font, brush, 40, 40, bitmap.Width - 80, bitmap.Height - 80, true, 4);
bitmap.Save(@"Images\Output\formatted.png");
}