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