Class TextRenderer

java.lang.Object
com.itextpdf.layout.renderer.AbstractRenderer
com.itextpdf.layout.renderer.TextRenderer
All Implemented Interfaces:
IPropertyContainer, ILeafElementRenderer, IRenderer
Direct Known Subclasses:
LinkRenderer

public class TextRenderer extends AbstractRenderer implements ILeafElementRenderer
This class represents the renderer object for a Text object. It will draw the glyphs of the textual content on the DrawContext.
  • Field Details

    • TEXT_SPACE_COEFF

      protected static final float TEXT_SPACE_COEFF
      See Also:
    • yLineOffset

      protected float yLineOffset
    • text

      protected GlyphLine text
    • line

      protected GlyphLine line
    • strToBeConverted

      protected String strToBeConverted
    • otfFeaturesApplied

      protected boolean otfFeaturesApplied
    • tabAnchorCharacterPosition

      protected float tabAnchorCharacterPosition
    • reversedRanges

      protected List reversedRanges
    • savedWordBreakAtLineEnding

      protected GlyphLine savedWordBreakAtLineEnding
  • Constructor Details

    • TextRenderer

      public TextRenderer (Text textElement)
      Creates a TextRenderer from its corresponding layout object.
      Parameters:
      textElement - the Text which this object should manage
    • TextRenderer

      public TextRenderer (Text textElement, String text)
      Creates a TextRenderer from its corresponding layout object, with a custom text to replace the contents of the Text.
      Parameters:
      textElement - the Text which this object should manage
      text - the replacement text
    • TextRenderer

      protected TextRenderer (TextRenderer other)
  • Method Details

    • layout

      public LayoutResult layout (LayoutContext layoutContext)
      Description copied from interface: IRenderer
      This method simulates positioning of the renderer, including all of its children, and returns the LayoutResult, representing the layout result, including occupied area, status, i.e. if there was enough place to fit the renderer subtree, etc. LayoutResult can be extended to return custom layout results for custom elements, e.g. TextRenderer uses TextLayoutResult as its result. This method can be called standalone to learn how much area the renderer subtree needs, or can be called before IRenderer.draw(DrawContext), to prepare the renderer to be flushed to the output stream.
      Specified by:
      layout in interface IRenderer
      Parameters:
      layoutContext - the description of layout area and any other additional information
      Returns:
      result of the layout process
    • applyOtf

      public void applyOtf()
    • draw

      public void draw (DrawContext drawContext)
      Description copied from class: AbstractRenderer
      Flushes the renderer subtree contents, i.e. draws itself on canvas, adds necessary objects to the PdfDocument etc.
      Specified by:
      draw in interface IRenderer
      Overrides:
      draw in class AbstractRenderer
      Parameters:
      drawContext - contains the PdfDocument to which the renderer subtree if flushed, the PdfCanvas on which the renderer subtree is drawn and other additional parameters needed to perform drawing
    • trimFirst

      public void trimFirst()
      Trims any whitespace characters from the start of the GlyphLine to be rendered.
    • getAscent

      public float getAscent()
      Gets the maximum offset above the base line that this Text extends to.
      Specified by:
      getAscent in interface ILeafElementRenderer
      Returns:
      the upwards vertical offset of this Text
    • getDescent

      public float getDescent()
      Gets the maximum offset below the base line that this Text extends to.
      Specified by:
      getDescent in interface ILeafElementRenderer
      Returns:
      the downwards vertical offset of this Text
    • getYLine

      public float getYLine()
      Gets the position on the canvas of the imaginary horizontal line upon which the Text's contents will be written.
      Returns:
      the y position of this text on the DrawContext
    • moveYLineTo

      public void moveYLineTo (float y)
      Moves the vertical position to the parameter's value.
      Parameters:
      y - the new vertical position of the Text
    • setText

      public void setText (String text)
      Manually sets the contents of the Text's representation on the canvas, regardless of the Text's own contents.
      Parameters:
      text - the replacement text
    • setText

      public void setText (GlyphLine text, PdfFont font)
      Manually set a GlyphLine and PdfFont for rendering.
      Parameters:
      text - the GlyphLine
      font - the font
    • getText

      public GlyphLine getText()
    • length

      public int length()
      The length of the whole text assigned to this renderer.
      Returns:
      the text length
    • toString

      public String toString()
      Description copied from class: AbstractRenderer
      Returns a string representation of the renderer.
      Overrides:
      toString in class AbstractRenderer
      Returns:
      a String
      See Also:
    • charAt

      public int charAt (int pos)
      Gets char code at given position for the text belonging to this renderer.
      Parameters:
      pos - the position in range [0; length())
      Returns:
      Unicode char code
    • getTabAnchorCharacterPosition

      public float getTabAnchorCharacterPosition()
    • getNextRenderer

      public IRenderer getNextRenderer()
      Gets a new instance of this class to be used as a next renderer, after this renderer is used, if layout(LayoutContext) is called more than once.

      If TextRenderer overflows to the next line, iText uses this method to create a renderer for the overflow part. So if one wants to extend TextRenderer, one should override this method: otherwise the default method will be used and thus the default rather than the custom renderer will be created. Another method that should be overridden in case of TextRenderer's extension is createCopy(GlyphLine, PdfFont). This method is responsible for creation of TextRenderer's copies, which represent its parts of specific font.

      Specified by:
      getNextRenderer in interface IRenderer
      Returns:
      new renderer instance
    • calculateAscenderDescender

      public static float[] calculateAscenderDescender (PdfFont font)
      Get ascender and descender from font metrics. If these values are obtained from typo metrics they are normalized with a scale coefficient.
      Parameters:
      font - from which metrics will be extracted
      Returns:
      array in which the first element is an ascender and the second is a descender
    • calculateAscenderDescender

      public static float[] calculateAscenderDescender (PdfFont font, RenderingMode mode)
      Get ascender and descender from font metrics. In RenderingMode.DEFAULT_LAYOUT_MODE if these values are obtained from typo metrics they are normalized with a scale coefficient.
      Parameters:
      font - from which metrics will be extracted
      mode - mode in which metrics will be obtained. Impact on the use of scale coefficient
      Returns:
      array in which the first element is an ascender and the second is a descender
    • getBackgroundArea

      protected Rectangle getBackgroundArea (Rectangle occupiedAreaWithMargins)
      Description copied from class: AbstractRenderer
      Evaluate the actual background
      Overrides:
      getBackgroundArea in class AbstractRenderer
      Parameters:
      occupiedAreaWithMargins - the current occupied area with applied margins
      Returns:
      the actual background area
    • getFirstYLineRecursively

      protected Float getFirstYLineRecursively()
      Description copied from class: AbstractRenderer
      Gets the first yLine of the nested children recursively. E.g. for a list, this will be the yLine of the first item (if the first item is indeed a paragraph). NOTE: this method will no go further than the first child.
      Overrides:
      getFirstYLineRecursively in class AbstractRenderer
      Returns:
      the first yline of the nested children, null if there is no text found
    • getLastYLineRecursively

      protected Float getLastYLineRecursively()
      Overrides:
      getLastYLineRecursively in class AbstractRenderer
    • lineLength

      protected int lineLength()
      Returns the length of the line which is the result of the layout call.
      Returns:
      the length of the line
    • baseCharactersCount

      protected int baseCharactersCount()
    • getMinMaxWidth

      public MinMaxWidth getMinMaxWidth()
      Description copied from class: AbstractRenderer
      Calculates min and max width values for current renderer.
      Overrides:
      getMinMaxWidth in class AbstractRenderer
      Returns:
      instance of MinMaxWidth
    • getNumberOfSpaces

      protected int getNumberOfSpaces()
    • createSplitRenderer

      protected TextRenderer createSplitRenderer()
    • createOverflowRenderer

      protected TextRenderer createOverflowRenderer()
    • split

      protected TextRenderer[] split (int initialOverflowTextPos)
    • drawSingleUnderline

      protected void drawSingleUnderline (Underline underline, TransparentColor fontStrokeColor, PdfCanvas canvas, float fontSize, float italicAngleTan)
    • calculateLineWidth

      protected float calculateLineWidth()
    • resolveFonts

      protected boolean resolveFonts (List<IRenderer> addTo)
      Resolve Property.FONT String[] value.
      Parameters:
      addTo - add all processed renderers to.
      Returns:
      true, if new TextRenderer has been created.
    • setProcessedGlyphLineAndFont

      protected void setProcessedGlyphLineAndFont (GlyphLine gl, PdfFont font)
    • createCopy

      protected TextRenderer createCopy (GlyphLine gl, PdfFont font)
      Creates a copy of this TextRenderer, which corresponds to the passed GlyphLine with PdfFont.

      While processing TextRenderer, iText uses this method to create glyph lines of specific fonts, which represent the TextRenderer's parts. If one extends TextRenderer, one should override this method, otherwise if FontSelector related logic is triggered, copies of this TextRenderer will have the default behavior rather than the custom one.

      Parameters:
      gl - a GlyphLine which represents some of this TextRenderer's content
      font - a PdfFont for this part of the TextRenderer's content
      Returns:
      copy of this TextRenderer, which correspond to the passed GlyphLine with PdfFont