下面列出了java.awt.Font#getFamily ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Style[] createHeaderStyles(Font font) {
String family = font.getFamily();
int size = font.getSize();
int[] sizes = {size * 2, size * 3 / 2, size * 5 / 4, size, size * 7 / 8};
int count = sizes.length;
Style[] styles = new Style[count];
for (int i = 0; i < count; i++) {
Style h = addStyle("h" + (i + 1), null);
h.addAttribute(StyleConstants.FontFamily, family);
h.addAttribute(StyleConstants.FontSize, Integer.valueOf(sizes[i]));
h.addAttribute(StyleConstants.Bold, Boolean.TRUE);
h.addAttribute(StyleConstants.SpaceBelow, Float.valueOf(sizes[i] / 2.0f));
styles[i] = h;
}
return styles;
}
public static void main(String[] args) throws Exception {
String os = System.getProperty("os.name");
if (!(os.startsWith("Mac"))) {
return;
}
File fontFile = new File(file);
if (!fontFile.exists()) {
return;
}
Font[] fonts = Font.createFonts(new File(file));
System.out.println("createFont from file returned " + fonts);
if (fonts == null || fonts.length == 0) {
throw new RuntimeException("No fonts");
}
for (Font f : fonts) {
System.out.println(f);
if (!f.getFamily().equals("Menlo"))
throw new RuntimeException("Expected Menlo, got " + f.getFamily());
}
}
private JPanel getLogPanel() {
JPanel theLogPanel = new JPanel();
theLogPanel.setLayout(new BorderLayout());
myLogPane = new JTextPane();
myLogPane.setPreferredSize(new Dimension(400,300));
editorKit = new HTMLEditorKit();
gameLog = (HTMLDocument) editorKit.createDefaultDocument();;
myLogPane.setEditorKit(editorKit);
myLogPane.setDocument(gameLog);
myLogScroll = new JScrollPane(myLogPane);
myLogScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// theScrollPane.setPreferredSize(new Dimension(400,400));
theLogPanel.add(myLogScroll,BorderLayout.CENTER);
Font font = new Font("Times New Roman", Font.PLAIN, 14);
String bodyRule = "body { font-family: " + font.getFamily() + "; " +
"font-size: " + font.getSize() + "pt; }";
((HTMLDocument)myLogPane.getDocument()).getStyleSheet().addRule(bodyRule);// myLogPane.revalidate();
return theLogPanel;
}
/** Creates new form MissingClientPanel */
public MissingClientPanel() {
initComponents();
if(Utilities.isWindows()) {
tipLabel.setText(org.openide.util.NbBundle.getMessage(MissingClientPanel.class, "MissingSvnClientPanel.jLabel1.windows.text")); // NOI18N
} else {
tipLabel.setText(org.openide.util.NbBundle.getMessage(MissingClientPanel.class, "MissingSvnClientPanel.jLabel1.unix.text")); // NOI18N
}
String text = org.openide.util.NbBundle.getMessage(MissingClientPanel.class, "MissingClientPanel.textPane.text"); // NOI18N
textPane.setText(text);
Document doc = textPane.getDocument();
if (doc instanceof HTMLDocument) { // Issue 185505
HTMLDocument htmlDoc = (HTMLDocument)doc;
Font font = UIManager.getFont("Label.font"); // NOI18N
String bodyRule = "body { font-family: " + font.getFamily() + "; " // NOI18N
+ "color: " + SvnUtils.getColorString(textPane.getForeground()) + "; " //NOI18N
+ "font-size: " + font.getSize() + "pt; }"; // NOI18N
htmlDoc.getStyleSheet().addRule(bodyRule);
}
textPane.setOpaque(false);
textPane.setBackground(new Color(0,0,0,0)); // windows and nimbus workaround see issue 145826
}
public JTextPaneTableCellRenderer() {
textPane.setContentType("text/html");
textPane.setEditable(false);
textPane.setOpaque(true);
textPane.setBorder(null);
textPane.setForeground(UIManager.getColor("Table.selectionForeground"));
textPane.setBackground(UIManager.getColor("Table.selectionBackground"));
Font font = UIManager.getFont("Label.font");
String bodyRule =
"body { font-family: " + font.getFamily() + "; " + "font-size: "
+ font.getSize() + "pt; "
+ (font.isBold() ? "font-weight: bold;" : "") + "}";
((HTMLDocument)textPane.getDocument()).getStyleSheet().addRule(bodyRule);
textPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED))
MainFrame.getInstance().showHelpFrame(e.getURL().toString(), "CREOLE Plugin Manager");
}
});
}
public static String getValidFontFamilyName(Object family) {
Font f = systemFonts.get(family instanceof String ? ((String) family).toLowerCase() : family);
if (f != null) {
return f.getFamily();
}
return checkFontFamilyName(family) ? (String) family : DEFAULT_FONT_NAME;
}
public static void main(String [] args) {
System.setProperty("sun.java2d.debugfonts", "true");
Font font = new Font("dialog", Font.PLAIN, 14);
System.out.println(font);
String s1 = font.getFamily();
String s2 = font.getFontName();
}
public static void main(String [] args) {
System.setProperty("sun.java2d.debugfonts", "true");
Font font = new Font("dialog", Font.PLAIN, 14);
System.out.println(font);
String s1 = font.getFamily();
String s2 = font.getFontName();
}
public static void main(String [] args) {
System.setProperty("sun.java2d.debugfonts", "true");
Font font = new Font("dialog", Font.PLAIN, 14);
System.out.println(font);
String s1 = font.getFamily();
String s2 = font.getFontName();
}
public static void showHtmlDialog(Component parent, String title, String body) {
Font font = parent.getFont();
JHtmlPane pane = new JHtmlPane(parent, "<html><body style=\"font-family:" +
font.getFamily() + ";text-align:center;\">" + body + "</body></html>");
pane.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
showComponentDialog(parent, title, pane);
}
/**
* Build a new font object from the GUI and assign it
*/
protected void buildAndSetFont() {
if (typeList.getSelectedValue() == null) return;
// We need @ignoreSelection since we cannot set the current
// font without signalling the font changes callbacks
if (ignoreSelection) return;
String name = (String) fontList.getSelectedValue();
String sizeString = (String) sizeList.getSelectedValue();
boolean isBold = bold.isSelected();
boolean isItalic = italic.isSelected();
if (name == null || sizeString == null) {
Font fallbackFont = controller.getTypeFont(selectedType);
if (name == null && sizeString == null && !isBold)
isBold = fallbackFont.isBold();
if (name == null && sizeString == null && !isItalic)
isItalic = fallbackFont.isItalic();
if (name == null) {
name = fallbackFont.getFamily();
fontList.setSelectedValue(name, true);
}
if (sizeString == null) {
sizeString = Integer.toString(fallbackFont.getSize());
sizeList.setSelectedValue(sizeString, true);
}
}
int style = Font.PLAIN;
if (isBold) style = style | Font.BOLD;
if (isItalic) style = style | Font.ITALIC;
controller.setTypeFont(selectedType, new Font(name, style, Integer
.parseInt(sizeString)));
}
public static void main(String[] args) throws Exception {
String osName = System.getProperty("os.name");
System.out.println("OS is " + osName);
osName = osName.toLowerCase();
if (!osName.startsWith("windows")) {
return;
}
Font msMincho = new Font("MS Mincho", Font.PLAIN, 16);
String family = msMincho.getFamily(java.util.Locale.ENGLISH);
if (!family.equalsIgnoreCase("MS Mincho")) {
System.out.println("Japanese fonts not installed");
return;
}
String s = "|";
BufferedImage bi1 = getFontImage(new Font("MS Mincho", Font.PLAIN, 16), s);
int h1 = bi1.getHeight();
int w1 = bi1.getWidth();
BufferedImage bi2 = getFontImage(new Font("MS Mincho", Font.ITALIC, 16), s);
int h2 = bi2.getHeight();
int w2 = bi2.getWidth();
if ((h1 == h2) && (w1 == w2)) {
int cnt = 0;
for(int yy = 0; yy < h1; yy++) {
for(int xx = 0; xx < w1; xx++) {
if (bi1.getRGB(xx, yy) != bi2.getRGB(xx, yy)) {
cnt++;
}
}
}
if (cnt == 0) {
throw new Exception("Test failed");
}
}
}
/**
* Creates new form SwitchToCliPanel
*/
public NotifyHtmlPanel () {
initComponents();
Document doc = msgPanel.getDocument();
if (doc instanceof HTMLDocument) { // Issue 185505
HTMLDocument htmlDoc = (HTMLDocument)doc;
Font font = UIManager.getFont("Label.font"); // NOI18N
String bodyRule = "body { font-family: " + font.getFamily() + "; " // NOI18N
+ "color: " + SvnUtils.getColorString(msgPanel.getForeground()) + "; " //NOI18N
+ "font-size: " + font.getSize() + "pt; }"; // NOI18N
htmlDoc.getStyleSheet().addRule(bodyRule);
}
msgPanel.setOpaque(false);
msgPanel.setBackground(new Color(0,0,0,0)); // windows and nimbus workaround see issue 145826
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
// recalculate font as well.
font = new Font(font.getFamily(),
font.getStyle(),
this.fontSize);
markDirty();
}
public InjectedParameterPlaceholderLabel(ConnectionParameterModel param) {
super(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel icon = new JLabel("", INJECTED_ICON, JLabel.LEFT);
add(icon, gbc);
Font font = icon.getFont();
String bodyRule = "body { font-family: " + font.getFamily() + "; " +
"font-size: " + font.getSize() + "pt; }";
((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule);
editorPane.setOpaque(false);
editorPane.setBorder(null);
editorPane.setEditable(false);
editorPane.addHyperlinkListener(event -> {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED && event.getURL() != null) {
RMUrlHandler.openInBrowser(event.getURL());
}
});
gbc.insets.left = icon.getIconTextGap();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(editorPane, gbc);
valueProviderChanged = (i, o, n) -> updateText(param);
valueProviderParameterChanged = c -> {
while (c.next()) {
for (ValueProviderParameterModel valueProviderParameterModel : c.getAddedSubList()) {
valueProviderParameterModel.valueProperty().removeListener(valueProviderChanged);
valueProviderParameterModel.valueProperty().addListener(valueProviderChanged);
}
}
updateText(param);
};
param.injectorNameProperty().addListener((i, o, n) -> registerListener(param));
registerListener(param);
}
private void buildHtmlContent() {
String categoryName = "general";
String result = "<html><body><table cellspacing='0' cellpadding='0'>";
Boolean flipFlop = false;
List<TagInfo.TagInfoItem> items = tagInfo.getInfos().get(categoryName);
result += "<tr bgcolor='#FDFDFD'>";
result += "<td width='50%' style='text-align:center;'>";
result += mainPanel.translate("tagInfo.header.name");
result += "</td>";
result += "<td width='50%' style='text-align:center;'>";
result += mainPanel.translate("tagInfo.header.value");
result += "</td>";
result += "</tr>";
for (TagInfo.TagInfoItem item : items) {
Boolean convertToCharacterList;
flipFlop = !flipFlop;
result += "<tr bgcolor='" + (flipFlop ? "#FDFDFD" : "#F4F4F4") + "'>";
String name = item.getName();
String key = "tagInfo." + name;
convertToCharacterList = name.equals("dependentCharacters") || name.equals("neededCharacters");
try {
name = mainPanel.translate(key);
} catch (MissingResourceException mes) {
if (Configuration._debugMode.get()) {
Logger.getLogger(TagInfoPanel.class.getName()).log(Level.WARNING, "Resource not found: {0}", key);
}
}
result += "<td>" + name + "</td>";
Object value = item.getValue();
if (value instanceof Boolean) {
boolean boolValue = (boolean) value;
value = boolValue ? AppStrings.translate("yes") : AppStrings.translate("no");
} else if (convertToCharacterList) {
String strValue = (String) value;
String[] strIds = strValue.split(", ");
List<Integer> sortedIds = new ArrayList<>();
strValue = "";
for (String strId : strIds) {
sortedIds.add(Integer.parseInt(strId));
}
Collections.sort(sortedIds);
for (int id : sortedIds) {
strValue += "<a href='jump://" + id + "'>" + id + "</a>, ";
}
value = strValue.substring(0, strValue.length() - 2);
}
result += "<td>" + value + "</td>";
result += "</tr>";
}
result += "</table></body></html>";
editorPane.setText(result);
Font font = UIManager.getFont("Label.font");
String bodyRule = "body { font-family: " + font.getFamily() + ";"
+ " font-size: " + font.getSize() + "pt;"
+ "}"
+ " table {"
+ " width:100%;"
+ " color:#053E6A;"
+ " padding:1px;"
+ "}"
+ "td { border: 1px solid #e4e4e4; }"
+ "html { border: 1px solid #789AC4; }";
((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule);
editorPane.setOpaque(false);
editorPane.setBorder(null);
editorPane.setEditable(false);
}
@Override
public String toStandardString(Font f) {
return f.getFamily() + " " + FontUtil.toStyleStandardString(f.getStyle()) + " " + f.getSize();
}
public static PDType1Font getPDFont(Font javaFont) {
switch (javaFont.getFamily()) {
case Font.SERIF:
if (javaFont.isPlain()) {
return PDType1Font.TIMES_ROMAN;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.TIMES_BOLD_ITALIC;
} else {
return PDType1Font.TIMES_BOLD;
}
} else {
return PDType1Font.TIMES_ITALIC;
}
case Font.SANS_SERIF:
if (javaFont.isPlain()) {
return PDType1Font.HELVETICA;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.HELVETICA_BOLD_OBLIQUE;
} else {
return PDType1Font.HELVETICA_BOLD;
}
} else {
return PDType1Font.HELVETICA_OBLIQUE;
}
case Font.MONOSPACED:
if (javaFont.isPlain()) {
return PDType1Font.COURIER;
} else if (javaFont.isBold()) {
if (javaFont.isItalic()) {
return PDType1Font.COURIER_BOLD_OBLIQUE;
} else {
return PDType1Font.COURIER_BOLD;
}
} else {
return PDType1Font.COURIER_OBLIQUE;
}
case Font.DIALOG:
case Font.DIALOG_INPUT:
return PDType1Font.SYMBOL;
default:
throw new DSSException("The font is not supported! Please use DSSFileFont implementation for custom fonts.");
}
}
public boolean registerFont(Font font) {
/* This method should not be called with "null".
* It is the caller's responsibility to ensure that.
*/
if (font == null) {
return false;
}
/* Initialise these objects only once we start to use this API */
synchronized (regFamilyKey) {
if (createdByFamilyName == null) {
createdByFamilyName = new Hashtable<String,FontFamily>();
createdByFullName = new Hashtable<String,Font2D>();
}
}
if (! FontAccess.getFontAccess().isCreatedFont(font)) {
return false;
}
/* We want to ensure that this font cannot override existing
* installed fonts. Check these conditions :
* - family name is not that of an installed font
* - full name is not that of an installed font
* - family name is not the same as the full name of an installed font
* - full name is not the same as the family name of an installed font
* The last two of these may initially look odd but the reason is
* that (unfortunately) Font constructors do not distinuguish these.
* An extreme example of such a problem would be a font which has
* family name "Dialog.Plain" and full name of "Dialog".
* The one arguably overly stringent restriction here is that if an
* application wants to supply a new member of an existing family
* It will get rejected. But since the JRE can perform synthetic
* styling in many cases its not necessary.
* We don't apply the same logic to registered fonts. If apps want
* to do this lets assume they have a reason. It won't cause problems
* except for themselves.
*/
HashSet<String> names = getInstalledNames();
Locale l = getSystemStartupLocale();
String familyName = font.getFamily(l).toLowerCase();
String fullName = font.getFontName(l).toLowerCase();
if (names.contains(familyName) || names.contains(fullName)) {
return false;
}
/* Checks passed, now register the font */
Hashtable<String,FontFamily> familyTable;
Hashtable<String,Font2D> fullNameTable;
if (!maybeMultiAppContext()) {
familyTable = createdByFamilyName;
fullNameTable = createdByFullName;
fontsAreRegistered = true;
} else {
AppContext appContext = AppContext.getAppContext();
familyTable =
(Hashtable<String,FontFamily>)appContext.get(regFamilyKey);
fullNameTable =
(Hashtable<String,Font2D>)appContext.get(regFullNameKey);
if (familyTable == null) {
familyTable = new Hashtable<String,FontFamily>();
fullNameTable = new Hashtable<String,Font2D>();
appContext.put(regFamilyKey, familyTable);
appContext.put(regFullNameKey, fullNameTable);
}
fontsAreRegisteredPerAppContext = true;
}
/* Create the FontFamily and add font to the tables */
Font2D font2D = FontUtilities.getFont2D(font);
int style = font2D.getStyle();
FontFamily family = familyTable.get(familyName);
if (family == null) {
family = new FontFamily(font.getFamily(l));
familyTable.put(familyName, family);
}
/* Remove name cache entries if not using app contexts.
* To accommodate a case where code may have registered first a plain
* family member and then used it and is now registering a bold family
* member, we need to remove all members of the family, so that the
* new style can get picked up rather than continuing to synthesise.
*/
if (fontsAreRegistered) {
removeFromCache(family.getFont(Font.PLAIN));
removeFromCache(family.getFont(Font.BOLD));
removeFromCache(family.getFont(Font.ITALIC));
removeFromCache(family.getFont(Font.BOLD|Font.ITALIC));
removeFromCache(fullNameTable.get(fullName));
}
family.setFont(font2D, style);
fullNameTable.put(fullName, font2D);
return true;
}
public boolean registerFont(Font font) {
/* This method should not be called with "null".
* It is the caller's responsibility to ensure that.
*/
if (font == null) {
return false;
}
/* Initialise these objects only once we start to use this API */
synchronized (regFamilyKey) {
if (createdByFamilyName == null) {
createdByFamilyName = new Hashtable<String,FontFamily>();
createdByFullName = new Hashtable<String,Font2D>();
}
}
if (! FontAccess.getFontAccess().isCreatedFont(font)) {
return false;
}
/* We want to ensure that this font cannot override existing
* installed fonts. Check these conditions :
* - family name is not that of an installed font
* - full name is not that of an installed font
* - family name is not the same as the full name of an installed font
* - full name is not the same as the family name of an installed font
* The last two of these may initially look odd but the reason is
* that (unfortunately) Font constructors do not distinuguish these.
* An extreme example of such a problem would be a font which has
* family name "Dialog.Plain" and full name of "Dialog".
* The one arguably overly stringent restriction here is that if an
* application wants to supply a new member of an existing family
* It will get rejected. But since the JRE can perform synthetic
* styling in many cases its not necessary.
* We don't apply the same logic to registered fonts. If apps want
* to do this lets assume they have a reason. It won't cause problems
* except for themselves.
*/
HashSet<String> names = getInstalledNames();
Locale l = getSystemStartupLocale();
String familyName = font.getFamily(l).toLowerCase();
String fullName = font.getFontName(l).toLowerCase();
if (names.contains(familyName) || names.contains(fullName)) {
return false;
}
/* Checks passed, now register the font */
Hashtable<String,FontFamily> familyTable;
Hashtable<String,Font2D> fullNameTable;
if (!maybeMultiAppContext()) {
familyTable = createdByFamilyName;
fullNameTable = createdByFullName;
fontsAreRegistered = true;
} else {
AppContext appContext = AppContext.getAppContext();
familyTable =
(Hashtable<String,FontFamily>)appContext.get(regFamilyKey);
fullNameTable =
(Hashtable<String,Font2D>)appContext.get(regFullNameKey);
if (familyTable == null) {
familyTable = new Hashtable<String,FontFamily>();
fullNameTable = new Hashtable<String,Font2D>();
appContext.put(regFamilyKey, familyTable);
appContext.put(regFullNameKey, fullNameTable);
}
fontsAreRegisteredPerAppContext = true;
}
/* Create the FontFamily and add font to the tables */
Font2D font2D = FontUtilities.getFont2D(font);
int style = font2D.getStyle();
FontFamily family = familyTable.get(familyName);
if (family == null) {
family = new FontFamily(font.getFamily(l));
familyTable.put(familyName, family);
}
/* Remove name cache entries if not using app contexts.
* To accommodate a case where code may have registered first a plain
* family member and then used it and is now registering a bold family
* member, we need to remove all members of the family, so that the
* new style can get picked up rather than continuing to synthesise.
*/
if (fontsAreRegistered) {
removeFromCache(family.getFont(Font.PLAIN));
removeFromCache(family.getFont(Font.BOLD));
removeFromCache(family.getFont(Font.ITALIC));
removeFromCache(family.getFont(Font.BOLD|Font.ITALIC));
removeFromCache(fullNameTable.get(fullName));
}
family.setFont(font2D, style);
fullNameTable.put(fullName, font2D);
return true;
}