Skip to content

Commit

Permalink
TextBlock output / alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 27, 2024
1 parent eebbe9d commit d79994a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 99 deletions.
197 changes: 110 additions & 87 deletions lib/mudbrick/text_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Mudbrick.TextBlock do
end

defmodule Output do
defstruct operations: []
defstruct font: nil, operations: []

alias Mudbrick.ContentStream.{BT, ET}
alias Mudbrick.ContentStream.Rg
Expand All @@ -33,6 +33,79 @@ defmodule Mudbrick.TextBlock do
alias Mudbrick.ContentStream.TL
alias Mudbrick.Font

defmodule LeftAlign do
def reduce_lines(output, [line]) do
reduce_parts(output, line, Tj, :first_line)
end

def reduce_lines(output, [line | lines]) do
output
|> reduce_parts(line, Tj, nil)
|> reduce_lines(lines)
end

# first line, first part
defp reduce_parts(output, %Line{parts: [part]}, _operator, :first_line) do
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)
end

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

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

defmodule RightAlign do
defp text(line) do
Enum.map_join(line.parts, "", & &1.text)
end

def reduce_lines(output, [line], measure) do
output
|> Output.end_block()
|> reduce_parts(line)
|> measure.(text(line), 1)
|> Output.start_block()
end

def reduce_lines(output, [line | lines], measure) do
output
|> Output.end_block()
|> reduce_parts(line)
|> measure.(text(line), length(lines) + 1)
|> Output.start_block()
|> reduce_lines(lines, measure)
end

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

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

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

def from(
%Mudbrick.TextBlock{
align: :left,
Expand All @@ -42,49 +115,13 @@ defmodule Mudbrick.TextBlock do
} = tb
) do
output =
%Output{}
|> start_block()
|> add(%Tf{font: font, size: font_size})
|> add(%TL{leading: leading(tb)})
|> add(%Td{tx: x, ty: y})
|> then(fn output ->
[%Line{parts: first_parts} | other_lines] = tb.lines |> Enum.reverse()

output
|> then(
&List.foldr(first_parts, &1, fn %Line.Part{text: text, colour: colour}, acc ->
acc
|> colour(colour)
|> add(%Tj{font: font, text: text})
end)
)
|> then(fn output ->
for %Line{parts: parts} <- other_lines, reduce: output do
acc ->
{_, acc} =
List.foldr(parts, {false, acc}, fn
%Line.Part{text: text, colour: colour}, {false, inner_acc} ->
{
true,
inner_acc
|> colour(colour)
|> add(%Apostrophe{font: font, text: text})
}

%Line.Part{text: text, colour: colour}, {true, inner_acc} ->
{
true,
inner_acc
|> colour(colour)
|> add(%Tj{font: font, text: text})
}
end)

acc
end
end)
end)
%Output{font: font}
|> 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
end
Expand All @@ -93,51 +130,48 @@ defmodule Mudbrick.TextBlock do
%Mudbrick.TextBlock{
align: :right,
font: font,
font_size: font_size
font_size: font_size,
position: {x, y}
} = tb
) do
output =
%Output{}
|> start_block()
|> add(%Tf{font: font, size: font_size})
|> add(%TL{leading: leading(tb)})
|> then(fn output ->
[%{parts: [first_part]} | rest] = Enum.reverse(tb.lines)

output
|> right_offset(tb, first_part.text, 1)
|> add(%Tj{font: font, text: first_part.text})
|> end_block()
|> then(fn output ->
{_line, output} =
for %{parts: [part]} <- rest, reduce: {2, output} do
{line, acc} ->
{
line + 1,
acc
|> start_block()
|> right_offset(tb, part.text, line)
|> add(%Tj{font: font, text: part.text})
|> end_block()
}
end

output
end)
%Output{font: font}
|> RightAlign.reduce_lines(tb.lines, fn output, text, line ->
right_offset(output, tb, text, line)
end)
|> add(%Td{tx: x, ty: y})
|> add(%TL{leading: leading(tb)})
|> add(%Tf{font: font, size: font_size})
|> start_block()

output.operations
end

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

def colour(output, {r, g, b}) do
new_colour = %Rg{r: r, g: g, b: b}
latest_colour = Enum.find(output.operations, &match?(%Rg{}, &1)) || %Rg{r: 0, g: 0, b: 0}

if latest_colour == new_colour do
remove(output, new_colour)
else
output
end
|> add(new_colour)
end

def start_block(output) do
add(output, %BT{})
end

defp end_block(output) do
def end_block(output) do
add(output, %ET{})
end

defp right_offset(output, tb, text, line) do
def right_offset(output, tb, text, line) do
n = line - 1
{x, y} = tb.position

Expand All @@ -147,19 +181,8 @@ defmodule Mudbrick.TextBlock do
})
end

