font_metrics v0.3.1 FontMetrics View Source
FontMetrics works with pre-generated font metrics to explore and calculate various measurements of text in a given font and size.
For example, if you want to know how wide or tall a string of text will be when it is rendered in a given font at a given size, then this can help you out.
This library is intended to be used with the Scenic framework, but doesn't depend on it, so it is usable elsewhere.
Installation
If available in Hex, the package can be installed
by adding font_metrics
to your list of dependencies in mix.exs
:
def deps do
[
{:font_metrics, "~> 0.3"}
]
end
Generating Metrics
You will need to use another package to compile the font metrics data from a font.
This can be done with the truetype_metrics package. Look for it on hex...
Link to this section Summary
Functions
Get the ascent of the font scaled to the pixel height
Get the descent of the font scaled to the pixel height
The type of hash used to verify the signature
Deserialize a binary into a %FontMetrics{}
Deserialize a binary into a %FontMetrics{}
Return a box that would hold the largest character in the font
Find the gap between to characters given an {x,y} coordinate
Transform point values into pixels
Returns the coordinates just before the given character number
Shorten a string to fit a given width
Checks if all the characters can be rendered by the font
Serialize a %FontMetrics{}
struct to a binary
Serialize a %FontMetrics{}
struct to a binary
Measure the width of a string, scaled to a pixel size
Wraps a string to a given width by adding returns
Link to this section Functions
ascent(pixels, font_metrics) View Source
Get the ascent of the font scaled to the pixel height
returns ascent
descent(pixels, font_metrics) View Source
Get the descent of the font scaled to the pixel height
returns descent
do_shorten(source, max_width, scale, arg4, arg5) View Source
expected_hash() View Source
The type of hash used to verify the signature
This should return :sha256
from_binary(binary) View Source
Deserialize a binary into a %FontMetrics{}
.
returns {:ok, font_metric}
from_binary!(binary) View Source
Deserialize a binary into a %FontMetrics{}
.
returns font_metric
max_box(pixels, font_metrics) View Source
Return a box that would hold the largest character in the font.
The response is scaled to the pixel size.
returns {x_min, y_min, x_max, y_max}
nearest_gap(source, pos, pixels, font_metrics, opts \\ []) View Source
Find the gap between to characters given an {x,y} coordinate
options
:kern
- account for Kerning - true or false
returns {character_number, x_position, line_number}
points_to_pixels(points) View Source
Transform point values into pixels
returns pixels
position_at(source, n, pixels, font_metric, opts \\ []) View Source
Returns the coordinates just before the given character number.
options
- :kern - account for Kerning - true or false
returns {x_position, line_number}
shorten(source, max_width, pixels, font_metrics, opts \\ []) View Source
Shorten a string to fit a given width
options
:kern
- account for Kerning - true or false:terminator
- add this string to the end of the shortened string. Defaults to "..."
returns string
supported?(codepoint, metrics) View Source
Checks if all the characters can be rendered by the font
returns true
or false
to_binary(metrics) View Source
Serialize a %FontMetrics{}
struct to a binary.
returns {:ok, binary}
to_binary!(metrics) View Source
Serialize a %FontMetrics{}
struct to a binary.
returns binary
width(source, pixels, font_metrics, kern \\ false) View Source
Measure the width of a string, scaled to a pixel size
returns width
wrap(source, max_width, pixels, font_metric, opts \\ []) View Source
Wraps a string to a given width by adding returns.
options
:indent
- indent wrapped lines by n spaces or a given string. examples:indent: 2
orindent: "___"
orindent: '...'
:kern
- account for Kerning - true or false
returns the wrapped string