sakshigarg2802 / androidsvg

Automatically exported from code.google.com/p/androidsvg
0 stars 0 forks source link

Feature request: replace text #47

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Along similar lines to the color replacement request, is there a chance of 
adding the ability to replace the contents of  one or more text elements, 
preferably by an ID match, but a regexp based search and replace may also be 
useful e.g.

svg.getTextElementById(id).setValue(string);
svg.getTextElementByValue(regexp).setValue(string);

Original issue reported on code.google.com by arobe...@arcalot.net on 15 Sep 2014 at 8:54

GoogleCodeExporter commented 8 years ago
My intention at some point is to support the SVG DOM.  You will then be able to 
manipulate any part of the SVG before rendering.

Original comment by paul.leb...@gmail.com on 15 Sep 2014 at 11:59

GoogleCodeExporter commented 8 years ago
Thanks, will look forward to having a play.

As a stopgap I've just made a quick fork / hack, so the likes of:
getElementById(String) and getElementsByTagName(Class), and TextSequence()
are now public and a: setTextSequenceById(String, String) has magically
appeared. A bit of a frig as even for my limited requirements.

Oh spotted what appears to be a typo in getElementsByTagName, shouldn't the
recursive call be prefixed by a: result.addAll( )   e.g.

   private List<SvgObject>  getElementsByTagName(SvgContainer obj, Class
clazz)
   {

...

         if (child instanceof SvgContainer)
             result.addAll(getElementsByTagName((SvgContainer) child,
clazz));
             ^^^^^^^^^^^^^
      }
      return result;
   }

Original comment by arobe...@arcalot.net on 15 Sep 2014 at 3:49

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Thanks for catching that bug!  I'll fix it for the next release.

Original comment by paul.leb...@gmail.com on 15 Sep 2014 at 5:49

GoogleCodeExporter commented 8 years ago
FYI / to comply with the Licence, I've created a custom version of SVG.java 
that includes the bugfix above and adds the following two functions:

public int setTextById(String id, String newText) {
        int updated = 0;

        if (id == null || id.length() == 0 || newText == null)
            return updated;

        SvgObject idMatch = getElementById(id);

        if ( idMatch != null) {
            List<SvgObject> allText = getElementsByTagName((SvgContainer) idMatch, TextSequence.class);

            //Should only find one child element, but to be safe
            for (SvgObject textSeq : allText) {
                ((TextSequence) textSeq).text = newText;
                updated++;
            }
        }
        return updated;
    }

    public int replaceTextById(String id, String oldText, String newText) {
        int updated = 0;

        if (id == null || id.length() == 0 || newText == null || oldText == null)
            return updated;

        SvgObject idMatch = getElementById(id);

        if ( idMatch != null) {
            List<SvgObject> allText = getElementsByTagName((SvgContainer) idMatch, TextSequence.class);

            //Should only find one child element, but to be safe
            for (SvgObject textSeq : allText) {
                ((TextSequence) textSeq).text = ((TextSequence) textSeq).text.replace(oldText, newText);
                updated++;
            }
        }
        return updated;
    }

Original comment by arobe...@arcalot.net on 7 Oct 2014 at 8:41