< prev index next >

src/java.desktop/share/classes/sun/swing/SwingUtilities2.java

Print this page




 507         // c may be null
 508 
 509         // All non-editable widgets that draw strings call into this
 510         // methods.  By non-editable that means widgets like JLabel, JButton
 511         // but NOT JTextComponents.
 512         if ( text == null || text.length() <= 0 ) { //no need to paint empty strings
 513             return;
 514         }
 515         if (isPrinting(g)) {
 516             Graphics2D g2d = getGraphics2D(g);
 517             if (g2d != null) {
 518                 /* The printed text must scale linearly with the UI.
 519                  * Calculate the width on screen, obtain a TextLayout with
 520                  * advances for the printer graphics FRC, and then justify
 521                  * it to fit in the screen width. This distributes the spacing
 522                  * more evenly than directly laying out to the screen advances.
 523                  */
 524                 String trimmedText = trimTrailingSpaces(text);
 525                 if (!trimmedText.isEmpty()) {
 526                     float screenWidth = (float) g2d.getFont().getStringBounds
 527                             (trimmedText, getFontRenderContext(c)).getWidth();
 528                     TextLayout layout = createTextLayout(c, text, g2d.getFont(),
 529                                                        g2d.getFontRenderContext());
 530 
 531                     // If text fits the screenWidth, then do not need to justify
 532                     if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
 533                                             trimmedText) > screenWidth) {
 534                         layout = layout.getJustifiedLayout(screenWidth);
 535                     }
 536                     /* Use alternate print color if specified */
 537                     Color col = g2d.getColor();
 538                     if (col instanceof PrintColorUIResource) {
 539                         g2d.setColor(((PrintColorUIResource)col).getPrintColor());
 540                     }
 541 
 542                     layout.draw(g2d, x, y);
 543 
 544                     g2d.setColor(col);
 545                 }
 546 
 547                 return;
 548             }
 549         }
 550 
 551         // If we get here we're not printing
 552         if (g instanceof Graphics2D) {
 553             Graphics2D g2 = (Graphics2D)g;
 554 
 555             boolean needsTextLayout = ((c != null) &&


 664                     syncCharsBuffer(text);
 665                     needsTextLayout =
 666                         isComplexLayout(charsBuffer, 0, textLength);
 667                 }
 668             }
 669             if (!needsTextLayout) {
 670                 FontMetrics fm = g.getFontMetrics();
 671                 underlineRectX = x +
 672                     SwingUtilities2.stringWidth(c,fm,
 673                                         text.substring(0,underlinedIndex));
 674                 underlineRectWidth = fm.charWidth(text.
 675                                                   charAt(underlinedIndex));
 676             } else {
 677                 Graphics2D g2d = getGraphics2D(g);
 678                 if (g2d != null) {
 679                     TextLayout layout =
 680                         createTextLayout(c, text, g2d.getFont(),
 681                                        g2d.getFontRenderContext());
 682                     if (isPrinting) {
 683                         float screenWidth = (float)g2d.getFont().
 684                             getStringBounds(text, getFontRenderContext(c)).getWidth();
 685                         // If text fits the screenWidth, then do not need to justify
 686                         if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
 687                                                         text) > screenWidth) {
 688                             layout = layout.getJustifiedLayout(screenWidth);
 689                         }
 690                     }
 691                     TextHitInfo leading =
 692                         TextHitInfo.leading(underlinedIndex);
 693                     TextHitInfo trailing =
 694                         TextHitInfo.trailing(underlinedIndex);
 695                     Shape shape =
 696                         layout.getVisualHighlightShape(leading, trailing);
 697                     Rectangle rect = shape.getBounds();
 698                     underlineRectX = x + rect.x;
 699                     underlineRectWidth = rect.width;
 700                 }
 701             }
 702             g.fillRect((int) underlineRectX, (int) underlineRectY + 1,
 703                        underlineRectWidth, underlineRectHeight);
 704         }
 705     }
 706 
 707 
 708     /**
 709      * A variation of locationToIndex() which only returns an index if the


 854         float nextX = x + getFontCharsWidth(data, offset, length,
 855                                             getFontMetrics(c, g),
 856                                             useFPAPI);
 857         if (isPrinting(g)) {
 858             Graphics2D g2d = getGraphics2D(g);
 859             if (g2d != null) {
 860                 FontRenderContext deviceFontRenderContext = g2d.
 861                     getFontRenderContext();
 862                 FontRenderContext frc = getFontRenderContext(c);
 863                 if (frc != null &&
 864                     !isFontRenderContextPrintCompatible
 865                     (deviceFontRenderContext, frc)) {
 866 
 867                     String text = new String(data, offset, length);
 868                     TextLayout layout = new TextLayout(text, g2d.getFont(),
 869                                     deviceFontRenderContext);
 870                     String trimmedText = trimTrailingSpaces(text);
 871                     if (!trimmedText.isEmpty()) {
 872                         float screenWidth = (float)g2d.getFont().
 873                             getStringBounds(trimmedText, frc).getWidth();
 874                         // If text fits the screenWidth, then do not need to justify
 875                         if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(),
 876                                                 trimmedText) > screenWidth) {
 877                             layout = layout.getJustifiedLayout(screenWidth);
 878                         }
 879 
 880                         /* Use alternate print color if specified */
 881                         Color col = g2d.getColor();
 882                         if (col instanceof PrintColorUIResource) {
 883                             g2d.setColor(((PrintColorUIResource)col).getPrintColor());
 884                         }
 885 
 886                         layout.draw(g2d,x,y);
 887 
 888                         g2d.setColor(col);
 889                     }
 890 
 891                     return nextX;
 892                 }
 893             }
 894         }
 895         // Assume we're not printing if we get here, or that we are invoked
 896         // via Swing text printing which is laid out for the printer.
 897         Object aaHint = (c == null)
 898                             ? null




 507         // c may be null
 508 
 509         // All non-editable widgets that draw strings call into this
 510         // methods.  By non-editable that means widgets like JLabel, JButton
 511         // but NOT JTextComponents.
 512         if ( text == null || text.length() <= 0 ) { //no need to paint empty strings
 513             return;
 514         }
 515         if (isPrinting(g)) {
 516             Graphics2D g2d = getGraphics2D(g);
 517             if (g2d != null) {
 518                 /* The printed text must scale linearly with the UI.
 519                  * Calculate the width on screen, obtain a TextLayout with
 520                  * advances for the printer graphics FRC, and then justify
 521                  * it to fit in the screen width. This distributes the spacing
 522                  * more evenly than directly laying out to the screen advances.
 523                  */
 524                 String trimmedText = trimTrailingSpaces(text);
 525                 if (!trimmedText.isEmpty()) {
 526                     float screenWidth = (float) g2d.getFont().getStringBounds
 527                             (trimmedText, DEFAULT_FRC).getWidth();
 528                     TextLayout layout = createTextLayout(c, text, g2d.getFont(),
 529                                                        g2d.getFontRenderContext());
 530 
 531                     layout = layout.getJustifiedLayout(screenWidth);




 532                     /* Use alternate print color if specified */
 533                     Color col = g2d.getColor();
 534                     if (col instanceof PrintColorUIResource) {
 535                         g2d.setColor(((PrintColorUIResource)col).getPrintColor());
 536                     }
 537 
 538                     layout.draw(g2d, x, y);
 539 
 540                     g2d.setColor(col);
 541                 }
 542 
 543                 return;
 544             }
 545         }
 546 
 547         // If we get here we're not printing
 548         if (g instanceof Graphics2D) {
 549             Graphics2D g2 = (Graphics2D)g;
 550 
 551             boolean needsTextLayout = ((c != null) &&


 660                     syncCharsBuffer(text);
 661                     needsTextLayout =
 662                         isComplexLayout(charsBuffer, 0, textLength);
 663                 }
 664             }
 665             if (!needsTextLayout) {
 666                 FontMetrics fm = g.getFontMetrics();
 667                 underlineRectX = x +
 668                     SwingUtilities2.stringWidth(c,fm,
 669                                         text.substring(0,underlinedIndex));
 670                 underlineRectWidth = fm.charWidth(text.
 671                                                   charAt(underlinedIndex));
 672             } else {
 673                 Graphics2D g2d = getGraphics2D(g);
 674                 if (g2d != null) {
 675                     TextLayout layout =
 676                         createTextLayout(c, text, g2d.getFont(),
 677                                        g2d.getFontRenderContext());
 678                     if (isPrinting) {
 679                         float screenWidth = (float)g2d.getFont().
 680                             getStringBounds(text, DEFAULT_FRC).getWidth();
 681                         layout = layout.getJustifiedLayout(screenWidth);




 682                     }
 683                     TextHitInfo leading =
 684                         TextHitInfo.leading(underlinedIndex);
 685                     TextHitInfo trailing =
 686                         TextHitInfo.trailing(underlinedIndex);
 687                     Shape shape =
 688                         layout.getVisualHighlightShape(leading, trailing);
 689                     Rectangle rect = shape.getBounds();
 690                     underlineRectX = x + rect.x;
 691                     underlineRectWidth = rect.width;
 692                 }
 693             }
 694             g.fillRect((int) underlineRectX, (int) underlineRectY + 1,
 695                        underlineRectWidth, underlineRectHeight);
 696         }
 697     }
 698 
 699 
 700     /**
 701      * A variation of locationToIndex() which only returns an index if the


 846         float nextX = x + getFontCharsWidth(data, offset, length,
 847                                             getFontMetrics(c, g),
 848                                             useFPAPI);
 849         if (isPrinting(g)) {
 850             Graphics2D g2d = getGraphics2D(g);
 851             if (g2d != null) {
 852                 FontRenderContext deviceFontRenderContext = g2d.
 853                     getFontRenderContext();
 854                 FontRenderContext frc = getFontRenderContext(c);
 855                 if (frc != null &&
 856                     !isFontRenderContextPrintCompatible
 857                     (deviceFontRenderContext, frc)) {
 858 
 859                     String text = new String(data, offset, length);
 860                     TextLayout layout = new TextLayout(text, g2d.getFont(),
 861                                     deviceFontRenderContext);
 862                     String trimmedText = trimTrailingSpaces(text);
 863                     if (!trimmedText.isEmpty()) {
 864                         float screenWidth = (float)g2d.getFont().
 865                             getStringBounds(trimmedText, frc).getWidth();
 866                         layout = layout.getJustifiedLayout(screenWidth);




 867 
 868                         /* Use alternate print color if specified */
 869                         Color col = g2d.getColor();
 870                         if (col instanceof PrintColorUIResource) {
 871                             g2d.setColor(((PrintColorUIResource)col).getPrintColor());
 872                         }
 873 
 874                         layout.draw(g2d,x,y);
 875 
 876                         g2d.setColor(col);
 877                     }
 878 
 879                     return nextX;
 880                 }
 881             }
 882         }
 883         // Assume we're not printing if we get here, or that we are invoked
 884         // via Swing text printing which is laid out for the printer.
 885         Object aaHint = (c == null)
 886                             ? null


< prev index next >