Skip to content

Commit b282baf

Browse files
committed
feat: set svg image in element content
1 parent 11e968a commit b282baf

File tree

9 files changed

+28
-12
lines changed

9 files changed

+28
-12
lines changed

fj-doc-base/src/main/java/org/fugerit/java/doc/base/model/DocImage.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The Apache Software Foundation (http://www.apache.org/).
3131
import java.net.URI;
3232
import java.net.URISyntaxException;
3333
import java.net.URL;
34+
import java.nio.charset.StandardCharsets;
3435
import java.util.Arrays;
3536
import java.util.Collection;
3637

@@ -82,7 +83,9 @@ public static Collection<String> getAcceptedImageTypes() {
8283
@Getter @Setter private String alt;
8384

8485
@Getter @Setter private int align;
85-
86+
87+
@Getter @Setter private String content = "";
88+
8689
public String getResolvedBase64() {
8790
return SafeFunction.get( () -> {
8891
String res = this.getBase64();
@@ -94,7 +97,13 @@ public String getResolvedBase64() {
9497
}
9598

9699
public String getResolvedText() {
97-
return SafeFunction.get( () -> new String( resolveImage( this ) ) );
100+
return SafeFunction.get( () -> {
101+
if ( StringUtils.isEmpty( this.content ) ) {
102+
return new String( resolveImage( this ) );
103+
} else {
104+
return this.content;
105+
}
106+
});
98107
}
99108

100109
public String getResolvedType() {
@@ -134,6 +143,8 @@ public static byte[] resolveImage( DocImage img ) throws IOException {
134143
data = Base64Helper.decodeBase64String( base64 );
135144
} else if ( path != null ) {
136145
data = byteResolverHelper( path );
146+
} if ( StringUtils.isNotEmpty( img.getContent() ) ) {
147+
data = img.getContent().getBytes( StandardCharsets.UTF_8 );
137148
} else {
138149
throw new IOException( "Null path provided!" );
139150
}

fj-doc-base/src/main/java/org/fugerit/java/doc/base/parser/DocParserContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public void handleText( String text ) {
120120
} else if ( this.currentElement instanceof DocInfo ) {
121121
DocInfo docInfo = (DocInfo)this.currentElement;
122122
docInfo.getContent().append( text );
123+
} else if ( this.currentElement instanceof DocImage ) {
124+
DocImage docImage = (DocImage)this.currentElement;
125+
docImage.setContent( docImage.getContent()+text );
123126
}
124127
}
125128
}

fj-doc-base/src/main/resources/config/doc-2-1.xsd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @project : fj-doc-base
88
* @creation : 2023-08-18
9-
* @version : 2.1.0-rc.3 (2025-03-22)
9+
* @version : 2.1.0-rc.4 (2025-11-19)
1010
*
1111
* XSD for fugerit doc configuration
1212
*/
@@ -408,7 +408,7 @@
408408
<xsd:documentation>An image to include in the document</xsd:documentation>
409409
<xsd:documentation>(roughly comparable to a HTML 'image' element)</xsd:documentation>
410410
</xsd:annotation>
411-
<xsd:complexType>
411+
<xsd:complexType mixed="true">
412412
<xsd:attribute name='url' type='doc:urlType' use='optional'/>
413413
<xsd:attribute name='type' type='doc:imageType' use='optional' />
414414
<xsd:attribute name='scaling' type='doc:scalingType' use='optional' />

fj-doc-base/src/test/resources/coverage/template/macro/html_element.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
<#if docImage.align = 2>
5656
<#assign imageAlign="style='display: block; margin-left: auto; margin-right: auto;'"/>
5757
</#if>
58-
</#if>
59-
<img <@handleId element=docImage/> ${imageAlign!''} <#if (docImage.alt)??> alt="${docImage.alt}" </#if> ${imageScaling} src="data:image/png;base64, ${docImage.resolvedBase64}" />
58+
</#if>
59+
<img <@handleId element=docImage/> ${imageAlign!''} <#if (docImage.alt)??> alt="${docImage.alt}" </#if> ${imageScaling} src="data:image/png;base64, <#if docImage.content?has_content>${docImage.content}<#else>${docImage.resolvedBase64}</#if>" />
6060
</#macro>
6161

6262
<#macro handleList docList>

fj-doc-base/src/test/resources/coverage/xml/default_doc.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<image scaling="100" url="cl://test/img_test_teal.jpg"/>
4141
<br/>
4242
<page-break/>
43-
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
43+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
4444
<row>
4545
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
4646
<cell align="center"><para style="bold">Surname</para></cell>

fj-doc-base/src/test/resources/coverage/xml/default_doc_alt.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<doc
33
xmlns="http://javacoredoc.fugerit.org"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-0.xsd" >
5+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd" >
66

77
<metadata>
88
<!-- Margin for document : left;right;top;bottom -->
@@ -29,7 +29,9 @@
2929
<body>
3030
<image scaling="100" url="cl://test/img_test_teal.jpg"/>
3131
<phrase leading="3">My sample title</phrase>
32-
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
32+
<image type="svg" alt="blank square"><![CDATA[<svg xmlns="http://www.w3.org/2000/svg" fill="#000000" width="10px" height="10px" viewBox="0 0 24 24"><path d="M21,2H3A1,1,0,0,0,2,3V21a1,1,0,0,0,1,1H21a1,1,0,0,0,1-1V3A1,1,0,0,0,21,2ZM20,20H4V4H20Z"/></svg>]]></image>
33+
34+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
3335
<row>
3436
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
3537
<cell align="center"><para fore-color="#ffffff" style="bold">Surname</para></cell>

fj-doc-freemarker/src/main/resources/fj_doc_freemarker_config/template/macro/html_element.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<#assign imageAlign="style='display: block; margin-left: auto; margin-right: auto;'"/>
6666
</#if>
6767
</#if>
68-
<img <@handleId element=docImage/> ${imageAlign!''} <#if (docImage.alt)??> alt="${docImage.alt}" </#if> ${imageScaling} src="data:image/png;base64, ${docImage.resolvedBase64}" />
68+
<img <@handleId element=docImage/> ${imageAlign!''} <#if (docImage.alt)??> alt="${docImage.alt}" </#if> ${imageScaling} src="data:image/png;base64, <#if docImage.content?has_content>${docImage.content}<#else>${docImage.resolvedBase64}</#if>" />
6969
</#if>
7070
</#macro>
7171

fj-doc-mod-fop/src/main/resources/fj_doc_mod_fop_config/template/macro/doc_element.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<#macro handleImage docImage>
5757
<fo:block <@handleAlign alignValue=docImage.align/>>
5858
<#if docImage.svg>
59-
<fo:instream-foreign-object <#if (docImage.alt)??> fox:alt-text="${docImage.alt}" </#if> xmlns:svg="http://www.w3.org/2000/svg">${base64ToString(docImage.resolvedBase64)}</fo:instream-foreign-object>
59+
<fo:instream-foreign-object <#if (docImage.alt)??> fox:alt-text="${docImage.alt}" </#if> xmlns:svg="http://www.w3.org/2000/svg"><#if docImage.content?has_content>${stringToBase64(docImage.content)}<#else>${base64ToString(docImage.resolvedBase64)}</#if></fo:instream-foreign-object>
6060
<#else>
6161
<#if (docImage.scaling)??>
6262
<#assign imageScaling="height='${docImage.scaling}%' content-height='${docImage.scaling}%' content-width='scale-to-fit' scaling='uniform' width='${docImage.scaling}%'"/>

fj-doc-mod-fop/src/test/resources/issue/gh580/sample-gh580.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<body>
1616
<para>Sample special characters reproducer ■ □ &#x25A0; &#x25A1;</para>
1717
<phrase>Working POC with SVG : </phrase>
18-
<image type="svg" alt="blank square" base64="PHN2ZyBmaWxsPSIjMDAwMDAwIiB3aWR0aD0iMTBweCIgaGVpZ2h0PSIxMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTIxLDJIM0ExLDEsMCwwLDAsMiwzVjIxYTEsMSwwLDAsMCwxLDFIMjFhMSwxLDAsMCwwLDEtMVYzQTEsMSwwLDAsMCwyMSwyWk0yMCwyMEg0VjRIMjBaIi8+PC9zdmc+"/>
18+
<image type="svg" alt="blank square"><![CDATA[<svg xmlns="http://www.w3.org/2000/svg" fill="#000000" width="10px" height="10px" viewBox="0 0 24 24"><path d="M21,2H3A1,1,0,0,0,2,3V21a1,1,0,0,0,1,1H21a1,1,0,0,0,1-1V3A1,1,0,0,0,21,2ZM20,20H4V4H20Z"/></svg>]]></image>
1919
</body>
2020

2121
</doc>

0 commit comments

Comments
 (0)