OS: Windows 2000
JDK: 1.4.1
Batik: 1.5.0
JBuilder 8

Hi

If I converting SVG files into JPG, PDF or WMF, I have the problem, that the
text from the SVG file is written with drawGlyphVector and not with
drawString. Thats bad for the WMF file, because the texts in it are not text
objects, there are graphic objects. And graphic object couldn't be edited in
PowerPoint for example. Another problem are the fonts who are used in the
converted files (JPG, PDF, WMF). Obviously Batik doesn't notice, if a text
is to be written bold or italic.

WMF
---
I have written my own TextPainter for writing text. You can see the code
below. That works fine for my simple text outputs.
The conversion to WMF works with GVTBuilder and the paint method. I set my
own TextPainter and the text and font in my WMF file are correct.

PDF/JPG
-------
The conversion to PDF and JPG works with the transcode method. But the fonts
in the converted files are wrong. Batik doesn't notice bold and italic. What
can I do? Is there a possibility to change this?

Can anybody help me?

Thanks.

Regards
Hans





package ch.pmm.chartstudio.chartgen;

import ch.pmm.util.StringFormat;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.text.AttributedCharacterIterator;
import java.awt.font.TextAttribute;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.Font;
import java.awt.Color;
import java.util.List;
import java.util.Iterator;

import java.awt.GraphicsEnvironment;

import org.apache.batik.gvt.TextNode;
import org.apache.batik.gvt.text.GVTAttributedCharacterIterator;
import org.apache.batik.gvt.text.TextPaintInfo;
import org.apache.batik.gvt.font.GVTFontFamily;
import org.apache.batik.bridge.SVGFontFamily;
import org.apache.batik.gvt.renderer.StrokingTextPainter;

public class CSTextPainter extends StrokingTextPainter {
    /**
     * Create a new text painter with the given font information.
     * @param fi the fint info
     */
    public CSTextPainter() {
    }

    /**
     * Paints the specified attributed character iterator using the
     * specified Graphics2D and context and font context.
     * @param node the TextNode to paint
     * @param g2d the Graphics2D to use
     */
    public void paint(TextNode node, Graphics2D g2d) {
      String    sFontName = "";
      String    txt = node.getText();
      String    s;
      int                       iStart = 0;
      Point2D loc = node.getLocation();
      List              oTextRuns = getTextRuns(node,
node.getAttributedCharacterIterator());
      TextRun   oTextRun;
                        double  dX = 0.0;
                        double  dY = 0.0;

      for (int iTextRun = 0; iTextRun < oTextRuns.size(); iTextRun++) {
        oTextRun = (TextRun) oTextRuns.get(iTextRun);
        AttributedCharacterIterator oAci = oTextRun.getACI();
        if (iTextRun == 0) {
          s = txt.substring(0, oAci.getEndIndex());
        } else {
          s = txt.substring(iStart, iStart + oAci.getEndIndex());
        }
        iStart += oAci.getEndIndex();
        // reset position to start of char iterator
        if (oAci.getBeginIndex() == oAci.getEndIndex()) {
          return;
        }
        char ch = oAci.first();
        if (ch == AttributedCharacterIterator.DONE) {
          return;
        }
        TextNode.Anchor anchor;
        //anchor = (TextNode.Anchor)
oAci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);

        List gvtFonts;
        gvtFonts = (List)
oAci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT_FAMI
LIES);

