1 /*
2 * Copyright (c) 2008, 2014, 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
23 * questions.
24 */
25
26 package javafx.scene.text;
27
28 import java.io.FilePermission;
29 import java.io.InputStream;
30 import java.net.URL;
31 import java.net.URLConnection;
32 import java.util.List;
33
34 import com.sun.javafx.tk.Toolkit;
35 import javafx.beans.NamedArg;
36
37 /**
38 * <p>The {@code Font} class represents fonts, which are used to render text on
39 * screen.
40 * <p>
41 * The size of a {@code Font} is described as being specified in points
42 * which are a real world measurement of approximately 1/72 inch.
43 * <p>
44 * Given that fonts scale with the rendering transform as determined
45 * by the transform attributes of a {@code Node} using the {@code Font}
46 * and its ancestors, the size will actually be relative to the local
47 * coordinate space of the node, which should provide coordinates
48 * similar to the size of a point if no scaling transforms are present
49 * in the environment of the node.
50 * Note that the real world distances specified by the default coordinate
51 * system only approximate point sizes as a rule of thumb and are typically
52 * defaulted to screen pixels for most displays.
53 * <p>
54 * For more information see {@link javafx.scene.Node} for more information
55 * on the default coordinate system
56 * @since JavaFX 2.0
57 */
58 public final class Font {
59
60 private static final String DEFAULT_FAMILY = "System";
61 private static final String DEFAULT_FULLNAME = "System Regular";
62
63 /**
64 * The default font for this platform. This is used whenever a font is not
65 * specifically set on a Text node or overridden by CSS.
66 */
67 private static float defaultSystemFontSize = -1;
68 private static float getDefaultSystemFontSize() {
69 if (defaultSystemFontSize == -1) {
70 defaultSystemFontSize =
71 Toolkit.getToolkit().getFontLoader().getSystemFontSize();
72 }
73 return defaultSystemFontSize;
74 }
75
76 private static Font DEFAULT;
77 /**
78 * Gets the default font which will be from the family "System",
79 * and typically the style "Regular", and be of a size consistent
298 this(null, size);
299 }
300
301
302 /**
303 * Constructs a font using the specified full face name and size
304 * @param name full name of the font.
305 * @param size the font size to use
306 */
307 public Font(@NamedArg("name") String name, @NamedArg("size") double size) {
308 this.name = name;
309 this.size = size;
310
311 if (name == null || "".equals(name)) this.name = DEFAULT_FULLNAME;
312 if (size < 0f) this.size = getDefaultSystemFontSize();
313 // if a search was done based on family + style information, then the
314 // native font has already been located and specified. Likewise if the
315 // Font was created based on an existing native font. If however a Font
316 // was created directly in FX, then we need to find the native font
317 // to use. This call will also set the family and style by invoking
318 // the impl_setNativeFont callback method.
319 Toolkit.getToolkit().getFontLoader().loadFont(this);
320 }
321
322 /**
323 * Private constructor for internal implementation
324 *
325 * @param f native font
326 * @param family font family name
327 * @param name font full name
328 * @param style style string
329 * @param size font size
330 */
331 private Font(Object f, String family, String name, String style, double size) {
332 this.nativeFont = f;
333 this.family = family;
334 this.name = name;
335 this.style = style;
336 this.size = size;
337 }
338
484 }
485 return false;
486 }
487
488 /**
489 * Returns a hash code for this {@code Font} object.
490 * @return a hash code for this {@code Font} object.
491 */
492 @Override public int hashCode() {
493 if (hash == 0) {
494 long bits = 17L;
495 bits = 37L * bits + name.hashCode();
496 bits = 37L * bits + Double.doubleToLongBits(size);
497 hash = (int) (bits ^ (bits >> 32));
498 }
499 return hash;
500 }
501
502 private Object nativeFont;
503
504 /**
505 * @treatAsPrivate implementation detail
506 * @deprecated This is an internal API that is not intended for use and will be removed in the next version
507 */
508 @Deprecated
509 public Object impl_getNativeFont() { return nativeFont; }
510
511 /**
512 * @treatAsPrivate implementation detail
513 * @deprecated This is an internal API that is not intended for use and will be removed in the next version
514 */
515 @Deprecated
516 public void impl_setNativeFont(Object f, String nam, String fam, String styl) {
517 nativeFont = f;
518 name = nam;
519 family = fam;
520 style = styl;
521 }
522
523 /**
524 * @treatAsPrivate implementation detail
525 * @deprecated This is an internal API that is not intended for use and will be removed in the next version
526 */
527 @Deprecated
528 public static Font impl_NativeFont(Object f, String name, String family,
529 String style, double size) {
530 Font retFont = new Font( f, family, name, style, size);
531 return retFont;
532 }
533 }
|
1 /*
2 * Copyright (c) 2008, 2016, 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
23 * questions.
24 */
25
26 package javafx.scene.text;
27
28 import com.sun.javafx.text.FontHelper;
29 import java.io.FilePermission;
30 import java.io.InputStream;
31 import java.net.URL;
32 import java.net.URLConnection;
33 import java.util.List;
34
35 import com.sun.javafx.tk.Toolkit;
36 import javafx.beans.NamedArg;
37
38 /**
39 * <p>The {@code Font} class represents fonts, which are used to render text on
40 * screen.
41 * <p>
42 * The size of a {@code Font} is described as being specified in points
43 * which are a real world measurement of approximately 1/72 inch.
44 * <p>
45 * Given that fonts scale with the rendering transform as determined
46 * by the transform attributes of a {@code Node} using the {@code Font}
47 * and its ancestors, the size will actually be relative to the local
48 * coordinate space of the node, which should provide coordinates
49 * similar to the size of a point if no scaling transforms are present
50 * in the environment of the node.
51 * Note that the real world distances specified by the default coordinate
52 * system only approximate point sizes as a rule of thumb and are typically
53 * defaulted to screen pixels for most displays.
54 * <p>
55 * For more information see {@link javafx.scene.Node} for more information
56 * on the default coordinate system
57 * @since JavaFX 2.0
58 */
59 public final class Font {
60
61 static {
62 // This is used by classes in different packages to get access to
63 // private and package private methods.
64 FontHelper.setFontAccessor(new FontHelper.FontAccessor() {
65
66 @Override
67 public Object getNativeFont(Font font) {
68 return font.getNativeFont();
69 }
70
71 @Override
72 public void setNativeFont(Font font, Object f, String nam, String fam, String styl) {
73 font.setNativeFont(f, nam, fam, styl);
74 }
75
76 @Override
77 public Font nativeFont(Object f, String name, String family, String style, double size) {
78 return Font.nativeFont(f, name, family, style, size);
79 }
80
81 });
82 }
83
84 private static final String DEFAULT_FAMILY = "System";
85 private static final String DEFAULT_FULLNAME = "System Regular";
86
87 /**
88 * The default font for this platform. This is used whenever a font is not
89 * specifically set on a Text node or overridden by CSS.
90 */
91 private static float defaultSystemFontSize = -1;
92 private static float getDefaultSystemFontSize() {
93 if (defaultSystemFontSize == -1) {
94 defaultSystemFontSize =
95 Toolkit.getToolkit().getFontLoader().getSystemFontSize();
96 }
97 return defaultSystemFontSize;
98 }
99
100 private static Font DEFAULT;
101 /**
102 * Gets the default font which will be from the family "System",
103 * and typically the style "Regular", and be of a size consistent
322 this(null, size);
323 }
324
325
326 /**
327 * Constructs a font using the specified full face name and size
328 * @param name full name of the font.
329 * @param size the font size to use
330 */
331 public Font(@NamedArg("name") String name, @NamedArg("size") double size) {
332 this.name = name;
333 this.size = size;
334
335 if (name == null || "".equals(name)) this.name = DEFAULT_FULLNAME;
336 if (size < 0f) this.size = getDefaultSystemFontSize();
337 // if a search was done based on family + style information, then the
338 // native font has already been located and specified. Likewise if the
339 // Font was created based on an existing native font. If however a Font
340 // was created directly in FX, then we need to find the native font
341 // to use. This call will also set the family and style by invoking
342 // the setNativeFont callback method.
343 Toolkit.getToolkit().getFontLoader().loadFont(this);
344 }
345
346 /**
347 * Private constructor for internal implementation
348 *
349 * @param f native font
350 * @param family font family name
351 * @param name font full name
352 * @param style style string
353 * @param size font size
354 */
355 private Font(Object f, String family, String name, String style, double size) {
356 this.nativeFont = f;
357 this.family = family;
358 this.name = name;
359 this.style = style;
360 this.size = size;
361 }
362
508 }
509 return false;
510 }
511
512 /**
513 * Returns a hash code for this {@code Font} object.
514 * @return a hash code for this {@code Font} object.
515 */
516 @Override public int hashCode() {
517 if (hash == 0) {
518 long bits = 17L;
519 bits = 37L * bits + name.hashCode();
520 bits = 37L * bits + Double.doubleToLongBits(size);
521 hash = (int) (bits ^ (bits >> 32));
522 }
523 return hash;
524 }
525
526 private Object nativeFont;
527
528 private Object getNativeFont() { return nativeFont; }
529
530 private void setNativeFont(Object f, String nam, String fam, String styl) {
531 nativeFont = f;
532 name = nam;
533 family = fam;
534 style = styl;
535 }
536
537 private static Font nativeFont(Object f, String name, String family,
538 String style, double size) {
539 Font retFont = new Font( f, family, name, style, size);
540 return retFont;
541 }
542 }
|