render.rb 6.7 KB

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