From 3678a4cb1275084511d5aae0879f42703f4ba43d Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Wed, 30 Oct 2024 17:16:15 +0000 Subject: [PATCH] More docs and types --- lib/mudbrick.ex | 6 +++++- lib/mudbrick/text_block.ex | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/mudbrick.ex b/lib/mudbrick.ex index f606418..bd418fe 100644 --- a/lib/mudbrick.ex +++ b/lib/mudbrick.ex @@ -1,6 +1,6 @@ defmodule Mudbrick do @moduledoc """ - Top-level API for creating documents. + API for creating and exporting PDF documents. ## Example @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/lib/mudbrick/text_block.ex b/lib/mudbrick/text_block.ex index caee01e..b4c3696 100644 --- a/lib/mudbrick/text_block.ex +++ b/lib/mudbrick/text_block.ex @@ -1,6 +1,4 @@ defmodule Mudbrick.TextBlock do - @moduledoc false - @type alignment :: :left | :right @type option :: @@ -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(), @@ -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) @@ -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)