Java字體優化

需求背景java

最近在作的項目顯示的字體感受太醜,因而乎想着DIY改進一下。web

查閱資料,總以爲別人寫的都不咋地,因而決心寫一篇略微完善點的關於項目字體優化方面的文章。windows

固然,這篇文章不會教你如何使用TrueType去建立屬於你本身的字體,而是使用現成的字體庫。api

讀取、使用系統字體回顧app

讀取系統字體的字體名字體數量ide

方法1: 使用GraphicsEnvironment類svg

  • 獲取全部字體的family
package com.forget406.study;

import java.awt.GraphicsEnvironment;

public class CustomeFontReading {
    public static void main(String[] args) {
        String[] fonts = 
            GraphicsEnvironment  // GraphicsEnvironment(抽象類)  圖形環境類
                .getLocalGraphicsEnvironment()  // 獲取本地圖形環境
                    .getAvailableFontFamilyNames();  // 獲取可用字體family名
        
        int fontCount = 0;   // 字體數統計
        for(String font : fonts) {
            fontCount ++;
            System.out.println(font);
        }
        System.out.println("系統字體數:" + fontCount);
    }
}
Agency FB
Algerian
Arial
Arial Black
Arial Narrow
Arial Rounded MT Bold
Arial Unicode MS
Arvo
Baskerville Old Face
Bauhaus 93
Bell MT
Berlin Sans FB
Berlin Sans FB Demi
Bernard MT Condensed
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Condensed
Bodoni MT Poster Compressed
Book Antiqua
Bookman Old Style
Bookshelf Symbol 7
Bradley Hand ITC
Britannic Bold
Broadway
Brush Script MT
Calibri
Calibri Light
Californian FB
Calisto MT
Cambria
Cambria Math
Candara
Castellar
Centaur
Century
Century Gothic
Century Schoolbook
Chiller
Colonna MT
Comic Sans MS
Consolas
Constantia
Cooper Black
Copperplate Gothic Bold
Copperplate Gothic Light
Corbel
Courier New
Curlz MT
Dialog
DialogInput
Droid Serif
Ebrima
Edwardian Script ITC
Elephant
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Euclid
Euclid Extra
Euclid Fraktur
Euclid Math One
Euclid Math Two
Euclid Symbol
Felix Titling
Fences
Footlight MT Light
Forte
Franklin Gothic Book
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium
Franklin Gothic Medium Cond
Freestyle Script
French Script MT
Gabriola
Gadugi
Garamond
Georgia
Gigi
Gill Sans MT
Gill Sans MT Condensed
Gill Sans MT Ext Condensed Bold
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Stout
Haettenschweiler
Harlow Solid Italic
Harrington
High Tower Text
Impact
Imprint MT Shadow
Indie Flower
Informal Roman
Javanese Text
Jokerman
Juice ITC
Kristen ITC
Kunstler Script
Leelawadee UI
Leelawadee UI Semilight
Lobster
Lucida Bright
Lucida Calligraphy
Lucida Console
Lucida Fax
Lucida Handwriting
Lucida Sans
Lucida Sans Typewriter
Lucida Sans Unicode
Magneto
Maiandra GD
Malgun Gothic
Malgun Gothic Semilight
Marlett
Matura MT Script Capitals
Meiryo
Meiryo UI
Microsoft Himalaya
Microsoft JhengHei
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Light
Microsoft MHei
Microsoft NeoGothic
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft YaHei UI
Microsoft YaHei UI Light
Microsoft Yi Baiti
MingLiU-ExtB
MingLiU_HKSCS-ExtB
Mistral
Modern No. 20
Mongolian Baiti
Monospaced
Monotype Corsiva
MS Gothic
MS Outlook
MS PGothic
MS Reference Sans Serif
MS Reference Specialty
MS UI Gothic
MT Extra
MT Extra Tiger
MV Boli
Myanmar Text
Niagara Engraved
Niagara Solid
Nirmala UI
Nirmala UI Semilight
OCR A Extended
Old English Text MT
Onyx
Open Sans
Palace Script MT
Palatino Linotype
Papyrus
Parchment
Perpetua
Perpetua Titling MT
Playbill
PMingLiU-ExtB
Poiret One
Poor Richard
Pristina
Rage Italic
Raleway
Ravie
Roboto
Roboto Condensed
Roboto Slab
Rockwell
Rockwell Condensed
Rockwell Extra Bold
SansSerif
Script MT Bold
Segoe MDL2 Assets
Segoe Print
Segoe Script
Segoe UI
Segoe UI Black
Segoe UI Emoji
Segoe UI Historic
Segoe UI Light
Segoe UI Semibold
Segoe UI Semilight
Segoe UI Symbol
Segoe WP
Segoe WP Black
Segoe WP Light
Segoe WP Semibold
Segoe WP SemiLight
Serif
Showcard Gothic
SimSun-ExtB
Sitka Banner
Sitka Display
Sitka Heading
Sitka Small
Sitka Subheading
Sitka Text
Snap ITC
Stencil
Sylfaen
Symbol
Symbol Tiger
Symbol Tiger Expert
Tahoma
Tempus Sans ITC
Tiger
Tiger Expert
Times New Roman
Trebuchet MS
Tw Cen MT
Tw Cen MT Condensed
Tw Cen MT Condensed Extra Bold
Verdana
Viner Hand ITC
Vivaldi
Vladimir Script
Webdings
Wide Latin
Wingdings
Wingdings 2
Wingdings 3
woziku-wfzt-CN4262
Yu Gothic
Yu Gothic Light
Yu Gothic Medium
Yu Gothic UI
Yu Gothic UI Light
Yu Gothic UI Semibold
Yu Gothic UI Semilight
仿宋
華文中宋
華文仿宋
華文宋體
華文彩雲
華文新魏
華文楷體
華文琥珀
華文細黑
華文行楷
華文隸書
宋體
幼圓
微軟雅黑
微軟雅黑 Light
我字酷無非字體
新宋體
方正蘭亭超細黑簡體
方正姚體
方正等線
方正舒體
楷體
等線
等線 Light
隸書
黑體
系統字體數:267
程序運行結果
  • 獲取全部字體的名字
