Sign in to follow this  

How to format the text by adding word spacing and letter spacing?

This topic is 812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I am very very new in Graphics programming and on this forum.

 

I have one following query and I hope this is right place to ask. If not please suggest me.

 

I am currenlty developing one Windows Player which render and animate  the Text (2d) .

 

I am using Slimdx, DirectWrite and C# to develope that. I am reading all the format properties like fontsize and line, height, word spacing and letter spacing from xml file provided and trying to apply it on text by using TextFormat and Layout. But here these components not provide more properties like Line height and word spacing. I have also tried to use LineMetrics but it also not that much usefull.

 

So this  is now quite difficult for me to render the text here. 

 

How I can do this by using slimd dx and C#?

 

Thanks

Abhishek

Share this post


Link to post
Share on other sites

I think the thing you want to look into in something called kerning.  I don't know anything about slimdx or directwrite so I can't help you but if my memory is on point in regards to font rendering kerning and kerning values is what you need to look for.

Share this post


Link to post
Share on other sites


I think the thing you want to look into in something called kerning.

Kerning is the art of spacing individual glyphs such that they appear to have a pleasing and even spacing within a word and usually just involves the pairwise overlap of individual glyphs.  I don't think it's what he's looking for here.

 

The art of wordflow and textual layout has many heuristics developed over the last few thousand years but for the most part you're going to need to calculate the text box extents from each word (calculated from the font metrics and, yes, kerning pair rules) and then do a standard 2D packing of those text boxes into your target layout.  DX is just used for rendering the pixels generated by the layed out text and fonts.

Share this post


Link to post
Share on other sites

I wrote this with SFML, not SlimDX, so the exact method calls won't be the same but the measurement should be similar. This is a pretty old project of mine, so don't assume that anything you see is an especially good practice. I added a couple of comments to explain the most important parts, but if anything is unclear please ask about it.


public void ParseText(string inputText)
{
    // This line breaks the input apart so that each segment can be measured individually
    string[] individualStrings = inputText.Split();
 
    SFML.Graphics.Text outputText = new SFML.Graphics.Text();
    SFML.Graphics.Text peekText = new SFML.Graphics.Text();
 
    // These lines set the font size
    outputText.CharacterSize = 20;
    peekText.CharacterSize = 20;
 
    StringBuilder sb = new StringBuilder();
 
    int currentString = 0;
 
    while (currentString < individualStrings.Count())
    {
        sb.Append(individualStrings[currentString]).Append(" ");
 
        peekText.DisplayedString = sb.ToString();
 
        // This is the key section. It measures the size of the next individual string to see if it will fit on the current line
        // If it will, it gets added to the current line of text
        if (peekText.GetLocalBounds().Width < maximumLineLength)
        {
            outputText.DisplayedString = peekText.DisplayedString;
 
            currentString++;
        }
 
        // When the line is as long as it can get, it gets added to a buffer. Later, the text buffer will display the lines one at a time.
        else
        {
            textBuffer.Add(new SFML.Graphics.Text(outputText.DisplayedString, SFML.Graphics.Font.DefaultFont, 20));
 
            sb.Clear();
        }
    }
 
            textBuffer.Add(new SFML.Graphics.Text(outputText.DisplayedString, SFML.Graphics.Font.DefaultFont, 20));
}
Edited by Khaiy

Share this post


Link to post
Share on other sites

This topic is 812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this