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

Link to this function

ascent(pixels, font_metrics) View Source

Get the ascent of the font scaled to the pixel height

returns ascent

Link to this function

descent(pixels, font_metrics) View Source

Get the descent of the font scaled to the pixel height

returns descent

Link to this function

do_shorten(source, max_width, scale, arg4, arg5) View Source

The type of hash used to verify the signature

This should return :sha256

Deserialize a binary into a %FontMetrics{}.

returns {:ok, font_metric}

Deserialize a binary into a %FontMetrics{}.

returns font_metric

Link to this function

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}

Link to this function

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}

Link to this function

points_to_pixels(points) View Source

Transform point values into pixels

returns pixels

Link to this function

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}

Link to this function

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

Link to this function

supported?(codepoint, metrics) View Source

Checks if all the characters can be rendered by the font

returns true or false

Serialize a %FontMetrics{} struct to a binary.

returns {:ok, binary}

Serialize a %FontMetrics{} struct to a binary.

returns binary

Link to this function

width(source, pixels, font_metrics, kern \\ false) View Source

Measure the width of a string, scaled to a pixel size

returns width

Link to this function

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 or indent: "___" or indent: '...'
  • :kern - account for Kerning - true or false

returns the wrapped string