diff --git a/lib/mudbrick.ex b/lib/mudbrick.ex index 25f1480..eefc349 100644 --- a/lib/mudbrick.ex +++ b/lib/mudbrick.ex @@ -480,12 +480,13 @@ defmodule Mudbrick do "Auto scaling works with width or height, but not both." {w, :auto} -> - ratio = w / image.width - {w, image.height * ratio} + scaled_height(w, image) {:auto, h} -> - ratio = h / image.height - {image.width * ratio, h} + scaled_width(h, image) + + nil -> + scaled_height(100, image) otherwise -> otherwise @@ -494,6 +495,16 @@ defmodule Mudbrick do Keyword.put(image_opts, :scale, scale) end + defp scaled_height(w, image) do + ratio = w / image.width + {w, image.height * ratio} + end + + defp scaled_width(h, image) do + ratio = h / image.height + {image.width * ratio, h} + end + defp fetch_font(doc, opts) do default_font = case Map.values(Document.root_page_tree(doc).value.fonts) do diff --git a/lib/mudbrick/parser.ex b/lib/mudbrick/parser.ex index 702d6a3..4e7b4f2 100644 --- a/lib/mudbrick/parser.ex +++ b/lib/mudbrick/parser.ex @@ -390,10 +390,13 @@ defmodule Mudbrick.Parser do if data == "" do [] else - %Mudbrick.ContentStream{operations: operations} = - to_mudbrick(data, :content_blocks) + case to_mudbrick(data, :content_blocks) do + %Mudbrick.ContentStream{operations: operations} -> + operations - operations + _ -> + raise Error, "Can't parse content blocks: #{data}" + end end end diff --git a/test/mudbrick/parser/roundtrip_test.exs b/test/mudbrick/parser/roundtrip_test.exs index 50093b1..d880919 100644 --- a/test/mudbrick/parser/roundtrip_test.exs +++ b/test/mudbrick/parser/roundtrip_test.exs @@ -21,18 +21,17 @@ defmodule Mudbrick.ParseRoundtripTest do if(Enum.empty?(images_options), do: [], else: Stream.cycle(images_options)), if(Enum.empty?(images), do: [], else: Stream.cycle(Map.keys(images))) ]) - |> Enum.reduce(doc, fn {page_options, _image_options, _image_identifier}, context -> + |> Enum.reduce(doc, fn {page_options, image_options, image_identifier}, context -> context |> page(page_options) - - # |> then(fn page_context -> - # if Enum.empty?(image_options) or Enum.empty?(document_options[:images]) do - # page_context - # else - # page_context - # |> image(image_identifier, image_options) - # end - # end) + |> then(fn page_context -> + if Enum.empty?(image_options) or Enum.empty?(document_options[:images]) do + page_context + else + page_context + |> image(image_identifier, image_options) + end + end) end) |> render() diff --git a/test/test_helper.exs b/test/test_helper.exs index 490c9b4..a6348fe 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -184,7 +184,7 @@ defmodule Mudbrick.TestHelper do end def coords do - {float(max: 999), float(max: 999)} + {float_non_exponential(), float_non_exponential()} end def non_negative_coords do @@ -193,11 +193,15 @@ defmodule Mudbrick.TestHelper do def scale do bind(one_of([:x_auto, :y_auto, :neither]), fn - :x_auto -> {:auto, float()} - :y_auto -> {float(), :auto} - :neither -> {float(), float()} + :x_auto -> {:auto, float_non_exponential()} + :y_auto -> {float_non_exponential(), :auto} + :neither -> coords() end) end + + defp float_non_exponential do + float(min: -999, max: 999) + end end ExUnit.start()