Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1KaD8j-0003sD-V1 for mharc-grub-devel@gnu.org; Mon, 01 Sep 2008 13:24:34 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KaD8g-0003qC-Dl for grub-devel@gnu.org; Mon, 01 Sep 2008 13:24:30 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KaD8e-0003p5-JK for grub-devel@gnu.org; Mon, 01 Sep 2008 13:24:29 -0400 Received: from [199.232.76.173] (port=37105 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KaD8e-0003or-99 for grub-devel@gnu.org; Mon, 01 Sep 2008 13:24:28 -0400 Received: from gateway05.websitewelcome.com ([67.18.125.8]:55321) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1KaD8c-00051a-JA for grub-devel@gnu.org; Mon, 01 Sep 2008 13:24:27 -0400 Received: (qmail 27834 invoked from network); 1 Sep 2008 17:36:05 -0000 Received: from gator297.hostgator.com (74.53.228.114) by gateway05.websitewelcome.com with SMTP; 1 Sep 2008 17:36:05 -0000 Received: from c-67-185-142-228.hsd1.wa.comcast.net ([67.185.142.228]:58890 helo=gamma.lan) by gator297.hostgator.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1KaD8S-0003th-R5 for grub-devel@gnu.org; Mon, 01 Sep 2008 12:24:17 -0500 Date: Mon, 1 Sep 2008 10:24:13 -0700 From: Colin D Bennett To: The development of GRUB 2 Message-ID: <20080901102413.4719946b@gamma.lan> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.12.11; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/Qkknjr50BpdPPVnYwpqDG6a"; protocol="application/pgp-signature"; micalg=PGP-SHA1 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator297.hostgator.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - gibibit.com X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 3) Subject: [PATCH] GSoC #10 font tools X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Sep 2008 17:24:30 -0000 --Sig_/Qkknjr50BpdPPVnYwpqDG6a Content-Type: multipart/mixed; boundary="MP_/e20.QTiG69Wd3+O3WFdOY/m" --MP_/e20.QTiG69Wd3+O3WFdOY/m Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This patch provides the font converter and viewer tools. The converter can convert BDF fonts to PFF2 format, and the viewer can view either BDF or PFF2 fonts. The tools are written in Java. A simple shell script, 'build.sh', is provided which builds the JAR file containing the compiled Java code. See util/fonttool/README.txt for complete instructions and information on running the tools. Regards, Colin --MP_/e20.QTiG69Wd3+O3WFdOY/m Content-Type: text/x-patch; name=11_font-tools.patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=11_font-tools.patch 2008-09-01 Colin D Bennett Added BDF->PFF2 font conversion and BDF/PFF2 viewing tool. * util/fonttool: New directory. * util/fonttool/README.txt: New file. * util/fonttool/build.sh: New file. * util/fonttool/build.xml: New file. * util/fonttool/fonttool.iml: New file. * util/fonttool/fonttool.ipr: New file. * util/fonttool/makepf2.sh: New file. * util/fonttool/src: New directory. * util/fonttool/src/org: New directory. * util/fonttool/src/org/gnu: New directory. * util/fonttool/src/org/gnu/grub: New directory. * util/fonttool/src/org/gnu/grub/fonttool: New directory. * util/fonttool/src/org/gnu/grub/fonttool/BDFFont.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/BDFLoader.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/CharDefs.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/CharMatrix.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/CharStorageInfo.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/CharView.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/Converter.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/FileFormatException.java: New fi= le. * util/fonttool/src/org/gnu/grub/fonttool/Font.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/Glyph.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/PFF2Loader.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/PFF2Sections.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/PFF2Writer.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/Viewer.java: New file. * util/fonttool/src/org/gnu/grub/fonttool/ViewerFrame.java: New file. * util/fonttool/test: New directory. * util/fonttool/test/org: New directory. * util/fonttool/test/org/gnu: New directory. * util/fonttool/test/org/gnu/grub: New directory. * util/fonttool/test/org/gnu/grub/fonttool: New directory. * util/fonttool/test/org/gnu/grub/fonttool/TestGlyph.java: New file. =3D=3D=3D added directory 'util/fonttool' =3D=3D=3D added file 'util/fonttool/README.txt' --- util/fonttool/README.txt 1970-01-01 00:00:00 +0000 +++ util/fonttool/README.txt 2008-09-01 17:13:43 +0000 @@ -0,0 +1,48 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +GRUB Font Tool +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Building +=3D=3D=3D=3D=3D=3D=3D=3D + +You can build the sources using the ``build.sh`` shell script, with:: + + sh build.sh + +or, you can use the Apache Ant tool to build the sources. The main benefit +using Ant is its incremental compilation when files are modified. Simply = run:: + + ant + +Using either method, the file 'build/fonttool.jar' is generated, and the J= ava +'.class' files are stored in 'build/src/'. + + +Running +=3D=3D=3D=3D=3D=3D=3D + +The font tool currently contains two programs: a viewer and a converter. + + +Running the viewer +------------------ + +Run: + + java -cp build/fonttool.jar org.gnu.grub.fonttool.Viewer myfont.pf2 + +The font viewer can view either PFF2 or BDF format font files. + + +Running the converter +--------------------- + +You can either run the converter directly, as :: + + java -cp build/fonttool.jar org.gnu.grub.fonttool.Converter \ + --in=3Da.bdf --out=3Db.pf2 + +or you can use the 'makepf2.sh' script to do a batch conversion of multiple +font files. This script converts all the BDF fonts specified on the comma= nd +line into PFF2 (.pf2) fonts, storing them in the current directory. + =3D=3D=3D added file 'util/fonttool/build.sh' --- util/fonttool/build.sh 1970-01-01 00:00:00 +0000 +++ util/fonttool/build.sh 2008-09-01 17:11:00 +0000 @@ -0,0 +1,27 @@ +#!/bin/sh +# Build script for font tool. + +die() { + echo "Error: $1" + exit 1 +} + +O=3Dbuild +SRC=3Dsrc + +if [ -e "${O}" ]; then + echo "Cleaning output directory ${O}..." + rm -rf "${O}" || die "Can't delete output directory ${O}" +fi + +mkdir -p "${O}" || die "Can't create output directory ${O}" + +echo "Compiling Java sources..." +mkdir -p "${O}/src" || die "Can't create output src directory ${O}/src" +javac -source 1.5 -target 1.5 -g -deprecation -encoding UTF-8 \ + -d "${O}/src" \ + $(find "${SRC}" -name '*.java') || die "Java compiler failed" + +echo "Building JAR file..." +jar cf "${O}/fonttool.jar" -C "${O}/src" . || die "Could not create JAR fi= le" + =3D=3D=3D added file 'util/fonttool/build.xml' --- util/fonttool/build.xml 1970-01-01 00:00:00 +0000 +++ util/fonttool/build.xml 2008-09-01 16:28:19 +0000 @@ -0,0 +1,23 @@ + + GRUB 2 font tool. + + + + + + + + + + + + + + + + + + + + =3D=3D=3D added file 'util/fonttool/fonttool.iml' --- util/fonttool/fonttool.iml 1970-01-01 00:00:00 +0000 +++ util/fonttool/fonttool.iml 2008-09-01 16:28:19 +0000 @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + =3D=3D=3D added file 'util/fonttool/fonttool.ipr' --- util/fonttool/fonttool.ipr 1970-01-01 00:00:00 +0000 +++ util/fonttool/fonttool.ipr 2008-09-01 16:28:19 +0000 @@ -0,0 +1,306 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + =3D=3D=3D added file 'util/fonttool/makepf2.sh' --- util/fonttool/makepf2.sh 1970-01-01 00:00:00 +0000 +++ util/fonttool/makepf2.sh 2008-09-01 16:28:19 +0000 @@ -0,0 +1,17 @@ +#!/bin/sh + +# This script will generate GRUB 2 PFF2 font files out of BDF. +# You need the GRUB font utilities "Converter" program for this. + +DIR=3D$( dirname $0 ) + +for font in "$@" +do + out=3D`basename "${font}" | cut -d'.' -f1` + out=3D"${out}.pf2" + echo "Converting ${font} -> ${out} ..." + java -cp "${DIR}/build/fonttool.jar" \ + org.gnu.grub.fonttool.Converter \ + --in=3D"${font}" \ + --out=3D"${out}" +done =3D=3D=3D added directory 'util/fonttool/src' =3D=3D=3D added directory 'util/fonttool/src/org' =3D=3D=3D added directory 'util/fonttool/src/org/gnu' =3D=3D=3D added directory 'util/fonttool/src/org/gnu/grub' =3D=3D=3D added directory 'util/fonttool/src/org/gnu/grub/fonttool' =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/BDFFont.java' --- util/fonttool/src/org/gnu/grub/fonttool/BDFFont.java 1970-01-01 00:00:0= 0 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/BDFFont.java 2008-09-01 16:28:1= 9 +0000 @@ -0,0 +1,24 @@ +package org.gnu.grub.fonttool; + +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Map; + +public class BDFFont { + private Map glyphs =3D new HashMap(); + private int height =3D 0; + + void putGlyph(char ch, BufferedImage glyph) { + glyphs.put(ch, glyph); + if (glyph.getHeight() > height) + height =3D glyph.getHeight(); + } + + public BufferedImage getGlyph(char c) { + return glyphs.get(c); + } + + public int getHeight() { + return height; + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/BDFLoader.jav= a' --- util/fonttool/src/org/gnu/grub/fonttool/BDFLoader.java 1970-01-01 00:00= :00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/BDFLoader.java 2008-09-01 16:28= :19 +0000 @@ -0,0 +1,254 @@ +package org.gnu.grub.fonttool; + +import java.io.*; +import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class BDFLoader { + private final BufferedReader in; + private final Font font; + private int maxCharWidth; + private int maxCharHeight; + + BDFLoader(BufferedReader in) { + this.in =3D in; + this.font =3D new Font(); + this.maxCharWidth =3D 0; + this.maxCharHeight =3D 0; + } + + public static boolean isBDFFile(String filename) { + DataInputStream in =3D null; + try { + in =3D new DataInputStream(new FileInputStream(filename)); + final String signature =3D "STARTFONT "; + byte[] b =3D new byte[signature.length()]; + in.readFully(b); + in.close(); + + String s =3D new String(b, "US-ASCII"); + return signature.equals(s); + } catch (IOException e) { + if (in !=3D null) { + try { + in.close(); + } catch (IOException e1) { + // Ignore. + } + } + return false; + } + } + + public static Font loadFontResource(String resourceName) throws IOExce= ption { + InputStream in =3D BDFLoader.class.getClassLoader().getResourceAsS= tream(resourceName); + if (in =3D=3D null) + throw new FileNotFoundException("Font resource " + resourceNam= e + " not found"); + return loadFontFromStream(in); + } + + public static Font loadFontFile(String filename) throws IOException { + InputStream in =3D new FileInputStream(filename); + return loadFontFromStream(in); + } + + private static Font loadFontFromStream(InputStream inStream) throws IO= Exception { + BufferedReader in; + try { + in =3D new BufferedReader(new InputStreamReader(inStream, "ISO= -8859-1")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Encoding not supported: " + e.getM= essage(), e); + } + return new BDFLoader(in).loadFont(); + } + + private Font loadFont() throws IOException { + loadFontInfo(); + while (loadChar()) { + /* Loop. */ + } + + font.setMaxCharWidth(maxCharWidth); + font.setMaxCharHeight(maxCharHeight); + return font; + } + + private void loadFontInfo() throws IOException { + final Pattern stringSettingPattern =3D Pattern.compile("^(\\w+)\\s= +\"([^\"]+)\"$"); + String line; + // Load the global font information that appears before CHARS. + final int UNSET =3D Integer.MIN_VALUE; + font.setAscent(UNSET); + font.setDescent(UNSET); + font.setFamily("Unknown"); + font.setBold(false); + font.setItalic(false); + font.setPointSize(Font.UNKNOWN_POINT_SIZE); + do { + line =3D in.readLine(); + if (line =3D=3D null) + throw new IOException("BDF format error: end of file while= " + + "reading global font information"); + + StringTokenizer st =3D new StringTokenizer(line); + if (st.hasMoreTokens()) { + String name =3D st.nextToken(); + if (name.equals("FONT_ASCENT")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: " + + "no tokens after " + name); + font.setAscent(Integer.parseInt(st.nextToken())); + } else if (name.equals("FONT_DESCENT")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: " + + "no tokens after " + name); + font.setDescent(Integer.parseInt(st.nextToken())); + } else if (name.equals("POINT_SIZE")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: " + + "no tokens after " + name); + // Divide by 10, since it is stored X10. + font.setPointSize(Integer.parseInt(st.nextToken()) / 1= 0); + } else if (name.equals("FAMILY_NAME")) { + Matcher matcher =3D stringSettingPattern.matcher(line); + if (!matcher.matches()) + throw new IOException("BDF format error: " + + "line doesn't match string "= + + "setting pattern: " + line); + font.setFamily(matcher.group(2)); + } else if (name.equals("WEIGHT_NAME")) { + Matcher matcher =3D stringSettingPattern.matcher(line); + if (!matcher.matches()) + throw new IOException("BDF format error: " + + "line doesn't match string "= + + "setting pattern: " + line); + String weightName =3D matcher.group(2); + font.setBold("bold".equalsIgnoreCase(weightName)); + } else if (name.equals("SLANT")) { + Matcher matcher =3D stringSettingPattern.matcher(line); + if (!matcher.matches()) + throw new IOException("BDF format error: " + + "line doesn't match string "= + + "setting pattern: " + line); + String slantType =3D matcher.group(2); + font.setItalic(!"R".equalsIgnoreCase(slantType)); + } else if (name.equals("CHARS")) { + // This is the end of the global font information and + // the beginning of the character definitions. + break; + } else { + // Skip other fields. + } + } + } while (true); + + if (font.getAscent() =3D=3D UNSET) + throw new IOException("BDF format error: no FONT_ASCENT proper= ty"); + if (font.getDescent() =3D=3D UNSET) + throw new IOException("BDF format error: no FONT_DESCENT prope= rty"); + } + + private boolean loadChar() throws IOException { + String line; + // Find start of character + do { + line =3D in.readLine(); + if (line =3D=3D null) + return false; + StringTokenizer st =3D new StringTokenizer(line); + if (st.hasMoreTokens() && st.nextToken().equals("STARTCHAR")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: no character = name after STARTCHAR"); + break; + } + } while (true); + + // Find properties + final int UNSET =3D Integer.MIN_VALUE; + int codePoint =3D UNSET; + int bbx =3D UNSET; + int bby =3D UNSET; + int bbox =3D UNSET; + int bboy =3D UNSET; + int dwidth =3D UNSET; + do { + line =3D in.readLine(); + if (line =3D=3D null) + return false; + StringTokenizer st =3D new StringTokenizer(line); + if (st.hasMoreTokens()) { + String field =3D st.nextToken(); + if (field.equals("ENCODING")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: no encodi= ng # after ENCODING"); + String codePointStr =3D st.nextToken(); + codePoint =3D Integer.parseInt(codePointStr); + } else if (field.equals("BBX")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: no tokens= after BBX"); + bbx =3D Integer.parseInt(st.nextToken()); + bby =3D Integer.parseInt(st.nextToken()); + bbox =3D Integer.parseInt(st.nextToken()); + bboy =3D Integer.parseInt(st.nextToken()); + } else if (field.equals("DWIDTH")) { + if (!st.hasMoreTokens()) + throw new IOException("BDF format error: no tokens= after DWIDTH"); + dwidth =3D Integer.parseInt(st.nextToken()); + int dwidthY =3D Integer.parseInt(st.nextToken()); + // The DWIDTH Y value should be zero for any normal fo= nt. + if (dwidthY !=3D 0) { + throw new IOException("BDF format error: dwidth Y = value" + + "is nonzero (" + dwidthY + "= ) " + + "for char " + codePoint + ".= "); + } + } else if (field.equals("BITMAP")) { + break; // now read the bitmap + } + } + } while (true); + + if (codePoint =3D=3D UNSET) + throw new IOException("BDF format error: " + + "no code point set"); + if (bbx =3D=3D UNSET || bby =3D=3D UNSET) + throw new IOException("BDF format error: " + + "bbx/bby missing: " + bbx + ", " + bby + + " for char " + codePoint); + + if (bbox =3D=3D UNSET || bboy =3D=3D UNSET) + throw new IOException("BDF format error: " + + "bbox/bboy missing: " + bbox + ", " + bb= oy + + " for char " + codePoint); + + if (dwidth =3D=3D UNSET) + throw new IOException("BDF format error: " + + "dwidth missing for char " + codePoint); + + final int glyphWidth =3D bbx; + final int glyphHeight =3D bby; + if (glyphWidth > maxCharWidth) + maxCharWidth =3D glyphWidth; + if (glyphHeight > maxCharHeight) + maxCharHeight =3D glyphHeight; + + // Read the bitmap + Glyph glyph =3D new Glyph(codePoint, glyphWidth, glyphHeight, bbox= , bboy, dwidth); + for (int y =3D 0; y < glyphHeight; y++) { + line =3D in.readLine(); + if (line =3D=3D null) + return false; + for (int b =3D 0; b < line.length(); b++) { + int v =3D Integer.parseInt(Character.toString(line.charAt(= b)), 16); + for (int x =3D b * 4, i =3D 0; i < 4 && x < glyphWidth; x+= +, i++) { + boolean set =3D (v & 0x8) !=3D 0; + v <<=3D 1; + glyph.setPixel(x, y, set); + } + } + } + + font.putGlyph(codePoint, glyph); + return true; + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/CharDefs.java' --- util/fonttool/src/org/gnu/grub/fonttool/CharDefs.java 1970-01-01 00:00:= 00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/CharDefs.java 2008-09-01 16:28:= 19 +0000 @@ -0,0 +1,135 @@ +package org.gnu.grub.fonttool; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.TreeMap; + +class CharDefs { + private boolean debug =3D "1".equals(System.getProperty("fonttool.debu= g")); + private TreeMap glyphs; + private ByteArrayOutputStream charDefsData; + private int maxCharWidth; + private int maxCharHeight; + private HashMap charIndex; + + public CharDefs(Font font) { + this.glyphs =3D font.getGlyphs(); + this.charIndex =3D null; + this.charDefsData =3D null; + + calculateMaxSizes(); + } + + private void calculateMaxSizes() { + maxCharWidth =3D 0; + maxCharHeight =3D 0; + for (Glyph glyph : glyphs.values()) { + final int w =3D glyph.getWidth(); + final int h =3D glyph.getHeight(); + if (w > maxCharWidth) + maxCharWidth =3D w; + if (h > maxCharHeight) + maxCharHeight =3D h; + } + } + + void buildDefinitions() { + charIndex =3D new HashMap(); + HashMap charDefIndex =3D new HashMap= (); + charDefsData =3D new ByteArrayOutputStream(); + DataOutputStream charDefs =3D new DataOutputStream(charDefsData); + try { + // Loop through all the glyphs, writing the glyph data to the + // in-memory byte stream, collapsing duplicate glyphs, and + // constructing index information. + for (Glyph glyph : glyphs.values()) { + CharDef charDef =3D new CharDef(glyph.getWidth(), + glyph.getHeight(), + glyph.getBitmap()); + + if (charDefIndex.containsKey(charDef)) { + // Use already-written glyph. + if (debug) + System.out.printf("Duplicate glyph for character U= +%04X%n", + glyph.getCodePoint()); + final int charOffset =3D charDefIndex.get(charDef).int= Value(); + final CharStorageInfo info =3D + new CharStorageInfo(glyph.getCodePoint(), char= Offset); + charIndex.put(glyph.getCodePoint(), info); + } else { + // Write glyph data. + final int charOffset =3D charDefs.size(); + final CharStorageInfo info =3D + new CharStorageInfo(glyph.getCodePoint(), char= Offset); + charIndex.put(glyph.getCodePoint(), info); + + charDefIndex.put(charDef, (long) charOffset); + + charDefs.writeShort(glyph.getWidth()); + charDefs.writeShort(glyph.getHeight()); + charDefs.writeShort(glyph.getBbox()); + charDefs.writeShort(glyph.getBboy()); + charDefs.writeShort(glyph.getDeviceWidth()); + charDefs.write(glyph.getBitmap()); + } + } + } catch (IOException e) { + throw new RuntimeException("Error writing to in-memory byte st= ream", e); + } + } + + public int getMaxCharWidth() { + return maxCharWidth; + } + + public int getMaxCharHeight() { + return maxCharHeight; + } + + public HashMap getCharIndex() { + if (charIndex =3D=3D null) throw new IllegalStateException(); + return charIndex; + } + + public byte[] getDefinitionData() { + if (charDefsData =3D=3D null) throw new IllegalStateException(); + return charDefsData.toByteArray(); + } + + + private static class CharDef { + private final int width; + private final int height; + private final byte[] data; + + public CharDef(int width, int height, byte[] data) { + this.width =3D width; + this.height =3D height; + this.data =3D data; + } + + public boolean equals(Object o) { + if (this =3D=3D o) return true; + if (o =3D=3D null || getClass() !=3D o.getClass()) return fals= e; + + CharDef charDef =3D (CharDef) o; + + if (height !=3D charDef.height) return false; + if (width !=3D charDef.width) return false; + if (!Arrays.equals(data, charDef.data)) return false; + + return true; + } + + public int hashCode() { + int result; + result =3D width; + result =3D 31 * result + height; + result =3D 31 * result + Arrays.hashCode(data); + return result; + } + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/CharMatrix.ja= va' --- util/fonttool/src/org/gnu/grub/fonttool/CharMatrix.java 1970-01-01 00:0= 0:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/CharMatrix.java 2008-09-01 16:2= 8:19 +0000 @@ -0,0 +1,139 @@ +package org.gnu.grub.fonttool; + +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionAdapter; +import java.util.HashMap; +import java.util.Map; +import javax.swing.*; + +public class CharMatrix extends JComponent { + private Font charFont; + private int zoom =3D 1; + private int startingChar; + private Map glyphLocations =3D new HashMap(); + private GlyphSelectionListener glyphSelectionListener; + private Point mouseLocation; + + public interface GlyphSelectionListener { + void glyphSelected(int codePoint); + } + + public CharMatrix() { + addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + int codePoint =3D getCodePointForGlyphAt(e.getPoint()); + if (codePoint !=3D -1) { + if (glyphSelectionListener !=3D null) + glyphSelectionListener.glyphSelected(codePoint); + } + } + + public void mouseExited(MouseEvent e) { + mouseLocation =3D null; + repaint(); + } + }); + + addMouseMotionListener(new MouseMotionAdapter() { + public void mouseMoved(MouseEvent e) { + mouseLocation =3D e.getPoint(); + repaint(); + } + }); + } + + public void setGlyphSelectionListener(GlyphSelectionListener listener)= { + this.glyphSelectionListener =3D listener; + } + + public Font getCharFont() { + return charFont; + } + + public void setCharFont(Font font) { + this.charFont =3D font; + repaint(); + } + + public int getZoom() { + return zoom; + } + + public void setZoom(int zoom) { + this.zoom =3D zoom; + repaint(); + } + + public int getStartingChar() { + return startingChar; + } + + public void setStartingChar(int startingChar) { + this.startingChar =3D startingChar; + repaint(); + } + + public int getCodePointForGlyphAt(Point p) { + for (Rectangle rectangle : glyphLocations.keySet()) { + if (rectangle.contains(p)) + return glyphLocations.get(rectangle); + } + return -1; + } + + protected void paintComponent(Graphics g) { + glyphLocations.clear(); + + if (charFont =3D=3D null) + return; + + int w =3D getWidth(); + int h =3D getHeight(); + int maxCharWidth =3D charFont.getMaxCharWidth(); + int maxCharHeight =3D charFont.getMaxCharHeight(); + int space =3D 2; + int charsPerRow =3D w / (maxCharWidth + space); + int rows =3D h / (maxCharHeight + space); + + Rectangle highlightBounds =3D null; + int codePoint =3D startingChar; + int y =3D 3 + charFont.getAscent(); // Point y to the baseline. + for (int row =3D 0; row < rows; row++) { + int x =3D 3; + for (int col =3D 0; col < charsPerRow; col++) { + Glyph glyph =3D charFont.getGlyph(codePoint); + + if (glyph !=3D null) { + Rectangle charBounds =3D + CharView.drawGlyph(g, charFont, glyph, x, y, z= oom, getForeground()); + // Expand the bounding box for a bigger clickable area. + final int boundsPad =3D 1; + charBounds.x -=3D boundsPad; + charBounds.y -=3D boundsPad; + charBounds.width +=3D 2 * boundsPad; + charBounds.height +=3D 2 * boundsPad; + glyphLocations.put(charBounds, codePoint); + if (mouseLocation !=3D null && charBounds.contains(mou= seLocation)) { + highlightBounds =3D charBounds; + } + } + x +=3D maxCharWidth + space; + codePoint++; + } + + y +=3D maxCharHeight + space; + } + + if (highlightBounds !=3D null) { + g.setColor(Color.blue); + Graphics2D g2 =3D (Graphics2D) g; + g2.setStroke(new BasicStroke(3f, BasicStroke.CAP_SQUARE, Basic= Stroke.JOIN_MITER, 2f)); + g2.draw(new Rectangle(highlightBounds.x - 2, highlightBounds.y= - 2, + highlightBounds.width + 3, highlightBoun= ds.height + 3)); + g2.setStroke(new BasicStroke(1f)); + } + + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/CharStorageIn= fo.java' --- util/fonttool/src/org/gnu/grub/fonttool/CharStorageInfo.java 1970-01-01= 00:00:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/CharStorageInfo.java 2008-09-01= 16:28:19 +0000 @@ -0,0 +1,19 @@ +package org.gnu.grub.fonttool; + +class CharStorageInfo { + private final int codePoint; + private final int fileOffset; + + public CharStorageInfo(int codePoint, int fileOffset) { + this.codePoint =3D codePoint; + this.fileOffset =3D fileOffset; + } + + public int getCodePoint() { + return codePoint; + } + + public int getFileOffset() { + return fileOffset; + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/CharView.java' --- util/fonttool/src/org/gnu/grub/fonttool/CharView.java 1970-01-01 00:00:= 00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/CharView.java 2008-09-01 16:28:= 19 +0000 @@ -0,0 +1,101 @@ +package org.gnu.grub.fonttool; + +import java.awt.*; +import java.util.Formatter; +import javax.swing.*; + +public class CharView extends JComponent { + private Font charFont =3D null; + private Glyph glyph =3D null; + private int zoom =3D 1; + + public Font getCharFont() { + return charFont; + } + + public void setCharFont(Font charFont) { + this.charFont =3D charFont; + } + + public Glyph getGlyph() { + return glyph; + } + + public void setGlyph(Glyph glyph) { + this.glyph =3D glyph; + repaint(); + } + + public void setZoom(int zoom) { + this.zoom =3D zoom; + } + + public Dimension getPreferredSize() { + if (glyph =3D=3D null) + return new Dimension(1, 1); + return new Dimension(glyph.getWidth() * zoom, glyph.getHeight() * = zoom); + } + + protected void paintComponent(Graphics g) { + if (glyph =3D=3D null) { + // Draw a gray 'X' indicating no character selected. + g.setColor(Color.gray); + g.drawLine(0, 0, getWidth() - 1, getHeight() - 1); + g.drawLine(getWidth(), 0, 0, getHeight() - 1); + return; + } + + final int x0 =3D (getWidth() - glyph.getWidth() * zoom) / 2; + final int y0 =3D getHeight() / 2; + drawGlyph(g, charFont, glyph, x0, y0, zoom, getForeground()); + + g.setFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 12)); + FontMetrics fm =3D g.getFontMetrics(); + Formatter f =3D new Formatter(); + f.format("U+%04X", glyph.getCodePoint()); + String str =3D f.toString(); + g.drawString(str, + (getWidth() - fm.stringWidth(str)) / 2, + getHeight() - fm.getHeight()); + } + + /** + * Draw the specified glyph with its baseline at y0 and the horizontal + * start at x0. + */ + static Rectangle drawGlyph(Graphics g, Font font, Glyph glyph, int sta= rtX, int baselineY, int zoom, Color fg) { + // Adjust (x0, y0) so that it puts the bitmap at the right locatio= n. + final int offsetX =3D zoom * glyph.getBbox(); + final int offsetY =3D zoom * glyph.getBboy(); + final int bitmapLeft =3D startX + offsetX; + final int bitmapBottom =3D baselineY - offsetY; + final int bitmapTop =3D bitmapBottom - zoom * glyph.getHeight(); + + // Draw a gray box around the glyph bounds. + g.setColor(Color.lightGray); + g.drawRect(bitmapLeft - 1, bitmapTop - 1, + glyph.getWidth() * zoom + 1, zoom * glyph.getHeight() += 1); + + // Draw the baseline + g.setColor(new Color(110, 110, 255)); + g.drawLine(startX, baselineY, startX + zoom * glyph.getDeviceWidth= (), baselineY); + + // Draw the glyph. + g.setColor(fg); + byte[] bitmap =3D glyph.getBitmap(); + int bitIndex =3D 0; + for (int y =3D 0; y < glyph.getHeight(); y++) { + for (int x =3D 0; x < glyph.getWidth(); x++, bitIndex++) { + int byteIndex =3D bitIndex / 8; + int bitShift =3D bitIndex % 8; + byte b =3D bitmap[byteIndex]; + boolean bitValue =3D (b & (0x80 >>> bitShift)) !=3D 0; + if (bitValue) { + g.fillRect(bitmapLeft + x * zoom, bitmapTop + y * zoom= , zoom, zoom); + } + } + } + + return new Rectangle(bitmapLeft, bitmapTop, zoom * glyph.getWidth(= ), zoom * glyph.getHeight()); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/Converter.jav= a' --- util/fonttool/src/org/gnu/grub/fonttool/Converter.java 1970-01-01 00:00= :00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/Converter.java 2008-09-01 16:28= :19 +0000 @@ -0,0 +1,78 @@ +package org.gnu.grub.fonttool; + +import java.io.IOException; + +/** + * Program to convert BDF fonts into PFF2 fonts for use with GRUB. + */ +public class Converter { + public static void main(String[] args) { + if (args.length < 1) { + printUsageAndExit(); + } + + String in =3D null; + String out =3D null; + + try { + for (String arg : args) { + if (arg.startsWith("--")) { + String option; + String value; + int equalsPos =3D arg.indexOf('=3D'); + if (equalsPos < 0) { + option =3D arg.substring(2); + value =3D null; + } else { + option =3D arg.substring(2, equalsPos); + value =3D arg.substring(equalsPos + 1); + } + + if ("in".equals(option)) { + if (value =3D=3D null) + throw new CommandLineException(option + " opti= on requires a value."); + in =3D value; + } else if ("out".equals(option)) { + if (value =3D=3D null) + throw new CommandLineException(option + " opti= on requires a value."); + out =3D value; + } + } else { + throw new CommandLineException("Non-option argument `"= + arg + "'."); + } + } + if (in =3D=3D null || out =3D=3D null) { + throw new CommandLineException("Both --in=3DX and --out=3D= Y must be specified."); + } + } catch (CommandLineException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(1); + } + + try { + // Read BDF. + Font font =3D BDFLoader.loadFontFile(in); + + // Write PFF2. + new PFF2Writer(out).writeFont(font); + } catch (IOException e) { + System.err.println("I/O error converting font: " + e); + e.printStackTrace(); + System.exit(1); + } + } + + private static class CommandLineException extends Exception { + public CommandLineException(String message) { + super(message); + } + } + + private static void printUsageAndExit() { + System.err.println("GNU GRUB Font Conversion Tool"); + System.err.println(); + System.err.println("Usage: Converter --in=3DIN.bdf --out=3DOUT.pf2= "); + System.err.println(); + System.exit(1); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/FileFormatExc= eption.java' --- util/fonttool/src/org/gnu/grub/fonttool/FileFormatException.java 1970-0= 1-01 00:00:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/FileFormatException.java 2008-0= 9-01 16:28:19 +0000 @@ -0,0 +1,9 @@ +package org.gnu.grub.fonttool; + +import java.io.IOException; + +public class FileFormatException extends IOException { + public FileFormatException(String msg) { + super(msg); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/Font.java' --- util/fonttool/src/org/gnu/grub/fonttool/Font.java 1970-01-01 00:00:00 += 0000 +++ util/fonttool/src/org/gnu/grub/fonttool/Font.java 2008-09-01 16:28:19 += 0000 @@ -0,0 +1,106 @@ +package org.gnu.grub.fonttool; + +import java.util.TreeMap; + +public class Font { + public static final int UNKNOWN_POINT_SIZE =3D -1; + + private TreeMap glyphs; + private String family; + private boolean bold; + private boolean italic; + private int pointSize; + private int maxCharWidth; + private int maxCharHeight; + private int ascent; + private int descent; + + public Font() { + glyphs =3D new TreeMap(); + } + + public String getFamily() { + return family; + } + + public void setFamily(String family) { + this.family =3D family; + } + + public boolean isBold() { + return bold; + } + + public void setBold(boolean bold) { + this.bold =3D bold; + } + + public boolean isItalic() { + return italic; + } + + public void setItalic(boolean italic) { + this.italic =3D italic; + } + + public int getPointSize() { + return pointSize; + } + + public void setPointSize(int pointSize) { + this.pointSize =3D pointSize; + } + + public int getMaxCharWidth() { + return maxCharWidth; + } + + public void setMaxCharWidth(int maxCharWidth) { + this.maxCharWidth =3D maxCharWidth; + } + + public int getMaxCharHeight() { + return maxCharHeight; + } + + public void setMaxCharHeight(int maxCharHeight) { + this.maxCharHeight =3D maxCharHeight; + } + + public int getAscent() { + return ascent; + } + + public void setAscent(int ascent) { + this.ascent =3D ascent; + } + + public int getDescent() { + return descent; + } + + public void setDescent(int descent) { + this.descent =3D descent; + } + + public void putGlyph(int codePoint, Glyph glyph) { + glyphs.put(codePoint, glyph); + } + + public TreeMap getGlyphs() { + return glyphs; + } + + public Glyph getGlyph(int codePoint) { + return glyphs.get(codePoint); + } + + public String getStandardName() { + StringBuilder name =3D new StringBuilder(getFamily()); + if (isBold()) name.append(" Bold"); + if (isItalic()) name.append(" Italic"); + name.append(' '); + name.append(getPointSize()); + return name.toString(); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/Glyph.java' --- util/fonttool/src/org/gnu/grub/fonttool/Glyph.java 1970-01-01 00:00:00 = +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/Glyph.java 2008-09-01 16:28:19 = +0000 @@ -0,0 +1,83 @@ +package org.gnu.grub.fonttool; + +public class Glyph { + private final int codePoint; + private final int width; + private final int height; + + // These define the amounts to shift the character bitmap by + // before drawing it. See + // http://www.adobe.com/devnet/font/pdfs/5005.BDF_Spec.pdf + // and + // http://www.linuxbabble.com/documentation/x/bdf/ + // for explanatory figures. + private final int bbox; + private final int bboy; + + // Number of pixels to advance horizontally from this character's orig= in + // to the origin of the next character. + private final int deviceWidth; + + // Row-major order, no padding. Rows can break within a byte. + // MSb is first (leftmost/uppermost) pixel. + private final byte[] bitmap; + + public Glyph(int codePoint, int width, int height, int bbox, int bboy,= int deviceWidth) { + this(codePoint, width, height, bbox, bboy, deviceWidth, + new byte[(width * height + 7) / 8]); + } + + public Glyph(int codePoint, int width, int height, + int bbox, int bboy, int deviceWidth, + byte[] bitmap) { + this.codePoint =3D codePoint; + this.width =3D width; + this.height =3D height; + this.bboy =3D bboy; + this.bbox =3D bbox; + this.deviceWidth =3D deviceWidth; + this.bitmap =3D bitmap; + } + + public void setPixel(int x, int y, boolean value) { + if (x < 0 || y < 0 || x >=3D width || y >=3D height) + throw new IllegalArgumentException( + "Invalid pixel location (" + x + ", " + y + ") for " + + width + "x" + height + " glyph"); + + int bitIndex =3D y * width + x; + int byteIndex =3D bitIndex / 8; + int bitPos =3D bitIndex % 8; + int v =3D value ? 0x80 >>> bitPos : 0; + int mask =3D ~(0x80 >>> bitPos); + bitmap[byteIndex] =3D (byte) ((bitmap[byteIndex] & mask) | v); + } + + public int getCodePoint() { + return codePoint; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public int getBbox() { + return bbox; + } + + public int getBboy() { + return bboy; + } + + public int getDeviceWidth() { + return deviceWidth; + } + + public byte[] getBitmap() { + return bitmap; + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/PFF2Loader.ja= va' --- util/fonttool/src/org/gnu/grub/fonttool/PFF2Loader.java 1970-01-01 00:0= 0:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/PFF2Loader.java 2008-09-01 16:2= 8:19 +0000 @@ -0,0 +1,203 @@ +package org.gnu.grub.fonttool; + +import java.io.EOFException; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.charset.Charset; + +/** + * Load a PFF2 font completely into memory as an instance of Font. + */ +public class PFF2Loader { + private static final int debug + =3D Integer.parseInt(System.getProperty("fonttool.debug", "0")= ); + + private RandomAccessFile f; + private Font font; + + private PFF2Loader(String filename) throws FileNotFoundException { + f =3D new RandomAccessFile(filename, "r"); + font =3D new Font(); + } + + public static boolean isPFF2File(String filename) { + RandomAccessFile f =3D null; + boolean isFont =3D false; + try { + f =3D new RandomAccessFile(filename, "r"); + byte[] sectionNameBytes =3D new byte[4]; + f.readFully(sectionNameBytes); + String sectionName =3D new String(sectionNameBytes, "US-ASCII"= ); + if (sectionName.equals(PFF2Sections.FILE)) { + final String expectedType =3D "PFF2"; + int len =3D f.readInt(); + if (len =3D=3D expectedType.length()) { + byte[] typeBytes =3D new byte[len]; + f.readFully(typeBytes); + String type =3D new String(typeBytes, "US-ASCII"); + if (type.equals(expectedType)) { + isFont =3D true; + } + } + } + f.close(); + } catch (IOException e) { + if (f !=3D null) { + try { + f.close(); + } catch (IOException e1) { + // Ignore. + } + } + } + + return isFont; + } + + public static Font loadFontFile(String filename) throws IOException { + PFF2Loader loader =3D new PFF2Loader(filename); + loader.loadFont(); + return loader.getFont(); + } + + private void loadFont() throws IOException { + String s; + s =3D openSection(); + if (!PFF2Sections.FILE.equals(s)) + throw new FileFormatException( + "First PFF2 section must be FILE but is `" + s + "'."); + + s =3D readCompleteSectionAsString(); + if (!"PFF2".equals(s)) + throw new FileFormatException("Unsupported file type `" + s + = "'."); + + font.setFamily("Unnamed"); + font.setPointSize(Font.UNKNOWN_POINT_SIZE); + try { + if (debug >=3D 1) System.out.println("Loading font information= ..."); + boolean doneLoadingInfo =3D false; + while (!doneLoadingInfo) { + s =3D openSection(); + int sectionLength =3D f.readInt(); + if (debug >=3D 2) + System.out.println("Section: " + s + " length: " + sec= tionLength); + if (s.equals(PFF2Sections.MAX_CHAR_WIDTH)) { + int maxCharWidth =3D readSectionContentsAsUShort(secti= onLength); + font.setMaxCharWidth(maxCharWidth); + } else if (s.equals(PFF2Sections.MAX_CHAR_HEIGHT)) { + int maxCharHeight =3D readSectionContentsAsUShort(sect= ionLength); + font.setMaxCharHeight(maxCharHeight); + } else if (s.equals(PFF2Sections.FONT_POINT_SIZE)) { + int pointSize =3D readSectionContentsAsUShort(sectionL= ength); + font.setPointSize(pointSize); + } else if (s.equals(PFF2Sections.FONT_ASCENT)) { + int ascent =3D readSectionContentsAsUShort(sectionLeng= th); + font.setAscent(ascent); + } else if (s.equals(PFF2Sections.FONT_DESCENT)) { + int descent =3D readSectionContentsAsUShort(sectionLen= gth); + font.setDescent(descent); + } else if (s.equals(PFF2Sections.FONT_NAME)) { + String fontName =3D readSectionContentsAsString(sectio= nLength); + if (debug >=3D 1) + System.out.println("Loaded font: " + fontName); + } else if (s.equals(PFF2Sections.FONT_FAMILY)) { + font.setFamily(readSectionContentsAsString(sectionLeng= th)); + } else if (s.equals(PFF2Sections.FONT_WEIGHT)) { + String weightString =3D readSectionContentsAsString(se= ctionLength); + font.setBold(weightString.equalsIgnoreCase("bold")); + } else if (s.equals(PFF2Sections.FONT_SLANT)) { + String slantString =3D readSectionContentsAsString(sec= tionLength); + font.setItalic(slantString.equalsIgnoreCase("italic")); + } else if (s.equals(PFF2Sections.CHAR_INDEX)) { + loadCharsFromIndex(sectionLength); + } else if (s.equals(PFF2Sections.REMAINDER_IS_DATA)) { + doneLoadingInfo =3D true; + } else { + // If the section is not handled, then we just skip it. + if (f.skipBytes(sectionLength) !=3D sectionLength) { + System.err.println("Warning: Could not skip " + + sectionLength + + " bytes for section `" + s + = "'."); + } + } + } + } catch (EOFException eofException) { + // End of file; done reading font. + } + f.close(); + } + + private void loadCharsFromIndex(int sectionLength) throws IOException { + long indexSectionEnd =3D f.getFilePointer() + sectionLength; + try { + while (f.getFilePointer() < indexSectionEnd) { + int codePoint =3D f.readInt(); + int storageFlags =3D f.readUnsignedByte(); + int offset =3D f.readInt(); + + if ((storageFlags & 0x07) =3D=3D 0) { + Glyph g =3D loadUncompressedGlyph(codePoint, offset); + font.putGlyph(codePoint, g); + } + } + } catch (EOFException eofException) { + // End of file; simple return. + } + } + + private Glyph loadUncompressedGlyph(int codePoint, int fileOffset) thr= ows IOException { + if (debug >=3D 3) + System.out.printf("Loading uncompressed glyph U+%04X at %d%n",= codePoint, fileOffset); + + long savedPos =3D f.getFilePointer(); + + f.seek(fileOffset); + int width =3D f.readUnsignedShort(); + int height =3D f.readUnsignedShort(); + int offsetX =3D f.readShort(); + int offsetY =3D f.readShort(); + int deviceWidth =3D f.readShort(); + if (debug >=3D 3) System.out.println(" Size=3D" + width + "x" + h= eight + + " offset=3D(" + offsetX + ", = " + offsetY + + ") dWidth=3D" + deviceWidth); + byte[] bitmap =3D new byte[(width * height + 7) / 8]; + f.readFully(bitmap); + + f.seek(savedPos); + return new Glyph(codePoint, width, height, + offsetX, offsetY, deviceWidth, bitmap); + } + + private int readSectionContentsAsUShort(int sectionLength) throws IOEx= ception { + if (sectionLength !=3D 2) + throw new FileFormatException( + "For uint16 section, expected 2 bytes but length is " + + sectionLength + "."); + return f.readUnsignedShort(); + } + + private String openSection() throws IOException { + byte[] b =3D new byte[4]; + f.readFully(b); + return new String(b, Charset.forName("US-ASCII")); + } + + private String readCompleteSectionAsString() throws IOException { + int n =3D f.readInt(); + return readSectionContentsAsString(n); + } + + private String readSectionContentsAsString(int n) throws IOException { + StringBuilder buf =3D new StringBuilder(); + while (n-- !=3D 0) { + char c =3D (char) (f.readByte() & 0xFF); + buf.append(c); + } + return buf.toString(); + } + + public Font getFont() { + return font; + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/PFF2Sections.= java' --- util/fonttool/src/org/gnu/grub/fonttool/PFF2Sections.java 1970-01-01 00= :00:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/PFF2Sections.java 2008-09-01 16= :28:19 +0000 @@ -0,0 +1,19 @@ +package org.gnu.grub.fonttool; + +/** + * Section name constants for the PFF2 file format. + */ +public class PFF2Sections { + static final String FILE =3D "FILE"; + static final String FONT_NAME =3D "NAME"; + static final String FONT_FAMILY =3D "FAMI"; + static final String FONT_WEIGHT =3D "WEIG"; + static final String FONT_SLANT =3D "SLAN"; + static final String FONT_POINT_SIZE =3D "PTSZ"; + static final String MAX_CHAR_WIDTH =3D "MAXW"; + static final String MAX_CHAR_HEIGHT =3D "MAXH"; + static final String FONT_ASCENT =3D "ASCE"; + static final String FONT_DESCENT =3D "DESC"; + static final String CHAR_INDEX =3D "CHIX"; + static final String REMAINDER_IS_DATA =3D "DATA"; +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/PFF2Writer.ja= va' --- util/fonttool/src/org/gnu/grub/fonttool/PFF2Writer.java 1970-01-01 00:0= 0:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/PFF2Writer.java 2008-09-01 16:2= 8:19 +0000 @@ -0,0 +1,133 @@ +package org.gnu.grub.fonttool; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +// TODO Add DEFLATE compressed blocks of characters. +public class PFF2Writer { + private RandomAccessFile f; + private String currentSection; + private long currentSectionStart; + + public PFF2Writer(String filename) throws FileNotFoundException { + this.f =3D new RandomAccessFile(filename, "rw"); + this.currentSection =3D null; + this.currentSectionStart =3D -1; + } + + public void writeFont(Font font) throws IOException { + // Write file type ID header. + writeSection(PFF2Sections.FILE, "PFF2"); + + writeSection(PFF2Sections.FONT_NAME, font.getStandardName()); + writeSection(PFF2Sections.FONT_FAMILY, font.getFamily()); + writeSection(PFF2Sections.FONT_WEIGHT, font.isBold() ? "bold" : "n= ormal"); + writeSection(PFF2Sections.FONT_SLANT, font.isItalic() ? "italic" := "normal"); + if (font.getPointSize() !=3D Font.UNKNOWN_POINT_SIZE) + writeShortSection(PFF2Sections.FONT_POINT_SIZE, font.getPointS= ize()); + + // Construct character definitions. + CharDefs charDefs =3D new CharDefs(font); + charDefs.buildDefinitions(); + + // Write max character width and height metrics. + writeShortSection(PFF2Sections.MAX_CHAR_WIDTH, charDefs.getMaxChar= Width()); + writeShortSection(PFF2Sections.MAX_CHAR_HEIGHT, charDefs.getMaxCha= rHeight()); + writeShortSection(PFF2Sections.FONT_ASCENT, font.getAscent()); + writeShortSection(PFF2Sections.FONT_DESCENT, font.getDescent()); + + // Write character index with pointers to the character definition= s. + beginSection(PFF2Sections.CHAR_INDEX); + + // Determine the size of the index, so we can properly refer to the + // character definition offset in the index. The actual number of + // bytes written is compared to the calculated value to ensure we + // are correct. + final int indexStart =3D (int) f.getFilePointer(); + final int calculatedIndexLength =3D + font.getGlyphs().size() * (4 + 1 + 4); + final int charDefStart =3D indexStart + calculatedIndexLength + 8; + + for (CharStorageInfo storageInfo : charDefs.getCharIndex().values(= )) { + f.writeInt(storageInfo.getCodePoint()); + f.writeByte(0); // Storage flags: bits 1..0 =3D 00b : uncomp= ressed. + f.writeInt(charDefStart + storageInfo.getFileOffset()); + } + + final int indexEnd =3D (int) f.getFilePointer(); + if (indexEnd - indexStart !=3D calculatedIndexLength) { + throw new RuntimeException("Incorrect index length calculated,= calc=3D" + + calculatedIndexLength + + " actual=3D" + (indexEnd - indexS= tart)); + } + endSection(PFF2Sections.CHAR_INDEX); + + f.writeBytes(PFF2Sections.REMAINDER_IS_DATA); + f.writeInt(-1); // Data takes up the rest of the file. + f.write(charDefs.getDefinitionData()); + + f.close(); + } + + private void beginSection(String sectionName) throws IOException { + verifyOkToBeginSection(sectionName); + + f.writeBytes(sectionName); + f.writeInt(-1); // Placeholder for the section length. + currentSection =3D sectionName; + currentSectionStart =3D f.getFilePointer(); + } + + private void endSection(String sectionName) throws IOException { + verifyOkToEndSection(sectionName); + + long sectionEnd =3D f.getFilePointer(); + long sectionLength =3D sectionEnd - currentSectionStart; + f.seek(currentSectionStart - 4); + f.writeInt((int) sectionLength); + f.seek(sectionEnd); + currentSection =3D null; + currentSectionStart =3D -1; + } + + private void verifyOkToBeginSection(String sectionName) { + if (sectionName.length() !=3D 4) + throw new IllegalArgumentException( + "Section names must be 4 characters: `" + sectionName = + "'."); + if (currentSection !=3D null) + throw new IllegalStateException( + "Attempt to start `" + sectionName + + "' section before ending the previous section `" + + currentSection + "'."); + } + + private void verifyOkToEndSection(String sectionName) { + if (sectionName.length() !=3D 4) + throw new IllegalStateException("Invalid section name '" + sec= tionName + + "'; must be 4 characters."); + if (currentSection =3D=3D null) + throw new IllegalStateException( + "Attempt to end section `" + sectionName + + "' when no section active."); + if (!sectionName.equals(currentSection)) + throw new IllegalStateException( + "Attempt to end `" + sectionName + + "' section during active section `" + + currentSection + "'."); + } + + private void writeSection(String sectionName, String contents) throws = IOException { + verifyOkToBeginSection(sectionName); + f.writeBytes(sectionName); + f.writeInt(contents.length()); + f.writeBytes(contents); + } + + private void writeShortSection(String sectionName, int value) throws I= OException { + verifyOkToBeginSection(sectionName); + f.writeBytes(sectionName); + f.writeInt(2); + f.writeShort(value); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/Viewer.java' --- util/fonttool/src/org/gnu/grub/fonttool/Viewer.java 1970-01-01 00:00:00= +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/Viewer.java 2008-09-01 16:28:19= +0000 @@ -0,0 +1,135 @@ +package org.gnu.grub.fonttool; + +import java.io.File; +import java.io.IOException; +import javax.swing.*; + +/** + * Program to view fonts. + *

