Skip to content

Commit

Permalink
Add support for per-locale fonts.
Browse files Browse the repository at this point in the history
This became necessary because wqy-microhei.ttf doesn't
support the Romanian "ț" character.
  • Loading branch information
jfly committed Jan 11, 2018
1 parent 358ad19 commit 760e466
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Binary file added fonts/NotoSans-Regular.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions lib/libraries
Expand Up @@ -44,4 +44,5 @@ requests: https://github.com/kennethreitz/requests

**** Fonts ****
fonts/LiberationMono-Regular.ttf: https://github.com/thewca/tnoodle/pull/223
fonts/NotoSans-Regular.ttf: https://github.com/googlei18n/noto-fonts
fonts/wqy-microhei.ttf: https://github.com/thewca/tnoodle/pull/246#issuecomment-349533724
Expand Up @@ -99,18 +99,30 @@ class ScrambleRequest {

private static final char NON_BREAKING_SPACE = '\u00A0';

private static BaseFont monoFont, sansSerifFont;
private static BaseFont monoFont, notoSans;
private static HashMap<Locale, BaseFont> FONT_BY_LOCALE = new HashMap<Locale, BaseFont>();
static {
try {
monoFont = BaseFont.createFont("fonts/LiberationMono-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
sansSerifFont = BaseFont.createFont("fonts/wqy-microhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

BaseFont cjk = BaseFont.createFont("fonts/wqy-microhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FONT_BY_LOCALE.put(Locale.forLanguageTag("zh-CN"), cjk);
FONT_BY_LOCALE.put(Locale.forLanguageTag("zh-TW"), cjk);
FONT_BY_LOCALE.put(Locale.forLanguageTag("ko"), cjk);
FONT_BY_LOCALE.put(Locale.forLanguageTag("ja"), cjk);

notoSans = BaseFont.createFont("fonts/NotoSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (DocumentException e) {
l.log(Level.INFO, "", e);
} catch (IOException e) {
l.log(Level.INFO, "", e);
}
}

private static BaseFont getFontForLocale(Locale locale) {
return FONT_BY_LOCALE.getOrDefault(locale, notoSans);
}

private static HashMap<String, ScrambleCacher> scrambleCachers = new HashMap<String, ScrambleCacher>();
private static SortedMap<String, LazyInstantiator<Puzzle>> puzzles;
static {
Expand Down Expand Up @@ -442,7 +454,7 @@ private static void addFmcSolutionSheet(PdfWriter docWriter, Document doc, Scram
}
PdfContentByte cb = docWriter.getDirectContent();
float LINE_THICKNESS = 0.5f;
BaseFont bf = sansSerifFont;
BaseFont bf = getFontForLocale(locale);

int bottom = 30;
int left = 35;
Expand Down Expand Up @@ -715,7 +727,7 @@ private static void addFmcScrambleCutoutSheet(PdfWriter docWriter, Document doc,
String scramble = scrambleRequest.scrambles[index];
PdfContentByte cb = docWriter.getDirectContent();

BaseFont bf = sansSerifFont;
BaseFont bf = getFontForLocale(Translate.DEFAULT_LOCALE);

int bottom = 30;
int left = 35;
Expand Down

0 comments on commit 760e466

Please sign in to comment.