show an error when something goes wrong at svg parsing
This commit is contained in:
parent
9ff5876a19
commit
8147db004b
3 changed files with 24 additions and 17 deletions
|
@ -1,6 +1,8 @@
|
||||||
package com.simplemobiletools.draw
|
package com.simplemobiletools.draw
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
import android.graphics.Path
|
import android.graphics.Path
|
||||||
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
import com.simplemobiletools.draw.actions.Action
|
import com.simplemobiletools.draw.actions.Action
|
||||||
import com.simplemobiletools.draw.actions.Line
|
import com.simplemobiletools.draw.actions.Line
|
||||||
import com.simplemobiletools.draw.actions.Move
|
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()
|
val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
|
||||||
var i = 0
|
var i = 0
|
||||||
while (i < tokens.size) {
|
try {
|
||||||
when (tokens[i][0]) {
|
while (i < tokens.size) {
|
||||||
'M' -> addAction(Move(tokens[i]))
|
when (tokens[i][0]) {
|
||||||
'L' -> addAction(Line(tokens[i]))
|
'M' -> addAction(Move(tokens[i]))
|
||||||
'Q' -> {
|
'L' -> addAction(Line(tokens[i]))
|
||||||
// Quad actions are of the following form:
|
'Q' -> {
|
||||||
// "Qx1,y1 x2,y2"
|
// Quad actions are of the following form:
|
||||||
// Since we split the tokens by whitespace, we need to join them again
|
// "Qx1,y1 x2,y2"
|
||||||
if (i + 1 >= tokens.size)
|
// Since we split the tokens by whitespace, we need to join them again
|
||||||
throw InvalidParameterException("Error parsing the data for a Quad.")
|
if (i + 1 >= tokens.size)
|
||||||
|
throw InvalidParameterException("Error parsing the data for a Quad.")
|
||||||
|
|
||||||
addAction(Quad(tokens[i] + " " + tokens[i + 1]))
|
addAction(Quad(tokens[i] + " " + tokens[i + 1]))
|
||||||
++i
|
++i
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
++i
|
||||||
}
|
}
|
||||||
++i
|
} catch (e: Exception) {
|
||||||
|
activity.toast(R.string.unknown_error_occurred)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.simplemobiletools.draw
|
package com.simplemobiletools.draw
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.drawable.ColorDrawable
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.sax.RootElement
|
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)
|
val svg = parseSvg(file)
|
||||||
|
|
||||||
canvas.clearCanvas()
|
canvas.clearCanvas()
|
||||||
|
@ -53,7 +54,7 @@ object Svg {
|
||||||
|
|
||||||
svg.paths.forEach {
|
svg.paths.forEach {
|
||||||
val path = MyPath()
|
val path = MyPath()
|
||||||
path.readObject(it.data)
|
path.readObject(it.data, activity)
|
||||||
val options = PaintOptions(it.color, it.strokeWidth)
|
val options = PaintOptions(it.color, it.strokeWidth)
|
||||||
|
|
||||||
canvas.addPath(path, options)
|
canvas.addPath(path, options)
|
||||||
|
|
|
@ -110,7 +110,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
|
||||||
private fun openFile() {
|
private fun openFile() {
|
||||||
FilePickerDialog(this, curPath) {
|
FilePickerDialog(this, curPath) {
|
||||||
if (it.endsWith(".svg")) {
|
if (it.endsWith(".svg")) {
|
||||||
Svg.loadSvg(File(it), my_canvas)
|
Svg.loadSvg(this, File(it), my_canvas)
|
||||||
} else if (File(it).isImageSlow()) {
|
} else if (File(it).isImageSlow()) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue