Difference between revisions of "love.graphics.printf"

(Added new 0.9.0 variant)
(See Also)
 
(10 intermediate revisions by 6 users not shown)
Line 3: Line 3:
 
See additional notes in [[love.graphics.print]].
 
See additional notes in [[love.graphics.print]].
  
'''NOTE: aligning does not work as one might expect! It doesn't align to the x/y coordinates given, but in a rectangle. See https://love2d.org/forums/viewtopic.php?f=4&t=9709&p=59709'''
+
The word wrap limit is applied before any scaling, rotation, and other coordinate transformations. Therefore the amount of text per line stays constant given the same wrap limit, even if the scale arguments change.
  
 +
In version [[0.9.2]] and earlier, wrapping was implemented by breaking up words by spaces and putting them back together to make sure things fit nicely within the limit provided. However, due to the way this is done, extra spaces between words would end up missing when printed on the screen, and some lines could overflow past the provided wrap limit. In version [[0.10.0]] and newer this is no longer the case.
 +
 +
In versions prior to [[11.0]], color and byte component values were within the range of 0 to 255 instead of 0 to 1.
 +
 +
{{notice|Aligning does not work as one might expect! It doesn't align to the x/y coordinates given, but in a rectangle, where the limit is the width.}}
 +
{{notice|Text may appear blurry if it's rendered at non-integer pixel locations.}}
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
love.graphics.printf( text, x, y, limit, align )
+
love.graphics.printf( text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
Line 16: Line 22:
 
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 
{{param|AlignMode|align ("left")|The alignment.}}
 
{{param|AlignMode|align ("left")|The alignment.}}
 +
{{New feature|0.9.0|
 +
{{param|number|r (0)|Orientation (radians).}}
 +
{{param|number|sx (1)|Scale factor (x-axis).}}
 +
{{param|number|sy (sx)|Scale factor (y-axis).}}
 +
{{param|number|ox (0)|Origin offset (x-axis).}}
 +
{{param|number|oy (0)|Origin offset (y-axis).}}
 +
{{param|number|kx (0)|Shearing factor (x-axis).}}
 +
{{param|number|ky (0)|Shearing factor (y-axis).}}
 +
}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
  
 
== Function ==
 
== Function ==
{{newin|[[0.9.0]]|090|type=variant}}
+
{{newin|[[11.0]]|110|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
love.graphics.printf( text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )
+
love.graphics.printf( text, font, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
 
{{param|string|text|A text string.}}
 
{{param|string|text|A text string.}}
 +
{{param|Font|font|The Font object to use.}}
 
{{param|number|x|The position on the x-axis.}}
 
{{param|number|x|The position on the x-axis.}}
 
{{param|number|y|The position on the y-axis.}}
 
{{param|number|y|The position on the y-axis.}}
Line 40: Line 56:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Function ==
 +
{{newin|[[11.0]]|110|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( text, transform, limit, align )
 +
</source>
 +
=== Arguments ===
 +
{{param|string|text|A text string.}}
 +
{{param|Transform|transform|Transformation object.}}
 +
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 +
{{param|AlignMode|align ("left")|The alignment.}}
 +
=== Returns ===
 +
Nothing.
 +
 +
== Function ==
 +
{{newin|[[11.0]]|110|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( text, font, transform, limit, align )
 +
</source>
 +
=== Arguments ===
 +
{{param|string|text|A text string.}}
 +
{{param|Font|font|The Font object to use.}}
 +
{{param|Transform|transform|Transformation object.}}
 +
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 +
{{param|AlignMode|align ("left")|The alignment.}}
 +
=== Returns ===
 +
Nothing.
 +
 +
== Function ==
 +
{{newin|[[0.10.0]]|100|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( coloredtext, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )
 +
</source>
 +
=== Arguments ===
 +
{{param|table|coloredtext|A table containing colors and strings to add to the object, in the form of <code>{color1, string1, color2, string2, ...}</code>.}}
 +
{{subparam|table|color1|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string1|A string of text which has a color specified by the previous color.}}
 +
{{subparam|table|color2|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string2|A string of text which has a color specified by the previous color.}}
 +
{{subparam|tables and strings|...|Additional colors and strings.}}
 +
{{param|number|x|The position of the text (x-axis).}}
 +
{{param|number|y|The position of the text (y-axis).}}
 +
{{param|number|limit|The maximum width in pixels of the text before it gets automatically wrapped to a new line.}}
 +
{{param|AlignMode|align|The alignment of the text.}}
 +
{{param|number|angle (0)|Orientation (radians).}}
 +
{{param|number|sx (1)|Scale factor (x-axis).}}
 +
{{param|number|sy (sx)|Scale factor (y-axis).}}
 +
{{param|number|ox (0)|Origin offset (x-axis).}}
 +
{{param|number|oy (0)|Origin offset (y-axis).}}
 +
{{param|number|kx (0)|Shearing / skew factor (x-axis).}}
 +
{{param|number|ky (0)|Shearing / skew factor (y-axis).}}
 +
=== Returns ===
 +
Nothing.
 +
=== Notes ===
 +
The color set by [[love.graphics.setColor]] will be combined (multiplied) with the colors of the text.
 +
 +
== Function ==
 +
{{newin|[[11.0]]|110|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( coloredtext, font, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )
 +
</source>
 +
=== Arguments ===
 +
{{param|table|coloredtext|A table containing colors and strings to add to the object, in the form of <code>{color1, string1, color2, string2, ...}</code>.}}
 +
{{subparam|table|color1|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string1|A string of text which has a color specified by the previous color.}}
 +
{{subparam|table|color2|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string2|A string of text which has a color specified by the previous color.}}
 +
{{subparam|tables and strings|...|Additional colors and strings.}}
 +
{{param|Font|font|The Font object to use.}}
 +
{{param|number|x|The position on the x-axis.}}
 +
{{param|number|y|The position on the y-axis.}}
 +
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 +
{{param|AlignMode|align ("left")|The alignment.}}
 +
{{param|number|angle (0)|Orientation (radians).}}
 +
{{param|number|sx (1)|Scale factor (x-axis).}}
 +
{{param|number|sy (sx)|Scale factor (y-axis).}}
 +
{{param|number|ox (0)|Origin offset (x-axis).}}
 +
{{param|number|oy (0)|Origin offset (y-axis).}}
 +
{{param|number|kx (0)|Shearing factor (x-axis).}}
 +
{{param|number|ky (0)|Shearing factor (y-axis).}}
 +
=== Returns ===
 +
Nothing.
 +
=== Notes ===
 +
The color set by [[love.graphics.setColor]] will be combined (multiplied) with the colors of the text.
 +
 +
== Function ==
 +
{{newin|[[11.0]]|110|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( coloredtext, transform, limit, align )
 +
</source>
 +
=== Arguments ===
 +
{{param|table|coloredtext|A table containing colors and strings to add to the object, in the form of <code>{color1, string1, color2, string2, ...}</code>.}}
 +
{{subparam|table|color1|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string1|A string of text which has a color specified by the previous color.}}
 +
{{subparam|table|color2|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string2|A string of text which has a color specified by the previous color.}}
 +
{{subparam|tables and strings|...|Additional colors and strings.}}
 +
{{param|Transform|transform|Transformation object.}}
 +
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 +
{{param|AlignMode|align ("left")|The alignment.}}
 +
=== Returns ===
 +
Nothing.
 +
=== Notes ===
 +
The color set by [[love.graphics.setColor]] will be combined (multiplied) with the colors of the text.
 +
 +
== Function ==
 +
{{newin|[[11.0]]|110|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.printf( coloredtext, font, transform, limit, align )
 +
</source>
 +
=== Arguments ===
 +
{{param|table|coloredtext|A table containing colors and strings to add to the object, in the form of <code>{color1, string1, color2, string2, ...}</code>.}}
 +
{{subparam|table|color1|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string1|A string of text which has a color specified by the previous color.}}
 +
{{subparam|table|color2|A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of <code>{red, green, blue, alpha}</code>.}}
 +
{{subparam|string|string2|A string of text which has a color specified by the previous color.}}
 +
{{subparam|tables and strings|...|Additional colors and strings.}}
 +
{{param|Font|font|The Font object to use.}}
 +
{{param|Transform|transform|Transformation object.}}
 +
{{param|number|limit|Wrap the line after this many horizontal pixels.}}
 +
{{param|AlignMode|align ("left")|The alignment.}}
 +
=== Returns ===
 +
Nothing.
 +
=== Notes ===
 +
The color set by [[love.graphics.setColor]] will be combined (multiplied) with the colors of the text.
  
 
== Examples ==
 
== Examples ==
Line 53: Line 200:
 
</source>
 
</source>
 
== See Also ==
 
== See Also ==
 +
* [[love.graphics.print]]
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 +
* [[Font:getWrap]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Sub-Category::Drawing| ]]
 
[[Sub-Category::Drawing| ]]
 
{{#set:Description=Draws formatted text, with word wrap and alignment.}}
 
{{#set:Description=Draws formatted text, with word wrap and alignment.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.printf}}
 
{{i18n|love.graphics.printf}}

Latest revision as of 20:52, 31 May 2021

Draws formatted text, with word wrap and alignment.

See additional notes in love.graphics.print.

The word wrap limit is applied before any scaling, rotation, and other coordinate transformations. Therefore the amount of text per line stays constant given the same wrap limit, even if the scale arguments change.

In version 0.9.2 and earlier, wrapping was implemented by breaking up words by spaces and putting them back together to make sure things fit nicely within the limit provided. However, due to the way this is done, extra spaces between words would end up missing when printed on the screen, and some lines could overflow past the provided wrap limit. In version 0.10.0 and newer this is no longer the case.

In versions prior to 11.0, color and byte component values were within the range of 0 to 255 instead of 0 to 1.

O.png Aligning does not work as one might expect! It doesn't align to the x/y coordinates given, but in a rectangle, where the limit is the width.  


O.png Text may appear blurry if it's rendered at non-integer pixel locations.  


Function

Synopsis

love.graphics.printf( text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )

Arguments

string text
A text string.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
Available since LÖVE 0.9.0
number r (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

Returns

Nothing.

Function

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

Synopsis

love.graphics.printf( text, font, x, y, limit, align, r, sx, sy, ox, oy, kx, ky )

Arguments

string text
A text string.
Font font
The Font object to use.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
number r (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

Returns

Nothing.

Function

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

Synopsis

love.graphics.printf( text, transform, limit, align )

Arguments

string text
A text string.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Function

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

Synopsis

love.graphics.printf( text, font, transform, limit, align )

Arguments

string text
A text string.
Font font
The Font object to use.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Function

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

Synopsis

love.graphics.printf( coloredtext, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
number x
The position of the text (x-axis).
number y
The position of the text (y-axis).
number limit
The maximum width in pixels of the text before it gets automatically wrapped to a new line.
AlignMode align
The alignment of the text.
number angle (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing / skew factor (x-axis).
number ky (0)
Shearing / skew factor (y-axis).

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

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

Synopsis

love.graphics.printf( coloredtext, font, x, y, limit, align, angle, sx, sy, ox, oy, kx, ky )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Font font
The Font object to use.
number x
The position on the x-axis.
number y
The position on the y-axis.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.
number angle (0)
Orientation (radians).
number sx (1)
Scale factor (x-axis).
number sy (sx)
Scale factor (y-axis).
number ox (0)
Origin offset (x-axis).
number oy (0)
Origin offset (y-axis).
number kx (0)
Shearing factor (x-axis).
number ky (0)
Shearing factor (y-axis).

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

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

Synopsis

love.graphics.printf( coloredtext, transform, limit, align )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Function

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

Synopsis

love.graphics.printf( coloredtext, font, transform, limit, align )

Arguments

table coloredtext
A table containing colors and strings to add to the object, in the form of {color1, string1, color2, string2, ...}.
table color1
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string1
A string of text which has a color specified by the previous color.
table color2
A table containing red, green, blue, and optional alpha components to use as a color for the next string in the table, in the form of {red, green, blue, alpha}.
string string2
A string of text which has a color specified by the previous color.
tables and strings ...
Additional colors and strings.
Font font
The Font object to use.
Transform transform
Transformation object.
number limit
Wrap the line after this many horizontal pixels.
AlignMode align ("left")
The alignment.

Returns

Nothing.

Notes

The color set by love.graphics.setColor will be combined (multiplied) with the colors of the text.

Examples

Draw text to the screen with right alignment and a horizontal limit of 125.

love.graphics.printf("This text is aligned right, and wraps when it gets too big.", 25, 25, 125, "right")

Notes

Note that the limit argument affects the position of your text for 'center' and 'right' alignment.

love.graphics.printf("This text is aligned center",100, 100, 200,"center") -- center your text around x = 200/2 + 100 = 200
love.graphics.printf("This text is aligned right",100, 100, 200,"right") -- align right to x = 100 + 200 = 300

See Also



Other Languages