Module: Drakkon::Images::TextToImage

Defined in:
lib/drakkon/lib/images/text.rb

Overview

Make some img files from fonts

Class Method Summary collapse

Class Method Details

.font_promptObject



60
61
62
63
64
65
66
67
68
# File 'lib/drakkon/lib/images/text.rb', line 60

def self.font_prompt
  list = Dir['*.ttf', '*.otf', 'fonts/*']

  prompt.select('Select Font:', filter: true) do |menu|
    list.each do |font|
      menu.choice name: font
    end
  end
end

.generate(font:, text:, size:, color:, file:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/drakkon/lib/images/text.rb', line 70

def self.generate(font:, text:, size:, color:, file:)
  LogBot.info('TextToImage', file)
  MiniMagick::Tool::Convert.new do |convert|
    convert.background 'none'
    convert.fill color
    convert.font font
    convert.pointsize size
    convert << "label:#{text}"
    convert << "#{file}.png"
  end
end

.promptObject



82
83
84
# File 'lib/drakkon/lib/images/text.rb', line 82

def self.prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
end

.run!(args = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'lib/drakkon/lib/images/text.rb', line 5

def self.run!(args = [])
  text = if args.empty?
           prompt.ask('Text? (e.g. rawr): ')
         else
           args.shift
         end

  size = if args.empty?
           24
         else
           args.shift
         end

  color = if args.empty?
            'white'
          else
            args.shift
          end

  file = if args.empty?
           text.gsub(' ', '').downcase
         else
           args.shift
         end

  file.gsub!(/[^0-9A-Za-z.-]/, '_')

  font = if args.empty?
           font_prompt
         else
           args.shift
         end

  puts <<~HELP
            Usage [text] [size=24] [color=white] [file=text] [font]
            - Will look for fonts directory
            - Generates via MiniMagick on transparent background


    Text: #{text}
    Size: #{size}
    Color: #{color}
    File: #{file}
    Font: #{font}
  HELP

  puts

  exit unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)}"

  generate(font: font, text: text, size: size, color: color, file: file)
rescue TTY::Reader::InputInterrupt
  exit 0
end