📄 ppgraphics2d.java
字号:
/* ==================================================================== 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.image.*;import java.awt.image.renderable.RenderableImage;import java.awt.geom.AffineTransform;import java.awt.geom.PathIterator;import java.text.AttributedCharacterIterator;import java.util.Map;import java.util.ArrayList;import org.apache.poi.ddf.EscherProperties;import org.apache.poi.hslf.usermodel.RichTextRun;import org.apache.poi.hslf.exceptions.HSLFException;/** * Translates Graphics2D calls into PowerPoint. * * @author Yegor Kozlov */public class PPGraphics2D extends Graphics2D { //The group to write the graphics calls into. private ShapeGroup group; private AffineTransform transform; private Stroke stroke; private Paint paint; private Font font; private Color foreground; private Color background = Color.white; private Shape clip; int count = 0; /** * 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(); } /** * @return the shape group being used for drawing */ public ShapeGroup getShapeGroup(){ return group; } public Font getFont(){ return font; } public void setFont(Font font){ this.font = font; } public Color getColor(){ return foreground; } public void setColor(Color color) { this.foreground = color; } public Stroke getStroke(){ return stroke; } public void setStroke(Stroke s){ this.stroke = s; } public Paint getPaint(){ return paint; } public void setPaint(Paint paint){ this.paint = paint; if (paint instanceof Color) setColor((Color)paint); } public AffineTransform getTransform(){ return (AffineTransform)transform.clone(); } public void setTransform(AffineTransform trans) { transform = (AffineTransform)trans.clone(); } public void draw(Shape shape){ if(clip != null) { java.awt.Rectangle bounds = getTransform().createTransformedShape(shape).getBounds(); if (bounds.width == 0) bounds.width = 1; if (bounds.height == 0) bounds.height = 1; if (!clip.getBounds().contains(bounds)) { return; } } PathIterator it = shape.getPathIterator(transform); double[] prev = null; double[] coords = new double[6]; double[] first = new double[6]; if(!it.isDone()) it.currentSegment(first); //first point while(!it.isDone()){ int type = it.currentSegment(coords); if (prev != null ){ Line line = new Line(group); if (stroke instanceof BasicStroke){ BasicStroke bs = (BasicStroke)stroke; line.setLineWidth(bs.getLineWidth()); float[] dash = bs.getDashArray(); if (dash != null) line.setLineDashing(Line.PEN_DASH); } if(getColor() != null) line.setLineColor(getColor()); if (type == PathIterator.SEG_LINETO) { line.setAnchor(new java.awt.Rectangle((int)prev[0], (int)prev[1], (int)(coords[0] - prev[0]), (int)(coords[1] - prev[1]))); } else if (type == PathIterator.SEG_CLOSE){ line.setAnchor(new java.awt.Rectangle((int)coords[0], (int)coords[1], (int)(first[0] - coords[0]), (int)(first[1] - coords[1]))); } group.addShape(line); } prev = new double[]{coords[0], coords[1]}; it.next(); } } public void drawString(String string, float x, float y){ TextBox txt = new TextBox(group); txt.getTextRun().supplySlideShow(group.getSheet().getSlideShow()); txt.getTextRun().setSheet(group.getSheet()); txt.setText(string); 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); if (!"".equals(string)) txt.resizeToFitText(); int height = (int)txt.getAnchor().getHeight(); /* 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.moveTo((int)x, (int)(y - height)); if(clip != null) { if (!clip.getBounds().contains(txt.getAnchor())) { ;//return; } } group.addShape(txt); } public void fill(Shape shape){ if(clip != null) { java.awt.Rectangle bounds = getTransform().createTransformedShape(shape).getBounds(); if (bounds.width == 0) bounds.width = 1; if (bounds.height == 0) bounds.height = 1; if (!clip.getBounds().contains(bounds)) { return; } } PathIterator it = shape.getPathIterator(transform); ArrayList pnt = new ArrayList(); double[] coords = new double[6]; while(!it.isDone()){ int type = it.currentSegment(coords); if (type != PathIterator.SEG_CLOSE) { pnt.add(new Point((int)coords[0], (int)coords[1])); } it.next(); } int[] xPoints= new int[pnt.size()]; int[] yPoints= new int[pnt.size()]; for (int i = 0; i < pnt.size(); i++) { Point p = (Point)pnt.get(i); xPoints[i] = p.x; yPoints[i] = p.y; } AutoShape r = new AutoShape(ShapeTypes.Rectangle); if (paint instanceof Color){ Color color = (Color)paint; r.setFillColor(color); } if(getColor() != null) r.setLineColor(getColor()); if (stroke instanceof BasicStroke){ BasicStroke bs = (BasicStroke)stroke; r.setLineWidth(bs.getLineWidth()); float[] dash = bs.getDashArray(); if (dash != null) r.setLineDashing(Line.PEN_DASH); } java.awt.Rectangle bounds = transform.createTransformedShape(shape).getBounds(); r.setAnchor(bounds); group.addShape(r); } public void translate(int x, int y) { AffineTransform at = new AffineTransform(); at.translate(x, y); transform.concatenate(at); } public void clip(Shape shape) { this.clip = transform.createTransformedShape(shape); //update size of the escher group which holds the drawing group.setAnchor(clip.getBounds()); } public Shape getClip() { return clip; } public void scale(double sx, double sy) { AffineTransform at = new AffineTransform(); at.scale(sx, sy); transform.concatenate(at); } //=============================================== public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) { throw new HSLFException("Not implemented"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -