comparison scripts/plot/text.m @ 12965:22bc9ec80c2c

allow multi-line string property for text objects using cell arrays or char matrices * __axis_label__.m: Don't check type of txt argument. * __go_draw_axes__.m: Handle multi-line string property for text objects. * text.m: Likewise. * gl2ps-renderer.cc (glps_renderer::draw_text): Handle text::properties string property as octave_value object that can contain either a char array or cellstr object. * graphics.cc (axes::properties::update_xlabel_position, axes::properties::update_ylabel_position, axes::properties::update_zlabel_position, axes::properties::get_extent, text::properties::update_text_extent): Likewise. * graphics.h.in (text_label_property::do_set): Don't forget to set stored_type when value is a cell. (text::properties::get_string): Delete custom getter.
author Ben Abbott <bpabbott@mac.com>
date Mon, 15 Aug 2011 10:24:09 -0400
parents a754c2d8a13f
children e65d1488ff6a
comparison
equal deleted inserted replaced
12964:8ec12d686796 12965:22bc9ec80c2c
45 z = zeros (size (x)); 45 z = zeros (size (x));
46 offset = 3; 46 offset = 3;
47 endif 47 endif
48 48
49 label = varargin{offset}; 49 label = varargin{offset};
50 if (ischar (label) || iscellstr (label)) 50 varargin(1:offset) = [];
51 varargin(1:offset) = []; 51
52 if (ischar (label)) 52 nx = numel (x);
53 ny = numel (y);
54 nz = numel (z);
55 if (ischar (label) || isnumeric (label))
56 nt = size (label, 1);
57 if (nx > 1 && nt == 1)
58 ## Mutiple text objects with same string
59 label = repmat ({label}, [nx, 1]);
60 nt = nx;
61 elseif (nx > 1 && nt == nx)
62 ## Mutiple text objects with different strings
53 label = cellstr (label); 63 label = cellstr (label);
64 elseif (ischar (label))
65 ## Single text object with one or more lines
66 label = {label};
54 endif 67 endif
55 n = numel (label); 68 elseif (iscell (label))
56 nx = numel (x); 69 nt = numel (label);
57 ny = numel (y); 70 if (nx > 1 && nt == 1)
58 nz = numel (z); 71 label = repmat ({label}, [nx, 1]);
72 nt = nx;
73 elseif (nx > 1 && nt == nx)
74 else
75 label = {label};
76 endif
59 else 77 else
60 error ("text: expecting LABEL to be a character string or cell array of character strings"); 78 error ("text: expecting LABEL to be a character string or cell array of character strings");
61 endif 79 endif
62 else 80 else
63 x = y = z = 0; 81 x = y = z = 0;
64 nx = ny = nz = 1; 82 nx = ny = nz = 1;
65 label = {""}; 83 label = {""};
66 n = 1; 84 nt = 1;
67 endif 85 endif
68 86
69 if (rem (numel (varargin), 2) == 0) 87 if (rem (numel (varargin), 2) == 0)
70 88
71 if (nx == ny && nx == nz) 89 if (nx == ny && nx == nz)
72 pos = [x(:), y(:), z(:)]; 90 pos = [x(:), y(:), z(:)];
73 ca = gca (); 91 ca = gca ();
74 tmp = zeros (n, 1); 92 tmp = zeros (nt, 1);
75 if (n == 1) 93 if (nx == 1)
76 label = label{1}; 94 ## TODO - Modify __go_text__() to accept cell-strings
77 for i = 1:nx 95 tmp = __go_text__ (ca, "string", "foobar",
78 tmp(i) = __go_text__ (ca, "string", label, 96 varargin{:},
97 "position", pos);
98 set (tmp, "string", label{1});
99 elseif (nt == nx)
100 for n = 1:nt
101 tmp(n) = __go_text__ (ca, "string", label{n},
79 varargin{:}, 102 varargin{:},
80 "position", pos(i,:)); 103 "position", pos(n,:));
81 endfor
82 __request_drawnow__ ();
83 elseif (n == nx)
84 for i = 1:nx
85 tmp(i) = __go_text__ (ca, "string", label{i},
86 varargin{:},
87 "position", pos(i,:));
88 endfor 104 endfor
89 __request_drawnow__ (); 105 __request_drawnow__ ();
90 else 106 else
91 error ("text: dimension mismatch for coordinates and LABEL"); 107 error ("text: dimension mismatch for coordinates and LABEL");
92 endif 108 endif
140 %! "horizontalalignment", "left", ... 156 %! "horizontalalignment", "left", ...
141 %! "verticalalignment", "bottom") 157 %! "verticalalignment", "bottom")
142 %! endfor 158 %! endfor
143 %! caxis ([-100 100]) 159 %! caxis ([-100 100])
144 %! title ("Vertically Aligned at Bottom") 160 %! title ("Vertically Aligned at Bottom")
161
162 %!demo
163 %! clf
164 %! axis ([0 8 0 8])
165 %! title (["First title";"Second title"])
166 %! xlabel (["First xlabel";"Second xlabel"])
167 %! ylabel (["First ylabel";"Second ylabel"])
168 %! text (4, 4, {"Hello", "World"}, ...
169 %! "horizontalalignment", "center", ...
170 %! "verticalalignment", "middle")
171 %! grid on
172
173 %!demo
174 %! clf
175 %! h = mesh (peaks, "edgecolor", 0.7 * [1 1 1], ...
176 %! "facecolor", "none", ...
177 %! "facealpha", 0);
178 %! title (["First title";"Second title"])
179 %! xlabel (["First xlabel";"Second xlabel"])
180 %! ylabel (["First ylabel";"Second ylabel"])
181 %! zlabel (["First zlabel";"Second zlabel"])
182 %! text (0, 0, 5, {"Hello", "World"}, ...
183 %! "horizontalalignment", "center", ...
184 %! "verticalalignment", "middle")
185 %! hold on
186 %! plot3 (0, 0, 5, "+k")
187 %!
188
189 %!demo
190 %! clf
191 %! h = text (0.6, 0.3, "char");
192 %! assert ("char", class (get (h, "string")))
193 %! h = text (0.6, 0.4, ["char row 1"; "char row 2"]);
194 %! assert ("char", class (get (h, "string")))
195 %! h = text (0.6, 0.6, {"cell2str (1,1)", "cell2str (1,2)"; "cell2str (2,1)", "cell2str (2,2)"});
196 %! assert ("cell", class (get (h, "string")))
197 %! h = text (0.6, 0.8, "foobar");
198 %! set (h, "string", 1:3)
199 %! h = text ([0.2, 0.2], [0.3, 0.4], "one string & two objects");
200 %! assert ("char", class (get (h(1), "string")))
201 %! assert ("char", class (get (h(2), "string")))
202 %! h = text ([0.2, 0.2], [0.5, 0.6], {"one cellstr & two objects"});
203 %! assert ("cell", class (get (h(1), "string")))
204 %! assert ("cell", class (get (h(2), "string")))
205 %! h = text ([0.2, 0.2], [0.7, 0.8], {"cellstr 1 object 1", "cellstr 2 object 2"});
206 %! assert ("char", class (get (h(1), "string")))
207 %! assert ("char", class (get (h(2), "string")))
208 %! xlabel (1:2)
209 %! ylabel (1:2)
210 %! title (1:2)
211