ResourceFontProvider.java
5.83 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.observation.EventIterator
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.image.internal.font.resource;
import com.day.image.font.AbstractFont;
import com.day.image.font.FontListEntry;
import com.day.image.internal.FontPath;
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.ResourceFontCache;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.jcr.observation.EventIterator;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class ResourceFontProvider
implements FontProvider {
private final Logger log;
private ResourceFontCache fontCache;
private ResourceResolver ticket;
private FontFileProvider fontFileProvider;
public ResourceFontProvider() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public List<FontListEntry> getFontList() {
return this.fontCache.getFontList();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public AbstractFont getFont(String faceName, int requestedSize, int style) {
ResourceFontCache resourceFontCache = this.fontCache;
synchronized (resourceFontCache) {
int fontSize = PlatformFont.scaleFontSize(requestedSize, style);
String fontName = AbstractFont.createFontFileName(faceName, fontSize, style);
this.log.debug("findFont: Try {} from cache", (Object)fontName);
Font awtFont = (Font)this.fontCache.get(fontName);
if (awtFont == null) {
this.log.debug("findFont: Derive size from {}/{}", (Object)faceName, (Object)AbstractFont.styleToDescription(style));
awtFont = this.loadBaseFont(faceName, style);
if (awtFont != null) {
awtFont = awtFont.deriveFont((float)fontSize);
this.log.debug("findFont: Caching new instance for {}", (Object)fontName);
this.fontCache.put(fontName, awtFont);
}
}
if (awtFont != null) {
return new PlatformFont(faceName, requestedSize, style, awtFont);
}
}
return null;
}
@Override
public void init(ResourceResolver resolver, String[] fontPath, FontFileProvider ffp) {
this.ticket = resolver;
this.fontFileProvider = ffp;
FontPath fp = new FontPath(this.ticket, fontPath);
this.fontCache = new ResourceFontCache(this.ticket, fontPath, ffp);
}
@Override
public void onEvent(EventIterator events) {
}
@Override
public void destroy() {
if (this.fontCache != null) {
this.fontCache.destroy();
this.fontCache = null;
}
}
private Font loadBaseFont(String family, int style) {
String baseFont = null;
String fontName = AbstractFont.createFontFileName(family, 0, style);
this.log.debug("loadBaseFont: Try {} from cache", (Object)fontName);
Font awtFont = (Font)this.fontCache.get(fontName);
if (awtFont != null) {
this.log.debug("loadBaseFont: Got it");
return awtFont;
}
String handle = this.fontCache.getFontHandle(fontName);
if (handle == null) {
this.log.debug("loadBaseFont: Derive style for {}/{} from plain {}", (Object)family, (Object)AbstractFont.styleToDescription(style));
baseFont = AbstractFont.createFontFileName(family, 0, 0);
awtFont = (Font)this.fontCache.get(baseFont);
if (awtFont != null) {
this.log.debug("loadBaseFont: Caching and returning derived font style");
awtFont = awtFont.deriveFont(style);
this.fontCache.put(fontName, awtFont);
return awtFont;
}
this.log.debug("loadBaseFont: Check mapping for plain {}", (Object)family);
handle = this.fontCache.getFontHandle(baseFont);
if (handle == null) {
this.log.debug("loadBaseFont: Not even found mapping for plain {}", (Object)family);
return null;
}
}
try {
this.log.debug("loadBaseFont: Load font from {}", (Object)handle);
Resource res = this.ticket.getResource(handle);
InputStream fi = (InputStream)res.adaptTo(InputStream.class);
if (fi == null) {
throw new IOException("Resource does not adapt to InputStream:" + res.getPath());
}
Font af = Font.createFont(0, this.fontFileProvider.getFileForStream(fi));
if (baseFont != null) {
this.log.debug("loadBaseFont: Cache plain style base and derive");
this.fontCache.put(baseFont, af);
af = af.deriveFont(style);
}
this.log.debug("loadBaseFont: Cache desired font with style");
this.fontCache.put(fontName, af);
return af;
}
catch (IOException ioe) {
this.log.warn("IO problem loading the font from " + handle, (Throwable)ioe);
}
catch (FontFormatException ffe) {
this.log.warn("The font page " + handle + " does not seem to contain a valid TrueType font", (Throwable)ffe);
}
return null;
}
}