org.gwtwidgets.client.ui.canvas.impl
Class BitmapFontImpl

java.lang.Object
  extended by org.gwtwidgets.client.ui.canvas.Font
      extended by org.gwtwidgets.client.ui.canvas.impl.BitmapFontImpl
All Implemented Interfaces:
com.google.gwt.http.client.RequestCallback
Direct Known Subclasses:
CanvasBackedBitmapFontImpl

public class BitmapFontImpl
extends Font
implements com.google.gwt.http.client.RequestCallback

A BitmapFontImpl object encapsulates a font and knows how to draw text on a canvas. This base implementation renders bitmap fonts read from an image and a font descriptor.

The font is usually not usable right after instantiation because asynchronous requests are made towards the server in order to retrieve font specific information. If required, load listeners can be attached to the font object in order to poll its load status.

BitmapFontImpl objects are expensive to create and maintain as two requests are made to the server for the font bitmap and the font descriptor, the entire bitmap is stored in the browser's cache and a costly parsing of the font descriptor is performed.

Author:
George Georgovassilis g.georgovassilis[at]gmail.com

Field Summary
protected  com.google.gwt.user.client.Element bitmap
           
protected  int blockHeight
           
protected  java.util.Map characters
           
protected  boolean descriptionLoaded
           
protected  boolean imageLoaded
           
protected  FontLoadListener listener
           
 
Constructor Summary
BitmapFontImpl(java.lang.String bitmapPath, java.lang.String descriptionPath, FontLoadListener listener)
          Constructs a canvas font and loads required resources from the server.
 
Method Summary
protected  FontLoadListener doWithListener(FontLoadListener listener)
          Allows substitution of a listener in the constructor of extending classes
protected  void drawGlyph(char c, Canvas canvas, double charOffsetLeft, double charOffsetTop, double charWidth, double blockHeight, double destinationX, double destinationY, double destinationWidth, double destinationHeight)
          Draws a character.
 void drawText(java.lang.String text, Canvas canvas, double x, double y)
           
 void onError(com.google.gwt.http.client.Request request, java.lang.Throwable exception)
           
 void onResponseReceived(com.google.gwt.http.client.Request request, com.google.gwt.http.client.Response response)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

characters

protected java.util.Map characters

blockHeight

protected int blockHeight

imageLoaded

protected boolean imageLoaded

descriptionLoaded

protected boolean descriptionLoaded

listener

protected FontLoadListener listener

bitmap

protected com.google.gwt.user.client.Element bitmap
Constructor Detail

BitmapFontImpl

public BitmapFontImpl(java.lang.String bitmapPath,
                      java.lang.String descriptionPath,
                      FontLoadListener listener)
Constructs a canvas font and loads required resources from the server. This method will issue two HTTP requests to the server:

Parameters:
bitmapPath - URL to the bitmap containing the font's graphical representation.
descriptionPath - URL to the font's descriptor.
listener - Optional listener to execute when the font is ready to use. Can be null.
token - Token to be passed to the listener
Method Detail

doWithListener

protected FontLoadListener doWithListener(FontLoadListener listener)
Allows substitution of a listener in the constructor of extending classes

Parameters:
listener -
Returns:

drawGlyph

protected void drawGlyph(char c,
                         Canvas canvas,
                         double charOffsetLeft,
                         double charOffsetTop,
                         double charWidth,
                         double blockHeight,
                         double destinationX,
                         double destinationY,
                         double destinationWidth,
                         double destinationHeight)
Draws a character. Invoked by drawText(String, Canvas, double, double). Can be overridden by extending implementations.

Parameters:
c - Character to draw
canvas - Canvas to draw on
charOffsetLeft - Horizontal character offset on the template bitmap
charOffsetTop - Vertical character offset on the template bitmap
charWidth - Glyph width
blockHeight - Glyph height
destinationX - Destination X on canvas
destinationY - Destination Y on canvas
destinationWidth - Width of character to draw
destinationHeight - Height of character to draw

drawText

public void drawText(java.lang.String text,
                     Canvas canvas,
                     double x,
                     double y)
Specified by:
drawText in class Font

onError

public void onError(com.google.gwt.http.client.Request request,
                    java.lang.Throwable exception)
Specified by:
onError in interface com.google.gwt.http.client.RequestCallback

onResponseReceived

public void onResponseReceived(com.google.gwt.http.client.Request request,
                               com.google.gwt.http.client.Response response)
Specified by:
onResponseReceived in interface com.google.gwt.http.client.RequestCallback


Copyright © 2009. All Rights Reserved.