Package org.apache.pdfbox.pdmodel.font

Examples of org.apache.pdfbox.pdmodel.font.PDFont


    public void validText(byte[] string) throws IOException
    {
        // TextSize accessible through the TextState
        PDTextState textState = getGraphicsState().getTextState();
        final int renderingMode = textState.getRenderingMode();
        final PDFont font = textState.getFont();
        if (font == null)
        {
            // Unable to decode the Text without Font
            registerError("Text operator can't be process without Font", ERROR_FONTS_UNKNOWN_FONT_REF);
            return;
        }

        FontContainer fontContainer = context.getFontContainer(font.getCOSObject());
        if (renderingMode == 3 && (fontContainer == null || !fontContainer.isEmbeddedFont()))
        {
            // font not embedded and rendering mode is 3. Valid case and nothing to check
            return;
        }
        else if (fontContainer == null)
        {
            // Font Must be embedded if the RenderingMode isn't 3
            registerError(font.getBaseFont() + " is unknown wasn't found by the FontHelperValdiator",
                    ERROR_FONTS_UNKNOWN_FONT_REF);
            return;
        }
        else if (!fontContainer.isValid() && !fontContainer.errorsAleadyMerged())
        {
            context.addValidationErrors(fontContainer.getAllErrors());
            fontContainer.setErrorsAleadyMerged(true);
            return;
        }
        if (!fontContainer.isValid() && fontContainer.errorsAleadyMerged())
        {
            // font already computed
            return;
        }

        int codeLength = 1;
        for (int i = 0; i < string.length; i += codeLength)
        {
            // explore the string to detect character code (length can be 1 or 2 bytes)
            int cid = -1;
            codeLength = 1;
            try
            {
                // according to the encoding, extract the character identifier
                cid = font.encodeToCID(string, i, codeLength);
                if (cid == -1 && i + 1 < string.length)
                {
                    // maybe a multibyte encoding
                    codeLength++;
                    cid = font.encodeToCID(string, i, codeLength);
                }
                fontContainer.checkGlyphWith(cid);
            }
            catch (IOException e)
            {
View Full Code Here


        //We won't know the actual number of characters until
        //we process the byte data(could be two bytes each) but
        //it won't ever be more than string.length*2(there are some cases
        //were a single byte will result in two output characters "fi"
       
        final PDFont font = graphicsState.getTextState().getFont();
        // all fonts are providing the width/height of a character in thousandths of a unit of text space
        float fontMatrixXScaling = 1/1000f;
        float fontMatrixYScaling = 1/1000f;
        float glyphSpaceToTextSpaceFactor = 1/1000f;
        // expect Type3 fonts, those are providing the width of a character in glyph space units
        if (font instanceof PDType3Font)
        {
            PDMatrix fontMatrix = font.getFontMatrix();
            fontMatrixXScaling = fontMatrix.getValue(0, 0);
            fontMatrixYScaling = fontMatrix.getValue(1, 1);
            //This will typically be 1000 but in the case of a type3 font
            //this might be a different number
            glyphSpaceToTextSpaceFactor = 1f/fontMatrix.getValue( 0, 0 );
        }
        float spaceWidthText=0;
        try
        {
            // to avoid crash as described in PDFBOX-614
            // lets see what the space displacement should be
            spaceWidthText = (font.getSpaceWidth() * glyphSpaceToTextSpaceFactor);
        }
        catch (Throwable exception)
        {
            LOG.warn(exception, exception);
        }
       
        if (spaceWidthText == 0)
        {
            spaceWidthText = (font.getAverageFontWidth() * glyphSpaceToTextSpaceFactor);
            // The average space width appears to be higher than necessary
            // so lets make it a little bit smaller.
            spaceWidthText *= .80f;
        }
        if (spaceWidthText == 0)
        {
            spaceWidthText = 1.0f; // if could not find font, use a generic value
        }       
        float maxVerticalDisplacementText = 0;

        Matrix textStateParameters = new Matrix();
        textStateParameters.setValue(0,0, fontSizeText*horizontalScalingText);
        textStateParameters.setValue(1,1, fontSizeText);
        textStateParameters.setValue(2,1, riseText);

        int pageRotation = page.findRotation();
        float pageHeight = page.findCropBox().getHeight();
        float pageWidth = page.findCropBox().getWidth();

        Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
        Matrix textXctm = new Matrix();
        Matrix textMatrixEnd = new Matrix();
        Matrix td = new Matrix();
        Matrix tempMatrix = new Matrix();

        int codeLength = 1;
        for( int i=0; i<string.length; i+=codeLength)
        {
            // Decode the value to a Unicode character
            codeLength = 1;
            String c = font.encode( string, i, codeLength );
            int[] codePoints = null;
            if( c == null && i+1<string.length)
            {
                //maybe a multibyte encoding
                codeLength++;
                c = font.encode( string, i, codeLength );
                codePoints = new int[] {font.getCodeFromArray(string, i, codeLength)};
            }

            // the space width has to be transformed into display units
            float spaceWidthDisp = spaceWidthText * fontSizeText * horizontalScalingText
                                    * textMatrix.getXScale() * ctm.getXScale();

            //todo, handle horizontal displacement
            // get the width and height of this character in text units
            float characterHorizontalDisplacementText = font.getFontWidth( string, i, codeLength );
            float characterVerticalDisplacementText = font.getFontHeight( string, i, codeLength );

            // multiply the width/height with the scaling factor
            characterHorizontalDisplacementText = characterHorizontalDisplacementText * fontMatrixXScaling;
            characterVerticalDisplacementText = characterVerticalDisplacementText * fontMatrixYScaling;

 
View Full Code Here

                    paint = graphicsState.getNonStrokingColor().getJavaColor();
            }
            graphics.setComposite(composite);
            graphics.setPaint(paint);
           
            PDFont font = text.getFont();
            Matrix textPos = text.getTextPos().copy();
            float x = textPos.getXPosition();
            // the 0,0-reference has to be moved from the lower left (PDF) to the upper left (AWT-graphics)
            float y = pageSize.height - textPos.getYPosition();
            // Set translation to 0,0. We only need the scaling and shearing
            textPos.setValue(2, 0, 0);
            textPos.setValue(2, 1, 0);
            // because of the moved 0,0-reference, we have to shear in the opposite direction
            textPos.setValue(0, 1, (-1)*textPos.getValue(0, 1));
            textPos.setValue(1, 0, (-1)*textPos.getValue(1, 0));
            AffineTransform at = textPos.createAffineTransform();
            PDMatrix fontMatrix = font.getFontMatrix();
            at.scale(fontMatrix.getValue(0, 0) * 1000f, fontMatrix.getValue(1, 1) * 1000f);
            //TODO setClip() is a massive performance hot spot. Investigate optimization possibilities
            graphics.setClip(graphicsState.getCurrentClippingPath());
            // the fontSize is no longer needed as it is already part of the transformation
            // we should remove it from the parameter list in the long run
            font.drawString( text.getCharacter(), text.getCodePoints(), graphics, 1, at, x, y );
        }
        catch( IOException io )
        {
            LOG.error (io, io);
        }
View Full Code Here

                    COSBase item = dicFonts.getItem((COSName) key);
                    COSDictionary xObjFont = COSUtils.getAsDictionary(item, cosDocument);

                    try
                    {
                        PDFont aFont = PDFontFactory.createFont(xObjFont);
                        FontContainer aContainer = this.context.getFontContainer(aFont.getCOSObject());
                        // another font is used in the Type3, check if the font is valid.
                        if (!aContainer.isValid())
                        {
                            this.fontContainer.push(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED,
                                    "The Resources dictionary of type 3 font contains invalid font"));
View Full Code Here

                    appearance.setNormalAppearance(appearanceStream);
                }

                List tokens = getStreamTokens(appearanceStream);
                List daTokens = getStreamTokens(getDefaultAppearance());
                PDFont pdFont = getFontAndUpdateResources(tokens, appearanceStream);

                // Special handling for listboxes to address PDFBOX-2249
                // TODO: Shall be addressed properly in a future release
                if (parent instanceof PDChoiceField
                        && (parent.getFieldFlags() & ((PDChoiceField) parent).FLAG_COMBO) == 0)
View Full Code Here

        printWriter.flush();
    }

    private PDFont getFontAndUpdateResources(List tokens, PDAppearanceStream appearanceStream) throws IOException
    {
        PDFont retval = null;
        PDResources streamResources = appearanceStream.getResources();
        PDResources formResources = acroForm.getDefaultResources();
        if (formResources != null)
        {
            if (streamResources == null)
View Full Code Here

                    COSBase font = fontsDictionary.getDictionaryObject(fontName);
                    // data-000174.pdf contains a font that is a COSArray, looks to be an error in the
                    // PDF, we will just ignore entries that are not dictionaries.
                    if (font instanceof COSDictionary)
                    {
                        PDFont newFont = null;
                        try
                        {
                            newFont = PDFontFactory.createFont((COSDictionary) font);
                        }
                        catch (IOException exception)
View Full Code Here

                    paint = graphicsState.getNonStrokingColor().getJavaColor();
            }
            graphics.setComposite(composite);
            graphics.setPaint(paint);
           
            PDFont font = text.getFont();
            Matrix textPos = text.getTextPos().copy();
            float x = textPos.getXPosition();
            // the 0,0-reference has to be moved from the lower left (PDF) to the upper left (AWT-graphics)
            float y = pageSize.height - textPos.getYPosition();
            // Set translation to 0,0. We only need the scaling and shearing
            textPos.setValue(2, 0, 0);
            textPos.setValue(2, 1, 0);
            // because of the moved 0,0-reference, we have to shear in the opposite direction
            textPos.setValue(0, 1, (-1)*textPos.getValue(0, 1));
            textPos.setValue(1, 0, (-1)*textPos.getValue(1, 0));
            AffineTransform at = textPos.createAffineTransform();
            PDMatrix fontMatrix = font.getFontMatrix();
            at.scale(fontMatrix.getValue(0, 0) * 1000f, fontMatrix.getValue(1, 1) * 1000f);
            //TODO setClip() is a massive performance hot spot. Investigate optimization possibilities
            graphics.setClip(graphicsState.getCurrentClippingPath());
            // the fontSize is no longer needed as it is already part of the transformation
            // we should remove it from the parameter list in the long run
            font.drawString( text.getCharacter(), text.getCodePoints(), graphics, 1, at, x, y );
        }
        catch( IOException io )
        {
            io.printStackTrace();
        }
View Full Code Here

        //We won't know the actual number of characters until
        //we process the byte data(could be two bytes each) but
        //it won't ever be more than string.length*2(there are some cases
        //were a single byte will result in two output characters "fi"
       
        final PDFont font = graphicsState.getTextState().getFont();
       
        //This will typically be 1000 but in the case of a type3 font
        //this might be a different number
        final float glyphSpaceToTextSpaceFactor = 1f/font.getFontMatrix().getValue( 0, 0 );
        float spaceWidthText=0;
       
        try{ // to avoid crash as described in PDFBOX-614
            // lets see what the space displacement should be
            spaceWidthText = (font.getFontWidth( SPACE_BYTES, 0, 1 )/glyphSpaceToTextSpaceFactor);
        }catch (Throwable exception)
        {
            log.warn( exception, exception);
        }
       
        if( spaceWidthText == 0 )
        {
            spaceWidthText = (font.getAverageFontWidth()/glyphSpaceToTextSpaceFactor);
            //The average space width appears to be higher than necessary
            //so lets make it a little bit smaller.
            spaceWidthText *= .80f;
        }
       
       
        /* Convert textMatrix to display units */
        final Matrix initialMatrix = new Matrix();
        initialMatrix.setValue(0,0,1);
        initialMatrix.setValue(0,1,0);
        initialMatrix.setValue(0,2,0);
        initialMatrix.setValue(1,0,0);
        initialMatrix.setValue(1,1,1);
        initialMatrix.setValue(1,2,0);
        initialMatrix.setValue(2,0,0);
        initialMatrix.setValue(2,1,riseText);
        initialMatrix.setValue(2,2,1);

        final Matrix ctm = graphicsState.getCurrentTransformationMatrix();
        final Matrix dispMatrix = initialMatrix.multiply( ctm );

        Matrix textMatrixStDisp = textMatrix.multiply( dispMatrix );
        Matrix textMatrixEndDisp = null;

        final float xScaleDisp = textMatrixStDisp.getXScale();
        final float yScaleDisp = textMatrixStDisp.getYScale();
       
        final float spaceWidthDisp = spaceWidthText * xScaleDisp * fontSizeText;
        final float wordSpacingDisp = wordSpacingText * xScaleDisp * fontSizeText;
       
        float maxVerticalDisplacementText = 0;

        float[] individualWidthsBuffer = new float[string.length];
        StringBuilder characterBuffer = new StringBuilder(string.length);

        int codeLength = 1;
        for( int i=0; i<string.length; i+=codeLength )
        {
            // Decode the value to a Unicode character
            codeLength = 1;
            String c = font.encode( string, i, codeLength );
            if( c == null && i+1<string.length)
            {
                //maybe a multibyte encoding
                codeLength++;
                c = font.encode( string, i, codeLength );
            }
            c = inspectFontEncoding(c);

            //todo, handle horizontal displacement
            // get the width and height of this character in text units
            float characterHorizontalDisplacementText =
                (font.getFontWidth( string, i, codeLength )/glyphSpaceToTextSpaceFactor);
            maxVerticalDisplacementText =
                Math.max(
                    maxVerticalDisplacementText,
                    font.getFontHeight( string, i, codeLength)/glyphSpaceToTextSpaceFactor);

            // PDF Spec - 5.5.2 Word Spacing
            //
            // Word spacing works the same was as character spacing, but applies
            // only to the space character, code 32.
View Full Code Here

            COSName mc2 = COSName.getPDFName("MC2");
            props.putMapping(mc2, disabled);

            //Setup page content stream and paint background/title
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false);
            PDFont font = PDType1Font.HELVETICA_BOLD;
            contentStream.beginMarkedContentSequence(COSName.OC, mc0);
            contentStream.beginText();
            contentStream.setFont(font, 14);
            contentStream.moveTextPositionByAmount(80, 700);
            contentStream.drawString("PDF 1.5: Optional Content Groups");
View Full Code Here

TOP

Related Classes of org.apache.pdfbox.pdmodel.font.PDFont

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.