private String lookupFont ( String family , CSSProperty . FontWeight weight , CSSProperty . FontStyle style ) { final String systemFontNames [ ] = GraphicsEnvironment . getLocalGraphicsEnvironment ( ) . getAvailableFontFamilyNames ( ) ; //try to look in the style font table String nameFound = null ; FontSpec spec = new FontSpec ( family , weight , style ) ; List < RuleFontFace . Source > srcs = findMatchingFontSources ( spec ) ; if ( srcs != null ) { for ( RuleFontFace . Source src : srcs ) { if ( src instanceof RuleFontFace . SourceLocal ) { String name = fontAvailable ( ( ( RuleFontFace . SourceLocal ) src ) . getName ( ) , systemFontNames ) ; if ( name != null ) { nameFound = name ; break ; } } else if ( src instanceof RuleFontFace . SourceURL && viewport . getConfig ( ) . isLoadFonts ( ) ) { try { TermURI urlstring = ( ( RuleFontFace . SourceURL ) src ) . getURI ( ) ; String format = ( ( RuleFontFace . SourceURL ) src ) . getFormat ( ) ; if ( format == null || FontDecoder . supportedFormats . contains ( format ) ) { URL url = DataURLHandler . createURL ( urlstring . getBase ( ) , urlstring . getValue ( ) ) ; String regName = FontDecoder . findRegisteredFont ( url ) ; if ( regName == null ) { DocumentSource imgsrc = viewport . getConfig ( ) . createDocumentSource ( url ) ; Font newFont = FontDecoder . decodeFont ( imgsrc , format ) ; if ( GraphicsEnvironment . getLocalGraphicsEnvironment ( ) . registerFont ( newFont ) ) log . debug ( "Registered font: {}" , newFont . getFontName ( ) ) ; else log . debug ( "Failed to register font: {} (not fatal, probably already existing)" , newFont . getFontName ( ) ) ; regName = newFont . getFontName ( ) ; FontDecoder . registerFont ( url , regName ) ; } nameFound = regName ; } } catch ( MalformedURLException e ) { log . error ( "Couldn't load font with URI {} ({})" , ( ( RuleFontFace . SourceURL ) src ) . getURI ( ) , e . getMessage ( ) ) ; } catch ( IOException e ) { log . error ( "Couldn't load font with URI {} ({})" , ( ( RuleFontFace . SourceURL ) src ) . getURI ( ) , e . getMessage ( ) ) ; } catch ( FontFormatException e ) { log . error ( "Couldn't decode font with URI {} ({})" , ( ( RuleFontFace . SourceURL ) src ) . getURI ( ) , e . getMessage ( ) ) ; } } } } //if nothing found, try the system font names if ( nameFound == null ) { nameFound = fontAvailable ( family , systemFontNames ) ; } //create the font when found return nameFound ; }
Check if the font family is available either among the CSS defined fonts or the system fonts . If found registers a system font with the given name .