Home | History | Annotate | Download | only in font
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.font;
     34 
     35 import com.jme3.export.*;
     36 import com.jme3.util.IntMap;
     37 import com.jme3.util.IntMap.Entry;
     38 import java.io.IOException;
     39 
     40 /**
     41  * Represents a single bitmap character.
     42  */
     43 public class BitmapCharacter implements Savable, Cloneable {
     44     private char c;
     45     private int x;
     46     private int y;
     47     private int width;
     48     private int height;
     49     private int xOffset;
     50     private int yOffset;
     51     private int xAdvance;
     52     private IntMap<Integer> kerning = new IntMap<Integer>();
     53     private int page;
     54 
     55     public BitmapCharacter() {}
     56 
     57     public BitmapCharacter(char c) {
     58         this.c = c;
     59     }
     60 
     61     @Override
     62     public BitmapCharacter clone() {
     63         try {
     64             BitmapCharacter result = (BitmapCharacter) super.clone();
     65             result.kerning = kerning.clone();
     66             return result;
     67         } catch (CloneNotSupportedException ex) {
     68             throw new AssertionError();
     69         }
     70     }
     71 
     72     public int getX() {
     73         return x;
     74     }
     75 
     76     public void setX(int x) {
     77         this.x = x;
     78     }
     79 
     80     public int getY() {
     81         return y;
     82     }
     83 
     84     public void setY(int y) {
     85         this.y = y;
     86     }
     87 
     88     public int getWidth() {
     89         return width;
     90     }
     91 
     92     public void setWidth(int width) {
     93         this.width = width;
     94     }
     95 
     96     public int getHeight() {
     97         return height;
     98     }
     99 
    100     public void setHeight(int height) {
    101         this.height = height;
    102     }
    103 
    104     public int getXOffset() {
    105         return xOffset;
    106     }
    107 
    108     public void setXOffset(int offset) {
    109         xOffset = offset;
    110     }
    111 
    112     public int getYOffset() {
    113         return yOffset;
    114     }
    115 
    116     public void setYOffset(int offset) {
    117         yOffset = offset;
    118     }
    119 
    120     public int getXAdvance() {
    121         return xAdvance;
    122     }
    123 
    124     public void setXAdvance(int advance) {
    125         xAdvance = advance;
    126     }
    127 
    128     public void setPage(int page) {
    129         this.page = page;
    130     }
    131 
    132     public int getPage() {
    133         return page;
    134     }
    135 
    136     public char getChar() {
    137         return c;
    138     }
    139 
    140     public void setChar(char c) {
    141         this.c = c;
    142     }
    143 
    144     public void addKerning(int second, int amount){
    145         kerning.put(second, amount);
    146     }
    147 
    148     public int getKerning(int second){
    149         Integer i = kerning.get(second);
    150         if (i == null)
    151             return 0;
    152         else
    153             return i.intValue();
    154     }
    155 
    156     public void write(JmeExporter ex) throws IOException {
    157         OutputCapsule oc = ex.getCapsule(this);
    158         oc.write(c, "c", 0);
    159         oc.write(x, "x", 0);
    160         oc.write(y, "y", 0);
    161         oc.write(width, "width", 0);
    162         oc.write(height, "height", 0);
    163         oc.write(xOffset, "xOffset", 0);
    164         oc.write(yOffset, "yOffset", 0);
    165         oc.write(xAdvance, "xAdvance", 0);
    166 
    167         int[] seconds = new int[kerning.size()];
    168         int[] amounts = new int[seconds.length];
    169 
    170         int i = 0;
    171         for (Entry<Integer> entry : kerning){
    172             seconds[i] = entry.getKey();
    173             amounts[i] = entry.getValue();
    174             i++;
    175         }
    176 
    177         oc.write(seconds, "seconds", null);
    178         oc.write(amounts, "amounts", null);
    179     }
    180 
    181     public void read(JmeImporter im) throws IOException {
    182         InputCapsule ic = im.getCapsule(this);
    183         c = (char) ic.readInt("c", 0);
    184         x = ic.readInt("x", 0);
    185         y = ic.readInt("y", 0);
    186         width = ic.readInt("width", 0);
    187         height = ic.readInt("height", 0);
    188         xOffset = ic.readInt("xOffset", 0);
    189         yOffset = ic.readInt("yOffset", 0);
    190         xAdvance = ic.readInt("xAdvance", 0);
    191 
    192         int[] seconds = ic.readIntArray("seconds", null);
    193         int[] amounts = ic.readIntArray("amounts", null);
    194 
    195         for (int i = 0; i < seconds.length; i++){
    196             kerning.put(seconds[i], amounts[i]);
    197         }
    198     }
    199 }