From dcf600a5c1b4810a76370e33761c137eb22f5492 Mon Sep 17 00:00:00 2001 From: Brian Stell Date: Mon, 22 Jun 2015 22:14:24 -0700 Subject: [PATCH] Add code to read the closure data. --- .../googlei18n/tachyfont/GetCharData.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/run_time/src/gae_java/TachyFont/src/com/github/googlei18n/tachyfont/GetCharData.java b/run_time/src/gae_java/TachyFont/src/com/github/googlei18n/tachyfont/GetCharData.java index b7abd865..457b240e 100644 --- a/run_time/src/gae_java/TachyFont/src/com/github/googlei18n/tachyfont/GetCharData.java +++ b/run_time/src/gae_java/TachyFont/src/com/github/googlei18n/tachyfont/GetCharData.java @@ -1,12 +1,16 @@ package com.github.googlei18n.tachyfont; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -22,8 +26,19 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc JarFile jarFile = new JarFile("WEB-INF/" + jarFilename); Map cmapMap = getCmapMap(jarFile); Iterator> cmapIterator = cmapMap.entrySet().iterator(); + resp.getWriter().println("code to gid:"); while (cmapIterator.hasNext()) { Entry pair = cmapIterator.next(); + resp.getWriter().println(" " + pair.getKey() + " = " + pair.getValue()); + } + + Map> closureMap = getClosureMap(jarFile); + Iterator>> closureMapIterator = + closureMap.entrySet().iterator(); + resp.getWriter().println("gid to closure:"); + while (closureMapIterator.hasNext()) { + Entry> pair = closureMapIterator.next(); + // TODO(bstell): this is not yet debugged. resp.getWriter().println(pair.getKey() + " = " + pair.getValue()); } @@ -34,7 +49,8 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc // TODO(bstell): create and return the glyph bundle. jarFile.close(); } - private HashMap getCmapMap(JarFile jarFile) throws IOException { + + private Map getCmapMap(JarFile jarFile) throws IOException { JarEntry codePointsJarEntry = jarFile.getJarEntry("codepoints"); InputStream codePointsStream = jarFile.getInputStream(codePointsJarEntry); DataInputStream codePointsDataStream = new DataInputStream(codePointsStream); @@ -51,4 +67,46 @@ private HashMap getCmapMap(JarFile jarFile) throws IOException } return cmapMap; } + + private Map> getClosureMap(JarFile jarFile) throws IOException { + JarEntry closureIndexJarEntry = jarFile.getJarEntry("closure_idx"); + InputStream closureIndexStream = jarFile.getInputStream(closureIndexJarEntry); + DataInputStream closureIndexDataStream = new DataInputStream(closureIndexStream); + + JarEntry closureDataJarEntry = jarFile.getJarEntry("closure_data"); + InputStream closureDataStream = jarFile.getInputStream(closureDataJarEntry); + int closureDataSize = closureDataStream.available(); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] buffer = new byte[closureDataSize]; + int readLength; + while ((readLength = closureDataStream.read(buffer, 0, closureDataSize)) != -1) + byteArrayOutputStream.write(buffer, 0, readLength); + buffer = byteArrayOutputStream.toByteArray(); + + ByteArrayInputStream closureDataByteArrayInputStream = + new ByteArrayInputStream(buffer); + DataInputStream closureDataInputStream = + new DataInputStream(closureDataByteArrayInputStream); + HashMap> closureMap = new HashMap>(); + Integer gid = -1; + while (closureIndexDataStream.available() > 0) { + gid++; + Integer offset = closureIndexDataStream.readInt(); + Integer size = closureIndexDataStream.readUnsignedShort(); + if (size == 0) { + continue; + } + // TODO(bstell): the following is not yet debugged. + Set closureGids = new HashSet(); + closureDataByteArrayInputStream.reset(); + closureDataByteArrayInputStream.skip(offset); + while (size > 0) { + Integer closureGid = closureDataInputStream.readUnsignedShort(); + closureGids.add(closureGid); + size -= 2; + } + closureMap.put(gid, closureGids); + } + return closureMap; + } }