Skip to content

Commit 1318b6f

Browse files
committed
Convert newlines to ' operator
Set leading to 120% of font size for now.
1 parent 7cd93fd commit 1318b6f

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

lib/mudbrick.ex

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,39 @@ defmodule Mudbrick do
7474
_ -> false
7575
end)
7676

77-
if latest_font_setting.font.descendant do
77+
context =
7878
ContentStream.add(
7979
context,
80-
ContentStream.Tj,
81-
font: latest_font_setting.font,
82-
text: text
80+
ContentStream.TL,
81+
leading: latest_font_setting.size * 1.2
8382
)
83+
84+
if latest_font_setting.font.descendant do
85+
text_parts = String.split(text, "\n")
86+
87+
{_, context} =
88+
for part <- text_parts, reduce: {true, context} do
89+
{true, acc} ->
90+
{false,
91+
ContentStream.add(
92+
acc,
93+
ContentStream.Tj,
94+
font: latest_font_setting.font,
95+
text: part
96+
)}
97+
98+
{false, acc} ->
99+
{false,
100+
ContentStream.add(
101+
acc,
102+
ContentStream.Tj,
103+
font: latest_font_setting.font,
104+
operator: "'",
105+
text: part
106+
)}
107+
end
108+
109+
context
84110
else
85111
ContentStream.add(
86112
context,

lib/mudbrick/content_stream.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ defmodule Mudbrick.ContentStream do
2424
end
2525
end
2626

27+
defmodule TL do
28+
defstruct [:leading]
29+
30+
defimpl Mudbrick.Object do
31+
def from(tl) do
32+
[to_string(tl.leading), " TL"]
33+
end
34+
end
35+
end
36+
2737
defmodule Tj do
28-
defstruct [:font, :text]
38+
defstruct font: nil,
39+
operator: "Tj",
40+
text: nil
2941

3042
defimpl Mudbrick.Object do
3143
def from(tj) do
@@ -42,9 +54,9 @@ defmodule Mudbrick.ContentStream do
4254
|> String.pad_leading(4, "0")
4355
end)
4456

45-
["<", glyph_ids_hex, "> Tj"]
57+
["<", glyph_ids_hex, "> #{tj.operator}"]
4658
else
47-
[Mudbrick.Object.from(tj.text), " Tj"]
59+
[Mudbrick.Object.from(tj.text), " #{tj.operator}"]
4860
end
4961
end
5062
end

test/mudbrick/content_stream_test.exs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,35 @@ defmodule Mudbrick.ContentStreamTest do
33

44
import Mudbrick
55

6-
alias Mudbrick.ContentStream
6+
alias Mudbrick.ContentStream.Tj
77
alias Mudbrick.Font
88
alias Mudbrick.Indirect
99
alias Mudbrick.Object
1010

1111
@font_data System.fetch_env!("FONT_LIBRE_BODONI_REGULAR") |> File.read!()
1212

13+
test "linebreaks are converted to the ' operator" do
14+
{_doc, content_stream} =
15+
new()
16+
|> page(
17+
size: :letter,
18+
fonts: %{bodoni: [file: @font_data]}
19+
)
20+
|> contents()
21+
|> font(:bodoni, size: 10)
22+
|> text_position(0, 700)
23+
|> text("""
24+
a
25+
b\
26+
""")
27+
28+
[apostrophe, tj, leading | _] = content_stream.value.operations
29+
30+
assert Object.from(leading) |> to_string() == "12.0 TL"
31+
assert Object.from(tj) |> to_string() == "<00A5> Tj"
32+
assert Object.from(apostrophe) |> to_string() == "<00B4> '"
33+
end
34+
1335
test "font is assigned to the operator struct when font descendant present" do
1436
{_doc, content_stream} =
1537
new()
@@ -24,7 +46,7 @@ defmodule Mudbrick.ContentStreamTest do
2446

2547
[show_text_operation | _] = content_stream.value.operations
2648

27-
assert %ContentStream.Tj{
49+
assert %Tj{
2850
text: "CO₂",
2951
font: %Font{
3052
name: :"LibreBodoni-Regular",

test/mudbrick_test.exs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ defmodule MudbrickTest do
3737
}
3838
)
3939
|> contents()
40-
|> font(:helvetica, size: 24)
40+
|> font(:helvetica, size: 100)
4141
|> text_position(300, 400)
4242
|> text("hello, world!")
43-
|> font(:courier, size: 12)
43+
|> font(:courier, size: 10)
4444
|> text_position(0, -24)
4545
|> text("a new line!")
4646
|> page(size: :a4)
@@ -86,15 +86,17 @@ defmodule MudbrickTest do
8686
>>
8787
endobj
8888
6 0 obj
89-
<</Length 81
89+
<</Length 99
9090
>>
9191
stream
9292
BT
93-
/F2 24 Tf
93+
/F2 100 Tf
9494
300 400 Td
95+
120.0 TL
9596
(hello, world!) Tj
96-
/F1 12 Tf
97+
/F1 10 Tf
9798
0 -24 Td
99+
12.0 TL
98100
(a new line!) Tj
99101
ET
100102
endstream
@@ -114,13 +116,13 @@ defmodule MudbrickTest do
114116
0000000241 00000 n
115117
0000000345 00000 n
116118
0000000490 00000 n
117-
0000000620 00000 n
119+
0000000638 00000 n
118120
trailer
119121
<</Root 2 0 R
120122
/Size 8
121123
>>
122124
startxref
123-
698
125+
716
124126
%%EOF\
125127
"""
126128
end

0 commit comments

Comments
 (0)