Skip to content

Commit 97bc9d0

Browse files
committed
Refactor
Move private functions below public Flatten TextBlock.Output and privatise functions
1 parent 4635c02 commit 97bc9d0

File tree

3 files changed

+185
-201
lines changed

3 files changed

+185
-201
lines changed

lib/mudbrick.ex

+51-51
Original file line numberDiff line numberDiff line change
@@ -334,57 +334,6 @@ defmodule Mudbrick do
334334
end)
335335
end
336336

337-
defp text_block(doc, writes, top_level_opts) do
338-
Enum.reduce(writes, Mudbrick.TextBlock.new(top_level_opts), fn
339-
{text, opts}, acc ->
340-
Mudbrick.TextBlock.write(acc, text, fetch_font(doc, opts))
341-
342-
text, acc ->
343-
Mudbrick.TextBlock.write(acc, text, [])
344-
end)
345-
end
346-
347-
@spec cm_opts(Mudbrick.Image.t(), Image.image_options()) :: Mudbrick.ContentStream.Cm.options()
348-
defp cm_opts(image, image_opts) do
349-
scale =
350-
case image_opts[:scale] do
351-
{:auto, :auto} ->
352-
raise Mudbrick.Image.AutoScalingError,
353-
"Auto scaling works with width or height, but not both."
354-
355-
{w, :auto} ->
356-
ratio = w / image.width
357-
{w, image.height * ratio}
358-
359-
{:auto, h} ->
360-
ratio = h / image.height
361-
{image.width * ratio, h}
362-
363-
otherwise ->
364-
otherwise
365-
end
366-
367-
Keyword.put(image_opts, :scale, scale)
368-
end
369-
370-
defp fetch_font(doc, opts) do
371-
default_font =
372-
case Map.values(Document.root_page_tree(doc).value.fonts) do
373-
[font] -> font.value
374-
_ -> nil
375-
end
376-
377-
Keyword.update(opts, :font, default_font, fn user_identifier ->
378-
case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do
379-
{:ok, font} ->
380-
font.value
381-
382-
:error ->
383-
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
384-
end
385-
end)
386-
end
387-
388337
@doc """
389338
Produce `iodata` from the current document.
390339
"""
@@ -463,4 +412,55 @@ defmodule Mudbrick do
463412
end)
464413
|> finish(& &1.value.contents)
465414
end
415+
416+
defp text_block(doc, writes, top_level_opts) do
417+
Enum.reduce(writes, Mudbrick.TextBlock.new(top_level_opts), fn
418+
{text, opts}, acc ->
419+
Mudbrick.TextBlock.write(acc, text, fetch_font(doc, opts))
420+
421+
text, acc ->
422+
Mudbrick.TextBlock.write(acc, text, [])
423+
end)
424+
end
425+
426+
@spec cm_opts(Mudbrick.Image.t(), Image.image_options()) :: Mudbrick.ContentStream.Cm.options()
427+
defp cm_opts(image, image_opts) do
428+
scale =
429+
case image_opts[:scale] do
430+
{:auto, :auto} ->
431+
raise Mudbrick.Image.AutoScalingError,
432+
"Auto scaling works with width or height, but not both."
433+
434+
{w, :auto} ->
435+
ratio = w / image.width
436+
{w, image.height * ratio}
437+
438+
{:auto, h} ->
439+
ratio = h / image.height
440+
{image.width * ratio, h}
441+
442+
otherwise ->
443+
otherwise
444+
end
445+
446+
Keyword.put(image_opts, :scale, scale)
447+
end
448+
449+
defp fetch_font(doc, opts) do
450+
default_font =
451+
case Map.values(Document.root_page_tree(doc).value.fonts) do
452+
[font] -> font.value
453+
_ -> nil
454+
end
455+
456+
Keyword.update(opts, :font, default_font, fn user_identifier ->
457+
case Map.fetch(Document.root_page_tree(doc).value.fonts, user_identifier) do
458+
{:ok, font} ->
459+
font.value
460+
461+
:error ->
462+
raise Font.Unregistered, "Unregistered font: #{user_identifier}"
463+
end
464+
end)
465+
end
466466
end

lib/mudbrick/content_stream.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ defmodule Mudbrick.ContentStream do
3838
end)
3939
end
4040

41-
defp update({doc, contents_obj}, f) do
42-
Document.update(doc, contents_obj, f)
43-
end
44-
4541
def update_operations(context, f) do
4642
update(context, fn contents ->
4743
Map.update!(contents, :operations, f)
4844
end)
4945
end
5046

47+
defp update({doc, contents_obj}, f) do
48+
Document.update(doc, contents_obj, f)
49+
end
50+
5151
defimpl Mudbrick.Object do
5252
def from(content_stream) do
5353
Mudbrick.Stream.new(

0 commit comments

Comments
 (0)