~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/java2d/GraphicContext.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: GraphicContext.java 1345683 2012-06-03 14:50:33Z gadams $ */
 
18
/* $Id: GraphicContext.java 1610846 2014-07-15 20:44:18Z vhennebert $ */
19
19
 
20
20
package org.apache.xmlgraphics.java2d;
21
21
 
52
52
 * + RenderingHints <br>
53
53
 * + AffineTransform <br>
54
54
 *
55
 
 * @version $Id: GraphicContext.java 1345683 2012-06-03 14:50:33Z gadams $
 
55
 * @version $Id: GraphicContext.java 1610846 2014-07-15 20:44:18Z vhennebert $
56
56
 *
57
57
 * Originally authored by Vincent Hardy and Christophe Jolif.
58
58
 */
59
 
public class GraphicContext implements Cloneable{
 
59
public class GraphicContext implements Cloneable {
60
60
    /**
61
61
     * Default Transform to be used for creating FontRenderContext.
62
62
     */
104
104
    /**
105
105
     * Current clip
106
106
     */
107
 
    protected Shape clip = null;
 
107
    protected Shape clip;
108
108
 
109
109
    /**
110
110
     * Current set of RenderingHints
204
204
    /**
205
205
     * @return a deep copy of this context
206
206
     */
207
 