        TextPaintInfo tpi = (TextPaintInfo)
oAci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);

        if (tpi == null) {
          return;
        }

        Paint forg = tpi.fillPaint;
        Paint strokePaint = tpi.strokePaint;
        Float size = (Float) oAci.getAttribute(TextAttribute.SIZE);
        if (size == null) {
          return;
        }
        Stroke stroke = tpi.strokeStroke;

        boolean useStrokePainter = false;

        if (forg instanceof Color) {
          Color col = (Color) forg;
          if (col.getAlpha() != 255) {
            useStrokePainter = true;
          }
          g2d.setColor(col);
        }
        g2d.setPaint(forg);
        g2d.setStroke(stroke);

        if (strokePaint != null) {
          // need to draw using AttributedCharacterIterator
          useStrokePainter = true;
        }

        if (hasUnsupportedAttributes(oAci)) {
          useStrokePainter = true;
        }

        // text contains unsupported information
        if (useStrokePainter) {
          super.paint(node, g2d);
          return;
        }

        int iStyle = Font.PLAIN;
        if (gvtFonts != null) {
          Iterator i = gvtFonts.iterator();
          if (i.hasNext()) {
            GVTFontFamily fam = (GVTFontFamily) i.next();
            if (fam instanceof SVGFontFamily) {
              super.paint(node, g2d);
              return;
            }
            sFontName = getFontName(fam.getFamilyName());
            iStyle = getFontStyle(fam.getFamilyName());
          }
        }
        Font font = new Font(sFontName, iStyle, (int) (size.floatValue() +
0.5f));
        g2d.setFont(font);

        float advance = StringFormat.getWidth(s, font);
        float tx = 0;
        switch (oTextRun.getAnchorType()) {
          case TextNode.Anchor.ANCHOR_MIDDLE:   tx = -advance / 2;      break;
          case TextNode.Anchor.ANCHOR_END:              tx = -advance; break;
        }
                                if (iTextRun > 0) {
                                        dX = oTextRun.getLayout().getOffset().getX();
                                        dY = oTextRun.getLayout().getOffset().getY();
                                } else {
                                        dX = 0.0;
                                        dY = 0.0;
                                }
        g2d.drawString(s,
                       (float) (loc.getX() + tx + dX),
                       (float) (loc.getY() + dY));
      }
    }

    private boolean hasUnsupportedAttributes(AttributedCharacterIterator
aci) {
        boolean hasunsupported = false;
        Object letSpace = aci.getAttribute(

GVTAttributedCharacterIterator.TextAttribute.LETTER_SPACING);
        if (letSpace != null) {
            hasunsupported = true;
        }

        Object wordSpace = aci.getAttribute(

GVTAttributedCharacterIterator.TextAttribute.WORD_SPACING);
        if (wordSpace != null) {
            hasunsupported = true;
        }

        AttributedCharacterIterator.Attribute key;
        key = GVTAttributedCharacterIterator.TextAttribute.WRITING_MODE;
        Object writeMod = aci.getAttribute(key);
        if
(!GVTAttributedCharacterIterator.TextAttribute.WRITING_MODE_LTR.equals(
                  writeMod)) {
            hasunsupported = true;
        }

        Object vertOr =
aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.VERTICAL_ORIEN
TATION);
        if
(GVTAttributedCharacterIterator.TextAttribute.ORIENTATION_ANGLE.equals(vertO
r)) {
                hasunsupported = true;
        }
        return hasunsupported;
    }

    private String getFontName(String sFontFamily) {
                        String  sFamilyName = StringFormat.removeString(sFontFamily, " 
");
                        String  s1 = "";
                        String  s2 = "";

          // Get all available font faces names
        GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
            Font[] fonts = ge.getAllFonts();

          // Process each font
        for (int i=0; i<fonts.length; i++) {
        // Get font's family and face
        String familyName = fonts[i].getFamily();
              String faceName = fonts[i].getName();
                                familyName = StringFormat.removeString(faceName, " ");

                                if (familyName.length() <= sFamilyName.length()) {
                                        s1 = familyName.substring(0, 
familyName.length());
                                        s2 = sFamilyName.substring(0, 
familyName.length());
                                        if (s1.equalsIgnoreCase(s2)) {
                                                return faceName;
                                        }
                                }
        }
                        return "";
                }

    private int getFontStyle(String sFontFamily) {
                        int                     iStyle = Font.PLAIN;
                        String  sFamilyName = sFontFamily.toLowerCase();

                        if (sFamilyName.indexOf("bold") > 0) {
                iStyle |= Font.BOLD;
                        }
                        if (sFamilyName.indexOf("italic") > 0) {
                iStyle |= Font.ITALIC;
                        }
                        return iStyle;
        }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to