Scrappy Fonts

Scrappy Fonts is a simple font management library for Solar2D that allows for easy font naming and access.

As with all my plugins it is free to use however if you find it useful, and are able to, I’d appreciate a coffee.

Image Credit: Writing by ProSymbols from the Noun Project.

Getting Started

There are multiple ways to include the library in your project, the simplest is probably via the plugin, as outlined below, however if you wish you can also access the source code.

Plugin

build.settings


settings =
{
    plugins =
    {
        ["plugin.scrappyFonts"] =
        {
            publisherId = "com.scrappyferret",
            supportedPlatforms =
            {
                iphone = { url="https://plugins.scrappyferret.com/scrappyFonts/iphone.tgz" },
                android = { url="https://plugins.scrappyferret.com/scrappyFonts/android.tgz" },
                macos = false,
                win32 = false
            },
        },
    },
}

main.lua


require( "plugin.scrappyFonts" )

Scrappy.Fonts:init()

Source Code

If you wish you to access the source code directly it is available on GitLab here.

You can then clone or fork it if you wish, or add the library as a submodule to your project with this command:

git submodule add https://gitlab.com/scrappyferret-libs/scrappy-fonts.git fonts

However you get the source code into your project, you will then need to include it and initiate it.

main.lua


require( "fonts.core" )

Scrappy.Fonts:init()

API

Scrappy.Fonts:init()

Initiates the library.


Scrappy.Fonts:register( name, filename )

Registers a font.

name ( string ) – The name of the font to register.

filename ( string ) – The font’s filename.

returns – The registered font, or nil if not registered.


Scrappy.Fonts:get( name )

Gets a registered font.

name ( string ) – The name of the font to retrieve.

returns – The font, or nil if not found.


Scrappy.Fonts:list( )

Gets a list of the names of all registered fonts.

returns – A list of font names, empty if none registered.


Scrappy.Fonts:registerNativeFonts()

Registers all the system native fonts.


Examples

Register and use a font.

-- Register the fonts
Scrappy.Fonts:register( "logo", "Sansation_Light.ttf" )
Scrappy.Fonts:register( "tagline", "Sansation_Light_Italic.ttf" )

-- Create the text options, retrieving the font name from the system
local options =
{
    text = "SOLAR 2D",
    x = display.contentCenterX,
    y = display.contentCenterY,
    font = Scrappy.Fonts:get( "logo" ),
    fontSize = 60,
	width = display.contentWidth,
	align = "center"
}

-- Create some text
local logo = display.newText( options )

-- Adjust the options for the tagline
options.text = "The 2D Game Engine."
options.font = Scrappy.Fonts:get( "tagline" )
options.fontSize = options.fontSize * 0.6

-- Create some more text
local tagline = display.newText( options )
tagline.y = logo.y + logo.contentHeight * 0.6