Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
import org.apache.fop.apps.io.ResourceResolverFactory;
import org.apache.fop.configuration.Configuration;
import org.apache.fop.configuration.ConfigurationException;
import org.apache.fop.fonts.CustomFontCollection;
import org.apache.fop.fonts.DefaultFontConfig;
import org.apache.fop.fonts.DefaultFontConfigurator;
import org.apache.fop.fonts.EmbedFontInfo;
import org.apache.fop.fonts.FontCacheManagerFactory;
import org.apache.fop.fonts.FontCollection;
import org.apache.fop.fonts.FontDetectorFactory;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontManagerConfigurator;
import org.apache.fop.fonts.FontSetup;
import org.apache.fop.fonts.base14.Base14FontCollection;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.render.pdf.PDFRendererConfig;
import org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser;
Expand Down Expand Up @@ -92,7 +96,11 @@ public static FontInfo createFontInfo(Configuration cfg, boolean useComplexScrip
FontManager fontManager = new FontManager(resourceResolver, FontDetectorFactory.createDefault(),
FontCacheManagerFactory.createDefault());

//TODO Make use of fontBaseURL, font substitution and referencing configuration
FontManagerConfigurator fmConfigurator = new FontManagerConfigurator(cfg, thisUri,
thisUri, null);
fmConfigurator.configure(fontManager, strict);

//TODO Make use of fontBaseURL and referencing configuration
//Requires a change to the expected configuration layout

DefaultFontConfig.DefaultFontConfigParser parser
Expand All @@ -102,7 +110,11 @@ public static FontInfo createFontInfo(Configuration cfg, boolean useComplexScrip
= new DefaultFontConfigurator(fontManager, null, strict);
List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fontInfoConfig);
fontManager.saveCache();
FontSetup.setup(fontInfo, fontInfoList, resourceResolver, useComplexScriptFeatures);
FontCollection[] fontCollections = new FontCollection[] {
new Base14FontCollection(fontManager.isBase14KerningEnabled()),
new CustomFontCollection(resourceResolver, fontInfoList, useComplexScriptFeatures)
};
fontManager.setup(fontInfo, fontCollections);
} else {
FontSetup.setup(fontInfo, useComplexScriptFeatures);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,42 @@

package org.apache.fop.svg;

import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.assertEquals;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

import org.apache.fop.configuration.Configuration;
import org.apache.fop.configuration.ConfigurationException;
import org.apache.fop.configuration.DefaultConfigurationBuilder;

/**
* Basic runtime test for the PDF transcoder. It is used to verify that
* nothing obvious is broken after compiling.
*/
public class BasicPDFTranscoderTestCase extends AbstractBasicTranscoderTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Override
protected Transcoder createTranscoder() {
return new PDFTranscoder();
Expand All @@ -52,4 +74,70 @@ public void testFontAutoDetect() {
autoDetectConf.getClass().getSimpleName());
}

@Test
public void testFontSubstitution() throws ConfigurationException, IOException, TranscoderException {

PDFTranscoder transcoder = (PDFTranscoder) createTranscoder();

DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
String cfgFragment =
"<pdf-renderer>"
+ "<fonts>"
+ "<substitutions>"
+ "<substitution>"
+ "<from font-family=\"Helvetica\"/>"
+ "<to font-family=\"Courier\"/>"
+ "</substitution>"
+ "</substitutions>"
+ "</fonts>"
+ "</pdf-renderer>";
Configuration cfg = cfgBuilder.build(new ByteArrayInputStream(cfgFragment.getBytes()));
transcoder.configure(cfg);

String svgFragment = "<svg xml:space=\"preserve\" x=\"-1.70458in\" y=\"0.198315in\" "
+ "width=\"2.6622in\" height=\"1.89672in\""
+ " viewBox=\"-4330 0 6762 4818\" xmlns=\"http://www.w3.org/2000/svg\">"
+ " <text x=\"-3653\" y=\"841\" style=\"fill:#1F1A17;font-size:639;font-family:Helvetica\">H</text>"
+ "</svg>";
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgFragment.getBytes()));

File outputFile = tempFolder.newFile("output.pdf");
OutputStream os = new FileOutputStream(outputFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not a bytearrayoutputstream

TranscoderOutput output = new TranscoderOutput(os);

try {
transcoder.transcode(input, output);
} finally {
os.close();
}

PDDocument pdfDocument = null;
try {
pdfDocument = Loader.loadPDF(outputFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try (x pdfDocument = Loader.loadPDF(outputFile)) {
xxx
}

FontExtractor fontExtractor = new FontExtractor();
fontExtractor.getText(pdfDocument);
assertEquals("Courier", fontExtractor.getFontUsage().get("H"));
} finally {
if (pdfDocument != null) {
pdfDocument.close();
}
}
}

class FontExtractor extends PDFTextStripper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt this be static


private Map<String, String> fontUsage = new HashMap<>();

@Override
protected void processTextPosition(TextPosition text) {
String fontName = text.getFont().getName();
fontUsage.put(text.toString(), fontName);
super.processTextPosition(text);
}

public Map<String, String> getFontUsage() {
return fontUsage;
}
}

}