1 #define __SP_TEXT_CHEMISTRY_C__
3 /*
4 * Text commands
5 *
6 * Authors:
7 * bulia byak
8 *
9 * Copyright (C) 2004 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 #include "libnr/nr-matrix-fns.h"
18 #include "xml/repr.h"
19 #include <glibmm/i18n.h>
20 #include "sp-rect.h"
21 #include "sp-textpath.h"
22 #include "inkscape.h"
23 #include "document.h"
24 #include "message-stack.h"
25 #include "selection.h"
26 #include "desktop-handles.h"
27 #include "text-editing.h"
28 #include "sp-flowtext.h"
29 #include "sp-flowregion.h"
30 #include "sp-flowdiv.h"
33 SPItem *
34 text_in_selection(Inkscape::Selection *selection)
35 {
36 for (GSList *items = (GSList *) selection->itemList();
37 items != NULL;
38 items = items->next) {
39 if (SP_IS_TEXT(items->data))
40 return ((SPItem *) items->data);
41 }
42 return NULL;
43 }
45 SPItem *
46 flowtext_in_selection(Inkscape::Selection *selection)
47 {
48 for (GSList *items = (GSList *) selection->itemList();
49 items != NULL;
50 items = items->next) {
51 if (SP_IS_FLOWTEXT(items->data))
52 return ((SPItem *) items->data);
53 }
54 return NULL;
55 }
57 SPItem *
58 text_or_flowtext_in_selection(Inkscape::Selection *selection)
59 {
60 for (GSList *items = (GSList *) selection->itemList();
61 items != NULL;
62 items = items->next) {
63 if (SP_IS_TEXT(items->data) || SP_IS_FLOWTEXT(items->data))
64 return ((SPItem *) items->data);
65 }
66 return NULL;
67 }
69 SPItem *
70 shape_in_selection(Inkscape::Selection *selection)
71 {
72 for (GSList *items = (GSList *) selection->itemList();
73 items != NULL;
74 items = items->next) {
75 if (SP_IS_SHAPE(items->data))
76 return ((SPItem *) items->data);
77 }
78 return NULL;
79 }
81 void
82 text_put_on_path()
83 {
84 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
85 if (!desktop)
86 return;
88 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
90 SPItem *text = text_or_flowtext_in_selection(selection);
91 SPItem *shape = shape_in_selection(selection);
93 if (!text || !shape || g_slist_length((GSList *) selection->itemList()) != 2) {
94 SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>a text and a path</b> to put text on path."));
95 return;
96 }
98 if (SP_IS_TEXT_TEXTPATH(text)) {
99 SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("This text object is <b>already put on a path</b>. Remove it from the path first. Use <b>Shift+D</b> to look up its path."));
100 return;
101 }
103 if (SP_IS_FLOWTEXT(text)) {
104 SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot put flowtext on a path. Convert flowtext to text first."));
105 return;
106 }
108 if (SP_IS_RECT(shape)) {
109 // rect is the only SPShape which is not <path> yet, and thus SVG forbids us from putting text on it
110 SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot put text on a rectangle in this version. Convert rectangle to path first."));
111 return;
112 }
114 Inkscape::Text::Layout const *layout = te_get_layout(text);
115 Inkscape::Text::Layout::Alignment text_alignment = layout->paragraphAlignment(layout->begin());
117 // remove transform from text, but recursively scale text's fontsize by the expansion
118 SP_TEXT(text)->_adjustFontsizeRecursive (text, NR::expansion(SP_ITEM(text)->transform));
119 SP_OBJECT_REPR(text)->setAttribute("transform", NULL);
121 // make a list of text children
122 GSList *text_reprs = NULL;
123 for (SPObject *o = SP_OBJECT(text)->children; o != NULL; o = o->next) {
124 text_reprs = g_slist_prepend(text_reprs, SP_OBJECT_REPR(o));
125 }
127 // create textPath and put it into the text
128 Inkscape::XML::Node *textpath = sp_repr_new("svg:textPath");
129 // reference the shape
130 textpath->setAttribute("xlink:href", g_strdup_printf("#%s", SP_OBJECT_REPR(shape)->attribute("id")));
131 if (text_alignment == Inkscape::Text::Layout::RIGHT)
132 textpath->setAttribute("startOffset", "100%");
133 else if (text_alignment == Inkscape::Text::Layout::CENTER)
134 textpath->setAttribute("startOffset", "50%");
135 SP_OBJECT_REPR(text)->addChild(textpath, NULL);
137 for ( GSList *i = text_reprs ; i ; i = i->next ) {
138 // make a copy of each text child
139 Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate();
140 // We cannot have multiline in textpath, so remove line attrs from tspans
141 if (!strcmp(copy->name(), "svg:tspan")) {
142 copy->setAttribute("sodipodi:role", NULL);
143 copy->setAttribute("x", NULL);
144 copy->setAttribute("y", NULL);
145 }
146 // remove the old repr from under text
147 SP_OBJECT_REPR(text)->removeChild((Inkscape::XML::Node *) i->data);
148 // put its copy into under textPath
149 textpath->addChild(copy, NULL); // fixme: copy id
150 }
152 // x/y are useless with textpath, and confuse Batik 1.5
153 SP_OBJECT_REPR(text)->setAttribute("x", NULL);
154 SP_OBJECT_REPR(text)->setAttribute("y", NULL);
156 sp_document_done(SP_DT_DOCUMENT(desktop));
157 g_slist_free(text_reprs);
158 }
160 void
161 text_remove_from_path()
162 {
163 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
165 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
167 if (selection->isEmpty()) {
168 SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>a text on path</b> to remove it from path."));
169 return;
170 }
172 bool did = false;
174 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
175 items != NULL;
176 items = items->next) {
178 if (!SP_IS_TEXT_TEXTPATH(SP_OBJECT(items->data))) {
179 continue;
180 }
182 SPObject *tp = sp_object_first_child(SP_OBJECT(items->data));
184 did = true;
186 sp_textpath_to_text(tp);
187 }
189 if (!did) {
190 SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No texts-on-paths</b> in the selection."));
191 } else {
192 selection->setList(g_slist_copy((GSList *) selection->itemList())); // reselect to update statusbar description
193 sp_document_done(SP_DT_DOCUMENT(desktop));
194 }
195 }
197 void
198 text_remove_all_kerns_recursively(SPObject *o)
199 {
200 SP_OBJECT_REPR(o)->setAttribute("dx", NULL);
201 SP_OBJECT_REPR(o)->setAttribute("dy", NULL);
202 SP_OBJECT_REPR(o)->setAttribute("rotate", NULL);
204 for (SPObject *i = sp_object_first_child(o); i != NULL; i = SP_OBJECT_NEXT(i)) {
205 text_remove_all_kerns_recursively(i);
206 }
207 }
209 //FIXME: must work with text selection
210 void
211 text_remove_all_kerns()
212 {
213 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
215 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
217 if (selection->isEmpty()) {
218 SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>text(s)</b> to remove kerns from."));
219 return;
220 }
222 bool did = false;
224 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
225 items != NULL;
226 items = items->next) {
228 if (!SP_IS_TEXT(SP_OBJECT(items->data))) {
229 continue;
230 }
232 text_remove_all_kerns_recursively(SP_OBJECT(items->data));
233 SP_OBJECT(items->data)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
234 did = true;
235 }
237 if (!did) {
238 SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("Select <b>text(s)</b> to remove kerns from."));
239 } else {
240 sp_document_done(SP_DT_DOCUMENT(desktop));
241 }
242 }
244 void
245 text_flow_into_shape()
246 {
247 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
248 if (!desktop)
249 return;
251 SPDocument *doc = SP_DT_DOCUMENT (desktop);
253 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
255 SPItem *text = text_in_selection(selection);
256 SPItem *shape = shape_in_selection(selection);
258 if (!text || !shape || g_slist_length((GSList *) selection->itemList()) < 2) {
259 SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>a text</b> and one or more <b>paths or shapes</b> to flow text into frame."));
260 return;
261 }
263 // remove transform from text, but recursively scale text's fontsize by the expansion
264 SP_TEXT(text)->_adjustFontsizeRecursive(text, NR::expansion(SP_ITEM(text)->transform));
265 SP_OBJECT_REPR(text)->setAttribute("transform", NULL);
267 Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot");
268 root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
269 root_repr->setAttribute("style", SP_OBJECT_REPR(text)->attribute("style")); // fixme: transfer style attrs too
270 SP_OBJECT_REPR(SP_OBJECT_PARENT(shape))->appendChild(root_repr);
271 SPObject *root_object = doc->getObjectByRepr(root_repr);
272 g_return_if_fail(SP_IS_FLOWTEXT(root_object));
274 Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion");
275 root_repr->appendChild(region_repr);
276 SPObject *object = doc->getObjectByRepr(region_repr);
277 g_return_if_fail(SP_IS_FLOWREGION(object));
279 /* Add clones */
280 for (GSList *items = (GSList *) selection->itemList();
281 items != NULL;
282 items = items->next) {
283 SPItem *item = SP_ITEM(items->data);
284 if (SP_IS_SHAPE(item)){
285 Inkscape::XML::Node *clone = sp_repr_new("svg:use");
286 clone->setAttribute("x", "0");
287 clone->setAttribute("y", "0");
288 clone->setAttribute("xlink:href", g_strdup_printf("#%s", SP_OBJECT_REPR(item)->attribute("id")));
290 // add the new clone to the region
291 region_repr->appendChild(clone);
292 }
293 }
295 Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara");
296 root_repr->appendChild(para_repr);
297 object = doc->getObjectByRepr(para_repr);
298 g_return_if_fail(SP_IS_FLOWPARA(object));
300 Inkscape::Text::Layout const *layout = te_get_layout(text);
301 Glib::ustring text_ustring = sp_te_get_string_multiline(text, layout->begin(), layout->end());
303 Inkscape::XML::Node *text_repr = sp_repr_new_text(text_ustring.c_str()); // FIXME: transfer all formatting! and convert newlines into flowParas!
304 para_repr->appendChild(text_repr);
306 SP_OBJECT(text)->deleteObject (true);
308 sp_document_done(doc);
310 SP_DT_SELECTION(desktop)->set(SP_ITEM(root_object));
312 Inkscape::GC::release(root_repr);
313 Inkscape::GC::release(region_repr);
314 Inkscape::GC::release(para_repr);
315 Inkscape::GC::release(text_repr);
316 }
318 void
319 text_unflow ()
320 {
321 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
322 if (!desktop)
323 return;
325 SPDocument *doc = SP_DT_DOCUMENT (desktop);
327 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
330 if (!flowtext_in_selection(selection) || g_slist_length((GSList *) selection->itemList()) < 1) {
331 SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>a flowed text</b> to unflow it."));
332 return;
333 }
335 GSList *new_objs = NULL;
336 GSList *old_objs = NULL;
338 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
339 items != NULL;
340 items = items->next) {
342 if (!SP_IS_FLOWTEXT(SP_OBJECT(items->data))) {
343 continue;
344 }
346 SPItem *flowtext = SP_ITEM(items->data);
348 /* Create <text> */
349 Inkscape::XML::Node *rtext = sp_repr_new("svg:text");
350 rtext->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
352 /* Set style */
353 rtext->setAttribute("style", SP_OBJECT_REPR(flowtext)->attribute("style")); // fixme: transfer style attrs too; and from descendants
355 NRRect bbox;
356 sp_item_invoke_bbox(SP_ITEM(flowtext), &bbox, sp_item_i2doc_affine(SP_ITEM(flowtext)), TRUE);
357 NR::Point xy(bbox.x0, bbox.y0);
358 if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
359 sp_repr_set_svg_double(rtext, "x", xy[NR::X]);
360 sp_repr_set_svg_double(rtext, "y", xy[NR::Y]);
361 }
363 /* Create <tspan> */
364 Inkscape::XML::Node *rtspan = sp_repr_new("svg:tspan");
365 rtspan->setAttribute("sodipodi:role", "line"); // otherwise, why bother creating the tspan?
366 rtext->addChild(rtspan, NULL);
368 gchar *text_string = sp_te_get_string_multiline(flowtext);
369 Inkscape::XML::Node *text_repr = sp_repr_new_text(text_string); // FIXME: transfer all formatting!!!
370 free(text_string);
371 rtspan->appendChild(text_repr);
373 SP_OBJECT_REPR(SP_OBJECT_PARENT(flowtext))->appendChild(rtext);
374 SPObject *text_object = doc->getObjectByRepr(rtext);
376 new_objs = g_slist_prepend (new_objs, text_object);
377 old_objs = g_slist_prepend (old_objs, flowtext);
379 Inkscape::GC::release(rtext);
380 Inkscape::GC::release(rtspan);
381 Inkscape::GC::release(text_repr);
382 }
384 selection->clear();
385 selection->setList(new_objs);
386 for (GSList *i = old_objs; i; i = i->next) {
387 SP_OBJECT(i->data)->deleteObject (true);
388 }
390 g_slist_free (old_objs);
391 g_slist_free (new_objs);
393 sp_document_done(doc);
394 }
398 /*
399 Local Variables:
400 mode:c++
401 c-file-style:"stroustrup"
402 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
403 indent-tabs-mode:nil
404 fill-column:99
405 End:
406 */
407 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :