Interface FontLoaderInterface

The FontLoaderInterface is used when displaying annotations.

The underlying optimized rendering algorithms of annotations must have access to glyph data and metrics, but these information are not accessible through the native javascript api. For these reasons, the api must be able to parse font files and retrieve such data.

There may be some specific fonts needed to display your annotations. Just register them to the system with registerFont. You may register fonts from their url, or from their data with an ArrayBuffer. Font loading takes place when annotations are parsed and included in the AnnotationRendererInterface. Even if loadFont is provided, the font loading mechanism will automatically take place when required.

The api provides a fixed size default font (included in its script, nothing needs to be done to get this default font), that supports usual special characters of annotations, the 3djuump Infinite default font.

If a font is not found in the FontLoaderInterface, then the default font is used. And if the default font could not be loaded, then the 3djuump Infinite default font is used. You may override the default font when registering fonts. There can be only one default font. The default font is changed through calling registerFont or setDefaultFont accordingly. If such a font is not valid, then the api will use the 3djuump Infinite default font.

Family names in the FontLoaderInterface are case insensitive, all family names will be used lowercase.

For memory reasons, there is only one FontLoaderInterface, and thus FontLoaderInterface(s) cannot be created, but is accessed through InfiniteApiControllerInterface.getFontLoader.

You may choose to load specific fonts beforehand, in that case when all the fonts are loaded, a FontLoaderInterfaceSignal signal is sent when the loading is over.

/** 
* Sample to illustrate the loading of fonts.
*/
import { FontLoaderInterface, InfiniteFactory, FontLoaderInterfaceSignal, InfiniteEvent, FontLoadingStatus, FontInterface } from 'generated_files/documentation/appinfiniteapi';

// font loading is used wheen loading annotations

// what to do when fonts are loaded
let lFontLoaded : (pEvent : InfiniteEvent) => void;
// get the font loader, no need to create it, all the time available (singleton)
const lFontLoader : FontLoaderInterface = InfiniteFactory.GetInfiniteApiController().getFontLoader();
// register font Open Sans normal (no bold, no italic) by its url
const lFontRegistered : boolean = lFontLoader.registerFont('Open Sans', false, false, 'resources/fonts/OpenSans-Regular.otf');
console.assert(lFontRegistered, 'font should be registered');

// what to do when fonts is loaded ?
lFontLoader.addEventListener(FontLoaderInterfaceSignal.FontsLoaded, lFontLoaded);
// load explicitly font, if not, font will be loaded if required when loading annotations
const lLoadStatus : FontLoadingStatus = lFontLoader.loadFont('Open Sans', false, false);
console.assert(lLoadStatus === FontLoadingStatus.FLS_LoadingStarted, 'Font loading should be started');

// when fonts are loaded => what do we do ?
// perhaps some gui code ?
lFontLoaded = (_pEvent : InfiniteEvent) : void =>
{
// get all registered fonts and their status
const lFonts : Array<FontInterface> = [];
lFontLoader.getRegisteredFonts(lFonts);
let i : number;
let lErrorCount : number = 0;
for (i = 0; i < lFonts.length; i += 1)
{
if (lFonts[i].error !== undefined)
{
lErrorCount += 1;
}
}
console.assert(lErrorCount === 0, 'There should be no font in error');
// when font is loaded, we unload it, but perhaps we should make some gui code ? :)
lFontLoader.unregisterFont('Open Sans', false, false);
};

Or asynchronously :
/** 
* Sample to illustrate the asynchronous loading of fonts.
*/
import { FontLoaderInterface, InfiniteFactory, FontLoadingStatus, FontInterface } from 'generated_files/documentation/appinfiniteapi';

// font loading is used wheen loading annotations

// get the font loader, no need to create it, all the time available (singleton)
const lFontLoader : FontLoaderInterface = InfiniteFactory.GetInfiniteApiController().getFontLoader();
// register font Open Sans normal (no bold, no italic) by its url
const lFontRegistered : boolean = lFontLoader.registerFont('Open Sans', false, false, 'resources/fonts/OpenSans-Regular.otf');
console.assert(lFontRegistered, 'font should be registered');

