1 /*
2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
164 */
165
166 private int familyWidth = 0;
167 private boolean preferredWidth(Font2D font) {
168
169 int newWidth = font.getWidth();
170
171 if (familyWidth == 0) {
172 familyWidth = newWidth;
173 return true;
174 }
175
176 if (newWidth == familyWidth) {
177 return true;
178 }
179
180 if (Math.abs(Font2D.FWIDTH_NORMAL - newWidth) <
181 Math.abs(Font2D.FWIDTH_NORMAL - familyWidth))
182 {
183 if (FontUtilities.debugFonts()) {
184 FontUtilities.getLogger().info(
185 "Found more preferred width. New width = " + newWidth +
186 " Old width = " + familyWidth + " in font " + font +
187 " nulling out fonts plain: " + plain + " bold: " + bold +
188 " italic: " + italic + " bolditalic: " + bolditalic);
189 }
190 familyWidth = newWidth;
191 plain = bold = italic = bolditalic = null;
192 return true;
193 } else if (FontUtilities.debugFonts()) {
194 FontUtilities.getLogger().info(
195 "Family rejecting font " + font +
196 " of less preferred width " + newWidth);
197 }
198 return false;
199 }
200
201 private boolean closerWeight(Font2D currFont, Font2D font, int style) {
202 if (familyWidth != font.getWidth()) {
203 return false;
204 }
205
206 if (currFont == null) {
207 return true;
208 }
209
210 if (FontUtilities.debugFonts()) {
211 FontUtilities.getLogger().info(
212 "New weight for style " + style + ". Curr.font=" + currFont +
213 " New font="+font+" Curr.weight="+ + currFont.getWeight()+
214 " New weight="+font.getWeight());
215 }
216
217 int newWeight = font.getWeight();
218 switch (style) {
219 case Font.PLAIN:
220 case Font.ITALIC:
221 return (newWeight <= Font2D.FWEIGHT_NORMAL &&
222 newWeight > currFont.getWeight());
223
224 case Font.BOLD:
225 case Font.BOLD|Font.ITALIC:
226 return (Math.abs(newWeight - Font2D.FWEIGHT_BOLD) <
227 Math.abs(currFont.getWeight() - Font2D.FWEIGHT_BOLD));
228
229 default:
230 return false;
231 }
232 }
233
234 public void setFont(Font2D font, int style) {
235
236 if (FontUtilities.isLogging()) {
237 String msg;
238 if (font instanceof CompositeFont) {
239 msg = "Request to add " + font.getFamilyName(null) +
240 " with style " + style + " to family " + familyName;
241 } else {
242 msg = "Request to add " + font +
243 " with style " + style + " to family " + this;
244 }
245 FontUtilities.getLogger().info(msg);
246 }
247 /* Allow a lower-rank font only if its a file font
248 * from the exact same source as any previous font.
249 */
250 if ((font.getRank() > familyRank) && !isFromSameSource(font)) {
251 if (FontUtilities.isLogging()) {
252 FontUtilities.getLogger()
253 .warning("Rejecting adding " + font +
254 " of lower rank " + font.getRank() +
255 " to family " + this +
256 " of rank " + familyRank);
257 }
258 return;
259 }
260
261 switch (style) {
262
263 case Font.PLAIN:
264 if (preferredWidth(font) && closerWeight(plain, font, style)) {
265 plain = font;
266 }
267 break;
268
269 case Font.BOLD:
270 if (preferredWidth(font) && closerWeight(bold, font, style)) {
271 bold = font;
272 }
273 break;
274
275 case Font.ITALIC:
276 if (preferredWidth(font) && closerWeight(italic, font, style)) {
277 italic = font;
|
1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
164 */
165
166 private int familyWidth = 0;
167 private boolean preferredWidth(Font2D font) {
168
169 int newWidth = font.getWidth();
170
171 if (familyWidth == 0) {
172 familyWidth = newWidth;
173 return true;
174 }
175
176 if (newWidth == familyWidth) {
177 return true;
178 }
179
180 if (Math.abs(Font2D.FWIDTH_NORMAL - newWidth) <
181 Math.abs(Font2D.FWIDTH_NORMAL - familyWidth))
182 {
183 if (FontUtilities.debugFonts()) {
184 FontUtilities.logInfo(
185 "Found more preferred width. New width = " + newWidth +
186 " Old width = " + familyWidth + " in font " + font +
187 " nulling out fonts plain: " + plain + " bold: " + bold +
188 " italic: " + italic + " bolditalic: " + bolditalic);
189 }
190 familyWidth = newWidth;
191 plain = bold = italic = bolditalic = null;
192 return true;
193 } else if (FontUtilities.debugFonts()) {
194 FontUtilities.logInfo(
195 "Family rejecting font " + font +
196 " of less preferred width " + newWidth);
197 }
198 return false;
199 }
200
201 private boolean closerWeight(Font2D currFont, Font2D font, int style) {
202 if (familyWidth != font.getWidth()) {
203 return false;
204 }
205
206 if (currFont == null) {
207 return true;
208 }
209
210 if (FontUtilities.debugFonts()) {
211 FontUtilities.logInfo(
212 "New weight for style " + style + ". Curr.font=" + currFont +
213 " New font="+font+" Curr.weight="+ + currFont.getWeight()+
214 " New weight="+font.getWeight());
215 }
216
217 int newWeight = font.getWeight();
218 switch (style) {
219 case Font.PLAIN:
220 case Font.ITALIC:
221 return (newWeight <= Font2D.FWEIGHT_NORMAL &&
222 newWeight > currFont.getWeight());
223
224 case Font.BOLD:
225 case Font.BOLD|Font.ITALIC:
226 return (Math.abs(newWeight - Font2D.FWEIGHT_BOLD) <
227 Math.abs(currFont.getWeight() - Font2D.FWEIGHT_BOLD));
228
229 default:
230 return false;
231 }
232 }
233
234 public void setFont(Font2D font, int style) {
235
236 if (FontUtilities.isLogging()) {
237 String msg;
238 if (font instanceof CompositeFont) {
239 msg = "Request to add " + font.getFamilyName(null) +
240 " with style " + style + " to family " + familyName;
241 } else {
242 msg = "Request to add " + font +
243 " with style " + style + " to family " + this;
244 }
245 FontUtilities.logInfo(msg);
246 }
247 /* Allow a lower-rank font only if its a file font
248 * from the exact same source as any previous font.
249 */
250 if ((font.getRank() > familyRank) && !isFromSameSource(font)) {
251 FontUtilities.logWarning("Rejecting adding " + font +
252 " of lower rank " + font.getRank() +
253 " to family " + this +
254 " of rank " + familyRank);
255 return;
256 }
257
258 switch (style) {
259
260 case Font.PLAIN:
261 if (preferredWidth(font) && closerWeight(plain, font, style)) {
262 plain = font;
263 }
264 break;
265
266 case Font.BOLD:
267 if (preferredWidth(font) && closerWeight(bold, font, style)) {
268 bold = font;
269 }
270 break;
271
272 case Font.ITALIC:
273 if (preferredWidth(font) && closerWeight(italic, font, style)) {
274 italic = font;
|