Java Font Text Size getSize(Font font)

Here you can find the source of getSize(Font font)

Description

get Size

License

Apache License

Declaration

public static Number getSize(Font font) 

Method Source Code


//package com.java2s;
/*/*w  w w .j a v a  2  s.co m*/
 * Copyright (C) 2011 Cozycode.net
 * 
 * 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.
 */

import java.awt.Font;

import java.awt.font.TextAttribute;

import java.text.AttributedCharacterIterator.Attribute;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static Number getSize(Font font) {
        return getAsNumber(font, TextAttribute.SIZE);
    }

    public static Number getAsNumber(Font font, Attribute attribute) {
        Object obj = font.getAttributes().get(attribute);
        return obj instanceof Number ? (Number) obj : null;
    }

    /**
     * Returns a map of known types rather than the unusable (poorly typed) map returned by Font.
     */
    public static Map<Attribute, Object> getAttributes(Font font) {
        return new HashMap<Attribute, Object>(font.getAttributes());
    }
}

Related

  1. getStringDimension(Component comp, String str)
  2. getStringDimensions(Graphics2D graphics, Font font, String[] stringsByLine)
  3. getStringDisplaySize(String input)
  4. getStringSize(final Graphics g, final Font font, final String text)