如何使用 OmniFaces 在 JSF 应用程序中正确嵌入 SVG 图形
How to embed SVG graphics properly in JSF application using OmniFaces
这是
的后续
在那个问题中,我了解到 OmniFaces-2.1 快照支持通过它的 o:graphicImage 标签利用 InputStream/byte[]。
在 Getting started with SVG graphics objects in JSF 2.0 pages 中,我了解到标准的 graphicImage 标签只是呈现一个 img 标签,而 SVG 应该呈现为 object 标签如下:<object type="image/svg+xml" data="#{bean.svgUrl}">
.
我的问题实际上直接针对 OmniFaces 项目的 BalusC:
不应该o:graphicImage以这种方式呈现 SVG 吗?我得到的只是 img-tags,它们在 IE10+ 中不起作用。
更新 #2:这是一个缩放问题!我将高度设置为 125 并开始看到图形的边缘。
更新 #1:呈现的 img 标签示例:
这个仅基于静态 SVG 文件:<img height="25" alt="" src="/javax.faces.resource/images/Logo.svg.xhtml;jsessionid=cc0acf6def65ac5c127d09384977?ln=default">
这是基于一个字节数组:
<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iCiAgIHhtbG5zOmNjPSJodHRwOi8vY3JlYXRpdmVjb21tb25zLm9yZy9ucyMiCiAgIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyIKICAgeG1sbnM6c3ZnPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIgogICB4bWxuczppbmtzY2FwZT0iaHR0cDovL3d3dy5pbmtzY2FwZS5vcmcvbmFtZXNwYWNlcy9pbmtzY2FwZSIKICAgdmVyc2lv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3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLW9wYWNpdHk6MTtzdHJva2UtZGFzaGFycmF5Om5vbmUiIC8+PC9nPjwvZz48L3N2Zz4=" alt="100.1 - Farlig sving" title="100.1 - Farlig sving" width="40" style="margin-left: 3px; pointer-events: none;" height="40">
因为我发现这是一个 SVG 缩放问题,而且我似乎不是唯一遇到过这个问题的人,所以我将 post 我的解决方案放在这里。
简而言之,我
- 检索 SVG。
- 使用 JDOM 解析它。
- 根据指定尺寸,如果缺少视图框,则注入视图框
这是没有错误处理的 Java 代码:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = execute(builder.buildGet(), InputStream.class);
Document doc = new SAXBuilder().build(is);
Element root = doc.getRootElement();
if (root.getAttribute("viewBox") == null)
{
String width = root.getAttributeValue("width"), height = root.getAttributeValue("height");
root.setAttribute("viewBox", String.format("0 0 %s %s", width, height));
}
new XMLOutputter().output(doc, baos);
return baos.toByteArray();
这是
在那个问题中,我了解到 OmniFaces-2.1 快照支持通过它的 o:graphicImage 标签利用 InputStream/byte[]。
在 Getting started with SVG graphics objects in JSF 2.0 pages 中,我了解到标准的 graphicImage 标签只是呈现一个 img 标签,而 SVG 应该呈现为 object 标签如下:<object type="image/svg+xml" data="#{bean.svgUrl}">
.
我的问题实际上直接针对 OmniFaces 项目的 BalusC:
不应该o:graphicImage以这种方式呈现 SVG 吗?我得到的只是 img-tags,它们在 IE10+ 中不起作用。
更新 #2:这是一个缩放问题!我将高度设置为 125 并开始看到图形的边缘。
更新 #1:呈现的 img 标签示例:
这个仅基于静态 SVG 文件:<img height="25" alt="" src="/javax.faces.resource/images/Logo.svg.xhtml;jsessionid=cc0acf6def65ac5c127d09384977?ln=default">
这是基于一个字节数组:
<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0g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IDAsODcuMTA5IDU0LjcyLDE2NS41OSAxMzYuOCwxOTUuODI4IGwgMTkyLjk2LDcxLjI4IDEyLjk2LC03Mi43MjEgYyAwLjczLC0yLjg3OSAzLjYxLC01LjAzOSA2LjQ4LC01LjAzOSAyLjE3LDAgMy42MSwwLjcxOCA1LjA1LDIuODcxIGwgMTQ5Ljc1LDIxNi43MjkgYyAwLjcyLDEuNDQgMS40NCwzLjYgMS40NCw1LjA0IDAsNC4zMyAtMi44OCw3LjkyIC02LjQ4LDguNjMgeiIKICAgICAgICAgaW5rc2NhcGU6Y29ubmVjdG9yLWN1cnZhdHVyZT0iMCIKICAgICAgICAgaWQ9InBhdGgyNCIKICAgICAgICAgc3R5bGU9ImZpbGw6bm9uZTtzdHJva2U6IzIzMWYyMDtzdHJva2Utd2lkdGg6Mi4xNTAwMDAxO3N0cm9rZS1saW5lY2FwOmJ1dHQ7c3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLW9wYWNpdHk6MTtzdHJva2UtZGFzaGFycmF5Om5vbmUiIC8+PC9nPjwvZz48L3N2Zz4=" alt="100.1 - Farlig sving" title="100.1 - Farlig sving" width="40" style="margin-left: 3px; pointer-events: none;" height="40">
因为我发现这是一个 SVG 缩放问题,而且我似乎不是唯一遇到过这个问题的人,所以我将 post 我的解决方案放在这里。
简而言之,我
- 检索 SVG。
- 使用 JDOM 解析它。
- 根据指定尺寸,如果缺少视图框,则注入视图框
这是没有错误处理的 Java 代码:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = execute(builder.buildGet(), InputStream.class);
Document doc = new SAXBuilder().build(is);
Element root = doc.getRootElement();
if (root.getAttribute("viewBox") == null)
{
String width = root.getAttributeValue("width"), height = root.getAttributeValue("height");
root.setAttribute("viewBox", String.format("0 0 %s %s", width, height));
}
new XMLOutputter().output(doc, baos);
return baos.toByteArray();