From db6d8534ae65765435548a988e2a07f1fbcf004c Mon Sep 17 00:00:00 2001 From: Andrew Bruce Date: Sun, 20 Oct 2024 15:42:56 +0100 Subject: [PATCH] Leading is customisable --- lib/mudbrick.ex | 4 +++- test/font_test.exs | 2 +- test/mudbrick_test.exs | 11 ++++++----- test/text_test.exs | 21 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/mudbrick.ex b/lib/mudbrick.ex index aee178f..9da9031 100644 --- a/lib/mudbrick.ex +++ b/lib/mudbrick.ex @@ -31,6 +31,8 @@ defmodule Mudbrick do def font({doc, _content_stream_obj} = context, user_identifier, opts) do import ContentStream + {leading, opts} = Keyword.pop(opts, :leading, Keyword.fetch!(opts, :size) * 1.2) + case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do {:ok, font} -> context @@ -42,7 +44,7 @@ defmodule Mudbrick do font.value ) ) - |> add(ContentStream.TL, leading: Keyword.fetch!(opts, :size) * 1.2) + |> add(ContentStream.TL, leading: leading) :error -> raise Font.Unregistered, "Unregistered font: #{user_identifier}" diff --git a/test/font_test.exs b/test/font_test.exs index 4e7955d..9ae4f56 100644 --- a/test/font_test.exs +++ b/test/font_test.exs @@ -110,7 +110,7 @@ defmodule Mudbrick.FontTest do e = assert_raise Font.Unregistered, fn -> - chain |> font(:bodoni, 24) + chain |> font(:bodoni, size: 24) end assert e.message == "Unregistered font: bodoni" diff --git a/test/mudbrick_test.exs b/test/mudbrick_test.exs index c12fb2b..bdefe84 100644 --- a/test/mudbrick_test.exs +++ b/test/mudbrick_test.exs @@ -11,6 +11,11 @@ defmodule MudbrickTest do } ) |> page(size: :letter) + |> image( + :flower, + scale: Mudbrick.Page.size(:letter), + position: {0, 0} + ) |> text_position(200, 700) |> font(:bodoni, size: 14) |> colour({1, 0, 0}) @@ -20,15 +25,11 @@ defmodule MudbrickTest do is Carbon Dioxide and HNO₃ is Nitric Acid """) - |> image( - :flower, - scale: Mudbrick.Page.size(:letter), - position: {0, 0} - ) |> text("wide stuff", align: :right) |> text("wider stuff", align: :right) |> text("z", align: :right) |> text_position(400, 600) + |> font(:bodoni, size: 14, leading: 14) |> text(""" I am left again diff --git a/test/text_test.exs b/test/text_test.exs index b708e8a..e90cd18 100644 --- a/test/text_test.exs +++ b/test/text_test.exs @@ -8,6 +8,27 @@ defmodule Mudbrick.TextTest do alias Mudbrick.Font alias Mudbrick.Indirect + test "can set leading" do + assert [ + "/F1 10 Tf", + "14 TL", + "(black and ) Tj" + ] = + new( + fonts: %{ + helvetica: [ + name: :Helvetica, + type: :TrueType, + encoding: :PDFDocEncoding + ] + } + ) + |> page() + |> font(:helvetica, size: 10, leading: 14) + |> text("black and ") + |> operations() + end + test "can set colour on a piece of text" do {_doc, content_stream} = new(