package com.forget406.study;

import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class CustomeFontReading4 {
    public static void main(String[] args) {
        
        Font[] fonts = GraphicsEnvironment
            .getLocalGraphicsEnvironment()
                .getAllFonts();
        for (Font font : fonts) {
            System.out.println(font.getFontName());
        }
System.out.println("字體數量:" + fonts.length); } }
Agency FB
Agency FB Bold
Algerian
Arial
Arial Black
Arial Bold
Arial Bold Italic
Arial Italic
Arial Narrow
Arial Narrow Bold
Arial Narrow Bold Italic
Arial Narrow Italic
Arial Rounded MT Bold
Arial Unicode MS
Arvo Bold
Arvo Bold
Arvo Bold Italic
Arvo-Italic
Baskerville Old Face
Bauhaus 93
Bell MT
Bell MT Bold
Bell MT Italic
Berlin Sans FB
Berlin Sans FB Bold
Berlin Sans FB Demi Bold
Bernard MT Condensed
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Black Italic
Bodoni MT Bold
Bodoni MT Bold Italic
Bodoni MT Condensed
Bodoni MT Condensed Bold
Bodoni MT Condensed Bold Italic
Bodoni MT Condensed Italic
Bodoni MT Italic
Bodoni MT Poster Compressed
Book Antiqua
Book Antiqua Bold
Book Antiqua Bold Italic
Book Antiqua Italic
Bookman Old Style
Bookman Old Style Bold
Bookman Old Style Bold Italic
Bookman Old Style Italic
Bookshelf Symbol 7
Bradley Hand ITC
Britannic Bold
Broadway
Brush Script MT Italic
Calibri
Calibri Bold
Calibri Bold Italic
Calibri Italic
Calibri Light
Calibri Light Italic
Californian FB
Californian FB Bold
Californian FB Italic
Calisto MT
Calisto MT Bold
Calisto MT Bold Italic
Calisto MT Italic
Cambria
Cambria Bold
Cambria Bold Italic
Cambria Italic
Cambria Math
Candara
Candara Bold
Candara Bold Italic
Candara Italic
Castellar
Centaur
Century
Century Gothic
Century Gothic Bold
Century Gothic Bold Italic
Century Gothic Italic
Century Schoolbook
Century Schoolbook Bold
Century Schoolbook Bold Italic
Century Schoolbook Italic
Chiller
Colonna MT
Comic Sans MS
Comic Sans MS Bold
Comic Sans MS Bold Italic
Comic Sans MS Italic
Consolas
Consolas Bold
Consolas Bold Italic
Consolas Italic
Constantia
Constantia Bold
Constantia Bold Italic
Constantia Italic
Cooper Black
Copperplate Gothic Bold
Copperplate Gothic Light
Corbel
Corbel Bold
Corbel Bold Italic
Corbel Italic
Courier New
Courier New Bold
Courier New Bold Italic
Courier New Italic
Curlz MT
Dialog.bold
Dialog.bolditalic
Dialog.italic
Dialog.plain
DialogInput.bold
DialogInput.bolditalic
DialogInput.italic
DialogInput.plain
Droid Serif Bold
Droid Serif Bold
Droid Serif Bold Italic
Droid Serif Italic
Ebrima
Ebrima Bold
Edwardian Script ITC
Elephant
Elephant Italic
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Euclid
Euclid Bold
Euclid Bold Italic
Euclid Extra
Euclid Extra Bold
Euclid Fraktur
Euclid Fraktur Bold
Euclid Italic
Euclid Math One
Euclid Math One Bold
Euclid Math Two
Euclid Math Two Bold
Euclid Symbol
Euclid Symbol Bold
Euclid Symbol Bold Italic
Euclid Symbol Italic
Felix Titling
Fences Plain
Footlight MT Light
Forte
Franklin Gothic Book
Franklin Gothic Book Italic
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Demi Italic
Franklin Gothic Heavy
Franklin Gothic Heavy Italic
Franklin Gothic Medium
Franklin Gothic Medium Cond
Franklin Gothic Medium Italic
Freestyle Script
French Script MT
Gabriola
Gadugi
Gadugi Bold
Garamond
Garamond Bold
Garamond Italic
Georgia
Georgia Bold
Georgia Bold Italic
Georgia Italic
Gigi
Gill Sans MT
Gill Sans MT Bold
Gill Sans MT Bold Italic
Gill Sans MT Condensed
Gill Sans MT Ext Condensed Bold
Gill Sans MT Italic
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Old Style Bold
Goudy Old Style Italic
Goudy Stout
Haettenschweiler
Harlow Solid Italic
Harrington
High Tower Text
High Tower Text Italic
Impact
Imprint MT Shadow
Indie Flower
Informal Roman
Javanese Text
Jokerman
Juice ITC
Kristen ITC
Kunstler Script
Leelawadee UI
Leelawadee UI Bold
Leelawadee UI Semilight
Lobster
Lucida Bright Regular
Lucida Bright Demibold
Lucida Bright Demibold Italic
Lucida Bright Italic
Lucida Bright Regular
Lucida Calligraphy Italic
Lucida Console
Lucida Fax Demibold
Lucida Fax Demibold Italic
Lucida Fax Italic
Lucida Fax Regular
Lucida Handwriting Italic
Lucida Sans Demibold
Lucida Sans Demibold
Lucida Sans Demibold Roman
Lucida Sans Italic
Lucida Sans Regular
Lucida Sans Typewriter Bold
Lucida Sans Typewriter Bold
Lucida Sans Typewriter Oblique
Lucida Sans Typewriter Regular
Lucida Sans Unicode
MS Gothic
MS Outlook
MS PGothic
MS Reference Sans Serif
MS Reference Specialty
MS UI Gothic
MT Extra
MT Extra Tiger
MV Boli
Magneto Bold
Maiandra GD
Malgun Gothic
Malgun Gothic Bold
Malgun Gothic Semilight
Marlett
Matura MT Script Capitals
Meiryo
Meiryo Bold
Meiryo Bold Italic
Meiryo Italic
Meiryo UI
Meiryo UI Bold
Meiryo UI Bold Italic
Meiryo UI Italic
Microsoft Himalaya
Microsoft JhengHei
Microsoft JhengHei Bold
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Bold
Microsoft JhengHei UI Light
Microsoft MHei
Microsoft MHei Bold
Microsoft NeoGothic
Microsoft NeoGothic Bold
Microsoft New Tai Lue
Microsoft New Tai Lue Bold
Microsoft PhagsPa
Microsoft PhagsPa Bold
Microsoft Sans Serif
Microsoft Tai Le
Microsoft Tai Le Bold
Microsoft YaHei UI Light
Microsoft Yahei UI
Microsoft Yahei UI Bold
Microsoft Yi Baiti
MingLiU-ExtB
MingLiU_HKSCS-ExtB
Mistral
Modern No. 20
Mongolian Baiti
Monospaced.bold
Monospaced.bolditalic
Monospaced.italic
Monospaced.plain
Monotype Corsiva
Myanmar Text
Myanmar Text Bold
新宋體
Niagara Engraved
Niagara Solid
Nirmala UI
Nirmala UI Bold
Nirmala UI Semilight
OCR A Extended
Old English Text MT
Onyx
Open Sans Bold
Open Sans Bold
Open Sans Bold Italic
Open Sans Italic
PMingLiU-ExtB
Palace Script MT
Palatino Linotype
Palatino Linotype Bold
Palatino Linotype Bold Italic
Palatino Linotype Italic
Papyrus
Parchment
Perpetua
Perpetua Bold
Perpetua Bold Italic
Perpetua Italic
Perpetua Titling MT Bold
Perpetua Titling MT Light
Playbill
Poiret One
Poor Richard
Pristina
Rage Italic
Raleway Bold
Raleway Bold
Ravie
Roboto Bold
Roboto Bold
Roboto Bold Italic
Roboto Condensed Bold
Roboto Condensed Bold
Roboto Condensed Bold Italic
Roboto Condensed Italic
Roboto Italic
Roboto Slab Bold
Roboto Slab Regular
Rockwell
Rockwell Bold
Rockwell Bold Italic
Rockwell Condensed
Rockwell Condensed Bold
Rockwell Extra Bold
Rockwell Italic
SansSerif.bold
SansSerif.bolditalic
SansSerif.italic
SansSerif.plain
Script MT Bold
Segoe MDL2 Assets
Segoe Print
Segoe Print Bold
Segoe Script
Segoe Script Bold
Segoe UI
Segoe UI Black
Segoe UI Black Italic
Segoe UI Bold
Segoe UI Bold Italic
Segoe UI Emoji
Segoe UI Historic
Segoe UI Italic
Segoe UI Light
Segoe UI Light Italic
Segoe UI Semibold
Segoe UI Semibold Italic
Segoe UI Semilight
Segoe UI Semilight Italic
Segoe UI Symbol
Segoe WP
Segoe WP Black
Segoe WP Bold
Segoe WP Light
Segoe WP SemiLight
Segoe WP Semibold
Serif.bold
Serif.bolditalic
Serif.italic
Serif.plain
Showcard Gothic
宋體
SimSun-ExtB
Sitka Banner
Sitka Banner Bold
Sitka Banner Bold Italic
Sitka Banner Italic
Sitka Display
Sitka Display Bold
Sitka Display Bold Italic
Sitka Display Italic
Sitka Heading
Sitka Heading Bold
Sitka Heading Bold Italic
Sitka Heading Italic
Sitka Small
Sitka Small Bold
Sitka Small Bold Italic
Sitka Small Italic
Sitka Subheading
Sitka Subheading Bold
Sitka Subheading Bold Italic
Sitka Subheading Italic
Sitka Text
Sitka Text Bold
Sitka Text Bold Italic
Sitka Text Italic
Snap ITC
Stencil
Sylfaen
Symbol
Symbol Tiger
Symbol Tiger Expert
Tahoma
Tahoma Bold
Tempus Sans ITC
Tiger
Tiger Expert
Times New Roman
Times New Roman Bold
Times New Roman Bold Italic
Times New Roman Italic
Trebuchet MS
Trebuchet MS Bold
Trebuchet MS Bold Italic
Trebuchet MS Italic
Tw Cen MT
Tw Cen MT Bold
Tw Cen MT Bold Italic
Tw Cen MT Condensed
Tw Cen MT Condensed Bold
Tw Cen MT Condensed Extra Bold
Tw Cen MT Italic
Verdana
Verdana Bold
Verdana Bold Italic
Verdana Italic
Viner Hand ITC
Vivaldi Italic
Vladimir Script
Webdings
Wide Latin
Wingdings
Wingdings 2
Wingdings 3
Yu Gothic Bold
Yu Gothic Light
Yu Gothic Medium
Yu Gothic Regular
Yu Gothic UI Bold
Yu Gothic UI Light
Yu Gothic UI Regular
Yu Gothic UI Semibold
Yu Gothic UI Semilight
YuGothic
YuGothic Bold
我字酷無非字體
仿宋
華文中宋
華文仿宋
華文宋體
華文彩雲
華文新魏
華文楷體
華文琥珀
華文細黑
華文行楷
華文隸書
宋體
幼圓
微軟雅黑
微軟雅黑 Bold
微軟雅黑 Light
我字酷無非字體
新宋體
方正蘭亭超細黑簡體
方正姚體
方正等線 Regular
方正舒體
楷體
方正等線 Regular
等線 Bold
等線 Light
隸書
黑體
字體數量:479
程序運行結果

