Difference between revisions of "love.graphics.newFont"

m (slime just told us about hinting mode)
m (formatting)
 
(17 intermediate revisions by 8 users not shown)
Line 1: Line 1:
Creates a new [[Font]] from a TrueType Font or BMFont file. Created fonts are not cached, in that calling this function with the same arguments will always create a new Font object.
+
Creates a new [[Font]] from a TrueType font or [http://www.angelcode.com/products/bmfont/doc/file_format.html BMFont] file. OpenType fonts (.otf) work as well but some of its features may not be supported.
 +
 
 +
Created fonts are not cached, in that calling this function with the same arguments will always create a new Font object.
  
 
All variants which accept a filename can also accept a [[Data]] object instead.
 
All variants which accept a filename can also accept a [[Data]] object instead.
 +
 +
The default font in LÖVE is 'Bitstream Vera Sans' size 12 (before LÖVE 12.0) and 'Noto Sans' size 13 (after LÖVE 12.0).
  
 
{{newobjectnotice}}
 
{{newobjectnotice}}
 +
 
== Function ==
 
== Function ==
 
Create a new BMFont or TrueType font.
 
Create a new BMFont or TrueType font.
Line 11: Line 16:
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|filename|The filepath to the BMFont or TrueType font file.}}
+
{{param|Variant|filename|The filepath or filedata to the font file. Types can be below:}}
 +
{{subparam|string|filename|The filepath to the font file. This can be TrueType or, since [[0.10.0]], BMFont.}}
 +
{{subparam|Data|filename|The filedata of the font file. This can be create by [[love.filesystem.newFileData]].}}
 +
 
 
=== Returns ===
 
=== Returns ===
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
Line 21: Line 29:
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
font = love.graphics.newFont( filename, size, hinting )
+
font = love.graphics.newFont( filename, size, hinting, dpiscale )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
Line 29: Line 37:
 
{{param|HintingMode|hinting ("normal")|True Type hinting mode.}}
 
{{param|HintingMode|hinting ("normal")|True Type hinting mode.}}
 
|100}}
 
|100}}
 +
{{New feature|11.0|
 +
{{param|number|dpiscale ([[love.graphics.getDPIScale]]())|The [[Font:getDPIScale|DPI scale factor]] of the font.}}
 +
|110}}
 
=== Returns ===
 
=== Returns ===
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
  
 
== Function ==
 
== Function ==
 +
{{newin|[[0.10.0]]|100|type=variant}}
 
Create a new BMFont.
 
Create a new BMFont.
 
=== Synopsis ===
 
=== Synopsis ===
Line 48: Line 60:
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
font = love.graphics.newFont( size, hinting )
+
font = love.graphics.newFont( size, hinting, dpiscale )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
Line 55: Line 67:
 
{{param|HintingMode|hinting ("normal")|True Type hinting mode.}}
 
{{param|HintingMode|hinting ("normal")|True Type hinting mode.}}
 
|100}}
 
|100}}
 +
{{New feature|11.0|
 +
{{param|number|dpiscale ([[love.graphics.getDPIScale]]())|The [[Font:getDPIScale|DPI scale factor]] of the font.}}
 +
|110}}
 
=== Returns ===
 
=== Returns ===
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
 
{{param|Font|font|A Font object which can be used to draw text on screen.}}
Line 70: Line 85:
 
-- Draws "Hello world!" at position x: 100, y: 200 with the custom font applied.
 
-- Draws "Hello world!" at position x: 100, y: 200 with the custom font applied.
 
love.graphics.print("Hello world!", 100, 200)
 
love.graphics.print("Hello world!", 100, 200)
 +
end
 +
</source>
 +
 +
 +
=== Use newFont to draw pixelated text ===
 +
[[File:font_size_7.png|upright=2|thumb|right|top|Pixelated font size 7]]
 +
 +
<source lang="lua">
 +
local font = love.graphics.newFont(7, "mono")
 +
