Skip to content

Commit 6068337

Browse files
committed
Avoid q/Q bugs in e.g. Mac Preview
For now, just reset the font colour to black when we haven't been explicit.
1 parent 6f3bba1 commit 6068337

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

lib/mudbrick.ex

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,14 @@ defmodule Mudbrick do
6969
ContentStream.add(context, ContentStream.Td, tx: x, ty: y)
7070
end
7171

72-
defp latest_font_operation!(content_stream) do
73-
Enum.find(
74-
content_stream.value.operations,
75-
&match?(%ContentStream.Tf{}, &1)
76-
) || raise Font.NotSet, "No font chosen"
77-
end
78-
79-
def text({_doc, content_stream} = context, text, colour: {r, g, b}) do
72+
def text(context, text, colour: {r, g, b}) do
8073
context
81-
|> ContentStream.add(ContentStream.QPush, [])
8274
|> ContentStream.add(ContentStream.Rg, r: r, g: g, b: b)
83-
|> text(text)
84-
|> ContentStream.add(ContentStream.QPop, [])
85-
|> ContentStream.add(latest_font_operation!(content_stream))
75+
|> write_text(text)
8676
end
8777

88-
def text({_doc, content_stream} = context, text) do
89-
import ContentStream
90-
91-
tf = latest_font_operation!(content_stream)
92-
93-
[first_part | parts] = String.split(text, "\n")
94-
95-
context
96-
|> add(ContentStream.Tj, font: tf.font, text: first_part)
97-
|> then(fn context ->
98-
for part <- parts, reduce: context do
99-
acc ->
100-
add(acc, ContentStream.Apostrophe, font: tf.font, text: part)
101-
end
102-
end)
78+
def text(context, text) do
79+
text(context, text, colour: {0.0, 0.0, 0.0})
10380
end
10481

10582
def render({doc, _page}) do
@@ -127,4 +104,28 @@ defmodule Mudbrick do
127104
def join(list, separator) do
128105
Enum.map_join(list, separator, &Mudbrick.Object.from/1)
129106
end
107+
108+
defp write_text({_doc, content_stream} = context, text) do
109+
import ContentStream
110+
111+
tf = latest_font_operation!(content_stream)
112+
113+
[first_part | parts] = String.split(text, "\n")
114+
115+
context
116+
|> add(ContentStream.Tj, font: tf.font, text: first_part)
117+
|> then(fn context ->
118+
for part <- parts, reduce: context do
119+
acc ->
120+
add(acc, ContentStream.Apostrophe, font: tf.font, text: part)
121+
end
122+
end)
123+
end
124+
125+
defp latest_font_operation!(content_stream) do
126+
Enum.find(
127+
content_stream.value.operations,
128+
&match?(%ContentStream.Tf{}, &1)
129+
) || raise Font.NotSet, "No font chosen"
130+
end
130131
end

test/mudbrick/font_test.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ defmodule Mudbrick.FontTest do
3636
BT
3737
/F1 10 Tf
3838
12.0 TL
39+
0.0 0.0 0.0 rg
3940
(black and ) Tj
40-
q
4141
1.0 0.0 0.0 rg
4242
(red) Tj
4343
(text) '
44-
Q
45-
/F1 10 Tf
4644
ET
4745
"""
4846
end

test/mudbrick_test.exs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,19 @@ defmodule MudbrickTest do
9090
>>
9191
endobj
9292
6 0 obj
93-
<</Length 99
93+
<</Length 129
9494
>>
9595
stream
9696
BT
9797
/F2 100 Tf
9898
120.0 TL
9999
300 400 Td
100+
0.0 0.0 0.0 rg
100101
(hello, world!) Tj
101102
/F1 10 Tf
102103
12.0 TL
103104
0 -24 Td
105+
0.0 0.0 0.0 rg
104106
(a new line!) Tj
105107
ET
106108
endstream
@@ -120,13 +122,13 @@ defmodule MudbrickTest do
120122
0000000241 00000 n
121123
0000000345 00000 n
122124
0000000490 00000 n
123-
0000000638 00000 n
125+
0000000669 00000 n
124126
trailer
125127
<</Root 2 0 R
126128
/Size 8
127129
>>
128130
startxref
129-
716
131+
747
130132
%%EOF\
131133
"""
132134
end

0 commit comments

Comments
 (0)