X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ftext-chemistry.cpp;h=79d5e3a2dc390d5a62068ea6b80c0db2beaff839;hb=129958c3b0e17dc15a5a4722ee83a89a780f9a57;hp=a61bbdf737a6314e78aa7a4f96dc017e8a9c85ea;hpb=6b15695578f07a3f72c4c9475c1a261a3021472a;p=inkscape.git diff --git a/src/text-chemistry.cpp b/src/text-chemistry.cpp index a61bbdf73..79d5e3a2d 100644 --- a/src/text-chemistry.cpp +++ b/src/text-chemistry.cpp @@ -20,11 +20,14 @@ #include "sp-rect.h" #include "sp-textpath.h" #include "inkscape.h" +#include "desktop.h" #include "document.h" #include "message-stack.h" #include "selection.h" +#include "style.h" #include "desktop-handles.h" #include "text-editing.h" +#include "text-chemistry.h" #include "sp-flowtext.h" #include "sp-flowregion.h" #include "sp-flowdiv.h" @@ -54,6 +57,18 @@ flowtext_in_selection(Inkscape::Selection *selection) return NULL; } +SPItem * +text_or_flowtext_in_selection(Inkscape::Selection *selection) +{ + for (GSList *items = (GSList *) selection->itemList(); + items != NULL; + items = items->next) { + if (SP_IS_TEXT(items->data) || SP_IS_FLOWTEXT(items->data)) + return ((SPItem *) items->data); + } + return NULL; +} + SPItem * shape_in_selection(Inkscape::Selection *selection) { @@ -73,27 +88,59 @@ text_put_on_path() if (!desktop) return; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); - SPItem *text = text_in_selection(selection); + SPItem *text = text_or_flowtext_in_selection(selection); SPItem *shape = shape_in_selection(selection); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + if (!text || !shape || g_slist_length((GSList *) selection->itemList()) != 2) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text and a path to put text on path.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text and a path to put text on path.")); return; } if (SP_IS_TEXT_TEXTPATH(text)) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("This text object is already put to a path. Remove it from the path first. Use Shift+D to look up its path.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("This text object is already put on a path. Remove it from the path first. Use Shift+D to look up its path.")); return; } if (SP_IS_RECT(shape)) { // rect is the only SPShape which is not yet, and thus SVG forbids us from putting text on it - SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot put text on a rectangle in this version. Convert rectangle to path first.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot put text on a rectangle in this version. Convert rectangle to path first.")); return; } + // if a flowed text is selected, convert it to a regular text object + if (SP_IS_FLOWTEXT(text)) { + + if (!SP_FLOWTEXT(text)->layout.outputExists()) { + sp_desktop_message_stack(desktop)-> + flash(Inkscape::WARNING_MESSAGE, + _("The flowed text(s) must be visible in order to be put on a path.")); + } + + Inkscape::XML::Node *repr = SP_FLOWTEXT(text)->getAsText(); + + if (!repr) return; + + Inkscape::XML::Node *parent = SP_OBJECT_REPR(text)->parent(); + parent->appendChild(repr); + + SPItem *new_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); + sp_item_write_transform(new_item, repr, text->transform); + SP_OBJECT(new_item)->updateRepr(); + + Inkscape::GC::release(repr); + text->deleteObject(); // delete the orignal flowtext + + sp_document_ensure_up_to_date(sp_desktop_document(desktop)); + + selection->clear(); + + text = new_item; // point to the new text + } + Inkscape::Text::Layout const *layout = te_get_layout(text); Inkscape::Text::Layout::Alignment text_alignment = layout->paragraphAlignment(layout->begin()); @@ -108,7 +155,7 @@ text_put_on_path() } // create textPath and put it into the text - Inkscape::XML::Node *textpath = sp_repr_new("svg:textPath"); + Inkscape::XML::Node *textpath = xml_doc->createElement("svg:textPath"); // reference the shape textpath->setAttribute("xlink:href", g_strdup_printf("#%s", SP_OBJECT_REPR(shape)->attribute("id"))); if (text_alignment == Inkscape::Text::Layout::RIGHT) @@ -136,7 +183,8 @@ text_put_on_path() SP_OBJECT_REPR(text)->setAttribute("x", NULL); SP_OBJECT_REPR(text)->setAttribute("y", NULL); - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT, + _("Put text on path")); g_slist_free(text_reprs); } @@ -145,10 +193,10 @@ text_remove_from_path() { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text on path to remove it from path.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text on path to remove it from path.")); return; } @@ -170,10 +218,11 @@ text_remove_from_path() } if (!did) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("No texts-on-paths in the selection.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("No texts-on-paths in the selection.")); } else { selection->setList(g_slist_copy((GSList *) selection->itemList())); // reselect to update statusbar description - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT, + _("Remove text from path")); } } @@ -195,10 +244,10 @@ text_remove_all_kerns() { SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select text(s) to remove kerns from.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select text(s) to remove kerns from.")); return; } @@ -218,9 +267,10 @@ text_remove_all_kerns() } if (!did) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, _("Select text(s) to remove kerns from.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("Select text(s) to remove kerns from.")); } else { - sp_document_done(SP_DT_DOCUMENT(desktop)); + sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT, + _("Remove manual kerns")); } } @@ -231,30 +281,33 @@ text_flow_into_shape() if (!desktop) return; - SPDocument *doc = SP_DT_DOCUMENT (desktop); + SPDocument *doc = sp_desktop_document (desktop); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc); - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); - SPItem *text = text_in_selection(selection); + SPItem *text = text_or_flowtext_in_selection(selection); SPItem *shape = shape_in_selection(selection); if (!text || !shape || g_slist_length((GSList *) selection->itemList()) < 2) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text and one or more paths or shapes to flow text into frame.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a text and one or more paths or shapes to flow text into frame.")); return; } - // remove transform from text, but recursively scale text's fontsize by the expansion - SP_TEXT(text)->_adjustFontsizeRecursive(text, NR::expansion(SP_ITEM(text)->transform)); - SP_OBJECT_REPR(text)->setAttribute("transform", NULL); + if (SP_IS_TEXT(text)) { + // remove transform from text, but recursively scale text's fontsize by the expansion + SP_TEXT(text)->_adjustFontsizeRecursive(text, NR::expansion(SP_ITEM(text)->transform)); + SP_OBJECT_REPR(text)->setAttribute("transform", NULL); + } - Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot"); + Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot"); root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create root_repr->setAttribute("style", SP_OBJECT_REPR(text)->attribute("style")); // fixme: transfer style attrs too SP_OBJECT_REPR(SP_OBJECT_PARENT(shape))->appendChild(root_repr); SPObject *root_object = doc->getObjectByRepr(root_repr); g_return_if_fail(SP_IS_FLOWTEXT(root_object)); - Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion"); + Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion"); root_repr->appendChild(region_repr); SPObject *object = doc->getObjectByRepr(region_repr); g_return_if_fail(SP_IS_FLOWREGION(object)); @@ -265,7 +318,7 @@ text_flow_into_shape() items = items->next) { SPItem *item = SP_ITEM(items->data); if (SP_IS_SHAPE(item)){ - Inkscape::XML::Node *clone = sp_repr_new("svg:use"); + Inkscape::XML::Node *clone = xml_doc->createElement("svg:use"); clone->setAttribute("x", "0"); clone->setAttribute("y", "0"); clone->setAttribute("xlink:href", g_strdup_printf("#%s", SP_OBJECT_REPR(item)->attribute("id"))); @@ -275,27 +328,42 @@ text_flow_into_shape() } } - Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara"); - root_repr->appendChild(para_repr); - object = doc->getObjectByRepr(para_repr); - g_return_if_fail(SP_IS_FLOWPARA(object)); + if (SP_IS_TEXT(text)) { // flow from text, as string + Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara"); + root_repr->appendChild(para_repr); + object = doc->getObjectByRepr(para_repr); + g_return_if_fail(SP_IS_FLOWPARA(object)); - Inkscape::Text::Layout const *layout = te_get_layout(text); - Glib::ustring text_ustring = sp_te_get_string_multiline(text, layout->begin(), layout->end()); + Inkscape::Text::Layout const *layout = te_get_layout(text); + Glib::ustring text_ustring = sp_te_get_string_multiline(text, layout->begin(), layout->end()); + + Inkscape::XML::Node *text_repr = xml_doc->createTextNode(text_ustring.c_str()); // FIXME: transfer all formatting! and convert newlines into flowParas! + para_repr->appendChild(text_repr); - Inkscape::XML::Node *text_repr = sp_repr_new_text(text_ustring.c_str()); // FIXME: transfer all formatting! and convert newlines into flowParas! - para_repr->appendChild(text_repr); + Inkscape::GC::release(para_repr); + Inkscape::GC::release(text_repr); + + } else { // reflow an already flowed text, preserving paras + for (SPObject *o = SP_OBJECT(text)->children; o != NULL; o = o->next) { + if (SP_IS_FLOWPARA(o)) { + Inkscape::XML::Node *para_repr = SP_OBJECT_REPR(o)->duplicate(); + root_repr->appendChild(para_repr); + object = doc->getObjectByRepr(para_repr); + g_return_if_fail(SP_IS_FLOWPARA(object)); + Inkscape::GC::release(para_repr); + } + } + } SP_OBJECT(text)->deleteObject (true); - sp_document_done(doc); + sp_document_done(doc, SP_VERB_CONTEXT_TEXT, + _("Flow text into shape")); - SP_DT_SELECTION(desktop)->set(SP_ITEM(root_object)); + sp_desktop_selection(desktop)->set(SP_ITEM(root_object)); Inkscape::GC::release(root_repr); Inkscape::GC::release(region_repr); - Inkscape::GC::release(para_repr); - Inkscape::GC::release(text_repr); } void @@ -305,13 +373,14 @@ text_unflow () if (!desktop) return; - SPDocument *doc = SP_DT_DOCUMENT (desktop); + SPDocument *doc = sp_desktop_document (desktop); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc); - Inkscape::Selection *selection = SP_DT_SELECTION(desktop); + Inkscape::Selection *selection = sp_desktop_selection(desktop); if (!flowtext_in_selection(selection) || g_slist_length((GSList *) selection->itemList()) < 1) { - SP_DT_MSGSTACK(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a flowed text to unflow it.")); + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select a flowed text to unflow it.")); return; } @@ -328,8 +397,12 @@ text_unflow () SPItem *flowtext = SP_ITEM(items->data); + if (sp_te_get_string_multiline(flowtext) == NULL) { // flowtext is empty + continue; + } + /* Create */ - Inkscape::XML::Node *rtext = sp_repr_new("svg:text"); + Inkscape::XML::Node *rtext = xml_doc->createElement("svg:text"); rtext->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create /* Set style */ @@ -344,12 +417,12 @@ text_unflow () } /* Create */ - Inkscape::XML::Node *rtspan = sp_repr_new("svg:tspan"); + Inkscape::XML::Node *rtspan = xml_doc->createElement("svg:tspan"); rtspan->setAttribute("sodipodi:role", "line"); // otherwise, why bother creating the tspan? rtext->addChild(rtspan, NULL); gchar *text_string = sp_te_get_string_multiline(flowtext); - Inkscape::XML::Node *text_repr = sp_repr_new_text(text_string); // FIXME: transfer all formatting!!! + Inkscape::XML::Node *text_repr = xml_doc->createTextNode(text_string); // FIXME: transfer all formatting!!! free(text_string); rtspan->appendChild(text_repr); @@ -373,9 +446,75 @@ text_unflow () g_slist_free (old_objs); g_slist_free (new_objs); - sp_document_done(doc); + sp_document_done(doc, SP_VERB_CONTEXT_TEXT, + _("Unflow flowed text")); } +void +flowtext_to_text() +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + + Inkscape::Selection *selection = sp_desktop_selection(desktop); + + if (selection->isEmpty()) { + sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, + _("Select flowed text(s) to convert.")); + return; + } + + bool did = false; + + GSList *reprs = NULL; + GSList *items = g_slist_copy((GSList *) selection->itemList()); + for (; items != NULL; items = items->next) { + + SPItem *item = (SPItem *) items->data; + + if (!SP_IS_FLOWTEXT(item)) + continue; + + if (!SP_FLOWTEXT(item)->layout.outputExists()) { + sp_desktop_message_stack(desktop)-> + flash(Inkscape::WARNING_MESSAGE, + _("The flowed text(s) must be visible in order to be converted.")); + return; + } + + Inkscape::XML::Node *repr = SP_FLOWTEXT(item)->getAsText(); + + if (!repr) break; + + did = true; + + Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent(); + parent->appendChild(repr); + + SPItem *new_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr); + sp_item_write_transform(new_item, repr, item->transform); + SP_OBJECT(new_item)->updateRepr(); + + Inkscape::GC::release(repr); + item->deleteObject(); + + reprs = g_slist_prepend(reprs, repr); + } + + g_slist_free(items); + + if (did) { + sp_document_done(sp_desktop_document(desktop), + SP_VERB_OBJECT_FLOWTEXT_TO_TEXT, + _("Convert flowed text to text")); + selection->setReprList(reprs); + } else { + sp_desktop_message_stack(desktop)-> + flash(Inkscape::ERROR_MESSAGE, + _("No flowed text(s) to convert in the selection.")); + } + + g_slist_free(reprs); +} /*