String EL_VIEWPORT = "viewport";
String EL_GROUP = "g";
String EL_IMAGE = "image";
+ String EL_CLIP_RECT = "clip-rect";
String EL_RECT = "rect";
String EL_BORDER_RECT = "border-rect";
String EL_FONT = "font";
*/
public class IFGraphicContext extends GraphicContext {
+ private static final AffineTransform[] EMPTY_TRANSFORM_ARRAY = new AffineTransform[0];
+
private List groupList = new java.util.ArrayList();
/**
this(new AffineTransform[] {transform});
}
+ public Group() {
+ this(EMPTY_TRANSFORM_ARRAY);
+ }
+
public AffineTransform[] getTransforms() {
return this.transforms;
}
*/
void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException;
+ /**
+ * Restricts the current clipping region with the given rectangle.
+ * @param rect the rectangle's coordinates and extent
+ * @throws IFException if an error occurs while handling this event
+ */
+ void clipRect(Rectangle rect) throws IFException;
+ //TODO clipRect() shall be considered temporary until verified with SVG and PCL
+
/**
* Draws a rectangle. Either fill or stroke has to be specified.
* @param rect the rectangle's coordinates and extent
elementHandlers.put(EL_GROUP, new GroupHandler());
elementHandlers.put(EL_FONT, new FontHandler());
elementHandlers.put(EL_TEXT, new TextHandler());
+ elementHandlers.put(EL_CLIP_RECT, new ClipRectHandler());
elementHandlers.put(EL_RECT, new RectHandler());
elementHandlers.put(EL_BORDER_RECT, new BorderRectHandler());
elementHandlers.put(EL_IMAGE, new ImageHandler());
}
+ private class ClipRectHandler extends AbstractElementHandler {
+
+ public void startElement(Attributes attributes) throws IFException {
+ int x = Integer.parseInt(attributes.getValue("x"));
+ int y = Integer.parseInt(attributes.getValue("y"));
+ int width = Integer.parseInt(attributes.getValue("width"));
+ int height = Integer.parseInt(attributes.getValue("height"));
+ painter.clipRect(new Rectangle(x, y, width, height));
+ }
+
+ }
+
private class RectHandler extends AbstractElementHandler {
public void startElement(Attributes attributes) throws IFException {
/** {@inheritDoc} */
protected void concatenateTransformationMatrix(AffineTransform at) {
if (!at.isIdentity()) {
- concatenateTransformationMatrixMpt(ptToMpt(at));
+ concatenateTransformationMatrixMpt(ptToMpt(at), false);
}
}
- private void concatenateTransformationMatrixMpt(AffineTransform at) {
- if (!at.isIdentity()) {
+ private void concatenateTransformationMatrixMpt(AffineTransform at, boolean force) {
+ if (force || !at.isIdentity()) {
if (log.isTraceEnabled()) {
log.trace("-----concatenateTransformationMatrix: " + at);
}
saveGraphicsState();
//Viewport position
- concatenateTransformationMatrixMpt(positionTransform);
+ concatenateTransformationMatrixMpt(positionTransform, false);
//Background and borders
float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd());
//Shift to content rectangle after border painting
AffineTransform contentRectTransform = new AffineTransform();
contentRectTransform.translate(borderPaddingStart, borderPaddingBefore);
- concatenateTransformationMatrixMpt(contentRectTransform);
+ concatenateTransformationMatrixMpt(contentRectTransform, false);
//Clipping
Rectangle clipRect = null;
/** {@inheritDoc} */
protected void clip() {
- // TODO Auto-generated method stub
- log.warn("clip() NYI");
+ throw new IllegalStateException("Not used");
}
/** {@inheritDoc} */
protected void clipRect(float x, float y, float width, float height) {
- // TODO Auto-generated method stub
- log.warn("clipRect() NYI");
+ pushGroup(new IFGraphicContext.Group());
+ try {
+ painter.clipRect(toMillipointRectangle(x, y, width, height));
+ } catch (IFException ife) {
+ handleIFException(ife);
+ }
}
/** {@inheritDoc} */
protected void closePath() {
- // TODO Auto-generated method stub
- log.warn("closePath() NYI");
+ throw new IllegalStateException("Not used");
}
/** {@inheritDoc} */
/** {@inheritDoc} */
protected void moveTo(float x, float y) {
- // TODO Auto-generated method stub
- log.warn("moveTo() NYI");
+ throw new IllegalStateException("Not used");
}
/** {@inheritDoc} */
protected void lineTo(float x, float y) {
- // TODO Auto-generated method stub
- log.warn("lineTo() NYI");
+ throw new IllegalStateException("Not used");
}
/** {@inheritDoc} */
}
}
+ /** {@inheritDoc} */
+ public void clipRect(Rectangle rect) throws IFException {
+ try {
+ AttributesImpl atts = new AttributesImpl();
+ addAttribute(atts, "x", Integer.toString(rect.x));
+ addAttribute(atts, "y", Integer.toString(rect.y));
+ addAttribute(atts, "width", Integer.toString(rect.width));
+ addAttribute(atts, "height", Integer.toString(rect.height));
+ element(EL_CLIP_RECT, atts);
+ } catch (SAXException e) {
+ throw new IFException("SAX error in clipRect()", e);
+ }
+ }
+
/** {@inheritDoc} */
public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException {
if (fill == null && stroke == null) {
generator.saveGraphicsState();
generator.concatenate(generator.toPoints(transform));
if (clipRect != null) {
- StringBuffer sb = new StringBuffer();
- sb.append(format(clipRect.x)).append(' ');
- sb.append(format(clipRect.y)).append(' ');
- sb.append(format(clipRect.width)).append(' ');
- sb.append(format(clipRect.height)).append(" re W n\n");
- generator.add(sb.toString());
+ clipRect(clipRect);
}
}
return PDFNumber.doubleOut(value / 1000f);
}
+ /** {@inheritDoc} */
+ public void clipRect(Rectangle rect) throws IFException {
+ generator.endTextObject();
+ StringBuffer sb = new StringBuffer();
+ sb.append(format(rect.x)).append(' ');
+ sb.append(format(rect.y)).append(' ');
+ sb.append(format(rect.width)).append(' ');
+ sb.append(format(rect.height)).append(" re W n\n");
+ generator.add(sb.toString());
+ }
+
/** {@inheritDoc} */
public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException {
if (fill == null && stroke == null) {
/** {@inheritDoc} */
public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
BorderProps start, BorderProps end) throws IFException {
- generator.endTextObject();
- this.borderPainter.drawBorders(rect, before, after, start, end);
+ if (before != null || after != null || start != null || end != null) {
+ generator.endTextObject();
+ this.borderPainter.drawBorders(rect, before, after, start, end);
+ }
}
private Typeface getTypeface(String fontName) {
FontTriplet triplet = new FontTriplet(
state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
//TODO Ignored: state.getFontVariant()
+ //TODO Opportunity for font caching if font state is more heavily used
String fontKey = fontInfo.getInternalFontKey(triplet);
int sizeMillipoints = state.getFontSize();
float fontSize = sizeMillipoints / 1000f;
if (CharUtilities.isFixedWidthSpace(orgChar)) {
//Fixed width space are rendered as spaces so copy/paste works in a reader
ch = font.mapChar(CharUtilities.SPACE);
- glyphAdjust = font.getCharWidth(ch) - font.getCharWidth(orgChar);
+ glyphAdjust = -(font.getCharWidth(ch) - font.getCharWidth(orgChar));
} else {
ch = font.mapChar(orgChar);
}
}
}
+ /** {@inheritDoc} */
+ public void clipRect(Rectangle rect) throws IFException {
+ //TODO Implement me!!!
+ }
+
/** {@inheritDoc} */
public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException {
if (fill == null && stroke == null) {