summaryrefslogtreecommitdiff
path: root/support/make4ht/domfilters/make4ht-odtsvg.lua
diff options
context:
space:
mode:
Diffstat (limited to 'support/make4ht/domfilters/make4ht-odtsvg.lua')
-rw-r--r--support/make4ht/domfilters/make4ht-odtsvg.lua42
1 files changed, 42 insertions, 0 deletions
diff --git a/support/make4ht/domfilters/make4ht-odtsvg.lua b/support/make4ht/domfilters/make4ht-odtsvg.lua
new file mode 100644
index 0000000000..453cf430f7
--- /dev/null
+++ b/support/make4ht/domfilters/make4ht-odtsvg.lua
@@ -0,0 +1,42 @@
+-- we need to set dimensions for SVG images produced by \Picture commands
+local log = logging.new "odtsvg"
+local function get_svg_dimensions(filename)
+ local width, height
+ log:debug("file exists", filename, mkutils.file_exists(filename))
+ if mkutils.file_exists(filename) then
+ for line in io.lines(filename) do
+ width = line:match("width%s*=%s*[\"'](.-)[\"']") or width
+ height = line:match("height%s*=%s*[\"'](.-)[\"']") or height
+ -- stop parsing once we get both width and height
+ if width and height then break end
+ end
+ end
+ return width, height
+end
+
+
+-- process
+return function(dom)
+ for _, pic in ipairs(dom:query_selector("draw|image")) do
+ local imagename = pic:get_attribute("xlink:href")
+ -- update SVG images dimensions
+ log:debug("image", imagename)
+ local parent = pic:get_parent()
+ local width = parent:get_attribute("svg:width")
+ local height = parent:get_attribute("svg:height")
+ -- if width == "0.0pt" then width = nil end
+ -- if height == "0.0pt" then height = nil end
+ if not width or not height then
+ if imagename:match("svg$") then
+ width, height = get_svg_dimensions(imagename) -- or width, height
+ elseif imagename:match("png$") or imagename:match("jpe?g$") then
+ end
+ end
+ log:debug("dimensions", width, height)
+ parent:set_attribute("svg:width", width)
+ parent:set_attribute("svg:height", height)
+ -- if
+ end
+ return dom
+end
+