font:setFilter("nearest")
 +
love.graphics.setFont(font)
 +
 +
function love.draw()
 +
love.graphics.scale(4)
 +
love.graphics.print ("Hello World 0123", 2, 2)
 
end
 
end
 
</source>
 
</source>
Line 77: Line 107:
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 
* [[love.graphics.setFont]]
 
* [[love.graphics.setFont]]
 +
* [[love.graphics.setNewFont]]
 +
* [[love.graphics.newImageFont]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Sub-Category::Object Creation| ]]
 
[[Sub-Category::Object Creation| ]]
 
{{#set:Description=Creates a new [[Font]] from a TrueType Font or BMFont file.}}
 
{{#set:Description=Creates a new [[Font]] from a TrueType Font or BMFont file.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.newFont}}
 
{{i18n|love.graphics.newFont}}

Latest revision as of 15:44, 20 October 2023

Creates a new Font from a TrueType font or BMFont file. OpenType fonts (.otf) work as well but some of its features may not be supported.

Created fonts are not cached, in that calling this function with the same arguments will always create a new Font object.

All variants which accept a filename can also accept a Data object instead.

The default font in LÖVE is 'Bitstream Vera Sans' size 12 (before LÖVE 12.0) and 'Noto Sans' size 13 (after LÖVE 12.0).


O.png This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused!  



Function

Create a new BMFont or TrueType font.

Synopsis

font = love.graphics.newFont( filename )

Arguments

Variant filename
The filepath or filedata to the font file. Types can be below:
string filename
The filepath to the font file. This can be TrueType or, since 0.10.0, BMFont.
Data filename
The filedata of the font file. This can be create by love.filesystem.newFileData.

Returns

Font font
A Font object which can be used to draw text on screen.

Notes

If the file is a TrueType font, it will be size 12. Use the variant below to create a TrueType font with a custom size.

Function

Create a new TrueType font.

Synopsis

font = love.graphics.newFont( filename, size, hinting, dpiscale )

Arguments

string filename
The filepath to the TrueType font file.
number size
The size of the font in pixels.
Available since LÖVE 0.10.0
HintingMode hinting ("normal")
True Type hinting mode.


Available since LÖVE 11.0
number dpiscale (love.graphics.getDPIScale())
The DPI scale factor of the font.

Returns

Font font
A Font object which can be used to draw text on screen.

Function

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.

Create a new BMFont.

Synopsis

font = love.graphics.newFont( filename, imagefilename )

Arguments

string filename
The filepath to the BMFont file.
string imagefilename
The filepath to the BMFont's image file. If this argument is omitted, the path specified inside the BMFont file will be used.

Returns

Font font
A Font object which can be used to draw text on screen.

Function

Create a new instance of the default font (Vera Sans) with a custom size.

Synopsis

font = love.graphics.newFont( size, hinting, dpiscale )

Arguments

number size (12)
The size of the font in pixels.
Available since LÖVE 0.10.0
HintingMode hinting ("normal")
True Type hinting mode.


Available since LÖVE 11.0
number dpiscale (love.graphics.getDPIScale())
The DPI scale factor of the font.

Returns

Font font
A Font object which can be used to draw text on screen.

Examples

Use newFont to draw a custom styled text

-- Create a ttf file font with a custom size of 20 pixels.
mainFont = love.graphics.newFont("anyfont.ttf", 20)

function love.draw() 
	-- Setting the font so that it is used when drawning the string.
	love.graphics.setFont(mainFont)

	-- Draws "Hello world!" at position x: 100, y: 200 with the custom font applied.
	love.graphics.print("Hello world!", 100, 200)
end


Use newFont to draw pixelated text

Pixelated font size 7
local font = love.graphics.newFont(7, "mono")
font:setFilter("nearest")
love.graphics.setFont(font)

function love.draw()
	love.graphics.scale(4)
	love.graphics.print ("Hello World 0123", 2, 2)
end

See Also



Other Languages