lib/frameit/editor.rb in frameit-2.4.2 vs lib/frameit/editor.rb in frameit-2.5.0

- old
+ new

@@ -3,10 +3,12 @@ attr_accessor :screenshot # reference to the screenshot object to fetch the path, title, etc. attr_accessor :frame # the frame of the device attr_accessor :image # the current image used for editing attr_accessor :top_space_above_device + FONT_SIZE_REFERENCE_WIDTH = 640.0 # The size of device a, e.g. "12px" is defined for + def frame!(screenshot) self.screenshot = screenshot prepare_image if load_frame # e.g. Mac doesn't need a frame @@ -170,22 +172,26 @@ # 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) + # Only resize if we haven't specified a custom font size + font_size = font_size('title') + if font_size.nil? + # 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) - UI.message "Text for image #{self.screenshot.path} is quite long, reducing font size by #{(ratio - 1.0).round(2)}" if $verbose + UI.message "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 + title.resize "#{(smaller * title.width).round}x" + keyword.resize "#{(smaller * keyword.width).round}x" if keyword + sum_width *= smaller + end end vertical_padding = vertical_frame_padding top_space = vertical_padding left_space = (background.width / 2.0 - sum_width / 2.0).round @@ -208,17 +214,17 @@ c.geometry "+#{left_space}+#{top_space}" end background end - def actual_font_size + def derived_font_size [@image.width / 10.0].max.round end # The space between the keyword and the title def keyword_padding - (actual_font_size / 2.0).round + (derived_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, max_height) words = [:keyword, :title].keep_if { |a| fetch_text(a) } # optional keyword/title @@ -232,21 +238,27 @@ # 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) + custom_font_size = font_size(key) text = fetch_text(key) UI.message "Using #{current_font} as font the #{key} of #{screenshot.path}" if $verbose and current_font UI.message "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 + if custom_font_size + i.pointsize custom_font_size + UI.message "Using custom font size #{custom_font_size}" if $verbose + else + i.pointsize derived_font_size + end i.draw "text 0,0 '#{text}'" i.fill fetch_config[key.to_s]['color'] end title_image.trim # remove white space @@ -312,9 +324,40 @@ end end end UI.message "No custom font specified for #{screenshot}, using the default one" if $verbose + return nil + end + + def scaled_font_size(font_size) + font_ratio = font_size / FONT_SIZE_REFERENCE_WIDTH + return (font_ratio * screenshot.size[0].to_f).round + end + + # The fontSize we want to use + def font_size(key) + single_font_size = fetch_config[key.to_s]['fontSize'] + return single_font_size if single_font_size + + fonts = fetch_config[key.to_s]['fonts'] + if fonts + fonts.each do |font| + if font['supported'] + font['supported'].each do |language| + if screenshot.path.include? language + return scaled_font_size(font["fontSize"]) + end + end + else + # No `supported` array, this will always be true + UI.message "Found a fontSize with no list of supported languages, using this now" if $verbose + return scaled_font_size(font["fontSize"]) + end + end + end + + UI.message "No custom fontSize specified for #{screenshot}, using the default one" if $verbose return nil end end end