Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Currently, with Window Presentation Foundation (WPF), it is not easy to display a text string that will follow the path of any given geometry. WPF’s TextEffects provides transformation on individual characters, but trying to use this capability to get the text to follow an arbitrary geometry requires some additional work. Also, TextEffects is not available in Silverlight. With the control provided here, a given text string can be made to display on any 2D WPF geometry. The control can, for example, display text on an EllipseGeometry:

ellipse.PNG

A sample application and source code has been provided.

Background

To display text on a path, the fundamental problem that must be solved is to figure out how to translate and rotate each character of the string to fit the specified geometry. A big help in solving the problem is the GetFlattenedPathGeometry() method provided by the Geometry base class. This method returns a PathGeometry that approximates the geometry by a series of lines (PolyLineSegment and LineSegment). For example, the red lines below depict the flattened path of the path geometry (black):

flattenedpath.PNG

For a good overview of the WPF geometry classes, see MSDN.

The next step in the problem is to take the flattened path geometry and figure out how to transform the characters of the string to follow the flattened path. The figure below depicts the angle that must be calculated so the letter “A” will follow the flattened path. In TextOnAPath, the method ‘GetIntersectionPoints’ in the GeometryHelper class calculates the points along the flattened path where the characters intersect with the path. Knowing the intersection points, the angle (and translation) values can then be calculated.

flattenedpathWithText.PNG

Using the Code

The TextOnAPath control is used just like any normal UIElement in WPF. The XAML below will display the text string along the curved path, centered in a grid:

<Window x:Class="TextOnAPathTestApp.Window1"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:TextOnAPath="clr-namespace:TextOnAPath;assembly=TextOnAPath"
    Title="Test App" Height="500" Width="500">
    <Grid>
    <TextOnAPath:TextOnAPath FontSize="30" DrawPath="True" 
        Text="The quick brown fox jumped over the lazy hen.">
            <TextOnAPath:TextOnAPath.TextPath>
                <PathGeometry Figures="M0,0 C120,361 230.5,276.5 230.5,276.5 
                      L308.5,237.50001 C308.5,237.50001 419.5,179.5002 367.5,265.49993 
                      315.5,351.49966 238.50028,399.49924 238.50028,399.49924 L61.500017,
                      420.49911"/>
            </TextOnAPath:TextOnAPath.TextPath>
        </TextOnAPath:TextOnAPath>
    </Grid>
</Window>

ExampleWindow.PNG

Parameters for the TextOnAPath control:

Points of Interest

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
AnswerText on EllipseGeometry
ANTONELLO72
1:57 21 May '09  
hi,
I must make one animation with several ellipse in a path using PointAnimationUsingKeyFrames.
the problem is that every ellipse must have a text to show the corresponding number.
but the EllipseGeometry object don't have this property.
can someone help me?
General'hen' -> 'dog'
NigelHome
22:26 20 Oct '08  
Silly comment really: the sentence 'The quick ...' needs to end in 'dog' and not 'hen' in order to use the letter 'g' and hence all 26 letters of the alphabet.
GeneralRe: 'hen' -> 'dog'
lneir
6:26 21 Oct '08  
I'll get right on that update Smile My memory from old days of typing classes have faded.
GeneralRe: 'hen' -> 'dog'
Richard Deeming
5:06 22 Oct '08  
It also needs to be "jumps" instead of "jumped".


"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

GeneralRe: 'hen' -> 'dog'
kalme
9:16 22 Oct '08  
NigelHome wrote:
needs to end in 'dog' and not 'hen' in order to use the letter 'g' and hence all 26 letters of the alphabet.

Attention, I can better that :]

Your sentence is missing an 'a', hence the original ends with "...dog's back" Laugh
GeneralRe: 'hen' -> 'dog'
Member 4647162
0:35 5 Nov '08  
hy
GeneralPath Flattening
kevingay
15:42 20 Oct '08  
Your idea of "path flattening" sounds a lot like the Douglas-Peucker algorithm. You can search for that and find a lot of information and various implementations. You can follow the link below and look at the last image on the page. It is animated and will give you a quick idea of how the algorithm works.

http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/algorithm.html[^]
GeneralEasier way
Daniel Larocque
3:56 14 Oct '08  
I have already coded something similar, but instead of creating my own function that calculates the angles, I was using the PathGeometry.GetPointAtFractionLength function. Basically, from an offset, this function will return you the point on your path and its tangeant angle.

Great work by the way!
GeneralRe: Easier way
lneir
6:41 14 Oct '08  
Thanks for feedback.

I looked at using the methods you mention and it should work well. I wanted (in the future) to port the code to Silverlight but the GetPointAtFractionLenght method is currently not available in silverlight. Maybe someone has an implementation for Silverlight?

To port the code in this article to Silverlight I still need a method to flatten the path (as that is not currently available in Silverlight) but I did find an implementation by Charles Petzold...I'll see if he can lone me the code Smile
GeneralRe: Easier way
mike castillo
7:11 21 Oct '08  
Yep. In WPF you get all the methods you need to easily do this (text on a path) with no heavy lifting at all. I needed this functionality in silverlight though, and there were none of the methods mention here (get flattened path, and then getPointAtFraction), so you are on your own. I think I actually implemented both of these functions (doing the heavy lifting) and I will look for the code.
Mike
GeneralRe: Easier way
lneir
7:32 21 Oct '08  
I am currently working on a 'Text on a Path' implementation for silverlight and have implementations for the missing functionality in silverlight...might have something to post in a week or so. I would be interested in seeing your flattened path method. Thks.


Last Updated 30 Oct 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010