From 37f5573e66f1e4ef0d7604d888e4d67f2b04aa7c Mon Sep 17 00:00:00 2001 From: Chris Bowditch Date: Fri, 10 Jul 2009 10:32:23 +0000 Subject: [PATCH] Bugfix: support justified text in AFP Renderer (already working in AFP Painter) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@792873 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/afp/DataStream.java | 98 ++++++++++++++++++- .../fop/afp/modca/AbstractPageObject.java | 8 +- .../apache/fop/render/afp/AFPRenderer.java | 5 +- status.xml | 3 + 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/fop/afp/DataStream.java b/src/java/org/apache/fop/afp/DataStream.java index b1ff96859..cb68af94e 100644 --- a/src/java/org/apache/fop/afp/DataStream.java +++ b/src/java/org/apache/fop/afp/DataStream.java @@ -30,8 +30,9 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.fop.afp.fonts.AFPFont; import org.apache.fop.afp.fonts.AFPFontAttributes; +import org.apache.fop.afp.fonts.AFPFont; +import org.apache.fop.afp.fonts.CharacterSet; import org.apache.fop.afp.modca.AbstractPageObject; import org.apache.fop.afp.modca.Document; import org.apache.fop.afp.modca.InterchangeSet; @@ -41,6 +42,10 @@ import org.apache.fop.afp.modca.PageObject; import org.apache.fop.afp.modca.ResourceGroup; import org.apache.fop.afp.modca.TagLogicalElementBean; import org.apache.fop.afp.modca.triplets.FullyQualifiedNameTriplet; +import org.apache.fop.afp.ptoca.PtocaProducer; +import org.apache.fop.afp.ptoca.PtocaBuilder; +import org.apache.fop.util.CharUtilities; +import org.apache.fop.fonts.Font; /** * A data stream is a continuous ordered stream of data elements and objects @@ -347,11 +352,15 @@ public class DataStream { * Helper method to create text on the current page, this method delegates * to the current presentation text object in order to construct the text. * - * @param textDataInfo - * the afp text data + * @param textDataInfo the afp text data + * @param letterSpacing letter spacing to draw text with + * @param wordSpacing word Spacing to draw text with + * @param font is the font to draw text with + * @param charSet is the AFP Character Set to use with the text * @throws UnsupportedEncodingException thrown if character encoding is not supported */ - public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException { + public void createText(final AFPTextDataInfo textDataInfo, final int letterSpacing, final int wordSpacing, + final Font font, final CharacterSet charSet) throws UnsupportedEncodingException { int rotation = paintingState.getRotation(); if (rotation != 0) { textDataInfo.setRotation(rotation); @@ -359,7 +368,86 @@ public class DataStream { textDataInfo.setX(p.x); textDataInfo.setY(p.y); } - currentPage.createText(textDataInfo); + // use PtocaProducer to create PTX records + PtocaProducer producer = new PtocaProducer() { + + public void produce(PtocaBuilder builder) throws IOException { + builder.setTextOrientation(textDataInfo.getRotation()); + builder.absoluteMoveBaseline(textDataInfo.getY()); + builder.absoluteMoveInline(textDataInfo.getX()); + + builder.setExtendedTextColor(textDataInfo.getColor()); + builder.setCodedFont((byte)textDataInfo.getFontReference()); + + int l = textDataInfo.getString().length(); + StringBuffer sb = new StringBuffer(); + + int interCharacterAdjustment = 0; + AFPUnitConverter unitConv = paintingState.getUnitConverter(); + if (letterSpacing != 0) { + interCharacterAdjustment = Math.round(unitConv.mpt2units(letterSpacing)); + } + builder.setInterCharacterAdjustment(interCharacterAdjustment); + + int spaceWidth = font.getCharWidth(CharUtilities.SPACE); + int spacing = spaceWidth + letterSpacing; + int fixedSpaceCharacterIncrement = Math.round(unitConv.mpt2units(spacing)); + int varSpaceCharacterIncrement = fixedSpaceCharacterIncrement; + if (wordSpacing != 0) { + varSpaceCharacterIncrement = Math.round(unitConv.mpt2units( + spaceWidth + wordSpacing + letterSpacing)); + } + builder.setVariableSpaceCharacterIncrement(varSpaceCharacterIncrement); + + boolean fixedSpaceMode = false; + + for (int i = 0; i < l; i++) { + char orgChar = textDataInfo.getString().charAt(i); + float glyphAdjust = 0; + if (CharUtilities.isFixedWidthSpace(orgChar)) { + flushText(builder, sb, charSet); + builder.setVariableSpaceCharacterIncrement( + fixedSpaceCharacterIncrement); + fixedSpaceMode = true; + sb.append(CharUtilities.SPACE); + int charWidth = font.getCharWidth(orgChar); + glyphAdjust += (charWidth - spaceWidth); + } else { + if (fixedSpaceMode) { + flushText(builder, sb, charSet); + builder.setVariableSpaceCharacterIncrement( + varSpaceCharacterIncrement); + fixedSpaceMode = false; + } + char ch; + if (orgChar == CharUtilities.NBSPACE) { + ch = ' '; //converted to normal space to allow word spacing + } else { + ch = orgChar; + } + sb.append(ch); + } + + if (glyphAdjust != 0) { + flushText(builder, sb, charSet); + int increment = Math.round(unitConv.mpt2units(glyphAdjust)); + builder.relativeMoveInline(increment); + } + } + flushText(builder, sb, charSet); + } + + private void flushText(PtocaBuilder builder, StringBuffer sb, + final CharacterSet charSet) throws IOException { + if (sb.length() > 0) { + builder.addTransparentData(charSet.encodeChars(sb)); + sb.setLength(0); + } + } + + }; + + currentPage.createText(producer); } /** diff --git a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java index cd8c44b5e..af676410f 100644 --- a/src/java/org/apache/fop/afp/modca/AbstractPageObject.java +++ b/src/java/org/apache/fop/afp/modca/AbstractPageObject.java @@ -25,9 +25,9 @@ import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.fop.afp.AFPLineDataInfo; -import org.apache.fop.afp.AFPTextDataInfo; import org.apache.fop.afp.Completable; import org.apache.fop.afp.Factory; +import org.apache.fop.afp.ptoca.PtocaProducer; import org.apache.fop.afp.fonts.AFPFont; /** @@ -170,8 +170,10 @@ public abstract class AbstractPageObject extends AbstractNamedAFPObject implemen * the afp text data * @throws UnsupportedEncodingException thrown if character encoding is not supported */ - public void createText(AFPTextDataInfo textDataInfo) throws UnsupportedEncodingException { - getPresentationTextObject().createTextData(textDataInfo); + public void createText(PtocaProducer producer) throws UnsupportedEncodingException { + //getPresentationTextObject().createTextData(textDataInfo); + getPresentationTextObject().createControlSequences(producer); + } /** diff --git a/src/java/org/apache/fop/render/afp/AFPRenderer.java b/src/java/org/apache/fop/render/afp/AFPRenderer.java index 5024fa7b6..b9eb345b7 100644 --- a/src/java/org/apache/fop/render/afp/AFPRenderer.java +++ b/src/java/org/apache/fop/render/afp/AFPRenderer.java @@ -75,6 +75,7 @@ import org.apache.fop.area.inline.TextArea; import org.apache.fop.datatypes.URISpecification; import org.apache.fop.events.ResourceEventProducer; import org.apache.fop.fo.extensions.ExtensionAttachment; +import org.apache.fop.fonts.Font; import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontManager; @@ -563,6 +564,8 @@ public class AFPRenderer extends AbstractPathOrientedRenderer implements AFPCust AFPFont font = (AFPFont)fontMetricMap.get(internalFontName); AFPPageFonts pageFonts = paintingState.getPageFonts(); AFPFontAttributes fontAttributes = pageFonts.registerFont(internalFontName, font, fontSize); + Font fnt = getFontFromArea(text); + // create text data info AFPTextDataInfo textDataInfo = new AFPTextDataInfo(); @@ -603,7 +606,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer implements AFPCust textDataInfo.setString(textString); try { - dataStream.createText(textDataInfo); + dataStream.createText(textDataInfo, textLetterSpaceAdjust, textWordSpaceAdjust, fnt, charSet); } catch (UnsupportedEncodingException e) { AFPEventProducer eventProducer = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster()); diff --git a/status.xml b/status.xml index d46bbb06c..3b7e3bac9 100644 --- a/status.xml +++ b/status.xml @@ -58,6 +58,9 @@ documents. Example: the fix of marks layering will be such a case when it's done. --> + + Bugfix: support justified text in AFP Renderer (already working in AFP Painter) + AFP Renderer Raster Fonts:
    -- 2.39.5