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