show an error when something goes wrong at svg parsing

This commit is contained in:
tibbi 2017-04-09 10:53:46 +02:00
parent 9ff5876a19
commit 8147db004b
3 changed files with 24 additions and 17 deletions

View file

@ -1,6 +1,8 @@
package com.simplemobiletools.draw
import android.app.Activity
import android.graphics.Path
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.draw.actions.Action
import com.simplemobiletools.draw.actions.Line
import com.simplemobiletools.draw.actions.Move
@ -23,25 +25,29 @@ class MyPath : Path(), Serializable {
}
}
fun readObject(pathData: String) {
fun readObject(pathData: String, activity: Activity) {
val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
var i = 0
while (i < tokens.size) {
when (tokens[i][0]) {
'M' -> addAction(Move(tokens[i]))
'L' -> addAction(Line(tokens[i]))
'Q' -> {
// Quad actions are of the following form:
// "Qx1,y1 x2,y2"
// Since we split the tokens by whitespace, we need to join them again
if (i + 1 >= tokens.size)
throw InvalidParameterException("Error parsing the data for a Quad.")
try {
while (i < tokens.size) {
when (tokens[i][0]) {
'M' -> addAction(Move(tokens[i]))
'L' -> addAction(Line(tokens[i]))
'Q' -> {
// Quad actions are of the following form:
// "Qx1,y1 x2,y2"
// Since we split the tokens by whitespace, we need to join them again
if (i + 1 >= tokens.size)
throw InvalidParameterException("Error parsing the data for a Quad.")
addAction(Quad(tokens[i] + " " + tokens[i + 1]))
++i
addAction(Quad(tokens[i] + " " + tokens[i + 1]))
++i
}
}
++i
}
++i
} catch (e: Exception) {
activity.toast(R.string.unknown_error_occurred)
}
}

View file

@ -1,5 +1,6 @@
package com.simplemobiletools.draw
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.sax.RootElement
@ -45,7 +46,7 @@ object Svg {
}
}
fun loadSvg(file: File, canvas: MyCanvas) {
fun loadSvg(activity: Activity, file: File, canvas: MyCanvas) {
val svg = parseSvg(file)
canvas.clearCanvas()
@ -53,7 +54,7 @@ object Svg {
svg.paths.forEach {
val path = MyPath()
path.readObject(it.data)
path.readObject(it.data, activity)
val options = PaintOptions(it.color, it.strokeWidth)
canvas.addPath(path, options)

View file

@ -110,7 +110,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private fun openFile() {
FilePickerDialog(this, curPath) {
if (it.endsWith(".svg")) {
Svg.loadSvg(File(it), my_canvas)
Svg.loadSvg(this, File(it), my_canvas)
} else if (File(it).isImageSlow()) {
} else {