render.rb

  1#!/bin/env ruby
  2
  3require 'xml'
  4
  5resolutions = {
  6	'mdpi' => 1,
  7	'hdpi' => 1.5,
  8	'xhdpi' => 2,
  9	'xxhdpi' => 3,
 10	'xxxhdpi' => 4,
 11	}
 12
 13images = {
 14	'conversations_baloon.svg' => ['ic_launcher', 48],
 15	'conversations_mono.svg' => ['ic_notification', 24],
 16	'ic_received_indicator.svg' => ['ic_received_indicator', 12],
 17	'ic_send_text_offline.svg' => ['ic_send_text_offline', 36],
 18	'ic_send_text_online.svg' => ['ic_send_text_online', 36],
 19	'ic_send_text_away.svg' => ['ic_send_text_away', 36],
 20	'ic_send_text_dnd.svg' => ['ic_send_text_dnd', 36],
 21	'ic_send_photo_online.svg' => ['ic_send_photo_online', 36],
 22	'ic_send_photo_offline.svg' => ['ic_send_photo_offline', 36],
 23	'ic_send_photo_away.svg' => ['ic_send_photo_away', 36],
 24	'ic_send_photo_dnd.svg' => ['ic_send_photo_dnd', 36],
 25	'ic_send_location_online.svg' => ['ic_send_location_online', 36],
 26	'ic_send_location_offline.svg' => ['ic_send_location_offline', 36],
 27	'ic_send_location_away.svg' => ['ic_send_location_away', 36],
 28	'ic_send_location_dnd.svg' => ['ic_send_location_dnd', 36],
 29	'ic_send_voice_online.svg' => ['ic_send_voice_online', 36],
 30	'ic_send_voice_offline.svg' => ['ic_send_voice_offline', 36],
 31	'ic_send_voice_away.svg' => ['ic_send_voice_away', 36],
 32	'ic_send_voice_dnd.svg' => ['ic_send_voice_dnd', 36],
 33	'ic_send_cancel_online.svg' => ['ic_send_cancel_online', 36],
 34	'ic_send_cancel_offline.svg' => ['ic_send_cancel_offline', 36],
 35	'ic_send_cancel_away.svg' => ['ic_send_cancel_away', 36],
 36	'ic_send_cancel_dnd.svg' => ['ic_send_cancel_dnd', 36],
 37	'ic_send_picture_online.svg' => ['ic_send_picture_online', 36],
 38	'ic_send_picture_offline.svg' => ['ic_send_picture_offline', 36],
 39	'ic_send_picture_away.svg' => ['ic_send_picture_away', 36],
 40	'ic_send_picture_dnd.svg' => ['ic_send_picture_dnd', 36],
 41	'md_switch_thumb_disable.svg' => ['switch_thumb_disable', 48],
 42	'md_switch_thumb_off_normal.svg' => ['switch_thumb_off_normal', 48],
 43	'md_switch_thumb_off_pressed.svg' => ['switch_thumb_off_pressed', 48],
 44	'md_switch_thumb_on_normal.svg' => ['switch_thumb_on_normal', 48],
 45	'md_switch_thumb_on_pressed.svg' => ['switch_thumb_on_pressed', 48],
 46	'message_bubble_received.svg' => ['message_bubble_received.9', 0],
 47	'message_bubble_sent.svg' => ['message_bubble_sent.9', 0],
 48	}
 49
 50# Executable paths for Mac OSX
 51# "/Applications/Inkscape.app/Contents/Resources/bin/inkscape"
 52
 53inkscape = "inkscape"
 54imagemagick = "convert"
 55
 56def execute_cmd(cmd)
 57	puts cmd
 58	system cmd
 59end
 60
 61images.each do |source_filename, settings|
 62	svg_content = File.read(source_filename)
 63
 64	svg = XML::Document.string(svg_content)
 65	base_width = svg.root["width"].to_i
 66	base_height = svg.root["height"].to_i
 67
 68	guides = svg.find(".//sodipodi:guide")
 69
 70	resolutions.each do |resolution, factor|
 71		output_filename, base_size = settings
 72
 73		if base_size > 0
 74			width = factor * base_size
 75			height = factor * base_size
 76		else
 77			width = factor * base_width
 78			height = factor * base_height
 79		end
 80
 81		path = "../src/main/res/drawable-#{resolution}/#{output_filename}.png"
 82		execute_cmd "#{inkscape} -f #{source_filename} -z -C -w #{width} -h #{height} -e #{path}"
 83
 84		top = []
 85		right = []
 86		bottom = []
 87		left = []
 88
 89		guides.each do |guide|
 90			orientation = guide["orientation"]
 91			x, y = guide["position"].split(",")
 92			x, y = x.to_i, y.to_i
 93
 94			if orientation == "1,0" and y == base_height
 95				top.push(x * factor)
 96			end
 97
 98			if orientation == "0,1" and x == base_width
 99				right.push((base_height - y) * factor)
100			end
101
102			if orientation == "1,0" and y == 0
103				bottom.push(x * factor)
104			end
105
106			if orientation == "0,1" and x == 0
107				left.push((base_height - y) * factor)
108			end
109		end
110
111		next if top.length != 2
112		next if right.length != 2
113		next if bottom.length != 2
114		next if left.length != 2
115
116		execute_cmd "#{imagemagick} -background none PNG32:#{path} -gravity center -extent #{width+2}x#{height+2} PNG32:#{path}"
117
118		draw_format = "-draw \"rectangle %d,%d %d,%d\""
119		top_line = draw_format % [top.min + 1, 0, top.max, 0]
120		right_line = draw_format % [width + 1, right.min + 1, width + 1, right.max]
121		bottom_line = draw_format % [bottom.min + 1, height + 1, bottom.max, height + 1]
122		left_line = draw_format % [0, left.min + 1, 0, left.max]
123		draws = "#{top_line} #{right_line} #{bottom_line} #{left_line}"
124
125		execute_cmd "#{imagemagick} -background none PNG32:#{path} -fill black -stroke none #{draws} PNG32:#{path}"
126	end
127end