以上兩段代碼有細微的區別,根本緣由在於:oop

所見字體文件中包含多個字體子文件 。以下圖所示,左側簡體字又分紅:常規、粗體、細體,即包含右側的字體。性能

             

方法2: 調用核心類庫的文件操做API測試

  • 過濾字體文件
package com.forget406.study;

import java.io.File;
import java.io.FileFilter;

public class CustomeFontReading2 {
    public static void main(String[] args) {
        // windows10 系統字體庫目錄
        File file = new File("C:" + 
            File.separator+"Windows"+
                File.separator+"Fonts"
            );
        // 文件信息判斷
        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("該路徑爲文件夾");
                
                // 獲取目錄下全部以".ttf"(True Type Font)結尾的文件
                File[] fontFile = file.listFiles(new FileFilter() {
                    // 過濾出名字符合要求的文件
                    @Override
                    public boolean accept(File file) {
                        return file.getName().endsWith(".");
                    }
                    
                });
                
                // 輸出全部字體文件名(截去後綴)
                for(File font : fontFile) {
                    String name = font.getName();
                    int suffixIndex = name.lastIndexOf(".ttf");
                    System.out.println(name.substring(0, suffixIndex));
                }
                System.out.println("字體數量:" + fontFile.length);
            } else if (file.isFile()){
                System.out.println("該路徑爲文件");
            } else {
                System.out.println("貌似沒有別的可能了");
            }
            System.out.println(file.getName());
        } else {
            System.out.println("文件路徑不存在");
        }        
    }
}
該路徑爲文件夾
arial
arialbd
arialbi
ariali
ariblk
Arvo-Bold
Arvo-BoldItalic
Arvo-Italic
Arvo-Regular
calibri
calibrib
calibrii
calibril
calibrili
calibriz
cambriab
cambriai
cambriaz
Candara
Candarab
Candarai
Candaraz
comic
comicbd
comici
comicz
consola
consolab
consolai
consolaz
constan
constanb
constani
constanz
corbel
corbelb
corbeli
corbelz
cour
courbd
courbi
couri
Deng
Dengb
Dengl
DengXian
DroidSerif-Bold
DroidSerif-BoldItalic
DroidSerif-Italic
DroidSerif
ebrima
ebrimabd
euclid
euclidb
euclidbi
euclidi
eucsym
eucsymb
eucsymbi
eucsymi
euextra
euextrab
eufrak
eufrakb
eumat1
eumat1b
eumat2
eumat2b
fences
framd
framdit
Gabriola
gadugi
gadugib
georgia
georgiab
georgiai
georgiaz
himalaya
impact
IndieFlower
javatext
LeelaUIb
LeelawUI
LeelUIsl
Lobster-Regular
lucon
l_10646
malgun
malgunbd
malgunsl
marlett
micross
mmrtext
mmrtextb
monbaiti
MSMHei-Bold
MSMHei
MSNeoGothic-Bold
MSNeoGothic
msyi
Mt Extra Tiger
mtextra
mvboli
Nirmala
NirmalaB
NirmalaS
ntailu
ntailub
OpenSans-Bold
OpenSans-BoldItalic
OpenSans-Italic
OpenSans-Regular
pala
palab
palabi
palai
phagspa
phagspab
PoiretOne-Regular
Raleway-Bold
Raleway-Regular
Roboto-Bold
Roboto-BoldItalic
Roboto-Italic
Roboto-Regular
RobotoCondensed-Bold
RobotoCondensed-BoldItalic
RobotoCondensed-Italic
RobotoCondensed-Regular
RobotoSlab-Bold
RobotoSlab-Regular
segmdl2
segoepr
segoeprb
segoesc
segoescb
segoeui
segoeuib
segoeuii
segoeuil
segoeuisl
segoeuiz
SegoeWP-Black
SegoeWP-Bold
SegoeWP-Light
SegoeWP-Semibold
SegoeWP-Semilight
SegoeWP
seguibl
seguibli
seguiemj
seguihis
seguili
seguisb
seguisbi
seguisli
seguisym
simfang
simhei
simkai
simsunb
sylfaen
Symbol Tiger Expert
Symbol Tiger
symbol
tahoma
tahomabd
taile
taileb
Tiger Expert
Tiger
times
timesbd
timesbi
timesi
trebuc
trebucbd
trebucbi
trebucit
verdana
verdanab
verdanai
verdanaz
webdings
wingding
YuGothic-Bold
YuGothic
我字酷無非字體
我字酷無非字體_0
我字酷無非字體_1
字體數量:191
程序運行結果

