You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-commits@xmlgraphics.apache.org by ga...@apache.org on 2014/08/02 04:26:12 UTC

svn commit: r1615279 - in /xmlgraphics/batik/trunk/sources/org/apache/batik: gvt/renderer/StrokingTextPainter.java gvt/text/GVTAttributedCharacterIterator.java gvt/text/GlyphLayout.java script/ImportInfo.java svggen/ImageCacher.java

Author: gadams
Date: Sat Aug  2 02:26:11 2014
New Revision: 1615279

URL: http://svn.apache.org/r1615279
Log:
1. permit null charmap to getTextRuns();
2. enable subclassing GlyphLayout (upgrade limited private to protected);
3. add LANGUAGE and SCRIPT ACI attributes;
4. remove commented out debug code;
5. fix findbugs warnings;


Modified:
    xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java
    xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GVTAttributedCharacterIterator.java
    xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GlyphLayout.java
    xmlgraphics/batik/trunk/sources/org/apache/batik/script/ImportInfo.java
    xmlgraphics/batik/trunk/sources/org/apache/batik/svggen/ImageCacher.java

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java?rev=1615279&r1=1615278&r2=1615279&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/renderer/StrokingTextPainter.java Sat Aug  2 02:26:11 2014
@@ -188,7 +188,6 @@ public class StrokingTextPainter extends
         System.out.println("");
     }
 
