Difference between revisions of "love.graphics.newImageFont"

m (Add simple example)
m (Cleanup.)
 
Line 4: Line 4:
  
 
{{newobjectnotice}}
 
{{newobjectnotice}}
 +
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
font = love.graphics.newImageFont( filename, glyphs )
+
font = love.graphics.newImageFont( filename, glyphs, extraspacing )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
 
{{param|string|filename|The filepath to the image file.}}
 
{{param|string|filename|The filepath to the image file.}}
 
{{param|string|glyphs|A string of the characters in the image in order from left to right.}}
 
{{param|string|glyphs|A string of the characters in the image in order from left to right.}}
 +
{{New_feature|0.10.0|
 +
{{param|number|extraspacing (0)|Additional spacing (positive or negative) to apply to each glyph in the Font.}}
 +
}}
 
=== 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 18: Line 22:
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
font = love.graphics.newImageFont( imagedata, glyphs )
+
font = love.graphics.newImageFont( imageData, glyphs, extraspacing )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
 
{{param|ImageData|imageData|The ImageData object to create the font from.}}
 
{{param|ImageData|imageData|The ImageData object to create the font from.}}
 
{{param|string|glyphs|A string of the characters in the image in order from left to right.}}
 
{{param|string|glyphs|A string of the characters in the image in order from left to right.}}
=== Returns ===
+
{{New_feature|0.10.0|
{{param|Font|font|A Font object which can be used to draw text on screen.}}
+
{{param|number|extraspacing (0)|Additional spacing (positive or negative) to apply to each glyph in the Font.}}
 
+
}}
== Function ==
 
{{newin|[[0.10.0]]|100|type=variant}}
 
=== Synopsis ===
 
<source lang="lua">
 
font = love.graphics.newImageFont( filename, glyphs, extraspacing )
 
</source>
 
=== Arguments ===
 
{{param|string|filename|The filepath to the image file.}}
 
{{param|string|glyphs|A string of the characters in the image in order from left to right.}}
 
{{param|number|extraspacing|Additional spacing (positive or negative) to apply to each glyph in the Font.}}
 
 
=== 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.}}
  
 
== Notes ==
 
== Notes ==
Instead of using this function, consider using a BMFont generator such as [http://www.angelcode.com/products/bmfont/ bmfont], [http://kvazars.com/littera/ littera], or [https://www.bmglyph.com/ bmGlyph] with [[love.graphics.newFont]]. Because slime said it was better.
+
Instead of using this limited function, consider using a BMFont generator such as [http://www.angelcode.com/products/bmfont/ bmfont], [http://kvazars.com/littera/ littera], or [https://www.bmglyph.com/ bmGlyph] with [[love.graphics.newFont]].
  
 
== Examples ==
 
== Examples ==
Creating a simple image font. Download this [[:File:font_example.png|image file]] which will be used by LÖVE to create the font. Obviously when you want to create a font for your game you want to make the background transparent.
+
=== Create a simple image font ===
 +
Download this [[:File:font_example.png|image file]] which will be used by LÖVE to create the font. Of course, when you want to create a font for your game you probably want to make the background transparent instead of black.
 +
 
 
<source lang="lua">
 
<source lang="lua">
local font = love.graphics.newImageFont( 'font_example.png', ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' )
+
local font = love.graphics.newImageFont("font_example.png", " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
 
function love.draw()
 
function love.draw()
    love.graphics.setFont( font )
+
love.graphics.setFont(font)
    love.graphics.print( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 16, 16 )
+
love.graphics.print("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 16, 16)
    love.graphics.print( 'Text is now drawn using the font', 16, 32 )
+
love.graphics.print("Text is now drawn using the font", 16, 32)
 
end
 
end
 
</source>
 
</source>
  
 
== See Also ==
 
== See Also ==
 +
* [[Constructs::Font]]
 +
* [[ImageFontFormat|Image Font Format]]
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
* [[Constructs::Font]]
+
 
* [[ImageFontFormat | Image Font Format]]
 
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Sub-Category::Object Creation| ]]
 
[[Sub-Category::Object Creation| ]]
Line 62: Line 59:
 
{{#set:Since=020}}
 
{{#set:Since=020}}
 
{{#set:PrettySince=0.2.0}}
 
{{#set:PrettySince=0.2.0}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.newImageFont}}
 
{{i18n|love.graphics.newImageFont}}

Latest revision as of 19:44, 26 July 2022

Creates a new Font by loading a specifically formatted image.

In versions prior to 0.9.0, LÖVE expects ISO 8859-1 encoding for the glyphs string.


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

Synopsis

font = love.graphics.newImageFont( filename, glyphs, extraspacing )

Arguments

string filename
The filepath to the image file.
string glyphs
A string of the characters in the image in order from left to right.
Available since LÖVE 0.10.0
number extraspacing (0)
Additional spacing (positive or negative) to apply to each glyph in the Font.

Returns

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

Function

Synopsis

font = love.graphics.newImageFont( imageData, glyphs, extraspacing )

Arguments

ImageData imageData
The ImageData object to create the font from.
string glyphs
A string of the characters in the image in order from left to right.
Available since LÖVE 0.10.0
number extraspacing (0)
Additional spacing (positive or negative) to apply to each glyph in the Font.

Returns

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

Notes

Instead of using this limited function, consider using a BMFont generator such as bmfont, littera, or bmGlyph with love.graphics.newFont.

Examples

Create a simple image font

Download this image file which will be used by LÖVE to create the font. Of course, when you want to create a font for your game you probably want to make the background transparent instead of black.

local font = love.graphics.newImageFont("font_example.png", " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
function love.draw()
	love.graphics.setFont(font)
	love.graphics.print("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 16, 16)
	love.graphics.print("Text is now drawn using the font", 16, 32)
end

See Also



Other Languages