lib/frameit/editor.rb in frameit-2.1.0 vs lib/frameit/editor.rb in frameit-2.2.0
- old
+ new
@@ -124,12 +124,17 @@
def put_device_into_background(background)
left_space = (background.width / 2.0 - image.width / 2.0).round
bottom_space = -(image.height / 10).round # to be just a bit below the image bottom
bottom_space -= 40 if screenshot.is_portrait? # even more for portrait mode
- bottom_space -= 50 if (screenshot.is_mini? and screenshot.is_portrait?) # super old devices
+ if screenshot.is_mini?
+ # Such small devices need special treatment
+ bottom_space -= 50 if screenshot.is_portrait?
+ bottom_space += 65 unless screenshot.is_portrait?
+ end
+
self.top_space_above_device = background.height - image.height - bottom_space
@image = background.composite(image, "png") do |c|
c.compose "Over"
c.geometry "+#{left_space}+#{top_space_above_device}"
@@ -150,22 +155,43 @@
def add_title
title_images = build_title_images(image.width)
keyword = title_images[:keyword]
title = title_images[:title]
- sum_width = (keyword.width rescue 0) + title.width + keyword_padding
- top_space = (top_space_above_device / 2.0 - actual_font_size / 2.0).round # centered
-
+ # sum_width: the width of both labels together including the space inbetween
+ # is used to calculate the ratio
+ sum_width = title.width
+ sum_width += keyword.width + keyword_padding if keyword
+
+ # Resize the 2 labels if necessary
+ smaller = 1.0 # default
+ ratio = (sum_width + keyword_padding * 2) / image.width.to_f
+ if ratio > 1.0
+ # too large - resizing now
+ smaller = (1.0 / ratio)
+
+ Helper.log.debug "Text for image #{self.screenshot.path} is quite long, reducing font size by #{(ratio - 1.0).round(2)}" if $verbose
+
+ title.resize"#{(smaller * title.width).round}x"
+ keyword.resize"#{(smaller * keyword.width).round}x" if keyword
+ sum_width *= smaller
+ end
+
+ top_space = (top_space_above_device / 2.0 - (actual_font_size / 2.0 * smaller)).round # centered
left_space = (image.width / 2.0 - sum_width / 2.0).round
+
+ # First, put the keyword on top of the screenshot, if we have one
if keyword
@image = image.composite(keyword, "png") do |c|
c.compose "Over"
c.geometry "+#{left_space}+#{top_space}"
end
- end
- left_space += (keyword.width rescue 0) + keyword_padding
+ left_space += keyword.width + (keyword_padding * smaller)
+ end
+
+ # Then, put the title on top of the screenshot next to the keyword
@image = image.composite(title, "png") do |c|
c.compose "Over"
c.geometry "+#{left_space}+#{top_space}"
end
image
@@ -173,10 +199,11 @@
def actual_font_size
[top_space_above_device / 3.0, @image.width / 30.0].max.round
end
+ # The space between the keyword and the title
def keyword_padding
(actual_font_size / 2.0).round
end
# This will build 2 individual images with the title, which will then be added to the real image
@@ -187,15 +214,18 @@
# Create empty background
empty_path = File.join(Helper.gem_path('frameit'), "lib/assets/empty.png")
title_image = MiniMagick::Image.open(empty_path)
image_height = actual_font_size * 2 # gets trimmed afterwards anyway, and on the iPad the `y` would get cut
title_image.combine_options do |i|
- i.resize "#{max_width}x#{image_height}!" # `!` says it should ignore the ratio
+ # * 2.0 as the text might be larger than the actual image. We're trimming afterwards anyway
+ i.resize "#{max_width * 2.0}x#{image_height}!" # `!` says it should ignore the ratio
end
current_font = font(key)
+ text = fetch_text(key)
Helper.log.debug "Using #{current_font} as font the #{key} of #{screenshot.path}" if $verbose and current_font
+ Helper.log.debug "Adding text '#{fetch_text(key)}'"if $verbose
# Add the actual title
title_image.combine_options do |i|
i.font current_font if current_font
i.gravity "Center"
@@ -266,10 +296,10 @@
return font["font"]
end
end
end
- Helper.log.debug "No custom font specified, using the default one" if $verbose
+ Helper.log.debug "No custom font specified for #{screenshot}, using the default one" if $verbose
return nil
end
end
-end
\ No newline at end of file
+end