Class TXTRenderer

  • All Implemented Interfaces:
    Constants, Renderer

    public class TXTRenderer
    extends AbstractPathOrientedRenderer

    Renderer that renders areas to plain text.

    This work was authored by Art Welch and Mark Lillywhite (mark-fop@inomial.com) [to use the new Renderer interface].

    • Field Detail

      • outputStream

        private java.io.OutputStream outputStream
        The stream for output
      • currentStream

        private TXTStream currentStream
        The current stream to add Text commands to.
      • charData

        private java.lang.StringBuffer[] charData
        Buffer for text.
      • decoData

        private java.lang.StringBuffer[] decoData
        Buffer for background and images.
      • LINE_LEADING

        public static final int LINE_LEADING
        Leading of line containing Courier font size of 10pt.
        See Also:
        Constant Field Values
      • CHAR_HEIGHT

        public static final int CHAR_HEIGHT
        Height of one symbol in Courier font size of 10pt.
        See Also:
        Constant Field Values
      • CHAR_WIDTH

        public static final int CHAR_WIDTH
        Width of one symbol in Courier font size of 10pt.
        See Also:
        Constant Field Values
      • pageWidth

        private int pageWidth
        Current processing page width.
      • pageHeight

        private int pageHeight
        Current processing page height.
      • LINE_ENDING

        private static final java.lang.String LINE_ENDING
        Every line except the last line on a page (which will end with pageEnding) will be terminated with this string.
        See Also:
        Constant Field Values
      • PAGE_ENDING

        private static final java.lang.String PAGE_ENDING
        Every page except the last one will end with this string.
        See Also:
        Constant Field Values
      • firstPage

        private boolean firstPage
        Equals true, if current page is first.
      • bm

        private BorderManager bm
        Manager for storing border's information.
      • fillChar

        private char fillChar
        Char for current filling.
      • currentState

        private final TXTState currentState
        Saves current coordinate transformation.
      • encoding

        private java.lang.String encoding
    • Constructor Detail

      • TXTRenderer

        public TXTRenderer​(FOUserAgent userAgent)
        Constructs a newly allocated TXTRenderer object.
        Parameters:
        userAgent - the user agent that contains configuration details. This cannot be null.
    • Method Detail

      • getMimeType

        public java.lang.String getMimeType()
        Get the MIME type of the renderer.
        Returns:
        The MIME type of the renderer, may return null if not applicable.
      • setEncoding

        public void setEncoding​(java.lang.String encoding)
        Sets the encoding of the target file.
        Parameters:
        encoding - the encoding, null to select the default encoding (UTF-8)
      • isLayInside

        public boolean isLayInside​(int x,
                                   int y)
        Indicates if point (x, y) lay inside currentPage.
        Parameters:
        x - x coordinate
        y - y coordinate
        Returns:
        true if point lay inside page
      • addChar

        protected void addChar​(int x,
                               int y,
                               char ch,
                               boolean ischar)
        Add char to text buffer.
        Parameters:
        x - x coordinate
        y - y coordinate
        ch - char to add
        ischar - boolean, repersenting is character adding to text buffer
      • putChar

        protected void putChar​(int x,
                               int y,
                               char ch,
                               boolean ischar)
        Add char to text or background buffer.
        Parameters:
        x - x coordinate
        y - x coordinate
        ch - char to add
        ischar - indicates if it char or background
      • addString

        protected void addString​(int row,
                                 int col,
                                 java.lang.String s)
        Adds string to text buffer (charData).

        Chars of string map in turn.

        Parameters:
        row - x coordinate
        col - y coordinate
        s - string to add
      • renderText

        protected void renderText​(TextArea area)
        Render TextArea to Text.
        Overrides:
        renderText in class AbstractRenderer
        Parameters:
        area - inline area to render
      • renderPage

        public void renderPage​(PageViewport page)
                        throws java.io.IOException,
                               FOPException
        Tells the renderer to render a particular page. A renderer typically responds by packing up the current page and writing it immediately to the output device.
        Specified by:
        renderPage in interface Renderer
        Overrides:
        renderPage in class AbstractRenderer
        Parameters:
        page - The page to be rendered
        Throws:
        java.io.IOException - if an I/O error occurs
        FOPException - if a FOP interal error occurs.
      • flushBorderToBuffer

        private void flushBorderToBuffer()
        Projects current page borders (i.e.bm) to buffer for background and images (i.e.decoData).
      • flushBuffer

        private void flushBuffer()
        Write out the buffer to output stream.
      • startRenderer

        public void startRenderer​(java.io.OutputStream os)
                           throws java.io.IOException
        Initiates the rendering phase. This must only be called once for a rendering. If stopRenderer is called then this may be called again for a new document rendering.
        Specified by:
        startRenderer in interface Renderer
        Overrides:
        startRenderer in class AbstractRenderer
        Parameters:
        os - The OutputStream to use for output
        Throws:
        java.io.IOException - If an I/O error occurs
      • stopRenderer

        public void stopRenderer()
                          throws java.io.IOException
        Signals the end of the rendering phase. The renderer should reset to an initial state and dispose of any resources for the completed rendering.
        Specified by:
        stopRenderer in interface Renderer
        Overrides:
        stopRenderer in class AbstractRenderer
        Throws:
        java.io.IOException - If an I/O error occurs
      • restoreStateStackAfterBreakOut

        protected void restoreStateStackAfterBreakOut​(java.util.List breakOutList)
        Does nothing. Restores the state stack after a break out.
        Specified by:
        restoreStateStackAfterBreakOut in class AbstractPathOrientedRenderer
        Parameters:
        breakOutList - the state stack to restore.
      • clipRect

        protected void clipRect​(float x,
                                float y,
                                float width,
                                float height)
        Does nothing. Clip using a rectangular area.
        Specified by:
        clipRect in class AbstractPathOrientedRenderer
        Parameters:
        x - the x coordinate (in points)
        y - the y coordinate (in points)
        width - the width of the rectangle (in points)
        height - the height of the rectangle (in points)
      • moveTo

        protected void moveTo​(float x,
                              float y)
        Does nothing. Moves the current point to (x, y), omitting any connecting line segment.
        Specified by:
        moveTo in class AbstractPathOrientedRenderer
        Parameters:
        x - x coordinate
        y - y coordinate
      • lineTo

        protected void lineTo​(float x,
                              float y)
        Does nothing. Appends a straight line segment from the current point to (x, y). The new current point is (x, y).
        Specified by:
        lineTo in class AbstractPathOrientedRenderer
        Parameters:
        x - x coordinate
        y - y coordinate
      • closePath

        protected void closePath()
        Does nothing. Closes the current subpath by appending a straight line segment from the current point to the starting point of the subpath.
        Specified by:
        closePath in class AbstractPathOrientedRenderer
      • fillRect

        private void fillRect​(int startX,
                              int startY,
                              int width,
                              int height,
                              char charToFill)
        Fills rectangle startX, startY, width, height with char charToFill.
        Parameters:
        startX - x-coordinate of upper left point
        startY - y-coordinate of upper left point
        width - width of rectangle
        height - height of rectangle
        charToFill - filling char
      • fillRect

        protected void fillRect​(float x,
                                float y,
                                float width,
                                float height)
        Fills a rectangular area with the current filling char. Fill a rectangular area.
        Specified by:
        fillRect in class AbstractPathOrientedRenderer
        Parameters:
        x - the x coordinate
        y - the y coordinate
        width - the width of the rectangle
        height - the height of the rectangle
      • updateColor

        protected void updateColor​(java.awt.Color col,
                                   boolean fill)
        Changes current filling char. Establishes a new foreground or fill color.
        Specified by:
        updateColor in class AbstractPathOrientedRenderer
        Parameters:
        col - the color to apply (null skips this operation)
        fill - true to set the fill color, false for the foreground color
      • drawImage

        protected void drawImage​(java.lang.String url,
                                 java.awt.geom.Rectangle2D pos,
                                 java.util.Map foreignAttributes)
        Draw an image at the indicated location.
        Specified by:
        drawImage in class AbstractPathOrientedRenderer
        Parameters:
        url - the URI/URL of the image
        pos - the position of the image
        foreignAttributes - an optional Map with foreign attributes, may be null
      • renderImage

        public void renderImage​(Image image,
                                java.awt.geom.Rectangle2D pos)
        Fills image rectangle with a IMAGE_CHAR.
        Overrides:
        renderImage in class AbstractRenderer
        Parameters:
        image - the base image
        pos - the position of the image
      • toMilli

        protected int toMilli​(float x)
        Returns the closest integer to the multiplication of a number and 1000.
        Parameters:
        x - the value of the argument, multiplied by 1000 and rounded
        Returns:
        the value of the argument multiplied by 1000 and rounded to the nearest integer
      • addBitOfBorder

        private void addBitOfBorder​(int x,
                                    int y,
                                    int style,
                                    int type)
        Adds one element of border.
        Parameters:
        x - x coordinate
        y - y coordinate
        style - integer, representing border style
        type - integer, representing border element type
      • drawBorderLine

        protected void drawBorderLine​(float x1,
                                      float y1,
                                      float x2,
                                      float y2,
                                      boolean horz,
                                      boolean startOrBefore,
                                      int style,
                                      java.awt.Color col)
        Draw a border segment of an XSL-FO style border.
        Specified by:
        drawBorderLine in class AbstractPathOrientedRenderer
        Parameters:
        x1 - starting x coordinate
        y1 - starting y coordinate
        x2 - ending x coordinate
        y2 - ending y coordinate
        horz - true for horizontal border segments, false for vertical border segments
        startOrBefore - true for border segments on the start or before edge, false for end or after.
        style - the border style (one of Constants.EN_DASHED etc.)
        col - the color for the border segment
      • drawBackAndBorders

        protected void drawBackAndBorders​(Area area,
                                          float startx,
                                          float starty,
                                          float width,
                                          float height)
        Draw the background and borders. This draws the background and border traits for an area given the position.
        Overrides:
        drawBackAndBorders in class AbstractPathOrientedRenderer
        Parameters:
        area - the area to get the traits from
        startx - the start x position
        starty - the start y position
        width - the width of the area
        height - the height of the area
      • startVParea

        protected void startVParea​(CTM ctm,
                                   java.awt.Rectangle clippingRect)
        Establishes a new viewport area.
        Specified by:
        startVParea in class AbstractRenderer
        Parameters:
        ctm - the coordinate transformation matrix to use
        clippingRect - the clipping rectangle if the viewport should be clipping, null if no clipping is performed.
      • endVParea

        protected void endVParea()
        Signals exit from a viewport area. Subclasses can restore transformation matrices valid before the viewport area was started.
        Specified by:
        endVParea in class AbstractRenderer
      • startLayer

        protected void startLayer​(java.lang.String layer)
        Establish new optional content group layer.
        Specified by:
        startLayer in class AbstractRenderer
        Parameters:
        layer - name of layer
      • endLayer

        protected void endLayer()
        Finish current optional content group layer.
        Specified by:
        endLayer in class AbstractRenderer
      • concatenateTransformationMatrix

        protected void concatenateTransformationMatrix​(java.awt.geom.AffineTransform at)
        Concatenates the current transformation matrix with the given one, therefore establishing a new coordinate system.
        Specified by:
        concatenateTransformationMatrix in class AbstractPathOrientedRenderer
        Parameters:
        at - the transformation matrix to process (coordinates in points)