Class: Asciidoctor::Diagram::MemeConverter

Inherits:
Object
  • Object
show all
Includes:
DiagramConverter
Defined in:
lib/asciidoctor-diagram/meme/converter.rb

Instance Method Summary collapse

Methods included from DiagramConverter

#native_scaling?, #wrap_source

Instance Method Details

#collect_options(source) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/asciidoctor-diagram/meme/converter.rb', line 18

def collect_options(source)
  bg_img = source.attr('background')
  raise "background attribute is required" unless bg_img

  margin = source.attr('margin', '')
  margin_parts = margin.split(' ')
  case margin_parts.length
    when 0
      ml = mr = mt = mb = nil
    when 1
      ml = mr = mt = mb = margin_parts[0]
    when 2
      mt = mb = margin_parts[0]
      ml = mr = margin_parts[1]
    when 3
      mt = margin_parts[0]
      ml = mr = margin_parts[1]
      mb = margin_parts[2]
    else
      mt = margin_parts[0]
      mr = margin_parts[1]
      mb = margin_parts[2]
      ml = margin_parts[3]
  end

  {
      :bg_img => bg_img,
      :top_label => source.attr('top'),
      :bottom_label => source.attr('bottom'),
      :fill_color => source.attr(['fillcolor', 'fill-color']),
      :stroke_color => source.attr(['strokecolor', 'stroke-color']),
      :stroke_width => source.attr(['strokewidth', 'stroke-width']),
      :font => source.attr('font', 'Impact'),
      :font_size => source.attr('font_size', nil),
      :alignment => source.attr('alignment', nil),
      :margin_left => ml,
      :margin_right => mr,
      :margin_top => mt,
      :margin_bottom => mb,
      :noupcase => source.opt('noupcase'),
      :imagesdir => source.attr('imagesdir')
  }
end

#convert(source, format, options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/asciidoctor-diagram/meme/converter.rb', line 62

def convert(source, format, options)
  magick = source.find_command('magick', :raise_on_error => false)
  if magick
    convert = ->(*args) { Cli.run(magick, 'convert', *args) }
    identify = ->(*args) { Cli.run(magick, 'identify', *args) }
  else
    convert_cmd = source.find_command('convert')
    convert = ->(*args) { Cli.run(convert_cmd, *args) }
    identify_cmd = source.find_command('identify')
    identify = ->(*args) { Cli.run(identify_cmd, *args) }
  end

  bg_img = options[:bg_img]
  raise "background attribute is required" unless bg_img

  bg_img = source.resolve_path(bg_img, options[:imagesdir])

  top_label = options[:top_label]
  bottom_label = options[:bottom_label]
  fill_color = options[:fill_color] || 'white'
  stroke_color = options[:stroke_color] || 'black'
  stroke_width = options[:stroke_width] || '2'
  font = options[:font] || 'Impact'
  noupcase = options[:noupcase]

  dimensions = identify.call('-format', '%w %h', bg_img)[:out].match(/(?<w>\d+) (?<h>\d+)/)
  bg_width = dimensions['w'].to_i
  bg_height = dimensions['h'].to_i
  label_width = bg_width
  label_height = bg_height / 5

  if top_label
    top_img = Tempfile.new(['meme', '.png'])
    convert.call(
        '-background', 'none',
        '-fill', fill_color,
        '-stroke', stroke_color,
        '-strokewidth', stroke_width,
        '-font', font,
        '-size', "#{label_width}x#{label_height}",
        '-gravity', 'north',
        "label:#{prepare_label(top_label, noupcase)}",
        top_img.path
    )
  else
    top_img = nil
  end

  if bottom_label
    bottom_img = Tempfile.new(['meme', '.png'])
    convert.call(
        '-background', 'none',
        '-fill', fill_color,
        '-stroke', stroke_color,
        '-strokewidth', stroke_width,
        '-font', font,
        '-size', "#{label_width}x#{label_height}",
        '-gravity', 'south',
        "label:#{prepare_label(bottom_label, noupcase)}",
        bottom_img.path
    )
  else
    bottom_img = nil
  end

  final_img = Tempfile.new(['meme', ".#{format.to_s}"])

  args = [bg_img]
  if top_img
    args << top_img.path << '-geometry' << '+0+0' << '-composite'
  end

  if bottom_img
    args << bottom_img.path << '-geometry' << "+0+#{bg_height - label_height}" << '-composite'
  end

  args << final_img.path

  convert.call(*args)

  File.binread(final_img)
end

#supported_formatsObject



13
14
15
# File 'lib/asciidoctor-diagram/meme/converter.rb', line 13

def supported_formats
  [:png, :gif]
end