class Mathtype5::RecordTmpl

Constants

DIGIT_MODE_VARIATIONS

When options overlap in the binary space, ordinary bitmasks are not the correct tool to use for detection. We use digit position and presence instead.

EXPOSED_IN_SNAPSHOT
SELECTORS
VARIATIONS

Top-level keys are template identifiers, defined in TEMPLATES. Second-level keys are bits for certain variations, negative keys mean that the variation is present if the bit is absent.

Public Instance Methods

check_bitmask(flag) click to toggle source
# File lib/records5/tmpl.rb, line 468
def check_bitmask(flag)
  if flag == 0
    @variation & 0xf == 0
  else
    @variation & flag == flag
  end
end
check_digit(flag) click to toggle source

E.g. is 0x3 present in 0x33

# File lib/records5/tmpl.rb, line 477
def check_digit(flag)
  digits = if flag == 0
    1
  else
    (Math.log(flag+1)/Math.log(16)).ceil # digits in a hex number
  end
  mask = (15<<(4*digits-4)) # e.g. 0xf0
  variation_digit = (@variation & mask) >> (digits * 4 - 4)
  flag_digit = (flag & mask) >> (digits * 4 - 4)
  variation_digit == flag_digit
end
check_flag(flag) click to toggle source
# File lib/records5/tmpl.rb, line 459
def check_flag(flag)
  case mode
  when :bitmask
    check_bitmask(flag)
  when :digit
    check_digit(flag)
  end
end
mode() click to toggle source
# File lib/records5/tmpl.rb, line 490
def mode
  DIGIT_MODE_VARIATIONS.include?(selector) ? :digit : :bitmask
end
process_variations() click to toggle source
# File lib/records5/tmpl.rb, line 439
def process_variations
  @variations.select do |flag, _|
    if flag < 0 # flag should NOT be active
      !check_flag(-flag)
    else # flag should be active
      check_flag(flag)
    end
  end.map do |flag, value|
    case value
    when Hash # Conditional variations
      result = value.detect do |conditional, _|
        check_flag(conditional)
      end
      result.last if result
    else
      value
    end
  end.uniq
end
selector() click to toggle source
# File lib/records5/tmpl.rb, line 494
def selector
  SELECTORS[_selector]
end
variation() click to toggle source
# File lib/records5/tmpl.rb, line 430
def variation
  @variation = (_variation_first_byte & 0x7F) | (_variation_second_byte << 8)
  @variations = VARIATIONS.select do |selector, _|
    selector === _selector
  end.values.reduce(Hash.new, :merge)

  process_variations
end