All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.intarsys.pdf.content.text.CSCharacterParser Maven / Gradle / Ivy

Go to download

This is a fork of http://sourceforge.net/projects/jpodlib/ as development seems to be frozen. We're providing some bug fixes along with deployments to maven.

There is a newer version: 2.0
Show newest version
/*
 * intarsys consulting GmbH
 * all rights reserved
 *
 */
package de.intarsys.pdf.content.text;

import de.intarsys.pdf.content.CSTextDevice;
import de.intarsys.pdf.font.PDGlyphs;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

/**
 * Collect the character objects in a content stream.
 */
public abstract class CSCharacterParser extends CSTextDevice {

    protected double lastStopX;

    protected double lastStopY;

    protected double lastStartX;

    protected double lastStartY;

    private Shape bounds;

    protected CSCharacterParser() {
        super();
    }

    @Override
    protected void basicTextShowGlyphs(PDGlyphs glyphs, float advance) {
        AffineTransform tx;
        tx = (AffineTransform) getDeviceTransform().clone();
        tx.concatenate(textState.globalTransform);
        lastStartX = tx.getTranslateX();
        lastStartY = tx.getTranslateY();
        // get the transformed character bounding box
        double glyphAscent = glyphs.getAscent();
        double glyphDescent = glyphs.getDescent();
        double ascent = (textState.fontSize * glyphAscent) / THOUSAND;
        double descent = (textState.fontSize * glyphDescent) / THOUSAND;
        if (descent > 0) {
            descent = -descent;
        }
        double[] pts = {0, descent, advance, ascent};
        tx.deltaTransform(pts, 0, pts, 0, 2);
        //
        float x = (float) lastStartX;
        float y = (float) (lastStartY + pts[1]);
        float width = (float) pts[2];
        float height = (float) (pts[3] - pts[1]);
        if (width < 0) {
            x += width;
            width = -width;
        }
        if (height < 0) {
            y += height;
            height = -height;
        }
        Rectangle2D charRect = new Rectangle2D.Float(x, y, width, height);
        if (getBounds() == null || getBounds().intersects(charRect)) {
            onCharacterFound(glyphs, charRect);
        }
        // advance text matrix and store position for reference
        super.basicTextShowGlyphs(glyphs, advance);
        tx = (AffineTransform) getDeviceTransform().clone();
        tx.concatenate(textState.globalTransform);
        lastStopX = tx.getTranslateX();
        lastStopY = tx.getTranslateY();
    }

    protected void onCharacterFound(PDGlyphs glyphs, Rectangle2D rect) {
        // redefine
    }

    public Shape getBounds() {
        return bounds;
    }

    public void setBounds(Shape bounds) {
        this.bounds = bounds;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy