From: Adrian Cumiskey Date: Thu, 27 Nov 2008 10:37:43 +0000 (+0000) Subject: * Text encoding is now handled by PresentationTextData and character set encoding... X-Git-Tag: fop-1_0~376^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=392d02e2db1041986b3874968727dee298fd33fd;p=xmlgraphics-fop.git * Text encoding is now handled by PresentationTextData and character set encoding errors are now handled by AFPEventProducer. * Moved AFPEventProducer from org.apache.fop.render.afp package to org.apache.fop.afp package. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AFPGOCAResources@721151 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/afp/AFPEventProducer.java b/src/java/org/apache/fop/afp/AFPEventProducer.java new file mode 100644 index 000000000..49792183f --- /dev/null +++ b/src/java/org/apache/fop/afp/AFPEventProducer.java @@ -0,0 +1,83 @@ +/* + * 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. + */ + +/* $Id$ */ + +package org.apache.fop.afp; + +import org.apache.fop.events.EventBroadcaster; +import org.apache.fop.events.EventProducer; +import org.apache.fop.events.model.AbstractEventModelFactory; +import org.apache.fop.events.model.EventModel; + +/** + * Event producer interface for AFP-specific events. + */ +public interface AFPEventProducer extends EventProducer { + + /** Provider class for the event producer. */ + class Provider { + + /** + * Returns an event producer. + * @param broadcaster the event broadcaster to use + * @return the event producer + */ + public static AFPEventProducer get(EventBroadcaster broadcaster) { + return (AFPEventProducer)broadcaster.getEventProducerFor( + AFPEventProducer.class); + } + } + + /** Event model factory for AFP. */ + public static class EventModelFactory extends AbstractEventModelFactory { + + /** {@inheritDoc} */ + public EventModel createEventModel() { + return loadModel(getClass(), "event-model.xml"); + } + + } + + /** + * Warn about using default font setup. + * + * @param source the event source + * @event.severity WARN + */ + void warnDefaultFontSetup(Object source); + + /** + * Warn about a missing default "any" font configuration. + * + * @param source the event source + * @param style the font style + * @param weight the font weight + * @event.severity WARN + */ + void warnMissingDefaultFont(Object source, String style, int weight); + + /** + * A character set encoding error occurred. + * + * @param source the event source + * @param charSetName the character set name + * @param encoding the encoding + * @event.severity ERROR + */ + void characterSetEncodingError(Object source, String charSetName, String encoding); +} diff --git a/src/java/org/apache/fop/afp/AFPEventProducer.xml b/src/java/org/apache/fop/afp/AFPEventProducer.xml new file mode 100644 index 000000000..8e6bb5429 --- /dev/null +++ b/src/java/org/apache/fop/afp/AFPEventProducer.xml @@ -0,0 +1,6 @@ + + + No AFP fonts configured. Using default setup. + No AFP default "any", {style}, {weight} font configured. + An error occurred when attempting to encode character set {charSetName} with encoding scheme {encoding}. + diff --git a/src/java/org/apache/fop/afp/AFPTextDataInfo.java b/src/java/org/apache/fop/afp/AFPTextDataInfo.java index 3e87fc473..8047c927c 100644 --- a/src/java/org/apache/fop/afp/AFPTextDataInfo.java +++ b/src/java/org/apache/fop/afp/AFPTextDataInfo.java @@ -26,22 +26,33 @@ import java.awt.Color; */ public class AFPTextDataInfo { + /** the text font reference */ private int fontReference; + /** the text x coordinate position */ private int x; + /** the text y coordinate position */ private int y; + /** the text color */ private Color color; + /** the text variable space adjustment */ private int variableSpaceCharacterIncrement; + /** the text inter character adjustment */ private int interCharacterAdjustment; - private byte[] data; - + /** the text orientation */ private int rotation; + /** the text encoding */ + private String textEncoding; + + /** the text string */ + private String textString; + /** * Returns the font reference * @@ -152,39 +163,57 @@ public class AFPTextDataInfo { } /** - * Return the text data + * Sets the text orientation * - * @return the text data + * @param rotation the text rotation */ - public byte[] getData() { - return data; + public void setRotation(int rotation) { + this.rotation = rotation; } /** - * Sets the text data + * Returns the text rotation * - * @param data the text orientation + * @return the text rotation */ - public void setData(byte[] data) { - this.data = data; + public int getRotation() { + return this.rotation; } /** - * Sets the text orientation + * Sets the text encoding * - * @param rotation the text rotation + * @param textEncoding the text encoding */ - public void setRotation(int rotation) { - this.rotation = rotation; + public void setEncoding(String textEncoding) { + this.textEncoding = textEncoding; } /** - * Returns the text rotation + * Returns the text encoding * - * @return the text rotation + * @return the text encoding */ - public int getRotation() { - return this.rotation; + public String getEncoding() { + return this.textEncoding; + } + + /** + * Sets the text string + * + * @param textString the text string + */ + public void setString(String textString) { + this.textString = textString; + } + + /** + * Returns the text string + * + * @return the text string + */ + public String getString() { + return this.textString; } /** {@inheritDoc} */ @@ -196,7 +225,8 @@ public class AFPTextDataInfo { + ", vsci=" + variableSpaceCharacterIncrement + ", ica=" + interCharacterAdjustment + ", orientation=" + rotation - + ", data=" + data + + ", textString=" + textString + + ", textEncoding=" + textEncoding + "}"; } } \ No newline at end of file diff --git a/src/java/org/apache/fop/afp/DataStream.java b/src/java/org/apache/fop/afp/DataStream.java index 34a7f0f9d..783c698ea 100644 --- a/src/java/org/apache/fop/afp/DataStream.java +++ b/src/java/org/apache/fop/afp/DataStream.java @@ -23,6 +23,7 @@ import java.awt.Color; import java.awt.Point; import java.io.IOException; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.Map; @@ -364,8 +365,9 @@ public class DataStream { * * @param textDataInfo * the afp text data + * @throws UnsupportedEncodingException thrown if character encoding is not supported */ - public void createText(AFPTextDataInfo textDataInfo) { + public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException { int rotation = paintingState.getRotation(); if (rotation != 0) { textDataInfo.setRotation(rotation); diff --git a/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java b/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java index f7216eb11..bf136225b 100644 --- a/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java +++ b/src/java/org/apache/fop/afp/fonts/AFPFontCollection.java @@ -22,12 +22,12 @@ package org.apache.fop.afp.fonts; import java.util.Iterator; import java.util.List; +import org.apache.fop.afp.AFPEventProducer; import org.apache.fop.events.EventBroadcaster; import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontTriplet; -import org.apache.fop.render.afp.AFPEventProducer; /** * A base collection of AFP fonts diff --git a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java index c7559a87f..249de1339 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java @@ -21,6 +21,7 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.fop.afp.AFPLineDataInfo; @@ -171,8 +172,9 @@ public abstract class AbstractPageObject extends AbstractNamedAFPObject implemen * * @param textDataInfo * the afp text data + * @throws UnsupportedEncodingException thrown if character encoding is not supported */ - public void createText(AFPTextDataInfo textDataInfo) { + public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException { getPresentationTextObject().createTextData(textDataInfo); } diff --git a/src/java/org/apache/fop/afp/modca/PresentationTextData.java b/src/java/org/apache/fop/afp/modca/PresentationTextData.java index 4bc2b914e..645a461d8 100644 --- a/src/java/org/apache/fop/afp/modca/PresentationTextData.java +++ b/src/java/org/apache/fop/afp/modca/PresentationTextData.java @@ -22,6 +22,7 @@ package org.apache.fop.afp.modca; import java.awt.Color; import java.io.IOException; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.fop.afp.AFPLineDataInfo; @@ -50,49 +51,31 @@ import org.apache.fop.afp.util.BinaryUtils; */ public class PresentationTextData extends AbstractAFPObject { - /** - * The maximum size of the presentation text data. - */ + /** the maximum size of the presentation text data.*/ private static final int MAX_SIZE = 8192; - /** - * The afp data relating to this presentation text data. - */ + /** the AFP data relating to this presentation text data. */ private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - /** - * The current x coordinate. - */ + /** the current x coordinate. */ private int currentX = -1; - /** - * The current y cooridnate - */ + /** the current y cooridnate */ private int currentY = -1; - /** - * The current font - */ + /** the current font */ private String currentFont = ""; - /** - * The current orientation - */ + /** the current orientation */ private int currentOrientation = 0; - /** - * The current color - */ + /** the current color */ private Color currentColor = new Color(0, 0, 0); - /** - * The current variable space increment - */ + /** the current variable space increment */ private int currentVariableSpaceCharacterIncrement = 0; - /** - * The current inter character adjustment - */ + /** the current inter character adjustment */ private int currentInterCharacterAdjustment = 0; /** @@ -115,9 +98,9 @@ public class PresentationTextData extends AbstractAFPObject { 0x5A, // Structured field identifier 0x00, // Record length byte 1 0x00, // Record length byte 2 - (byte) 0xD3, // PresentationTextData identifier byte 1 - (byte) 0xEE, // PresentationTextData identifier byte 2 - (byte) 0x9B, // PresentationTextData identifier byte 3 + SF_CLASS, // PresentationTextData identifier byte 1 + Type.DATA, // PresentationTextData identifier byte 2 + Category.PRESENTATION_TEXT, // PresentationTextData identifier byte 3 0x00, // Flag 0x00, // Reserved 0x00, // Reserved @@ -270,10 +253,12 @@ public class PresentationTextData extends AbstractAFPObject { * @param textDataInfo * the afp text data * @throws MaximumSizeExceededException - * thrown if the maximum number of text data is exceeded + * thrown if the maximum number of text data is exceeded + * @throws UnsupportedEncodingException + * thrown if character encoding is not supported */ public void createTextData(AFPTextDataInfo textDataInfo) - throws MaximumSizeExceededException { + throws MaximumSizeExceededException, UnsupportedEncodingException { ByteArrayOutputStream afpdata = new ByteArrayOutputStream(); @@ -325,7 +310,9 @@ public class PresentationTextData extends AbstractAFPObject { afpdata); // Add transparent data - byte[] data = textDataInfo.getData(); + String textString = textDataInfo.getString(); + String encoding = textDataInfo.getEncoding(); + byte[] data = textString.getBytes(encoding); if (data.length <= TRANSPARENT_MAX_SIZE) { addTransparentData(data, afpdata); } else { diff --git a/src/java/org/apache/fop/afp/modca/PresentationTextObject.java b/src/java/org/apache/fop/afp/modca/PresentationTextObject.java index 070a33a84..4a8bbbb18 100644 --- a/src/java/org/apache/fop/afp/modca/PresentationTextObject.java +++ b/src/java/org/apache/fop/afp/modca/PresentationTextObject.java @@ -21,6 +21,7 @@ package org.apache.fop.afp.modca; import java.io.IOException; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.fop.afp.AFPLineDataInfo; @@ -68,8 +69,9 @@ public class PresentationTextObject extends AbstractNamedAFPObject { * * @param textDataInfo * The afp text data + * @throws UnsupportedEncodingException thrown if character encoding is not supported */ - public void createTextData(AFPTextDataInfo textDataInfo) { + public void createTextData(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException { if (currentPresentationTextData == null) { startPresentationTextData(); } @@ -78,6 +80,9 @@ public class PresentationTextObject extends AbstractNamedAFPObject { } catch (MaximumSizeExceededException msee) { endPresentationTextData(); createTextData(textDataInfo); + } catch (UnsupportedEncodingException e) { + endPresentationTextData(); + throw e; } } diff --git a/src/java/org/apache/fop/render/afp/AFPEventProducer.java b/src/java/org/apache/fop/render/afp/AFPEventProducer.java deleted file mode 100644 index 28d93cf65..000000000 --- a/src/java/org/apache/fop/render/afp/AFPEventProducer.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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. - */ - -/* $Id$ */ - -package org.apache.fop.render.afp; - -import org.apache.fop.events.EventBroadcaster; -import org.apache.fop.events.EventProducer; -import org.apache.fop.events.model.AbstractEventModelFactory; -import org.apache.fop.events.model.EventModel; - -/** - * Event producer interface for AFP-specific events. - */ -public interface AFPEventProducer extends EventProducer { - - /** Provider class for the event producer. */ - class Provider { - - /** - * Returns an event producer. - * @param broadcaster the event broadcaster to use - * @return the event producer - */ - public static AFPEventProducer get(EventBroadcaster broadcaster) { - return (AFPEventProducer)broadcaster.getEventProducerFor( - AFPEventProducer.class); - } - } - - /** Event model factory for AFP. */ - public static class EventModelFactory extends AbstractEventModelFactory { - - /** {@inheritDoc} */ - public EventModel createEventModel() { - return loadModel(getClass(), "event-model.xml"); - } - - } - - /** - * Warn about using default font setup. - * @param source the event source - * @event.severity WARN - */ - void warnDefaultFontSetup(Object source); - - /** - * Warn about a missing default "any" font configuration. - * @param source the event source - * @param style the font style - * @param weight the font weight - * @event.severity WARN - */ - void warnMissingDefaultFont(Object source, String style, int weight); -} diff --git a/src/java/org/apache/fop/render/afp/AFPEventProducer.xml b/src/java/org/apache/fop/render/afp/AFPEventProducer.xml deleted file mode 100644 index 31ce19a65..000000000 --- a/src/java/org/apache/fop/render/afp/AFPEventProducer.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - No AFP fonts configured. Using default setup. - No AFP default "any", {style}, {weight} font configured. - diff --git a/src/java/org/apache/fop/render/afp/AFPRenderer.java b/src/java/org/apache/fop/render/afp/AFPRenderer.java index b73b036c3..8035a9490 100644 --- a/src/java/org/apache/fop/render/afp/AFPRenderer.java +++ b/src/java/org/apache/fop/render/afp/AFPRenderer.java @@ -34,8 +34,8 @@ import java.util.List; import java.util.Map; import org.apache.fop.afp.AFPBorderPainter; -import org.apache.fop.afp.AFPConstants; import org.apache.fop.afp.AFPDataObjectInfo; +import org.apache.fop.afp.AFPEventProducer; import org.apache.fop.afp.AFPPaintingState; import org.apache.fop.afp.AFPRectanglePainter; import org.apache.fop.afp.AFPResourceManager; @@ -48,6 +48,7 @@ import org.apache.fop.afp.fonts.AFPFont; import org.apache.fop.afp.fonts.AFPFontAttributes; import org.apache.fop.afp.fonts.AFPFontCollection; import org.apache.fop.afp.fonts.AFPPageFonts; +import org.apache.fop.afp.fonts.CharacterSet; import org.apache.fop.afp.modca.PageObject; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; @@ -537,40 +538,34 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { Color color = (Color) text.getTrait(Trait.COLOR); textDataInfo.setColor(color); - int variableSpaceCharacterIncrement = font.getWidth(' ', fontSize) / 1000 - + text.getTextWordSpaceAdjust() - + text.getTextLetterSpaceAdjust(); + int textWordSpaceAdjust = text.getTextWordSpaceAdjust(); + int textLetterSpaceAdjust = text.getTextLetterSpaceAdjust(); + int textWidth = font.getWidth(' ', fontSize) / 1000; + int variableSpaceCharacterIncrement + = textWidth + textWordSpaceAdjust + textLetterSpaceAdjust; + variableSpaceCharacterIncrement = Math.round(unitConv.mpt2units(variableSpaceCharacterIncrement)); textDataInfo.setVariableSpaceCharacterIncrement(variableSpaceCharacterIncrement); int interCharacterAdjustment - = Math.round(unitConv.mpt2units(text.getTextLetterSpaceAdjust())); + = Math.round(unitConv.mpt2units(textLetterSpaceAdjust)); textDataInfo.setInterCharacterAdjustment(interCharacterAdjustment); - // Try and get the encoding to use for the font - String encoding = null; - try { - encoding = font.getCharacterSet(fontSize).getEncoding(); - } catch (Throwable ex) { - encoding = AFPConstants.EBCIDIC_ENCODING; - log.warn("renderText():: Error getting encoding for font '" - + font.getFullName() + "' - using default encoding " - + encoding); - } + CharacterSet charSet = font.getCharacterSet(fontSize); + String encoding = charSet.getEncoding(); + textDataInfo.setEncoding(encoding); String textString = text.getText(); - byte[] data = null; + textDataInfo.setString(textString); + try { - data = textString.getBytes(encoding); - textDataInfo.setData(data); - } catch (UnsupportedEncodingException usee) { - log.error("renderText:: Font " + fontAttributes.getFontKey() - + " caused UnsupportedEncodingException"); - return; + dataStream.createText(textDataInfo); + } catch (UnsupportedEncodingException e) { + AFPEventProducer eventProducer + = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster()); + eventProducer.characterSetEncodingError(this, charSet.getName(), encoding); } - - dataStream.createText(textDataInfo); // word.getOffset() = only height of text itself // currentBlockIPPosition: 0 for beginning of line; nonzero // where previous line area failed to take up entire allocated space