]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/FontMapping.java
ConstantLabelDecorationRule to avoid Display in creating FontDescriptors
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / pdf / FontMapping.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.export.core.pdf;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import com.lowagie.text.FontFactory;
17 import com.lowagie.text.pdf.DefaultFontMapper;
18 import com.lowagie.text.pdf.FontMapper;
19
20 /**
21  * A utility class for constructing properly initialize iText {@link FontMapper}
22  * instances.
23  * 
24  * @author Tuukka Lehtonen
25  * @since 1.28.0
26  */
27 public class FontMapping {
28
29         private static final AtomicBoolean fontFactoryInitialized = new AtomicBoolean();
30
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(); 
36         }
37
38         public static FontMapper createDefaultFontMapper() {
39                 initializeFontFactory();
40                 DefaultFontMapper mapper = new DefaultFontMapper();
41                 insertDirectories(mapper);
42                 return mapper;
43         }
44
45         private static void insertDirectories(DefaultFontMapper mapper) {
46                 switch (OSType.calculate()) {
47                 case APPLE:
48                         mapper.insertDirectory("/Library/Fonts");
49                         mapper.insertDirectory("/System/Library/Fonts");
50
51                 case LINUX:
52                 case SUN:
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");
58                         break;
59
60                 case WINDOWS:
61                         String windir = System.getenv("WINDIR");
62                         if (windir != null && !windir.isEmpty()) {
63                                 mapper.insertDirectory(windir + "\\Fonts");
64                         }
65                         break;
66
67                 default:
68                         break;
69                 }
70         }
71
72         private static void initializeFontFactory() {
73                 if (!fontFactoryInitialized.compareAndSet(false, true))
74                         return;
75
76                 switch (OSType.calculate()) {
77                 case APPLE:
78                         FontFactory.registerDirectory("/Library/Fonts");
79                         FontFactory.registerDirectory("/System/Library/Fonts");
80
81                 case LINUX:
82                 case SUN:
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);
88                         break;
89
90                 case WINDOWS:
91                         String windir = System.getenv("WINDIR");
92                         if (windir != null && !windir.isEmpty()) {
93                                 FontFactory.registerDirectory(windir + "\\Fonts");
94                         }
95                         break;
96
97                 default:
98                         break;
99                 }
100         }
101
102         static enum OSType {
103                 APPLE, LINUX, SUN, WINDOWS, UNKNOWN;
104
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"))
110                                 return APPLE;
111                         if (osName.startsWith("windows"))
112                                 return WINDOWS;
113                         if (osName.startsWith("linux"))
114                                 return LINUX;
115                         if (osName.startsWith("sun"))
116                                 return SUN;
117                         return UNKNOWN;
118                 }
119         }
120
121 }