1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.export.core.pdf;
14 import java.util.concurrent.atomic.AtomicBoolean;
16 import com.lowagie.text.FontFactory;
17 import com.lowagie.text.pdf.DefaultFontMapper;
18 import com.lowagie.text.pdf.FontMapper;
21 * A utility class for constructing properly initialize iText {@link FontMapper}
24 * @author Tuukka Lehtonen
27 public class FontMapping {
29 private static final AtomicBoolean fontFactoryInitialized = new AtomicBoolean();
31 public static FontMapper defaultFontMapper() {
32 // NOTE: recreation is intentional because users might add fonts between exports.
33 // At some point, perhaps we could listen to file system changes in these locations
34 // and only then recreate the mapper.
35 return createDefaultFontMapper();
38 public static FontMapper createDefaultFontMapper() {
39 initializeFontFactory();
40 DefaultFontMapper mapper = new DefaultFontMapper();
41 insertDirectories(mapper);
45 private static void insertDirectories(DefaultFontMapper mapper) {
46 switch (OSType.calculate()) {
48 mapper.insertDirectory("/Library/Fonts");
49 mapper.insertDirectory("/System/Library/Fonts");
53 mapper.insertDirectory("/usr/share/X11/fonts");
54 mapper.insertDirectory("/usr/X/lib/X11/fonts");
55 mapper.insertDirectory("/usr/openwin/lib/X11/fonts");
56 mapper.insertDirectory("/usr/share/fonts");
57 mapper.insertDirectory("/usr/X11R6/lib/X11/fonts");
61 String windir = System.getenv("WINDIR");
62 if (windir != null && !windir.isEmpty()) {
63 mapper.insertDirectory(windir + "\\Fonts");
72 private static void initializeFontFactory() {
73 if (!fontFactoryInitialized.compareAndSet(false, true))
76 switch (OSType.calculate()) {
78 FontFactory.registerDirectory("/Library/Fonts");
79 FontFactory.registerDirectory("/System/Library/Fonts");
83 FontFactory.registerDirectory("/usr/share/X11/fonts", true);
84 FontFactory.registerDirectory("/usr/X/lib/X11/fonts", true);
85 FontFactory.registerDirectory("/usr/openwin/lib/X11/fonts", true);
86 FontFactory.registerDirectory("/usr/share/fonts", true);
87 FontFactory.registerDirectory("/usr/X11R6/lib/X11/fonts", true);
91 String windir = System.getenv("WINDIR");
92 if (windir != null && !windir.isEmpty()) {
93 FontFactory.registerDirectory(windir + "\\Fonts");
103 APPLE, LINUX, SUN, WINDOWS, UNKNOWN;
105 public static OSType calculate() {
106 String osName = System.getProperty("os.name");
107 assert osName != null;
108 osName = osName.toLowerCase();
109 if (osName.startsWith("mac os x"))
111 if (osName.startsWith("windows"))
113 if (osName.startsWith("linux"))
115 if (osName.startsWith("sun"))