const waitForResult = async () : Promise<void> =>
{
// load explicitly font, if not, font will be loaded if required when loading annotations
const lLoadStatus : FontLoadingStatus = lFontLoader.loadFont('Open Sans', false, false);
console.assert(lLoadStatus === FontLoadingStatus.FLS_LoadingStarted, 'Font loading should be started');
// wait for loading to complete
await lFontLoader.asyncWaitForFontsLoaded();
// fonts are now loaded => what do we do ?
// perhaps some gui code ?
// get all registered fonts and their status
const lFonts : Array<FontInterface> = [];
lFontLoader.getRegisteredFonts(lFonts);
let i : number;
let lErrorCount : number = 0;
for (i = 0; i < lFonts.length; i += 1)
{
if (lFonts[i].error !== undefined)
{
lErrorCount += 1;
}
}
console.assert(lErrorCount === 0, 'There should be no font in error');
// when font is loaded, we unload it, but perhaps we should make some gui code ? :)
lFontLoader.unregisterFont('Open Sans', false, false);
};

waitForResult();

Please make sure the destination browser supports promises before using async calls.

Remember that font loading is only used with Annotations.


3D Rendering

interface FontLoaderInterface {
    addEventListener(pType, pListener, pObject): string;
    addEventListener(pType, pListener): string;
    areSignalsBlocked(): boolean;
    asyncWaitForFontsLoaded(): Promise<void>;
    blockSignals(pBlock): void;
    getFontErrors(): tFontErrorList;
    getFontsInErrorCount(): number;
    getLoadingFontsCount(): number;
    getRegisteredFonts(pFonts): void;
    getRegisteredFontsCount(): number;
    hasEventListener(pType, pListener): boolean;
    hasEventListenerById(pId): boolean;
    isFontLoaded(pFamilyName, pBold, pItalic): boolean;
    loadFont(pFamilyName, pBold, pItalic): FontLoadingStatus;
    registerFont(pFamilyName, pBold, pItalic, pUrlOrContent, pDefaultFont?): boolean;
    removeAllEventListeners(): boolean;
    removeEventListener(pType, pListener, pObject): boolean;
    removeEventListener(pType, pListener): boolean;
    removeEventListenerById(pId): boolean;
    setDefaultFont(pFamilyName, pBold, pItalic): boolean;
    unregisterFont(pFamilyName, pBold, pItalic): boolean;
}

Hierarchy (view full)

