]> source.dussan.org Git - poi.git/commitdiff
#52297 - Bullets are not aligned properly while converting ppt slide to image
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 8 Nov 2015 23:11:22 +0000 (23:11 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 8 Nov 2015 23:11:22 +0000 (23:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1713316 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/sl/draw/DrawTextParagraph.java

index fbb40bb23814671557566e59b7fa060f367f6519..474d357137f5dcebbcdd8303dac70bbcba506bd0 100644 (file)
@@ -43,6 +43,7 @@ import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
 import org.apache.poi.sl.usermodel.TextRun;\r
 import org.apache.poi.sl.usermodel.TextRun.TextCap;\r
 import org.apache.poi.sl.usermodel.TextShape;\r
+import org.apache.poi.util.StringUtil;\r
 import org.apache.poi.util.Units;\r
 \r
 public class DrawTextParagraph implements Drawable {\r
@@ -96,7 +97,7 @@ public class DrawTextParagraph implements Drawable {
         if (indent == null) {\r
             indent = Units.toPoints(347663*indentLevel);\r
         }\r
-        if (paragraph.getClass().getName().contains("HSLF")) {\r
+        if (isHSLF()) {\r
             // special handling for HSLF\r
             indent -= leftMargin;\r
         }\r
@@ -265,7 +266,7 @@ public class DrawTextParagraph implements Drawable {
         String buFont = bulletStyle.getBulletFont();\r
         if (buFont == null) buFont = paragraph.getDefaultFontFamily();\r
         assert(buFont != null);\r
-\r
+        \r
         PlaceableShape<?,?> ps = getParagraphShape();\r
         PaintStyle fgPaintStyle = bulletStyle.getBulletFontColor();\r
         Paint fgPaint;\r
@@ -282,7 +283,7 @@ public class DrawTextParagraph implements Drawable {
         else fontSize = (float)-buSz;\r
 \r
         \r
-        AttributedString str = new AttributedString(buCharacter);\r
+        AttributedString str = new AttributedString(mapFontCharset(buCharacter,buFont));\r
         str.addAttribute(TextAttribute.FOREGROUND, fgPaint);\r
         str.addAttribute(TextAttribute.FAMILY, buFont);\r
         str.addAttribute(TextAttribute.SIZE, fontSize);\r
@@ -322,9 +323,9 @@ public class DrawTextParagraph implements Drawable {
      */\r
     private String tab2space(TextRun tr) {\r
         AttributedString string = new AttributedString(" ");\r
-        String typeFace = tr.getFontFamily();\r
-        if (typeFace == null) typeFace = "Lucida Sans";\r
-        string.addAttribute(TextAttribute.FAMILY, typeFace);\r
+        String fontFamily = tr.getFontFamily();\r
+        if (fontFamily == null) fontFamily = "Lucida Sans";\r
+        string.addAttribute(TextAttribute.FAMILY, fontFamily);\r
 \r
         Double fs = tr.getFontSize();\r
         if (fs == null) fs = 12d;\r
@@ -362,6 +363,10 @@ public class DrawTextParagraph implements Drawable {
         Rectangle2D anchor = DrawShape.getAnchor(graphics, paragraph.getParentShape());\r
 \r
         int indentLevel = paragraph.getIndentLevel();\r
+        if (indentLevel == -1) {\r
+            // default to 0, if indentLevel is not set\r
+            indentLevel = 0;\r
+        }\r
         Double leftMargin = paragraph.getLeftMargin();\r
         if (leftMargin == null) {\r
             // if the marL attribute is omitted, then a value of 347663 is implied\r
@@ -383,7 +388,7 @@ public class DrawTextParagraph implements Drawable {
             width = ts.getSheet().getSlideShow().getPageSize().getWidth() - anchor.getX();\r
         } else {\r
             width = anchor.getWidth() - leftInset - rightInset - leftMargin - rightMargin;\r
-            if (firstLine) {\r
+            if (firstLine && !isHSLF()) {\r
                 if (bullet != null){\r
                     if (indent > 0) width -= indent;\r
                 } else {\r
@@ -442,14 +447,6 @@ public class DrawTextParagraph implements Drawable {
             // skip empty runs\r
             if (runText.isEmpty()) continue;\r
 \r
-            int beginIndex = text.length();\r
-            text.append(runText);\r
-            int endIndex = text.length();\r
-\r
-            PaintStyle fgPaintStyle = run.getFontColor();\r
-            Paint fgPaint = new DrawPaint(ps).getPaint(graphics, fgPaintStyle);\r
-            attList.add(new AttributedStringData(TextAttribute.FOREGROUND, fgPaint, beginIndex, endIndex));\r
-\r
             // user can pass an custom object to convert fonts\r
             String fontFamily = run.getFontFamily();\r
             @SuppressWarnings("unchecked")\r
@@ -463,8 +460,17 @@ public class DrawTextParagraph implements Drawable {
             if (fontFamily == null) {\r
                 fontFamily = paragraph.getDefaultFontFamily();\r
             }\r
+            \r
+            int beginIndex = text.length();\r
+            text.append(mapFontCharset(runText,fontFamily));\r
+            int endIndex = text.length();\r
+\r
             attList.add(new AttributedStringData(TextAttribute.FAMILY, fontFamily, beginIndex, endIndex));\r
 \r
+            PaintStyle fgPaintStyle = run.getFontColor();\r
+            Paint fgPaint = new DrawPaint(ps).getPaint(graphics, fgPaintStyle);\r
+            attList.add(new AttributedStringData(TextAttribute.FOREGROUND, fgPaint, beginIndex, endIndex));\r
+\r
             Double fontSz = run.getFontSize();\r
             if (fontSz == null) fontSz = paragraph.getDefaultFontSize();\r
             attList.add(new AttributedStringData(TextAttribute.SIZE, fontSz.floatValue(), beginIndex, endIndex));\r
@@ -506,5 +512,41 @@ public class DrawTextParagraph implements Drawable {
         return string;\r
     }\r
 \r
+    protected boolean isHSLF() {\r
+        return paragraph.getClass().getName().contains("HSLF");        \r
+    }\r
+    \r
+    /**\r
+     * Map text charset depending on font family.\r
+     * Currently this only maps for wingdings font (into unicode private use area)\r
+     *\r
+     * @param text the raw text\r
+     * @param fontFamily the font family\r
+     * @return AttributedString with mapped codepoints\r
+     * \r
+     * @see <a href="http://stackoverflow.com/questions/8692095">Drawing exotic fonts in a java applet</a>\r
+     * @see StringUtil#mapMsCodepointString(String)\r
+     */\r
+    protected String mapFontCharset(String text, String fontFamily) {\r
+        // TODO: find a real charset mapping solution instead of hard coding for Wingdings\r
+        String attStr = text;\r
+        if ("Wingdings".equalsIgnoreCase(fontFamily)) {\r
+            // wingdings doesn't contain high-surrogates, so chars are ok\r
+            boolean changed = false;\r
+            char chrs[] = attStr.toCharArray();\r
+            for (int i=0; i<chrs.length; i++) {\r
+                // only change valid chars\r
+                if ((0x20 <= chrs[i] && chrs[i] <= 0x7f) ||\r
+                    (0xa0 <= chrs[i] && chrs[i] <= 0xff)) {\r
+                    chrs[i] |= 0xf000;\r
+                    changed = true;\r
+                }\r
+            }\r
 \r
+            if (changed) {\r
+                attStr = new String(chrs);\r
+            }\r
+        }\r
+        return attStr;\r
+    }\r
 }\r