ppgraphics2d.java

来自「EXCEL read and write」· Java 代码 · 共 1,499 行 · 第 1/5 页

JAVA
1,499
字号
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */package org.apache.poi.hslf.model;import java.awt.*;import java.awt.Shape;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.font.TextLayout;import java.awt.image.*;import java.awt.image.renderable.RenderableImage;import java.awt.geom.*;import java.text.AttributedCharacterIterator;import java.util.Map;import org.apache.poi.hslf.usermodel.RichTextRun;import org.apache.poi.hslf.exceptions.HSLFException;import org.apache.poi.util.POILogger;import org.apache.poi.util.POILogFactory;/** * Translates Graphics2D calls into PowerPoint. * * @author Yegor Kozlov */public class PPGraphics2D extends Graphics2D implements Cloneable {    protected POILogger log = POILogFactory.getLogger(this.getClass());    //The ppt object to write into.    private ShapeGroup group;    private AffineTransform transform;    private Stroke stroke;    private Paint paint;    private Font font;    private Color foreground;    private Color background;    private RenderingHints hints;    /**     * Construct Java Graphics object which translates graphic calls in ppt drawing layer.     *     * @param group           The shape group to write the graphics calls into.     */    public PPGraphics2D(ShapeGroup group){        this.group = group;        transform = new AffineTransform();        stroke = new BasicStroke();        paint = Color.black;        font = new Font("Arial", Font.PLAIN, 12);        background = Color.black;        foreground = Color.white;        hints = new RenderingHints(null);    }    /**     * @return  the shape group being used for drawing     */    public ShapeGroup getShapeGroup(){        return group;    }    /**     * Gets the current font.     * @return    this graphics context's current font.     * @see       java.awt.Font     * @see       java.awt.Graphics#setFont(Font)     */    public Font getFont(){        return font;    }    /**     * Sets this graphics context's font to the specified font.     * All subsequent text operations using this graphics context     * use this font.     * @param  font   the font.     * @see     java.awt.Graphics#getFont     * @see     java.awt.Graphics#drawString(java.lang.String, int, int)     * @see     java.awt.Graphics#drawBytes(byte[], int, int, int, int)     * @see     java.awt.Graphics#drawChars(char[], int, int, int, int)    */    public void setFont(Font font){        this.font = font;    }    /**     * Gets this graphics context's current color.     * @return    this graphics context's current color.     * @see       java.awt.Color     * @see       java.awt.Graphics#setColor     */     public Color getColor(){        return foreground;    }    /**     * Sets this graphics context's current color to the specified     * color. All subsequent graphics operations using this graphics     * context use this specified color.     * @param     c   the new rendering color.     * @see       java.awt.Color     * @see       java.awt.Graphics#getColor     */    public void setColor(Color c) {        setPaint(c);    }    /**     * Returns the current <code>Stroke</code> in the     * <code>Graphics2D</code> context.     * @return the current <code>Graphics2D</code> <code>Stroke</code>,     *                 which defines the line style.     * @see #setStroke     */    public Stroke getStroke(){        return stroke;    }    /**     * Sets the <code>Stroke</code> for the <code>Graphics2D</code> context.     * @param s the <code>Stroke</code> object to be used to stroke a     * <code>Shape</code> during the rendering process     */    public void setStroke(Stroke s){        this.stroke = s;    }    /**     * Returns the current <code>Paint</code> of the     * <code>Graphics2D</code> context.     * @return the current <code>Graphics2D</code> <code>Paint</code>,     * which defines a color or pattern.     * @see #setPaint     * @see java.awt.Graphics#setColor     */    public Paint getPaint(){        return paint;    }    /**     * Sets the <code>Paint</code> attribute for the     * <code>Graphics2D</code> context.  Calling this method     * with a <code>null</code> <code>Paint</code> object does     * not have any effect on the current <code>Paint</code> attribute     * of this <code>Graphics2D</code>.     * @param paint the <code>Paint</code> object to be used to generate     * color during the rendering process, or <code>null</code>     * @see java.awt.Graphics#setColor     */     public void setPaint(Paint paint){        if(paint == null) return;        this.paint = paint;        if (paint instanceof Color) foreground = (Color)paint;    }    /**     * Returns a copy of the current <code>Transform</code> in the     * <code>Graphics2D</code> context.     * @return the current <code>AffineTransform</code> in the     *             <code>Graphics2D</code> context.     * @see #transform     * @see #setTransform     */    public AffineTransform getTransform(){        return new AffineTransform(transform);    }    /**     * Sets the <code>Transform</code> in the <code>Graphics2D</code>     * context.     * @param Tx the <code>AffineTransform</code> object to be used in the     * rendering process     * @see #transform     * @see AffineTransform     */    public void setTransform(AffineTransform Tx) {        transform = new AffineTransform(Tx);    }    /**     * Strokes the outline of a <code>Shape</code> using the settings of the     * current <code>Graphics2D</code> context.  The rendering attributes     * applied include the <code>Clip</code>, <code>Transform</code>,     * <code>Paint</code>, <code>Composite</code> and     * <code>Stroke</code> attributes.     * @param shape the <code>Shape</code> to be rendered     * @see #setStroke     * @see #setPaint     * @see java.awt.Graphics#setColor     * @see #transform     * @see #setTransform     * @see #clip     * @see #setClip     * @see #setComposite     */    public void draw(Shape shape){        GeneralPath path = new GeneralPath(transform.createTransformedShape(shape));        Freeform p = new Freeform(group);        p.setPath(path);        p.getFill().setForegroundColor(null);        applyStroke(p);        group.addShape(p);    }    /**     * Renders the text specified by the specified <code>String</code>,     * using the current text attribute state in the <code>Graphics2D</code> context.     * The baseline of the first character is at position     * (<i>x</i>,&nbsp;<i>y</i>) in the User Space.     * The rendering attributes applied include the <code>Clip</code>,     * <code>Transform</code>, <code>Paint</code>, <code>Font</code> and     * <code>Composite</code> attributes. For characters in script systems     * such as Hebrew and Arabic, the glyphs can be rendered from right to     * left, in which case the coordinate supplied is the location of the     * leftmost character on the baseline.     * @param s the <code>String</code> to be rendered     * @param x the x coordinate of the location where the     * <code>String</code> should be rendered     * @param y the y coordinate of the location where the     * <code>String</code> should be rendered     * @throws NullPointerException if <code>str</code> is     *         <code>null</code>     * @see #setPaint     * @see java.awt.Graphics#setColor     * @see java.awt.Graphics#setFont     * @see #setTransform     * @see #setComposite     * @see #setClip     */    public void drawString(String s, float x, float y) {        TextBox txt = new TextBox(group);        txt.getTextRun().supplySlideShow(group.getSheet().getSlideShow());        txt.getTextRun().setSheet(group.getSheet());        txt.setText(s);        RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];        rt.setFontSize(font.getSize());        rt.setFontName(font.getFamily());        if (getColor() != null) rt.setFontColor(getColor());        if (font.isBold()) rt.setBold(true);        if (font.isItalic()) rt.setItalic(true);        txt.setMarginBottom(0);        txt.setMarginTop(0);        txt.setMarginLeft(0);        txt.setMarginRight(0);        txt.setWordWrap(TextBox.WrapNone);        txt.setHorizontalAlignment(TextBox.AlignLeft);        txt.setVerticalAlignment(TextBox.AnchorMiddle);        TextLayout layout = new TextLayout(s, font, getFontRenderContext());        float ascent = layout.getAscent();        float width = (float) Math.floor(layout.getAdvance());        /**         * Even if top and bottom margins are set to 0 PowerPoint         * always sets extra space between the text and its bounding box.         *         * The approximation height = ascent*2 works good enough in most cases         */        float height = ascent * 2;        /*          In powerpoint anchor of a shape is its top left corner.          Java graphics sets string coordinates by the baseline of the first character          so we need to shift up by the height of the textbox        */        y -= height / 2 + ascent / 2;        /*          In powerpoint anchor of a shape is its top left corner.          Java graphics sets string coordinates by the baseline of the first character          so we need to shift down by the height of the textbox        */        txt.setAnchor(new Rectangle2D.Float(x, y, width, height));        group.addShape(txt);    }    /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?