-    // static long reorderTime, fontMatchingTime, layoutTime;
     public List getTextRuns(TextNode node, AttributedCharacterIterator aci) {
         List textRuns = node.getTextRuns();
         if (textRuns != null) {
@@ -198,9 +197,6 @@ public class StrokingTextPainter extends
         AttributedCharacterIterator[] chunkACIs = getTextChunkACIs(aci);
         textRuns = computeTextRuns(node, aci, chunkACIs);
 
-        // t1 = System.currentTimeMillis();
-        // layoutTime += t1-t0;
-        // System.out.println("Reorder: " + reorderTime + " FontMatching: " + fontMatchingTime + " Layout: " + layoutTime);
         // cache the textRuns so don't need to recalculate
         node.setTextRuns(textRuns);
         return node.getTextRuns();
@@ -211,8 +207,6 @@ public class StrokingTextPainter extends
                                 AttributedCharacterIterator [] chunkACIs) {
         int [][] chunkCharMaps = new int[chunkACIs.length][];
 
-        // long t0, t1;
-        // t0 = System.currentTimeMillis();
         // reorder each chunk ACI for bidi text
         int chunkStart = aci.getBeginIndex();
         for (int i = 0; i < chunkACIs.length; i++) {
@@ -221,17 +215,21 @@ public class StrokingTextPainter extends
                 (chunkACIs[i], fontRenderContext, chunkStart);
             chunkACIs    [i] = iter;
             chunkCharMaps[i] = iter.getCharMap();
-            // t1 = System.currentTimeMillis();
-            // reorderTime += t1-t0;
-            // t0=t1;
-            chunkACIs    [i] = createModifiedACIForFontMatching
-                (chunkACIs[i]);
-
             chunkStart += (chunkACIs[i].getEndIndex()-
                            chunkACIs[i].getBeginIndex());
-            // t1 = System.currentTimeMillis();
-            // fontMatchingTime += t1-t0;
-            // t0 = t1;
+        }
+        return computeTextRuns(node, aci, chunkACIs, chunkCharMaps);
+    }
+
+    protected List computeTextRuns(TextNode node,
+                                AttributedCharacterIterator aci,
+                                AttributedCharacterIterator [] chunkACIs,
+                                int [][] chunkCharMaps) {
+        // add font matching attributes
+        int chunkStart = aci.getBeginIndex();
+        for (int i = 0; i < chunkACIs.length; i++) {
+            chunkACIs    [i] = createModifiedACIForFontMatching(chunkACIs[i]);
+            chunkStart += (chunkACIs[i].getEndIndex() - chunkACIs[i].getBeginIndex());
         }
 
         // create text runs for each chunk and add them to the list
@@ -247,7 +245,7 @@ public class StrokingTextPainter extends
 
             chunk = getTextChunk(node,
                                  chunkACIs[currentChunk],
-                                 chunkCharMaps[currentChunk],
+                                 chunkCharMaps != null ? chunkCharMaps[currentChunk] : null,
                                  textRuns,
                                  prevChunk);
 
@@ -285,7 +283,7 @@ public class StrokingTextPainter extends
                 TextPath textPath = (TextPath) aci.getAttribute(TEXTPATH);
 
                 if (start != chunkStartIndex) {
-                    // If we aren't the first composite in a chunck see
+                    // If we aren't the first composite in a chunk see
                     // if we need to form a new TextChunk...
                     // We only create new chunks when given an absolute
                     // location in progression direction [Spec says
@@ -319,7 +317,6 @@ public class StrokingTextPainter extends
                 // This prevents BIDI reordering across paragraphs.
                 if (aci.getAttribute(FLOW_PARAGRAPH) != null) {
                     end = aci.getRunLimit(FLOW_PARAGRAPH);
-                    // System.out.println("End: " + end);
                     aci.setIndex(end);
                     break;
                 }
@@ -378,10 +375,8 @@ public class StrokingTextPainter extends
                 }
             }
 
-            // found the end of a text chunck
+            // found the end of a text chunk
             int chunkEndIndex = aci.getIndex();
-            // System.out.println("Bounds: " + chunkStartIndex +
-            //                    "," + chunkEndIndex);
             aciList.add(new AttributedCharacterSpanIterator
                 (aci, chunkStartIndex, chunkEndIndex));
 
@@ -492,7 +487,6 @@ public class StrokingTextPainter extends
                         for (int j = currentIndex; j < displayUpToIndex; j++) {
                             if (fontAssigned[j - start]) {
                                 if (runStart != -1) {
-                                    // System.out.println("Font 1: " + font);
                                     as.addAttribute(GVT_FONT, font,
                                                     runStart-begin, j-begin);
                                     runStart=-1;
@@ -505,7 +499,6 @@ public class StrokingTextPainter extends
                             numSet++;
                         }
                         if (runStart != -1) {
-                            // System.out.println("Font 2: " + font);
                             as.addAttribute(GVT_FONT, font,
                                             runStart-begin,
                                             displayUpToIndex-begin);
@@ -528,7 +521,6 @@ public class StrokingTextPainter extends
             for (int i = 0; i < aciLength; i++) {
                 if (fontAssigned[i]) {
                     if (runStart != -1) {
-                        // System.out.println("Font 3: " + prevF);
                         as.addAttribute(GVT_FONT, prevF,
                                         runStart+asOff, i+asOff);
                         runStart = -1;
@@ -551,7 +543,6 @@ public class StrokingTextPainter extends
                             prevF = fontFamily.deriveFont(fontSize, aci);
                     } else if (prevFF != fontFamily) {
                         // Font family changed...
-                        // System.out.println("Font 4: " + prevF);
                         as.addAttribute(GVT_FONT, prevF,
                                         runStart+asOff, i+asOff);
 
@@ -565,7 +556,6 @@ public class StrokingTextPainter extends
                 }
             }
             if (runStart != -1) {
-                // System.out.println("Font 5: " + prevF);
                 as.addAttribute(GVT_FONT, prevF,
                                 runStart+asOff, aciLength+asOff);
             }
@@ -597,7 +587,6 @@ public class StrokingTextPainter extends
             beginChunk = prevChunk.end;
         int endChunk = beginChunk;
         int begin    = aci.getIndex();
-        // System.out.println("New Chunk");
         if (aci.current() == CharacterIterator.DONE)
             return null;
 
@@ -615,7 +604,13 @@ public class StrokingTextPainter extends
             runaci = new AttributedCharacterSpanIterator(aci, start, end);
 
             int [] subCharMap = new int[end-start];
-            System.arraycopy( charMap, start - begin, subCharMap, 0, subCharMap.length );
+            if (charMap != null) {
+                System.arraycopy( charMap, start - begin, subCharMap, 0, subCharMap.length );
+            } else {
+                for (int i = 0, n = subCharMap.length; i < n; ++i) {
+                    subCharMap[i] = i;
+                }
+            }
 
             FontRenderContext frc = fontRenderContext;
             RenderingHints rh = node.getRenderingHints();
@@ -633,11 +628,8 @@ public class StrokingTextPainter extends
                 (runaci, subCharMap, offset, frc);
 
             textRuns.add(new TextRun(layout, runaci, isChunkStart));
-            // System.out.println("TextRun: " + start +  "->" + end +
-            //                    " Start: " + isChunkStart);
 
             Point2D layoutAdvance = layout.getAdvance2D();
-            // System.out.println("layoutAdv: " + layoutAdvance);
             advance.x +=  (float)layoutAdvance.getX();
             advance.y +=  (float)layoutAdvance.getY();
 
@@ -646,9 +638,6 @@ public class StrokingTextPainter extends
             isChunkStart = false;
         } while (true);
 
-        // System.out.println("Adv: " + advance);
-        // System.out.println("Chunks: [" + beginChunk + ", " +
-        //                    endChunk + "]");
         return new TextChunk(beginChunk, endChunk, advance);
     }
 
@@ -699,14 +688,6 @@ public class StrokingTextPainter extends
         Point2D visualAdvance;
 
         if (!doAdjust) {
-            // System.err.println("Anchor: " + anchorType);
-            // System.err.println("Advance: " + chunk.advance);
-            // System.err.println("LastBounds: " + lastBounds);
-            // System.err.println("LastMetrics.hadv: " +
-            //                    lastMetrics.getHorizontalAdvance());
-            // System.err.println("LastMetrics.vadv: " +
-            //                    lastMetrics.getVerticalAdvance());
-
             visualAdvance = new Point2D.Float
             ((float)(chunk.advance.getX() + lastW -
                      lastMetrics.getHorizontalAdvance()),
@@ -742,8 +723,6 @@ public class StrokingTextPainter extends
                 visualAdvance = new Point2D.Float(length.floatValue(), 0);
             }
 
-            // System.out.println("Adv: " + advance + " Len: " + length +
-            //                    " scale: [" + xScale + ", " + yScale + "]");
             Point2D.Float adv = new Point2D.Float(0,0);
             for (int n=chunk.begin; n<chunk.end; ++n) {
                 r = (TextRun) textRuns.get(n);
@@ -768,12 +747,9 @@ public class StrokingTextPainter extends
             dy = (float) (-visualAdvance.getY());
             break;
         default:
-            break;
-            // leave untouched
+            break; // leave untouched
         }
 
-        // System.out.println("DX/DY: [" + dx + ", " + dy + "]");
-
         r = (TextRun) textRuns.get(chunk.begin);
         layout = r.getLayout();
         AttributedCharacterIterator runaci = r.getACI();
@@ -815,9 +791,6 @@ public class StrokingTextPainter extends
             tpShiftY  = 0;
         }
 
-        // System.out.println("ABS: [" + absX + "," + absY + "," +
-        //                    visualAdvance.getX() + "," +
-        //                    visualAdvance.getY() + "]");
         for (int n=chunk.begin; n<chunk.end; ++n) {
             r = (TextRun) textRuns.get(n);
             layout = r.getLayout();
@@ -901,8 +874,7 @@ public class StrokingTextPainter extends
                     stroke      = tpi.strikethroughStroke;
                     strokePaint = tpi.strikethroughStrokePaint;
                     break;
-                default:
-                    // should never get here
+                default: // should never get here
                     return;
                 }
             }
@@ -1100,7 +1072,6 @@ public class StrokingTextPainter extends
                 if (bounds == null)
                     bounds = runBounds;
                 else
-                    //bounds = bounds.createUnion(runBounds);
                     bounds.add( runBounds );
             }
         }
