首页 > 解决方案 > 使用 gdtools::font_family_exists 检测系统字体的问题

问题描述

ggiraph我想通过将字体更改为“DejaVu Sans Condensed”来绘制一个交互式 html 图。这种特殊字体与我将其放入 css 时的交互式工具提示效果很好girafe,但不能作为 html 绘图本身中文本元素的通用字体。让我们尝试一个例子。首先,使用 ggplot2 一切正常:

library(tidyverse)
library(ggiraph)
windowsFonts(sans = windowsFont("DejaVu Sans Condensed")) 
z <- ggplot(data = iris, 
            mapping = aes(x = Sepal.Length, y = Petal.Width)) +
  geom_point() +
  ggtitle("Title with special characters ♀♂&quot;) +
  xlab("Sepal.Length ♀♂&quot;) + ylab("Miles per Gallon ♀♂&quot; ) + 
    theme_minimal(base_family = "sans", base_size = 18)
z

在此处输入图像描述

我们可以看到 DejaVu Sans Condensed 字体起作用了,因为标题中的男性和女性符号与字母一样粗体。但是使用girafe,符号显得更薄,因为字体不起作用/回到 Arial :

girafe(ggobj = z, fonts = list(sans = "DejaVu Sans Condensed"))

在此处输入图像描述

问题可能来自用于管理字体的gdtoolsggiraph,因为它找不到 DejaVu Sans Condensed :

gdtools::font_family_exists("DejaVu Sans Condensed")
# [1] FALSE

但是,如果我们使用gdtools::sys_fonts,我们可以找到 DejaVu Sans Condensed,但字体系列名称不完整:

gdtools::sys_fonts() %>% filter(str_detect(name, "DejaVuSansCondensed")) %>% select(3:5)

# A tibble: 4 x 3
# name                              family         style                 
# DejaVuSansCondensed-Bold          DejaVu Sans    Condensed Bold        
# DejaVuSansCondensed-BoldOblique   DejaVu Sans    Condensed Bold Oblique
# DejaVuSansCondensed-Oblique       DejaVu Sans    Condensed Oblique     
# DejaVuSansCondensed               DejaVu Sans    Condensed    

“DejaVu Sans Condensed”存在,但“family”名称只是“Deja Vu Sans”,而“Condensed”部分转到“style”(在我电脑中这种字体的资料中,使用 Windows,字体系列名称是“DejaVu Sans Condensed”)。

是系统字体检测算法有问题吗?或者它是一种正常的行为,然后在哪里指定 Condensed 样式来使用它ggiraph

谢谢,

标签: rfontsfont-familyggiraph

解决方案


推荐阅读