[sword-svn] r131 - trunk/app/src/org/crosswire/flashcards

Apache apache at www.crosswire.org
Fri Nov 9 02:28:58 MST 2007


Author: 
Date: 2007-11-09 02:28:58 -0700 (Fri, 09 Nov 2007)
New Revision: 131

Modified:
   trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
   trunk/app/src/org/crosswire/flashcards/LessonManager.java
Log:
added ability to specify lessonFont in a lessonXXX.flash file


Modified: trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2007-11-08 16:20:11 UTC (rev 130)
+++ trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2007-11-09 09:28:58 UTC (rev 131)
@@ -23,12 +23,14 @@
 
 import java.awt.Color;
 import java.awt.Font;
+import java.awt.FontFormatException;
 import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.InputStream;
 import java.net.JarURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -42,9 +44,10 @@
  * A Lesson is an ordered list of FlashCards.
  * The lesson also has a description which is useful for showing to a user.
  */
-public class ComplexLesson
-          extends Lesson {
+public class ComplexLesson extends Lesson {
 
+     private static final String DIR_PROJECT = ".flashcards";
+
      public ComplexLesson(String url) throws Exception {
           super(url);
      }
@@ -60,12 +63,21 @@
       */
      public void load() {
           try {
+               String homeProjectPath = System.getProperty("user.home") + File.separator + DIR_PROJECT;
                URL lessonURL = new URL(getURL());
                Properties lesson = new Properties();
                lesson.load(lessonURL.openConnection().getInputStream());
                int wordCount = Integer.parseInt(lesson.getProperty("wordCount"));
                setDescription(lesson.getProperty("lessonTitle", getURL().substring(getURL().lastIndexOf('/') + 1)));
-
+               String font = lesson.getProperty("lessonFont");
+               if (font != null && font.length() > 0) {
+                    String fontPath = homeProjectPath + File.separator + font+ ".ttf";
+                    File fontFile = new File(fontPath);
+                    if (fontFile.exists()) {
+                         String url = fontFile.toURL().toString();
+                         setFont(url);
+                    }
+               }
                int baseOffset = getURL().lastIndexOf("/");
                if (baseOffset < 0) {
                     baseOffset = getURL().lastIndexOf( ("\\"));
@@ -144,7 +156,39 @@
           }
      }
 
+     public Font loadFont(String url) {
+          Font font = null;
+          if (url.length() > 2) {
+               InputStream is = null;
+               try {
+                    URLConnection connection = new URL(url).openConnection();
+                    is = connection.getInputStream();
+                    font = loadFont(is);
+               }
+               catch (IOException ex) {
+                    ex.printStackTrace(System.err);
+               }
+               catch (FontFormatException e) {
+                    e.printStackTrace(System.err);
+               } finally {
+                    if (is != null) {
+                         try {
+                              is.close();
+                         } catch (IOException e) {
+                              e.printStackTrace(System.err);
+                         }
+                    }
+               }
+          }
+          return font;
+     }
 
+
+     public Font loadFont(InputStream is) throws FontFormatException, IOException {
+          Font font = Font.createFont(Font.TRUETYPE_FONT, is);
+          return font;
+     }
+
      /**
       * Save this lesson to persistent store named by the lesson's <code>filename</code>.
       */
@@ -159,6 +203,14 @@
                String lname = getURL().substring(baseOffset + 1);
                lname = lname.substring(0, lname.indexOf(".flash"));
                String imagesPath = getURL().substring(0, baseOffset) + "/images";
+               final int width = 800;
+               final int height = 40;
+               Font font = null;
+               if (getFont() != null && getFont().length() > 0) {
+                    Font newFont = loadFont(getFont());
+                    font = newFont.deriveFont(Font.BOLD, (int)(height*.75));
+               }
+//               else font = new Font(g2d.getFont().getName(), Font.BOLD, (int)(height*.75));
 
                int i = 0;
                for (; i < getFlashcards().size(); i++) {
@@ -181,8 +233,6 @@
                          dir.mkdirs();
                     }
                     outStream = new FileOutputStream(file);
-                    final int width = 800;
-                    final int height = 40;
 
                     // Create a buffered image in which to draw
                     BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
@@ -194,9 +244,13 @@
                     g2d.setColor(Color.white);
                     g2d.fillRect(0, 0, width, height);
                     g2d.setColor(Color.black);
-                    Font font = new Font(g2d.getFont().getName(), Font.BOLD, (int)(height*.75));
-                    g2d.setFont(font);
-                    Rectangle2D rect = font.getStringBounds(f.getFront(), g2d.getFontRenderContext());
+
+                   // We need more intelligent font handling here.  Maybe in load(), when we
+                   // grab the font name, we could also look in the lesson for the font file
+                   // itself, otherwise look for it app-wide (whatever that means)
+                    g2d.setFont((font != null) ? font : g2d.getFont().deriveFont(Font.BOLD, (int)(height*.75)));
+
+                    Rectangle2D rect = g2d.getFont().getStringBounds(f.getFront(), g2d.getFontRenderContext());
                     g2d.drawString(f.getFront(), 4, (int)(height*.70));
                     bufferedImage = bufferedImage.getSubimage(0, 0, (int)(rect.getWidth()+8), 40);
 

Modified: trunk/app/src/org/crosswire/flashcards/LessonManager.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/LessonManager.java	2007-11-08 16:20:11 UTC (rev 130)
+++ trunk/app/src/org/crosswire/flashcards/LessonManager.java	2007-11-09 09:28:58 UTC (rev 131)
@@ -275,13 +275,22 @@
          public boolean accept(File dir, String name) {
               return name.toUpperCase(Locale.ENGLISH).endsWith(".JAR");
          }
-    }
-    public static void main( String [ ] arguments ) {
-        // Parse the command line arguments
-        for( int index = 0; arguments.length > index; ++ index ) {
-            if( ( arguments [ index ] ).equals( "-genImages" ) ) {
-                LessonManager.instance().genImages();
-            }
-        }
-    }
+     }
+
+
+     public static void main(String argv[]) {
+          // Parse the command line arguments
+          String font = null;
+          int action = 0;
+          for (int i = 0; i < argv.length; i++) {
+               if ("-genImages".equals(argv[i])) {
+                    action = 1;
+               }
+          }
+          switch (action) {
+               case 0: System.out.println("usage: LessonManager [-genImages]"); break;
+               case 1: LessonManager.instance().genImages(); break;
+               default: break;
+          }
+     }
 }




More information about the sword-cvs mailing list