Exemplo n.º 1
0
  /**
   * Parse the font definition file
   *
   * @param fntFile The stream from which the font file can be read
   * @throws SlickException
   */
  private void parseFnt(InputStream fntFile) throws SlickException {
    if (displayListCaching) {
      baseDisplayListID = GL.glGenLists(DISPLAY_LIST_CACHE_SIZE);
      if (baseDisplayListID == 0) displayListCaching = false;
    }

    try {
      // now parse the font file
      BufferedReader in = new BufferedReader(new InputStreamReader(fntFile));
      String info = in.readLine();
      String common = in.readLine();
      String page = in.readLine();

      Map kerning = new HashMap(64);
      List charDefs = new ArrayList(MAX_CHAR);
      int maxChar = 0;
      boolean done = false;
      while (!done) {
        String line = in.readLine();
        if (line == null) {
          done = true;
        } else {
          if (line.startsWith("chars c")) {
            // ignore
          } else if (line.startsWith("char")) {
            CharDef def = parseChar(line);
            if (def != null) {
              maxChar = Math.max(maxChar, def.id);
              charDefs.add(def);
            }
          }
          if (line.startsWith("kernings c")) {
            // ignore
          } else if (line.startsWith("kerning")) {
            StringTokenizer tokens = new StringTokenizer(line, " =");
            tokens.nextToken(); // kerning
            tokens.nextToken(); // first
            short first = Short.parseShort(tokens.nextToken()); // first value
            tokens.nextToken(); // second
            int second = Integer.parseInt(tokens.nextToken()); // second value
            tokens.nextToken(); // offset
            int offset = Integer.parseInt(tokens.nextToken()); // offset value
            List values = (List) kerning.get(new Short(first));
            if (values == null) {
              values = new ArrayList();
              kerning.put(new Short(first), values);
            }
            // Pack the character and kerning offset into a short.
            values.add(new Short((short) ((offset << 8) | second)));
          }
        }
      }

      chars = new CharDef[maxChar + 1];
      for (Iterator iter = charDefs.iterator(); iter.hasNext(); ) {
        CharDef def = (CharDef) iter.next();
        chars[def.id] = def;
      }

      // Turn each list of kerning values into a short[] and set on the chardef.
      for (Iterator iter = kerning.entrySet().iterator(); iter.hasNext(); ) {
        Entry entry = (Entry) iter.next();
        short first = ((Short) entry.getKey()).shortValue();
        List valueList = (List) entry.getValue();
        short[] valueArray = new short[valueList.size()];
        int i = 0;
        for (Iterator valueIter = valueList.iterator(); valueIter.hasNext(); i++)
          valueArray[i] = ((Short) valueIter.next()).shortValue();
        chars[first].kerning = valueArray;
      }
    } catch (IOException e) {
      Log.error(e);
      throw new SlickException("Failed to parse font file: " + fntFile);
    }
  }