org.apache.poi.util.Units Java Examples

The following examples show how to use org.apache.poi.util.Units. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageHeaderBitmap.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ImageHeaderBitmap(byte data[], int offset) {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new ByteArrayInputStream(data, offset, data.length-offset));
    } catch (IOException e) {
        LOG.log(POILogger.WARN, "Can't determine image dimensions", e);
    }
    // set dummy size, in case of dummy dimension can't be set
    size = (img == null)
        ? new Dimension(200,200)
        : new Dimension(
            (int)Units.pixelToPoints(img.getWidth()),
            (int)Units.pixelToPoints(img.getHeight())
        );
}
 
Example #2
Source File: ImageHeaderWMF.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ImageHeaderWMF(Rectangle dim) {
    handle = 0;
    left = dim.x;
    top = dim.y;
    right = dim.x + dim.width;
    bottom = dim.y + dim.height;
    inch = Units.POINT_DPI; //default resolution is 72 dpi
    reserved = 0;
}
 
Example #3
Source File: ImageHeaderWMF.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ImageHeaderWMF(byte[] data, final int off) {
    int offset = off;
    int key = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE; //header key
    if (key != APMHEADER_KEY) {
        LOG.log(POILogger.WARN, "WMF file doesn't contain a placeable header - ignore parsing");
        handle = 0;
        left = 0;
        top = 0;
        right = 200;
        bottom = 200;
        inch = Units.POINT_DPI; //default resolution is 72 dpi
        reserved = 0;
        return;
    }

    handle = LittleEndian.getUShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    left = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    top = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    right = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    bottom = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;

    inch = LittleEndian.getUShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    reserved = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;

    checksum = LittleEndian.getShort(data, offset); offset += LittleEndian.SHORT_SIZE;
    if (checksum != getChecksum()){
        LOG.log(POILogger.WARN, "WMF checksum does not match the header data");
    }
}
 
Example #4
Source File: DrawTextParagraph.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
    public void draw(Graphics2D graphics){
        if (lines.isEmpty()) {
            return;
        }

        double penY = y;

        boolean firstLine = true;
        int indentLevel = paragraph.getIndentLevel();
        Double leftMargin = paragraph.getLeftMargin();
        if (leftMargin == null) {
            // if the marL attribute is omitted, then a value of 347663 is implied
            leftMargin = Units.toPoints(347663L*indentLevel);
        }
        Double indent = paragraph.getIndent();
        if (indent == null) {
            indent = Units.toPoints(347663L*indentLevel);
        }
        if (isHSLF()) {
            // special handling for HSLF
            indent -= leftMargin;
        }

//        Double rightMargin = paragraph.getRightMargin();
//        if (rightMargin == null) {
//            rightMargin = 0d;
//        }

        //The vertical line spacing
        Double spacing = paragraph.getLineSpacing();
        if (spacing == null) {
            spacing = 100d;
        }

        for(DrawTextFragment line : lines){
            double penX;

            if(firstLine) {
                if (!isEmptyParagraph()) {
                    // TODO: find out character style for empty, but bulleted/numbered lines
                    bullet = getBullet(graphics, line.getAttributedString().getIterator());
                }

                if (bullet != null){
                    bullet.setPosition(x+leftMargin+indent, penY);
                    bullet.draw(graphics);
                    // don't let text overlay the bullet and advance by the bullet width
                    double bulletWidth = bullet.getLayout().getAdvance() + 1;
                    penX = x + Math.max(leftMargin, leftMargin+indent+bulletWidth);
                } else {
                    penX = x + leftMargin;
                }
            } else {
                penX = x + leftMargin;
            }

            Rectangle2D anchor = DrawShape.getAnchor(graphics, paragraph.getParentShape());
            // Insets are already applied on DrawTextShape.drawContent
            // but (outer) anchor need to be adjusted
            Insets2D insets = paragraph.getParentShape().getInsets();
            double leftInset = insets.left;
            double rightInset = insets.right;

            TextAlign ta = paragraph.getTextAlign();
            if (ta == null) {
                ta = TextAlign.LEFT;
            }
            switch (ta) {
                case CENTER:
                    penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset - leftMargin) / 2;
                    break;
                case RIGHT:
                    penX += (anchor.getWidth() - line.getWidth() - leftInset - rightInset);
                    break;
                default:
                    break;
            }

            line.setPosition(penX, penY);
            line.draw(graphics);
            
            if(spacing > 0) {
                // If linespacing >= 0, then linespacing is a percentage of normal line height.
                penY += spacing*0.01* line.getHeight();
            } else {
                // negative value means absolute spacing in points
                penY += -spacing;
            }

            firstLine = false;
        }

        y = penY - y;
    }
 
Example #5
Source File: DrawTextParagraph.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns wrapping width to break lines in this paragraph
 *
 * @param firstLine whether the first line is breaking
 *
 * @return  wrapping width in points
 */
