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