注意:windows 10系統中字體文件後綴(suffix)含有三種:ttf、.TTF、.ttc、fon。

  • 不過濾字體文件

因爲c:\Windows\Font文件夾下除字體文件外不含其它形式文件,所以能夠不對文件結果進行過濾。

package com.forget406.study;

import java.io.File;
import java.io.FileFilter;

public class CustomeFontReading3 {
    public static void main(String[] args) {
        // windows10 系統字體庫目錄
        File file = new File("C:" + 
            File.separator+"Windows"+
                File.separator+"Fonts"
            );
        // 文件信息判斷
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] fontFile = file.listFiles();
                for(File font : fontFile) {
                    String name = font.getName();
                    int suffixIndex = name.lastIndexOf(".");
                    System.out.println(name.substring(0, suffixIndex));
                }
                System.out.println("字體數量:" + fontFile.length);
            } else if (file.isFile()){
                System.out.println("該路徑爲文件");
            } else {
                System.out.println("貌似沒有別的可能了");
            }
        } else {
            System.out.println("文件路徑不存在");
        }        
    }
}
該路徑爲文件夾
8514fix
8514fixe
8514fixg
8514fixr
8514fixt
8514oem
8514oeme
8514oemg
8514oemr
8514oemt
8514sys
8514syse
8514sysg
8514sysr
8514syst
85775
85855
85f1255
85f1256
85f1257
85f874
85s1255
85s1256
85s1257
85s874
AGENCYB
AGENCYR
ALGER
ANTQUAB
ANTQUABI
ANTQUAI
app775
app850
app852
app855
app857
app866
app932
app936
app949
app950
arial
arialbd
arialbi
ariali
ARIALN
ARIALNB
ARIALNBI
ARIALNI
ARIALUNI
ariblk
ARLRDBD
Arvo-Bold
Arvo-BoldItalic
Arvo-Italic
Arvo-Regular
BASKVILL
BAUHS93
BELL
BELLB
BELLI
BERNHC
BKANT
BOD_B
BOD_BI
BOD_BLAI
BOD_BLAR
BOD_CB
BOD_CBI
BOD_CI
BOD_CR
BOD_I
BOD_PSTC
BOD_R
BOOKOS
BOOKOSB
BOOKOSBI
BOOKOSI
BRADHITC
BRITANIC
BRLNSB
BRLNSDB
BRLNSR
BROADW
BRUSHSCI
BSSYM7
c8514fix
c8514oem
c8514sys
calibri
calibrib
calibrii
calibril
calibrili
calibriz
CALIFB
CALIFI
CALIFR
CALIST
CALISTB
CALISTBI
CALISTI
cambria
cambriab
cambriai
cambriaz
Candara
Candarab
Candarai
Candaraz
CASTELAR
CENSCBK
CENTAUR
CENTURY
cga40737
cga40850
cga40852
cga40857
cga40866
cga40869
cga40woa
cga80737
cga80850
cga80852
cga80857
cga80866
cga80869
cga80woa
CHILLER
COLONNA
comic
comicbd
comici
comicz
consola
consolab
consolai
consolaz
constan
constanb
constani
constanz
COOPBL
COPRGTB
COPRGTL
corbel
corbelb
corbeli
corbelz
coue1255
coue1256
coue1257
couf1255
couf1256
couf1257
cour
courbd
courbi
coure
couree
coureg
courer
couret
courf
courfe
courfg
courfr
courft
couri
CURLZ___
cvgafix
cvgasys
Deng
Dengb
Dengl
DengXian
desktop
dos737
dos869
dosapp
DroidSerif-Bold
DroidSerif-BoldItalic
DroidSerif-Italic
DroidSerif
ebrima
ebrimabd
ega40737
ega40850
ega40852
ega40857
ega40866
ega40869
ega40woa
ega80737
ega80850
ega80852
ega80857
ega80866
ega80869
ega80woa
ELEPHNT
ELEPHNTI
ENGR
ERASBD
ERASDEMI
ERASLGHT
ERASMD
euclid
euclidb
euclidbi
euclidi
eucsym
eucsymb
eucsymbi
eucsymi
euextra
euextrab
eufrak
eufrakb
eumat1
eumat1b
eumat2
eumat2b
FELIXTI
fences
fms_metadata
FORTE
FRABK
FRABKIT
FRADM
FRADMCN
FRADMIT
FRAHV
FRAHVIT
framd
FRAMDCN
framdit
FREESCPT
FRSCRIPT
FTLTLT
FZLTCXHJW
FZSTK
FZYTK
Gabriola
gadugi
gadugib
GARA
GARABD
GARAIT
georgia
georgiab
georgiai
georgiaz
GIGI
GILBI___
GILB____
GILC____
GILI____
GILLUBCD
GILSANUB
GIL_____
GLECB
GlobalMonospace
GlobalSansSerif
GlobalSerif
GlobalUserInterface
GLSNECB
GOTHIC
GOTHICB
GOTHICBI
GOTHICI
GOUDOS
GOUDOSB
GOUDOSI
GOUDYSTO
h8514fix
h8514oem
h8514sys
HARLOWSI
HARNGTON
HATTEN
himalaya
HTOWERT
HTOWERTI
hvgafix
hvgasys
impact
IMPRISHA
IndieFlower
INFROMAN
ITCBLKAD
ITCEDSCR
ITCKRIST
j8514fix
j8514oem
j8514sys
javatext
JOKERMAN
jsmalle
jsmallf
JUICE___
jvgafix
jvgasys
KUNSTLER
LATINWD
LBRITE
LBRITED
LBRITEDI
LBRITEI
LCALLIG
LeelaUIb
LeelawUI
LeelUIsl
LFAX
LFAXD
LFAXDI
LFAXI
LHANDW
Lobster-Regular
LSANS
LSANSD
LSANSDI
LSANSI
LTYPE
LTYPEB
LTYPEBO
LTYPEO
lucon
l_10646
MAGNETOB
MAIAN
malgun
malgunbd
malgunsl
marlett
MATURASC
MEIRYO
MEIRYOB
micross
mingliub
MISTRAL
mmrtext
mmrtextb
MOD20
modern
monbaiti
msgothic
msjh
msjhbd
msjhl
MSMHei-Bold
MSMHei
MSNeoGothic-Bold
MSNeoGothic
msyh
msyhbd
msyhl
msyi
Mt Extra Tiger
MTCORSVA
mtextra
mvboli
NIAGENG
NIAGSOL
Nirmala
NirmalaB
NirmalaS
ntailu
ntailub
OCRAEXT
OLDENGL
ONYX
OpenSans-Bold
OpenSans-BoldItalic
OpenSans-Italic
OpenSans-Regular
OUTLOOK
pala
palab
palabi
palai
PALSCRI
PAPYRUS
PARCHM
PERBI___
PERB____
PERI____
PERTIBD
PERTILI
PER_____
phagspa
phagspab
PLAYBILL
PoiretOne-Regular
POORICH
PRISTINA
RAGE
Raleway-Bold
Raleway-Regular
RAVIE
REFSAN
REFSPCL
Roboto-Bold
Roboto-BoldItalic
Roboto-Italic
Roboto-Regular
RobotoCondensed-Bold
RobotoCondensed-BoldItalic
RobotoCondensed-Italic
RobotoCondensed-Regular
RobotoSlab-Bold
RobotoSlab-Regular
ROCCB___
ROCC____
ROCK
ROCKB
ROCKBI
ROCKEB
ROCKI
roman
s8514fix
s8514oem
s8514sys
SCHLBKB
SCHLBKBI
SCHLBKI
script
SCRIPTBL
segmdl2
segoepr
segoeprb
segoesc
segoescb
segoeui
segoeuib
segoeuii
segoeuil
segoeuisl
segoeuiz
SegoeWP-Black
SegoeWP-Bold
SegoeWP-Light
SegoeWP-Semibold
SegoeWP-Semilight
SegoeWP
seguibl
seguibli
seguiemj
seguihis
seguili
seguisb
seguisbi
seguisli
seguisym
sere1255
sere1256
sere1257
serf1255
serf1256
serf1257
serife
serifee
serifeg
serifer
serifet
seriff
seriffe
seriffg
seriffr
serifft
SHOWG
simfang
simhei
simkai
SIMLI
simsun
simsunb
SIMYOU
Sitka
SitkaB
SitkaI
SitkaZ
smae1255
smae1256
smae1257
smaf1255
smaf1256
smaf1257
smalle
smallee
smalleg
smaller
smallet
smallf
smallfe
smallfg
smallfr
smallft
SNAP____
ssee1255
ssee1256
ssee1257
ssee874
ssef1255
ssef1256
ssef1257
ssef874
sserife
sserifee
sserifeg
sserifer
sserifet
sseriff
sseriffe
sseriffg
sseriffr
sserifft
StaticCache
STCAIYUN
STENCIL
STFANGSO
STHUPO
STKAITI
STLITI
STSONG
STXIHEI
STXINGKA
STXINWEI
STZHONGS
svgafix
svgasys
sylfaen
Symbol Tiger Expert
Symbol Tiger
symbol
tahoma
tahomabd
taile
taileb
TCBI____
TCB_____
TCCB____
TCCEB
TCCM____
TCMI____
TCM_____
TEMPSITC
Tiger Expert
Tiger
times
timesbd
timesbi
timesi
trebuc
trebucbd
trebucbi
trebucit
verdana
verdanab
verdanai
verdanaz
vga737
vga775
vga850
vga852
vga855
vga857
vga860
vga861
vga863
vga865
vga866
vga869
vga932
vga936
vga949
vga950
vgaf1255
vgaf1256
vgaf1257
vgaf874
vgafix
vgafixe
vgafixg
vgafixr
vgafixt
vgaoem
vgas1255
vgas1256
vgas1257
vgas874
vgasys
vgasyse
vgasysg
vgasysr
vgasyst
VINERITC
VIVALDII
VLADIMIR
webdings
wingding
WINGDNG2
WINGDNG3
YuGothB
YuGothic-Bold
YuGothic
YuGothL
YuGothM
YuGothR
我字酷無非字體
我字酷無非字體_0
我字酷無非字體_1
字體數量:612
程序運行結果

