Why doesn't the line break change w/font size??

Hello,
I am trying to print out a simple paragraph (String object) to the printer. I am trying to get the text to wrap properly based on font size. I've search on the posts and can't find the answer.
It looks like the LineBreakMeasurer is only working with default font size? No matter what font I use in the graphics, the string always seems to break in the same column? Any ideas?
Here is a sample of the code I'm using.
Note - the actual printing (draw) is commented out.)
Thanks,
Jim
import java.awt.*;
import java.awt.print.*;
import java.text.*; //for AttributedCharacter
import java.awt.font.*; //for FontRenderContext
public class TestPrint implements Printable {
     String textToPrint;
     AttributedString as;
     AttributedCharacterIterator aci;
     //Font font = new Font("TimesRoman", Font.PLAIN, 16);
     Font font = new Font("TimesRoman", Font.PLAIN, 8);
     public TestPrint(String text) {
          //text represents one paragraph of text
          textToPrint = text;
          as = new AttributedString(textToPrint);
          aci = as.getIterator();
          PrinterJob job = PrinterJob.getPrinterJob();
          job.setPrintable(this);
          if (job.printDialog() ) {
               try {
                    job.print();
               catch(Exception e) {
                    e.printStackTrace();
     public static void main(String args[]) {
          String testMsg = new String("TEST TEST TEST - The quick brown fox jumped over the lazy dogs back. " +
                         "How now brown cow? This will be a fairly long line so I can see how the text will wrap " +
"around on the printer when it prints. This is the end of the test string.\n");
          TestPrint tp = new TestPrint(testMsg);
          System.exit(0);
     public int print(Graphics g, PageFormat pf, int idx) {
          if (idx > 0)
               return Printable.NO_SUCH_PAGE;
          Graphics2D g2d = (Graphics2D)g;
Font fonta = g2d.getFont();
System.out.println("orig graphics font = " + fonta.getName() + " - " + fonta.getSize() );
          g2d.setFont(font);
Font fontb = g2d.getFont();
System.out.println("new graphics font = " + fontb.getName() + " - " + fontb.getSize() );
          float xo = (float)pf.getImageableX();
          float yo = (float)pf.getImageableY();
          int y = font.getSize();
          FontRenderContext frc = g2d.getFontRenderContext();
          //aci is the AttributedCharacterIterator
          LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
          float wrappingWidth = (float)pf.getImageableWidth();
System.out.println("wrappingWidth = " + wrappingWidth);
          while (measurer.getPosition() < aci.getEndIndex() ) {
System.out.println("measurer position = " + measurer.getPosition() + " next offset = " +
                                   measurer.nextOffset(wrappingWidth) );               
               String printLine = textToPrint.substring(measurer.getPosition(), measurer.nextOffset(wrappingWidth) );
               measurer.setPosition(measurer.nextOffset(wrappingWidth));
System.out.println("printLine = " + printLine);               
               //g2d.drawString(printLine, xo, y + yo);
               y += font.getSize();
          //return Printable.PAGE_EXISTS;
          return Printable.NO_SUCH_PAGE;
}

Just for everyone elses reference, below is the modified code that works for me.
<pre>
import java.awt.*;
import java.awt.print.*;
import java.text.*; //for AttributedCharacter
import java.awt.font.*; //for FontRenderContext
import java.util.*;
import javax.swing.*;
public class PrintTest3 implements Printable {
String textToPrint;
AttributedString as;
AttributedCharacterIterator aci;
Font font = new Font("Arial", Font.PLAIN, 18);
//Font font = new Font("TimesRoman", Font.PLAIN, 8);
public PrintTest3(String text) {
//text represents one paragraph of text
textToPrint = text;
as = new AttributedString(textToPrint);
aci = as.getIterator();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
if (job.printDialog() ) {
try {
job.print();
catch(Exception e) {
e.printStackTrace();
public static void main(String args[]) {
String testMsg = new String("TEST TEST TEST - The quick brown fox jumped over the lazy dogs back. " +
"How now brown cow? This will be a fairly long line so I can see how the text will wrap " +
"around on the printer when it prints. This is the end of the test string.\n");
PrintTest3 tp = new PrintTest3(testMsg);
System.exit(0);
public int print(Graphics g, PageFormat pf, int idx) {
if (idx > 0){
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.setFont(font);
as.addAttribute(TextAttribute.FONT, font);
float xo = (float)pf.getImageableX();
float yo = (float)pf.getImageableY();
int y = font.getSize();
//aci is the AttributedCharacterIterator
LineBreakMeasurer measurer = new LineBreakMeasurer(aci, g2d.getFontRenderContext());
float wrappingWidth = (float)pf.getImageableWidth();
while (measurer.getPosition() < aci.getEndIndex() ) {
String printLine = textToPrint.substring(measurer.getPosition(), measurer.nextOffset(wrappingWidth) );
measurer.setPosition(measurer.nextOffset(wrappingWidth));
g2d.drawString(printLine, xo, y + yo);
y += font.getSize();
return Printable.PAGE_EXISTS;
</pre>

Similar Messages

  • Why doesn't my custom ItemRenderer accept Application font size?

    I have a CSS file:
    s|Application {
              fontFamily: Verdana;
              fontSize: 9;
    Why doesn't my custom mx:AdvancedDataGrid  ItemRenterer accept the font size defined in the stylesheet?

    What is the value of fontSize?  Could it be set in some other selector?

  • Why doesn't the numberical keypad changes spaces in Mavericks?

    I upgraded to Mavericks, but when I press control and a number on the keypad, it doesn't take me to the other desktop.
    If I press control and one of the numbers above the letters, it will take me to the other space.

    thats interesting, the product manager is saying it's photoshops fault, and you're saying it's a problem with their metadata modification.
    This is their response to you. It would be fantastic if I could get this sorted. thanks for all your help so far, it is much appreciated.
    So Photoshop has a "double standard" for reading the EXIF Horizontal/Vertical resolution fields, they do it one way with general images and another way with Photoshop generated images. But I'm not saying that they are doing something "not-quite-right" since they are the ones setting the laws
    Unfortunately we don't know how Photoshop reads the resolution for images generated by Photoshop so there is nothing that we can do about this.
    BatchPhoto Forum • View topic - Advanced resize changes not recognised in PhotoshopCS6

  • When changing the default printer in windows vista why doesn't the default printer change in firefox (firefox was running when the default printer was changed)?

    I am not sure whether or not this is a bug or a feature, but I was running firefox on my laptop. I then went to add a new printer and set it as a default printer. When I went back to firefox to print, it did not automatically choose that new default printer as the printer of choice. I then had to choose the new printer manually to print to. This is merely a convenience issue but thought I would bring it to the attention of Mozilla in case.

    Hello bradleesargent
    as i can see the note in the kb article ([https://support.mozilla.org/en-US/kb/how-print-websites#w_print-window-settings Print window settings]) ''The default printer is the Windows one. When a web page is printed with the selected printer, it becomes the new default printer.''
    do you have your previous printer also, or you uninstall the previous printer and then install the new one and make it default ?
    I think firefox must be closed when you set up a new printer. Do you have it open when set up the new printer ?
    thank you

  • Why won't my line spacing change even after I change the # in the leading panel

    why won't my line spacing change even after I change the # in the leading pane

    Is this for text you've already typed? Did you select it first? If that's not it, for PSE 11 and 12 go to the far right of the tool options, click the four-lined square and choose Reset Tool. For PSE 10 and earlier go to the far left options bar, click the tiny triangle (or the tool icon for really old versions) and choose Reset Tool.

  • Why doesn't the rotate gesture on my trackpad behave as it used to for switching between tabs when it works the same with all other programs besides Firefox?

    Why doesn't the rotate gesture on my trackpad behave as it used to for switching between tabs when it works the same with all other programs besides Firefox?
    I changed the about:config to make the rotate gesture move between tabs, and it worked great for well over a year. Now it behaves erratically as described here:
    https://bugzilla.mozilla.org/show_bug.cgi?id=877598
    It used to be very controllable to move one tab over, now it is erratic and moves quickly and to unexpected tabs regardless of how slow I do the rotate gesture. This is the main reason I use Firefox over Chrome and I would like to continue to do so unless there is no fix to this.

    Just set the browser.gesture.twist.threshold to something around 15-25.

  • HT1853 Why doesn't the iTunes show your device?

    Why doesn't the new iTunes show your device? I want to sync new albums..

    when I try and put songs on it it will sit their for hours saying updating files on zachs iphone preparing to update and it never changes and never updates

  • Why doesn't the "zoom in/out" via pinching the trackpad on my mac not work anymore with firefox? It worked before and still works with Safari.

    why doesn't the "zoom in/out" feature by pinching the trackpad on my mac work anymore with firefox? It worked about a month ago, and still works on Safari.

    Some gestures have been removed in Firefox 4 and later.
    You can restore the zoom feature by changing the values of the related prefs on the <b>about:config</b> page.
    browser.gesture.pinch.in -> <b>cmd_fullZoomReduce</b>
    browser.gesture.pinch.in.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.out -> <b>cmd_fullZoomEnlarge</b>
    browser.gesture.pinch.out.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.latched -> <b>false</b>
    To open the <i>about:config</i> page, type <b>about:config</b> in the location (address) bar and press the "<i>Enter</i>" key, just like you type the url of a website to open a website.<br />
    If you see a warning then you can confirm that you want to access that page.<br />
    *Use the Filter bar at to top of the about:config page to locate a preference more easily.
    *Preferences that have been modified show as bold(user set).
    *Preferences can be reset to the default or changed via the right-click context menu.

  • Why doesn't the pwdchangedtime attribute get created?

    In OID, I set up a password policy where passwords never expire. Then I create some users. Then, I change the password policy so passwords expire in 30 days. But, the passwords never expire for the users I previously created. I think it's because the pwdchangedtime attribute never got created. But, if I change a user's password, then the pwdchangedtime attribute gets created. Why doesn't the attribute get created when I change the password policy?

    which version are you using?
    regards,
    --Olaf                                                                                                                                                                                                                   

  • Why doesn't the result look right when comparing?

    Why doesn't the result look right even I used higlight execution to check true and false and found to be correct right?
    Attachments:
    comparenresult.zip ‏27 KB

    At first I was not sure what you were asking. After looking more closely, I think I can answer your question.
    First of all, you need to initialize your shift register. This is the array of Boolean results. The first time through it works OK, but subsequent runs are also added on instead of replacing the results, even though you used a local to "clear" the array first.
    You are adding 2 to the iterations and indexing on the new number. This is a so that you skip the first two "bad" values. Therefore, you only have 13 data points and should only be running the FOR loop 13 times. Change the FOR loop count constant to 13 (or get the array size and subtract 2 and wire it in if the number of tests is variable).
    Also, there is an 'In Range a
    nd Coerce' node that will perform the two tests and the AND all in one node. This was not an error, of course, but it does make it a bit easier to read.
    Bob Young - Test Engineer - Lapsed Certified LabVIEW Developer
    DISTek Integration, Inc. - NI Alliance Member
    mailto:[email protected]
    Attachments:
    fixed_test4a.vi ‏73 KB

  • Why doesn't the iPad have a guest screen like the PC for extra security?

    I just want to start off first by saying I just purchased my iPad about six months ago and I'm really enjoying it,but if you're like me,I believe in protecting my privacy.
    I don't like to turn down any of friends or coworkers when I'm asked to use my iPad,but when you have such things as your personal pictures,movies,music,business documents,etc...you'd like to keep them from prying eyes. I thought that with the new iOS 7 update,that issue would've been fixed, but I was wrong. I'm hoping Apple would correct this problem with the next iOS update,it would make a lot of loyal Apple consumers &amp; anyone who's new to purchasing the iPad for the first time very happy to know that feature is available.
    Why doesn't the iPad offer a guest screen?

    Tell Apple at the link below.
    http://www.apple.com/feedback/ipad.html

  • Why doesn't the CC app work properly?

    I've been using Creative Cloud for some time now and am really loving it, however I must say it is a source of annoyance that the Creative Cloud app just doesn't work properly.
    Firstly, I used it initially to download all my apps, then after an update those apps no longer appeared in the "Apps" list. Now only those that have been installed since that update appear in this list. Why doesn't the "Apps" list display all of the Adobe CC apps that I have installed on my computer?
    Why does it keep reinstalling fonts? I'm always getting "Lush Script Regular" and other fonts being added.
    It regularly comes up telling me that Dreamweaver and Premiere Pro need to be updated, but always I get an error, then the update request disappears.
    I'm regularly getting 4 files that it can't sync - even though they are the same files as others in the folder. If nothing else, why can't I say "Great, got the message". Also, why is the error message just "due to server error" with no advice on how to resolve it? Why is there a server error on only these 4 files?
    It seems to me there are still some teething issues with the CC app, but what I don't understand is why they aren't being resolved much faster, given that this is the portal to the service.
    Of course, given Adobe I doubt I'll get satisfactory answers – a little jaded with their responses. Anyway, thought I'd at least post the questions.  

    Questapo I am sorry you have been facing difficulties with your Creative Cloud experience.  I don't believe it will be possible to address all of your concerns in one discussion.  I would not be surprised if at least three of the issues are related to the same root cause.
    First in order for the applications to be visible are you regularly needing to delete the OPM.db file?  You can find this listed as solution 2 in CC desktop lists applications as "Up to Date" when not installed - http://helpx.adobe.com/creative-cloud/kb/aam-lists-removed-apps-date.html.

  • Why Doesn't the XIRR function work?

    In Excel if you have 5 records from a1:b5 then XIRR looks like this:
    XIRR(A1:A5, B1:B5) you get a nice neat answer like .35
    Here is a version that works in Crystal that would require manual entry of any new quarters numbers and dates-- and if you plug this into Crystal it works:
    (XIRR([1000000,-100000,-100000,-100000,-100000,10277.49,-100000], [DateValue(1999,2,1),DateValue(1999,3,1),DateValue(1999,6,1), DateValue(1999,12,1),DateValue(2000,3,1),DateValue(2000,6,1),DateValue(2000,9,1)]))*.100
    You do get a nice answer of something like .035.  But this is all done manually and I need it to function automatically when the report is refreshed.
    So you have a range for the currency and a range for the date fields. I need to simulate this in crystal. Crystal has an XIRR function, and with the arrays, I should be able to fill in the range, but am having difficulty getting it to work.
    After building the arrays, I made another formula to combine the arrays in the XIRR formula.
    I am down to this error now after getting this formula this far  --
    "Numerical method did not converge; try another value for guess."
    Here's where I am with the formula, although they don't want to use the guess part of the XIRR function - they just want to use the XIRR(number/currency, date).  Maybe you could pass this on too?:
    /{@Reset} for the group header (NOT using a repeated group header):
    whileprintingrecords;
    numbervar array x := 0;
    datevar array y := date(0,0,0);
    numbervar i := 0;
    numbervar j := 0;
    //{@accum} for the detail section:
    whileprintingrecords;
    numbervar array x;
    datevar array y;
    numbervar i := i + 1;
    numbervar j := count({table.groupfield},{table.groupfield});
    if i <= j then (
    redim preserve x[j];
    redim preserve y[j];
    x<i> := tonumber({table.currency});
    y<i> := datevalue({table.datetime})
    //{@xirr calc} to be placed in the group footer:
    whileprintingrecords;
    numbervar array x;
    datevar array y;
    xirr(x,y)
    The array works correctly.  So why do I get that error and why doesn't the XIRR formula work like they say it should?  Has anyone used this successfully in Crystal--maybe you could shed some light?
    Thanks!

    Hi,
    I am receiving that same error when the last item in the array is 0, otherwise all works perfectly.
    When I run the same group of numbers and dates in Excel it returns without issue.
    -265500.00,-690000.00,-570000.00,16814.25,-855000.00,-619500.00,55293.46,30411.40,15183.76,  0.00
    01-25-2007,03-06-2007,05-02-2007,06-29-2007,08-01-2007,08-24-2007,09-17-2007,03-14-2008,05-28-2008,03-31-2010
    =XIRR(A2:J2,A1:J1,-0.1)
    Is there a known bug in Crystal's XIRR function when the last value is 0? 
    Or a hot-fix that will repair this?
    Thanks in advance,
    Gary
    PS. I am using Crystal XI Product Version: 11.0.0.2495

  • Why doesn't the location information in an iCal meeting show up on my iPhone, when it does show up in iCal?

    Why doesn't the location information show up on my iPhone 4S calendar when I accept an invitation to a meeting?
    The other information such as the date, time and invitees all show up on the iPhone, while in iCal all the information including the Location field is showing up.

    If it won't charge you probably need to try a different dock to usb cable.  More information on troubshooting this is available here:  http://support.apple.com/kb/TS1591.

  • Why doesn't the weather icon display the current temperature in your "local" city?

    Why doesn't the current temperature display in the weather icon on the home screen?

    The application works and I do have location-based services on. I'm just wondering why the icon itself does not show the current temperature. Sort of how they upgraded the clock after show the current time.

Maybe you are looking for

  • Only German Language fails in Portal view of SUS app

    Hello, We are running SAP's SUS (Supplier Self Services) through the portal.  Everything works fine in any language except German.  German works when using SUS through the backend.  When a user with the German Language set opens the Self Services tab

  • How to gve the colors to the TOP-OF-PAGE in ALV Grid Disply..

    i want to give 3 different colors in the top-of-page(there are three different lines each line should disply in different colors)...   plaese tell me how can i do that...........

  • How do i auto update my library

    i download alot of songs not always from i-tunes and it gets annoying having to look through my library and my music folder just to see whats missing and when you have over 2k songs it takes forever so is there any way to add all missing songs to my

  • Html and flash website

    im pretty sure this question has been frequently asked here. I have been searching high and low, from google to bing to find help with this question. Im sort of a rooky by the way. I have a website thats created fully from flash professional and been

  • Tiger 10.4.6 does cd does not launch

    Hello. The newly cd received from Apple Store simply does not launch. How come. Thank you for your assistance. Donald