
it.tidalwave.swing.FontCache Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* OpenBlueSky - NetBeans Platform Enhancements
* Copyright (C) 2006-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://openbluesky.kenai.com
* SCM: https://kenai.com/hg/openbluesky~src
*
**********************************************************************************************************************/
package it.tidalwave.swing;
import java.awt.Font;
import javax.swing.JComponent;
import javax.swing.JLabel;
/*******************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
******************************************************************************/
public class FontCache
{
private final int minFontSize;
private final int maxFontSize;
private Font font;
private final JComponent component = new JLabel();
/*******************************************************************************
*
* @param minFontSize
* @param maxFontSize
*
*******************************************************************************/
public FontCache (final String family, final int minFontSize, final int maxFontSize)
{
this(family, Font.PLAIN, minFontSize, maxFontSize);
}
/*******************************************************************************
*
* @param minFontSize
* @param maxFontSize
*
*******************************************************************************/
public FontCache (final String family, final int style, final int minFontSize, final int maxFontSize)
{
component.setFont(new Font(family, style, minFontSize));
this.minFontSize = minFontSize;
this.maxFontSize = maxFontSize;
}
/*******************************************************************************
*
* @param baseFont
* @param size
* @return
*
*******************************************************************************/
public Font getFont (final double size)
{
return getFont(component, size);
}
/*******************************************************************************
*
* @param baseFont
* @param size
* @return
*
*******************************************************************************/
private Font getFont (final JComponent component, final double size)
{
final float fontSize = (float)Math.min(maxFontSize, Math.max(minFontSize, size));
if ((font == null) || (Math.abs(font.getSize2D() - fontSize) > 0.05))
{
font = component.getFont().deriveFont(fontSize);
// TODO: Put in cache
System.err.println("CREATED FONT FOR " + fontSize + ": " + font);
}
return font;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy