Class: Thermal::Stargraphic::Writer

Inherits:
WriterBase show all
Defined in:
lib/thermal/stargraphic/writer.rb

Overview

TODO: image buffer TODO: page limit

Constant Summary collapse

DOT_WIDTH =
576
LINE_HEIGHT =
34
PAGE_LENGTH =
32_000
MAX_BYTES =

Star API allows 500KB max

500_000

Constants inherited from WriterBase

WriterBase::ALIGNMENTS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WriterBase

#align, #format

Constructor Details

#initialize(profile) ⇒ Writer

Returns a new instance of Writer.



13
14
15
16
17
# File 'lib/thermal/stargraphic/writer.rb', line 13

def initialize(profile)
  @width = DOT_WIDTH
  @align = :left
  super
end

Class Method Details

.fontObject

Persists font as singleton



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/thermal/stargraphic/writer.rb', line 199

def font
  @font ||= if OS.windows?
              'Sans'
            else
              font_list = begin
                `fc-list`
              rescue StandardError
                ''
              end
              case font_list
              when /NotoSans/ then 'NotoSans'
              else 'Sans'
              end
            end
end

.formatObject



19
20
21
# File 'lib/thermal/stargraphic/writer.rb', line 19

def self.format
  'stargraphic'
end

Instance Method Details

#boldObject



44
45
46
47
48
# File 'lib/thermal/stargraphic/writer.rb', line 44

def bold
  @bold = true
  yield
  @bold = false
end

#cutObject



71
72
73
74
# File 'lib/thermal/stargraphic/writer.rb', line 71

def cut
  seq_set_em_mode
  seq_exec_em
end

#feed(lines = 1) ⇒ Object



56
57
58
# File 'lib/thermal/stargraphic/writer.rb', line 56

def feed(lines = 1)
  feed_rows(lines * LINE_HEIGHT)
end

#hr(style = nil, width: col_width) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



39
40
41
42
# File 'lib/thermal/stargraphic/writer.rb', line 39

def hr(style = nil, width: col_width) # rubocop:disable Lint/UnusedMethodArgument
  append_image(hr_image(style))
  # append_raw(hr_seq(style))
end

#image(path) ⇒ Object



60
61
62
63
64
65
# File 'lib/thermal/stargraphic/writer.rb', line 60

def image(path)
  width, height = ::MiniMagick::Image.open(path).dimensions
  output, = Open3.capture3("convert #{path} -depth 1 R:-", binmode: true)
  output = output.chomp
  append_image([width, height, output])
end


23
24
25
26
27
28
29
30
31
# File 'lib/thermal/stargraphic/writer.rb', line 23

def print(flush: true)
  seq_init_raster_mode
  seq_enter_raster_mode
  seq_set_page_length
  super
  feed(5)
  cut
  finalize(flush: flush)
end

#qr_code(url) ⇒ Object



67
68
69
# File 'lib/thermal/stargraphic/writer.rb', line 67

def qr_code(url)
  append_image(qr_code_image(url))
end

#text(str, feed: true, replace: nil, **_kwargs) ⇒ Object



33
34
35
36
37
# File 'lib/thermal/stargraphic/writer.rb', line 33

def text(str, feed: true, replace: nil, **_kwargs)
  str = ::Thermal::Util.normalize_utf8(str, replace: replace)
  str = "#{str}\n" if feed
  append_text(str) if str
end

#underline(weight: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



50
51
52
53
54
# File 'lib/thermal/stargraphic/writer.rb', line 50

def underline(weight: nil) # rubocop:disable Lint/UnusedMethodArgument
  @underline = true
  yield
  @underline = false
end