2003/10/31 09:50:10
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:42 +0000 (07:40 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:42 +0000 (07:40 +0000)
darcs-hash:20040130074042-2ba56-6abdbd3215602af6ed7e5f5b5d7d2dcb2aeda226.gz

src/org/xwt/translators/Freetype.c
src/org/xwt/translators/Freetype.java
src/org/xwt/util/Callback.java [new file with mode: 0644]
src/org/xwt/util/Log.java
src/org/xwt/util/Queue.java

index fad6018..f1b4f50 100644 (file)
@@ -56,12 +56,12 @@ int main(int argc, char** argv) {
     short charcode;
     
     FT_Check(FT_Init_FreeType(&library));
-    emu_pause();
+    FT_Check(FT_New_Memory_Face(library, _user_info[0], (int)_user_info[1], 0, &face));
 
     for(;;) {
-       FT_Check(FT_New_Memory_Face(library, _user_info[0], (int)_user_info[1], 0, &face));
-       FT_Check(FT_Set_Char_Size(face, 0, ((int)_user_info[4]) * 64, 72, 72));
-
+      
+        emu_pause();
+        FT_Check(FT_Set_Char_Size(face, 0, ((int)_user_info[4]) * 64, 72, 72));
         for(charcode = (int)_user_info[2]; charcode <= (int)_user_info[3]; charcode++) {
 
             glyph_index = FT_Get_Char_Index(face, charcode);
@@ -72,11 +72,10 @@ int main(int argc, char** argv) {
             _user_info[6]  = (char*)face->glyph->bitmap.width;
             _user_info[7]  = (char*)face->glyph->bitmap.rows;
             _user_info[8]  = (char*)(face->size->metrics.ascender >> 6);
-            _user_info[9]  = (char*)(face->size->metrics.descender >> 6);
+            _user_info[9]  = (char*)((-1 * face->size->metrics.descender) >> 6);
             _user_info[10] = (char*)(face->glyph->metrics.horiBearingY >> 6);
             _user_info[11] = (char*)(face->glyph->advance.x >> 6);
 
-            emu_pause();
         }
     }
 }
index 411a409..9d89d7f 100644 (file)
@@ -3,72 +3,66 @@ import org.xwt.*;
 import org.xwt.util.*;
 import java.io.*;
 import java.util.zip.*;
+import java.util.*;
 
 // FEATURE: use streams, not memoryfont's
 // FEATURE: kerning pairs
 public class Freetype {
 
-    Freetype() { }
+    public Freetype() { }
 
-    private static org.xwt.mips.Interpreter vm = null;
+    private static byte[] image = null;
+    private static final int FONT_RESERVED = 256*1024;
 
-    public static void renderGlyphs(Res res, int pointsize, int firstGlyph, int lastGlyph, Cache glyphCache) {
+    private org.xwt.mips.Interpreter vm = null;
 
+    private Res loadedStream = null;
+
+    public void loadFontByteStream(Res res) {
         try {
+            Log.log(this, "loading font " + res.getDescriptiveName());
+            loadedStream = res;
             InputStream is = res.getInputStream();
             byte[] fontstream = InputStreamToByteArray.convert(is);
+            if (image == null) image = InputStreamToByteArray.convert(Main.builtin.getInputStream("freetype.mips"));
+            vm = new org.xwt.mips.Interpreter(image);
+            int baseAddr = vm.sbrk(FONT_RESERVED);
+            vm.copyout(fontstream, baseAddr, fontstream.length);
+            vm.setUserInfo(0, baseAddr);
+            vm.setUserInfo(1, fontstream.length);
+            vm.start(new String[]{ "freetype.mips" });
+            vm.execute();
+        } catch (Exception e) {
+            Log.log(this, e);
+        }
+    }
 
-            synchronized(Freetype.class) {
-                if (vm == null) {
-                    InputStream bis = Platform.getBuiltinInputStream();
-                    ZipInputStream zis = new ZipInputStream(bis);
-                    for(ZipEntry ze = zis.getNextEntry(); ze != null && !ze.getName().equals("freetype.mips"); ze = zis.getNextEntry()) { }
-                    byte[] image = InputStreamToByteArray.convert(zis);
-                    vm = new org.xwt.mips.Interpreter(image);
-                    vm.start(new String[]{ "freetype.mips" });
-                    vm.execute();
-                }
-                
-                int FONT_RESERVED = 256*1024;
-                int baseAddr = vm.sbrk(FONT_RESERVED);
-                
-                vm.copyout(fontstream, baseAddr, fontstream.length);
-                vm.setUserInfo(0, baseAddr);
-                vm.setUserInfo(1, fontstream.length);
-                vm.setUserInfo(2, firstGlyph);
-                vm.setUserInfo(3, lastGlyph);
-                vm.setUserInfo(4, pointsize);
-                
-                long start = System.currentTimeMillis();
+    public synchronized void renderGlyph(Res res, Glyph glyph) {
+        try {
+            if (loadedStream != res) loadFontByteStream(res);
+            vm.setUserInfo(2, (int)glyph.c);
+            vm.setUserInfo(3, (int)glyph.c);
+            vm.setUserInfo(4, glyph.pointsize);
+            long start = System.currentTimeMillis();
+            vm.execute();
+            glyph.max_ascent = vm.getUserInfo(8);
+            glyph.max_descent = vm.getUserInfo(9);
+            glyph.baseline = vm.getUserInfo(10);
+            glyph.advance = vm.getUserInfo(11);
+            
+            int width = vm.getUserInfo(6);
+            int height = vm.getUserInfo(7);
+            if (width == 0 || height == 0) {
+                Log.log(Freetype.class, "warning glyph has zero width/height");
                 
-                for(int g = firstGlyph; g <= lastGlyph; g++) {
-                    vm.execute();
-                    
-                    Glyph glyph = new Glyph();
-                    glyph.max_ascent = vm.getUserInfo(8);
-                    glyph.max_descent = vm.getUserInfo(9);
-                    glyph.baseline = vm.getUserInfo(10);
-                    glyph.advance = vm.getUserInfo(11);
-                    glyph.c = (char)g;
-                    
-                    int width = vm.getUserInfo(6);
-                    int height = vm.getUserInfo(7);
-                    
-                    if (width == 0 || height == 0) {
-                        Log.log(Freetype.class, "warning glyph " + g + " has zero width/height");
-                        
-                    } else {
-                        byte[] data = new byte[width * height];
-                        int addr = vm.getUserInfo(5);
-                        vm.copyin(addr,data,width*height);
-                        glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
-                    }
-                    
-                    glyphCache.put(res, new Integer((g << 16) | pointsize), glyph);
-                }
+            } else {
+                byte[] data = new byte[width * height];
+                int addr = vm.getUserInfo(5);
+                vm.copyin(addr,data,width*height);
+                glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
             }
         } catch (Exception e) {
-            Log.log(Freetype.class, e);
+            Log.log(this, e);
         }
     }
 }
diff --git a/src/org/xwt/util/Callback.java b/src/org/xwt/util/Callback.java
new file mode 100644 (file)
index 0000000..16cba39
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt.util;
+
+/** a simple interface for callbacks*/
+public interface Callback {
+
+    public abstract Object call(Object arg);
+
+}
index 05ee8d3..eb456d1 100644 (file)
@@ -18,7 +18,7 @@ public class Log {
     /** log a message with the current JavaScript sourceName/line */
     public static void logJS(Object o, Object message) { logJS(message); }
     public static void logJS(Object message) {
-        JS.Thread current = org.xwt.js.JS.Thread.fromJavaThread(Thread.currentThread());
+        JS.Thread current = org.xwt.js.JS.Thread.current();
         if (current == null) {
             log("<none>", message);
         } else {
index 7ed4850..a6f0a92 100644 (file)
@@ -38,7 +38,17 @@ public class Queue {
         for(int i=0; i<vec.length; i++) vec[i] = null;
     }
 
-    /** Add an element to the queue */
+    /** Add an element to the front of the queue */
+    public synchronized void prepend(Object o) {
+        if (size == vec.length) grow(vec.length * 2);
+        first--;
+        if (first < 0) first += vec.length;
+        vec[first] = o;
+        size++;
+        if (size == 1) notify();
+    }
+    
+    /** Add an element to the back of the queue */
     public synchronized void append(Object o) {
         if (size == vec.length) grow(vec.length * 2);
         if (first + size >= vec.length) vec[first + size - vec.length] = o;