1 package com.tapina.robe.swi;
2
3 import com.tapina.robe.runtime.Environment;
4 import com.tapina.robe.runtime.FontUtils;
5
6 import java.lang.reflect.Method;
7 import java.awt.*;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10
11 /***
12 * This class represents a standard RISC OS module.
13 */
14 public final class Font extends SWIHandler {
15 private static Font ourInstance;
16
17 public synchronized static Font getInstance() {
18 if (ourInstance == null) {
19 ourInstance = new Font();
20 }
21 return ourInstance;
22 }
23
24 private Font() {
25 }
26
27 public static int getBase() {
28 return 0x40080;
29 }
30
31 public String getName() {
32 return "FontManager";
33 }
34
35 private static final String[] methodNames = new String[] {
36 "", "", "", "", "", "", "", "",
37 "", "", "", "", "", "", "", "",
38 "", "ListFonts", "", "", "", "", "", "",
39 "", "", "", "", "", "", "", "",
40 "", "", "", "", "", "", "", "",
41 "", "", "", "", "", "", "", "",
42 "", "", "", "", "", "", "", "",
43 "", "", "", "", "", "", "", ""
44 };
45
46 public static Method getMethod(Integer offset) throws NoSuchMethodException {
47 return getMethod(methodNames[offset.intValue()]);
48 }
49
50 private static final Method getMethod(String name) throws NoSuchMethodException {
51 return Font.class.getMethod(name, METHOD_PARAMETERS);
52 }
53
54 ArrayList allFonts = null;
55
56 /***
57 * This SWI returns, one at a time, the fonts found with the given path, or a font menu.
58 * @param env
59 */
60 public final void ListFonts(Environment env) {
61 final int[] R = env.getCpu().R;
62 if ((R[2] & 0xffff0000) != 0) {
63 throw new SWIError(0, "Font_ListFonts works in RISC OS 2-compatible mode only");
64 }
65 if (allFonts == null) {
66 java.awt.Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
67 allFonts = new ArrayList(fonts.length + 3);
68 allFonts.addAll(Arrays.asList(fonts));
69 allFonts.add(new java.awt.Font("Serif", java.awt.Font.PLAIN, 12));
70 allFonts.add(new java.awt.Font("Monospaced", java.awt.Font.PLAIN, 12));
71 }
72 final int bufferSize = 40;
73 String acornName;
74 final int numFonts = allFonts.size();
75 do {
76 final java.awt.Font font = (java.awt.Font) allFonts.get(R[2]++);
77 acornName = FontUtils.nativeToAcornName(font, bufferSize);
78 } while (acornName == null && R[2] != numFonts);
79
80 if (acornName != null) {
81 env.getMemoryMap().storeStringCR(R[1], acornName);
82
83 R[3] = acornName.length() + 1;
84 }
85
86 if (R[2] == numFonts) {
87 R[2] = -1;
88 }
89 }
90
91 }
92