1
# legends.rb: legend implementation of CTioga
2
# copyright (c) 2008 by Vincent Fourmond:
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.
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).
15
require 'CTioga/debug'
16
require 'CTioga/curve_style'
17
require 'CTioga/dimension'
21
Version::register_svn_info('$Revision: 839 $', '$Date: 2008-10-11 14:47:08 +0200 (Sat, 11 Oct 2008) $')
23
# The base class for all legends items (real legends, spacing, lines).
29
# This class-wide variable is used to number text
31
@@legend_item_numbering = 0
34
@legend_number = @@legend_item_numbering
35
@@legend_item_numbering += 1
38
# Returns the (width, height) in figure coordinates
39
# of the legend element with the given LegendStyle
40
# and FigureMaker reference objects.
42
# The returned values can be inaccurate to some extent.
43
def size(t, legend_style)
47
# Draws the legend at the given top left position (x,y)
48
# in figure coordinates.
49
def draw(t, legend_style, x, y)
54
return "legend-#{@legend_number}"
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))
67
# A class representing the style of a single legend line (unrelated
69
class LegendLine < LegendItem
71
# The text of the line
74
def initialize(text = "")
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,
84
'justification' => LEFT_JUSTIFIED,
85
'measure' => legend_name
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)
95
width = t.convert_output_to_figure_dx(10*info['width'])
100
return [ width, height ]
105
# The class handling the drawing of one Curve
106
class CurveLegend < LegendItem
108
attr_accessor :curve_style
110
def initialize(style)
115
# Draw one single text line
116
def draw(t, legend_style, x, y)
117
y = get_baseline_y(t, legend_style, y)
119
# Position specification for the legend pictogram
120
margin_specs = { 'left' => x,
121
'right' => 1 - x - legend_style.picto_width.to_figure(t, :x),
123
'top' => 1 - y - legend_style.picto_height.to_figure(t, :y)
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
130
# t.stroke_rect(0,0,1,1)
131
t.rescale_text(1/legend_style.text_scale)
132
@curve_style.output_legend_pictogram(t)
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)
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)
148
width = legend_style.picto_width.to_figure(t, :x) +
149
legend_style.picto_to_text.to_figure(t, :x)
151
info = t.get_text_size(legend_name)
154
width += t.convert_output_to_figure_dx(10*info['width'])
157
return [ width, height ]