1919
2020package org .apache .fop .svg ;
2121
22- import org .junit .Test ;
22+ import java .io .ByteArrayInputStream ;
23+ import java .io .File ;
24+ import java .io .FileOutputStream ;
25+ import java .io .IOException ;
26+ import java .io .OutputStream ;
27+ import java .util .HashMap ;
28+ import java .util .Map ;
2329
30+ import org .junit .Rule ;
31+ import org .junit .Test ;
32+ import org .junit .rules .TemporaryFolder ;
2433import static org .junit .Assert .assertEquals ;
2534
35+ import org .apache .pdfbox .Loader ;
36+ import org .apache .pdfbox .pdmodel .PDDocument ;
37+ import org .apache .pdfbox .text .PDFTextStripper ;
38+ import org .apache .pdfbox .text .TextPosition ;
39+
2640import org .apache .batik .transcoder .Transcoder ;
41+ import org .apache .batik .transcoder .TranscoderException ;
42+ import org .apache .batik .transcoder .TranscoderInput ;
43+ import org .apache .batik .transcoder .TranscoderOutput ;
2744
2845import org .apache .fop .configuration .Configuration ;
46+ import org .apache .fop .configuration .ConfigurationException ;
47+ import org .apache .fop .configuration .DefaultConfigurationBuilder ;
2948
3049/**
3150 * Basic runtime test for the PDF transcoder. It is used to verify that
3251 * nothing obvious is broken after compiling.
3352 */
3453public class BasicPDFTranscoderTestCase extends AbstractBasicTranscoderTest {
3554
55+ @ Rule
56+ public TemporaryFolder tempFolder = new TemporaryFolder ();
57+
3658 @ Override
3759 protected Transcoder createTranscoder () {
3860 return new PDFTranscoder ();
@@ -52,4 +74,70 @@ public void testFontAutoDetect() {
5274 autoDetectConf .getClass ().getSimpleName ());
5375 }
5476
77+ @ Test
78+ public void testFontSubstitution () throws ConfigurationException , IOException , TranscoderException {
79+
80+ PDFTranscoder transcoder = (PDFTranscoder ) createTranscoder ();
81+
82+ DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder ();
83+ String cfgFragment =
84+ "<pdf-renderer>"
85+ + "<fonts>"
86+ + "<substitutions>"
87+ + "<substitution>"
88+ + "<from font-family=\" Helvetica\" />"
89+ + "<to font-family=\" Courier\" />"
90+ + "</substitution>"
91+ + "</substitutions>"
92+ + "</fonts>"
93+ + "</pdf-renderer>" ;
94+ Configuration cfg = cfgBuilder .build (new ByteArrayInputStream (cfgFragment .getBytes ()));
95+ transcoder .configure (cfg );
96+
97+ String svgFragment = "<svg xml:space=\" preserve\" x=\" -1.70458in\" y=\" 0.198315in\" "
98+ + "width=\" 2.6622in\" height=\" 1.89672in\" "
99+ + " viewBox=\" -4330 0 6762 4818\" xmlns=\" http://www.w3.org/2000/svg\" >"
100+ + " <text x=\" -3653\" y=\" 841\" style=\" fill:#1F1A17;font-size:639;font-family:Helvetica\" >H</text>"
101+ + "</svg>" ;
102+ TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (svgFragment .getBytes ()));
103+
104+ File outputFile = tempFolder .newFile ("output.pdf" );
105+ OutputStream os = new FileOutputStream (outputFile );
106+ TranscoderOutput output = new TranscoderOutput (os );
107+
108+ try {
109+ transcoder .transcode (input , output );
110+ } finally {
111+ os .close ();
112+ }
113+
114+ PDDocument pdfDocument = null ;
115+ try {
116+ pdfDocument = Loader .loadPDF (outputFile );
117+ FontExtractor fontExtractor = new FontExtractor ();
118+ fontExtractor .getText (pdfDocument );
119+ assertEquals ("Courier" , fontExtractor .getFontUsage ().get ("H" ));
120+ } finally {
121+ if (pdfDocument != null ) {
122+ pdfDocument .close ();
123+ }
124+ }
125+ }
126+
127+ class FontExtractor extends PDFTextStripper {
128+
129+ private Map <String , String > fontUsage = new HashMap <>();
130+
131+ @ Override
132+ protected void processTextPosition (TextPosition text ) {
133+ String fontName = text .getFont ().getName ();
134+ fontUsage .put (text .toString (), fontName );
135+ super .processTextPosition (text );
136+ }
137+
138+ public Map <String , String > getFontUsage () {
139+ return fontUsage ;
140+ }
141+ }
142+
55143}
0 commit comments