Skip to content

Commit

Permalink
More docs and types
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 30, 2024
1 parent 1e0b40a commit 3678a4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/mudbrick.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Mudbrick do
@moduledoc """
Top-level API for creating documents.
API for creating and exporting PDF documents.
## Example
Expand Down Expand Up @@ -206,6 +206,7 @@ defmodule Mudbrick do
...> |> text(["I am ", {"bold", font: :bold}], font: :regular, position: {200, 200})
"""

@spec text(context(), Mudbrick.TextBlock.write(), Mudbrick.TextBlock.options()) :: context()
def text(context, write_or_writes, opts \\ [])

def text({doc, _contents_obj} = context, writes, opts) when is_list(writes) do
Expand Down Expand Up @@ -246,6 +247,7 @@ defmodule Mudbrick do
@doc """
Produce `iodata` from the current document.
"""
@spec render(Document.t() | context()) :: iodata()
def render({doc, _page}) do
render(doc)
end
Expand Down Expand Up @@ -282,6 +284,7 @@ defmodule Mudbrick do
iex> Mudbrick.compress(["hi", "there", ["you"]])
[<<120, 156, 203, 200, 44, 201, 72, 45, 74, 173, 204, 47, 5, 0, 23, 45, 4, 71>>]
"""
@spec compress(iodata()) :: iodata()
def compress(data) do
z = :zlib.open()
:ok = :zlib.deflateInit(z)
Expand All @@ -299,6 +302,7 @@ defmodule Mudbrick do
iex> Mudbrick.decompress([<<120, 156, 203, 200, 44, 201, 72, 45, 74, 173, 204, 47, 5, 0, 23, 45, 4, 71>>])
["hithereyou"]
"""
@spec decompress(iodata()) :: iodata()
def decompress(data) do
z = :zlib.open()
:zlib.inflateInit(z)
Expand Down
18 changes: 16 additions & 2 deletions lib/mudbrick/text_block.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule Mudbrick.TextBlock do
@moduledoc false

@type alignment :: :left | :right

@type option ::
Expand All @@ -12,6 +10,20 @@ defmodule Mudbrick.TextBlock do

@type options :: [option()]

@type part_option ::
{:font, atom()}
| {:font_size, number()}
| {:leading, number()}

@type part_options :: [part_option()]

@type write_tuple :: {String.t(), part_options()}

@type write ::
String.t()
| write_tuple()
| list(write_tuple())

@type t :: %__MODULE__{
align: alignment(),
font: atom(),
Expand All @@ -30,6 +42,7 @@ defmodule Mudbrick.TextBlock do

alias Mudbrick.TextBlock.Line

@doc false
@spec new(options()) :: t()
def new(opts \\ []) do
block = struct!(__MODULE__, opts)
Expand All @@ -43,6 +56,7 @@ defmodule Mudbrick.TextBlock do
end)
end

@doc false
def write(tb, text, opts \\ []) do
line_texts = String.split(text, "\n")
opts = Keyword.put_new(opts, :leading, tb.leading)
Expand Down

0 comments on commit 3678a4c

Please sign in to comment.