Skip to content

Commit

Permalink
Spread content stream modules into files
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 25, 2024
1 parent db96bf3 commit 5e20a2a
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 166 deletions.
155 changes: 3 additions & 152 deletions lib/mudbrick/content_stream.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule Mudbrick.ContentStream do
@moduledoc false

alias Mudbrick.ContentStream.ET
alias Mudbrick.ContentStream.{BT, ET}
alias Mudbrick.ContentStream.Td
alias Mudbrick.ContentStream.Tf
alias Mudbrick.ContentStream.{Tj, Apostrophe}
alias Mudbrick.Document
alias Mudbrick.Font

Expand All @@ -15,161 +17,10 @@ defmodule Mudbrick.ContentStream do
current_tf: nil,
current_tl: nil

defmodule Cm do
@moduledoc false
defstruct scale: {0, 0},
skew: {0, 0},
position: {0, 0}

defimpl Mudbrick.Object do
def from(%Cm{
scale: {x_scale, y_scale},
skew: {x_skew, y_skew},
position: {x_translate, y_translate}
}) do
[
Mudbrick.join([x_scale, x_skew, y_skew, y_scale, x_translate, y_translate]),
" cm"
]
end
end
end

defmodule QPush do
@moduledoc false
defstruct []

defimpl Mudbrick.Object do
def from(_), do: ["q"]
end
end

defmodule QPop do
@moduledoc false
defstruct []

defimpl Mudbrick.Object do
def from(_), do: ["Q"]
end
end

defmodule Do do
@moduledoc false
defstruct [:image]

defimpl Mudbrick.Object do
def from(operator) do
[
Mudbrick.Object.from(operator.image.resource_identifier),
" Do"
]
end
end
end

defmodule BT do
@moduledoc false
defstruct []

def open({_doc, content_stream} = context) do
context
|> Mudbrick.ContentStream.add(%BT{})
|> Mudbrick.ContentStream.add(content_stream.value.current_base_td)
|> Mudbrick.ContentStream.add(content_stream.value.current_tf)
|> Mudbrick.ContentStream.add(content_stream.value.current_tl)
end

defimpl Mudbrick.Object do
def from(_), do: ["BT"]
end
end

defmodule Rg do
@moduledoc false
defstruct [:r, :g, :b]

def new(opts) do
if Enum.any?(opts, fn {_k, v} ->
v < 0 or v > 1
end) do
raise Mudbrick.ContentStream.InvalidColour,
"tuple must be made of floats or integers between 0 and 1"
end

struct!(__MODULE__, opts)
end

defimpl Mudbrick.Object do
def from(%Rg{r: r, g: g, b: b}) do
[[r, g, b] |> Enum.map_join(" ", &to_string/1), " rg"]
end
end
end

defmodule InvalidColour do
defexception [:message]
end

defmodule Tf do
@moduledoc false
defstruct [:font, :size]

def current!(content_stream) do
content_stream.value.current_tf || raise Mudbrick.Font.NotSet, "No font chosen"
end

defimpl Mudbrick.Object do
def from(tf) do
[
Mudbrick.Object.from(tf.font.resource_identifier),
" ",
to_string(tf.size),
" Tf"
]
end
end
end

defmodule TL do
@moduledoc false
defstruct [:leading]

defimpl Mudbrick.Object do
def from(tl) do
[to_string(tl.leading), " TL"]
end
end
end

defmodule Tj do
@moduledoc false
defstruct font: nil,
operator: "Tj",
text: nil
end

defmodule Apostrophe do
@moduledoc false
defstruct font: nil,
operator: "'",
text: nil
end

defimpl Mudbrick.Object, for: [Tj, Apostrophe] do
def from(op) do
if op.font.descendant && String.length(op.text) > 0 do
{glyph_ids_decimal, _positions} =
OpenType.layout_text(op.font.parsed, op.text)

glyph_ids_hex = Enum.map(glyph_ids_decimal, &Mudbrick.to_hex/1)

["<", glyph_ids_hex, "> ", op.operator]
else
[Mudbrick.Object.from(op.text), " ", op.operator]
end
end
end

def new(opts \\ []) do
struct!(__MODULE__, opts)
end
Expand Down
31 changes: 31 additions & 0 deletions lib/mudbrick/content_stream/bt_et.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Mudbrick.ContentStream.BT do
@moduledoc false
defstruct []

