Skip to content

Commit 3352dd7

Browse files
committed
Revert "Introduce TextObject"
This reverts commit 6f75b44. This turned out to be more trouble than it was worth, for now.
1 parent 6f75b44 commit 3352dd7

File tree

4 files changed

+96
-117
lines changed

4 files changed

+96
-117
lines changed

lib/mudbrick.ex

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
defmodule Mudbrick do
22
alias Mudbrick.ContentStream
3-
alias Mudbrick.ContentStream.TextObject
43
alias Mudbrick.Document
54
alias Mudbrick.Font
65
alias Mudbrick.Page
@@ -52,22 +51,22 @@ defmodule Mudbrick do
5251
{:ok, font} ->
5352
context
5453
|> add(
55-
TextObject.Tf,
54+
ContentStream.Tf,
5655
Keyword.put(
5756
opts,
5857
:font,
5958
font.value
6059
)
6160
)
62-
|> add(TextObject.TL, leading: Keyword.fetch!(opts, :size) * 1.2)
61+
|> add(ContentStream.TL, leading: Keyword.fetch!(opts, :size) * 1.2)
6362

6463
:error ->
6564
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
6665
end
6766
end
6867

6968
def text_position(context, x, y) do
70-
ContentStream.add(context, TextObject.Td, tx: x, ty: y)
69+
ContentStream.add(context, ContentStream.Td, tx: x, ty: y)
7170
end
7271

7372
def text(context, text, colour: {r, g, b}) do
@@ -114,24 +113,19 @@ defmodule Mudbrick do
114113
[first_part | parts] = String.split(text, "\n")
115114

116115
context
117-
|> add(TextObject.Tj, font: tf.font, text: first_part)
116+
|> add(ContentStream.Tj, font: tf.font, text: first_part)
118117
|> then(fn context ->
119118
for part <- parts, reduce: context do
120119
acc ->
121-
add(acc, TextObject.Apostrophe, font: tf.font, text: part)
120+
add(acc, ContentStream.Apostrophe, font: tf.font, text: part)
122121
end
123122
end)
124123
end
125124

