Git Product home page Git Product logo

font's Introduction

Font

An experiment in font scaling inspired media query like behaviour.

Motivation

At one point or another, many developers with Love find out that font scaling via the sx and sy parameters leave less than desirable effects. Unless you're using a bitmap font with nearest filtering applied, your scaled text often ends up being blurry.

The common answer is to just create different Font objects at different sizes. This library seeks to see if it's any use to predefine a series of "breakpoints" similar to responsive css to just define a consistent font scale throughout your game.

Usage

The entrypoint to Font is Font.register which will take the path to the font, and a table containing the various sizes at different screen widths.

local Font = require "path.to.font"

local wakuwaku = nil

function love.load()
  wakuwaku = Font.new(
    "path/to/font/wakuwaku.otf",
    {sm = 16, md = 24, lg = 32, xl = 48, xxl = 72 }
    )

  -- Hook into the `love.resize` event (optional - read the API section)
  wakuwaku:hook()
end

function love.draw()
  love.graphics.print("Hello, World", wakuwaku:get())
end

If the screen width is larger than the configured with, then it will use the font size configured. By default these sizes are

  • sm is 640px
  • md is 768px
  • lg is 1024px
  • xl is 1280px
  • xxl is 1536px

(These are borrowed from the Tailwind configured media queries)

Therefore is the screen is 660px wide, then it shall use the sm configured value. If it's 1100px, then the lg will be used, and so on.

Configuration

These aren't overly helpful without some customisation of behaviour. You can change the default screen size breakpoints via the Font.configure function.

Font.configure({
    sm  = 640,
    md  = 768,
    lg  = 1024,
    xl  = 1280,
    xxl = 1536
  })

You can also set them on the individual results of calling new by using reconfigure

  local wakuwaku = Font.new(
    "path/to/font/wakuwaku.otf",
    {sm = 16, md = 24, lg = 32, xl = 48, xxl = 72 }
    )
  wakuwaku:reconfigure({
    sm  = 200,
    md  = 300,
    lg  = 400,
    xl  = 500,
    xxl = 600
  })

API

Font.new(font: string, sizes: Table<string, number>): FontObj

Takes a path to a love supported font, and a table from size to number that will define the font sizes for each screen size, and returns a new Font object.

Font.configure(screenSizes: Table<string, number>): nil

Takes a table from screen size (sm, md, lg, xl, xxl) to number to use as the default set of breakpoints to change the font size.

FontObj:get(): love.graphics.Font

Returns a love Font Object for the current size. Note that the current size won't change unless you've called FontObj:resize(width) or FontObj:hook().

FontObj:resize(width: number): nil

Sets the current size value for the given screen size. The current size value is determined by the default value, or the value configured by Font.configure or FontObj:reconfigure

FontObj:hook(): nil

Hooks this FontObj to the love.resize event, calling FontObj:resize internally whenever the screen is resized.

FontObj:reconfigure(screenSizes: Table<string, number>): nil

Takes a table from screen size (sm, md, lg, xl, xxl) to number to use as the this instance's set of breakpoints to change the font size.

Contributing

Contributions welcome :) As this is an experiment, there's no doubt flaws or improvements to be made

Attribution

Example font is mini-wakuwaku by miniyama, gratefully retrieved from https://www.freejapanesefont.com/mini-wakuwaku/

font's People

Contributors

zombrodo avatar

Stargazers

flamendless avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.