Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/main/java/com/larvalabs/svgandroid/SVGParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ static class SVGHandler extends DefaultHandler {

Paint textPaint;
boolean drawCharacters;
// See http://stackoverflow.com/questions/4567636/java-sax-parser-split-calls-to-characters
StringBuilder textBuilder;
Float textX;
Float textY;
int newLineCount;
Expand Down Expand Up @@ -1443,8 +1445,9 @@ public void startElement(String namespaceURI, String localName, String qName, At
}
this.newLineCount = 0;
textPaint.setTextSize(textSize);
canvas.save();
popTransform();
canvas.save();
textBuilder = new StringBuilder();
}
} else if (!hidden && (localName.equals("circle") || localName.equals("ellipse"))) {
Float centerX, centerY, radiusX, radiusY;
Expand Down Expand Up @@ -1528,24 +1531,9 @@ public LayerAttributes currentLayerAttributes() {
@Override
public void characters(char ch[], int start, int length) {
if (this.drawCharacters) {
if (length == 1 && ch[0] == '\n') {
canvas.restore();
canvas.save();

newLineCount += 1;
canvas.translate(0, newLineCount * textSize);
} else {
String text = new String(ch, start, length);
if (this.textX != null && this.textY != null) {
canvas.drawText(text, this.textX, this.textY, textPaint);
} else {
canvas.setMatrix(font_matrix);
canvas.drawText(text, 0, 0, textPaint);
}
Float delta = textPaint.measureText(text);

canvas.translate(delta, 0);
}
String text = new String(ch, start, length);
textBuilder.append(text);
return;
}
}

Expand Down Expand Up @@ -1595,10 +1583,35 @@ public void endElement(String namespaceURI, String localName, String qName) thro
}
} else if (localName.equals("text")) {
if (this.drawCharacters) {
this.drawCharacters = false;
String text = getFullText();
if (text.length() == 1 && text.equals("\n")) {
newLineCount += 1;
canvas.translate(0, newLineCount * textSize);
} else {
if (this.textX != null && this.textY != null) {
canvas.drawText(text, this.textX, this.textY, textPaint);
} else {
canvas.concat(font_matrix);
canvas.drawText(text, 0, 0, textPaint);
}
}
canvas.restore();
textBuilder = null;
this.drawCharacters = false;
}
}
}

/**
* Return full text value for 'text' element.
* You can override this method if you want replace text before drawing.
* See <a href="http://stackoverflow.com/questions/4567636/java-sax-parser-split-calls-to-characters">here</a> for details.
*/
protected String getFullText() {
if (textBuilder == null) {
throw new IllegalStateException("Must be called only if current element is 'text' and when the value is fully parsed!");
}
return textBuilder.toString();
}
}
}