+ * Supports PFF2 and BDF format font files. + */ +public class Viewer { + public static void main(String[] args) { + if (args.length < 1) { + printUsageAndExit(); + } + + boolean verbose =3D false; + String format =3D null; + String filename =3D null; + + try { + for (String arg : args) { + if (arg.startsWith("--")) { + String option; + String value; + int equalsPos =3D arg.indexOf('=3D'); + if (equalsPos < 0) { + option =3D arg.substring(2); + value =3D null; + } else { + option =3D arg.substring(2, equalsPos); + value =3D arg.substring(equalsPos + 1); + } + + if ("format".equals(option)) { + if (value =3D=3D null) + throw new CommandLineException(option + " opti= on requires a value."); + format =3D value; + } else if ("verbose".equals(option)) { + verbose =3D true; + } + } else { + if (filename =3D=3D null) { + filename =3D arg; + } else { + throw new CommandLineException( + "Extra argument after filename: `" + arg += "'."); + } + } + } + if (filename =3D=3D null) { + throw new CommandLineException("A filename must be specifi= ed."); + } + if (!(format =3D=3D null || "bdf".equals(format) || "pff2".equ= als(format))) { + throw new CommandLineException("Invalid format: must be `b= df' or `pff2'."); + } + } catch (CommandLineException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(1); + } + + if (!new File(filename).exists()) { + System.err.println("Error: File `" + filename + "' not found."= ); + System.exit(1); + } + + if (format =3D=3D null) { + // Autodetect the format. + if (PFF2Loader.isPFF2File(filename)) + format =3D "pff2"; + else if (BDFLoader.isBDFFile(filename)) + format =3D "bdf"; + else { + System.err.println("Error: Unable to detect font file form= at " + + "for `" + filename + "'."); + System.exit(1); + } + if (verbose) + System.out.println("Detected file format `" + format + "'.= "); + } + + Font font =3D null; + try { + if ("bdf".equals(format)) { + if (verbose) + System.out.println("Loading BDF font."); + font =3D BDFLoader.loadFontFile(filename); + } else if ("pff2".equals(format)) { + if (verbose) + System.out.println("Loading PFF2 font."); + font =3D PFF2Loader.loadFontFile(filename); + } else { + System.err.println("Error: Bad format `" + format + "'"); + System.exit(1); + } + } catch (IOException e) { + System.err.println("I/O error loading font: " + e); + e.printStackTrace(); + System.exit(1); + } + + if (font =3D=3D null) { + System.err.println("Error: Font not loaded."); + System.exit(1); + } + + if (verbose) { + System.out.println("Loaded font: '" + font.getStandardName() += "'"); + } + + ViewerFrame f =3D new ViewerFrame(font); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setLocationRelativeTo(null); // Center the frame on screen. + f.setVisible(true); + } + + private static class CommandLineException extends Exception { + public CommandLineException(String message) { + super(message); + } + } + + private static void printUsageAndExit() { + System.err.println("GNU GRUB Font Viewer"); + System.err.println(); + System.err.println("Usage: Viewer [--format=3DFORMAT] FILE"); + System.err.println(" --format=3Dbdf The font is in BDF format= ."); + System.err.println(" --format=3Dpff2 The font is in PFF2 forma= t."); + System.err.println(" If no --format option is given, the type wil= l be"); + System.err.println(" automatically detected."); + System.err.println(); + System.exit(1); + } +} =3D=3D=3D added file 'util/fonttool/src/org/gnu/grub/fonttool/ViewerFrame.j= ava' --- util/fonttool/src/org/gnu/grub/fonttool/ViewerFrame.java 1970-01-01 00:= 00:00 +0000 +++ util/fonttool/src/org/gnu/grub/fonttool/ViewerFrame.java 2008-09-01 16:= 28:19 +0000 @@ -0,0 +1,61 @@ +package org.gnu.grub.fonttool; + +import java.awt.*; +import javax.swing.*; +import javax.swing.border.BevelBorder; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +public class ViewerFrame extends JFrame { + private final Font font; + + private JLabel startingCodePointLabel; + private JSlider startingCodePointSlider; + private CharMatrix charMatrix; + private CharView charView; + private JLabel statusBar; + + public ViewerFrame(Font font) { + this.font =3D font; + + createComponents(); + buildUI(); + setSize(800, 600); + } + + private void createComponents() { + startingCodePointLabel =3D new JLabel("Starting Code Point"); + startingCodePointSlider =3D new JSlider(JSlider.HORIZONTAL, 0, 655= 35, 0); + startingCodePointSlider.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + charMatrix.setStartingChar(startingCodePointSlider.getValu= e()); + } + }); + charMatrix =3D new CharMatrix(); + charMatrix.setCharFont(font); + charMatrix.setGlyphSelectionListener(new CharMatrix.GlyphSelection= Listener() { + public void glyphSelected(int codePoint) { + charView.setGlyph(font.getGlyph(codePoint)); + } + }); + charView =3D new CharView(); + charView.setZoom(3); + charView.setBorder(new BevelBorder(BevelBorder.RAISED)); + statusBar =3D new JLabel(); + } + + private void buildUI() { + JComponent topPanel =3D Box.createVerticalBox(); + topPanel.add(startingCodePointLabel); + topPanel.add(startingCodePointSlider); + + JComponent charPanel =3D new JPanel(new GridLayout(2, 1)); + charPanel.add(charMatrix); + charPanel.add(charView); + + setLayout(new BorderLayout()); + add(topPanel, BorderLayout.NORTH); + add(charPanel, BorderLayout.CENTER); + add(statusBar, BorderLayout.SOUTH); + } +} =3D=3D=3D added directory 'util/fonttool/test' =3D=3D=3D added directory 'util/fonttool/test/org' =3D=3D=3D added directory 'util/fonttool/test/org/gnu' =3D=3D=3D added directory 'util/fonttool/test/org/gnu/grub' =3D=3D=3D added directory 'util/fonttool/test/org/gnu/grub/fonttool' =3D=3D=3D added file 'util/fonttool/test/org/gnu/grub/fonttool/TestGlyph.ja= va' --- util/fonttool/test/org/gnu/grub/fonttool/TestGlyph.java 1970-01-01 00:0= 0:00 +0000 +++ util/fonttool/test/org/gnu/grub/fonttool/TestGlyph.java 2008-09-01 16:2= 8:19 +0000 @@ -0,0 +1,110 @@ +package org.gnu.grub.fonttool; + +import org.junit.Assert; +import org.junit.Test; + +public class TestGlyph { + @Test + public void testConstructor3x5() { + Glyph g =3D new Glyph(65, 3, 5, 0, 0, 0); + + // Should be empty upon construction. + assertBitmapEquals("..." + + "..." + + "..." + + "..." + + "...", + g.getBitmap()); + + // Should have ceil(3 * 5 / 8) =3D ceil(15 / 8) =3D ceil(1 + 7/8) = =3D 2 bytes. + Assert.assertEquals("Wrong bitmap array size.", + 2, g.getBitmap().length); + } + + @Test + public void testConstructor5x5() { + Glyph g =3D new Glyph(65, 5, 5, 0, 0, 0); + + // Should be empty upon construction. + assertBitmapEquals("....." + + "....." + + "....." + + "....." + + ".....", + g.getBitmap()); + + // Should have ceil(5 * 5 / 8) =3D ceil(25 / 8) =3D ceil(3 + 1/8) = =3D 4 bytes. + Assert.assertEquals("Wrong bitmap array size.", + 4, g.getBitmap().length); + } + + @Test + public void testSetPixel() { + Glyph g =3D new Glyph(65, 3, 5, 0, 0, 0); + + g.setPixel(0, 0, true); + Assert.assertEquals(0x80, g.getBitmap()[0] & 0xFF); + Assert.assertEquals(0x00, g.getBitmap()[1] & 0xFF); + assertBitmapEquals("X.." + + "..." + + "..." + + "..." + + "...", + g.getBitmap()); + + g.setPixel(2, 1, true); + Assert.assertEquals(0x84, g.getBitmap()[0] & 0xFF); + Assert.assertEquals(0x00, g.getBitmap()[1] & 0xFF); + assertBitmapEquals("X.." + + "..X" + + "..." + + "..." + + "...", + g.getBitmap()); + } + + static void assertBitmapEquals(String expectedBitmap, byte[] actualBit= map) { + Assert.assertNotNull("expected bitmap null", expectedBitmap); + Assert.assertNotNull("actual bitmap null", actualBitmap); + StringBuilder errors =3D new StringBuilder(); + + int bit =3D 7; + int byteIndex =3D -1; + for (int bitIndex =3D 0; bitIndex < expectedBitmap.length(); bitIn= dex++) { + char c =3D expectedBitmap.charAt(bitIndex); + boolean expectedBit; + if (c =3D=3D '.') { + expectedBit =3D false; + } else if (c =3D=3D 'X') { + expectedBit =3D true; + } else { + throw new IllegalArgumentException("Bad character '" + c += "' in expected bitmap string"); + } + + byteIndex =3D bitIndex / 8; + byte b =3D actualBitmap[byteIndex]; + boolean actualBit =3D (b & (1 << bit)) !=3D 0; + if (actualBit !=3D expectedBit) { + errors.append("[").append(bitIndex) + .append("] wrong (it is ") + .append(actualBit ? "1" : "0").append("). "); + } + + if (bit =3D=3D 0) { + bit =3D 7; + } else { + bit--; + } + } + + if (byteIndex !=3D expectedBitmap.length() / 8) { + errors.append("Last byte index wrong, was ") + .append(byteIndex).append(" but expected ") + .append(expectedBitmap.length() - 1); + } + + if (errors.length() !=3D 0) { + Assert.fail("Bitmap wrong: " + errors); + } + } +} --MP_/e20.QTiG69Wd3+O3WFdOY/m-- --Sig_/Qkknjr50BpdPPVnYwpqDG6a Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAki8JUAACgkQokx8fzcGbYf7ywCeKgJv7nboRMQr1JV6APOrqdUP rbUAnjRK5zPFWQSlqgGOSm9hRUw06ZcN =7SHo -----END PGP SIGNATURE----- --Sig_/Qkknjr50BpdPPVnYwpqDG6a--