~ubuntu-branches/ubuntu/trusty/ctioga/trusty

« back to all changes in this revision

Viewing changes to lib/legends/item.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2009-03-09 22:57:05 UTC
  • mfrom: (1.1.5 upstream) (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090309225705-hkp4lmr4uqjr99da
Tags: 1.10-1
* New upstream release
* Remove debian/patches/11-manpage-typo now unnecessary

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# legends.rb: legend implementation of CTioga
 
2
# copyright (c) 2008 by Vincent Fourmond: 
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'CTioga/log'
 
15
require 'CTioga/debug'
 
16
require 'CTioga/curve_style'
 
17
require 'CTioga/dimension'
 
18
 
 
19
module CTioga
 
20
 
 
21
  Version::register_svn_info('$Revision: 839 $', '$Date: 2008-10-11 14:47:08 +0200 (Sat, 11 Oct 2008) $')
 
22
 
 
23
  # The base class for all legends items (real legends, spacing, lines).
 
24
  class LegendItem
 
25
 
 
26
    include Debug
 
27
    include Log
 
28
 
 
29
    # This class-wide variable is used to number text
 
30
    # in a unique fashion
 
31
    @@legend_item_numbering = 0
 
32
 
 
33
    def initialize
 
34
      @legend_number = @@legend_item_numbering
 
35
      @@legend_item_numbering += 1
 
36
    end
 
37
 
 
38
    # Returns the (width, height) in figure coordinates
 
39
    # of the legend element with the given LegendStyle
 
40
    # and FigureMaker reference objects.
 
41
    #
 
42
    # The returned values can be inaccurate to some extent.
 
43
    def size(t, legend_style)
 
44
      return [0, 0]
 
45
    end
 
46
 
 
47
    # Draws the legend at the given top left position (x,y)
 
48
    # in figure coordinates.
 
49
    def draw(t, legend_style, x, y)
 
50
    end
 
51
 
 
52
    # For internal use
 
53
    def legend_name
 
54
      return "legend-#{@legend_number}"
 
55
    end
 
56
 
 
57
    # We put the baseline so that the space above and below in the
 
58
    # box of height legend_style.dy is even.
 
59
    def get_baseline_y(t, legend_style, y)
 
60
      return y - 0.5 * ( TextDimension.new(1.0).to_figure(t, :y)  +
 
61
                         legend_style.dy.to_figure(t, :y))
 
62
    end
 
63
 
 
64
 
 
65
  end
 
66
 
 
67
  # A class representing the style of a single legend line (unrelated
 
68
  # to a curve)
 
69
  class LegendLine < LegendItem
 
70
 
 
71
    # The text of the line
 
72
    attr_accessor :text
 
73
    
 
74
    def initialize(text = "")
 
75
      super()
 
76
      @text = text
 
77
    end
 
78
 
 
79
    # Draw one single text line
 
80
    def draw(t, legend_style, x, y)
 
81
      y = get_baseline_y(t, legend_style, y) 
 
82
      t.show_text('x' => x, 'y' => y, 
 
83
                  'text' => @text,
 
84
                  'justification' => LEFT_JUSTIFIED,
 
85
                  'measure' => legend_name
 
86
                  )
 
87
    end
 
88
 
 
89
    # Computes the size of the line. Height should always
 
90
    # be accurate, but width can be 0 sometimes...
 
91
    def size(t, legend_style)
 
92
      height = legend_style.dy.to_figure(t, :y)
 
93
      info = t.get_text_size(legend_name)
 
94
      if info.key? 'width'
 
95
        width = t.convert_output_to_figure_dx(10*info['width'])
 
96
      else
 
97
        width = 0
 
98
      end
 
99
 
 
100
      return [ width, height ]
 
101
    end
 
102
      
 
103
  end
 
104
 
 
105
  # The class handling the drawing of one Curve
 
106
  class CurveLegend < LegendItem
 
107
    
 
108
    attr_accessor :curve_style
 
109
 
 
110
    def initialize(style)
 
111
      super()
 
112
      @curve_style = style
 
113
    end
 
114
    
 
115
    # Draw one single text line
 
116
    def draw(t, legend_style, x, y)
 
117
      y = get_baseline_y(t, legend_style, y) 
 
118
      t.context do 
 
119
        # Position specification for the legend pictogram
 
120
        margin_specs = { 'left' => x,
 
121
          'right' => 1 - x - legend_style.picto_width.to_figure(t, :x),
 
122
          'bottom' => y,
 
123
          'top' => 1 - y - legend_style.picto_height.to_figure(t, :y)
 
124
        }
 
125
        debug "Legend margins for '#{@curve_style.legend}' : #{margin_specs.inspect}"
 
126
        t.subfigure(margin_specs) do
 
127
          # We scale the text back to normal so the markers have the right
 
128
          # size
 
129
          # t.line_width = 0.1
 
130
          # t.stroke_rect(0,0,1,1)
 
131
          t.rescale_text(1/legend_style.text_scale)
 
132
          @curve_style.output_legend_pictogram(t)
 
133
        end
 
134
      end
 
135
      t.show_text('x' => x + 
 
136
                  legend_style.picto_width.to_figure(t, :x) + 
 
137
                  legend_style.picto_to_text.to_figure(t, :x), 
 
138
                  'y' => y, 'text' => @curve_style.legend,
 
139
                  'measure' => legend_name,
 
140
                  'justification' => LEFT_JUSTIFIED)
 
141
    end
 
142
 
 
143
    # Computes the size of the line. Height should always
 
144
    # be accurate, but width can be 0 sometimes...
 
145
    def size(t, legend_style)
 
146
      height = legend_style.dy.to_figure(t, :y)
 
147
 
 
148
      width = legend_style.picto_width.to_figure(t, :x) + 
 
149
        legend_style.picto_to_text.to_figure(t, :x) 
 
150
 
 
151
      info = t.get_text_size(legend_name)
 
152
      
 
153
      if info.key? 'width'
 
154
        width += t.convert_output_to_figure_dx(10*info['width'])
 
155
      end
 
156
 
 
157
      return [ width, height ]
 
158
    end
 
159
    
 
160
    
 
161
  end
 
162
 
 
163
 
 
164
end