protected double getWrappingWidth(boolean firstLine, Graphics2D graphics){
    TextShape<?,?> ts = paragraph.getParentShape();

    // internal margins for the text box
    Insets2D insets = ts.getInsets();
    double leftInset = insets.left;
    double rightInset = insets.right;

    int indentLevel = paragraph.getIndentLevel();
    if (indentLevel == -1) {
        // default to 0, if indentLevel is not set
        indentLevel = 0;
    }
    Double leftMargin = paragraph.getLeftMargin();
    if (leftMargin == null) {
        // if the marL attribute is omitted, then a value of 347663 is implied
        leftMargin = Units.toPoints(347663L*(indentLevel+1));
    }
    Double indent = paragraph.getIndent();
    if (indent == null) {
        indent = Units.toPoints(347663L*indentLevel);
    }
    Double rightMargin = paragraph.getRightMargin();
    if (rightMargin == null) {
        rightMargin = 0d;
    }

    Rectangle2D anchor = DrawShape.getAnchor(graphics, ts);
    TextDirection textDir = ts.getTextDirection();
    double width;
    if (!ts.getWordWrap()) {
        Dimension pageDim = ts.getSheet().getSlideShow().getPageSize();
        // if wordWrap == false then we return the advance to the (right) border of the sheet
        switch (textDir) {
            default:
                width = pageDim.getWidth() - anchor.getX();
                break;
            case VERTICAL:
                width = pageDim.getHeight() - anchor.getX();
                break;
            case VERTICAL_270:
                width = anchor.getX();
                break;
        }
    } else {
        switch (textDir) {
            default:
                width = anchor.getWidth() - leftInset - rightInset - leftMargin - rightMargin;
                break;
            case VERTICAL:
            case VERTICAL_270:
                width = anchor.getHeight() - leftInset - rightInset - leftMargin - rightMargin;
                break;
        }
        if (firstLine && !isHSLF()) {
            if (bullet != null){
                if (indent > 0) {
                    width -= indent;
                }
            } else {
                if (indent > 0) {
                    width -= indent; // first line indentation
                } else if (indent < 0) { // hanging indentation: the first line start at the left margin
                    width += leftMargin;
                }
            }
        }
    }

    return width;
}
 
Example #6
Source File: DrawSimpleShape.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected Collection<Outline> computeOutlines(Graphics2D graphics) {
    final SimpleShape<?,?> sh = getShape();

    List<Outline> lst = new ArrayList<Outline>();
    CustomGeometry geom = sh.getGeometry();
    if(geom == null) {
        return lst;
    }

    Rectangle2D anchor = getAnchor(graphics, sh);
    for (Path p : geom) {

        double w = p.getW(), h = p.getH(), scaleX = Units.toPoints(1), scaleY = scaleX;
        if (w == -1) {
            w = Units.toEMU(anchor.getWidth());
        } else {
            scaleX = anchor.getWidth() / w;
        }
        if (h == -1) {
            h = Units.toEMU(anchor.getHeight());
        } else {
            scaleY = anchor.getHeight() / h;
        }

        // the guides in the shape definitions are all defined relative to each other,
        // so we build the path starting from (0,0).
        final Rectangle2D pathAnchor = new Rectangle2D.Double(0,0,w,h);

        Context ctx = new Context(geom, pathAnchor, sh);

        java.awt.Shape gp = p.getPath(ctx);

        // translate the result to the canvas coordinates in points
        AffineTransform at = new AffineTransform();
        at.translate(anchor.getX(), anchor.getY());
        at.scale(scaleX, scaleY);

        java.awt.Shape canvasShape = at.createTransformedShape(gp);

        lst.add(new Outline(canvasShape, p));
    }

    return lst;
}
 
Example #7
Source File: ImageHeaderWMF.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Dimension getSize() {
    //coefficient to translate from WMF dpi to 72dpi
    double coeff = ((double)Units.POINT_DPI)/inch;
    return new Dimension((int)Math.round((right-left)*coeff), (int)Math.round((bottom-top)*coeff));
}
 
Example #8
Source File: ImageUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static double getRowHeightInPixels(Sheet sheet, int rowNum) {
    Row r = sheet.getRow(rowNum);
    double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints();
    return Units.toEMU(points)/(double)EMU_PER_PIXEL;
}
 
Example #9
Source File: WordDocument.java    From tutorials with MIT License 4 votes vote down vote up
public void handleSimpleDoc() throws Exception {
    XWPFDocument document = new XWPFDocument();

    XWPFParagraph title = document.createParagraph();
    title.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun titleRun = title.createRun();
    titleRun.setText("Build Your REST API with Spring");
    titleRun.setColor("009933");
    titleRun.setBold(true);
    titleRun.setFontFamily("Courier");
    titleRun.setFontSize(20);

    XWPFParagraph subTitle = document.createParagraph();
    subTitle.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun subTitleRun = subTitle.createRun();
    subTitleRun.setText("from HTTP fundamentals to API Mastery");
    subTitleRun.setColor("00CC44");
    subTitleRun.setFontFamily("Courier");
    subTitleRun.setFontSize(16);
    subTitleRun.setTextPosition(20);
    subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);

    XWPFParagraph image = document.createParagraph();
    image.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun imageRun = image.createRun();
    imageRun.setTextPosition(20);
    Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI());
    imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50));

    XWPFParagraph sectionTitle = document.createParagraph();
    XWPFRun sectionTRun = sectionTitle.createRun();
    sectionTRun.setText("What makes a good API?");
    sectionTRun.setColor("00CC44");
    sectionTRun.setBold(true);
    sectionTRun.setFontFamily("Courier");

    XWPFParagraph para1 = document.createParagraph();
    para1.setAlignment(ParagraphAlignment.BOTH);
    String string1 = convertTextFileToString(paragraph1);
    XWPFRun para1Run = para1.createRun();
    para1Run.setText(string1);

    XWPFParagraph para2 = document.createParagraph();
    para2.setAlignment(ParagraphAlignment.RIGHT);
    String string2 = convertTextFileToString(paragraph2);
    XWPFRun para2Run = para2.createRun();
    para2Run.setText(string2);
    para2Run.setItalic(true);

    XWPFParagraph para3 = document.createParagraph();
    para3.setAlignment(ParagraphAlignment.LEFT);
    String string3 = convertTextFileToString(paragraph3);
    XWPFRun para3Run = para3.createRun();
    para3Run.setText(string3);

    FileOutputStream out = new FileOutputStream(output);
    document.write(out);
    out.close();
    document.close();
}