社会人から始めたプログラミング

プログラミング、エンジニアに関することでの備忘録、シェアしたい情報などを共有するためのブログです。

Dockerfile で fontconfig の設定 明朝 ゴシックのフォント設定

目的:Dockerfile で fontconfig の設定を行い、MS明朝はIPA明朝、MSゴシックはIPAゴシックが呼ばれるようにする。

fontの読み込まれる優先順位を調整する。

【準備】

1) fontconfig を docker にインストール MS明朝はIPA明朝、MSゴシックはIPAゴシックが呼ばれるようにするために、 そもそもfontconfigを入れないと、設定されなかったので、 Dockerfileに下記を記述

RUN apt-get update -qq \
 && apt-get install -y \
 fontconfig

2) 指定したいフォントを入れておく 今回はipaフォントを入れている。 Dockerfile に下記を記述

RUN apt-get install -y fonts-ipafont
RUN fc-cache -fv
【fonts.conf の設定】

1)コピー MS明朝はIPA明朝、MSゴシックはIPAゴシックが呼ばれるようにするための記述を fonts.confというファイルに用意。 Dockerfileと同じ階層に準備。 Dockerの中にもfonts.confが設定されるように、 用意したfonts.confをDockerの中に、下記の記述でコピー。

COPY fonts.conf /etc/fonts/local.conf

*注意点 /etc/fonts/fonts.conf にコピーしちゃうとデフォルトの物が消えてしまって、 どこからフォント持ってくるかという表記とかないので、 /etc/fonts/local.conf におく

/etc/fonts/local.conf におけば、デフォルトの /etc/fonts/fonts.conf プラスアルファで /etc/fonts/local.confを使える。

具体的な記述内容。

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>
  <!-- MS 明朝 -->
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS 明朝</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA明朝</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS 明朝</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA明朝</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS Mincho</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA明朝</string>
    </edit>
  </match>
  <!-- MS P明朝 -->
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS P明朝</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA P明朝</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS P明朝</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA P明朝</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS PMincho</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA P明朝</string>
    </edit>
  </match>
  <!-- MS ゴシック -->
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS ゴシック</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPAゴシック</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS ゴシック</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPAゴシック</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS Gothic</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPAゴシック</string>
    </edit>
  </match>
  <!-- MS Pゴシック -->
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS Pゴシック</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA Pゴシック</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS Pゴシック</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA Pゴシック</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>MS PGothic</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>IPA Pゴシック</string>
    </edit>
  </match>
</fontconfig>

*本当はtestの中身の条件を複数書いて、「または」的な条件でスマートに記述したかったが、 ちょっとわからなかったので、今みたいな形になった。 local.confが適応されたかどうかは、 fc-cache したあとに、 fc-match "MS 明朝" など、確かめたいフォントを指定すると◎