系統字體名字都用英文字母表示(不含中文)。自定義字體安裝到操做系統中根據實際取名狀況而定。

比較兩種方式的區別 

GraphicsEnvironment類 實現對字體(常規、粗細、斜體等)封裝,即粗略分類 讀取系統字體Java推薦
核心類庫文件操做API 讀取全部字體文件,即詳細分類(包含.fon字體文件) 讀取系統字體時不推薦

使用系統字體在屏幕顯示 

package com.forget406.study;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class CustomeFontReading extends JFrame {

    public CustomeFontReading() {
        setTitle("自定義字體使用");
        setSize(300, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);    
        MyPanel panel = new MyPanel();
        add(panel);
    }
    
    public static void main(String[] args) {
        new CustomeFontReading();        
    }
}

class MyPanel extends JPanel {
    
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.setFont(new Font("楷體", Font.BOLD, 50));
        g.drawString("和諧社會", 30, 200);
    }
}

程序運行結果

將自定義字體導入系統字體庫,也可以經過這種方法使用自定義字體。

引入外部自定義字體

字體下載網站傳送門 

 【分享】全球最大的字體下載網站:www.fontke.com(字客網)

建立、註冊外部字體 

以本身項目中使用過的字體文件Deeko Comic Regular.ttf爲例,加以說明。如下是此字體對應的參數說明。

