armadillu / ofxFontStash

Easy (and fast) unicode string rendering addon for OpenFrameworks. FontStash is made by Andreas Krinke and Mikko Mononen
http://uri.cat/
91 stars 42 forks source link

Aligned option for multiple line box? #20

Open harryhow opened 8 years ago

harryhow commented 8 years ago

Hi,

Just wondering if there's any option to draw multiple line with alignment? (e.g. center, left, right )

It might be an useful feature.

Thanks!

moebiussurfing commented 1 year ago

Left alignment and centered paragraphs work well using font.drawMultiLineColumn(. Maybe ofxFontStash2 allows right alignment easely.

BTW I found a workaround to draw a column paragraph right aligned here with ofxFontStash. What we have to do is to break the text lines adding \n previously. That can be done using the bbox getter without drawing, and the to draw the new string text with font.drawmMultiLineColumn(:

string _str = "dgsdfgsdg..."; // long text without using '\n' to break lines.
float _w = 500; // variable width with adaptative text lines

if (bRightAlign) // right aligned
{
    // add '\n\ to _str
    {
        auto __bbox = font.drawMultiLineColumn(
            _str,       /*string*/
            _size,      /*size*/
            _x, _y,     /*where*/
            MAX(10, _w), /*column width*/
            _numLines,  /*get back the number of lines*/
            true,       /* if true, we wont draw (just get bbox back) */
            20,         /* max number of lines to draw, crop after that */
            true,       /*get the final text formatting (by adding \n's) in the supplied string;
                        BE ARWARE that using TRUE in here will modify your supplied string! */
            &_wordsWereCropped, /* this bool will b set to true if the box was to small to fit all text*/
            false       /*centered*/
        );
    }

    // draw
    auto _bbox = font.drawMultiLine(
        _str,       /*string*/
        _size,      /*size*/
        _x, _y,     /*where*/
        OF_ALIGN_HORZ_RIGHT,
        MAX(10, _w) /*column width*/
    );
}