Class: RTree::Style

Inherits:
RTreeStyleBase show all
Defined in:
lib/rtree.rb

Overview

A Ruby wrapper around RTree styles, used in PostScript plotting. in particular by #postscript.

Author:

  • RTree::Style J. J. Green

Class Method Summary collapse

Methods inherited from RTreeStyleBase

#free

Class Method Details

.from_a(array) ⇒ RTree::Style Also known as: from_array

Create a new Style instance from array of Hash

Examples:

Create a simple one-level style:

style = RTree::Style.from_a(
  [
    {
      fill: {
        rgb: [0.5, 0.5, 0.5]
      },
      stroke: {
        rgb: [0, 0, 1],
        width: 3
      }
    }
  ]
)

Parameters:

  • array (Array)

    an array of hash, this will be converted to JSON and read by .from_json

Returns:

See Also:



543
544
545
# File 'lib/rtree.rb', line 543

def from_a(array)
  from_json(array.to_json)
end

.from_json(json) ⇒ RTree::Style

Create a new Style instance from JSON string

Parameters:

  • json (String)

    a JSON string

Returns:

Raises:

  • (TypeError)

See Also:



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/rtree.rb', line 500

def from_json(json)
  raise TypeError unless json.is_a? String
  rd, wr = IO.pipe(Encoding::UTF_8)
  if fork then
    wr.close
    begin
      result = json_read(rd)
    ensure
      rd.close
      Process.wait
    end
  else
    rd.close
    begin
      wr.write(json)
    ensure
      wr.close
      exit!
    end
  end
  result
end

.json_read(io) ⇒ RTree::Style

Create a new Style instance from JSON stream

Examples:

Read from file

style = File.open('some.style', 'r') do |io|
  RTree::Style.json_read(io)
end

Parameters:

  • io (IO)

    a readable stream object

Returns:



491
492
493
# File 'lib/rtree.rb', line 491

def json_read(io)
  super
end