defp colour(output, {r, g, b}) do
new_colour = %Rg{r: r, g: g, b: b}
latest_colour = Enum.find(output.operations, &match?(%Rg{}, &1)) || %Rg{r: 0, g: 0, b: 0}

if latest_colour != new_colour do
add(output, new_colour)
else
output
end
end

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

defp leading(tb) do
Expand Down
50 changes: 38 additions & 12 deletions test/mudbrick/text_block_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule Mudbrick.TextBlockTest do
"/F1 10 Tf",
"12.0 TL",
"400 500 Td",
"0 0 0 rg",
"<014C010F0116011D01B700ED00D900F400C0> Tj",
"<011600C000B500FC00F400BB01B700ED00D900F400C0> '",
"() '",
Expand All @@ -86,6 +87,7 @@ defmodule Mudbrick.TextBlockTest do
"/F1 10 Tf",
"12.0 TL",
"400 500 Td",
"0 0 0 rg",
"<00A5> Tj",
"1 0 0 rg",
"<00B4> Tj",
Expand Down Expand Up @@ -118,20 +120,31 @@ defmodule Mudbrick.TextBlockTest do
"BT",
"/F1 10 Tf",
"12.0 TL",
"400 500 Td",
"BT",
"384.82 500.0 Td",
"<00A500A500A5> Tj",
"0 0 0 rg",
"<00A5> Tj",
"1 0 0 rg",
"<00A500A5> Tj",
"ET",
"BT",
"379.42 488.0 Td",
"() Tj",
"0 0 0 rg",
"<013801380138> Tj",
"ET",
"BT",
"306.48 476.0 Td",
"<00880055008800550088005500880055008800550088> Tj",
"314.3 476.0 Td",
"<008800550088> Tj",
"0 1 0 rg",
"<0088005500880055008800550088> Tj",
"ET",
"BT",
"400 464.0 Td",
"() Tj",
"0 0 0 rg",
"() Tj",
"ET",
"BT",
"390.74 452.0 Td",
Expand All @@ -145,10 +158,24 @@ defmodule Mudbrick.TextBlockTest do
position: {400, 500},
align: :right
)
|> Mudbrick.TextBlock.write("a")
|> Mudbrick.TextBlock.write(
"""
aa
""",
colour: {1, 0, 0}
)
|> Mudbrick.TextBlock.write("""
aaa
www
WOWOWOWOWOW
WOW\
""")
|> Mudbrick.TextBlock.write(
"""
WOWOWOW
""",
colour: {0, 1, 0}
)
|> Mudbrick.TextBlock.write("""
hi\
""")
Expand All @@ -157,11 +184,8 @@ defmodule Mudbrick.TextBlockTest do
end
end

defp operations(tb) do
tb
|> Output.from()
|> Enum.reverse()
|> Enum.map(&Mudbrick.TestHelper.show/1)
defp operations(ops) do
Enum.map(ops, &Mudbrick.TestHelper.show/1)
end

defp output(f) when is_function(f) do
Expand All @@ -179,12 +203,14 @@ defmodule Mudbrick.TextBlockTest do

block = f.(contents_obj.value.current_tf.font)

ops = Output.from(block)

context
|> Mudbrick.ContentStream.put(operations: Output.from(block))
|> Mudbrick.ContentStream.put(operations: Enum.reverse(ops))
|> render()
|> output()

block
ops
end

defp output(chain) do
Expand Down

0 comments on commit d79994a

Please sign in to comment.