Class PlainText.Paragraph

  • Enclosing class:
    PlainText

    static class PlainText.Paragraph
    extends java.lang.Object
    A block of text to be formatted as a whole.

    A block of text can contain multiple paragraphs which will be treated individually within the block placement.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String textContent  
    • Constructor Summary

      Constructors 
      Constructor Description
      Paragraph​(java.lang.String text)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static float[] buildPrefixWidths​(java.lang.String word, PDFont font, float scale)
      Build the prefix-sum array of scaled character widths for the given word.
      private static int findMaxFittingChars​(float[] prefixWidth, float width)
      Find the maximum number of Java chars from a prefix-width array that fit within the given available width.
      (package private) java.util.List<PlainText.Line> getLines​(PDFont font, float fontSize, float width)
      Break the paragraph into individual lines.
      (package private) java.lang.String getText()
      Get the paragraph text.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • textContent

        private final java.lang.String textContent
    • Constructor Detail

      • Paragraph

        Paragraph​(java.lang.String text)
    • Method Detail

      • getText

        java.lang.String getText()
        Get the paragraph text.
        Returns:
        the text.
      • getLines

        java.util.List<PlainText.Line> getLines​(PDFont font,
                                                float fontSize,
                                                float width)
                                         throws java.io.IOException
        Break the paragraph into individual lines.
        Parameters:
        font - the font used for rendering the text.
        fontSize - the fontSize used for rendering the text.
        width - the width of the box holding the content.
        Returns:
        the individual lines.
        Throws:
        java.io.IOException
      • buildPrefixWidths

        private static float[] buildPrefixWidths​(java.lang.String word,
                                                 PDFont font,
                                                 float scale)
                                          throws java.io.IOException
        Build the prefix-sum array of scaled character widths for the given word.
        Parameters:
        word - the word to measure.
        font - the font used to obtain glyph advance widths.
        scale - fontSize / FONTSCALE, pre-computed by the caller.
        Returns:
        the float[word.length() + 1] prefix-sum array.
        Throws:
        java.io.IOException - if the font cannot provide a glyph width.
      • findMaxFittingChars

        private static int findMaxFittingChars​(float[] prefixWidth,
                                               float width)
        Find the maximum number of Java chars from a prefix-width array that fit within the given available width.

        Binary search over the pre-computed prefixWidth array. The result is always at least 1 so that the caller is guaranteed to make forward progress (PDFBOX-6082).

        Parameters:
        prefixWidth - array as returned by buildPrefixWidths(java.lang.String, org.apache.pdfbox.pdmodel.font.PDFont, float); length is wordLength + 1.
        width - the available line width in the same unit as the widths stored in prefixWidth.
        Returns:
        the largest index k >= 1 such that prefixWidth[k] < width, or 1 if even a single character exceeds the available width.