Skip to content

Commit 210aedf

Browse files
committed
Always set leading on lines
So we can easily keep track of offset required for underlining etc.
1 parent 0f2e737 commit 210aedf

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

lib/mudbrick/text_block.ex

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,36 @@ defmodule Mudbrick.TextBlock do
6565
def write(tb, text, opts \\ []) do
6666
line_texts = String.split(text, "\n")
6767

68+
text_block_opts = [
69+
colour: tb.colour,
70+
font_size: tb.font_size,
71+
font: tb.font,
72+
leading: tb.leading
73+
]
74+
6875
opts =
6976
Keyword.merge(
70-
[
71-
colour: tb.colour,
72-
font_size: tb.font_size,
73-
font: tb.font,
74-
leading: tb.leading
75-
],
77+
text_block_opts,
7678
opts,
7779
&prefer_lhs_over_nil/3
7880
)
7981

8082
Map.update!(tb, :lines, fn
8183
[] ->
82-
add_texts([], line_texts, opts)
84+
add_texts([], line_texts, opts, text_block_opts)
8385

8486
existing_lines ->
8587
case line_texts do
8688
# \n at beginning of new line
8789
["" | new_line_texts] ->
8890
existing_lines
89-
|> add_texts(new_line_texts, opts)
91+
|> add_texts(new_line_texts, opts, text_block_opts)
9092

9193
# didn't start with \n, so first part belongs to previous line
9294
[first_new_line_text | new_line_texts] ->
9395
existing_lines
9496
|> update_previous_line(first_new_line_text, opts)
95-
|> add_texts(new_line_texts, opts)
97+
|> add_texts(new_line_texts, opts, text_block_opts)
9698
end
9799
end)
98100
end
@@ -104,9 +106,14 @@ defmodule Mudbrick.TextBlock do
104106
]
105107
end
106108

107-
defp add_texts(existing_lines, new_line_texts, opts) do
109+
defp add_texts(existing_lines, new_line_texts, opts, opts_for_empty_lines) do
108110
for text <- new_line_texts, reduce: existing_lines do
109-
acc -> [Line.wrap(text, opts) | acc]
111+
acc ->
112+
if text == "" do
113+
[Line.wrap(text, opts_for_empty_lines) | acc]
114+
else
115+
[Line.wrap(text, opts) | acc]
116+
end
110117
end
111118
end
112119

lib/mudbrick/text_block/line.ex

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Mudbrick.TextBlock.Line do
22
@moduledoc false
33

4+
@enforce_keys [:leading, :parts]
45
defstruct leading: nil, parts: []
56

67
defmodule Part do
@@ -25,24 +26,24 @@ defmodule Mudbrick.TextBlock.Line do
2526
)
2627
end
2728

28-
def wrap(text, opts) when text != "" do
29+
def new(text, opts) when text != "" do
2930
struct(__MODULE__, Keyword.put_new(opts, :text, text))
3031
end
3132
end
3233

33-
def wrap("", _prefer_options_from_subsequent_appends) do
34-
%__MODULE__{}
34+
def wrap("", opts) do
35+
struct(__MODULE__, opts)
3536
end
3637

3738
def wrap(text, opts) do
38-
struct(%__MODULE__{parts: [Part.wrap(text, opts)]}, opts)
39+
struct(__MODULE__, Keyword.put(opts, :parts, [Part.new(text, opts)]))
3940
end
4041

4142
def append(line, text, opts) do
42-
line = Map.update!(line, :parts, &[Part.wrap(text, opts) | &1])
43+
line = Map.update!(line, :parts, &[Part.new(text, opts) | &1])
4344
new_leading = Keyword.fetch!(opts, :leading)
4445

45-
if line.leading == nil or new_leading > line.leading do
46+
if new_leading > line.leading do
4647
Map.put(line, :leading, new_leading)
4748
else
4849
line
@@ -51,9 +52,7 @@ defmodule Mudbrick.TextBlock.Line do
5152

5253
def width(line) do
5354
for part <- line.parts, reduce: 0.0 do
54-
acc ->
55-
acc +
56-
Part.width(part)
55+
acc -> acc + Part.width(part)
5756
end
5857
end
5958
end

0 commit comments

Comments
 (0)