-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guo Yunhe
committed
Sep 6, 2016
1 parent
97e3da3
commit f5027ce
Showing
1 changed file
with
4 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,84 +17,21 @@ | |
package me.guoyunhe.fontweak; | ||
|
||
import java.awt.GraphicsEnvironment; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* @author Guo Yunhe <[email protected]> | ||
*/ | ||
public class SystemFontList { | ||
|
||
private final GraphicsEnvironment env; | ||
|
||
private List<String> list; | ||
private List<String> ignoreList; | ||
private final String[] list; | ||
|
||
public SystemFontList() { | ||
env = GraphicsEnvironment.getLocalGraphicsEnvironment(); | ||
buildIgnoreList(); | ||
updateList(); | ||
} | ||
|
||
private void updateList() { | ||
// Return English names of fonts, easier for match | ||
String[] originalList = env.getAvailableFontFamilyNames(); | ||
list = new ArrayList(); | ||
for (String font : originalList) { | ||
font = removeFontWeight(font); | ||
if (!isIgnored(font) && !list.contains(font)) { | ||
list.add(font); | ||
} | ||
} | ||
} | ||
|
||
private String removeFontWeight(String font) { | ||
String fi = font.toLowerCase(); | ||
String[] weights = { | ||
" demilight", | ||
" light", | ||
" thin", | ||
" normal", | ||
" regular", | ||
" medium", | ||
" semibold", | ||
" bold", | ||
" extrabold", | ||
" black" | ||
}; | ||
for(String weight: weights) { | ||
if(fi.endsWith(weight)) { | ||
font = font.substring(0, font.length() - weight.length()); | ||
} | ||
} | ||
return font; | ||
} | ||
|
||
private void buildIgnoreList() { | ||
ignoreList = new ArrayList(); | ||
ignoreList.add("Sans Serif"); | ||
ignoreList.add("SansSerif"); | ||
ignoreList.add("Serif"); | ||
ignoreList.add("Monospace"); | ||
ignoreList.add("Monospaced"); | ||
} | ||
|
||
private boolean isIgnored(String font) { | ||
return ignoreList.contains(font); | ||
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); | ||
list = env.getAvailableFontFamilyNames(); | ||
} | ||
|
||
public String[] get() { | ||
String[] array = new String[list.size()]; | ||
list.toArray(array); | ||
return array; | ||
} | ||
|
||
public void refresh() { | ||
updateList(); | ||
} | ||
|
||
public boolean contains(String font) { | ||
return list.contains(font); | ||
return list; | ||
} | ||
} |