Solved

Print Previews

Posted on 1998-10-19
6
280 Views
Last Modified: 2013-12-03
I'm writing a preview report program as an add on to a DOS program.  My delima
is how to calculate font sizes for the report data.  Im using Java2D for my
printing capabilities (the preview module wont actually be released for 6
months or so)
Is there a standard way to calculate what the font sizes should be given a
report width (whether the user wants to print landscape/portrait and 8.5 X 11/
8.5/14 etc)
What Ive started to do is pretty much go through a while loop and start with a
big font and test to see if the font works for how long the line is and decrement
the font until it does work.
This would be ok for fixed width fonts but I would like to use variable width fonts
and be able to still line up the columns for financial data.
I also normally deal in cpi (characters per inch) so I would like to know how the
point size system works (how it is calculated...especially with variable width fonts)

Ive done some research on JTable and pretty much found that it is hard print anything
unless you print the component itself (including the GUI itself.. not just the data)
I need to show the report onscreen probably using the same Graphics object that I
calculated given the PageFormat.. I think I can handle that if I only knew how to
figure out the calculations.

I dont necessarily need code.. just a good description of how things work

I would appreciate any ideas anyone has for this.
0
Comment
Question by:conick
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1225941
How about using

g.setFont(new Font(face, style, point));
g.getFontMetrics().stringWidth(s);

If in s you have the String you are going to print, and g is the printing Graphics?

From my experience with this I know that Java is quite immature with the printing. Anyway, what I suggest should return at least an approximation how many points the string will take.

Tell me if this works.


0
 
LVL 7

Author Comment

by:conick
ID: 1225942
Possibly I didnt explain myself very well...
I do have the string (and consequently its width) also I have the width of the page (I can get that from PageFormat with getImageableX())

With those two values I can get the font size of a monospaced font that will work...
I can use a while loop similar to:

int fontSize=25;
int maxStringWidth=pf.getImageableX();
FontMetrics currFontMetrics= currFont.getFontMetrics();
currStringWidth= currFontMetrics.stringWidth(s);
         
while (currStringWidth >= maxStringWidth)  {  
               fontSize -= 2;
               currFont= new Font("SansSerif",Font.BOLD,fontSize);
               currFontMetrics= this.getFontMetrics(currFont);
                currStringWidth=currFontMetrics.stringWidth(s);
}

That will approximate the upper bound of the font size depending on the length of the string.
I would have to use the longest string in the report (all the reports will be of a tabular type so
approximately the same width)

But I still have a couple of problems:

1) I would like to use a Serif or SansSerif font (not monospaced).  If I use a font that is not monospaced I cant predict how the columns will appear (they may not be right underneath the heading (and for financial data lining up the decimal points would be a nightmare).  If necessary I can use a monospaced font for the data and have the heading Serif.

2)  I would like to know how the point sizes in most graphical based fonts are calculated (not cpi).
cpi (characters per inch) are bigger as the numbers get lower (10 cpi is bigger than 12 cpi)
Obviously graphical fonts do not work like that (the bigger the number the bigger the font)

3) How is everyone else going about this?  This should be a fairly common (although new) practice.  With the new Java2D printing capabilities (bundled with JDK1.2) printing CAN be done with java.  I dont even know how its done in windows programming so Im kind of at a disadvantage here.

After reading my initial question I can see how it could be confusing with where Im at in the process.  I hope this comment helps.

Thanks  
         
0
 
LVL 7

Author Comment

by:conick
ID: 1225943
The line
FontMetrics currFontMetrics= currFont.getFontMetrics();
should be
FontMetrics currFontMetrics= this.getFontMetrics(currFont);
in the code above


Thanks again
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Expert Comment

by:diakov
ID: 1225944
1) Doesn't the max row length calculation work with the Serif and SansSerif fonts? Quote from the docs "...The advance width is the sum of the advance widths of each of the characters in the array..." This is when you use FontMetrics of the printing Graphics.

2) I point in the fonts is meant to be 1/72 of inch

3) http://developer.java.sun.com/developer/onlineTraining/Graphics/2DText/style.html

Isuppose this link might be usefull to you.
0
 
LVL 7

Author Comment

by:conick
ID: 1225945

1) It would work for the fonts however I couldnt predict where each data element would start with a nonmonospaced font.  Say the line is 80 char long, the 60th char would probably be on a different x coordinate in one line than it would be in the next line in a Serif or SansSerif font.  In a monospaced font the 60th character would always be on the same x coordinate.
What I could probably do is break the strings down into individual elements and calculate where each column would start and then dump the "substring" into that position.  In fact I'll just write the source file that way and use StringTokenizer()... Anyway I think that question is taken care of.

2) Ah HA ... thats why the docs always measure things in 1/72 of an inch (i thought that was kinda strange)... mystery solved!

3)I completely forgot about the online help at JDC.. I'll check into that.  It looks as though I could get quite a few ideas from that.

Well I think you answered all my current questions (and probably some I would have asked in the future)

Thanks
Leave an answer and I'll give you a well deserved A
0
 
LVL 8

Accepted Solution

by:
diakov earned 120 total points
ID: 1225946
Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
erros on link  checking 2 43
wordcount challenge 11 76
Java asynchronous logging 4 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now