embed free monospaced font

This commit is contained in:
Benjamin Gamard 2017-07-31 14:40:35 +02:00
parent 14f8239ba3
commit e1e1b4e278
4 changed files with 27 additions and 3 deletions

View File

@ -3,7 +3,7 @@ dist: trusty
language: java
before_install:
- sudo apt-get -qq update
- sudo apt-get -y -q install tesseract-ocr tesseract-ocr-fra tesseract-ocr-jpn ttf-mscorefonts-installer
- sudo apt-get -y -q install tesseract-ocr tesseract-ocr-fra tesseract-ocr-jpn
- sudo apt-get -y -q install haveged && sudo service haveged start
after_success:
- mvn -Pprod -DskipTests clean install

View File

@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.lowagie.text.FontFactory;
import com.sismics.docs.core.constant.ConfigType;
import com.sismics.docs.core.dao.jpa.ConfigDao;
import com.sismics.docs.core.listener.async.DocumentCreatedAsyncListener;
@ -20,6 +21,7 @@ import com.sismics.docs.core.listener.async.RebuildIndexAsyncListener;
import com.sismics.docs.core.listener.sync.DeadEventListener;
import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.core.service.IndexingService;
import com.sismics.docs.core.util.PdfUtil;
import com.sismics.util.EnvironmentUtil;
/**
@ -58,11 +60,14 @@ public class AppContext {
*/
private AppContext() {
resetEventBus();
// Start indexing service
ConfigDao configDao = new ConfigDao();
Config luceneStorageConfig = configDao.getById(ConfigType.LUCENE_DIRECTORY_STORAGE);
indexingService = new IndexingService(luceneStorageConfig != null ? luceneStorageConfig.getValue() : null);
indexingService.startAsync();
PdfUtil.registerFonts();
}
/**

View File

@ -2,8 +2,10 @@ package com.sismics.docs.core.util;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Closer;
import com.google.common.io.Resources;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
@ -33,6 +35,7 @@ import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
@ -122,7 +125,7 @@ public class PdfUtil {
output.open();
String content = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
Font font = FontFactory.getFont(FontFactory.COURIER);
Font font = FontFactory.getFont("LiberationMono-Regular");
Paragraph paragraph = new Paragraph(content, font);
paragraph.setAlignment(Element.ALIGN_LEFT);
output.add(paragraph);
@ -315,4 +318,20 @@ public class PdfUtil {
return renderer.renderImage(0);
}
}
/**
* Register fonts.
*/
public static void registerFonts() {
URL url = Resources.getResource("fonts/LiberationMono-Regular.ttf");
try (InputStream is = url.openStream()) {
Path file = Files.createTempFile("sismics_docs_font_mono", ".ttf");
try (OutputStream os = Files.newOutputStream(file)) {
ByteStreams.copy(is, os);
}
FontFactory.register(file.toAbsolutePath().toString(), "LiberationMono-Regular");
} catch (IOException e) {
log.error("Error loading font", e);
}
}
}