Created
February 23, 2014 07:43
-
-
Save cxx/9168365 to your computer and use it in GitHub Desktop.
フォントのweight値を書き換える
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
fail unless ARGV.length == 2 | |
file = ARGV[0] | |
weight = ARGV[1].to_i | |
open(file, "r+b:ASCII-8BIT") do |io| | |
ver, ntables, srange, eselector, rshift = io.read(12).unpack("Nn*") | |
fail unless Math.log2(ntables).floor == eselector and | |
(2**eselector)*16 == srange and | |
ntables*16-srange == rshift | |
header_pos = nil | |
ntables.times do | |
if io.read(4) == "OS/2" | |
header_pos = io.pos - 4 | |
break | |
end | |
io.pos += 12 | |
end | |
fail unless header_pos | |
checksum, offset, length = io.read(12).unpack("N*") | |
io.pos = offset + 4 | |
io.write([weight].pack("n")) | |
io.pos = offset | |
sum = 0 | |
(length/4.0).ceil.times { sum += io.read(4).unpack("N")[0] } | |
sum &= 0xffffffff | |
io.pos = header_pos + 4 | |
io.write([sum].pack("N")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment