Skip to content

Commit 18602e4

Browse files
committed
Fix underline length of unkerned text
1 parent b723e0f commit 18602e4

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

lib/mudbrick/font.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,23 @@ defmodule Mudbrick.Font do
8282
end
8383

8484
@doc false
85-
def width(_font, _size, "") do
85+
def width(_font, _size, "", _opts) do
8686
0
8787
end
8888

89-
def width(font, size, text) do
90-
{_glyph_ids, positions} = OpenType.layout_text(font.parsed, text)
89+
def width(font, size, text, opts) do
90+
{glyph_ids, positions} = OpenType.layout_text(font.parsed, text)
9191

92-
for {_, _, _, width, _} <- positions, reduce: 0 do
93-
acc -> acc + width / 1000 * size
94-
end
92+
widths =
93+
if Keyword.get(opts, :auto_kern) do
94+
Enum.map(positions, fn {_, _, _, width, _} -> width end)
95+
else
96+
Enum.map(glyph_ids, &Enum.at(font.parsed.glyphWidths, &1))
97+
end
98+
99+
Enum.reduce(widths, 0, fn width, acc ->
100+
acc + width / 1000 * size
101+
end)
95102
end
96103

97104
@doc false

lib/mudbrick/text_block/line.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ defmodule Mudbrick.TextBlock.Line do
3030
Mudbrick.Font.width(
3131
part.font,
3232
part.font_size,
33-
part.text
33+
part.text,
34+
auto_kern: part.auto_kern
3435
)
3536
end
3637

test/mudbrick/text_block_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ defmodule Mudbrick.TextBlockTest do
159159
}
160160
] = block.lines
161161
end
162+
163+
test "length of line is different when kerned" do
164+
line = fn opts ->
165+
output(fn %{fonts: fonts} ->
166+
TextBlock.new(Keyword.merge(opts, font: fonts.regular))
167+
|> TextBlock.write("underlined", underline: [width: 1])
168+
end)
169+
|> operations()
170+
|> Enum.find(fn op -> String.ends_with?(op, " l") end)
171+
end
172+
173+
line_with_kerning = line.(auto_kern: true)
174+
line_without_kerning = line.(auto_kern: false)
175+
176+
assert line_with_kerning != line_without_kerning
177+
end
162178
end
163179

164180
describe "leading" do

0 commit comments

Comments
 (0)