Skip to content

Commit

Permalink
Fix right-aligned underline
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Nov 17, 2024
1 parent 97bc9d0 commit 6dff83b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/mudbrick/text_block/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,53 +92,53 @@ defmodule Mudbrick.TextBlock.Output do
output
|> leading(line)
|> reset_offset(x_offsetter.(line))
|> reduce_parts(line, TJ, :first_line, x_offsetter)
|> reduce_parts(line, TJ, :first_line, x_offsetter.(line))
|> offset(x_offsetter.(line))
end

defp reduce_lines(output, [line | lines], x_offsetter) do
output
|> leading(line)
|> reset_offset(x_offsetter.(line))
|> reduce_parts(line, TJ, nil, x_offsetter)
|> reduce_parts(line, TJ, nil, x_offsetter.(line))
|> offset(x_offsetter.(line))
|> reduce_lines(lines, x_offsetter)
end

defp reduce_parts(output, %Line{parts: []}, _operator, :first_line, _x_offsetter) do
defp reduce_parts(output, %Line{parts: []}, _operator, :first_line, _x_offset) do
output
end

defp reduce_parts(output, %Line{parts: [part]} = line, _operator, :first_line, x_offsetter) do
defp reduce_parts(output, %Line{parts: [part]}, _operator, :first_line, x_offset) do
output
|> add_part(part, TJ)
|> underline(part, x_offsetter.(line))
|> underline(part, x_offset)
end

defp reduce_parts(output, %Line{parts: []}, _operator, nil, _x_offsetter) do
defp reduce_parts(output, %Line{parts: []}, _operator, nil, _x_offset) do
output
|> add(%TJ{font: output.font, text: ""})
|> add(%TStar{})
end

defp reduce_parts(output, %Line{parts: [part]} = line, _operator, nil, x_offsetter) do
defp reduce_parts(output, %Line{parts: [part]}, _operator, nil, x_offset) do
output
|> add_part(part, TJ)
|> add(%TStar{})
|> underline(part, x_offsetter.(line))
|> underline(part, x_offset)
end

defp reduce_parts(
output,
%Line{parts: [part | parts]} = line,
operator,
line_kind,
x_offsetter
x_offset
) do
output
|> add_part(part, operator)
|> underline(part, x_offsetter.(line))
|> reduce_parts(%{line | parts: parts}, TJ, line_kind, x_offsetter)
|> underline(part, x_offset)
|> reduce_parts(%{line | parts: parts}, TJ, line_kind, x_offset)
end

defp leading(output, line) do
Expand All @@ -163,11 +163,11 @@ defmodule Mudbrick.TextBlock.Output do
end

defp underline_path(output, part, line_x_offset) do
{x, y} = output.position
{initial_x, initial_y} = output.position
{offset_x, offset_y} = part.left_offset

x = x + offset_x - line_x_offset
y = y + offset_y - part.font_size / 10
x = initial_x + offset_x - line_x_offset
y = initial_y + offset_y - part.font_size / 10

Path.new()
|> Path.move(to: {x, y})
Expand Down
20 changes: 20 additions & 0 deletions test/mudbrick/text_block_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,26 @@ defmodule Mudbrick.TextBlockTest do
|> operations()
end

test "underlines are correctly aligned" do
assert [
"q",
"-173.59199999999998 498.8 m",
"0 0 0 RG",
"1 w",
"-111.624 498.8 l",
"S",
"Q"
| _
] =
output(fn %{fonts: fonts} ->
TextBlock.new(font: fonts.regular, position: {0, 500}, align: :right)
|> TextBlock.write("not underlined ")
|> TextBlock.write("underlined ", underline: [width: 1])
|> TextBlock.write("not underlined again")
end)
|> operations()
end

test "inline font sizes affect alignment offset of whole line" do
assert offset_with_partial_font_size(50) < offset_with_partial_font_size(12)
end
Expand Down

0 comments on commit 6dff83b

Please sign in to comment.