Class: JekyllOgImage::Element::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/jekyll_og_image/element/text.rb

Instance Method Summary collapse

Constructor Details

#initialize(message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil) ⇒ Text

Returns a new instance of Text.



4
5
6
7
8
9
10
11
12
13
# File 'lib/jekyll_og_image/element/text.rb', line 4

def initialize(message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil)
  @message = message
  @gravity = gravity
  @width = width
  @dpi = dpi
  @color = color
  @font = font

  validate_gravity!
end

Instance Method Details

#apply_to(canvas, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll_og_image/element/text.rb', line 15

def apply_to(canvas, &block)
  params = {
    font: @font,
    width: @width,
    dpi: @dpi,
    align: :low
  }

  params[:wrap] = :word if wrap_supported?

  text = Vips::Image.text(@message, **params)

  text = text
    .new_from_image(hex_to_rgb(@color))
    .copy(interpretation: :srgb)
    .bandjoin(text)

  result = block.call(canvas, text) if block_given?

  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]

  composite_with_gravity(canvas, text, x, y)
end