Method: Origami::PDF#export_to_graph

Defined in:
lib/origami/export.rb

#export_to_graph(path) ⇒ Object

Exports the document to a dot Graphiz file.

filename

The path where to save the file.



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/origami/export.rb', line 29

def export_to_graph(path)

    appearance = -> (object) do
        label = object.type.to_s
        case object
        when Catalog
            fontcolor = "red"
            color = "mistyrose"
            shape = "ellipse"
        when Name, Number
            label = object.value
            fontcolor = "brown"
            color = "lightgoldenrodyellow"
            shape = "polygon"
        when String
            label = object.value if (object.ascii_only? and object.length <= 50)
            fontcolor = "red"
            color = "white"
            shape = "polygon"
        when Array
            fontcolor = "darkgreen"
            color = "lightcyan"
            shape = "ellipse"
        else
          fontcolor = "blue"
          color = "aliceblue"
          shape = "ellipse"
        end

        { label: label, fontcolor: fontcolor, color: color, shape: shape }
    end

    add_edges = -> (fd, object) do
        if object.is_a?(Array) or object.is_a?(ObjectStream)
            object.each do |subobj|
                fd << "\t#{object.object_id} -> #{subobj.solve.object_id}\n"
            end

        elsif object.is_a?(Dictionary)
            object.each_pair do |name, subobj|
                fd << "\t#{object.object_id} -> #{subobj.solve.object_id} "
                fd << "[label=\"#{name.value}\",fontsize=9];\n"
            end
        end

        if object.is_a?(Stream)
            object.dictionary.each_pair do |key, value|
                fd << "\t#{object.object_id} -> #{value.solve.object_id} "
                fd << "[label=\"#{key.value}\",fontsize=9];\n"
            end
        end
    end

    graph_name = "PDF" if graph_name.nil? or graph_name.empty?
    fd = File.open(path, "w")

    begin
        fd << "digraph #{graph_name} {\n\n"

        objects = self.objects(include_keys: false).find_all{ |obj| not obj.is_a?(Reference) }

        objects.each do |object|
            attr = appearance[object]

            fd << "\t#{object.object_id} "
            fd << "[label=\"#{attr[:label]}\",shape=#{attr[:shape]},color=#{attr[:color]},style=filled,fontcolor=#{attr[:fontcolor]},fontsize=16];\n"

            if object.is_a?(Stream)
                object.dictionary.each do |value|
                    unless value.is_a?(Reference)
                        attr = appearance[value]
                        fd << "\t#{value.object_id} "
                        fd << "[label=\"#{attr[:label]}\",shape=#{attr[:shape]},color=#{attr[:color]},style=filled,fontcolor=#{attr[:fontcolor]},fontsize=16];\n"
                    end
                end
            end

            add_edges.call(fd, object)
        end

        fd << "\n}"
    ensure
        fd.close
    end
end