2012-10-25 5 views
1

자바에서 Font 개체의 네이티브 글꼴 이름을 가져 오는 방법이 있습니까?Java에서 기본 글꼴 이름을 검색하는 방법은 무엇입니까?

이 코드 Font.decode("Serif")을 사용하여 내 글꼴을 얻었으며 디버깅 목적으로 사용 된 기본 글꼴을 알고 싶습니다.

+1

"네이티브 글꼴"이란 무엇입니까? –

+0

시스템에 기본 글꼴이 설치되어 있다는 것을 보여주는 Java 응용 프로그램이 있으므로 (예 : IntelliJ IDEA는 내 Linux 상자에 'DejaVu Sans Mono'와 같은 글꼴 목록을 표시합니다.) 그것은 이것을 달성하기위한 정확한 단계가 아니라는 것입니다. –

+0

font.getFontName()을 사용해 보셨나요? – GreyBeardedGeek

답변

5

그것은 그렇게 간단하지 않을 수 있습니다. 일부 글꼴은 서로 다른 글립 문자에 대해 서로 다른 실제 글꼴을 사용하는 많은 실제 글꼴로 구성됩니다.

  • ** 트루 타입 글꼴 : 가족 = 굴림 이름 = 굴림 스타일 = 0 fileName에 = C : \ WINDOWS \ 예를 들어

    , 내 Windows 시스템에서 고딕 글꼴 (12 개) 물리적 글꼴을 사용합니다 글꼴 \ TIMES.TTF

  • ** 트루 타입 글꼴 : 가족 = Wingdings를 이름 = Wingdings를 스타일 = 0 fileName에 = C : \ WINDOWS \ 글꼴 \ WINGDING.TTF
  • ** 트루 타입 글꼴 : 가족 = 기호 명칭 = 기호 스타일 = 0 파일 이름 = C : \ Windows \ Fonts \ SYMBOL.TTF
  • ** TrueType 글꼴 : Family = Lucida Sans Name = Lucida Sans 일반 스타일 = 0 파일 이름 = 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
  • ** 트루 타입 글꼴 : 가족 = Lucida 나타 이름 = Lucida 나타 정기 스타일 = 0 fileName에 = C : \ 자바 \ JDK \ jdk1.6.0_37 \ JRE \ lib 디렉토리 \ 글꼴 \ LucidaSansRegular.ttf
  • ** TrueType 글꼴 : Family = SimSun Name = SimSun 스타일 = 0 파일 이름 = C : \ Windows \ Fonts \ SIMSUN.TTC
  • ** TrueType 글꼴 : Family = Lucida Sans Name = Lucida Sans 일반 스타일 = 0 fileName = C : \ Java \ jdk \ jdk1.6.0_37 \ jre \ lib \ fonts \ LucidaSansRegular.ttf
  • ** 트루 타입 글꼴 : Family = MS 민초 Name = MS 민초 스타일 = 0 파일 이름 = C : \ Windows \ Fonts \ MSMINCHO.TTC
  • ** 트루 타입 글꼴 : 패밀리 = Batang 이름 = Batang 스타일 = 0 파일 이름 = C : \ Windows \ Fonts \ batang.TTC
  • ** 트루 타입 글꼴 : 패밀리 = MingLiU-ExtB 이름 = MingLiU-ExtB 스타일 = 0 파일 이름 = C : \ Windows \ Fonts \ MINGLIUB.TTC
  • ** 트루 타입 글꼴 : Family = SimSun-ExtB Name = SimSun-ExtB style = 0 fileName = C : \ Windows \ Fonts \ SIMSUNB.TTF

다음 코드는 글꼴을 물리적 구성 요소로 나눌 수 있습니다. 그것은 반사가 sun.awt.Font2D 개체에 액세스하는 해킹, 그래서 당신의 자신의 위험에 사용하는 사용 (오라클 자바 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); 
    } 
} 
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; 
} 
+0

비표준 변수 명명 이외 (AS_System_Fonts는 systemFonts라는 이름이 훨씬 좋습니다), 좋은 답변입니다! – GreyBeardedGeek

+0

변수 이름에 대해 미안하다. 특수한 명명 규칙이있는 유틸리티 키트에서 나오는 아티팩트 –

0

prunge의 대답은 거의 완벽했다 (물리적 인) 폰트의 이름. describeFont2D 메소드에 대한 다음과 같은 작은 변경 사항은 자바 리플렉션을 다시 사용하여 트릭을 수행합니다.

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); 
} 
+0

[@ dan-borque] (https://stackoverflow.com/users/77479/dan-bourque)),'sun.font.CFont'도 임포트해야합니다. 즉,'sun.font.CFont'는 Mac OS X에서만 사용 가능합니다.이 방법은이 수정 된 메소드를 해당 플랫폼에 사용하는 것을 제한합니다. –

관련 문제