/** * Computes and returns the horizontal dialog base units. Honors the font, * font size and resolution. * <p> * * Implementation Note: 14dluY map to 22 pixel for 8pt Tahoma on 96 dpi. I * could not yet manage to compute the Microsoft compliant font height. * Therefore this method adds a correction value that seems to work well * with the vast majority of desktops. * <p> * * TODO: revise the computation of vertical base untis, as soon as there are * more information about the original computation in Microsoft * environments. * * @param metrics * the FontMetrics used to measure the dialog font * @return the horizontal and vertical dialog base units */ private DialogBaseUnits computeDialogBaseUnits(FontMetrics metrics) { double averageCharWidth = computeAverageCharWidth(metrics, averageCharWidthTestString); int ascent = metrics.getAscent(); double height = ascent > 14 ? ascent : ascent + (15 - ascent) / 3; DialogBaseUnits dialogBaseUnits = new DialogBaseUnits(averageCharWidth, height); logInfo("Computed dialog base units " + dialogBaseUnits + " for: " + metrics.getFont()); return dialogBaseUnits; }
/** * Looks up and returns the dialog base units for the given component. In * case the component is <code>null</code> the global dialog base units * are answered. * <p> * * Before we compute the dialog base units we check whether they have been * computed and cached before - for the same component * <code>FontMetrics</code>. * * @param c * the component that provides the graphics object * @return the DialogBaseUnits object for the given component */ private DialogBaseUnits getDialogBaseUnits(Component c) { if (c == null) { // || (font = c.getFont()) == null) { logInfo("Missing font metrics: " + c); return getGlobalDialogBaseUnits(); } FontMetrics fm = c.getFontMetrics(getDefaultDialogFont()); DialogBaseUnits dialogBaseUnits = (DialogBaseUnits) cachedDialogBaseUnits.get(fm); if (dialogBaseUnits == null) { dialogBaseUnits = computeDialogBaseUnits(fm); cachedDialogBaseUnits.put(fm, dialogBaseUnits); } return dialogBaseUnits; }
/** * Computes the global dialog base units. The current implementation assumes * a fixed 8pt font and on 96 or 120 dpi. A better implementation should ask * for the main dialog font and should honor the current screen resolution. * <p> * * Should be re-computed if the l&f, platform, or screen changes. * * @return a DialogBaseUnits object used globally if no container is * available */ private DialogBaseUnits computeGlobalDialogBaseUnits() { logInfo("Computing global dialog base units..."); Font dialogFont = getDefaultDialogFont(); FontMetrics metrics = createDefaultGlobalComponent().getFontMetrics(dialogFont); DialogBaseUnits globalDialogBaseUnits = computeDialogBaseUnits(metrics); return globalDialogBaseUnits; }