Module: Pdfrb::Arlington::Predicate::Functions::ArithmeticPredicates

Defined in:
lib/pdfrb/arlington/predicate/functions/arithmetic.rb

Overview

Arithmetic helpers: ArrayLength, StringLength, RequiredValue, BitSet, BitsClear, BitsSet, KeyNameIsColorant.

Class Method Summary collapse

Class Method Details

.register_allObject



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
59
60
# File 'lib/pdfrb/arlington/predicate/functions/arithmetic.rb', line 12

def register_all
  Functions.register("ArrayLength") do |args, ctx|
    key = args.first
    val = ctx.current && ctx.current[key.to_sym]
    val = val.to_a if val.is_a?(Pdfrb::Model::PdfArray)
    val.is_a?(::Array) ? val.length : 0
  end

  Functions.register("StringLength") do |args, ctx|
    key = args.first
    val = ctx.current && ctx.current[key.to_sym]
    val.is_a?(::String) ? val.bytesize : 0
  end

  Functions.register("RequiredValue") do |_args, _ctx|
    # Caller wraps as RequiredValue(@X == Y); the comparison
    # happens in BinOp, this just passes through.
    true
  end

  Functions.register("BitSet") do |args, _ctx|
    value, bit = args
    next false unless value.is_a?(::Integer) && bit.is_a?(::Integer)

    (value & (1 << bit)).nonzero?
  end

  Functions.register("BitsClear") do |args, _ctx|
    value, mask = args
    next false unless value.is_a?(::Integer) && mask.is_a?(::Integer)

    (value & mask).zero?
  end

  Functions.register("BitsSet") do |args, _ctx|
    value, mask = args
    next false unless value.is_a?(::Integer) && mask.is_a?(::Integer)

    (value & mask) == mask
  end

  Functions.register("KeyNameIsColorant") do |_args, _ctx|
    false # consumer-side; default to false
  end

  Functions.register("NoCycle") do |_args, _ctx|
    true # consumer-side; assume no cycles until proven
  end
end