FontHelper.java
9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.image.internal.font;
import com.day.image.font.AbstractFont;
import com.day.image.font.FontListEntry;
import com.day.image.internal.font.FontFileProvider;
import com.day.image.internal.font.FontProvider;
import com.day.image.internal.font.PlatformFont;
import com.day.image.internal.font.resource.ResourceFontProvider;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
@Component(name="com.day.cq.image.internal.font.FontHelper", metatype=1, label="Day Commons GFX Font Helper", description="Configures the GFX Font Helper which helps managing the font path and caching of font information.")
public class FontHelper {
private static FontHelper INSTANCE;
private final Logger log;
@Property(cardinality=Integer.MAX_VALUE, label="Font Path", description="The list of Resource Tree locations providing fonts. Multiple entries may be listed. Each entry must be an absolute path; that is empty entries or entries not starting with a slash (/) character are ignored. The location of each entry must be existing. This may cause the creation of JCR Nodes at configured entry paths.")
private static final String FONTPATH = "fontpath";
private static final int DEFAULT_OVERSAMPLING_FACTOR = 16;
@Property(intValue={16}, label="Oversampling Factor", description="The factor to apply internally to the font when drawing text with the TTOVERSAMPLING flag set. Default value is 16 which is also the value used by the Fonter.exe tool to prepare FonterFonts.")
private static final String OVERSAMPLING_FACTOR = "oversamplingFactor";
@Reference
private SlingRepository slingRepository;
@Reference
private JcrResourceResolverFactory resourceResolverFactory;
@Reference
private FontFileProvider fontFileProvider;
private ResourceResolver resourceResolver;
private String[] fontPath;
private FontProvider[] fontProviders;
public FontHelper() {
this.log = LoggerFactory.getLogger(this.getClass());
this.fontPath = new String[0];
this.fontProviders = new FontProvider[0];
}
protected void activate(ComponentContext context) {
try {
Session session = this.slingRepository.loginAdministrative(null);
this.resourceResolver = this.resourceResolverFactory.getResourceResolver(session);
}
catch (RepositoryException re) {
this.log.error("activate: Cannot get administrative session to access fonts !", (Throwable)re);
}
Dictionary props = context.getProperties();
this.initFontPath(OsgiUtil.toStringArray(props.get("fontpath")));
PlatformFont.setOversamplingFactor(OsgiUtil.toInteger(props.get("oversamplingFactor"), (int)16));
INSTANCE = this;
if (this.resourceResolver != null) {
this.addFontProvider(new ResourceFontProvider());
}
}
protected void deactivate(ComponentContext context) {
for (FontProvider fontProvider : this.fontProviders) {
fontProvider.destroy();
}
if (INSTANCE == this) {
INSTANCE = null;
}
if (this.resourceResolver != null) {
Session session = (Session)this.resourceResolver.adaptTo(Session.class);
if (session != null) {
session.logout();
}
this.resourceResolver = null;
}
this.fontProviders = new FontProvider[0];
this.fontPath = new String[0];
}
protected void addFontProvider(FontProvider provider) {
provider.init(this.resourceResolver, this.getFontPath(), this.fontFileProvider);
if (this.fontProviders.length == 0) {
this.fontProviders = new FontProvider[]{provider};
} else {
FontProvider[] newfp = new FontProvider[this.fontProviders.length + 1];
System.arraycopy(this.fontProviders, 0, newfp, 0, this.fontProviders.length);
newfp[this.fontProviders.length] = provider;
this.fontProviders = newfp;
}
}
protected void removeFontProvider(FontProvider provider) {
for (int i = 0; i < this.fontProviders.length; ++i) {
if (this.fontProviders[i] != provider) continue;
provider.destroy();
if (this.fontProviders.length == 1) {
this.fontProviders = new FontProvider[0];
continue;
}
FontProvider[] newfp = new FontProvider[this.fontProviders.length - 1];
if (i > 0) {
System.arraycopy(this.fontProviders, 0, newfp, 0, i);
}
if (i >= this.fontProviders.length - 1) continue;
System.arraycopy(this.fontProviders, i + 1, newfp, i, this.fontProviders.length - i);
}
}
public static FontHelper getInstance() {
if (INSTANCE == null) {
return new FontHelper();
}
return INSTANCE;
}
String[] getFontPath() {
return this.fontPath;
}
FontProvider[] getFontProviders() {
return this.fontProviders;
}
public List<FontListEntry> getFontList() {
ArrayList<FontListEntry> fontList = new ArrayList<FontListEntry>();
for (FontProvider fontProvider : this.getFontProviders()) {
fontList.addAll(fontProvider.getFontList());
}
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
for (String platFont : env.getAvailableFontFamilyNames()) {
fontList.add(new FontListEntry("[Java Platform]", platFont, 0, 255));
}
return fontList;
}
public AbstractFont getFont(String faceName, int size, int style) {
AbstractFont fnt = null;
for (int i = 0; i < this.fontProviders.length; ++i) {
this.log.debug("Font: Asking provider {}", (Object)this.fontProviders[i]);
fnt = this.fontProviders[i].getFont(faceName, size, style);
if (fnt == null) continue;
this.log.debug("Font: Using font {}", (Object)fnt);
break;
}
if (fnt == null) {
this.log.debug("Font: No provider has font, fallback to base class");
fnt = new PlatformFont(faceName, size, style);
}
return fnt;
}
private void initFontPath(String[] fontPathConfig) {
if (fontPathConfig != null && fontPathConfig.length > 0) {
ArrayList<String> fontPathList = new ArrayList<String>();
for (String fontPathEntry2 : fontPathConfig) {
if (fontPathEntry2 == null || fontPathEntry2.length() == 0) {
this.log.info("initFontPath: Ignoring empty font path entry");
continue;
}
if (fontPathEntry2.charAt(0) != '/') {
this.log.info("initFontPath: Ignoring non-absolute font path entry {}", (Object)fontPathEntry2);
continue;
}
fontPathList.add(fontPathEntry2);
}
this.fontPath = fontPathList.toArray(new String[fontPathList.size()]);
if (this.log.isDebugEnabled()) {
this.log.debug("Communique font settings:");
for (String fontPathEntry2 : this.fontPath) {
this.log.debug(" Path entry {}", (Object)fontPathEntry2);
}
}
} else {
this.log.debug("initFontPath: Font path not configured");
this.fontPath = new String[0];
}
}
protected void bindSlingRepository(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
protected void unbindSlingRepository(SlingRepository slingRepository) {
if (this.slingRepository == slingRepository) {
this.slingRepository = null;
}
}
protected void bindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.resourceResolverFactory = jcrResourceResolverFactory;
}
protected void unbindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.resourceResolverFactory == jcrResourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindFontFileProvider(FontFileProvider fontFileProvider) {
this.fontFileProvider = fontFileProvider;
}
protected void unbindFontFileProvider(FontFileProvider fontFileProvider) {
if (this.fontFileProvider == fontFileProvider) {
this.fontFileProvider = null;
}
}
}