Files
business-suite/bin/svg.lua
T

111 lines
4.2 KiB
Lua
Raw Permalink Normal View History

-- Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
-- Every drawing a document shows, converted while pandoc reads the document.
--
-- graphicx cannot read an SVG, and pandoc's own answer to one is the svg
-- package, which calls inkscape from inside the LaTeX run and therefore needs
-- shell escape — a document could then run any command it likes. So the drawing
-- is converted here, before a single line of LaTeX exists, and the picture in
-- the document points at the PDF that comes out.
--
-- The two directories arrive in the environment, from the converter: where the
-- document lies, and where the build may write.
local build = os.getenv('MD2PDF_BUILD')
local source = os.getenv('MD2PDF_SOURCE_DIR')
local function absolute(file)
return file:sub(1, 1) == '/' and file or source .. '/' .. file
end
-- Numbered, so that a/logo.svg and b/logo.svg do not share one PDF in the
-- build directory, where the second overwrote the first. The output of a source
-- path is remembered, so a drawing shown twice is converted once.
local count = 0
local converted_to = {}
local function number(value)
return value and tonumber(value:match('[-+]?[%d.]+')) or nil
end
local function attribute(tag, name)
local escaped = name:gsub('(%W)', '%%%1')
return tag:match(escaped .. '%s*=%s*"([^"]+)"')
or tag:match(escaped .. "%s*=%s*'([^']+)'")
end
local function linked_texts(svg, target)
local view = svg:match('<svg[%s%S]-viewBox%s*=%s*"([^"]+)"')
local min_x, min_y, width, height
if view then
min_x, min_y, width, height = view:match(
'^%s*([-+%d.]+)%s+([-+%d.]+)%s+([-+%d.]+)%s+([-+%d.]+)')
min_x, min_y, width, height = tonumber(min_x), tonumber(min_y),
tonumber(width), tonumber(height)
else
local root = svg:match('<svg[%s%S]-%>') or ''
min_x, min_y = 0, 0
width, height = number(attribute(root, 'width')),
number(attribute(root, 'height'))
end
if not width or not height or width == 0 or height == 0 then return end
local lines = {}
for opening, body in svg:gmatch('(<a%s+[%s%S]-%>)([%s%S]-)</a>') do
local href = attribute(opening, 'href')
if href and href:match('^#[%w_.:-]+$') then
for tag in body:gmatch('<text%s+[%s%S]-%>') do
local x, y = number(attribute(tag, 'x')), number(attribute(tag, 'y'))
local size = number(attribute(tag, 'font-size'))
local length = number(attribute(tag, 'textLength'))
if x and y and size and length then
local left = (x - min_x) / width
local bottom = 1 - (y + size * 0.25 - min_y) / height
local link_width = length / width
local link_height = size * 1.25 / height
table.insert(lines, string.format(
'\\businessimagelink{%s}{%.8f}{%.8f}{%.8f}{%.8f}',
href:sub(2), left, bottom, link_width, link_height))
end
end
end
end
-- A kept build folder still holds the links of the drawing that carried this
-- number before; the template would lay them over this one.
os.remove(target .. '.links.tex')
if #lines == 0 then return end
local sidecar, message = io.open(target .. '.links.tex', 'w')
if not sidecar then
error('the links of the drawing could not be written: ' .. tostring(message))
end
sidecar:write(table.concat(lines, '\n'), '\n')
sidecar:close()
end
function Image(image)
if not image.src:lower():match('%.svg$') then return nil end
local source_path = absolute(image.src)
local target = converted_to[source_path]
if not target then
count = count + 1
local name = count .. '-'
.. image.src:gsub('.*/', ''):gsub('%.[Ss][Vv][Gg]$', '') .. '.pdf'
target = build .. '/' .. name
local converted, message = pcall(pandoc.pipe, 'inkscape',
{'--export-type=pdf', '--export-filename=' .. target, source_path}, '')
if not converted then
error('the drawing ' .. image.src .. ' could not be converted: '
.. tostring(message))
end
local handle, read_message = io.open(source_path, 'r')
if not handle then
error('the drawing ' .. image.src .. ' could not be read: '
.. tostring(read_message))
end
local svg = handle:read('*a')
handle:close()
linked_texts(svg, target)
converted_to[source_path] = target
end
image.src = target
return image
end