如何在XSL-FO中对齐jEuclid生成的SVG公式与文本基线?
我们团队在PDF生成流程中使用jEuclid将MathML公式转换为SVG图像,目前已成功将SVG作为外部图形嵌入,但行内公式无法与周边文本对齐——会根据图像大小出现上下偏移的情况。想请教是否可以通过定义SVG本身,让公式的基线与外部文本对齐?我尝试过vertical-align和alignment-baseline的几乎所有组合,目前效果最好的是vertical-align: mathematical,但仍需额外添加3pts的基线偏移,最终效果还是不理想。
附上当前使用的SVG代码:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-dasharray="none" shape-rendering="auto" font-family="'Dialog'" width="26" text-rendering="auto" fill-opacity="1" contentScriptType="text/ecmascript" color-interpolation="auto" color-rendering="auto" preserveAspectRatio="xMidYMid meet" font-size="12" fill="black" stroke="black" image-rendering="auto" stroke-miterlimit="10" zoomAndPan="magnify" version="1.0" stroke-linecap="square" stroke-linejoin="miter" contentStyleType="text/css" font-style="normal" height="13" stroke-width="1" stroke-dashoffset="0" font-weight="normal" stroke-opacity="1"> </svg>
解决方法
给SVG添加精准的
viewBox并调整对齐规则
jEuclid生成的SVG默认可能没有定义明确的排版基线参考,你可以手动添加viewBox属性,同时将preserveAspectRatio改为xMinYMin meet,让SVG的左下角对齐容器的左下角,便于和外部文本基线匹配。修改后的SVG根元素示例:<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-dasharray="none" shape-rendering="auto" font-family="'Dialog'" width="26" text-rendering="auto" fill-opacity="1" contentScriptType="text/ecmascript" color-interpolation="auto" color-rendering="auto" preserveAspectRatio="xMinYMin meet" viewBox="0 0 26 13" font-size="12" fill="black" stroke="black" image-rendering="auto" stroke-miterlimit="10" zoomAndPan="magnify" version="1.0" stroke-linecap="square" stroke-linejoin="miter" contentStyleType="text/css" font-style="normal" height="13" stroke-width="1" stroke-dashoffset="0" font-weight="normal" stroke-opacity="1"> <!-- 公式图形内容 --> </svg>用CSS微调偏移
如果调整viewBox后仍有偏差,可以给嵌入SVG的元素添加精准的CSS偏移,放弃vertical-align: mathematical,改用基线对齐配合变换:.inline-math { vertical-align: baseline; transform: translateY(-0.8px); /* 根据实际偏差调整数值 */ }配置jEuclid生成带基线信息的SVG
jEuclid支持生成包含基线、 ascent、 descent等排版元数据的SVG,你可以在生成时开启相关配置(比如设置net.sourceforge.jeuclid.output.SvgOutput.BASELINE参数为true),这样生成的SVG会自带基线参考,无需手动调整对齐。
内容的提问来源于stack exchange,提问作者Mario Di Giacomo

