37 import java.util.Map;
38 import java.util.NoSuchElementException;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
41
42 import javax.swing.plaf.FontUIResource;
43 import sun.awt.motif.MFontConfiguration;
44 import sun.font.CompositeFont;
45 import sun.font.FontManager;
46 import sun.font.SunFontManager;
47 import sun.font.FontConfigManager;
48 import sun.font.FcFontConfiguration;
49 import sun.font.FontAccess;
50 import sun.font.FontUtilities;
51 import sun.font.NativeFont;
52 import sun.util.logging.PlatformLogger;
53
54 /**
55 * The X11 implementation of {@link FontManager}.
56 */
57 public final class X11FontManager extends SunFontManager {
58
59 // constants identifying XLFD and font ID fields
60 private static final int FOUNDRY_FIELD = 1;
61 private static final int FAMILY_NAME_FIELD = 2;
62 private static final int WEIGHT_NAME_FIELD = 3;
63 private static final int SLANT_FIELD = 4;
64 private static final int SETWIDTH_NAME_FIELD = 5;
65 private static final int ADD_STYLE_NAME_FIELD = 6;
66 private static final int PIXEL_SIZE_FIELD = 7;
67 private static final int POINT_SIZE_FIELD = 8;
68 private static final int RESOLUTION_X_FIELD = 9;
69 private static final int RESOLUTION_Y_FIELD = 10;
70 private static final int SPACING_FIELD = 11;
71 private static final int AVERAGE_WIDTH_FIELD = 12;
72 private static final int CHARSET_REGISTRY_FIELD = 13;
73 private static final int CHARSET_ENCODING_FIELD = 14;
74
75 /*
76 * fontNameMap is a map from a fontID (which is a substring of an XLFD like
77 * "-monotype-arial-bold-r-normal-iso8859-7")
137 */
138 HashMap<String, String> oblmap = null;
139
140
141 /*
142 * Used to eliminate redundant work. When a font directory is
143 * registered it added to this list. Subsequent registrations for the
144 * same directory can then be skipped by checking this Map.
145 * Access to this map is not synchronised here since creation
146 * of the singleton GE instance is already synchronised and that is
147 * the only code path that accesses this map.
148 */
149 private static HashMap<String, Object> registeredDirs = new HashMap<>();
150
151 /* Array of directories to be added to the X11 font path.
152 * Used by static method called from Toolkits which use X11 fonts.
153 * Specifically this means MToolkit
154 */
155 private static String[] fontdirs = null;
156
157 private FontConfigManager fcManager = null;
158
159 public static X11FontManager getInstance() {
160 return (X11FontManager) SunFontManager.getInstance();
161 }
162
163 /**
164 * Takes family name property in the following format:
165 * "-linotype-helvetica-medium-r-normal-sans-*-%d-*-*-p-*-iso8859-1"
166 * and returns the name of the corresponding physical font.
167 * This code is used to resolve font configuration fonts, and expects
168 * only to get called for these fonts.
169 */
170 @Override
171 public String getFileNameFromPlatformName(String platName) {
172
173 /* If the FontConfig file doesn't use xlfds, or its
174 * FcFontConfiguration, this may be already a file name.
175 */
176 if (platName.startsWith("/")) {
177 return platName;
178 }
767 (!mFontConfig.foundOsSpecificFile() ||
768 !mFontConfig.fontFilesArePresent()) ||
769 (FontUtilities.isSolaris && !mFontConfig.fontFilesArePresent()))) {
770 FcFontConfiguration fcFontConfig =
771 new FcFontConfiguration(this);
772 if (fcFontConfig.init()) {
773 return fcFontConfig;
774 }
775 }
776 mFontConfig.init();
777 return mFontConfig;
778 }
779 public FontConfiguration
780 createFontConfiguration(boolean preferLocaleFonts,
781 boolean preferPropFonts) {
782
783 return new MFontConfiguration(this,
784 preferLocaleFonts, preferPropFonts);
785 }
786
787 public synchronized native String getFontPathNative(boolean noType1Fonts);
788
789 protected synchronized String getFontPath(boolean noType1Fonts) {
790 isHeadless(); // make sure GE is inited, as its the X11 lock.
791 return getFontPathNative(noType1Fonts);
792 }
793
794 @Override
795 protected String[] getDefaultPlatformFont() {
796 final String[] info = new String[2];
797 getFontConfigManager().initFontConfigFonts(false);
798 FontConfigManager.FcCompFont[] fontConfigFonts =
799 getFontConfigManager().getFontConfigFonts();
800 for (int i=0; i<fontConfigFonts.length; i++) {
801 if ("sans".equals(fontConfigFonts[i].fcFamily) &&
802 0 == fontConfigFonts[i].style) {
803 info[0] = fontConfigFonts[i].firstFont.familyName;
804 info[1] = fontConfigFonts[i].firstFont.fontFile;
805 break;
806 }
807 }
808 /* Absolute last ditch attempt in the face of fontconfig problems.
809 * If we didn't match, pick the first, or just make something
810 * up so we don't NPE.
811 */
812 if (info[0] == null) {
813 if (fontConfigFonts.length > 0 &&
814 fontConfigFonts[0].firstFont.fontFile != null) {
815 info[0] = fontConfigFonts[0].firstFont.familyName;
816 info[1] = fontConfigFonts[0].firstFont.fontFile;
817 } else {
818 info[0] = "Dialog";
819 info[1] = "/dialog.ttf";
820 }
821 }
822 return info;
823 }
824
825 public synchronized FontConfigManager getFontConfigManager() {
826
827 if (fcManager == null) {
828 fcManager = new FontConfigManager();
829 }
830
831 return fcManager;
832 }
833
834 @Override
835 protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
836
837 CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
838
839 if (font2D == null) { // Not expected, just a precaution.
840 return new FontUIResource(family, style, size);
841 }
842
843 /* The name of the font will be that of the physical font in slot,
844 * but by setting the handle to that of the CompositeFont it
845 * renders as that CompositeFont.
846 * It also needs to be marked as a created font which is the
847 * current mechanism to signal that deriveFont etc must copy
848 * the handle from the original font.
849 */
850 FontUIResource fuir =
851 new FontUIResource(font2D.getFamilyName(null), style, size);
|
37 import java.util.Map;
38 import java.util.NoSuchElementException;
39 import java.util.StringTokenizer;
40 import java.util.Vector;
41
42 import javax.swing.plaf.FontUIResource;
43 import sun.awt.motif.MFontConfiguration;
44 import sun.font.CompositeFont;
45 import sun.font.FontManager;
46 import sun.font.SunFontManager;
47 import sun.font.FontConfigManager;
48 import sun.font.FcFontConfiguration;
49 import sun.font.FontAccess;
50 import sun.font.FontUtilities;
51 import sun.font.NativeFont;
52 import sun.util.logging.PlatformLogger;
53
54 /**
55 * The X11 implementation of {@link FontManager}.
56 */
57 public final class X11FontManager extends FcFontManager {
58
59 // constants identifying XLFD and font ID fields
60 private static final int FOUNDRY_FIELD = 1;
61 private static final int FAMILY_NAME_FIELD = 2;
62 private static final int WEIGHT_NAME_FIELD = 3;
63 private static final int SLANT_FIELD = 4;
64 private static final int SETWIDTH_NAME_FIELD = 5;
65 private static final int ADD_STYLE_NAME_FIELD = 6;
66 private static final int PIXEL_SIZE_FIELD = 7;
67 private static final int POINT_SIZE_FIELD = 8;
68 private static final int RESOLUTION_X_FIELD = 9;
69 private static final int RESOLUTION_Y_FIELD = 10;
70 private static final int SPACING_FIELD = 11;
71 private static final int AVERAGE_WIDTH_FIELD = 12;
72 private static final int CHARSET_REGISTRY_FIELD = 13;
73 private static final int CHARSET_ENCODING_FIELD = 14;
74
75 /*
76 * fontNameMap is a map from a fontID (which is a substring of an XLFD like
77 * "-monotype-arial-bold-r-normal-iso8859-7")
137 */
138 HashMap<String, String> oblmap = null;
139
140
141 /*
142 * Used to eliminate redundant work. When a font directory is
143 * registered it added to this list. Subsequent registrations for the
144 * same directory can then be skipped by checking this Map.
145 * Access to this map is not synchronised here since creation
146 * of the singleton GE instance is already synchronised and that is
147 * the only code path that accesses this map.
148 */
149 private static HashMap<String, Object> registeredDirs = new HashMap<>();
150
151 /* Array of directories to be added to the X11 font path.
152 * Used by static method called from Toolkits which use X11 fonts.
153 * Specifically this means MToolkit
154 */
155 private static String[] fontdirs = null;
156
157 public static X11FontManager getInstance() {
158 return (X11FontManager) SunFontManager.getInstance();
159 }
160
161 /**
162 * Takes family name property in the following format:
163 * "-linotype-helvetica-medium-r-normal-sans-*-%d-*-*-p-*-iso8859-1"
164 * and returns the name of the corresponding physical font.
165 * This code is used to resolve font configuration fonts, and expects
166 * only to get called for these fonts.
167 */
168 @Override
169 public String getFileNameFromPlatformName(String platName) {
170
171 /* If the FontConfig file doesn't use xlfds, or its
172 * FcFontConfiguration, this may be already a file name.
173 */
174 if (platName.startsWith("/")) {
175 return platName;
176 }
765 (!mFontConfig.foundOsSpecificFile() ||
766 !mFontConfig.fontFilesArePresent()) ||
767 (FontUtilities.isSolaris && !mFontConfig.fontFilesArePresent()))) {
768 FcFontConfiguration fcFontConfig =
769 new FcFontConfiguration(this);
770 if (fcFontConfig.init()) {
771 return fcFontConfig;
772 }
773 }
774 mFontConfig.init();
775 return mFontConfig;
776 }
777 public FontConfiguration
778 createFontConfiguration(boolean preferLocaleFonts,
779 boolean preferPropFonts) {
780
781 return new MFontConfiguration(this,
782 preferLocaleFonts, preferPropFonts);
783 }
784
785 protected synchronized String getFontPath(boolean noType1Fonts) {
786 isHeadless(); // make sure GE is inited, as its the X11 lock.
787 return getFontPathNative(noType1Fonts, true);
788 }
789
790 @Override
791 protected FontUIResource getFontConfigFUIR(String family, int style, int size) {
792
793 CompositeFont font2D = getFontConfigManager().getFontConfigFont(family, style);
794
795 if (font2D == null) { // Not expected, just a precaution.
796 return new FontUIResource(family, style, size);
797 }
798
799 /* The name of the font will be that of the physical font in slot,
800 * but by setting the handle to that of the CompositeFont it
801 * renders as that CompositeFont.
802 * It also needs to be marked as a created font which is the
803 * current mechanism to signal that deriveFont etc must copy
804 * the handle from the original font.
805 */
806 FontUIResource fuir =
807 new FontUIResource(font2D.getFamilyName(null), style, size);
|