pdfgraphics2d.java
来自「源码包含生成 PDF 和 HTML 的类库」· Java 代码 · 共 1,670 行 · 第 1/4 页
JAVA
1,670 行
weight = (font.isBold()) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR; } if ((font.isBold() || (weight.floatValue() >= TextAttribute.WEIGHT_SEMIBOLD.floatValue())) && (font.getFontName().equals(font.getName()))) { // Simulate a bold font. float strokeWidth = font.getSize2D() * (weight.floatValue() - TextAttribute.WEIGHT_REGULAR.floatValue()) / 30f; if (strokeWidth != 1) { if(realPaint instanceof Color){ cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); cb.setLineWidth(strokeWidth); Color color = (Color)realPaint; int alpha = color.getAlpha(); if (alpha != currentStrokeGState) { currentStrokeGState = alpha; PdfGState gs = strokeGState[alpha]; if (gs == null) { gs = new PdfGState(); gs.setStrokeOpacity(alpha / 255f); strokeGState[alpha] = gs; } cb.setGState(gs); } cb.setColorStroke(color); restoreTextRenderingMode = true; } } } } double width = 0; if (font.getSize2D() > 0) { float scale = 1000 / font.getSize2D(); Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale)); width = derivedFont.getStringBounds(s, getFontRenderContext()).getWidth(); if (derivedFont.isTransformed()) width /= scale; } // if the hyperlink flag is set add an action to the text Object url = getRenderingHint(HyperLinkKey.KEY_INSTANCE); if (url != null && !url.equals(HyperLinkKey.VALUE_HYPERLINKKEY_OFF)) { float scale = 1000 / font.getSize2D(); Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale)); double height = derivedFont.getStringBounds(s, getFontRenderContext()).getHeight(); if (derivedFont.isTransformed()) height /= scale; double leftX = cb.getXTLM(); double leftY = cb.getYTLM(); PdfAction action = new PdfAction(url.toString()); cb.setAction(action, (float)leftX, (float)leftY, (float)(leftX+width), (float)(leftY+height)); } if (s.length() > 1) { float adv = ((float)width - baseFont.getWidthPoint(s, fontSize)) / (s.length() - 1); cb.setCharacterSpacing(adv); } cb.showText(s); if (s.length() > 1) { cb.setCharacterSpacing(0); } if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth)) cb.setHorizontalScaling(100); // Restore the original TextRenderingMode if needed. if (restoreTextRenderingMode) { cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); } cb.endText(); setTransform(at); if(underline) { // These two are supposed to be taken from the .AFM file //int UnderlinePosition = -100; int UnderlineThickness = 50; // double d = asPoints(UnderlineThickness, (int)fontSize); Stroke savedStroke = originalStroke; setStroke(new BasicStroke((float)d)); y = (float)(y + asPoints(UnderlineThickness, (int)fontSize)); Line2D line = new Line2D.Double(x, y, width+x, y); draw(line); setStroke(savedStroke); } } } /** * @see Graphics#drawString(AttributedCharacterIterator, int, int) */ public void drawString(AttributedCharacterIterator iterator, int x, int y) { drawString(iterator, (float)x, (float)y); } /** * @see Graphics2D#drawString(AttributedCharacterIterator, float, float) */ public void drawString(AttributedCharacterIterator iter, float x, float y) {/* StringBuffer sb = new StringBuffer(); for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) { sb.append(c); } drawString(sb.toString(),x,y);*/ StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex()); for(char c = iter.first(); c != '\uFFFF'; c = iter.next()) { if(iter.getIndex() == iter.getRunStart()) { if(stringbuffer.length() > 0) { drawString(stringbuffer.toString(), x, y); FontMetrics fontmetrics = getFontMetrics(); x = (float)(x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth()); stringbuffer.delete(0, stringbuffer.length()); } doAttributes(iter); } stringbuffer.append(c); } drawString(stringbuffer.toString(), x, y); underline = false; } /** * @see Graphics2D#drawGlyphVector(GlyphVector, float, float) */ public void drawGlyphVector(GlyphVector g, float x, float y) { Shape s = g.getOutline(x, y); fill(s); } /** * @see Graphics2D#fill(Shape) */ public void fill(Shape s) { followPath(s, FILL); } /** * @see Graphics2D#hit(Rectangle, Shape, boolean) */ public boolean hit(Rectangle rect, Shape s, boolean onStroke) { if (onStroke) { s = stroke.createStrokedShape(s); } s = transform.createTransformedShape(s); Area area = new Area(s); if (clip != null) area.intersect(clip); return area.intersects(rect.x, rect.y, rect.width, rect.height); } /** * @see Graphics2D#getDeviceConfiguration() */ public GraphicsConfiguration getDeviceConfiguration() { return dg2.getDeviceConfiguration(); } /** * Method contributed by Alexej Suchov * @see Graphics2D#setComposite(Composite) */ public void setComposite(Composite comp) { if (comp instanceof AlphaComposite) { AlphaComposite composite = (AlphaComposite) comp; if (composite.getRule() == 3) { alpha = composite.getAlpha(); this.composite = composite; if (realPaint != null && (realPaint instanceof Color)) { Color c = (Color) realPaint; paint = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (c.getAlpha() * alpha)); } return; } } this.composite = comp; alpha = 1.0F; } /** * Method contributed by Alexej Suchov * @see Graphics2D#setPaint(Paint) */ public void setPaint(Paint paint) { if (paint == null) return; this.paint = paint; realPaint = paint; if ((composite instanceof AlphaComposite) && (paint instanceof Color)) { AlphaComposite co = (AlphaComposite) composite; if (co.getRule() == 3) { Color c = (Color) paint; this.paint = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (c.getAlpha() * alpha)); realPaint = paint; } } } private Stroke transformStroke(Stroke stroke) { if (!(stroke instanceof BasicStroke)) return stroke; BasicStroke st = (BasicStroke)stroke; float scale = (float)Math.sqrt(Math.abs(transform.getDeterminant())); float dash[] = st.getDashArray(); if (dash != null) { for (int k = 0; k < dash.length; ++k) dash[k] *= scale; } return new BasicStroke(st.getLineWidth() * scale, st.getEndCap(), st.getLineJoin(), st.getMiterLimit(), dash, st.getDashPhase() * scale); } private void setStrokeDiff(Stroke newStroke, Stroke oldStroke) { if (newStroke == oldStroke) return; if (!(newStroke instanceof BasicStroke)) return; BasicStroke nStroke = (BasicStroke)newStroke; boolean oldOk = (oldStroke instanceof BasicStroke); BasicStroke oStroke = null; if (oldOk) oStroke = (BasicStroke)oldStroke; if (!oldOk || nStroke.getLineWidth() != oStroke.getLineWidth()) cb.setLineWidth(nStroke.getLineWidth()); if (!oldOk || nStroke.getEndCap() != oStroke.getEndCap()) { switch (nStroke.getEndCap()) { case BasicStroke.CAP_BUTT: cb.setLineCap(0); break; case BasicStroke.CAP_SQUARE: cb.setLineCap(2); break; default: cb.setLineCap(1); } } if (!oldOk || nStroke.getLineJoin() != oStroke.getLineJoin()) { switch (nStroke.getLineJoin()) { case BasicStroke.JOIN_MITER: cb.setLineJoin(0); break; case BasicStroke.JOIN_BEVEL: cb.setLineJoin(2); break; default: cb.setLineJoin(1); } } if (!oldOk || nStroke.getMiterLimit() != oStroke.getMiterLimit()) cb.setMiterLimit(nStroke.getMiterLimit()); boolean makeDash; if (oldOk) { if (nStroke.getDashArray() != null) { if (nStroke.getDashPhase() != oStroke.getDashPhase()) { makeDash = true; } else if (!java.util.Arrays.equals(nStroke.getDashArray(), oStroke.getDashArray())) { makeDash = true; } else makeDash = false; } else if (oStroke.getDashArray() != null) { makeDash = true; } else makeDash = false; } else { makeDash = true; } if (makeDash) { float dash[] = nStroke.getDashArray(); if (dash == null) cb.setLiteral("[]0 d\n"); else { cb.setLiteral('['); int lim = dash.length; for (int k = 0; k < lim; ++k) { cb.setLiteral(dash[k]); cb.setLiteral(' '); } cb.setLiteral(']'); cb.setLiteral(nStroke.getDashPhase()); cb.setLiteral(" d\n"); } } } /** * @see Graphics2D#setStroke(Stroke) */ public void setStroke(Stroke s) { originalStroke = s; this.stroke = transformStroke(s); } /** * Sets a rendering hint * @param arg0 * @param arg1 */ public void setRenderingHint(Key arg0, Object arg1) { if (arg1 != null) { rhints.put(arg0, arg1); } else { if (arg0 instanceof HyperLinkKey) { rhints.put(arg0, HyperLinkKey.VALUE_HYPERLINKKEY_OFF); } else { rhints.remove(arg0); } } } /** * @param arg0 a key * @return the rendering hint */ public Object getRenderingHint(Key arg0) { return rhints.get(arg0); } /** * @see Graphics2D#setRenderingHints(Map) */ public void setRenderingHints(Map hints) { rhints.clear(); rhints.putAll(hints); } /** * @see Graphics2D#addRenderingHints(Map) */ public void addRenderingHints(Map hints) { rhints.putAll(hints); } /** * @see Graphics2D#getRenderingHints() */ public RenderingHints getRenderingHints() { return rhints; } /** * @see Graphics#translate(int, int) */ public void translate(int x, int y) { translate((double)x, (double)y); } /** * @see Graphics2D#translate(double, double) */ public void translate(double tx, double ty) { transform.translate(tx,ty); } /** * @see Graphics2D#rotate(double) */ public void rotate(double theta) { transform.rotate(theta); } /** * @see Graphics2D#rotate(double, double, double) */ public void rotate(double theta, double x, double y) { transform.rotate(theta, x, y); } /** * @see Graphics2D#scale(double, double) */ public void scale(double sx, double sy) { transform.scale(sx, sy); this.stroke = transformStroke(originalStroke); } /** * @see Graphics2D#shear(double, double) */ public void shear(double shx, double shy) { transform.shear(shx, shy); } /** * @see Graphics2D#transform(AffineTransform) */ public void transform(AffineTransform tx) { transform.concatenate(tx); this.stroke = transformStroke(originalStroke); } /** * @see Graphics2D#setTransform(AffineTransform) */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?