Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 27, 2024
1 parent f8ad7e2 commit 89d8581
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/mudbrick.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ defmodule Mudbrick do

context
|> ContentStream.update_operations(fn ops ->
Enum.reverse(TextBlock.Output.from(text_block)) ++ ops
Enum.reverse(TextBlock.Output.from(text_block).operations) ++ ops
end)
end

Expand Down
66 changes: 27 additions & 39 deletions lib/mudbrick/text_block/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,19 @@ defmodule Mudbrick.TextBlock.Output do

# first line, first part
defp reduce_parts(output, %Line{parts: [part]}, _operator, :first_line) do
add_part(output, part, Tj)
Output.add_part(output, part, Tj)
end

# subsequent line, first part
defp reduce_parts(output, %Line{parts: [part]}, _operator, nil) do
add_part(output, part, Apostrophe)
Output.add_part(output, part, Apostrophe)
end

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

def add_part(output, part, operator) do
output
|> Output.with_font(
struct!(operator, font: part.font || output.font, text: part.text),
part
)
|> Output.colour(part.colour)
end
end

defmodule RightAlign do
Expand All @@ -76,20 +67,14 @@ defmodule Mudbrick.TextBlock.Output do
end

defp reduce_parts(output, %Line{parts: [part]}) do
add_part(output, part)
Output.add_part(output, part, Tj)
end

defp reduce_parts(output, %Line{parts: [part | parts]} = line) do
output
|> add_part(part)
|> Output.add_part(part, Tj)
|> reduce_parts(%{line | parts: parts})
end

def add_part(output, part) do
output
|> Output.with_font(%Tj{font: part.font || output.font, text: part.text}, part)
|> Output.colour(part.colour)
end
end

def from(
Expand All @@ -100,16 +85,13 @@ defmodule Mudbrick.TextBlock.Output do
position: {x, y}
} = tb
) do
output =
%__MODULE__{font: font, font_size: font_size}
|> end_block()
|> LeftAlign.reduce_lines(tb.lines)
|> add(%Td{tx: x, ty: y})
|> add(%TL{leading: leading(tb)})
|> add(%Tf{font: font, size: font_size})
|> start_block()

output.operations
%__MODULE__{font: font, font_size: font_size}
|> end_block()
|> LeftAlign.reduce_lines(tb.lines)
|> add(%Td{tx: x, ty: y})
|> add(%TL{leading: leading(tb)})
|> add(%Tf{font: font, size: font_size})
|> start_block()
end

def from(
Expand All @@ -119,21 +101,27 @@ defmodule Mudbrick.TextBlock.Output do
font_size: font_size
} = tb
) do
output =
%__MODULE__{font: font, font_size: font_size}
|> RightAlign.reduce_lines(tb.lines, fn output, text, line ->
right_offset(output, tb, text, line)
end)
|> add(%TL{leading: leading(tb)})
|> add(%Tf{font: font, size: font_size})

output.operations
%__MODULE__{font: font, font_size: font_size}
|> RightAlign.reduce_lines(tb.lines, fn output, text, line ->
right_offset(output, tb, text, line)
end)
|> add(%TL{leading: leading(tb)})
|> add(%Tf{font: font, size: font_size})
end

def add(%__MODULE__{} = output, op) do
Map.update!(output, :operations, &[op | &1])
end

def add_part(output, part, operator) do
output
|> with_font(
struct!(operator, font: part.font || output.font, text: part.text),
part
)
|> colour(part.colour)
end

def with_font(output, op, part) do
if part.font == nil or output.font == part.font do
add(output, op)
Expand Down
2 changes: 1 addition & 1 deletion test/mudbrick/text_block_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ defmodule Mudbrick.TextBlockTest do
franklin_regular_font = Map.fetch!(fonts, :c).value
block = f.(regular_font, bold_font, franklin_regular_font)

ops = Output.from(block)
ops = Output.from(block).operations

context
|> Mudbrick.ContentStream.put(operations: Enum.reverse(ops))
Expand Down

0 comments on commit 89d8581

Please sign in to comment.