Improve column detection heuristics
This commit is contained in:
@@ -13,6 +13,10 @@ defmodule MusicLibrary.Records.TracklistPdf do
|
||||
{4, 5, false}
|
||||
]
|
||||
|
||||
# Empirically measured per-column capacities at block(spacing: 2pt),
|
||||
# including the header area consumed by artist name and album title.
|
||||
@capacities %{8 => 35, 7 => 38, 6 => 45, 5 => 53}
|
||||
|
||||
@spec generate(MusicLibrary.Records.Record.t(), Release.t()) ::
|
||||
{:ok, binary()} | {:error, term()}
|
||||
def generate(record, release) do
|
||||
@@ -42,6 +46,7 @@ defmodule MusicLibrary.Records.TracklistPdf do
|
||||
"""
|
||||
#set page(width: 120mm, height: 120mm, margin: 5mm, columns: #{columns}, background: rect(width: 100%, height: 100%, stroke: 0.5pt + black))
|
||||
#set text(font: "Liberation Sans", size: #{font_size}pt)
|
||||
#set block(spacing: 2pt)
|
||||
|
||||
#place(top + center, scope: "parent", float: true)[
|
||||
#align(center)[
|
||||
@@ -125,11 +130,8 @@ defmodule MusicLibrary.Records.TracklistPdf do
|
||||
end)
|
||||
end
|
||||
|
||||
# Row height in mm: font size converted to mm (× 0.353) scaled by ~1.2 for
|
||||
# ascender + descender, plus grid gutter (2pt = 0.706mm).
|
||||
defp capacity(columns, font_size) do
|
||||
row_height_mm = font_size * 0.353 * 1.2 + 0.706
|
||||
floor(95 / row_height_mm) * columns
|
||||
Map.fetch!(@capacities, font_size) * columns
|
||||
end
|
||||
|
||||
defp artist_names(record) do
|
||||
|
||||
@@ -108,19 +108,20 @@ defmodule MusicLibrary.Records.TracklistPdfTest do
|
||||
end
|
||||
|
||||
test "two columns for medium track count" do
|
||||
assert {2, 8, true} = TracklistPdf.layout_params(30, 1)
|
||||
assert {2, 8, true} = TracklistPdf.layout_params(40, 1)
|
||||
end
|
||||
|
||||
test "scales up columns and reduces font for large track counts" do
|
||||
assert {3, 7, true} = TracklistPdf.layout_params(55, 1)
|
||||
assert {3, 6, true} = TracklistPdf.layout_params(80, 1)
|
||||
assert {4, 6, false} = TracklistPdf.layout_params(100, 1)
|
||||
assert {4, 5, false} = TracklistPdf.layout_params(120, 1)
|
||||
assert {2, 7, true} = TracklistPdf.layout_params(75, 1)
|
||||
assert {3, 7, true} = TracklistPdf.layout_params(80, 1)
|
||||
assert {3, 6, true} = TracklistPdf.layout_params(120, 1)
|
||||
assert {4, 6, false} = TracklistPdf.layout_params(150, 1)
|
||||
assert {4, 5, false} = TracklistPdf.layout_params(200, 1)
|
||||
end
|
||||
|
||||
test "accounts for medium headers in multi-medium releases" do
|
||||
# 22 tracks + 2 medium headers = 24 items, exceeds single column capacity (23)
|
||||
assert {2, 8, true} = TracklistPdf.layout_params(24, 2)
|
||||
# 34 tracks + 2 medium headers = 36 items, exceeds single column capacity (35)
|
||||
assert {2, 8, true} = TracklistPdf.layout_params(36, 2)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user