📄 fontregistry.java
字号:
* Find the first valid fontData in the provided list. * If none are valid return the first one regardless. * If the list is empty return null. * @deprecated use filterData in order to preserve * multiple entry fonts on Motif */ public FontData[] bestDataArray(FontData[] fonts, Display display) { FontData bestData = bestData(fonts, display); if (bestData == null) { return null; } FontData[] datas = new FontData[1]; datas[0] = bestData; return datas; } /** * Removes from the list all fonts that do not exist in this system. * If none are valid, return the first irregardless. If the list is * empty return <code>null</code>. * * @param fonts the fonts to check * @param display the display to check against * @return the list of fonts that have been found on this system * @since 3.1 */ public FontData [] filterData(FontData [] fonts, Display display) { ArrayList good = new ArrayList(fonts.length); for (int i = 0; i < fonts.length; i++) { FontData fd = fonts[i]; if (fd == null) { continue; } FontData[] fixedFonts = display.getFontList(fd.getName(), false); if (isFixedFont(fixedFonts, fd)) { good.add(fd); } FontData[] scalableFonts = display.getFontList(fd.getName(), true); if (scalableFonts.length > 0) { good.add(fd); } } //None of the provided datas are valid. Return the //first one as it is at least the first choice. if (good.isEmpty() && fonts.length > 0) { good.add(fonts[0]); } else if (fonts.length == 0) { return null; } return (FontData[]) good.toArray(new FontData[good.size()]); } /** * Creates a new font with the given font datas or <code>null</code> * if there is no data. * @return FontRecord for the new Font or <code>null</code>. */ private FontRecord createFont(String symbolicName, FontData[] fonts) { Display display = Display.getCurrent(); if (display == null) { return null; } FontData[] validData = filterData(fonts, display); if (validData.length == 0) { //Nothing specified return null; } //Do not fire the update from creation as it is not a property change put(symbolicName, validData, false); Font newFont = new Font(display, validData); return new FontRecord(newFont, validData); } /** * Calculates the default font and returns the result */ Font calculateDefaultFont() { Display current = Display.getCurrent(); if (current == null) { Shell shell = new Shell(); Font font = new Font(null, shell.getFont().getFontData()); shell.dispose(); return font; } return new Font(current, current.getSystemFont().getFontData()); } /** * Returns the default font data. Creates it if necessary. * @return Font */ public Font defaultFont() { return defaultFontRecord().getBaseFont(); } /** * Returns the default font record. */ private FontRecord defaultFontRecord() { FontRecord record = (FontRecord) stringToFontRecord .get(JFaceResources.DEFAULT_FONT); if (record == null) { Font defaultFont = calculateDefaultFont(); record = createFont(JFaceResources.DEFAULT_FONT, defaultFont .getFontData()); stringToFontRecord.put(JFaceResources.DEFAULT_FONT, record); } return record; } /** * Returns the default font data. Creates it if necessary. */ private FontData[] defaultFontData() { return defaultFontRecord().baseData; } /** * Returns the font data associated with the given symbolic font name. * Returns the default font data if there is no special value associated * with that name. * * @param symbolicName symbolic font name * @return the font */ public FontData[] getFontData(String symbolicName) { Assert.isNotNull(symbolicName); Object result = stringToFontData.get(symbolicName); if (result == null) { return defaultFontData(); } return (FontData[]) result; } /** * Returns the font associated with the given symbolic font name. * Returns the default font if there is no special value associated * with that name. * * @param symbolicName symbolic font name * @return the font */ public Font get(String symbolicName) { return getFontRecord(symbolicName).getBaseFont(); } /** * Returns the bold font associated with the given symbolic font name. * Returns the bolded default font if there is no special value associated * with that name. * * @param symbolicName symbolic font name * @return the font * @since 3.0 */ public Font getBold(String symbolicName) { return getFontRecord(symbolicName).getBoldFont(); } /** * Returns the italic font associated with the given symbolic font name. * Returns the italic default font if there is no special value associated * with that name. * * @param symbolicName symbolic font name * @return the font * @since 3.0 */ public Font getItalic(String symbolicName) { return getFontRecord(symbolicName).getItalicFont(); } /** * Return the font record for the key. * @param symbolicName The key for the record. * @return FontRecird */ private FontRecord getFontRecord(String symbolicName) { Assert.isNotNull(symbolicName); Object result = stringToFontRecord.get(symbolicName); if (result != null) { return (FontRecord) result; } result = stringToFontData.get(symbolicName); FontRecord fontRecord; if (result == null) { fontRecord = defaultFontRecord(); } else { fontRecord = createFont(symbolicName, (FontData[]) result); } if (fontRecord == null) { fontRecord = defaultFontRecord(); } stringToFontRecord.put(symbolicName, fontRecord); return fontRecord; } /* (non-Javadoc) * @see org.eclipse.jface.resource.ResourceRegistry#getKeySet() */ public Set getKeySet() { return Collections.unmodifiableSet(stringToFontData.keySet()); } /* (non-Javadoc) * @see org.eclipse.jface.resource.ResourceRegistry#hasValueFor(java.lang.String) */ public boolean hasValueFor(String fontKey) { return stringToFontData.containsKey(fontKey); } /* (non-Javadoc) * @see org.eclipse.jface.resource.ResourceRegistry#clearCaches() */ protected void clearCaches() { Iterator iterator = stringToFontRecord.values().iterator(); while (iterator.hasNext()) { Object next = iterator.next(); ((FontRecord) next).dispose(); } disposeFonts(staleFonts.iterator()); stringToFontRecord.clear(); staleFonts.clear(); } /** * Dispose of all of the fonts in this iterator. * @param Iterator over Collection of Font */ private void disposeFonts(Iterator iterator) { while (iterator.hasNext()) { Object next = iterator.next(); ((Font) next).dispose(); } } /** * Hook a dispose listener on the SWT display. */ private void hookDisplayDispose(Display display) { display.disposeExec(displayRunnable); } /** * Checks whether the given font is in the list of fixed fonts. */ private boolean isFixedFont(FontData[] fixedFonts, FontData fd) { // Can't use FontData.equals() since some values aren't // set if a fontdata isn't used. int height = fd.getHeight(); String name = fd.getName(); for (int i = 0; i < fixedFonts.length; i++) { FontData fixed = fixedFonts[i]; if (fixed.getHeight() == height && fixed.getName().equals(name)) { return true; } } return false; } /** * Converts a String into a FontData object. */ private FontData makeFontData(String value) throws MissingResourceException { try { return StringConverter.asFontData(value.trim()); } catch (DataFormatException e) { throw new MissingResourceException( "Wrong font data format. Value is: \"" + value + "\"", getClass().getName(), value); //$NON-NLS-2$//$NON-NLS-1$ } } /** * Adds (or replaces) a font to this font registry under the given * symbolic name. * <p> * A property change event is reported whenever the mapping from * a symbolic name to a font changes. The source of the event is * this registry; the property name is the symbolic font name. * </p> * * @param symbolicName the symbolic font name * @param fontData an Array of FontData */ public void put(String symbolicName, FontData[] fontData) { put(symbolicName, fontData, true); } /** * Adds (or replaces) a font to this font registry under the given * symbolic name. * <p> * A property change event is reported whenever the mapping from * a symbolic name to a font changes. The source of the event is * this registry; the property name is the symbolic font name. * </p> * * @param symbolicName the symbolic font name * @param fontData an Array of FontData * @param update - fire a font mapping changed if true. False * if this method is called from the get method as no setting * has changed. */ private void put(String symbolicName, FontData[] fontData, boolean update) { Assert.isNotNull(symbolicName); Assert.isNotNull(fontData); FontData[] existing = (FontData[]) stringToFontData.get(symbolicName); if (Arrays.equals(existing, fontData)) { return; } FontRecord oldFont = (FontRecord) stringToFontRecord .remove(symbolicName); stringToFontData.put(symbolicName, fontData); if (update) { fireMappingChanged(symbolicName, existing, fontData); } if (oldFont != null) { oldFont.addAllocatedFontsToStale(defaultFontRecord().getBaseFont()); } } /** * Reads the resource bundle. This puts FontData[] objects * in the mapping table. These will lazily be turned into * real Font objects when requested. */ private void readResourceBundle(ResourceBundle bundle, String bundleName) throws MissingResourceException { Enumeration keys = bundle.getKeys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); int pos = key.lastIndexOf('.'); if (pos == -1) { stringToFontData.put(key, new FontData[] { makeFontData(bundle .getString(key)) }); } else { String name = key.substring(0, pos); int i = 0; try { i = Integer.parseInt(key.substring(pos + 1)); } catch (NumberFormatException e) { //Panic the file can not be parsed. throw new MissingResourceException( "Wrong key format ", bundleName, key); //$NON-NLS-1$ } FontData[] elements = (FontData[]) stringToFontData.get(name); if (elements == null) { elements = new FontData[8]; stringToFontData.put(name, elements); } if (i > elements.length) { FontData[] na = new FontData[i + 8]; System.arraycopy(elements, 0, na, 0, elements.length); elements = na; stringToFontData.put(name, elements); } elements[i] = makeFontData(bundle.getString(key)); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -