import { assert } from '../auxiliaries' ; import { GLfloat2 , GLfloat4 , GLsizei2 } from '../tuples' ; import { Context } from '../context' ; import { Texture2D } from '../texture2d' ; import { Wizard } from '../wizard' ; import { fetchAsync } from '../fetch' ; import { FontFaceLoader } from './fontfaceloader' ; import { Glyph } from './glyph' ; /**
 * Font related data for glyph based text rendering. The glyph-based font face is described by, e.g., font-size,
 * line spacing, a glyph catalogue, as well as kerning information. The glyph catalogue is based on a set of glyphs
 * referring to a texture atlas (@see  Glyph ). All measures are provided in float even though most
 * glyph-textures and associated font data is encoded via integer values. A font face explicitly relies on floating
 * values to reduce the need of casting as well as to simplify the use for dpi aware text rendering.
 * The font face interface is designed to access most basic font settings ascent, descent, and line gap (leading).
 * Additional font settings are provided via interface but are derived from or mapped to the above mentioned three
 * settings, e.g., font size is the sum of descent and ascent. This is to provide as much convenience measures for
 * type setting/font rendering as possible.
 * Note: This class does not provide dpi awareness, which has to be handled outside of this class, e.g., during
 * layouting and rendering.
 *
 * A font can be loaded as follows:
 * ```
 * let fontFace: FontFace | undefined;
 * FontFace.fromFile(context, './data/opensansr144.fnt').then((value) => fontFace = value);
 * ```
  export class FontFace { see size protected _size : number ; see base protected _base : number ; see ascent protected _ascent = 0.0 ; see descent protected _descent = 0.0 ; see lineGap protected _lineGap = 0.0 ; see glyphTextureExtent protected _glyphTextureExtent : GLfloat2 = [ 0.0 , 0.0 ] ; see glyphTexturePadding protected _glyphTexturePadding : GLfloat4 = [ 0.0 , 0.0 , 0.0 , 0.0 ] ; see glyphTexturePadding protected _glyphTexture : Texture2D ; see glyph ,  see hasGlyph ,  see addGlyph protected _glyphs = new Map < GLsizei , Glyph > ( ) ; protected _context : Context ; param url param context param headless param identifier static fromFile ( url , context , headless = false , identifier ? ) { const transform = ( data ) : PromiseLike < FontFace > => { const font = new FontFace ( context , identifier ) ; return FontFaceLoader . process ( font , data , url , undefined , headless ) . then ( ( fontFace ) => fontFace ) . catch ( ( reason ) => Promise . reject ( `processing font face data failed: ${ reason }` ) ) ; } ; return fetchAsync < FontFace > ( url , 'text' ) . then ( transform ) ; } static fromFiles ( fontFileUrl , pageFileUrlsByPageID , context , headless = false , identifier ? ) { const transform = ( data ) : PromiseLike < FontFace > => { const font = new FontFace ( context , identifier ) ; return FontFaceLoader . process ( font , data , fontFileUrl , pageFileUrlsByPageID , headless ) . then ( ( fontFace ) => fontFace ) . catch ( ( reason ) => Promise . reject ( `processing font face data failed: ${ reason }` ) ) ; } ; return fetchAsync < FontFace > ( fontFileUrl , 'text' ) . then ( transform ) ; } /**
     * Constructs an unconfigured, empty font face specification. The appropriate setters should be used for
     * configuring the font face. Alternatively, the font importer (@see  FontImporter ) provides the import
     * of bitmap-font base configuration file ( http ).
     *  param context param identifier constructor ( context , identifier ? ) { this . _context = context ; const gl = context . gl ; identifier = identifier !== undefined && identifier !== `` ? identifier : this . constructor . name ; this . _glyphTexture = new Texture2D ( context , `${ identifier }GlyphAtlas` ) ; const internalFormat = Wizard . queryInternalTextureFormat ( context , gl . RGBA , Wizard . Precision . byte ) ; this . _glyphTexture . initialize ( 1 , 1 , internalFormat [ 0 ] , gl . RGBA , internalFormat [ 1 ] ) ; this . _glyphTexture . filter ( gl . LINEAR , gl . LINEAR ) ; } param index returns hasGlyph ( index ) { return ! ! this . _glyphs . get ( index ) ; } param index returns glyph ( index ) { const existingGlyph = this . _glyphs . get ( index ) ; if ( existingGlyph ) { return existingGlyph ; } const glyph = new Glyph ( ) ; glyph . index = index ; return glyph ; } param glyph addGlyph ( glyph ) { assert ( ! ( this . _glyphs . get ( glyph . index ) ) , 'expected glyph to not already exist' ) ; this . _glyphs . set ( glyph . index , glyph ) ; } returns arrayOfGlyphIndices ( ) { return Array . from ( this . _glyphs . keys ( ) ) ; } param index returns depictable ( index ) { return this . glyph ( index ) . depictable ( ) ; } param index param subsequentIndex returns kerning ( index , subsequentIndex ) { const glyph = this . _glyphs . get ( index ) ; if ( ! glyph ) { return 0.0 ; } return glyph . kerning ( subsequentIndex ) ; } param index param subsequentIndex param kerning setKerning ( index , subsequentIndex , kerning ) { const glyph = this . _glyphs . get ( index ) ; if ( ! glyph || ! this . hasGlyph ( subsequentIndex ) ) { assert ( false , `expected glyph or glyph of subsequent index to exist, \
                given ${ index } and ${ subsequentIndex } respectively` ) ; return ; } glyph . setKerning ( subsequentIndex , kerning ) ; } returns set size ( size ) { assert ( size > 0.0 , `expected size to be greater than 0.0, given ${ size }` ) ; this . _size = size ; } get size ( ) : number { return this . _size ; } param base set base ( base ) { assert ( base > 0.0 , `expected base to be greater than 0.0, given ${ base }` ) ; this . _base = base ; } get base ( ) : number { return this . _base ; } param ascent set ascent ( ascent ) { assert ( ascent > 0.0 , `expected ascent to be greater than 0.0, given ${ ascent }` ) ; this . _ascent = ascent ; } get ascent ( ) : number { return this . _ascent ; } param descent set descent ( descent ) { this . _descent = descent ; } get descent ( ) : number { return this . _descent ; } param lineGap set lineGap ( lineGap ) { this . _lineGap = lineGap ; } get lineGap ( ) : number { return this . _lineGap ; } param lineHeight set lineHeight ( lineHeight ) { assert ( this . size > 0 , `expected size to be greater than zero to derive line gap from line height` ) ; this . _lineGap = lineHeight - this . size ; } get lineHeight ( ) : number { return this . size + this . lineGap ; } param lineSpace set lineSpace ( lineSpace ) { this . _lineGap = this . size * ( lineSpace - 1 ) ; } returns get lineSpace ( ) : number { if ( this . lineHeight === 0.0 ) { return this . lineHeight ; } return this . size / this . lineHeight ; } param extent set glyphTextureExtent ( extent ) { assert ( extent [ 0 ] > 0 , `expected extent.x to be greater than 0.0, given ${ extent [ 0 ] }` ) ; assert ( extent [ 1 ] > 0 , `expected extent.y to be greater than 0.0, given ${ extent [ 1 ] }` ) ; this . _glyphTextureExtent = extent ; } returns get glyphTextureExtent ( ) : GLsizei2 { return this . _glyphTextureExtent ; } param padding set glyphTexturePadding ( padding ) { assert ( padding [ 0 ] >= 0.0 , `expected padding[0] to be greater than 0.0, given ${ padding [ 0 ] }` ) ; assert ( padding [ 1 ] >= 0.0 , `expected padding[1] to be greater than 0.0, given ${ padding [ 1 ] }` ) ; assert ( padding [ 2 ] >= 0.0 , `expected padding[2] to be greater than 0.0, given ${ padding [ 2 ] }` ) ; assert ( padding [ 3 ] >= 0.0 , `expected padding[3] to be greater than 0.0, given ${ padding [ 3 ] }` ) ; this . _glyphTexturePadding = padding ; } get glyphTexturePadding ( ) : GLfloat4 { return this . _glyphTexturePadding ; } param texture set glyphTexture ( texture ) { this . _glyphTexture = texture ; } get glyphTexture ( ) : Texture2D { return this . _glyphTexture ; } }