def open({_doc, content_stream} = context) do
context
|> Mudbrick.ContentStream.add(%__MODULE__{})
|> Mudbrick.ContentStream.add(content_stream.value.current_base_td)
|> Mudbrick.ContentStream.add(content_stream.value.current_tf)
|> Mudbrick.ContentStream.add(content_stream.value.current_tl)
end

defimpl Mudbrick.Object do
def from(_), do: ["BT"]
end
end

defmodule Mudbrick.ContentStream.ET do
@moduledoc false
defstruct []

def remove(context) do
Mudbrick.ContentStream.update_operations(context, fn [%__MODULE__{} | operations] ->
operations
end)
end

defimpl Mudbrick.Object do
def from(_), do: ["ET"]
end
end
19 changes: 19 additions & 0 deletions lib/mudbrick/content_stream/cm.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Mudbrick.ContentStream.Cm do
@moduledoc false
defstruct scale: {0, 0},
skew: {0, 0},
position: {0, 0}

defimpl Mudbrick.Object do
def from(%Mudbrick.ContentStream.Cm{
scale: {x_scale, y_scale},
skew: {x_skew, y_skew},
position: {x_translate, y_translate}
}) do
[
Mudbrick.join([x_scale, x_skew, y_skew, y_scale, x_translate, y_translate]),
" cm"
]
end
end
end
13 changes: 13 additions & 0 deletions lib/mudbrick/content_stream/do.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Mudbrick.ContentStream.Do do
@moduledoc false
defstruct [:image]

defimpl Mudbrick.Object do
def from(operator) do
[
Mudbrick.Object.from(operator.image.resource_identifier),
" Do"
]
end
end
end
14 changes: 0 additions & 14 deletions lib/mudbrick/content_stream/et.ex

This file was deleted.

17 changes: 17 additions & 0 deletions lib/mudbrick/content_stream/q.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Mudbrick.ContentStream.QPush do
@moduledoc false
defstruct []

defimpl Mudbrick.Object do
def from(_), do: ["q"]
end
end

defmodule Mudbrick.ContentStream.QPop do
@moduledoc false
defstruct []

defimpl Mudbrick.Object do
def from(_), do: ["Q"]
end
end
21 changes: 21 additions & 0 deletions lib/mudbrick/content_stream/rg.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Mudbrick.ContentStream.Rg do
@moduledoc false
defstruct [:r, :g, :b]

def new(opts) do
if Enum.any?(opts, fn {_k, v} ->
v < 0 or v > 1
end) do
raise Mudbrick.ContentStream.InvalidColour,
"tuple must be made of floats or integers between 0 and 1"
end

struct!(__MODULE__, opts)
end

defimpl Mudbrick.Object do
def from(%Mudbrick.ContentStream.Rg{r: r, g: g, b: b}) do
[[r, g, b] |> Enum.map_join(" ", &to_string/1), " rg"]
end
end
end
19 changes: 19 additions & 0 deletions lib/mudbrick/content_stream/tf.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Mudbrick.ContentStream.Tf do
@moduledoc false
defstruct [:font, :size]

def current!(content_stream) do
content_stream.value.current_tf || raise Mudbrick.Font.NotSet, "No font chosen"
end

defimpl Mudbrick.Object do
def from(tf) do
[
Mudbrick.Object.from(tf.font.resource_identifier),
" ",
to_string(tf.size),
" Tf"
]
end
end
end
28 changes: 28 additions & 0 deletions lib/mudbrick/content_stream/tj_apostrophe.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Mudbrick.ContentStream.Tj do
@moduledoc false
defstruct font: nil,
operator: "Tj",
text: nil
end

defmodule Mudbrick.ContentStream.Apostrophe do
@moduledoc false
defstruct font: nil,
operator: "'",
text: nil
end

defimpl Mudbrick.Object, for: [Mudbrick.ContentStream.Tj, Mudbrick.ContentStream.Apostrophe] do
def from(op) do
if op.font.descendant && String.length(op.text) > 0 do
{glyph_ids_decimal, _positions} =
OpenType.layout_text(op.font.parsed, op.text)

glyph_ids_hex = Enum.map(glyph_ids_decimal, &Mudbrick.to_hex/1)

["<", glyph_ids_hex, "> ", op.operator]
else
[Mudbrick.Object.from(op.text), " ", op.operator]
end
end
end
10 changes: 10 additions & 0 deletions lib/mudbrick/content_stream/tl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Mudbrick.ContentStream.TL do
@moduledoc false
defstruct [:leading]

defimpl Mudbrick.Object do
def from(tl) do
[to_string(tl.leading), " TL"]
end
end
end

0 comments on commit 5e20a2a

Please sign in to comment.