0
0

More than 5 years have passed since last update.

SVG画像を3x3で並べる

Last updated at Posted at 2019-06-27

原子構造をベクター画像で描くツールを作りました。
https://github.com/NuNoKehai/xyzb2image
が、このプログラムだと1ユニットセル分しか描かないので、そのままでは見た目がイマイチ。
NFB.png
もともとイラストレータで3x3に並べていましたが、イラストレーターって画像の位置合わせがイマイチ苦手ですね。だからと言って描画ユニットセルを増やすオプションはつけたくない。
と言うことで、ベクトル画像を3x3するrubyスクリプトを書きました。

svg3x3.rb
require 'rubygems'
require 'cairo'
require 'rsvg2'

def read_suffix(filename)

  index_period=filename.rindex(".")
  suffix=filename[index_period+1..filename.size-1]

  return suffix

end

def cairo_imagesurface_general(filename_image, width, height)

  suffix_image=read_suffix(filename_image)

  if suffix_image.casecmp("pdf")==0
     surface = Cairo::PDFSurface.new(filename_image, width, height)
  elsif suffix_image.casecmp("svg")==0
    surface = Cairo::SVGSurface.new(filename_image, width, height)
  else
    format = Cairo::FORMAT_ARGB32
    surface = Cairo::ImageSurface.new(format, width, height)
  end

  return surface

end

def context_finish_general(filename_image,context)

  suffix_image=read_suffix(filename_image)

  if suffix_image.casecmp("pdf")==0 || suffix_image.casecmp("svg")==0
    context.target.finish
  elsif suffix_image.casecmp("png")==0
    context.target.write_to_png(filename_image)
  end

end

filename_in=ARGV[0]
filename_out=ARGV[1]

handle_in = RSVG::Handle.new_from_file(filename_in)
width_in=handle_in.dimensions.width
height_in=handle_in.dimensions.height

surface_out = cairo_imagesurface_general(filename_out, width_in*3, height_in*3)
context_out = Cairo::Context.new(surface_out)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_out.translate(-2*width_in,height_in)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_out.translate(-2*width_in,height_in)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_out.translate(width_in,0)
context_out.render_rsvg_handle(handle_in)
context_finish_general(filename_out,context_out)

このスクリプトを動作させるためには rubygem の cairo と rsvg2 が必要です。
最初に定義した3つの関数は出力ファイルの拡張子の汎用性を増やすためです。
やってることはユーザー空間の原点をずらして入力画像を描画してるだけ。

使い方です。
ruby svg3x3.rb input.svg output.pdf
のように、入力ファイル名と出力ファイル名をargumentで指定。
入力ファイルはsvg形式である必要があります。

結果。
NFB_3x3.png
できました。
後は、画像出力的にいらない部分をイラストレーターのクリッピングマスク等で切り取ってしまえばOK。

0
0
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
0
0