在使用外部字體時,必須先建立字體,而後在操做系統中註冊外部字體;不然沒法使用外部字體。

package com.forget406.study;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class CustomeFontReading extends JFrame {
    
    private static final long serialVersionUID = 270583473364792225L;

    public CustomeFontReading() {
        setTitle("自定義字體使用");
        setSize(300, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);    
        MyPanel panel = new MyPanel();
        add(panel);
    }
    
    public static void main(String[] args) {
        new CustomeFontReading();   
    }
}

class MyPanel extends JPanel {
    
    private static final long serialVersionUID = 4897720234933257563L;
    
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        registerCustomeFont("Deeko Comic Regular.ttf");
     // 畫筆使用自定義字體(字體風格、尺寸可與建立時參數不一樣) g.setFont(
new Font("Deeko Comic Regular", Font.BOLD, 30)); g.drawString("HARMONY", 30, 200); } private void registerCustomeFont(String filename) { File fontFile = new File( "font"+File.separator+filename); System.out.println(fontFile.exists()); Font font = null; try {
       // 建立自定義字體 font
= Font.createFont( Font.TRUETYPE_FONT, fontFile); // 字體.ttf格式 // 設置字體風格、尺寸
       font.deriveFont(Font.BOLD,30F); GraphicsEnvironment ge
= // 得到本地圖形環境對象 GraphicsEnvironment .getLocalGraphicsEnvironment();
       // 在本地圖形環境中註冊當前字體
boolean register = ge.registerFont(font); if(register) { System.out.println("註冊字體成功"); } else { System.out.println("註冊字體失敗"); } System.out.println("外部字體建立成功"); } catch (FontFormatException e) { e.printStackTrace(); System.out.println("字體格式化異常"); } catch (IOException e) { e.printStackTrace(); System.out.println("讀寫字體文件異常"); } } }

程序測試結果

 

除了使用File,還可以使用InputStream。但就性能而言,推薦使用File。

相關文章
相關標籤/搜索