lib/frameit/editor.rb in frameit-2.3.0 vs lib/frameit/editor.rb in frameit-2.4.0

- old
+ new

@@ -90,28 +90,47 @@ if self.frame # we have no frame on le mac resize_frame! @image = put_into_frame # Decrease the size of the framed screenshot to fit into the defined padding + background - frame_width = background.width - frame_padding * 2 + frame_width = background.width - horizontal_frame_padding * 2 image.resize "#{frame_width}x" end - @image = put_device_into_background(background) + self.top_space_above_device = vertical_frame_padding if fetch_config['title'] - @image = add_title + background = put_title_into_background(background) end + @image = put_device_into_background(background) + image end - # Padding around the frames - def frame_padding + # Horizontal adding around the frames + def horizontal_frame_padding + padding = fetch_config['padding'] + unless padding.kind_of?(Integer) + padding = padding.split('x')[0].to_i + end + return scale_padding(padding) + end + + # Vertical adding around the frames + def vertical_frame_padding + padding = fetch_config['padding'] + unless padding.kind_of?(Integer) + padding = padding.split('x')[1].to_i + end + return scale_padding(padding) + end + + def scale_padding(padding) multi = 1.0 multi = 1.7 if self.screenshot.triple_density? - return fetch_config['padding'] * multi + return padding * multi end # Returns a correctly sized background image def generate_background background = MiniMagick::Image.open(fetch_config['background']) @@ -122,21 +141,11 @@ background end 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.portrait? # even more for portrait mode - if screenshot.mini? - # Such small devices need special treatment - bottom_space -= 50 if screenshot.portrait? - bottom_space += 65 unless screenshot.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}" end @@ -150,13 +159,13 @@ frame.resize "#{new_frame_width.round}x" # resize it to the calculated witdth modify_offset(multiplicator) # modify the offset to properly insert the screenshot into the frame later end # Add the title above the device - # rubocop:disable Metrics/AbcSize - def add_title - title_images = build_title_images(image.width) + def put_title_into_background(background) + title_images = build_title_images(image.width, image.height) + keyword = title_images[:keyword] title = title_images[:title] # sum_width: the width of both labels together including the space inbetween # is used to calculate the ratio @@ -175,60 +184,64 @@ 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 + vertical_padding = vertical_frame_padding + top_space = vertical_padding + left_space = (background.width / 2.0 - sum_width / 2.0).round + self.top_space_above_device += title.height + vertical_padding + # First, put the keyword on top of the screenshot, if we have one if keyword - @image = image.composite(keyword, "png") do |c| + background = background.composite(keyword, "png") do |c| c.compose "Over" c.geometry "+#{left_space}+#{top_space}" end 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| + background = background.composite(title, "png") do |c| c.compose "Over" c.geometry "+#{left_space}+#{top_space}" end - image + background end - # rubocop:enable Metrics/AbcSize def actual_font_size - [top_space_above_device / 3.0, @image.width / 30.0].max.round + [@image.width / 10.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 - def build_title_images(max_width) + def build_title_images(max_width, max_height) words = [:keyword, :title].keep_if { |a| fetch_text(a) } # optional keyword/title results = {} words.each do |key| # 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 + image_height = max_height # gets trimmed afterwards anyway, and on the iPad the `y` would get cut title_image.combine_options do |i| - # * 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 + # Oversize as the text might be larger than the actual image. We're trimming afterwards anyway + i.resize "#{max_width * 5.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 '#{text}'" if $verbose + text.gsub! '\n', "\n" + # Add the actual title title_image.combine_options do |i| i.font current_font if current_font i.gravity "Center" i.pointsize actual_font_size @@ -265,13 +278,13 @@ result = parsed.find { |k, v| screenshot.path.include? k } return result.last if result end # No string files, fallback to Framefile config - result = fetch_config[type.to_s]['text'] + result = fetch_config[type.to_s]['text'] if fetch_config[type.to_s] Helper.log.debug "Falling back to default text as there was nothing specified in the .strings file" if $verbose - if !result and type == :title + if type == :title and !result # title is mandatory raise "Could not get title for screenshot #{screenshot.path}. Please provide one in your Framefile.json".red end return result