Advertisement

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

Started by September 18, 2015 12:43 PM
3 comments, last by Khaiy 9 years, 3 months ago

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

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.

-potential energy is easily made kinetic-

Advertisement


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.

Stephen M. Webb
Professional Free Software Developer

Thanks both for suggestions!

@Bregma,

Could you please put here some example or code spinet? It will be help full for me .

Abhishek

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));
}

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

This topic is closed to new replies.

Advertisement