    public Object clone(){
 
207
    public Object clone() {
208
208
        return new GraphicContext(this);
209
209
    }
210
210
 
214
214
     * @see       java.awt.Color
215
215
     * @see       java.awt.Graphics#setColor
216
216
     */
217
 
    public Color getColor(){
 
217
    public Color getColor() {
218
218
        return foreground;
219
219
    }
220
220
 
226
226
     * @see       java.awt.Color
227
227
     * @see       java.awt.Graphics#getColor
228
228
     */
229
 
    public void setColor(Color c){
230
 
        if(c == null) {
 
229
    public void setColor(Color c) {
 
230
        if (c == null) {
231
231
            return;
232
232
        }
233
233
 
234
 
        if(paint != c) {
 
234
        if (paint != c) {
235
235
            setPaint(c);
236
236
        }
237
237
    }
242
242
     * @see       java.awt.Font
243
243
     * @see       java.awt.Graphics#setFont
244
244
     */
245
 
    public Font getFont(){
 
245
    public Font getFont() {
246
246
        return font;
247
247
    }
248
248
 
253
253
     * @param  font   the font.
254
254
     * @see     java.awt.Graphics#getFont
255
255
     */
256
 
    public void setFont(Font font){
257
 
        if(font != null) {
 
256
    public void setFont(Font font) {
 
257
        if (font != null) {
258
258
            this.font = font;
259
259
        }
260
260
    }
276
276
     * @see         java.awt.Graphics#setClip(Shape)
277
277
     * @since       JDK1.1
278
278
     */
279
 
    public Rectangle getClipBounds(){
 
279
    public Rectangle getClipBounds() {
280
280
        Shape c = getClip();
281
 
        if(c==null) {
 
281
        if (c == null) {
282
282
            return null;
283
283
        } else {
284
284
            return c.getBounds();
305
305
     * @see #setClip(int, int, int, int)
306
306
     * @see #setClip(Shape)
307
307
     */
308
 
    public void clipRect(int x, int y, int width, int height){
 
308
    public void clipRect(int x, int y, int width, int height) {
309
309
        clip(new Rectangle(x, y, width, height));
310
310
    }
311
311
 
324
324
     * @see         java.awt.Graphics#setClip(Shape)
325
325
     * @since       JDK1.1
326
326
     */
327
 
    public void setClip(int x, int y, int width, int height){
 
327
    public void setClip(int x, int y, int width, int height) {
328
328
        setClip(new Rectangle(x, y, width, height));
329
329
    }
330
330
 
345
345
     * @see         java.awt.Graphics#setClip(Shape)
346
346
     * @since       JDK1.1
347
347
     */
348
 
    public Shape getClip(){
349
 
        try{
 
348
    public Shape getClip() {
 
349
        try {
350
350
            return transform.createInverse().createTransformedShape(clip);
351
 
        }catch(NoninvertibleTransformException e){
 
351
        } catch (NoninvertibleTransformException e) {
352
352
            return null;
353
353
        }
354
354
    }
403
403
     * @see java.awt.Graphics#setPaintMode
404
404
     * @see java.awt.AlphaComposite
405
405
     */
406
 
    public void setComposite(Composite comp){
 
406
    public void setComposite(Composite comp) {
407
407
        this.composite = comp;
408
408
    }
409
409
 
421
421
     * @see java.awt.TexturePaint
422
422
     */
423
423
    public void setPaint(Paint paint) {
424
 
        if(paint == null) {
 
424
        if (paint == null) {
425
425
            return;
426
426
        }
427
427
 
428
428
        this.paint = paint;
429
 
        if(paint instanceof Color) {
 
429
        if (paint instanceof Color) {
430
430
            foreground = (Color)paint;
 
431
        } else {
 
432
            // use default; otherwise the previous Color will be used
 
433
            foreground = Color.black;
431
434
        }
432
435
    }
433
436
 
438
441
     * <code>Shape</code> during the rendering process
439
442
     * @see BasicStroke
440
443
     */
441
 
    public void setStroke(Stroke s){
 
444
    public void setStroke(Stroke s) {
442
445
        stroke = s;
443
446
    }
444
447
 
453
456
     * hint category.
454
457
     * @see RenderingHints
455
458
     */
456
 
    public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue){
 
459
    public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) {
457
460
        hints.put(hintKey, hintValue);
458
461
    }
459
462
 
470
473
     * <code>RenderingHints</code> class.
471
474
     * @see RenderingHints
472
475
     */
473
 
    public Object getRenderingHint(RenderingHints.Key hintKey){
 
476
    public Object getRenderingHint(RenderingHints.Key hintKey) {
474
477
        return hints.get(hintKey);
475
478
    }
476
479
 
488
491
     * @param hints the rendering hints to be set
489
492
     * @see RenderingHints
490
493
     */
491
 
    public void setRenderingHints(Map hints){
 
494
    public void setRenderingHints(Map hints) {
492
495
        this.hints = new RenderingHints(hints);
493
496
    }
494
497
 
507
510
     * @param hints the rendering hints to be set
508
511
     * @see RenderingHints
509
512
     */
510
 
    public void addRenderingHints(Map hints){
 
513
    public void addRenderingHints(Map hints) {
511
514
        this.hints.putAll(hints);
512
515
    }
513
516
 
524
527
     * that contains the current preferences.
525
528
     * @see RenderingHints
526
529
     */
527
 
    public RenderingHints getRenderingHints(){
 
530
    public RenderingHints getRenderingHints() {
528
531
        return hints;
529
532
    }
530
533
 
539
542
     * @param  x   the <i>x</i> coordinate.
540
543
     * @param  y   the <i>y</i> coordinate.
541
544
     */
542
 
    public void translate(int x, int y){
543
 
        if(x!=0 || y!=0){
 
545
    public void translate(int x, int y) {
 
546
        if (x != 0 || y != 0) {
544
547
            transform.translate(x, y);
545
548
            transformStack.add(TransformStackElement.createTranslateElement(x, y));
546
549
        }
563
566
     * @param tx the distance to translate along the x-axis
564
567
     * @param ty the distance to translate along the y-axis
565
568
     */
566
 
    public void translate(double tx, double ty){
 
569
    public void translate(double tx, double ty) {
567
570
        transform.translate(tx, ty);
568
571
        transformStack.add(TransformStackElement.createTranslateElement(tx, ty));
569
572
    }
584
587
     * x axis toward the positive y axis.
585
588
     * @param theta the angle of rotation in radians
586
589
     */
587
 
    public void rotate(double theta){
 
590
    public void rotate(double theta) {
588
591
        transform.rotate(theta);
589
592
        transformStack.add(TransformStackElement.createRotateElement(theta));
590
593
    }
608
611
     * @param x x coordinate of the origin of the rotation
609
612
     * @param y y coordinate of the origin of the rotation
610
613
     */
611
 
    public void rotate(double theta, double x, double y){
 
614
    public void rotate(double theta, double x, double y) {
612
615
        transform.rotate(theta, x, y);
613
616
        transformStack.add(TransformStackElement.createTranslateElement(x, y));
614
617
        transformStack.add(TransformStackElement.createRotateElement(theta));
634
637
     * rendering operations are multiplied relative to previous
635
638
     * rendering operations.
636
639
     */
637
 
    public void scale(double sx, double sy){
 
640
    public void scale(double sx, double sy) {
638
641
        transform.scale(sx, sy);
639
642
        transformStack.add(TransformStackElement.createScaleElement(sx, sy));
640
643
    }
657
660
     * @param shy the multiplier by which coordinates are shifted in
658
661
     * the positive Y axis direction as a function of their X coordinate
659
662
     */
660
 
    public void shear(double shx, double shy){
 
663
    public void shear(double shx, double shy) {
661
664
        transform.shear(shx, shy);
662
665
        transformStack.add(TransformStackElement.createShearElement(shx, shy));
663
666
    }
674
677
     * the result by the original <code>Transform</code> Cx.  In other
675
678
     * words, Cx'(p) = Cx(Tx(p)).  A copy of the Tx is made, if necessary,
676
679
     * so further modifications to Tx do not affect rendering.
677
 
     * @param Tx the <code>AffineTransform</code> object to be composed with
 
680
     * @param tx the <code>AffineTransform</code> object to be composed with
678
681
     * the current <code>Transform</code>
679
682
     * @see #setTransform
680
683
     * @see AffineTransform
681
684
     */
682
 
    public void transform(AffineTransform Tx){
683
 
        transform.concatenate(Tx);
684
 
        transformStack.add(TransformStackElement.createGeneralTransformElement(Tx));
 
685
    public void transform(AffineTransform tx) {
 
686
        transform.concatenate(tx);
 
687
        transformStack.add(TransformStackElement.createGeneralTransformElement(tx));
685
688
    }
686
689
 
687
690
    /**
688
691
     * Sets the <code>Transform</code> in the <code>Graphics2D</code>
689
692
     * context.
690
 
     * @param Tx the <code>AffineTransform</code> object to be used in the
 
693
     * @param tx the <code>AffineTransform</code> object to be used in the
691
694
     * rendering process
692
695
     * @see #transform
693
696
     * @see AffineTransform
694
697
     */
695
 
    public void setTransform(AffineTransform Tx){
696
 
        transform = new AffineTransform(Tx);
 
698
    public void setTransform(AffineTransform tx) {
 
699
        transform = new AffineTransform(tx);
697
700
        invalidateTransformStack();
698
 
        if(!Tx.isIdentity()) {
699
 
            transformStack.add(TransformStackElement.createGeneralTransformElement(Tx));
 
701
        if (!tx.isIdentity()) {
 
702
            transformStack.add(TransformStackElement.createGeneralTransformElement(tx));
700
703
        }
701
704
    }
702
705
 
706
709
     * has not been reset. Only the setTransform method can
707
710
     * override this memento.
708
711
     */
709
 
    public void validateTransformStack(){
 
712
    public void validateTransformStack() {
710
713
        transformStackValid = true;
711
714
    }
712
715
 
714
717
     * Checks the status of the transform stack.
715
718
     * @return true if the transform stack is valid
716
719
     */
717
 
    public boolean isTransformStackValid(){
 
720
    public boolean isTransformStackValid() {
718
721
        return transformStackValid;
719
722
    }
720
723
 
722
725
     * @return array containing the successive transforms that
723
726
     *         were concatenated with the original one.
724
727
     */
725
 
    public TransformStackElement[] getTransformStack(){
 
728
    public TransformStackElement[] getTransformStack() {
726
729
        TransformStackElement[] stack = new TransformStackElement[transformStack.size()];
727
730
        transformStack.toArray(stack);
728
731
        return stack;
734
737
     * since it was last read. Only validateTransformStack
735
738
     * can override this memento
736
739
     */
737
 
    protected void invalidateTransformStack(){
 
740
    protected void invalidateTransformStack() {
738
741
        transformStack.clear();
739
742
        transformStackValid = false;
740
743
    }
747
750
     * @see #transform
748
751
     * @see #setTransform
749
752
     */
750
 
    public AffineTransform getTransform(){
 
753
    public AffineTransform getTransform() {
751
754
        return new AffineTransform(transform);
752
755
    }
753
756
 
759
762
     * @see #setPaint
760
763
     * @see java.awt.Graphics#setColor
761
764
     */
762
 
    public Paint getPaint(){
 
765
    public Paint getPaint() {
763
766
        return paint;
764
767
    }
765
768
 
771
774
     *              which defines a compositing style.
772
775
     * @see #setComposite
773
776
     */
774
 
    public Composite getComposite(){
 
777
    public Composite getComposite() {
775
778
        return composite;
776
779
    }
777
780
 
791
794
     * @see #getBackground
792
795
     * @see java.awt.Graphics#clearRect
793
796
     */
794
 
    public void setBackground(Color color){
795
 
        if(color == null) {
 
797
    public void setBackground(Color color) {
 
798
        if (color == null) {
796
799
            return;
797
800
        }
798
801
 
806
809
     * which defines the background color.
807
810
     * @see #setBackground
808
811
     */
809
 
    public Color getBackground(){
 
812
    public Color getBackground() {
810
813
        return background;
811
814
    }
812
815
 
817
820
     *                 which defines the line style.
818
821
     * @see #setStroke
819
822
     */
820
 
    public Stroke getStroke(){
 
823
    public Stroke getStroke() {
821
824
        return stroke;
822
825
    }
823
826
 
841
844
     *          <code>Clip</code>.  If <code>s</code> is <code>null</code>,
842
845
     *          this method clears the current <code>Clip</code>.
843
846
     */
844
 
    public void clip(Shape s){
 
847
    public void clip(Shape s) {
845
848
        if (s != null) {
846
849
            s = transform.createTransformedShape(s);
847
850
        }
876
879
     * @see java.awt.font.TextLayout
877
880
     * @since     JDK1.2
878
881
     */
879
 
    public FontRenderContext getFontRenderContext(){
 
882
    public FontRenderContext getFontRenderContext() {
880
883
        //
881
884
        // Find if antialiasing should be used.
882
885
        //
883
886
        Object antialiasingHint = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
884
887
        boolean isAntialiased = true;
885
 
        if(antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON &&
886
 
           antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT){
 
888
        if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON
 
889
           && antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT) {
887
890
 
888
891
            // If antialias was not turned off, then use the general rendering
889
892
            // hint.
890
 
            if(antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF){
 
893
            if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) {
891
894
                antialiasingHint = hints.get(RenderingHints.KEY_ANTIALIASING);
892
895
 
893
896
                // Test general hint
894
 
                if(antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON &&
895
 
                   antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT){
 
897
                if (antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON
 
898
                   && antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT) {
896
899
                    // Antialiasing was not requested. However, if it was not turned
897
900
                    // off explicitly, use it.
898
 
                    if(antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF) {
 
901
                    if (antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF) {
899
902
                        isAntialiased = false;
900
903
                    }
901
904
                }
909
912
        // Find out whether fractional metrics should be used.
910
913
        //
911
914
        boolean useFractionalMetrics = true;
912
 
        if(hints.get(RenderingHints.KEY_FRACTIONALMETRICS)
 
915
        if (hints.get(RenderingHints.KEY_FRACTIONALMETRICS)
913
916
           == RenderingHints.VALUE_FRACTIONALMETRICS_OFF) {
914
917
            useFractionalMetrics = false;
915
918
        }