Draw different styles of “illuminated” text in C#

[illuminated text]

My previous post Draw “illuminated” text in C# showed how to draw “illuminated” text. It drew each paragraph’s initial letter in a large font with a box around it.

Real illuminated manuscripts typically make the initial letter much more elaborate. They use ornate fonts and often have patterns or little pictures behind them. Sometimes they even included gold leaf and crushed gems.

You can modify the previous example to draw more interesting initial letter, but that requires you to modify the program’s code in some non-trivial ways. This example makes this process a lot easier. Its version of the DrawIlluminatedText method takes a final parameter that is a method that should be called to draw the background behind each paragraph’s initial letter. The following code shows the method’s new signature.

// Draw an illuminated paragraph.
private void DrawIlluminatedText(Graphics gr,
    float min_lead_width, float min_lead_height,
    Font lead_font, Brush lead_brush, Pen lead_pen,
    Font body_font, Brush body_brush,
    ref RectangleF rect, int paragraph_spacing, string paragraph,
    Action<Graphics, RectangleF> illuminator)
    // Illuminate the lead character.
    RectangleF lead_rect = new RectangleF(
        rect.X, rect.Y, lead_size.Width, lead_size.Height);
    illuminator(gr, lead_rect);

    // Draw the lead character.

Before the method draws the initial character, it calls this illuminator method to draw the background behind that letter.

The following code shows the simplest of this program’s three illuminator methods.

// Fill an illumination box with an image.
private void DrawIlluminationBox3(Graphics gr, RectangleF rectf)
    // Make the rectangle a little smaller.
    const int margin = 5;
    rectf = new RectangleF(
        rectf.X + margin, rectf.Y + margin,
        rectf.Width - 2 * margin, rectf.Height - 2 * margin);

    // Fill the rectangle with an image.
    using (Brush brush =
        new TextureBrush(Properties.Resources.Butterflies))
        gr.FillRectangle(brush, rectf);

    // Outline the rectangle.
    using (Pen pen = new Pen(Color.Blue, 5))
        gr.DrawRectangle(pen, Rectangle.Round(rectf));

This method creates a TextureBrush to fill areas with the image stored in the Butterflies resource and uses that brush to fill the initial character’s drawing area. It subtracts a margin from the area to allow a little extra space between the character and the paragraph’s text.

The code then outlines the area with a 5-pixel-wide blue box.

The program’s other illumination methods are a bit more complicated. Download the program to see how they work.

Now you can easily modify the illumination style by changing the illuminator method that is passed into the DrawIlluminatedText method by the main program.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in fonts, graphics, strings and tagged , , , , , , , , , . Bookmark the permalink.

1 Response to Draw different styles of “illuminated” text in C#

  1. Pingback: Flow blocks around obstacles for document layout in C# - C# HelperC# Helper

Comments are closed.