class Lipstick::Images::EmailBanner

Creates an AAF standard email banner, with three elements, positioned as follows:

  1. AAF Logo. Vertically centered and left-aligned within the content box, and with a blank space to the right.

  2. Application title. Vertically centered, and left-aligned to the logo’s right margin.

  3. Environment string. Positioned 10 pixels below the app title, and left-aligned to the logo’s right margin.

The “box” containing these three elements is horizontally centered in the resulting image.

Text metrics work as follows, using “Alimony” as the example word:

For image compositing, (x, y) is the position of the top-left corner.

Constants

HEIGHT
MID_Y
WIDTH

Public Class Methods

new(image:, title:, environment:, background_color:, resize_to:) click to toggle source
# File lib/lipstick/images/email_banner.rb, line 29
def initialize(image:, title:, environment:,
               background_color:, resize_to:)
  require 'rmagick'

  @title = title
  @environment = environment
  @image = image
  @background_color = background_color
  @resize_to = resize_to
end

Public Instance Methods

to_png() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 40
def to_png
  bgcolor = @background_color

  canvas = Magick::ImageList.new
  canvas.new_image(WIDTH, HEIGHT) do |f|
    f.format = 'PNG'
    f.background_color = bgcolor
  end

  annotate_title(canvas)
  annotate_environment(canvas)

  canvas.composite!(logo, logo_x, logo_y, Magick::SrcOverCompositeOp)

  canvas.to_blob
end

Private Instance Methods

annotate_environment(canvas) click to toggle source
# File lib/lipstick/images/email_banner.rb, line 90
def annotate_environment(canvas)
  return if blank_environment?

  environment_format.annotate(canvas, 0, 0, env_x, env_y, @environment)
end
annotate_title(canvas) click to toggle source
# File lib/lipstick/images/email_banner.rb, line 86
def annotate_title(canvas)
  title_format.annotate(canvas, 0, 0, title_x, title_y, @title)
end
blank_environment?() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 82
def blank_environment?
  !@environment&.present?
end
env_metrics() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 122
def env_metrics
  return OpenStruct.new(width: 0, height: 0) if blank_environment?

  environment_format.get_type_metrics(@environment)
end
env_x() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 146
def env_x
  title_x
end
env_y() click to toggle source

‘ascent` is a positive value, and `descent` is a negative value. They describe the full height of the letters. See RMagick documentation.

# File lib/lipstick/images/email_banner.rb, line 152
def env_y
  MID_Y + title_metrics.ascent - title_metrics.descent + 10
end
environment_format() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 73
def environment_format
  @environment_format ||= Magick::Draw.new do |title|
    title.font_family = 'Helvetica'
    title.pointsize = 20
    title.gravity = Magick::ForgetGravity
    title.fill = '#dd7727'
  end
end
gap() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 101
def gap
  @gap ||= environment_format.get_type_metrics('AAF').width
end
logo_w() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 105
def logo_w
  logo.bounding_box.width
end
logo_x() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 109
def logo_x
  margin_x
end
logo_y() click to toggle source

y position to vertically center logo

# File lib/lipstick/images/email_banner.rb, line 114
def logo_y
  ((HEIGHT - logo.bounding_box.height) / 2).round
end
margin_x() click to toggle source

Halve the total margin to horizontally center the whole content area.

# File lib/lipstick/images/email_banner.rb, line 129
def margin_x
  # | whitespace | logo | whitespace | text | whitespace |
  # |<---------->|
  ((WIDTH - total_w) / 2).round
end
text_w() click to toggle source

Center using the largest width for aesthetics.

# File lib/lipstick/images/email_banner.rb, line 157
def text_w
  [title_metrics.width, env_metrics.width].max
end
title_format() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 64
def title_format
  @title_format ||= Magick::Draw.new do |title|
    title.font_family = 'Helvetica'
    title.pointsize = 24
    title.gravity = Magick::ForgetGravity
    title.fill = 'white'
  end
end
title_metrics() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 118
def title_metrics
  title_format.get_type_metrics(@title)
end
title_x() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 135
def title_x
  # | whitespace | logo | whitespace | text | whitespace |
  #                                    ^ this position
  margin_x + logo_w + gap
end
title_y() click to toggle source

y position to vertically center title

# File lib/lipstick/images/email_banner.rb, line 142
def title_y
  MID_Y + (title_metrics.ascent / 2).round
end
total_w() click to toggle source
# File lib/lipstick/images/email_banner.rb, line 161
def total_w
  # | whitespace | logo | whitespace | text | whitespace |
  #              |<------------------------>|
  logo_w + gap + text_w
end