126125
defp latest_font_operation!(content_stream) do
127-
Enum.find_value(
128-
content_stream.value.text_objects,
129-
fn text_object ->
130-
Enum.find(
131-
text_object.operations,
132-
&match?(%TextObject.Tf{}, &1)
133-
)
134-
end
126+
Enum.find(
127+
content_stream.value.operations,
128+
&match?(%ContentStream.Tf{}, &1)
135129
) || raise Font.NotSet, "No font chosen"
136130
end
137131
end

lib/mudbrick/content_stream.ex

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
defmodule Mudbrick.ContentStream do
22
@enforce_keys [:page]
3-
defstruct page: nil, text_objects: []
3+
defstruct page: nil, operations: []
44

5-
alias Mudbrick.ContentStream.TextObject
65
alias Mudbrick.Document
76

87
defmodule Rg do
98
defstruct [:r, :g, :b]
109

1110
defimpl Mudbrick.Object do
1211
def from(%Rg{r: r, g: g, b: b}) do
13-
["#{r} #{g} #{b} rg"]
12+
[[r, g, b] |> Enum.map_join(" ", &to_string/1), " rg"]
1413
end
1514
end
1615
end
@@ -31,33 +30,102 @@ defmodule Mudbrick.ContentStream do
3130
end
3231
end
3332

33+
defmodule Tf do
34+
defstruct [:font, :size]
35+
36+
defimpl Mudbrick.Object do
37+
def from(tf) do
38+
[
39+
Mudbrick.Object.from(tf.font.resource_identifier),
40+
" ",
41+
to_string(tf.size),
42+
" Tf"
43+
]
44+
end
45+
end
46+
end
47+
48+
defmodule Td do
49+
defstruct [:tx, :ty]
50+
51+
defimpl Mudbrick.Object do
52+
def from(td) do
53+
[td.tx, td.ty, "Td"]
54+
|> Enum.map(&to_string/1)
55+
|> Enum.intersperse(" ")
56+
end
57+
end
58+
end
59+
60+
defmodule TL do
61+
defstruct [:leading]
62+
63+
defimpl Mudbrick.Object do
64+
def from(tl) do
65+
[to_string(tl.leading), " TL"]
66+
end
67+
end
68+
end
69+
70+
defmodule Tj do
71+
defstruct font: nil,
72+
operator: "Tj",
73+
text: nil
74+
end
75+
76+
defmodule Apostrophe do
77+
defstruct font: nil,
78+
operator: "'",
79+
text: nil
80+
end
81+
82+
defimpl Mudbrick.Object, for: [Tj, Apostrophe] do
83+
def from(op) do
84+
if op.font.descendant do
85+
{glyph_ids_decimal, _positions} =
86+
OpenType.layout_text(op.font.parsed, op.text)
87+
88+
glyph_ids_hex = Enum.map(glyph_ids_decimal, &Mudbrick.to_hex/1)
89+
90+
["<", glyph_ids_hex, "> ", op.operator]
91+
else
92+
[Mudbrick.Object.from(op.text), " ", op.operator]
93+
end
94+
end
95+
end
96+
3497
def new(opts \\ []) do
3598
struct!(__MODULE__, opts)
3699
end
37100

38101
def add({doc, contents_obj}, operation) do
39102
Document.update(doc, contents_obj, fn contents ->
40-
Map.update!(contents, :text_objects, fn
41-
[] ->
42-
[TextObject.new(operations: [operation])]
43-
44-
[object] ->
45-
[Map.update!(object, :operations, &[operation | &1])]
103+
Map.update!(contents, :operations, fn operations ->
104+
[operation | operations]
46105
end)
47106
end)
48107
end
49108

50-
def add(context, mod, opts) do
51-
add(context, struct!(mod, opts))
109+
def add({doc, contents_obj}, mod, opts) do
110+
Document.update(doc, contents_obj, fn contents ->
111+
Map.update!(contents, :operations, fn operations ->
112+
[struct!(mod, opts) | operations]
113+
end)
114+
end)
52115
end
53116

54117
defimpl Mudbrick.Object do
55118
def from(content_stream) do
56-
Mudbrick.Object.from(
57-
Mudbrick.Stream.new(
58-
data: Enum.map_join(content_stream.text_objects, "\n", &Mudbrick.Object.from/1)
59-
)
119+
Mudbrick.Stream.new(
120+
data: [
121+
"BT\n",
122+
content_stream.operations
123+
|> Enum.reverse()
124+
|> Mudbrick.join("\n"),
125+
"\nET"
126+
]
60127
)
128+
|> Mudbrick.Object.from()
61129
end
62130
end
63131
end

lib/mudbrick/content_stream/text_object.ex

Lines changed: 0 additions & 83 deletions
This file was deleted.

test/mudbrick/content_stream_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Mudbrick.ContentStreamTest do
33

44
import Mudbrick
55

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

@@ -30,7 +30,7 @@ defmodule Mudbrick.ContentStreamTest do
3030
b\
3131
""")
3232

33-
assert List.last(content_stream.value.text_objects).operations
33+
assert content_stream.value.operations
3434
|> render(2) ==
3535
"""
3636
(a) Tj
@@ -53,7 +53,7 @@ defmodule Mudbrick.ContentStreamTest do
5353
b\
5454
""")
5555

56-
assert List.last(content_stream.value.text_objects).operations
56+
assert content_stream.value.operations
5757
|> render(2) ==
5858
"""
5959
<00A5> Tj
@@ -73,7 +73,7 @@ defmodule Mudbrick.ContentStreamTest do
7373
|> text_position(0, 700)
7474
|> text("CO₂")
7575

76-
[show_text_operation | _] = List.last(content_stream.value.text_objects).operations
76+
[show_text_operation | _] = content_stream.value.operations
7777

7878
assert %Tj{
7979
text: "CO₂",
@@ -97,7 +97,7 @@ defmodule Mudbrick.ContentStreamTest do
9797
|> text_position(0, 700)
9898
|> text("CO₂")
9999

100-
assert List.last(content_stream.value.text_objects).operations
100+
assert content_stream.value.operations
101101
|> render(1) ==
102102
"""
103103
<001100550174> Tj\

0 commit comments

Comments
 (0)