Tuesday, October 8, 2019

The Plain Vanilla custom font loader


Now I’ll test to see if showtext package would be able to load custom fonts, and draw the wordcloud for which I couldn’t import custom texts with the extrafont package.
Again, I am using the Padauk Google font, and two ttf fonts installed on my laptop as it was done for my last post.
library(showtext)
font_add_google(name = "Padauk", family = "pa-dauk")
font_add("pyi", regular = "Pyidaungsu-1.8.3_Regular.ttf")
font_add("mya-3", regular = "Myanmar3_0.ttf")

library(quanteda)
# using Google Padauk font
png(filename = "WCloud_Gpadauk.png")
set.seed(2307)
#windows()
showtext_begin()
textplot_wordcloud(x100_iwpN2.dfm, family = "pa-dauk", min_size = .6, max_size = 6, min_count = 30, color = RColorBrewer::brewer.pal(8, "Dark2"))
showtext_end()
dev.off()

# using Pyidaungsu font
png(filename = "WCloud_pyidaungsu.png")
set.seed(2307)
showtext_begin()
textplot_wordcloud(x100_iwpN2.dfm, family = "pyi", min_size = .6, max_size = 6, min_count = 30, color = RColorBrewer::brewer.pal(8, "Dark2"))
showtext_end()
dev.off()

# using Myanmar3 font
png(filename = "WCloud_myanmar3.png")
set.seed(2307)
showtext_begin()
textplot_wordcloud(x100_iwpN2.dfm, family = "mya-3", min_size = .6, max_size = 6, min_count = 30, color = RColorBrewer::brewer.pal(8, "Dark2"))
showtext_end()
dev.off()
That works, but rendering of text in all three cases is still faulty.
This is with showtext and Google Padauk font:
With showtext and Pyidaungsu font:
With showtext and Myanmar3 font:
When I was feeling hopeless and was about to give up I thought of reading Yixuan Qiu’s article for one more time. This time more closely. And this is the paragraph I’d missed:
However, when Cairo graphics is not available, it will require more effort to customize the font. For PNG graphs, the user needs to first register a font family name in R which is mapped to a font that is installed in the system, and then specify the font family name in plotting functions.
That saved me at last:
windowsFonts("pyi-r" = "Pyidaungsu")
set.seed(2307)
textplot_wordcloud(x100_iwpN2.dfm, family = "pyi-r", min_size = .9, max_size = 13, min_count = 30, color = RColorBrewer::brewer.pal(8, "Dark2"))
Notice also that this approach works with RstudioGD (Rstudio graphic device).
Here at last is the syllable co-occurrence network plot we want!
So simple, shall I call it Plain Vanilla font loader?
windowsFonts("pyi-b" = "Pyidaungsu Bold")
size <- log(colSums(dfm_select(syll20k.dfm, feat)))
set.seed(93019)
textplot_network(syll20k.fcm_select, min_freq = 0.8, vertex_size = size / max(size) * 3, vertex_labelfont = "pyi-b")

No comments:

Post a Comment