Esempio n. 1
0
  /**
   * Render based on immediate rendering
   *
   * @param text The text to be rendered
   * @param start The index of the first character in the string to render
   * @param end The index of the last character in the string to render
   */
  private void render(String text, int start, int end) {
    GL.glBegin(SGL.GL_QUADS);

    int x = 0, y = 0;
    CharDef lastCharDef = null;
    char[] data = text.toCharArray();
    for (int i = 0; i < data.length; i++) {
      int id = data[i];
      if (id == '\n') {
        x = 0;
        y += getLineHeight();
        continue;
      }
      if (id >= chars.length) {
        continue;
      }
      CharDef charDef = chars[id];
      if (charDef == null) {
        continue;
      }

      if (lastCharDef != null) x += lastCharDef.getKerning(id);
      lastCharDef = charDef;

      if ((i >= start) && (i <= end)) {
        charDef.draw(x, y);
      }

      x += charDef.xadvance;
    }
    GL.glEnd();
  }
Esempio n. 2
0
  private CharDef parseChar(final String line) throws Exception {
    CharDef def = new CharDef();
    StringTokenizer tokens = new StringTokenizer(line, " =");

    tokens.nextToken();
    tokens.nextToken();
    def.id = Short.parseShort(tokens.nextToken());
    if (def.id < 0) {
      return null;
    }
    if (def.id > DEFAULT_MAX_CHAR) {
      throw new Exception(def.id + " > " + DEFAULT_MAX_CHAR);
    }

    tokens.nextToken();
    def.tx = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.ty = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.width = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.height = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.xoffset = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.yoffset = Short.parseShort(tokens.nextToken());
    tokens.nextToken();
    def.advance = Short.parseShort(tokens.nextToken());

    if (def.id != (short) ' ') {
      lineHeight = MathUtils.max(def.height + def.yoffset, lineHeight);
    }

    return def;
  }
Esempio n. 3
0
  public int getWidth(String text) {
    if (text == null) {
      return 0;
    }
    Display display = null;
    for (Display d : displays.values()) {
      if (d != null && text.equals(d.text)) {
        display = d;
        break;
      }
    }
    if (display != null && display.width != 0) {
      return display.width;
    }
    if (display == null) {
      display = new Display();
    }
    int width = 0;
    CharDef lastCharDef = null;
    for (int i = 0, n = text.length(); i < n; i++) {
      int id = text.charAt(i);
      if (id == '\n') {
        width = 0;
        continue;
      }
      if (id >= chars.length) {
        continue;
      }
      CharDef charDef = chars[id];
      if (charDef == null) {
        continue;
      }
      if (lastCharDef != null) {
        width += lastCharDef.getKerning(id);
      }
      lastCharDef = charDef;
      if (i < n - 1) {
        width += charDef.advance;
      } else {
        width += charDef.width;
      }
      display.width = MathUtils.max(display.width, width);
    }

    return display.width;
  }
Esempio n. 4
0
  /**
   * Parse a single character line from the definition
   *
   * @param line The line to be parsed
   * @return The character definition from the line
   * @throws SlickException Indicates a given character is not valid in an angel code font
   */
  private CharDef parseChar(String line) throws SlickException {
    CharDef def = new CharDef();
    StringTokenizer tokens = new StringTokenizer(line, " =");

    tokens.nextToken(); // char
    tokens.nextToken(); // id
    def.id = Short.parseShort(tokens.nextToken()); // id value
    if (def.id < 0) {
      return null;
    }
    if (def.id > MAX_CHAR) {
      throw new SlickException(
          "Invalid character '"
              + def.id
              + "': AngelCodeFont does not support characters above "
              + MAX_CHAR);
    }

    tokens.nextToken(); // x
    def.x = Short.parseShort(tokens.nextToken()); // x value
    tokens.nextToken(); // y
    def.y = Short.parseShort(tokens.nextToken()); // y value
    tokens.nextToken(); // width
    def.width = Short.parseShort(tokens.nextToken()); // width value
    tokens.nextToken(); // height
    def.height = Short.parseShort(tokens.nextToken()); // height value
    tokens.nextToken(); // x offset
    def.xoffset = Short.parseShort(tokens.nextToken()); // xoffset value
    tokens.nextToken(); // y offset
    def.yoffset = Short.parseShort(tokens.nextToken()); // yoffset value
    tokens.nextToken(); // xadvance
    def.xadvance = Short.parseShort(tokens.nextToken()); // xadvance

    def.init();

    if (def.id != ' ') {
      lineHeight = Math.max(def.height + def.yoffset, lineHeight);
    }

    return def;
  }
Esempio n. 5
0
  /** @see org.newdawn.slick.Font#getWidth(java.lang.String) */
  public int getWidth(String text) {
    DisplayList displayList = null;
    if (displayListCaching) {
      displayList = (DisplayList) displayLists.get(text);
      if (displayList != null && displayList.width != null) return displayList.width.intValue();
    }

    int maxWidth = 0;
    int width = 0;
    CharDef lastCharDef = null;
    for (int i = 0, n = text.length(); i < n; i++) {
      int id = text.charAt(i);
      if (id == '\n') {
        width = 0;
        continue;
      }
      if (id >= chars.length) {
        continue;
      }
      CharDef charDef = chars[id];
      if (charDef == null) {
        continue;
      }

      if (lastCharDef != null) width += lastCharDef.getKerning(id);
      lastCharDef = charDef;

      if (i < n - 1) {
        width += charDef.xadvance;
      } else {
        width += charDef.width;
      }
      maxWidth = Math.max(maxWidth, width);
    }

    if (displayList != null) displayList.width = new Short((short) maxWidth);

    return maxWidth;
  }
Esempio n. 6
0
  private void drawBatchString(
      float tx, float ty, String text, LColor c, int startIndex, int endIndex) {

    if (isClose) {
      return;
    }

    if (displays.size > DEFAULT_MAX_CHAR) {
      displays.clear();
    }

    lazyHashCode = 1;

    if (c != null) {
      lazyHashCode = LSystem.unite(lazyHashCode, c.r);
      lazyHashCode = LSystem.unite(lazyHashCode, c.g);
      lazyHashCode = LSystem.unite(lazyHashCode, c.b);
      lazyHashCode = LSystem.unite(lazyHashCode, c.a);
    }

    String key = text + lazyHashCode;

    Display display = displays.get(key);

    if (display == null) {

      int x = 0, y = 0;

      displayList.glBegin();
      displayList.setBatchPos(tx, ty);

      if (c != null) {
        displayList.setImageColor(c);
      }

      CharDef lastCharDef = null;
      char[] data = text.toCharArray();
      for (int i = 0; i < data.length; i++) {
        int id = data[i];
        if (id == '\n') {
          x = 0;
          y += getLineHeight();
          continue;
        }
        if (id >= chars.length) {
          continue;
        }
        CharDef charDef = chars[id];
        if (charDef == null) {
          continue;
        }

        if (lastCharDef != null) {
          x += lastCharDef.getKerning(id);
        }
        lastCharDef = charDef;

        if ((i >= startIndex) && (i <= endIndex)) {
          charDef.draw(x, y);
        }

        x += charDef.advance;
      }

      if (c != null) {
        displayList.setImageColor(LColor.white);
      }

      displayList.glEnd();

      display = new Display();

      display.cache = displayList.newBatchCache();
      display.text = text;
      display.width = 0;
      display.height = 0;

      displays.put(key, display);

    } else if (display.cache != null) {
      display.cache.x = tx;
      display.cache.y = ty;
      displayList.postCache(display.cache);
    }
  }