mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Update
This commit is contained in:
@@ -47,4 +47,6 @@ interface Canvas {
|
||||
swipeAngle: Double)
|
||||
fun fillCircle(centerX: Double, centerY: Double, radius: Double)
|
||||
fun setTextAlign(align: TextAlign)
|
||||
}
|
||||
fun toImage(): Image
|
||||
}
|
||||
|
||||
|
||||
64
core/src/main/common/org/isoron/platform/gui/Image.kt
Normal file
64
core/src/main/common/org/isoron/platform/gui/Image.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.gui
|
||||
|
||||
import kotlin.math.*
|
||||
|
||||
interface Image {
|
||||
val width: Int
|
||||
val height: Int
|
||||
|
||||
fun getPixel(x: Int, y: Int): Color
|
||||
fun setPixel(x: Int, y: Int, color: Color)
|
||||
|
||||
suspend fun export(path: String)
|
||||
|
||||
fun diff(other: Image) {
|
||||
if (width != other.width) error("Width must match")
|
||||
if (height != other.height) error("Height must match")
|
||||
|
||||
for (x in 0 until width) {
|
||||
for (y in 0 until height) {
|
||||
val p1 = getPixel(x, y)
|
||||
var l = 1.0
|
||||
for (dx in -2..2) {
|
||||
if (x + dx < 0 || x + dx >= width) continue
|
||||
for (dy in -2..2) {
|
||||
if (y + dy < 0 || y + dy >= height) continue
|
||||
val p2 = other.getPixel(x + dx, y + dy)
|
||||
l = min(l, abs(p1.luminosity - p2.luminosity))
|
||||
}
|
||||
}
|
||||
setPixel(x, y, Color(l, l, l, 1.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val averageLuminosity: Double
|
||||
get() {
|
||||
var luminosity = 0.0
|
||||
for (x in 0 until width) {
|
||||
for (y in 0 until height) {
|
||||
luminosity += getPixel(x, y).luminosity
|
||||
}
|
||||
}
|
||||
return luminosity / (width * height)
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
|
||||
interface FileOpener {
|
||||
/**
|
||||
* Opens a file which was shipped bundled with the application, such as a
|
||||
@@ -88,4 +90,9 @@ interface ResourceFile {
|
||||
* Returns true if the file exists.
|
||||
*/
|
||||
suspend fun exists(): Boolean
|
||||
|
||||
/**
|
||||
* Loads resource file as an image.
|
||||
*/
|
||||
suspend fun toImage(): Image
|
||||
}
|
||||
@@ -28,8 +28,7 @@ val Color.uicolor: UIColor
|
||||
val Color.cgcolor: CGColorRef?
|
||||
get() = uicolor.CGColor
|
||||
|
||||
class IosCanvas() : Canvas {
|
||||
val ctx = UIGraphicsGetCurrentContext()
|
||||
class IosCanvas(val ctx: CGContextRef) : Canvas {
|
||||
var textColor = UIColor.blackColor
|
||||
|
||||
override fun setColor(color: Color) {
|
||||
@@ -79,4 +78,8 @@ class IosCanvas() : Canvas {
|
||||
|
||||
override fun setTextAlign(align: TextAlign) {
|
||||
}
|
||||
|
||||
override fun toImage(): Image {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import platform.Foundation.*
|
||||
|
||||
class IosFileOpener : FileOpener {
|
||||
@@ -54,6 +55,11 @@ class IosFile(val path: String) : UserFile, ResourceFile {
|
||||
}
|
||||
|
||||
override suspend fun copyTo(dest: UserFile) {
|
||||
NSFileManager.defaultManager.copyItemAtPath(path, (dest as IosFile).path, null)
|
||||
val manager = NSFileManager.defaultManager
|
||||
manager.copyItemAtPath(path, (dest as IosFile).path, null)
|
||||
}
|
||||
|
||||
override suspend fun toImage(): Image {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import kotlin.math.*
|
||||
class JsCanvas(val element: HTMLCanvasElement,
|
||||
val pixelScale: Double) : Canvas {
|
||||
|
||||
|
||||
val ctx = element.getContext("2d") as CanvasRenderingContext2D
|
||||
var fontSize = 12.0
|
||||
var fontFamily = "NotoRegular"
|
||||
@@ -57,7 +58,7 @@ class JsCanvas(val element: HTMLCanvasElement,
|
||||
ctx.font = "${fontSize}px ${fontFamily}"
|
||||
ctx.textAlign = align
|
||||
ctx.textBaseline = CanvasTextBaseline.MIDDLE
|
||||
ctx.fillText(text, toPixel(x), toPixel(y + fontSize * 0.05))
|
||||
ctx.fillText(text, toPixel(x), toPixel(y + fontSize * 0.025))
|
||||
}
|
||||
|
||||
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
||||
@@ -83,7 +84,7 @@ class JsCanvas(val element: HTMLCanvasElement,
|
||||
}
|
||||
|
||||
override fun setFont(font: Font) {
|
||||
fontFamily = when(font) {
|
||||
fontFamily = when (font) {
|
||||
Font.REGULAR -> "NotoRegular"
|
||||
Font.BOLD -> "NotoBold"
|
||||
Font.FONT_AWESOME -> "FontAwesome"
|
||||
@@ -125,21 +126,18 @@ class JsCanvas(val element: HTMLCanvasElement,
|
||||
}
|
||||
|
||||
override fun setTextAlign(align: TextAlign) {
|
||||
this.align = when(align) {
|
||||
this.align = when (align) {
|
||||
TextAlign.LEFT -> CanvasTextAlign.LEFT
|
||||
TextAlign.CENTER -> CanvasTextAlign.CENTER
|
||||
TextAlign.RIGHT -> CanvasTextAlign.RIGHT
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun loadImage(src: String) {
|
||||
Promise<Int> { resolve, reject ->
|
||||
val img = Image()
|
||||
img.onload = {
|
||||
ctx.drawImage(img, 0.0, 0.0)
|
||||
resolve(0)
|
||||
}
|
||||
img.src = src
|
||||
}.await()
|
||||
override fun toImage(): Image {
|
||||
return JsImage(this,
|
||||
ctx.getImageData(0.0,
|
||||
0.0,
|
||||
element.width.toDouble(),
|
||||
element.height.toDouble()))
|
||||
}
|
||||
}
|
||||
72
core/src/main/js/org/isoron/platform/gui/JsImage.kt
Normal file
72
core/src/main/js/org/isoron/platform/gui/JsImage.kt
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.gui
|
||||
|
||||
import org.khronos.webgl.*
|
||||
import org.w3c.dom.*
|
||||
import kotlin.browser.*
|
||||
import kotlin.math.*
|
||||
|
||||
class JsImage(val canvas: JsCanvas,
|
||||
val imageData: ImageData) : Image {
|
||||
|
||||
override val width: Int
|
||||
get() = imageData.width
|
||||
|
||||
override val height: Int
|
||||
get() = imageData.height
|
||||
|
||||
val pixels = imageData.unsafeCast<Uint16Array>()
|
||||
|
||||
init {
|
||||
console.log(width, height, imageData.data.length)
|
||||
}
|
||||
|
||||
override suspend fun export(path: String) {
|
||||
canvas.ctx.putImageData(imageData, 0.0, 0.0)
|
||||
val container = document.createElement("div")
|
||||
container.className = "export"
|
||||
val title = document.createElement("div")
|
||||
title.innerHTML = path
|
||||
document.body?.appendChild(container)
|
||||
container.appendChild(title)
|
||||
container.appendChild(canvas.element)
|
||||
}
|
||||
|
||||
override fun getPixel(x: Int, y: Int): Color {
|
||||
val offset = 4 * (y * width + x)
|
||||
return Color(imageData.data[offset + 0] / 255.0,
|
||||
imageData.data[offset + 1] / 255.0,
|
||||
imageData.data[offset + 2] / 255.0,
|
||||
imageData.data[offset + 3] / 255.0)
|
||||
}
|
||||
|
||||
override fun setPixel(x: Int, y: Int, color: Color) {
|
||||
val offset = 4 * (y * width + x)
|
||||
inline fun map(x: Double): Byte {
|
||||
return (x * 255).roundToInt().unsafeCast<Byte>()
|
||||
}
|
||||
imageData.data.set(offset + 0, map(color.red))
|
||||
imageData.data.set(offset + 1, map(color.green))
|
||||
imageData.data.set(offset + 2, map(color.blue))
|
||||
imageData.data.set(offset + 3, map(color.alpha))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,11 @@
|
||||
package org.isoron.platform.io
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.gui.Image
|
||||
import org.w3c.dom.*
|
||||
import org.w3c.xhr.*
|
||||
import kotlin.browser.*
|
||||
import kotlin.js.*
|
||||
|
||||
class JsFileStorage {
|
||||
@@ -142,4 +146,20 @@ class JsResourceFile(val filename: String) : ResourceFile {
|
||||
val fs = (dest as JsUserFile).fs
|
||||
fs.put(dest.filename, lines().joinToString("\n"))
|
||||
}
|
||||
|
||||
override suspend fun toImage(): Image {
|
||||
return Promise<Image> { resolve, reject ->
|
||||
val img = org.w3c.dom.Image()
|
||||
img.onload = {
|
||||
val canvas = JsCanvas(document.createElement("canvas") as HTMLCanvasElement, 1.0)
|
||||
canvas.element.width = img.naturalWidth
|
||||
canvas.element.height = img.naturalHeight
|
||||
canvas.setColor(Color(0xffffff))
|
||||
canvas.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight())
|
||||
canvas.ctx.drawImage(img, 0.0, 0.0)
|
||||
resolve(canvas.toImage())
|
||||
}
|
||||
img.src = "/assets/$filename"
|
||||
}.await()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@ import kotlin.math.*
|
||||
|
||||
class JavaCanvas(val image: BufferedImage,
|
||||
val pixelScale: Double = 2.0) : Canvas {
|
||||
override fun toImage(): Image {
|
||||
return JavaImage(image)
|
||||
}
|
||||
|
||||
private val frc = FontRenderContext(null, true, true)
|
||||
private var fontSize = 12.0
|
||||
|
||||
48
core/src/main/jvm/org/isoron/platform/gui/JavaImage.kt
Normal file
48
core/src/main/jvm/org/isoron/platform/gui/JavaImage.kt
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.platform.gui
|
||||
|
||||
import java.awt.image.*
|
||||
import java.io.*
|
||||
import javax.imageio.*
|
||||
|
||||
class JavaImage(val bufferedImage: BufferedImage) : Image {
|
||||
override fun setPixel(x: Int, y: Int, color: Color) {
|
||||
bufferedImage.setRGB(x, y, java.awt.Color(color.red.toFloat(),
|
||||
color.green.toFloat(),
|
||||
color.blue.toFloat()).rgb)
|
||||
}
|
||||
|
||||
override suspend fun export(path: String) {
|
||||
val file = File(path)
|
||||
file.parentFile.mkdirs()
|
||||
ImageIO.write(bufferedImage, "png", file)
|
||||
}
|
||||
|
||||
override val width: Int
|
||||
get() = bufferedImage.width
|
||||
|
||||
override val height: Int
|
||||
get() = bufferedImage.height
|
||||
|
||||
override fun getPixel(x: Int, y: Int): Color {
|
||||
return Color(bufferedImage.getRGB(x, y))
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,10 @@
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import java.io.*
|
||||
import java.nio.file.*
|
||||
import javax.imageio.*
|
||||
|
||||
class JavaResourceFile(val path: String) : ResourceFile {
|
||||
private val javaPath: Path
|
||||
@@ -49,6 +51,10 @@ class JavaResourceFile(val path: String) : ResourceFile {
|
||||
fun stream(): InputStream {
|
||||
return Files.newInputStream(javaPath)
|
||||
}
|
||||
|
||||
override suspend fun toImage(): Image {
|
||||
return JavaImage(ImageIO.read(stream()))
|
||||
}
|
||||
}
|
||||
|
||||
class JavaUserFile(val path: Path) : UserFile {
|
||||
|
||||
Reference in New Issue
Block a user