LoginSignup
6
6

More than 5 years have passed since last update.

nodejsの標準出力でttfフォントをcssに変換する

Last updated at Posted at 2015-02-02

baseuri使ってますか?小さいファイルは、datauriを使って、どんどんcssにしまっちゃいましょう

お手持ちのターミナルか、cmderを起動して以下を入力します。

出力

$ node -p '(new Buffer("hoge")).toString("base64")'
# aG9nZQ==

$ node -p '(new Buffer(((new Buffer("hoge")).toString("base64")),"base64").toString("utf8"))'
# hoge

はい、ワンライナーですね。

保存

$ node -p '(new Buffer("hoge")).toString("base64")' > hogekosan.txt
$ more hogekosan.txt
# aG9nZQ==

$ node -p "(new Buffer(require('fs').readFileSync('./hogekosan.txt').toString(),'base64').toString())"
# hoge

標準出力なので、最後に> filenameを足すと、出力を保存できます。

加工

$ node -p '"data:text/plain;base64,"+(new Buffer("hoge")).toString("base64")'
# data:text/plain;base64,aG9nZQ==

文字連結でdatauriスキーマに加工できますね。

data:text/plain;base64,aG9nZQ==

をコピペしてブラウザで開けば、テキストファイルとして表示できます。

nodejsの標準出力でttfフォントをcssに変換する

さいきん美咲フォントの作者が、k8x12というフォントを公開されたので、これをcssにdatauriとして埋め込んで、htmlで表示します。

k8x12.htmlイメージ
<style>
@font-face{
  font-family:"k8x12";
  src:url(ここにブチ込む) format('truetype');
}
h1{
  font-family:"k8x12";
}
</style>
<h1>やったぜ。</h1>

公式サイトでttfのzipをダウンロードして、その中のk8x12.ttfを変換します

node -p "'<style>@font-face{font-family:\"k8x12\";src:url(data:application/x-font-ttf;base64,'+require('fs').readFileSync('./k8x12.ttf').toString('base64')+')format(\"truetype\");}h1{font-family:\"k8x12\";}</style><h1>やったぜ。</h1>'" > k8x12.html

k8x12.html

スクリーンショット 2015-02-02 11.01.05 AM.png

やったぜ。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6