4

Fontオブジェクトからネイティブフォント名を取得する方法はJavaにありますか?

このコードFont.decode("Serif")を使用してフォントを取得します。デバッグの目的で、使用されているネイティブフォントを知りたいです。

4

3 に答える 3

6

それほど単純ではないかもしれません。一部のフォントは、さまざまなグリフにさまざまな物理フォントを使用して、多くの物理フォントで構成されています。

たとえば、私のWindowsシステムでは、Serifフォントは12の物理フォントを使用します。

  • ** TrueTypeフォント:Family = Times New Roman Name = Times New Roman style = 0 fileName = C:\ Windows \ Fonts \ TIMES.TTF
  • ** TrueTypeフォント:Family = Wingdings Name = Wingdings style = 0 fileName = C:\ Windows \ Fonts \ WINGDING.TTF
  • ** TrueTypeフォント:Family = Symbol Name = Symbol style = 0 fileName = C:\ Windows \ Fonts \ SYMBOL.TTF
  • ** TrueTypeフォント:Family = Lucida Sans Name = Lucida Sans Regular style = 0 fileName = C:\ Java \ jdk \ jdk1.6.0_37 \ jre \ lib \ fonts \ LucidaSansRegular.ttf
  • ** TrueTypeフォント:Family = MingLiU Name = MingLiU style = 0 fileName = C:\ Windows \ Fonts \ MINGLIU.TTC
  • ** TrueTypeフォント:Family = Lucida Sans Name = Lucida Sans Regular style = 0 fileName = C:\ Java \ jdk \ jdk1.6.0_37 \ jre \ lib \ fonts \ LucidaSansRegular.ttf
  • ** TrueTypeフォント:Family = SimSun Name = SimSun style = 0 fileName = C:\ Windows \ Fonts \ SIMSUN.TTC
  • ** TrueTypeフォント:Family = Lucida Sans Name = Lucida Sans Regular style = 0 fileName = C:\ Java \ jdk \ jdk1.6.0_37 \ jre \ lib \ fonts \ LucidaSansRegular.ttf
  • ** TrueTypeフォント:Family = MS Mincho Name = MS Mincho style = 0 fileName = C:\ Windows \ Fonts \ MSMINCHO.TTC
  • ** TrueTypeフォント:Family = Batang Name = Batang style = 0 fileName = C:\ Windows \ Fonts \ batang.TTC
  • ** TrueTypeフォント:Family = MingLiU-ExtB Name = MingLiU-ExtB style = 0 fileName = C:\ Windows \ Fonts \ MINGLIUB.TTC
  • ** TrueTypeフォント:Family = SimSun-ExtB Name = SimSun-ExtB style = 0 fileName = C:\ Windows \ Fonts \ SIMSUNB.TTF

次のコードは、フォントを物理コンポーネントに分解できます。リフレクションハックを使用してsun.awt.Font2Dオブジェクトにアクセスするため、自己責任で使用してください(Oracle Java 6u37で動作します)。

import java.awt.Font;
import java.lang.reflect.Method;
import java.util.Locale;

import sun.font.CompositeFont;
import sun.font.Font2D;
import sun.font.PhysicalFont;

public class FontTester
{
    public static void main(String... args)
    throws Exception
    {
        Font font = new Font("Serif", Font.PLAIN, 12);
        describeFont(font);
    }

    private static void describeFont(Font font)
    throws Exception
    {
        Method method = font.getClass().getDeclaredMethod("getFont2D");
        method.setAccessible(true);
        Font2D f = (Font2D)method.invoke(font);

        describeFont2D(f);
    }

    private static void describeFont2D(Font2D font)
    {
        if (font instanceof CompositeFont)
        {
            System.out.println("Font '" + font.getFontName(Locale.getDefault()) + "' is composed of:");

            CompositeFont cf = (CompositeFont)font;
            for (int i = 0; i < cf.getNumSlots(); i++)
            {
                PhysicalFont pf = cf.getSlotFont(i);
                describeFont2D(pf);
            }
        }
        else
            System.out.println("-> " + font);
    }
}
于 2012-10-25T22:50:52.843 に答える
2

prungeの答えは、ネイティブ(物理)フォントの名前を実際に公開しなかったことを除いて、ほぼ完璧でした。describeFont2Dメソッドに対する次の小さな変更は、Javaリフレクションを再び活用することでトリックを実行します。

java.lang.reflect.Fieldをインポートすることを忘れないでください。

private static void describeFont2D( Font2D font ) throws Exception{
    if( font instanceof CompositeFont ){
        System.out.println( "Font '"+font.getFontName( Locale.getDefault() )+"' is composed of:" );
        CompositeFont cf = ( CompositeFont )font;
        for( int i = 0; i<cf.getNumSlots(); i++ ){
            PhysicalFont pf = cf.getSlotFont( i );
            describeFont2D( pf );
        }
    }else if( font instanceof CFont ){
        Field field = CFont.class.getDeclaredField( "nativeFontName" );
        field.setAccessible( true );
        String nativeFontName = ( String )field.get( font );                
        System.out.println( "-> "+nativeFontName );
    }else
        System.out.println( "-> "+font );
}
于 2016-05-11T18:00:31.647 に答える
1

このコードは、システムフォントが使用可能な場合はそれを取得し、何らかの理由で使用できない場合はデフォルトのファミリを取得します。

static String[] AS_System_Fonts = null;
public static String[] getFontFamilies(){
    if( AS_System_Fonts != null ) return AS_System_Fonts;
    java.awt.GraphicsEnvironment gEnv = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
    AS_System_Fonts = gEnv.getAvailableFontFamilyNames();
    if( AS_System_Fonts == null ){ // should not happen
        AS_System_Fonts = new String[8];
        AS_System_Fonts[0] = "Serif";
        AS_System_Fonts[1] = "Sans-Serif";
        AS_System_Fonts[2] = "Monospaced";
        AS_System_Fonts[3] = "Dialog";
        AS_System_Fonts[4] = "Dialog Input";
        AS_System_Fonts[5] = "Lucida Bright";
        AS_System_Fonts[6] = "Lucida Sans";
        AS_System_Fonts[7] = "Lucida Sans Typewriter";
    }
    return AS_System_Fonts;
}
于 2012-10-25T22:45:09.897 に答える