Hi Riad,

I've attached a patch that does formatted PDF output. Nested Phrase
objects were needed, as was some XML parsing. No wonder you left it as
unformatted output in the first place! :)

I've hardwired the PDF output for "vertical arrangement" (question on
top row, answer on bottom row). Maybe a new export option is needed in
the GUI (say "PDF (vertical)" and "PDF (horizontal)").

I'll leave you to factor in my patch. Let me know when you're done, then
I'll update from svn trunk. Thanks!

Regards
Jonathan

Jonathan Wong wrote:
> Hi Riad,
>
> Great usability improvements in the trunk!
>
> I was gonna do a PDF output that shows longer answers (with short
> questions), as I mentioned on the "Open Discussion" forum:
>
> "Specifically, I would like to have jMemorize output each card to a
> 4-cell table (2 by 2). The left column will just display "Question" and
> "Answer". The right column displays the question (top) and answer (bottom)."
>
> But your CardPanel's use of JSplitPane may make it unnecessary for that
> PDF output. Whenever I want to study (not learn yet) the cards, I can
> now reduce the front-side (Question) size, and increase the flip-side
> size. The flip-side contains short answers in bold, plus long
> explanatory texts.
>
> I'll still want to do the PDF output at some point. Good to carry around
> an easy print-out of the cards to study.
>
> Have you planned anything on J2ME midlets yet?
>
> Regards
> Jonathan
>
>
>   

Index: src/jmemorize/core/io/PdfRtfBuilder.java
===================================================================
--- src/jmemorize/core/io/PdfRtfBuilder.java    (revision 1127)
+++ src/jmemorize/core/io/PdfRtfBuilder.java    (working copy)
@@ -18,6 +18,7 @@
  */
 package jmemorize.core.io;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -25,6 +26,14 @@
 import java.util.List;
 import java.util.logging.Logger;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
 import jmemorize.core.Card;
 import jmemorize.core.Category;
 import jmemorize.core.Lesson;
@@ -170,6 +179,78 @@
     }
 
     /**
+     * Takes a formatted text string, and returns formatted Phrase(s). A 
formatted Phrase is possibly made up
+     * of many nested Phrase(s).
+     * 
+     * @param formattedText What you get from FormattedText.getFormatted()
+     * @param font Font to use for top-level Phrase
+     * @return A Phrase with formatting.
+     */
+    private static Phrase getPhrase(String formattedText, Font font) {
+      String text = "<xml>" + formattedText + "</xml>";
+      DocumentBuilder builder;
+      Phrase phrase = new Phrase();
+      try {
+        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        org.w3c.dom.Document doc = builder.parse(new 
ByteArrayInputStream(text.getBytes()));
+        phrase = getPhrase(doc.getDocumentElement());
+        phrase.setFont(font);
+      } catch (ParserConfigurationException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      } catch (SAXException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      } catch (IOException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+      return phrase; // returns an empty Phrase if all else fails.
+    }
+    
+    /**
+     * 
+     * @param parentNode The parent Node.
+     * @return
+     */
+    private static Phrase getPhrase(Node parentNode) {
+      Phrase phrase = new Phrase();
+
+      String fontName = 
Settings.loadFont(FontType.CARD_FRONT).getFont().getFamily();
+      Font font = FontFactory.getFont(fontName, BaseFont.IDENTITY_H, 
BaseFont.EMBEDDED);
+      
+      // Get the current node name, and set the font style if it's a 
recognizable style tag.
+      String nodeName = parentNode.getNodeName();
+      if (nodeName.equalsIgnoreCase("i")) {
+        font.setStyle(Font.ITALIC);
+      }
+      else if (nodeName.equalsIgnoreCase("b")) {
+        font.setStyle(Font.BOLD);
+      }
+      else if (nodeName.equalsIgnoreCase("u")) {
+        font.setStyle(Font.UNDERLINE);
+      } 
+      else {
+        // Ignore unknown style tag.
+      }
+      phrase.setFont(font);
+      
+      NodeList list = parentNode.getChildNodes();
+      for (int i = 0; i < list.getLength(); i++) {
+        Node node = list.item(i);
+        if (node.getNodeType() == Node.TEXT_NODE) {
+          phrase.add(new Phrase(node.getNodeValue()));
+        }
+        else if (node.getNodeType() == Node.ELEMENT_NODE) {
+          // Nested node. Recurse.
+          phrase.add(getPhrase(node));
+        }
+      }
+
+      return phrase;
+    }
+
+    /**
      * Adds given card to document
      * 
      * @param doc document to add to
@@ -178,16 +259,19 @@
     private static void writeCard(Document doc, Card card) 
         throws DocumentException
     {
-        Table table = new Table(2);
+        Table table = new Table(1);
     
         table.setPadding(3f);
         table.setBorderWidth(1.0f);
+        // This doesn't work at all, bug in Table. Try having long phrases for 
back side to test this. 
         table.setTableFitsPage(true);
         table.complete();
-    
-        Phrase front = new 
Phrase(card.getFrontSide().getText().getUnformatted(), frontFont);
+ 
+        //Phrase front = new 
Phrase(card.getFrontSide().getText().getUnFormatted(), frontFont);
+        Phrase front = getPhrase(card.getFrontSide().getText().getFormatted(), 
frontFont);
         table.addCell(front);
-        Phrase back = new 
Phrase(card.getBackSide().getText().getUnformatted(), backFont);
+        //Phrase back = new 
Phrase(card.getBackSide().getText().getUnformatted(), backFont);
+        Phrase back = getPhrase(card.getBackSide().getText().getFormatted(), 
backFont);
         table.addCell(back);
     
         doc.add(table);
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
jmemorize-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmemorize-developers

Reply via email to