Re: Private message regarding: [libharu] TextRect and GetCurrentTextPos

146 views
Skip to first unread message

Bill Horger

unread,
Apr 21, 2016, 5:32:16 PM4/21/16
to LibHaru Group
To do this manually requires knowledge of the absolute position of the box and the use of font metrics.

To get the process that works for you, I suggest you get some graph paper and play around using the information below.

You need to know the absolute height of the font for a given "font size". For example, a font presented at 72 points is NOT necessarily 1" in height. The metrics for the font are complicated and I am no expert. I use a fixed pitch font to make my life easier. Both the height and width are easily measured. Print out capital letters and a few lower case with descenders (g,q,p,...) and see what works as far as scalings and line-to-line spacing.

Use the libHaru call
    width = HPDF_Page_TextWidth  (page,p); // in default measurement units (points)

where p is pointer to your text. While it does not give you the height, you might be able to use the font calls to get the basics of the metrics (https://github.com/libharu/libharu/wiki/API%3A-Fonts)

Once you have this, you can "estimate" where the text break should be, then go backward or forward in your original string and use the width returned value to "fit" the text in. Then, break the string, go to the next line in the box and repeat.

If you have a fixed size box, then you might have to change the font size to get it all to fix.

The easiest problem would be one where the text can be folded again and again without a limit (i.e. an open-bottomed box). Once the entire row of text segments can be placed, then draw the boxes accordingly and fit the text in. If it won't fit on the page, then you'd generate a new page (in the simplest of cases) and place the boxes and text there.

I've never used foreign multi-byte fonts (i.e. Chinese, Korean,...), so I am of no help there

I only place text in absolute terms (to produce plots and graphs), or to display consecutive lines of text on a page. I don't use TextRect



chahat bhatia

unread,
Apr 26, 2016, 7:48:35 AM4/26/16
to libHaru
Hey i wanted to know how to  break the text as in how to word wrap. i have written a function defined by me but it seems to fail after a certain point .

Here's the code snippet :

void SplitString(int iLength, string strInput, string& strOutput)
{

    int iSubstringsCnt;
    int iAddedCnt;


    iSubstringsCnt = strInput.length() / iLength;
    iAddedCnt = iSubstringsCnt / iLength;
    cout<<iSubstringsCnt<<endl;
    cout<<iAddedCnt<<endl;

    cout<<strInput.length()<<endl;
    for (int iCnt = 0; iCnt <= iSubstringsCnt+ iAddedCnt; iCnt++)
    {
            if (0 == iCnt)
                    continue;
                strInput.insert((iCnt * iLength)+(iCnt-1) , " ");
    }

    strOutput= strInput;

}
  • iLength: The length after which I want to split.
  • iAddedCnt: The count of the string after I added space after a few characters.


Anything better than this which could possibly work ? which tells me when the width is exceeded and adds a space after a certain point.
Reply all
Reply to author
Forward
0 new messages