Skip to content

Commit e638782

Browse files
committed
More docs, add A3 page size.
1 parent 81cce04 commit e638782

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

lib/mudbrick/page.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Mudbrick.Page do
88
@dpi 72
99

1010
@page_sizes %{
11+
a3: {11.7 * @dpi, 16.5 * @dpi},
1112
a4: {8.3 * @dpi, 11.7 * @dpi},
1213
letter: {8.5 * @dpi, 11 * @dpi}
1314
}
@@ -17,6 +18,20 @@ defmodule Mudbrick.Page do
1718
struct!(__MODULE__, opts)
1819
end
1920

21+
@doc """
22+
Returns predefined page sizes in points.
23+
24+
## Examples
25+
26+
iex> Mudbrick.Page.size(:a4)
27+
{597.6, 842.4}
28+
29+
iex> Mudbrick.Page.size(:a3)
30+
{842.4, 1188.0}
31+
32+
iex> Mudbrick.Page.size(:letter)
33+
{612.0, 792}
34+
"""
2035
def size(name) do
2136
@page_sizes[name]
2237
end

lib/mudbrick/predicates.ex

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
11
defmodule Mudbrick.Predicates do
2+
@moduledoc """
3+
Useful for testing PDF documents.
4+
5+
While these predicates do check the PDF in a black-box way, it's not expected
6+
that they will work on all PDFs found in the wild.
7+
"""
8+
9+
@doc """
10+
Checks for presence of `text` in the `pdf` `iodata`. Searches compressed and uncompressed data.
11+
12+
This arity only works with text that can be found in literal form inside a stream, compressed or uncompressed,
13+
"""
14+
@spec has_text?(pdf :: iodata(), text :: binary()) :: boolean()
215
def has_text?(pdf, text) do
316
binary = IO.iodata_to_binary(pdf)
417
streams = extract_streams(binary)
518
Enum.any?(streams, &String.contains?(&1, text))
619
end
720

8-
def has_text?(pdf, text, in_font: font) do
21+
@doc """
22+
Checks for presence of `text` in the `pdf` `iodata`. Searches compressed and uncompressed data.
23+
24+
This arity requires you to pass the raw font data in which the text is
25+
expected to be written. It must be present in raw hexadecimal form
26+
corresponding to the font's glyph IDs.
27+
28+
The [OpenType](https://hexdocs.pm/opentype) library is used to find font
29+
features, such as ligatures, which are expected to have been used in the PDF.
30+
31+
## Options
32+
33+
- `:in_font` - raw font data in which the text is expected.
34+
35+
## Example
36+
37+
iex> Mudbrick.Predicates.has_text?("some-pdf", "hello", in_font: Mudbrick.TestHelper.bodoni())
38+
"""
39+
@spec has_text?(pdf :: iodata(), text :: binary(), opts :: list()) :: boolean()
40+
def has_text?(pdf, text, opts) do
41+
font = Keyword.fetch!(opts, :in_font)
942
parsed_font = OpenType.new() |> OpenType.parse(font)
1043
{glyph_ids_decimal, _positions} = OpenType.layout_text(parsed_font, text)
1144
glyph_ids_hex = Enum.map_join(glyph_ids_decimal, "", &Mudbrick.to_hex/1)

test/mudbrick/page_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule Mudbrick.PageTest do
2+
use ExUnit.Case, async: true
3+
doctest Mudbrick.Page
4+
end

test/predicates_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
defmodule Mbc.PredicatesTest do
1+
defmodule Mudbrick.PredicatesTest do
22
use ExUnit.Case, async: true
3+
doctest Mudbrick.Predicates
34

45
import Mudbrick
56
import Mudbrick.Predicates

0 commit comments

Comments
 (0)