mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
converting a couple more classes to kotlin
This commit is contained in:
@ -0,0 +1,15 @@
|
||||
package com.simplemobiletools.draw
|
||||
|
||||
import android.graphics.Color
|
||||
|
||||
internal class PaintOptions {
|
||||
var color = Color.BLACK
|
||||
var strokeWidth = 5f
|
||||
|
||||
constructor()
|
||||
|
||||
constructor(color: Int, strokeWidth: Float) {
|
||||
this.color = color
|
||||
this.strokeWidth = strokeWidth
|
||||
}
|
||||
}
|
133
app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt
Normal file
133
app/src/main/kotlin/com/simplemobiletools/draw/Svg.kt
Normal file
@ -0,0 +1,133 @@
|
||||
package com.simplemobiletools.draw
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.sax.RootElement
|
||||
import android.util.Xml
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
object Svg {
|
||||
@Throws(Exception::class)
|
||||
fun saveSvg(output: File, canvas: MyCanvas) {
|
||||
val backgroundColor = (canvas.background as ColorDrawable).color
|
||||
|
||||
val out = FileOutputStream(output)
|
||||
val writer = BufferedWriter(OutputStreamWriter(out))
|
||||
writeSvg(writer, backgroundColor, canvas.paths, canvas.width, canvas.height)
|
||||
writer.close()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writeSvg(writer: Writer, backgroundColor: Int, paths: Map<MyPath, PaintOptions>, width: Int, height: Int) {
|
||||
writer.write("<svg width=\"")
|
||||
writer.write(width.toString())
|
||||
writer.write("\" height=\"")
|
||||
writer.write(height.toString())
|
||||
writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">")
|
||||
|
||||
// background rect
|
||||
writer.write("<rect width=\"")
|
||||
writer.write(width.toString())
|
||||
writer.write("\" height=\"")
|
||||
writer.write(height.toString())
|
||||
writer.write("\" fill=\"#")
|
||||
writer.write(Integer.toHexString(backgroundColor).substring(2)) // Skip the alpha FF
|
||||
writer.write("\"/>")
|
||||
|
||||
for ((key, value) in paths) {
|
||||
writePath(writer, key, value)
|
||||
}
|
||||
writer.write("</svg>")
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun writePath(writer: Writer, path: MyPath, options: PaintOptions) {
|
||||
writer.write("<path d=\"")
|
||||
for (action in path.getActions()) {
|
||||
action.perform(writer)
|
||||
writer.write(" ")
|
||||
}
|
||||
|
||||
writer.write("\" fill=\"none\" stroke=\"#")
|
||||
writer.write(Integer.toHexString(options.color).substring(2)) // Skip the alpha FF
|
||||
writer.write("\" stroke-width=\"")
|
||||
writer.write(options.strokeWidth.toString())
|
||||
writer.write("\" stroke-linecap=\"round\"/>")
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun loadSvg(file: File, canvas: MyCanvas) {
|
||||
val svg = parseSvg(file)
|
||||
|
||||
canvas.clearCanvas()
|
||||
canvas.setBackgroundColor(svg.background!!.color)
|
||||
|
||||
for (sp in svg.paths) {
|
||||
val path = MyPath()
|
||||
path.readObject(sp.data)
|
||||
val options = PaintOptions(sp.color, sp.strokeWidth)
|
||||
|
||||
canvas.addPath(path, options)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun parseSvg(file: File): SSvg {
|
||||
var inputStream: InputStream? = null
|
||||
val svg = SSvg()
|
||||
try {
|
||||
inputStream = FileInputStream(file)
|
||||
|
||||
// Actual parsing (http://stackoverflow.com/a/4828765)
|
||||
val ns = "http://www.w3.org/2000/svg"
|
||||
val root = RootElement(ns, "svg")
|
||||
val rectElement = root.getChild(ns, "rect")
|
||||
val pathElement = root.getChild(ns, "path")
|
||||
|
||||
root.setStartElementListener { attributes ->
|
||||
val width = Integer.parseInt(attributes.getValue("width"))
|
||||
val height = Integer.parseInt(attributes.getValue("height"))
|
||||
svg.setSize(width, height)
|
||||
}
|
||||
|
||||
rectElement.setStartElementListener { attributes ->
|
||||
val width = Integer.parseInt(attributes.getValue("width"))
|
||||
val height = Integer.parseInt(attributes.getValue("height"))
|
||||
val color = Color.parseColor(attributes.getValue("fill"))
|
||||
if (svg.background != null)
|
||||
throw UnsupportedOperationException("Unsupported SVG, should only have one <rect>.")
|
||||
|
||||
svg.background = SRect(width, height, color)
|
||||
}
|
||||
|
||||
pathElement.setStartElementListener { attributes ->
|
||||
val d = attributes.getValue("d")
|
||||
val color = Color.parseColor(attributes.getValue("stroke"))
|
||||
val width = java.lang.Float.parseFloat(attributes.getValue("stroke-width"))
|
||||
svg.paths.add(SPath(d, color, width))
|
||||
}
|
||||
|
||||
Xml.parse(inputStream, Xml.Encoding.UTF_8, root.contentHandler)
|
||||
} finally {
|
||||
inputStream?.close()
|
||||
}
|
||||
return svg
|
||||
}
|
||||
|
||||
private class SSvg internal constructor() : Serializable {
|
||||
internal var width: Int = 0
|
||||
internal var height: Int = 0
|
||||
internal var background: SRect? = null
|
||||
internal val paths: ArrayList<SPath> = ArrayList()
|
||||
|
||||
internal fun setSize(w: Int, h: Int) {
|
||||
width = w
|
||||
height = h
|
||||
}
|
||||
}
|
||||
|
||||
private class SRect internal constructor(internal val width: Int, internal val height: Int, internal val color: Int) : Serializable
|
||||
|
||||
private class SPath internal constructor(internal var data: String, internal var color: Int, internal var strokeWidth: Float) : Serializable
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.simplemobiletools.draw.actions
|
||||
|
||||
import android.graphics.Path
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.Serializable
|
||||
import java.io.Writer
|
||||
|
||||
interface Action : Serializable {
|
||||
fun perform(path: Path)
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun perform(writer: Writer)
|
||||
}
|
Reference in New Issue
Block a user