Skip to content

Instantly share code, notes, and snippets.

@cxx
Created February 23, 2014 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxx/9168365 to your computer and use it in GitHub Desktop.
Save cxx/9168365 to your computer and use it in GitHub Desktop.
フォントのweight値を書き換える
#!/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