Solved

Print Previews

Posted on 1998-10-19
6
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

740 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