| Method from com.lowagie.text.pdf.PdfGraphics2D Detail: |
public void addRenderingHints(Map hints) {
rhints.putAll(hints);
}
|
public static double asPoints(double d,
int i) {
return d * i / AFM_DIVISOR;
}
Calculates position and/or stroke thickness depending on the font size |
public void clearRect(int x,
int y,
int width,
int height) {
Paint temp = paint;
setPaint(background);
fillRect(x,y,width,height);
setPaint(temp);
}
|
public void clip(Shape s) {
if (s == null) {
setClip(null);
return;
}
s = transform.createTransformedShape(s);
if (clip == null)
clip = new Area(s);
else
clip.intersect(new Area(s));
followPath(s, CLIP);
}
|
public void clipRect(int x,
int y,
int width,
int height) {
Rectangle2D rect = new Rectangle2D.Double(x,y,width,height);
clip(rect);
}
|
public void copyArea(int x,
int y,
int width,
int height,
int dx,
int dy) {
}
|
public Graphics create() {
PdfGraphics2D g2 = new PdfGraphics2D();
g2.onlyShapes = this.onlyShapes;
g2.transform = new AffineTransform(this.transform);
g2.baseFonts = this.baseFonts;
g2.fontMapper = this.fontMapper;
g2.paint = this.paint;
g2.fillGState = this.fillGState;
g2.strokeGState = this.strokeGState;
g2.background = this.background;
g2.mediaTracker = this.mediaTracker;
g2.convertImagesToJPEG = this.convertImagesToJPEG;
g2.jpegQuality = this.jpegQuality;
g2.setFont(this.font);
g2.cb = this.cb.getDuplicate();
g2.cb.saveState();
g2.width = this.width;
g2.height = this.height;
g2.followPath(new Area(new Rectangle2D.Float(0, 0, width, height)), CLIP);
if (this.clip != null)
g2.clip = new Area(this.clip);
g2.composite = composite;
g2.stroke = stroke;
g2.originalStroke = originalStroke;
g2.strokeOne = (BasicStroke)g2.transformStroke(g2.strokeOne);
g2.oldStroke = g2.strokeOne;
g2.setStrokeDiff(g2.oldStroke, null);
g2.cb.saveState();
if (g2.clip != null)
g2.followPath(g2.clip, CLIP);
g2.kid = true;
if (this.kids == null)
this.kids = new ArrayList();
this.kids.add(new Integer(cb.getInternalBuffer().size()));
this.kids.add(g2);
return g2;
}
|
public void dispose() {
if (kid)
return;
if (!disposeCalled) {
disposeCalled = true;
cb.restoreState();
cb.restoreState();
dg2.dispose();
dg2 = null;
if (kids != null) {
ByteBuffer buf = new ByteBuffer();
internalDispose(buf);
ByteBuffer buf2 = cb.getInternalBuffer();
buf2.reset();
buf2.append(buf);
}
}
}
|
protected void doAttributes(AttributedCharacterIterator iter) {
underline = false;
Set set = iter.getAttributes().keySet();
for(Iterator iterator = set.iterator(); iterator.hasNext();) {
AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute)iterator.next();
if (!(attribute instanceof TextAttribute))
continue;
TextAttribute textattribute = (TextAttribute)attribute;
if(textattribute.equals(TextAttribute.FONT)) {
Font font = (Font)iter.getAttributes().get(textattribute);
setFont(font);
}
else if(textattribute.equals(TextAttribute.UNDERLINE)) {
if(iter.getAttributes().get(textattribute) == TextAttribute.UNDERLINE_ON)
underline = true;
}
else if(textattribute.equals(TextAttribute.SIZE)) {
Object obj = iter.getAttributes().get(textattribute);
if(obj instanceof Integer) {
int i = ((Integer)obj).intValue();
setFont(getFont().deriveFont(getFont().getStyle(), i));
}
else if(obj instanceof Float) {
float f = ((Float)obj).floatValue();
setFont(getFont().deriveFont(getFont().getStyle(), f));
}
}
else if(textattribute.equals(TextAttribute.FOREGROUND)) {
setColor((Color) iter.getAttributes().get(textattribute));
}
else if(textattribute.equals(TextAttribute.FAMILY)) {
Font font = getFont();
Map fontAttributes = font.getAttributes();
fontAttributes.put(TextAttribute.FAMILY, iter.getAttributes().get(textattribute));
setFont(font.deriveFont(fontAttributes));
}
else if(textattribute.equals(TextAttribute.POSTURE)) {
Font font = getFont();
Map fontAttributes = font.getAttributes();
fontAttributes.put(TextAttribute.POSTURE, iter.getAttributes().get(textattribute));
setFont(font.deriveFont(fontAttributes));
}
else if(textattribute.equals(TextAttribute.WEIGHT)) {
Font font = getFont();
Map fontAttributes = font.getAttributes();
fontAttributes.put(TextAttribute.WEIGHT, iter.getAttributes().get(textattribute));
setFont(font.deriveFont(fontAttributes));
}
}
}
This routine goes through the attributes and sets the font
before calling the actual string drawing routine |
public void draw(Shape s) {
followPath(s, STROKE);
}
|
public void drawArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle) {
Arc2D arc = new Arc2D.Double(x,y,width,height,startAngle, arcAngle, Arc2D.OPEN);
draw(arc);
}
|
public void drawGlyphVector(GlyphVector g,
float x,
float y) {
Shape s = g.getOutline(x, y);
fill(s);
}
|
public boolean drawImage(Image img,
AffineTransform xform,
ImageObserver obs) {
return drawImage(img, null, xform, null, obs);
}
|
public void drawImage(BufferedImage img,
BufferedImageOp op,
int x,
int y) {
BufferedImage result = img;
if (op != null) {
result = op.createCompatibleDestImage(img, img.getColorModel());
result = op.filter(img, result);
}
drawImage(result, x, y, null);
}
|
public boolean drawImage(Image img,
int x,
int y,
ImageObserver observer) {
return drawImage(img, x, y, null, observer);
}
|
public boolean drawImage(Image img,
int x,
int y,
Color bgcolor,
ImageObserver observer) {
waitForImage(img);
return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer);
}
|
public boolean drawImage(Image img,
int x,
int y,
int width,
int height,
ImageObserver observer) {
return drawImage(img, x, y, width, height, null, observer);
}
|
public boolean drawImage(Image img,
int x,
int y,
int width,
int height,
Color bgcolor,
ImageObserver observer) {
waitForImage(img);
double scalex = width/(double)img.getWidth(observer);
double scaley = height/(double)img.getHeight(observer);
AffineTransform tx = AffineTransform.getTranslateInstance(x,y);
tx.scale(scalex,scaley);
return drawImage(img, null, tx, bgcolor, observer);
}
|
public boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
ImageObserver observer) {
return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null, observer);
}
|
public boolean drawImage(Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
Color bgcolor,
ImageObserver observer) {
waitForImage(img);
double dwidth = (double)dx2-dx1;
double dheight = (double)dy2-dy1;
double swidth = (double)sx2-sx1;
double sheight = (double)sy2-sy1;
//if either width or height is 0, then there is nothing to draw
if (dwidth == 0 || dheight == 0 || swidth == 0 || sheight == 0) return true;
double scalex = dwidth/swidth;
double scaley = dheight/sheight;
double transx = sx1*scalex;
double transy = sy1*scaley;
AffineTransform tx = AffineTransform.getTranslateInstance(dx1-transx,dy1-transy);
tx.scale(scalex,scaley);
BufferedImage mask = new BufferedImage(img.getWidth(observer), img.getHeight(observer), BufferedImage.TYPE_BYTE_BINARY);
Graphics g = mask.getGraphics();
g.fillRect(sx1,sy1, (int)swidth, (int)sheight);
drawImage(img, mask, tx, null, observer);
g.dispose();
return true;
}
|
public void drawLine(int x1,
int y1,
int x2,
int y2) {
Line2D line = new Line2D.Double(x1, y1, x2, y2);
draw(line);
}
|
public void drawOval(int x,
int y,
int width,
int height) {
Ellipse2D oval = new Ellipse2D.Float(x, y, width, height);
draw(oval);
}
|
public void drawPolygon(int[] xPoints,
int[] yPoints,
int nPoints) {
Polygon poly = new Polygon(xPoints, yPoints, nPoints);
draw(poly);
}
|
public void drawPolyline(int[] x,
int[] y,
int nPoints) {
PolylineShape polyline = new PolylineShape(x, y, nPoints);
draw(polyline);
}
|
public void drawRect(int x,
int y,
int width,
int height) {
draw(new Rectangle(x, y, width, height));
}
|
public void drawRenderableImage(RenderableImage img,
AffineTransform xform) {
drawRenderedImage(img.createDefaultRendering(), xform);
}
|
public void drawRenderedImage(RenderedImage img,
AffineTransform xform) {
BufferedImage image = null;
if (img instanceof BufferedImage) {
image = (BufferedImage)img;
} else {
ColorModel cm = img.getColorModel();
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable properties = new Hashtable();
String[] keys = img.getPropertyNames();
if (keys!=null) {
for (int i = 0; i < keys.length; i++) {
properties.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
img.copyData(raster);
image=result;
}
drawImage(image, xform, null);
}
|
public void drawRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight) {
RoundRectangle2D rect = new RoundRectangle2D.Double(x,y,width,height,arcWidth, arcHeight);
draw(rect);
}
|
public void drawString(String s,
int x,
int y) {
drawString(s, (float)x, (float)y);
}
|
public void drawString(String s,
float x,
float y) {
if (s.length() == 0)
return;
setFillPaint();
if (onlyShapes) {
drawGlyphVector(this.font.layoutGlyphVector(getFontRenderContext(), s.toCharArray(), 0, s.length(), java.awt.Font.LAYOUT_LEFT_TO_RIGHT), x, y);
// Use the following line to compile in JDK 1.3
// drawGlyphVector(this.font.createGlyphVector(getFontRenderContext(), s), x, y);
}
else {
boolean restoreTextRenderingMode = false;
AffineTransform at = getTransform();
AffineTransform at2 = getTransform();
at2.translate(x, y);
at2.concatenate(font.getTransform());
setTransform(at2);
AffineTransform inverse = this.normalizeMatrix();
AffineTransform flipper = AffineTransform.getScaleInstance(1,-1);
inverse.concatenate(flipper);
double[] mx = new double[6];
inverse.getMatrix(mx);
cb.beginText();
cb.setFontAndSize(baseFont, fontSize);
// Check if we need to simulate an italic font.
// When there are different fonts for italic, bold, italic bold
// the font.getName() will be different from the font.getFontName()
// value. When they are the same value then we are normally dealing
// with a single font that has been made into an italic or bold
// font.
if (font.isItalic() && font.getFontName().equals(font.getName())) {
float angle = baseFont.getFontDescriptor(BaseFont.ITALICANGLE, 1000);
float angle2 = font.getItalicAngle();
// We don't have an italic version of this font so we need
// to set the font angle ourselves to produce an italic font.
if (angle2 == 0) {
// The JavaVM didn't have an angle setting for making
// the font an italic font so use a default of
// italic angle of 15 degrees.
angle2 = 15.0f;
} else {
// This sign of the angle for Java and PDF seams
// seams to be reversed.
angle2 = -angle2;
}
if (angle == 0) {
mx[2] = angle2 / 100.0f;
}
}
cb.setTextMatrix((float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]);
Float fontTextAttributeWidth = (Float)font.getAttributes().get(TextAttribute.WIDTH);
fontTextAttributeWidth = (fontTextAttributeWidth == null)
? TextAttribute.WIDTH_REGULAR
: fontTextAttributeWidth;
if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth))
cb.setHorizontalScaling(100.0f / fontTextAttributeWidth.floatValue());
// Check if we need to simulate a bold font.
// Do nothing if the BaseFont is already bold. This test is not foolproof but it will work most of the times.
if (baseFont.getPostscriptFontName().toLowerCase().indexOf("bold") < 0) {
// Get the weight of the font so we can detect fonts with a weight
// that makes them bold, but the Font.isBold() value is false.
Float weight = (Float) font.getAttributes().get(TextAttribute.WEIGHT);
if (weight == null) {
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) {
cb.setTextRenderingMode(PdfContentByte.
TEXT_RENDER_MODE_FILL_STROKE);
cb.setLineWidth(strokeWidth);
cb.setColorStroke(getColor());
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);
}
}
}
|
public void drawString(AttributedCharacterIterator iterator,
int x,
int y) {
drawString(iterator, (float)x, (float)y);
}
|
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;
}
|
public void fill(Shape s) {
followPath(s, FILL);
}
|
public void fillArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle) {
Arc2D arc = new Arc2D.Double(x,y,width,height,startAngle, arcAngle, Arc2D.PIE);
fill(arc);
}
|
public void fillOval(int x,
int y,
int width,
int height) {
Ellipse2D oval = new Ellipse2D.Float(x, y, width, height);
fill(oval);
}
|
public void fillPolygon(int[] xPoints,
int[] yPoints,
int nPoints) {
Polygon poly = new Polygon();
for (int i = 0; i < nPoints; i++) {
poly.addPoint(xPoints[i], yPoints[i]);
}
fill(poly);
}
|
public void fillRect(int x,
int y,
int width,
int height) {
fill(new Rectangle(x,y,width,height));
}
|
public void fillRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight) {
RoundRectangle2D rect = new RoundRectangle2D.Double(x,y,width,height,arcWidth, arcHeight);
fill(rect);
}
|
public Color getBackground() {
return background;
}
|
public Shape getClip() {
try {
return transform.createInverse().createTransformedShape(clip);
}
catch (NoninvertibleTransformException e) {
return null;
}
}
|
public Rectangle getClipBounds() {
if (clip == null)
return null;
return getClip().getBounds();
}
|
public Color getColor() {
if (paint instanceof Color) {
return (Color)paint;
} else {
return Color.black;
}
}
|
public Composite getComposite() {
return composite;
}
|
public PdfContentByte getContent() {
return this.cb;
}
|
public GraphicsConfiguration getDeviceConfiguration() {
return dg2.getDeviceConfiguration();
}
|
public Font getFont() {
return font;
}
|
public FontMetrics getFontMetrics(Font f) {
return dg2.getFontMetrics(f);
}
|
public FontRenderContext getFontRenderContext() {
boolean antialias = RenderingHints.VALUE_TEXT_ANTIALIAS_ON.equals(getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING));
boolean fractions = RenderingHints.VALUE_FRACTIONALMETRICS_ON.equals(getRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS));
return new FontRenderContext(new AffineTransform(), antialias, fractions);
}
|
public Paint getPaint() {
if (realPaint != null) {
return realPaint;
} else {
return paint;
}
}
Method contributed by Alexej Suchov |
public Object getRenderingHint(Key arg0) {
return rhints.get(arg0);
}
|
public RenderingHints getRenderingHints() {
return rhints;
}
|
public Stroke getStroke() {
return originalStroke;
}
|
public AffineTransform getTransform() {
return new AffineTransform(transform);
}
|
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);
}
|
public void rotate(double theta) {
transform.rotate(theta);
}
|
public void rotate(double theta,
double x,
double y) {
transform.rotate(theta, x, y);
}
|
public void scale(double sx,
double sy) {
transform.scale(sx, sy);
this.stroke = transformStroke(originalStroke);
}
|
public void setBackground(Color color) {
background = color;
}
|
public void setClip(Shape s) {
cb.restoreState();
cb.saveState();
if (s != null)
s = transform.createTransformedShape(s);
if (s == null) {
clip = null;
}
else {
clip = new Area(s);
followPath(s, CLIP);
}
paintFill = paintStroke = null;
currentFillGState = currentStrokeGState = 255;
oldStroke = strokeOne;
}
|
public void setClip(int x,
int y,
int width,
int height) {
Rectangle2D rect = new Rectangle2D.Double(x,y,width,height);
setClip(rect);
}
|
public void setColor(Color color) {
setPaint(color);
}
|
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 |
public void setFont(Font f) {
if (f == null)
return;
if (onlyShapes) {
font = f;
return;
}
if (f == font)
return;
font = f;
fontSize = f.getSize2D();
baseFont = getCachedBaseFont(f);
}
|
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;
}
}
}
Method contributed by Alexej Suchov |
public void setPaintMode() {
}
|
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);
}
}
}
|
public void setRenderingHints(Map hints) {
rhints.clear();
rhints.putAll(hints);
}
|
public void setStroke(Stroke s) {
originalStroke = s;
this.stroke = transformStroke(s);
}
|
public void setTransform(AffineTransform t) {
transform = new AffineTransform(t);
this.stroke = transformStroke(originalStroke);
}
|
public void setXORMode(Color c1) {
}
|
public void shear(double shx,
double shy) {
transform.shear(shx, shy);
}
|
public void transform(AffineTransform tx) {
transform.concatenate(tx);
this.stroke = transformStroke(originalStroke);
}
|
public void translate(int x,
int y) {
translate((double)x, (double)y);
}
|
public void translate(double tx,
double ty) {
transform.translate(tx,ty);
}
|