Skip to content

Commit

Permalink
Default image scale when not set
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Jan 7, 2025
1 parent f0c9f3f commit c8ba676
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
19 changes: 15 additions & 4 deletions lib/mudbrick.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/mudbrick/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 9 additions & 10 deletions test/mudbrick/parser/roundtrip_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 8 additions & 4 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

0 comments on commit c8ba676

Please sign in to comment.