1    package org.xwt.imp;
2    import java.io.*;
3    
4    public class Freetype {
5      /*
6        private static final int OURWIDTH=512;
7        private static final int OURHEIGHT=128;
8        private byte[] render = new byte[OURWIDTH*OURHEIGHT];
9        private StringBuffer sb = new StringBuffer();
10       private View view;
11       private Image image;
12       
13       private MIPSInterpreter emu;
14       int renderAddr;
15       int stringAddr;
16       int FONT_RESERVED = 256*1024;
17       int RENDER_RESERVED = OURWIDTH*OURHEIGHT;
18       int STRING_RESERVED = 1024;
19       
20       public static void main(String[] argv) throws Exception {
21           new FreeTypeDemo(argv);
22       }
23       
24       public FreeTypeDemo(String[] argv) throws Exception {
25           emu = new MIPSInterpreter();
26           emu.loadImage("freetype.mips");
27           int baseAddr = emu.sbrk(FONT_RESERVED+RENDER_RESERVED+STRING_RESERVED);
28           renderAddr = baseAddr + FONT_RESERVED;
29           stringAddr = renderAddr + RENDER_RESERVED;
30           byte[] font = new byte[FONT_RESERVED];
31           int p=0;
32           FileInputStream fis = new FileInputStream(argv[0]);
33           while(p < font.length) {
34               int n = fis.read(font,p,font.length-p);
35               if(n==-1) break;
36               p+=n;
37           }
38           if(p > font.length) throw new Error("Font too big");
39           emu.copyout(font,baseAddr,font.length);
40           emu.setUserInfo(0,baseAddr);
41           emu.setUserInfo(1,p);
42           emu.setUserInfo(2,renderAddr);
43           emu.setUserInfo(3,stringAddr);
44           
45           createImage();
46           
47           frame = new JFrame("FreeTypeDemo");
48           frame.setSize(OURWIDTH+32,OURHEIGHT+32);
49           view = new View();
50           frame.getContentPane().add(view,BorderLayout.CENTER);
51           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52           frame.show();
53           view.requestFocus();
54           emu.start(new String[]{ "freetype.mips"});
55           emu.execute();
56       }
57      
58       private static ColorModel cmodel = new DirectColorModel(8, 0xff,0xff,0xff);
59       private void createImage() {
60           for(int i=0;i<OURHEIGHT;i++)
61               for(int j=0;j<OURWIDTH;j++)
62                   render[i*OURWIDTH+j] = (byte)((~(render[i*OURWIDTH+j]&0xff))&0xff);
63           image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(OURWIDTH, OURHEIGHT, cmodel, render, 0, OURWIDTH));
64           MediaTracker mediatracker = new MediaTracker(new Canvas());
65           mediatracker.addImage(image, 1);
66           try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
67           mediatracker.removeImage(image);
68       }
69       private void renderText(String s) {
70           try {
71               byte[] b = (s+"\0").getBytes("UTF-16BE");
72               emu.copyout(b,stringAddr,Math.min(b.length,STRING_RESERVED));
73               long start = System.currentTimeMillis();
74               emu.execute();
75               System.out.println("Render of: " + s + " took " + (System.currentTimeMillis()-start) + " ms");
76               emu.copyin(renderAddr,render,render.length);
77               createImage();
78               view.repaint();
79           } catch(Exception e) {
80               throw new Error(e);
81           }
82       }
83       
84       private void keyPress(char c) {
85           if(c == '\n' || c == '\r') {
86               sb.setLength(0);
87           } else {
88               sb.append(c);
89               renderText(sb.toString());
90           }
91       }
92                   
93       public class View extends JComponent {
94           public void paintComponent(Graphics g) {
95               g.drawImage(image,0,0,OURWIDTH,OURHEIGHT,0,0,OURWIDTH,OURHEIGHT,null);
96           }
97           public View() {
98               addKeyListener(new KeyAdapter() {
99                   public void keyTyped(KeyEvent e) {
100                      keyPress(e.getKeyChar());
101                  }
102              });
103              setPreferredSize(new Dimension(OURWIDTH,OURHEIGHT));
104          }
105      }
106    */
107  }
108