-
Notifications
You must be signed in to change notification settings - Fork 33
/
FontBuilder.java
158 lines (137 loc) · 5.89 KB
/
FontBuilder.java
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
/**
* Get more info at : www.jrebirth.org .
* Copyright JRebirth.org © 2011-2013
* Contact : sebastien.bordes@jrebirth.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jrebirth.af.core.resource.font;
import java.net.URL;
import java.util.List;
import javafx.scene.text.Font;
import org.jrebirth.af.core.resource.Resources;
import org.jrebirth.af.core.resource.builder.AbstractResourceBuilder;
import org.jrebirth.af.core.resource.provided.JRebirthParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The class <strong>FontBuilder</strong>.
*
* Class used to manage font with weak reference.
*
* @author Sébastien Bordes
*/
public final class FontBuilder extends AbstractResourceBuilder<FontItem, FontParams, Font> {
/** The class logger. */
private static final Logger LOGGER = LoggerFactory.getLogger(FontBuilder.class);
/**
* {@inheritDoc}
*/
@Override
protected Font buildResource(final FontItem fontItem, final FontParams jrFont) {
Font font = null;
if (jrFont instanceof RealFont) {
// Build the requested font
font = buildRealFont((RealFont) jrFont);
} else if (jrFont instanceof FamilyFont) {
// Build a family like font
font = buildFamilyFont((FamilyFont) jrFont);
} else {
// Return the default font
font = Font.getDefault();
}
return font;
}
/**
* Build a real font with name and size.
*
* @param rFont the real font enum
*
* @return the javafx font
*/
private Font buildRealFont(final RealFont rFont) {
checkFontStatus(rFont);
return javafx.scene.text.FontBuilder.create()
.name(transformFontName(rFont.name().name()))
.size(rFont.size())
.build();
}
/**
* Build a Family Font with name and size.
*
* @param familyFont the family font enum
*
* @return the javafx font
*/
private Font buildFamilyFont(final FamilyFont familyFont) {
Font font = null;
if (familyFont.posture() == null && familyFont.weight() == null) {
font = Font.font(transformFontName(familyFont.family()), familyFont.size());
} else if (familyFont.posture() == null) {
font = Font.font(transformFontName(familyFont.family()), familyFont.weight(), familyFont.size());
} else if (familyFont.weight() == null) {
font = Font.font(transformFontName(familyFont.family()), familyFont.posture(), familyFont.size());
} else {
font = Font.font(transformFontName(familyFont.family()), familyFont.weight(), familyFont.posture(), familyFont.size());
}
return font;
}
/**
* TRansform the font name by replacing _ by space.
*
* @param fontName the font name to transform
* @return the transformed font
*/
private String transformFontName(final String fontName) {
return fontName.replace("_", " ");
}
/**
* Load the font file.
*
* @param realFont the name of the font to load
*/
private void checkFontStatus(final FontParams realFont) {
// Try to load system fonts
final List<String> fonts = Font.getFontNames(transformFontName(realFont.name().name()));
Font font = null;
String fontName = null;
if (fonts.isEmpty()) {
List<String> fontPaths = JRebirthParameters.FONT_FOLDER.get();
for(int i = 0 ; i < fontPaths.size() && font == null ;i++){
String fontPath = fontPaths.get(i);
// This variable will hold the 2 alternative font names
fontName = fontPath + Resources.PATH_SEP + transformFontName(realFont.name().name()) + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();
LOGGER.trace("Try to load Transformed Font {}", fontName);
font = Font.loadFont(Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());
// The font name contains '_' in its file name to replace ' '
if (font == null) {
fontName = JRebirthParameters.FONT_FOLDER.get() + Resources.PATH_SEP + realFont.name().name() + JRebirthParameters.TRUE_TYPE_FONT_EXTENSION.get();
LOGGER.trace("Try to load Raw Font {}", fontName);
font = Font.loadFont(
Thread.currentThread().getContextClassLoader().getResourceAsStream(fontName), realFont.size());
if (font != null) {
// Raw font has been loaded
LOGGER.info("{} Raw Font loaded", fontName);
}
} else {
// Transformed font has been loaded
LOGGER.info("{} Transformed Font loaded", fontName);
}
}
if (font == null) {
// Neither transformed nor raw font has been loaded (with or without '_')
LOGGER.error("Font : {} not found into base folder: {}", realFont.name().name(), JRebirthParameters.FONT_FOLDER.get());
}
}
}
}