AWT. Font

Font contains properties and provides methods to manage fonts

Constructor

new Font(familyopt, sizeopt, boldopt, italicopt, variantopt)

Font constructor

Parameters:
NameTypeAttributesDefaultDescription
familystring<optional>
'Arial'
sizenumber<optional>
17
boldnumber<optional>
0
italicnumber<optional>
0
variantstring<optional>
''

Members

bold :number

The font bold value

Type:
  • number

family :string

The font-family property

Type:
  • string

italic :number

The font italic value

Type:
  • number

size :number

The font size
Warning: Do not change size directly. Use setSize() instead.

Type:
  • number

variant :string

The font variant value

Type:
  • string

(static) ALREADY_CALCULATED_FONTS

Array of font objects with already calculated heights

(static) ALREADY_LOADED_FONTS

Array of font names already loaded from Google Fonts, or generic names provided by browsers by default
See: https://developer.mozilla.org/en-US/docs/Web/CSS/font-family

(static) GOOGLEFONTS

Google Fonts currently used in substitutions

(static) SUBSTITUTIONS

Google Fonts equivalent for special fonts used in some JClic projects.
More substitutions can be added to the list for specific projects indicating a
fontSubstitutions object in the data-options attribute of the HTML div element
containing the player.
For example:
<div class ="JClic" data-project="demo.jclic" data-options='{"fontSubstitutions":{"arial":"Arimo"}}'/>

Methods

_calcHeight() → {module:AWT.Font}

The TextMetrics object used
by CanvasRenderingContext2D
does not provide a heigth value for rendered text.
This stackoverflow question
has an excellent response by Daniel Earwicker explaining how to measure the
vertical dimension of rendered text using a span element.
The code has been slighty adapted to deal with Font objects.

Warning: Do not call this method direcly. Use getHeight() or getMetrics() instead

Returns:
Type: 
module:AWT.Font

cssFont() → {string}

Gets the codification of this font in a single string, suitable to be used in a font
CSS attribute.

Returns:
  • A string with all the CSS font properties concatenated
Type: 
string

equals(font) → {boolean}

Checks if two Font objects are equivalent

Parameters:
NameTypeDescription
fontmodule:AWT.Font

The Font object to compare against this one

Returns:
  • true if both objects are equivalent, false otherwise
Type: 
boolean

getAttributes() → {object}

Gets a object with the basic attributes needed to rebuild this instance excluding functions,
parent references, constants and also attributes retaining the default value.
The resulting object is commonly usued to serialize elements in JSON format.

Returns:
  • The resulting object, with minimal attrributes
Type: 
object

getHeight() → {number}

Calculates the font metrics and returns its height

Returns:
  • The font height
Type: 
number

getMetrics() → {Object}

Calculates the font metrics

Returns:
  • The font metrics
Type: 
Object

setAttributes(data) → {module:AWT.Font}

Reads the properties of this Font from a data object

Parameters:
NameTypeDescription
dataobject

The data object to be parsed

Returns:
Type: 
module:AWT.Font

setProperties($xml) → {module:AWT.Font}

Reads the properties of this Font from an XML element

Parameters:
NameTypeDescription
$xmlexternal:jQuery

The xml element to be parsed

Returns:
Type: 
module:AWT.Font

setSize(size) → {module:AWT.Font}

Allows to change the size member, recalculating the vertical metrics.

Parameters:
NameTypeDescription
sizenumber

The new size to set

Returns:
Type: 
module:AWT.Font

toCss(css) → {object}

Translates the Font properties into CSS statements

Parameters:
NameTypeDescription
cssobject

The object where to add CSS properties. When null or undefined, a new
object will be created and returned.

Returns:
Type: 
object

zoom(amount) → {module:AWT.Font}

Increases or decreases the current font size by the specified amount

Parameters:
NameTypeDescription
amountnumber

The amount to increase or decrease current size

Returns:
Type: 
module:AWT.Font

(static) checkTree($tree, optionsopt)

Finds the XML elements with typeface specifications, checks its value against the font
substitution list, replacing the family attribute and loading the alternative font when needed.

Parameters:
NameTypeAttributesDescription
$treeexternal:jQuery

The xml element to be processed

optionsobject<optional>

Optional param that can contain a fontSubstitutions attribute with
a substition table to be added to SUBSTITUTIONS

(static) loadGoogleFont(name)

Try to load a specific font from [http://www.google.com/fonts]

Parameters:
NameTypeDescription
namestring

The font family name

(static) loadGoogleFonts(fonts)

Try to load a set of Google fonts

Parameters:
NameTypeDescription
fontsArray.<string>

An array of font names