Calculate text width

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculate text width

Joern.Karthaus

I want to avoid a line break in a table cell.

So my Idea is to calculate the String width of a cell and cut the String
even if its longer.

so it works for my like this  :

   PDFGraphics2D pdfgrafics2d = new PDFGraphics2D(false);
    fontmetrics = pdfgrafics2d.getFontMetrics(new Font(FOPTelefonTable.FONT_FAMILY ,Font.PLAIN,new Integer(FOPTelefonTable.FONT_SIZE_PT).intValue()));

    private String cutName(FontMetrics fontmetrics,String source) {
        String result = source;
        if (fontmetrics.stringWidth(result) <= MAX_NAME_WIDTH_PX) return result;
        while (fontmetrics.stringWidth(result) > MAX_NAME_WIDTH_PX) {
            result = result.substring(0,result.length()-1);
        }
        return result;
    }


But it seems so that the calculatet width from

fontmetrics.stringWidth

ist not the width that i can see in the generatet PDF


Have someone a better Idea to calculate the width ?


Thanks JK





---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Calculate text width

Louis.Masters

JK:

I also ran into similar issues when trying to cut off the overflow in a
cell.  I eventually abandoned the idea and put in hyphenation to get around
our issue.

However, below is an overflow workaround I was toying with that was never
implemented, but may be of some use:

1.  Find the largest character on your PDF output (may entail printing all
of the characters and then measuring).  You only need to do this once
(unless you change your face).
2.  Now, during your processing, multiply the number of characters in your
string by the largest character width from #1.  If it is larger than your
context size, you need to truncate the string.  I was writing a "size
truncater" that would find the max characters by the following method:
            a.  Cut the string in half.
            b.  Measure the first half.  If it is less than your max,
subtract from your max to get what space is left.
            c.  Do an integer divide of what space is left by your largest
character width from #1.  Add this many characters to a.

3.  The resulting string from #2 is now your cell data.

OK, it may not be the best method or even totally foolproof, but it was a
direction I was looking into.

Hope it helps even a little.
-Lou



                                                                                                                                                   
                      Joern.Karthaus@k                                                                                                            
                      ampf.de                  To:      [hidden email]                                                          
                                               cc:                                                                                                
                      05/19/2005 04:45         Subject: Calculate text width                                                                      
                      Please respond                                                                                                              
                      to fop-users                                                                                                                
                                                                                                                                                   
                                                                                                                                                   




I want to avoid a line break in a table cell.

So my Idea is to calculate the String width of a cell and cut the String
even if its longer.

so it works for my like this  :

   PDFGraphics2D pdfgrafics2d = new PDFGraphics2D(false);
    fontmetrics = pdfgrafics2d.getFontMetrics(new
Font(FOPTelefonTable.FONT_FAMILY ,Font.PLAIN,new
Integer(FOPTelefonTable.FONT_SIZE_PT).intValue()));

    private String cutName(FontMetrics fontmetrics,String source) {
        String result = source;
        if (fontmetrics.stringWidth(result) <= MAX_NAME_WIDTH_PX) return
result;
        while (fontmetrics.stringWidth(result) > MAX_NAME_WIDTH_PX) {
            result = result.substring(0,result.length()-1);
        }
        return result;
    }


But it seems so that the calculatet width from

fontmetrics.stringWidth

ist not the width that i can see in the generatet PDF


Have someone a better Idea to calculate the width ?


Thanks JK





---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]







---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Calculate text width

J.Pietschmann
In reply to this post by Joern.Karthaus
[hidden email] wrote:
> But it seems so that the calculatet width from
>
> fontmetrics.stringWidth
>
> ist not the width that i can see in the generatet PDF

FOP doesn't take kerning into account, while Acrobat Reader
does.

> Have someone a better Idea to calculate the width ?

Use a fixed width font :-)

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]