The Font object is used to manage fonts in application.


Can be created with method new:

Lib.Media.Text.Font.new(fontName, fontStyle, fontType)

Creates Font object.

fontName - The name of font.

fontStyle - The style of the font. (FontStyle)

fontType - The type of the font. (FontType)


Created Font object has properties:

fontName

[read-only] The name of font.

fontStyle

[read-only] The style of the font. (FontStyle)

fontType

[read-only] The type of the font. (FontType)


methods:

toString()

Return string representation of Font object.



static methods:

loadBytes(bytes)

Load native font data object from bytes

This object has fields:

NativeFontData = 

{

   has_kerning: Bool

   is_fixed_width: Bool

   has_glyph_names: Bool

   is_italic: Bool

   is_bold: Bool

   num_glyphs: Int

   family_name: String

   style_name: String

   em_size: Int

   ascend: Int

   descend: Int

   height: Int

   glyphs: Array of NativeGlyphData

   kerning: Array of NativeKerningData

}


NativeGlyphData = 

{

   char_code: Int

   advance: Int

   min_x: Int

   max_x: Int

   min_y: Int

   max_y: Int

   points: Array of Int

}


NativeKerningData = 

{

   left_glyph:Int

   right_glyph:Int

   x:Int

   y:Int

}

bytes - ByteArray object.

registerFontData(instance, bytes)

If you want use Font in TextField object you need to register loaded font bytes.

instance - Font object

bytes - ByteArray object.

enumerateFonts(enumerateDeviceFonts)

return array of registerd fonts.

enumerateDeviceFonts - include device system fonts if true, default false

isRegistered(instance)

check if instance (Font) registered (true/false)

instance - Font object

unregisterFontData(instance)

Unregisters font and releases all related memory. You can't use this font in TextField object from this moment. System will not release memory completely if you still have even single TextField object pointed to this Font object.

instance - Font object


Examples:


Text = Lib.Media.Text

Font = Text.Font


font = Font.new("Purisa", Text.FontStyle.REGULAR, Text.FontType.EMBEDDED)

bytes = Lib.Project.getBytes("/Font/assets/Purisa.ttf")


nativeFontData = Font.loadBytes(bytes)

print("has kerning: "..(nativeFontData.has_kerning and "yes" or "not"))

print("has glyph names: "..(nativeFontData.has_glyph_names and "yes" or "not"))

print("is italic: "..(nativeFontData.is_italic and "yes" or "not"))

print("is bold: "..(nativeFontData.is_bold and "yes" or "not"))

print("num glyphs: "..nativeFontData.num_glyphs)

print("family name: "..nativeFontData.family_name)

print("style name: "..nativeFontData.style_name)

print("em size: "..nativeFontData.em_size)

print("ascend: "..nativeFontData.ascend)

print("descend: "..nativeFontData.descend)

print("height: "..nativeFontData.height)

if #nativeFontData.glyphs >= 65 then

       print("char code: "..nativeFontData.glyphs[65].char_code)

       print("advance: "..nativeFontData.glyphs[65].advance)

       print("min_x: "..nativeFontData.glyphs[65].min_x)

       print("max_x: "..nativeFontData.glyphs[65].max_x)

       print("min_y: "..nativeFontData.glyphs[65].min_y)

       print("max_y: "..nativeFontData.glyphs[65].max_y)

       print("points count: "..#nativeFontData.glyphs[65].points)--integer values array

end

if nativeFontData.has_kerning then

       print("kernings count: "..#nativeFontData.kerning)-- array of objects with fields: left_glyph, right_glyph, x, y

end


Font.registerFontData(font, bytes)

bytes = nil

fonts = Font.enumerateFonts(false)

for i=1, #fonts, 1 do

       print(fonts[i].toString())

end


textFormat = Text.TextFormat.new("Purisa", 24, 0, false, false, false)

textField = Text.TextField.new()

textField.defaultTextFormat = textFormat

textField.border = true

textField.text = "Test"


Lib.Media.Display.stage.addChild(textField)


--self destruct

i = 3

print("Self destruct in:")

timer = Lib.Media.Utils.Timer.new(1000,4)

timer.addEventListener(Lib.Media.Events.TimerEvent.TIMER, function(e)

    print(i)

    if i == 0 then

               Lib.Media.Display.stage.removeChild(textField)

               textField = nil

               textFormat = nil

               Lib.Media.Text.Font.unregisterFontData(font)

               Lib.Media.System.gc()

    end

    i = i - 1  

end, false, 0 ,false)

timer.start()

Created with the Personal Edition of HelpNDoc: Free help authoring tool