Methods

  • Adds a listener to an event type.

    When an event of the type pType fires, the callback pListener will be called. This function returns a unique string id that may be used in removeEventListenerById to allow simple listener removal. It is possible to add an object that will be included in the callback to avoid creating too many closures. Calling twice addEventListener with the same parameters results in the second call to be ignored, only unique pairs callback / object are allowed, in order to avoid calling multiple times the same thing.

    Parameters

    • pType: string
      in
      The type of the event pListener will be called upon.
    • pListener: tListenerCallback
      in
      The listener function that fires when the given event type occurs.
    • pObject: Object
      in
      The optional object the callback will be called with when the given event fires.

    Returns string

    The id of the inserted callback (actually an UUID).

  • Adds a listener to an event type.

    When an event of the type pType fires, the callback pListener will be called. This function returns a unique string id that may be used in removeEventListenerById to allow simple listener removal.

    Parameters

    • pType: string
      in
      The type of the event pListener will be called upon.
    • pListener: tListenerCallback
      in
      The listener function that fires when the given event type occurs.

    Returns string

    The id of the inserted callback (actually an UUID).

  • Tells if signals sent by the object are blocked or not.

    If signals are blocked, no signal will be emitted nor buffered, such signal will be lost.

    Returns boolean

    true if signals are blocked.

  • Waits till no font loading is in progress.

    Returns Promise<void>

    A Promise. There is no return value.

  • Blocks / Unblocks all signals sent by the object.

    If signals are blocked, no signal will be emitted nor buffered, such signal will be lost.

    Parameters

    • pBlock: boolean
      in
      If set to true, all further signals will be silently discarded.

    Returns void

  • Gets the number of fonts in error.

    Fonts are loaded by a call to loadFont.

    Returns number

    The number of fonts in error.

  • Gets the number of loading fonts.

    Fonts are loaded by a call to loadFont.

    Returns number

    The number of loading fonts.

  • Gets all the registered fonts.

    Fonts are registered by a call to registerFont.

    Parameters

    Returns void

  • Gets the number of registered fonts.

    Fonts are registered by a call to registerFont.

    Returns number

    The number of registered fonts.

  • Tells if the EventDispatcher has such a callback registered for the given event type.

    Parameters

    • pType: string
      in
      The type of the event to test.
    • pListener: tListenerCallback
      in
      The listener function that gets tested.

    Returns boolean

    true if such a listener is installed for the given type of event.

  • Tells if the EventDispatcher has such a callback registered for the given callback id.

    Parameters

    • pId: string
      in
      The id of the callback to test.

    Returns boolean

    true if such a listener is installed for the given callback id.

  • Tells if the given font is loaded.

    Returns true if the given font is loaded.

    Parameters

    • pFamilyName: string
      in
      The family name of the font to query.
    • pBold: boolean
      in
      Tells if the font is a bold one.
    • pItalic: boolean
      in
      Tells if the font is an italic one.

    Returns boolean

    true if the font is loaded.

  • Asks the font loader to begin loading the given font.

    Returns a FontLoadingStatus that tells the result of the load request.

    Parameters

    • pFamilyName: string
      in
      The family name of the font to load.
    • pBold: boolean
      in
      Tells if the font is a bold one.
    • pItalic: boolean
      in
      Tells if the font is an italic one.

    Returns FontLoadingStatus

    The FontLoadingStatus that tells the result of the load request.

  • Registers a font to the font loading mechanism.

    As fonts are loaded only when needed, the family name, bold and italic properties must be set beforehand in order to tell the system to load such a font when an annotation needs it.

    If a font with the same properties was already registered, the call to registerFont may only change the default font if set. It will return true even if the font url or data is not changed.

    If pUrlOrContent is a string, next calls to loadFont will be asynchronous, if pUrlOrContent is an ArrayBuffer, then next calls to loadFont will be synchronous (and the FontLoaderInterfaceSignal.FontsLoaded signal is sent right away).

    Parameters

    • pFamilyName: string
      in
      The family name of the font to register.
    • pBold: boolean
      in
      Tells if the font is a bold one.
    • pItalic: boolean
      in
      Tells if the font is an italic one.
    • pUrlOrContent: string | ArrayBuffer
      in
      The url or the content of the font file.
    • Optional pDefaultFont: boolean
      in
      Tells that this font will be the default one (defaults to false).

    Returns boolean

    true if the input parameters are correct. If such a font is already registered, true is also returned.

  • Removes a listener from an event type.

    If no such listener is found, then the function returns false and does nothing. You must use the exact parameters that were used in addEventListener to actually remove the listener.

    Parameters

    • pType: string
      in
      The type of the listener that gets removed.
    • pListener: tListenerCallback

      The listener function that gets removed.

    • pObject: Object

      The listener object that was used when addEventListener was called.

    Returns boolean

    true if the callback was removed else false.

  • Removes a listener from an event type.

    If no such listener is found, then the function returns false and does nothing. You must use the exact parameters that were used in addEventListener to actually remove the listener.

    Parameters

    • pType: string
      in
      The type of the listener that gets removed.
    • pListener: tListenerCallback

      The listener function that gets removed.

    Returns boolean

    true if the callback was removed else false.

  • Removes a listener by its id.

    If no such listener is found, then the function returns false and does nothing. You must use the return value of addEventListener to actually remove the listener.

    Parameters

    • pId: string
      in
      The id returned by the call to addEventListener that you want to remove.

    Returns boolean

    true if the callback was removed else false.

  • Sets an already registered font to be the default one.

    Returns true if such a font was found and set as default.

    Parameters

    • pFamilyName: string
      in
      The family name of the font to set as default.
    • pBold: boolean
      in
      Tells if the font is a bold one.
    • pItalic: boolean
      in
      Tells if the font is an italic one.

    Returns boolean

    true if such a font was found and set as default. If such a font was already the default font, then true is also returned.

  • Un-registers a font from the font loading mechanism.

    You cannot unregister the default font nor the 3djuump Infinite default font.

    Returns true if such a font was found and unregistered.

    Parameters

    • pFamilyName: string
      in
      The family name of the font to unregister.
    • pBold: boolean
      in
      Tells if the font is a bold one.
    • pItalic: boolean
      in
      Tells if the font is an italic one.

    Returns boolean

    true if such a font was found and unregistered.