AFPGraphics2D (Apache FOP Core 2.11 API)
Date: 2025-04-24

org.apache.fop.afp

## Class AFPGraphics2D

- java.lang.Object
- - java.awt.Graphics
  - - java.awt.Graphics2D
    - - org.apache.xmlgraphics.java2d.AbstractGraphics2D
      - - org.apache.fop.afp.AFPGraphics2D

- All Implemented Interfaces:
    Cloneable, NativeImageHandler

      public class AFPGraphics2D
      extends org.apache.xmlgraphics.java2d.AbstractGraphics2D
      implements NativeImageHandler

  This is a concrete implementation of `AbstractGraphics2D` (and therefore of `Graphics2D`) which is able to generate GOCA byte codes.
  See Also:
    `AbstractGraphics2D`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected org.apache.xmlgraphics.java2d.TextHandler` | `customTextHandler` Custom text handler
    `protected org.apache.xmlgraphics.java2d.TextHandler` | `fallbackTextHandler` Fallback text handler

    - ### Fields inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D

      `gc, inPossibleRecursion, textAsShapes`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AFPGraphics2D(AFPGraphics2D g2d)` Copy Constructor
    `AFPGraphics2D(boolean textAsShapes, AFPPaintingState paintingState, AFPResourceManager resourceManager, AFPResourceInfo resourceInfo, FontInfo fontInfo)` Main constructor

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addNativeImage(org.apache.xmlgraphics.image.loader.Image image, float x, float y, float width, float height)` Add a natively handled image directly to the document.
    `protected void` | `applyStroke(Stroke stroke)` Apply the stroke to the AFP graphics object.
    `void` | `clearRect(int x, int y, int width, int height)`
    `double` | `convertToAbsoluteLength(double length)` Converts a length value to an absolute value.
    `void` | `copyArea(int x, int y, int width, int height, int dx, int dy)`
    `Graphics` | `create()`
    `void` | `dispose()`
    `void` | `draw(Shape shape)`
    `boolean` | `drawImage(Image img, int x, int y, ImageObserver observer)`
    `boolean` | `drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)`
    `void` | `drawRenderableImage(RenderableImage img, AffineTransform xform)`
    `void` | `drawRenderedImage(RenderedImage img, AffineTransform xform)`
    `void` | `drawString(String str, float x, float y)`
    `void` | `fill(Shape shape)`
    `GraphicsConfiguration` | `getDeviceConfiguration()`
    `FontInfo` | `getFontInfo()` Returns the FontInfo
    `FontMetrics` | `getFontMetrics(Font f)`
    `GraphicsObject` | `getGraphicsObject()` Returns the GOCA graphics object
    `AFPPaintingState` | `getPaintingState()` Returns the AFP painting state
    `AFPResourceManager` | `getResourceManager()` Returns the AFP resource manager associated with this `Graphics2D` instance.
    `void` | `handleIOException(IOException ioe)` Central handler for IOExceptions for this class.
    `void` | `setCustomTextHandler(org.apache.xmlgraphics.java2d.TextHandler handler)` Sets a custom TextHandler implementation that is responsible for painting text.
    `void` | `setGraphicContext(org.apache.xmlgraphics.java2d.GraphicContext gc)` Sets the GraphicContext
    `void` | `setGraphicsObject(GraphicsObject obj)` Sets the GOCA graphics object
    `void` | `setXORMode(Color col)`

    - ### Methods inherited from class org.apache.xmlgraphics.java2d.AbstractGraphics2D

      `addRenderingHints, clip, clipRect, drawArc, drawGlyphVector, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, drawString, drawString, fillArc, fillOval, fillPolygon, fillRect, fillRoundRect, getBackground, getClip, getClipBounds, getColor, getComposite, getFont, getFontRenderContext, getGraphicContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, rotate, rotate, scale, setBackground, setClip, setClip, setColor, setComposite, setFont, setPaint, setPaintMode, setRenderingHint, setRenderingHints, setStroke, setTransform, shear, transform, translate, translate`

    - ### Methods inherited from class java.awt.Graphics2D

      `draw3DRect, fill3DRect`

    - ### Methods inherited from class java.awt.Graphics

      `create, drawBytes, drawChars, drawPolygon, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### fallbackTextHandler

          protected org.apache.xmlgraphics.java2d.TextHandler fallbackTextHandler

      Fallback text handler

    - #### customTextHandler

          protected org.apache.xmlgraphics.java2d.TextHandler customTextHandler

      Custom text handler

  - ### Constructor Detail

    - #### AFPGraphics2D

          public AFPGraphics2D(boolean textAsShapes,
                               AFPPaintingState paintingState,
                               AFPResourceManager resourceManager,
                               AFPResourceInfo resourceInfo,
                               FontInfo fontInfo)

      Main constructor
      Parameters:
        `textAsShapes` - if true, all text is turned into shapes in the convertion. No text is output.
        `paintingState` - painting state
        `resourceManager` - resource manager
        `resourceInfo` - resource info
        `fontInfo` - font info

    - #### AFPGraphics2D

          public AFPGraphics2D(AFPGraphics2D g2d)

      Copy Constructor
      Parameters:
        `g2d` - a AFPGraphics2D whose properties should be copied

  - ### Method Detail

    - #### getResourceManager

          public AFPResourceManager getResourceManager()

      Returns the AFP resource manager associated with this `Graphics2D` instance.
      Returns:
        the resource manager

    - #### getGraphicsObject

          public GraphicsObject getGraphicsObject()

      Returns the GOCA graphics object
      Returns:
        the GOCA graphics object

    - #### setGraphicsObject

          public void setGraphicsObject(GraphicsObject obj)

      Sets the GOCA graphics object
      Parameters:
        `obj` - the GOCA graphics object

    - #### getPaintingState

          public AFPPaintingState getPaintingState()

      Returns the AFP painting state
      Returns:
        the AFP painting state

    - #### getFontInfo

          public FontInfo getFontInfo()

      Returns the FontInfo
      Returns:
        the FontInfo

    - #### setGraphicContext

          public void setGraphicContext(org.apache.xmlgraphics.java2d.GraphicContext gc)

      Sets the GraphicContext
      Parameters:
        `gc` - GraphicContext to use

    - #### convertToAbsoluteLength

          public double convertToAbsoluteLength(double length)

      Converts a length value to an absolute value. Please note that this only uses the "ScaleY" factor, so this will result in a bad value should "ScaleX" and "ScaleY" be different.
      Parameters:
        `length` - the length
      Returns:
        the absolute length

    - #### applyStroke

          protected void applyStroke(Stroke stroke)

      Apply the stroke to the AFP graphics object. This takes the java stroke and outputs the appropriate settings to the AFP graphics object so that the stroke attributes are handled.
      Parameters:
        `stroke` - the java stroke

    - #### draw

          public void draw(Shape shape)

      Specified by:
        `draw` in class `Graphics2D`

    - #### fill

          public void fill(Shape shape)

      Specified by:
        `fill` in class `Graphics2D`

    - #### handleIOException

          public void handleIOException(IOException ioe)

      Central handler for IOExceptions for this class.
      Parameters:
        `ioe` - IOException to handle

    - #### drawString

          public void drawString(String str,
                                 float x,
                                 float y)

      Specified by:
        `drawString` in class `Graphics2D`

    - #### getDeviceConfiguration

          public GraphicsConfiguration getDeviceConfiguration()

      Specified by:
        `getDeviceConfiguration` in class `Graphics2D`

    - #### create

          public Graphics create()

      Specified by:
        `create` in class `Graphics`

    - #### dispose

          public void dispose()

      Specified by:
        `dispose` in class `Graphics`

    - #### drawImage

          public boolean drawImage(Image img,
                                   int x,
                                   int y,
                                   ImageObserver observer)

      Specified by:
        `drawImage` in class `Graphics`

    - #### drawImage

          public boolean drawImage(Image img,
                                   int x,
                                   int y,
                                   int width,
                                   int height,
                                   ImageObserver observer)

      Specified by:
        `drawImage` in class `Graphics`

    - #### drawRenderedImage

          public void drawRenderedImage(RenderedImage img,
                                        AffineTransform xform)

      Specified by:
        `drawRenderedImage` in class `Graphics2D`

    - #### setCustomTextHandler

          public void setCustomTextHandler(org.apache.xmlgraphics.java2d.TextHandler handler)

      Sets a custom TextHandler implementation that is responsible for painting text. The default TextHandler paints all text as shapes. A custom implementation can implement text painting using text painting operators.
      Parameters:
        `handler` - the custom TextHandler implementation

    - #### drawRenderableImage

          public void drawRenderableImage(RenderableImage img,
                                          AffineTransform xform)

      Specified by:
        `drawRenderableImage` in class `Graphics2D`

    - #### getFontMetrics

          public FontMetrics getFontMetrics(Font f)

      Specified by:
        `getFontMetrics` in class `Graphics`

    - #### setXORMode

          public void setXORMode(Color col)

      Specified by:
        `setXORMode` in class `Graphics`

    - #### addNativeImage

          public void addNativeImage(org.apache.xmlgraphics.image.loader.Image image,
                                     float x,
                                     float y,
                                     float width,
                                     float height)

      Add a natively handled image directly to the document. This is used by the ImageElementBridge to draw a natively handled image (like JPEG or CCITT images) directly into the document rather than converting the image into a bitmap and increasing the size.
      Specified by:
        `addNativeImage` in interface `NativeImageHandler`
      Parameters:
        `image` - the image to draw
        `x` - the x position
        `y` - the y position
        `width` - the width to draw the image
        `height` - the height to draw the image

    - #### copyArea

          public void copyArea(int x,
                               int y,
                               int width,
                               int height,
                               int dx,
                               int dy)

      Specified by:
        `copyArea` in class `Graphics`

    - #### clearRect

          public void clearRect(int x,
                                int y,
                                int width,
                                int height)

      Overrides:
        `clearRect` in class `org.apache.xmlgraphics.java2d.AbstractGraphics2D`

Copyright © 2025 Apache Software Foundation. All rights reserved.