Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/font_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ defmodule FontMetrics do
defp do_position_at(line, n, cp_metrics, kerning, kern, k_next \\ 0, line_no \\ 0, width \\ 0)

defp do_position_at('', _, _, _, _, _, line_no, width), do: {width, line_no}
defp do_position_at(_, -1, _, _, _, _, line_no, width), do: {width, line_no}
defp do_position_at(_, p, _, _, _, _, line_no, width) when p <= 0, do: {width, line_no}

# handle newlines
defp do_position_at([10 | cps], n, cp_metrics, kerning, kern, _, line_no, _) do
Expand Down
40 changes: 34 additions & 6 deletions test/font_metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,49 @@ defmodule FontMetricsTest do
test "position_at works with a simple string" do
string = "PANCAKE breafasts are yummy"
{w, 0} = FontMetrics.position_at(string, 8, 22, @roboto_metrics)
assert trunc(w) == 116
assert trunc(w) == 104
{w, 0} = FontMetrics.position_at(string, 8, 22, @bitter_metrics)
assert trunc(w) == 123
assert trunc(w) == 110
{w, 0} = FontMetrics.position_at(string, 8, 22, @bitter_metrics, kern: true)
assert trunc(w) == 121
assert trunc(w) == 108
end

test "position_at returns zero length at position 0" do
string = "PANCAKE breafasts are yummy"
{w, 0} = FontMetrics.position_at(string, 0, 22, @roboto_metrics)
assert w == 0
end

test "with just one character, different positions in the string are multiples of the same width" do
string = "AAAAAAAAAA" # 10 chars long
{w, 0} = FontMetrics.position_at(string, 1, 22, @roboto_metrics)
{five_w, 0} = FontMetrics.position_at(string, 5, 22, @roboto_metrics)
assert 5*w == five_w
{ten_w, 0} = FontMetrics.position_at(string, 10, 22, @roboto_metrics)
assert 2*five_w == ten_w
end

test "the length of an empty string is zero" do
{w1, 0} = FontMetrics.position_at("", 0, 22, @roboto_metrics)
{w2, 0} = FontMetrics.position_at("", 1, 22, @roboto_metrics)
assert w1 == 0
assert w1 == w2
end

test "position one returns the same length as a one-character long string" do
{w1, 0} = FontMetrics.position_at("a", 1, 22, @roboto_metrics)
{w2, 0} = FontMetrics.position_at("aaaa", 1, 22, @roboto_metrics)
assert w1 == w2
end

test "position_at works with a multiline string" do
string = "PANCAKE breafasts\nPANCAKE are yummy"
{w, 1} = FontMetrics.position_at(string, 25, 22, @roboto_metrics)
assert trunc(w) == 104
assert trunc(w) == 98
{w, 1} = FontMetrics.position_at(string, 25, 22, @bitter_metrics)
assert trunc(w) == 110
assert trunc(w) == 105
{w, 1} = FontMetrics.position_at(string, 25, 22, @bitter_metrics, kern: true)
assert trunc(w) == 108
assert trunc(w) == 103
end

# ============================================================================
Expand Down