Android Open Source - DivisionByZero Font Manager






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggstudios.divisionbyzero;
//from   ww w  .ja  v a2  s  .c o  m
import com.ggstudios.utils.BitmapUtils;
import com.ggstudios.utils.DebugLog;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class FontManager {
  // number of possible characters
  private static final String TAG = "FontManager";

  private static final char START_CHAR = ' ', END_CHAR = '~' + 1;
  private static final int CHAR_COUNT = END_CHAR - START_CHAR;

  // this should be a PoT
  private static final int MAX_TEXTURE_WIDTH = 512;

  int cellW = 0;
  int cellH = 0;

  float[] charWidth = new float[END_CHAR];
  TextureRegion[] charRegion = new TextureRegion[END_CHAR];

  private int textureHeight;
  int textureHandle;

  private Paint textPaint;

  // indicates whether a font has been specified yet
  private boolean hasFont = false;

  public FontManager() {
  }

  public void generateFont(float size){
    textPaint = new Paint();
    textPaint.setTextSize(size);
    textPaint.setAntiAlias(true);
    textPaint.setARGB(255, 255, 255, 255);

    hasFont = true;

    refresh();
  }

  public void generateFont(Paint painter){
    textPaint = painter;

    hasFont = true;

    refresh();
  }

  public int breakText(String input, int w){
    return textPaint.breakText(input, true, w, null);
  }

  public int getWidth(String input){
    return (int) (textPaint.measureText(input) + 0.5f);
  }
  
  public float getHeight() {
    return cellH;
  }
  
  public float getFontSize() {
    return textPaint.getTextSize();
  }

  public void refresh(){
    DebugLog.d(TAG, "refreshing...");
    if(!hasFont){ 
      DebugLog.d(TAG, "No font loaded... aborting...");
      return;
    }
    
    cellW = (int) textPaint.getFontSpacing();
    cellH = textPaint.getFontMetricsInt().descent - textPaint.getFontMetricsInt().ascent;

    int cols = MAX_TEXTURE_WIDTH / cellW;
    int rows = (int)Math.ceil(CHAR_COUNT / (float)cols);

    textureHeight = Utils.findSmallestPot(rows * cellH);

    String strTemp = "";
    Bitmap bitmap = Bitmap.createBitmap(MAX_TEXTURE_WIDTH, textureHeight, Bitmap.Config.ARGB_8888);
    bitmap.eraseColor(Color.TRANSPARENT);
    Canvas canvas = new Canvas(bitmap);

    float x = 0f;
    float y = textPaint.getTextSize() - textPaint.descent();

    for(int i = START_CHAR; i < END_CHAR; i++){
      // convert our character to string
      strTemp = Character.toString((char) (i));

      canvas.drawText(strTemp, x, y, textPaint);

      x += cellW;
      if((x + cellW) > MAX_TEXTURE_WIDTH) {
        x = 0f;
        y += cellH;
      }
    }

    textureHandle = BitmapUtils.loadBitmap(bitmap);

    char[] everyLetter = new char[END_CHAR];
    for(int i = 0; i < everyLetter.length; i++){
      everyLetter[i] = (char) i;
    }

    textPaint.getTextWidths(everyLetter, 0, END_CHAR, charWidth);

    // setup the array of character texture regions
    x = 0;                                          // Initialize X
    y = 0;                                          // Initialize Y
    for ( int c = START_CHAR; c < END_CHAR; c++ )  {         // FOR Each Character (On Texture)
      charRegion[c] = new TextureRegion( MAX_TEXTURE_WIDTH, textureHeight, x, y, cellW - 2, cellH - 2);  // Create Region for Character
      x += cellW;                              // Move to Next Char (Cell)
      if ( x + cellW > MAX_TEXTURE_WIDTH )  {
        x = 0;                                    // Reset X Position to Start
        y += cellH;                          // Move to Next Row (Cell)
      }
    }

    DebugLog.d(TAG, "Texture size:" + MAX_TEXTURE_WIDTH + ", " + textureHeight);
    DebugLog.d(TAG, "cellw:" +cellW + "cellh:" +cellH);
  }

  static class TextureRegion {

    //--Members--//
    public float u1, v1;                               // Top/Left U,V Coordinates
    public float u2, v2;                               // Bottom/Right U,V Coordinates

    private float texW, texH, w, h;
    
    /**
     * Calculate U,V coordinates from specified texture coordinates
     * @param texWidth  Width of the texture atlas
     * @param texHeight Height of the texture atlas
     * @param x      The x coordinate of the region
     * @param y      The y coordinate of the region
     * @param width    The width of the region
     * @param height  The height of the region
     */
    public TextureRegion(float texWidth, float texHeight, float x, float y, float width, float height)  {
      this.u1 = x / texWidth;                     // Calculate U1
      this.v1 = y / texHeight;                    // Calculate V1
      this.u2 = ( (x + width) / texWidth );       // Calculate U2
      this.v2 = ( (y + height) / texHeight );     // Calculate V2
      
      texW = texWidth;
      texH = texHeight;
      w = width;
      h = height;
    }
    
    public TextureRegion(float texWidth, float texHeight, float width, float height) {
      texW = texWidth;
      texH = texHeight;
      w = width;
      h = height;
    }

    public void setRegion(float x, float y) {
      this.u1 = x / texW;                     // Calculate U1
      this.v1 = y / texH;                    // Calculate V1
      this.u2 = ( (x + w) / texW );       // Calculate U2
      this.v2 = ( (y + h) / texH );     // Calculate V2
    }
  }
  
  public int getUnderlyingTextureWidth() {
    return MAX_TEXTURE_WIDTH;
  }
  
  public int getUnderlyingTextureHeight() {
    return textureHeight;
  }
}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java