(X) Hide this Watch the recordings of our recent webinars: Switching on the Cloud for Silverlight by Gill Cleeren and WCF RIA Services Validation by Brian Noyes.
Sign up for the upcoming SilverlightShow webinars
Skip Navigation LinksHome / Articles / View Article

Silverlight: Convert Text to Path

+ Add to SilverlightShow Favorites
3 comments   /   aggregated from Laurence Moroney's Web.NEXT Blog on May 23, 2007  /  original article
(0 votes)
Categories: Learn , Tutorials

The WPF APIs provide a FormattedText object that allows you to export its contents as a Geometry, which in turn allows you to generate its contents in the XAML Path Mini Language. 

Here's an example of a Web service that takes in the text, typeface, size and other parameters, loads them into a FormattedText, and uses this to generate a Path as a result.

You can then take the returned value from this Web service and load it into the Data attribute of a Path to get the desired text rendered in Silverlight. This will allow you to have any text from any font rendered in your Silverlight page.

This will allow you to:

  • Provide text from unsupported fonts
  • Provide text from languages other than English

Here's the Source Code for the Web Service:

 

[WebMethod]
public string Text2Path(String strText, string strCulture, bool LtoR, string strTypeFace, int nSize)
{
    // Set up the Culture
    if (strCulture == "")
        strCulture = "en-us"
    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(strCulture);

    // Set up the flow direction
    System.Windows.FlowDirection fd;
    if (LtoR)
        fd = FlowDirection.LeftToRight;
    else
        fd = FlowDirection.RightToLeft;

    // Set up the font family from the parameter
    FontFamily ff = new FontFamily(strTypeFace);

    // Create the new typeface
    System.Windows.Media.Typeface tf = new System.Windows.Media.Typeface(ff,  

        FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);

    // Create a formatted text object from the text,

    // culture, flowdirection, typeface, size and black

    FormattedText t = new FormattedText(strText, ci, fd, tf, nSize,    

        System.Windows.Media.Brushes.Black);

    // Build a Geometry out of this
    Geometry g = t.BuildGeometry(new Point(0, 0));

    // Get the Path info from the geometry
    PathGeometry p = g.GetFlattenedPathGeometry();

    // Return the path info
    return p.ToString();

}

Here's an example of calling this Web service using some Korean Text:

 

This Web Service returns a String containing the Path Mini Language:

 

 

In Silverlight you use the tag to define a path. It takes a 'Data' attribute which takes a string in the path mini language format, so all you ahve to do is set it to the contents of the value returned from the Web service.

Here's the Korean text from earlier, being rendered in Silverlight using this path.

 

In the next installment of this blog I'll go into adding a text box to the page, overlaying it on the Silverlight content using windowless mode. It will support text input using an IME, and then use ASP.NET AJAX to call this service, get the response and load it into the path.

Share


Comments

Comments RSS RSS
  • RE: Silverlight: Convert Text to Path  

    posted by Beketata on Mar 30, 2010 14:57

    Change the next line

    // Return the path info
    return p.ToString();

    with the

    // Return the path info
    return p.ToString( CultureInfo.InvariantCulture );
  • RE: Silverlight: Convert Text to Path  

    posted by Thomas on Apr 29, 2010 18:09

    WCF does not allow System.Windows.Media and also nothing for FormattedText.

    I tried adding WCF and your code.

  • RE: Silverlight: Convert Text to Path  

    posted by Thomas on Apr 29, 2010 18:09

    WCF does not allow System.Windows.Media and also nothing for FormattedText.

    I tried adding WCF and your code.

Add Comment

 
 

   
  
  
   
Please add 4 and 4 and type the answer here: