Java Size getFontSizeInPoints(String fontSizeWithUnit)

Here you can find the source of getFontSizeInPoints(String fontSizeWithUnit)

Description

get Font Size In Points

License

Open Source License

Declaration

public static Integer getFontSizeInPoints(String fontSizeWithUnit) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.ja  va 2s  .  co  m*/
  * (c) Copyright 2010-2011 AgileBirds
  *
  * This file is part of OpenFlexo.
  *
  * OpenFlexo is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * OpenFlexo is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  *
  */

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParsePosition;

public class Main {
    public static Integer getFontSizeInPoints(String fontSizeWithUnit) {
        fontSizeWithUnit = fontSizeWithUnit.trim();

        DecimalFormat formatter = new DecimalFormat();
        DecimalFormatSymbols formatterSymbol = new DecimalFormatSymbols();
        formatterSymbol.setDecimalSeparator('.');
        formatter.setDecimalFormatSymbols(formatterSymbol);

        ParsePosition position = new ParsePosition(0);
        Number size = formatter.parse(fontSizeWithUnit, position);

        if (size == null)
            return null;

        String unit = "px";
        if (position.getIndex() < fontSizeWithUnit.length())
            unit = fontSizeWithUnit.substring(position.getIndex()).trim().toLowerCase();

        if ("px".equals(unit))
            return new Double(size.doubleValue() * (92 / 72)).intValue(); //Round to transform px to points, 92 dpi usually, 1 inch = 72 points
        if ("pt".equals(unit))
            return new Double(size.doubleValue()).intValue();

        //Don't handle % or em

        return null;
    }
}

Related

  1. calcSize(double s, String type, int div)
  2. createSizeFormat(Locale locale)
  3. getBytesSpecificationDescription(double sizeInBytes, String formatSpec)
  4. getByteStr(long size)
  5. getDataSize(long size)
  6. getFormatSize(double size)
  7. getFormatSize(long size)
  8. getFormattedSize(long size)
  9. getReadableByteSize(long size)