Beispiel #1
0
  /**
   * Check's FontInfo alias database, if not Font.getFont(name) is returned;
   *
   * @param name
   * @return either java's default font or font from font database.
   */
  public static Font createFont(String name) {
    FontInfo info = FontInfo.getFontInfo(name);

    if (info != null) return info.createFont();

    return Font.getFont(name);
  }
Beispiel #2
0
 public static int calcCharWidth(char c, FontInfo fontInfo) {
   if (c == ' ' || c == '\t') {
     return fontInfo.getSpaceSize();
   }
   CharInfo charInfo = resolveCharInfo(c, fontInfo);
   if (charInfo == null) {
     return fontInfo.getSpaceSize();
   }
   return charInfo.getWidth();
 }
Beispiel #3
0
  public static int calcTextWidth(
      String value, int ptr, FontInfo fontInfo, boolean splitWords, int maxWidth) {
    int width = 0;
    int lastNonSpace = 0;
    int lastWordEnd = 0;

    char c;
    while (ptr < value.length()) {
      c = value.charAt(ptr++);
      if (width > 0) {
        width += fontInfo.getCharSpace();
      } else if (isWhitespace(c)) {
        continue;
      }

      if (isNewLine(c)) {
        break;
      }

      width += calcCharWidth(c, fontInfo);

      if (width > maxWidth) {
        width = (splitWords || lastWordEnd == 0) ? lastNonSpace : lastWordEnd;
        break;
      }

      if (isWhitespace(c)) {
        lastWordEnd = lastNonSpace;
      } else {
        lastNonSpace = width;
      }
    }
    return width > WatchConstants.SCREEN_WIDTH ? WatchConstants.SCREEN_WIDTH : width;
  }
Beispiel #4
0
  public static CharInfo resolveCharInfo(char c, FontInfo fontInfo) {
    if ((c < fontInfo.getMinChar()) || (c > fontInfo.getMaxChar())) {
      return null;
    }

    CharInfoLookup lookupTable = null;
    for (CharInfoLookup lookup : fontInfo.getCharInfoLookup()) {
      if (c >= lookup.getMinChar() && c <= lookup.getMaxChar()) {
        lookupTable = lookup;
      }
    }
    if (lookupTable == null) {
      return null;
    }
    int charIndex = c - lookupTable.getMinChar();
    return lookupTable.getCharTable()[charIndex];
  }
Beispiel #5
0
 /**
  * Show the cached object.
  *
  * @param info the font info
  * @param character the encoded character
  */
 public CharWidth showCachedCharacter(
     FontInfo info, AffineTransform ctm, Point2D curpt, String character) {
   this.character = character;
   CacheEntry key = new CacheEntry(info, character);
   CacheEntry val = (CacheEntry) cache.get(key);
   if (val == null) return null;
   ctm.translate(curpt.getX(), curpt.getY());
   AffineTransform ftm = info.getFontMatrix();
   ctm.concatenate(ftm);
   show(val.getObject(), ctm);
   return val.getCharWidth().transform(ftm);
 }
Beispiel #6
0
  public static String calcTextToDraw(
      String text,
      int ptr,
      int startX,
      int startY,
      int width,
      int height,
      int fontType,
      int fontAlignment) {
    FontInfo fontInfo = FontUtils.resolveFont(fontType);
    int initPtr = ptr;
    if (text != null) {
      int prevPtr = ptr;
      int x = startX;
      int y = startY;
      if (width == 0) {
        width = WatchConstants.SCREEN_WIDTH - x;
      }
      if (height == 0) {
        height = WatchConstants.SCREEN_HEIGHT - y;
      }
      boolean multiline = (fontAlignment & WatchConstants.TEXT_FLAGS_MULTILINE) != 0;
      boolean splitWord = (fontAlignment & WatchConstants.TEXT_FLAGS_SPLIT_WORD) != 0;

      int maxY = y + height;
      boolean lastLine;
      do {
        lastLine = !multiline || (y + 2 * fontInfo.getHeight() + fontInfo.getCharSpace() > maxY);

        int textWidth =
            FontUtils.calcTextWidth(text, ptr, fontInfo, splitWord || !multiline, width);

        if ((fontAlignment & WatchConstants.HORIZONTAL_ALIGN_CENTER) != 0) {
          x += (width - textWidth) / 2;
        } else if ((fontAlignment & WatchConstants.HORIZONTAL_ALIGN_RIGHT) != 0) {
          x += (width - textWidth);
        }
        int maxX = x + textWidth;

        boolean firstChar = true;
        char c = 0;
        while (ptr < text.length()) {
          c = text.charAt(ptr++);
          if (firstChar && FontUtils.isWhitespace(c)) {
            continue;
          }
          firstChar = false;
          int charWidth = FontUtils.calcCharWidth(c, fontInfo);
          if (x + charWidth > maxX) {
            // overflow
            ptr = prevPtr;
            break;
          }

          x += charWidth;
          x += fontInfo.getCharSpace();
          prevPtr = ptr;
        }
        if (ptr == text.length()) {
          lastLine = true;
        }

        x = startX;
        y += fontInfo.getHeight() + (c == 11 ? fontInfo.getHeight() / 2 : fontInfo.getCharSpace());
      } while (!lastLine);
      return text.substring(initPtr, ptr);
    }
    return "";
  }
Beispiel #7
0
 /** Get FontInfo from Font Info data base. */
 public static FontInfo getFontInfo(String name) {
   return FontInfo.getFontInfo(name);
 }
Beispiel #8
0
 /** Create Font Info from Font, copying font attributes like style and name. */
 public static FontInfo createFontInfo(Font font) {
   FontInfo info = new FontInfo();
   info.init(font);
   return info;
 }