@@ -1114,7 +1085,6 @@ public class StrokingTextPainter extends
             if (bounds == null)
                 bounds = underline.getBounds2D();
             else
-                //bounds = bounds.createUnion(underline.getBounds2D());
                 bounds.add( underline.getBounds2D() );
         }
 
@@ -1124,7 +1094,6 @@ public class StrokingTextPainter extends
             if (bounds == null)
                 bounds = strikeThrough.getBounds2D();
             else
-                //bounds = bounds.createUnion(strikeThrough.getBounds2D());
                 bounds.add( strikeThrough.getBounds2D() );
         }
 
@@ -1134,7 +1103,6 @@ public class StrokingTextPainter extends
             if (bounds == null)
                 bounds = overline.getBounds2D();
             else
-                //bounds = bounds.createUnion(overline.getBounds2D());
                 bounds.add( overline.getBounds2D() );
         }
         return bounds;
@@ -1308,8 +1276,7 @@ public class StrokingTextPainter extends
                     stroke      = tpi.strikethroughStroke;
                     strokePaint = tpi.strikethroughStrokePaint;
                     break;
-                default:
-                    // should never get here
+                default: // should never get here
                     return null;
                 }
             }
@@ -1618,7 +1585,7 @@ public class StrokingTextPainter extends
         return highlightedShape;
     }
 
