reformat ruby render script

This commit is contained in:
Daniel Gultsch 2022-12-26 15:21:18 +01:00
parent eb51b03d1a
commit a157d6796a
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -1,4 +1,5 @@
#!/bin/env ruby #!/bin/env ruby
# frozen_string_literal: true
require 'xml' require 'xml'
@ -7,7 +8,7 @@ resolutions = {
'hdpi' => 1.5, 'hdpi' => 1.5,
'xhdpi' => 2, 'xhdpi' => 2,
'xxhdpi' => 3, 'xxhdpi' => 3,
'xxxhdpi' => 4, 'xxxhdpi' => 4
} }
images = { images = {
@ -28,7 +29,6 @@ images = {
'conversations_mono.svg' => ['conversations/ic_notification', 24], 'conversations_mono.svg' => ['conversations/ic_notification', 24],
'quicksy_mono.svg' => ['quicksy/ic_notification', 24], 'quicksy_mono.svg' => ['quicksy/ic_notification', 24],
'flip_camera_android-black-24dp.svg' => ['ic_flip_camera_android_black_24dp', 24], 'flip_camera_android-black-24dp.svg' => ['ic_flip_camera_android_black_24dp', 24],
'ic_missed_call_notification.svg' => ['ic_missed_call_notification', 24],
'ic_send_text_offline.svg' => ['ic_send_text_offline', 36], 'ic_send_text_offline.svg' => ['ic_send_text_offline', 36],
'ic_send_text_offline_white.svg' => ['ic_send_text_offline_white', 36], 'ic_send_text_offline_white.svg' => ['ic_send_text_offline_white', 36],
'ic_send_text_online.svg' => ['ic_send_text_online', 36], 'ic_send_text_online.svg' => ['ic_send_text_online', 36],
@ -82,11 +82,8 @@ images = {
'marker.svg' => ['marker', 0] 'marker.svg' => ['marker', 0]
} }
# Executable paths for Mac OSX inkscape = 'inkscape'
# "/Applications/Inkscape.app/Contents/Resources/bin/inkscape" imagemagick = 'convert'
inkscape = "inkscape"
imagemagick = "magick"
def execute_cmd(cmd) def execute_cmd(cmd)
puts cmd puts cmd
@ -95,17 +92,16 @@ end
images.each do |source_filename, settings| images.each do |source_filename, settings|
svg_content = File.read(source_filename) svg_content = File.read(source_filename)
svg = XML::Document.string(svg_content)
base_width = svg.root["width"].to_i
base_height = svg.root["height"].to_i
guides = svg.find(".//sodipodi:guide","sodipodi:http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd")
resolutions.each do |resolution, factor|
output_filename, base_size = settings output_filename, base_size = settings
if base_size > 0 svg = XML::Document.string(svg_content)
base_width = svg.root['width'].to_i
base_height = svg.root['height'].to_i
guides = svg.find('.//sodipodi:guide', 'sodipodi:http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd')
resolutions.each do |resolution, factor|
if base_size.positive?
width = factor * base_size width = factor * base_size
height = factor * base_size height = factor * base_size
else else
@ -115,12 +111,12 @@ images.each do |source_filename, settings|
output_parts = output_filename.split('/') output_parts = output_filename.split('/')
if output_parts.count != 2 path = if output_parts.count != 2
path = "../src/main/res/drawable-#{resolution}/#{output_filename}.png" "../src/main/res/drawable-#{resolution}/#{output_filename}.png"
else else
path = "../src/#{output_parts[0]}/res/drawable-#{resolution}/#{output_parts[1]}.png" "../src/#{output_parts[0]}/res/drawable-#{resolution}/#{output_parts[1]}.png"
end end
execute_cmd "#{inkscape} #{source_filename} -C -w #{width} -h #{height} -e #{path}" execute_cmd "#{inkscape} #{source_filename} -C -w #{width.to_i} -h #{height.to_i} --export-filename=#{path}"
top = [] top = []
right = [] right = []
@ -128,25 +124,18 @@ images.each do |source_filename, settings|
left = [] left = []
guides.each do |guide| guides.each do |guide|
orientation = guide["orientation"] orientation = guide['orientation']
x, y = guide["position"].split(",") x, y = guide['position'].split(',')
x, y = x.to_i, y.to_i x = x.to_i
y = y.to_i
if orientation == "1,0" and y == base_height top.push(x * factor) if (orientation == '1,0') && (y == base_height)
top.push(x * factor)
end
if orientation == "0,1" and x == base_width right.push((base_height - y) * factor) if (orientation == '0,1') && (x == base_width)
right.push((base_height - y) * factor)
end
if orientation == "1,0" and y == 0 bottom.push(x * factor) if (orientation == '1,0') && y.zero?
bottom.push(x * factor)
end
if orientation == "0,1" and x == 0 left.push((base_height - y) * factor) if (orientation == '0,1') && x.zero?
left.push((base_height - y) * factor)
end
end end
next if top.length != 2 next if top.length != 2
@ -156,11 +145,11 @@ images.each do |source_filename, settings|
execute_cmd "#{imagemagick} -background none PNG32:#{path} -gravity center -extent #{width + 2}x#{height + 2} PNG32:#{path}" execute_cmd "#{imagemagick} -background none PNG32:#{path} -gravity center -extent #{width + 2}x#{height + 2} PNG32:#{path}"
draw_format = "-draw \"line %d,%d %d,%d\"" draw_format = '-draw "line %d,%d %d,%d"'
top_line = draw_format % [top.min + 1, 0, top.max, 0] top_line = format(draw_format, top.min + 1, 0, top.max, 0)
right_line = draw_format % [width + 1, right.min + 1, width + 1, right.max] right_line = format(draw_format, width + 1, right.min + 1, width + 1, right.max)
bottom_line = draw_format % [bottom.min + 1, height + 1, bottom.max, height + 1] bottom_line = format(draw_format, bottom.min + 1, height + 1, bottom.max, height + 1)
left_line = draw_format % [0, left.min + 1, 0, left.max] left_line = format(draw_format, 0, left.min + 1, 0, left.max)
draws = "#{top_line} #{right_line} #{bottom_line} #{left_line}" draws = "#{top_line} #{right_line} #{bottom_line} #{left_line}"
execute_cmd "#{imagemagick} -background none PNG32:#{path} -fill black -stroke none #{draws} PNG32:#{path}" execute_cmd "#{imagemagick} -background none PNG32:#{path} -fill black -stroke none #{draws} PNG32:#{path}"