diff --git a/core/src/main/common/org/isoron/platform/gui/Image.kt b/core/src/main/common/org/isoron/platform/gui/Image.kt index 4f9c41302..044df1c6f 100644 --- a/core/src/main/common/org/isoron/platform/gui/Image.kt +++ b/core/src/main/common/org/isoron/platform/gui/Image.kt @@ -31,8 +31,8 @@ interface Image { 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") + if (width != other.width) error("Width must match: $width !== ${other.width}") + if (height != other.height) error("Height must match: $height !== ${other.height}") for (x in 0 until width) { for (y in 0 until height) { diff --git a/core/src/main/ios/org/isoron/platform/gui/IosCanvas.kt b/core/src/main/ios/org/isoron/platform/gui/IosCanvas.kt index c8f2e6871..35293a284 100644 --- a/core/src/main/ios/org/isoron/platform/gui/IosCanvas.kt +++ b/core/src/main/ios/org/isoron/platform/gui/IosCanvas.kt @@ -28,8 +28,12 @@ val Color.uicolor: UIColor val Color.cgcolor: CGColorRef? get() = uicolor.CGColor -class IosCanvas(val ctx: CGContextRef) : Canvas { +class IosCanvas(val width: Double, + val height: Double, + val pixelScale: Double = 2.0 + ) : Canvas { var textColor = UIColor.blackColor + val ctx = UIGraphicsGetCurrentContext()!! override fun setColor(color: Color) { CGContextSetStrokeColorWithColor(ctx, color.cgcolor) @@ -38,32 +42,47 @@ class IosCanvas(val ctx: CGContextRef) : Canvas { } override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) { + CGContextMoveToPoint(ctx, x1 * pixelScale, y1 * pixelScale) + CGContextAddLineToPoint(ctx, x2 * pixelScale, y2 * pixelScale) + CGContextStrokePath(ctx) } override fun drawText(text: String, x: Double, y: Double) { } override fun fillRect(x: Double, y: Double, width: Double, height: Double) { + CGContextFillRect(ctx, + CGRectMake(x * pixelScale, + y * pixelScale, + width * pixelScale, + height * pixelScale)) } override fun drawRect(x: Double, y: Double, width: Double, height: Double) { + CGContextStrokeRect(ctx, + CGRectMake(x * pixelScale, + y * pixelScale, + width * pixelScale, + height * pixelScale)) } override fun getHeight(): Double { - return 0.0 + return height } override fun getWidth(): Double { - return 0.0 + return width } override fun setFont(font: Font) { } override fun setFontSize(size: Double) { + CGContextSetFontSize(ctx, size * pixelScale) } override fun setStrokeWidth(size: Double) { + CGContextSetLineWidth(ctx, size * pixelScale) } override fun fillArc(centerX: Double, @@ -80,6 +99,6 @@ class IosCanvas(val ctx: CGContextRef) : Canvas { } override fun toImage(): Image { - TODO() + return IosImage(UIGraphicsGetImageFromCurrentImageContext()!!) } -} \ No newline at end of file +} diff --git a/core/src/main/ios/org/isoron/platform/gui/IosImage.kt b/core/src/main/ios/org/isoron/platform/gui/IosImage.kt new file mode 100644 index 000000000..33e1c2254 --- /dev/null +++ b/core/src/main/ios/org/isoron/platform/gui/IosImage.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016-2019 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.platform.gui + +import platform.UIKit.* +import platform.CoreGraphics.* +import platform.Foundation.* + +class IosImage(val image: UIImage) : Image { + + override val width: Int + get() { + return CGImageGetWidth(image.CGImage).toInt() + } + + override val height: Int + get() { + return CGImageGetHeight(image.CGImage).toInt() + } + + override fun getPixel(x: Int, y: Int): Color { + return Color(1.0, 0.0, 0.0, 1.0) + } + + override fun setPixel(x: Int, y: Int, color: Color) { + } + + @Suppress("CAST_NEVER_SUCCEEDS") + override suspend fun export(path: String) { + val tmpPath = "${NSTemporaryDirectory()}/$path" + val dir = (tmpPath as NSString).stringByDeletingLastPathComponent + NSFileManager.defaultManager.createDirectoryAtPath(dir, true, null, null) + val data = UIImagePNGRepresentation(image)!! + val success = data.writeToFile(tmpPath, true) + if (!success) throw RuntimeException("could not write to $tmpPath") + println(tmpPath) + } +} \ No newline at end of file diff --git a/core/src/main/ios/org/isoron/platform/io/IosFiles.kt b/core/src/main/ios/org/isoron/platform/io/IosFiles.kt index 4a2f3c85a..16d189133 100644 --- a/core/src/main/ios/org/isoron/platform/io/IosFiles.kt +++ b/core/src/main/ios/org/isoron/platform/io/IosFiles.kt @@ -22,6 +22,7 @@ package org.isoron.platform.io import org.isoron.platform.gui.* import platform.Foundation.* +import platform.UIKit.* class IosFileOpener : FileOpener { override fun openResourceFile(path: String): ResourceFile { @@ -62,6 +63,6 @@ class IosFile(val path: String) : UserFile, ResourceFile { } override suspend fun toImage(): Image { - TODO() + return IosImage(UIImage.imageWithContentsOfFile(path)!!) } } \ No newline at end of file diff --git a/core/src/test/common/org/isoron/platform/gui/CanvasTest.kt b/core/src/test/common/org/isoron/platform/gui/CanvasTest.kt index d4ab43f8d..1c2065cde 100644 --- a/core/src/test/common/org/isoron/platform/gui/CanvasTest.kt +++ b/core/src/test/common/org/isoron/platform/gui/CanvasTest.kt @@ -24,7 +24,7 @@ import org.isoron.uhabits.* import kotlin.test.* class CanvasTest: BaseViewTest() { - //@Test + @Test fun run() = asyncTest{ val canvas = DependencyResolver.createCanvas(500, 400) diff --git a/core/src/test/ios/org/isoron/DependencyResolver.kt b/core/src/test/ios/org/isoron/DependencyResolver.kt index 9227991ce..0044336af 100644 --- a/core/src/test/ios/org/isoron/DependencyResolver.kt +++ b/core/src/test/ios/org/isoron/DependencyResolver.kt @@ -19,6 +19,7 @@ package org.isoron +import kotlinx.cinterop.* import org.isoron.platform.gui.* import org.isoron.platform.io.* import org.isoron.platform.time.* @@ -37,9 +38,9 @@ actual object DependencyResolver { } actual fun createCanvas(width: Int, height: Int): Canvas { - UIGraphicsBeginImageContext(CGSizeMake(width.toDouble(), height.toDouble())) - val ctx = UIGraphicsGetCurrentContext()!! - return IosCanvas(ctx) + val scale = 2.0 + UIGraphicsBeginImageContext(CGSizeMake(width * scale, height * scale)) + return IosCanvas(width * scale, height * scale, pixelScale = scale) } actual suspend fun getDatabase(): Database {