Class: Thermal::Profile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/thermal/profile.rb

Constant Summary collapse

CODEPOINTS_CJK_SKIP =

These characters should always use codepage, even if present in CJK. HR chars have issues with double-printing in CJK encodings on Epson printers. This has been observed on Japanese (Shift-JIS) with u2500 and Simplified Chinese (GB18030) with u2584.

[
  "\u2500".."\u259F", # box drawing + block elements

  "\u2660".."\u2667"  # card suits

].map(&:to_a).flatten.join.each_codepoint.to_a.freeze
CODEPOINTS_CJK_FORCE =

These characters exist in the Katakana codepage, but should use CJK encoding if available.

'円年月日時分秒〒市区町村人'.each_codepoint.to_a.freeze

Instance Method Summary collapse

Constructor Details

#initialize(device, **opts) ⇒ Profile

Wraps a Device object with CJK encoding support.



23
24
25
26
# File 'lib/thermal/profile.rb', line 23

def initialize(device, **opts)
  @device_key = device.to_s
  @cjk = ::Thermal::Db.find_cjk_encoding(opts[:cjk_encoding]) || opts[:cjk_encoding]
end

Instance Method Details

#cjk_encodingObject



32
33
34
35
36
# File 'lib/thermal/profile.rb', line 32

def cjk_encoding
  return @cjk_encoding if defined?(@cjk_encoding)

  @cjk_encoding = ::Thermal::Db.cjk_encoding(@cjk)
end

#codepoint_cjk_force?(u_codepoint) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/thermal/profile.rb', line 61

def codepoint_cjk_force?(u_codepoint)
  CODEPOINTS_CJK_FORCE.include?(u_codepoint)
end

#codepoint_cjk_skip?(u_codepoint) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/thermal/profile.rb', line 57

def codepoint_cjk_skip?(u_codepoint)
  CODEPOINTS_CJK_SKIP.include?(u_codepoint)
end

#device_nameObject



28
29
30
# File 'lib/thermal/profile.rb', line 28

def device_name
  device.name
end

#find_encoding(u_codepoint, no_cjk: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/thermal/profile.rb', line 45

def find_encoding(u_codepoint, no_cjk: false)
  device_encoding = device.find_encoding(u_codepoint)
  if device_encoding && !codepoint_cjk_force?(u_codepoint)
    device_encoding
  elsif !no_cjk && cjk_encoding&.codepoint?(u_codepoint)
    [:cjk, true].freeze
  else # rubocop:disable Lint/DuplicateBranch

    # codepoint_cjk_force failed

    device_encoding
  end
end