-// inner classes
+    // inner classes
 
     class TextChunk {
 

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GVTAttributedCharacterIterator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GVTAttributedCharacterIterator.java?rev=1615279&r1=1615278&r2=1615279&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GVTAttributedCharacterIterator.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GVTAttributedCharacterIterator.java Sat Aug  2 02:26:11 2014
@@ -345,6 +345,12 @@ public interface GVTAttributedCharacterI
         public static final TextAttribute ARABIC_FORM =
                                           new TextAttribute("ARABIC_FORM");
 
+        public static final TextAttribute SCRIPT =
+                                          new TextAttribute("SCRIPT");
+
+        public static final TextAttribute LANGUAGE =
+                                          new TextAttribute("LANGUAGE");
+
         // VALUES
 
         /** Value for WRITING_MODE indicating left-to-right */

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GlyphLayout.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GlyphLayout.java?rev=1615279&r1=1615278&r2=1615279&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GlyphLayout.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/text/GlyphLayout.java Sat Aug  2 02:26:11 2014
@@ -52,11 +52,11 @@ import org.apache.batik.gvt.font.GVTLine
  */
 public class GlyphLayout implements TextSpanLayout {
 
-    private GVTGlyphVector gv;
+    protected GVTGlyphVector gv;
     private GVTFont font;
     private GVTLineMetrics metrics;
     private AttributedCharacterIterator aci;
-    private Point2D advance;
+    protected Point2D advance;
     private Point2D offset;
     private float   xScale=1;
     private float   yScale=1;
@@ -70,7 +70,7 @@ public class GlyphLayout implements Text
     // When layoutApplied is false it means that the glyph positions
     // are different from where they would be if you did
     // doExplicitGlyphLayout().
-    private boolean layoutApplied = false;
+    protected boolean layoutApplied = false;
     // When spacingApplied is false it means that xScale, yScale and
     // kerning/wordspacing stuff haven't been applied. This can
     // be rectified by calling adjustTextSpacing().  Note that when
@@ -250,7 +250,6 @@ public class GlyphLayout implements Text
             this.adjSpacing = adjSpacing;
 
             // We don't affect layoutApplied directly...
-            // System.out.println("layoutApplied: " + layoutApplied);
 
             // However if we did path layout or spacing it's all junk now...
             spacingApplied = false;
@@ -266,7 +265,6 @@ public class GlyphLayout implements Text
      * (e.g. if the aci has multiple X or Y values).
      */
     public void setOffset(Point2D offset) {
-        // System.err.println("SetOffset: " + offset + " - " + this.offset);
         if ((offset.getX() != this.offset.getX()) ||
             (offset.getY() != this.offset.getY())) {
             if ((layoutApplied)||(spacingApplied)) {
@@ -276,7 +274,6 @@ public class GlyphLayout implements Text
                 float dy = (float)(offset.getY()-this.offset.getY());
                 int numGlyphs = gv.getNumGlyphs();
 
-                // System.out.println("DXY: [" + dx +","+dy+"]");
                 float [] gp = gv.getGlyphPositions(0, numGlyphs+1, null);
                 Point2D.Float pos = new Point2D.Float();
                 for (int i=0; i<=numGlyphs; i++) {
@@ -365,7 +362,6 @@ public class GlyphLayout implements Text
      */
     private final void syncLayout() {
         if (!pathApplied) {
-            // System.out.println("Doing Path Layout: " + this);
             doPathLayout();
         }
     }
@@ -638,8 +634,6 @@ public class GlyphLayout implements Text
                             // box isn't empty so use it's points...
                             ptIdx += 2;
                     } else {
-                        // System.out.println("Type: " + type +
-                        //                    " count: " + count);
                         // Wasn't a quadralateral so just add it don't try
                         // and merge it...
                         addPtsToPath(shape, topPts, botPts, ptIdx);
@@ -666,7 +660,6 @@ public class GlyphLayout implements Text
     public static int makeConvexHull(Point2D.Float [] pts, int numPts) {
         // Sort the Pts in X...
         Point2D.Float tmp;
-        // System.out.print("Sorting...");
         for (int i=1; i<numPts; i++) {
             // Simple bubble sort (numPts should be small so shouldn't
             // be too bad.).
@@ -680,8 +673,6 @@ public class GlyphLayout implements Text
             }
         }
 
-        // System.out.println("Sorted");
-
         Point2D.Float pt0 = pts[0];
         Point2D.Float pt1 = pts[numPts-1];
         Point2D.Float dxdy = new Point2D.Float(pt1.x-pt0.x, pt1.y-pt0.y);
@@ -790,7 +781,6 @@ public class GlyphLayout implements Text
         for (int n=nBotPts-1; n>0; n--, i++)
             pts[i] = botList[n];
 
-        // System.out.println("CHull has " + i + " pts");
         return i;
     }
 
@@ -943,8 +933,6 @@ public class GlyphLayout implements Text
             Shape gbounds = gv.getGlyphLogicalBounds(i);
             if (gbounds != null) {
                 Rectangle2D gbounds2d = gbounds.getBounds2D();
-                // System.out.println("Hit Test: [" + x + ", " + y + "] - " +
-                //                    gbounds2d);
                 if (gbounds.contains(x, y)) {
                     boolean isRightHalf =
                         (x > (gbounds2d.getX()+(gbounds2d.getWidth()/2d)));
@@ -1076,7 +1064,6 @@ public class GlyphLayout implements Text
             (metrics.getAscent() + Math.abs(metrics.getDescent()));
 
         int numGlyphs = gv.getNumGlyphs();
-        // System.out.println("NumGlyphs: " + numGlyphs);
 
         float[] gp = gv.getGlyphPositions(0, numGlyphs+1, null);
         float verticalFirstOffset = 0f;
@@ -1105,7 +1092,6 @@ public class GlyphLayout implements Text
         boolean hasArabicTransparent = false;
 
         while (i < numGlyphs) {
-            //System.out.println("limit: " + runLimit + ", " + aciIndex);
             if (aciIndex+aciStart >= runLimit) {
                 runLimit = aci.getRunLimit(runAtts);
                 x        = (Float) aci.getAttribute(X);
@@ -1168,7 +1154,6 @@ public class GlyphLayout implements Text
             float glyphOrientationRotation = 0f;
             float glyphRotation = 0f;
 
-
             if (ch != CharacterIterator.DONE) {
                 if (vertical) {
                     if (glyphOrientationAuto) {
@@ -1520,12 +1505,10 @@ public class GlyphLayout implements Text
         if ((kern != null) && (!kern.isNaN())) {
             kernVal = kern.floatValue();
             autoKern = false;
-            //System.out.println("KERNING: "+kernVal);
         }
         if ((letterSpacing != null) && (!letterSpacing.isNaN())) {
             letterSpacingVal = letterSpacing.floatValue();
             doLetterSpacing = true;
-            //System.out.println("LETTER-SPACING: "+letterSpacingVal);
         }
         if ((wordSpacing != null) && (!wordSpacing.isNaN())) {
             doWordSpacing = true;
@@ -1841,16 +1824,6 @@ public class GlyphLayout implements Text
                 glyphMidY -= (float)currentGlyphPos.getY();
             }
 
-            // System.err.println("GMX: " + glyphMidX +
-            //                    " W2: " + (glyphWidth/2) +
-            //                    " PosX: " + currentGlyphPos.getX() +
-            //                    " BX: "   + glyphBounds.getX());
-            //
-            // System.err.println("GMY: " + glyphMidY +
-            //                    " H2: " + (glyphHeight/2) +
-            //                    " PosY: " + currentGlyphPos.getY() +
-            //                    " BY: "   + glyphBounds.getY());
-
             float charMidPos;
             if (horizontal) {
                 charMidPos = currentPosition + glyphMidX;

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/script/ImportInfo.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/script/ImportInfo.java?rev=1615279&r1=1615278&r2=1615279&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/script/ImportInfo.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/script/ImportInfo.java Sat Aug  2 02:26:11 2014
@@ -232,7 +232,7 @@ public class ImportInfo {
                 } catch ( IOException ignored ){}
                 r = null;
             }
-            if ( br == null ){
+            if ( br != null ){
                 try{
                     br.close();
                 } catch ( IOException ignored ){}

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/svggen/ImageCacher.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/svggen/ImageCacher.java?rev=1615279&r1=1615278&r2=1615279&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/svggen/ImageCacher.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/svggen/ImageCacher.java Sat Aug  2 02:26:11 2014
@@ -327,9 +327,9 @@ public abstract class ImageCacher implem
         boolean imagesMatch(Object o1, Object o2)
                 throws SVGGraphics2DIOException {
             boolean match = false;
+            FileInputStream imageStream = null;
             try {
-                FileInputStream imageStream =
-                                    new FileInputStream((File) o1);
+                imageStream = new FileInputStream((File) o1);
                 int imageLen = imageStream.available();
                 byte[] imageBytes = new byte[imageLen];
                 byte[] candidateBytes =
@@ -345,6 +345,12 @@ public abstract class ImageCacher implem
             } catch(IOException e) {
                 throw new SVGGraphics2DIOException(
                                     ERR_READ+((File) o1).getName());
+            } finally {
+                try {
+                    if (imageStream != null)
+                        imageStream.close();
+                } catch (IOException e) {
+                }
             }
             return match;
         }