Skip to content

Commit

Permalink
Merge pull request #1 from Omega-R/feature/text_core
Browse files Browse the repository at this point in the history
Feature/text core
  • Loading branch information
anton-knyazev authored Feb 28, 2020
2 parents 67e84b6 + accd743 commit 2c98ed6
Show file tree
Hide file tree
Showing 170 changed files with 5,219 additions and 270 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
classpath("com.android.tools.build:gradle:$android_tools_version")
classpath("com.github.dcendents:android-maven-gradle-plugin:2.1")
}
}
70 changes: 49 additions & 21 deletions library/build.gradle.kts → core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.konan.properties.Properties

buildscript {
val kotlin_version: String by project
Expand Down Expand Up @@ -26,6 +27,7 @@ buildscript {

plugins {
id("org.jetbrains.kotlin.multiplatform")
id("com.github.dcendents.android-maven")
id("maven-publish")
}

Expand All @@ -49,6 +51,7 @@ repositories {
configurations.create("compileClasspath")

kotlin {
val ktor_version: String by project
//need to use jvm because android doesnt export type alias
jvm("android") {
compilations.all {
Expand All @@ -58,24 +61,26 @@ kotlin {
}
}

js {
val main by compilations.getting {
kotlinOptions {
metaInfo = true
sourceMap = true
sourceMapEmbedSources = "always"
moduleKind = "commonjs"
//select iOS target platform depending on the Xcode environment variables
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64

iOSTarget("ios") {
binaries {
framework {
baseName = "core"
}
}
}

iosArm64()
iosX64()

sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("io.ktor:ktor-client-core:$ktor_version")
}
}

Expand All @@ -89,6 +94,7 @@ kotlin {
val androidMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("io.ktor:ktor-client-android:$ktor_version")
compileOnly("org.robolectric:android-all:9-robolectric-4913185-2")
}
}
Expand All @@ -101,22 +107,44 @@ kotlin {
}
}

val jsMain by getting {
val iosMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-js")
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("io.ktor:ktor-client-ios:$ktor_version")
}
}

//JS tests currently not working, need to wait for jetbrains to release support
val jsTest by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-test-js")
}
}
}
}

val javadocJar by tasks.creating(Jar::class) {
archiveClassifier.value("javadoc")
}
}

val packForXcode by tasks.creating(Sync::class) {
val targetDir = File(buildDir, "xcode-frameworks")

/// selecting the right configuration for the iOS
/// framework depending on the environment
/// variables set by Xcode build
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val framework = kotlin.targets
.getByName<KotlinNativeTarget>("ios")
.binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)

from({ framework.outputDirectory })
into(targetDir)

/// generate a helpful ./gradlew wrapper with embedded Java path
doLast {
val gradlew = File(targetDir, "gradlew")
gradlew.writeText("#!/bin/bash\n"
+ "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
+ "cd '${rootProject.rootDir}'\n"
+ "./gradlew \$@\n")
gradlew.setExecutable(true)
}
}

tasks.getByName("build").dependsOn(packForXcode)
1 change: 1 addition & 0 deletions core/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.omega_r.libs.entities" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.omega_r.libs.entities.decoders

import android.annotation.TargetApi
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build
import java.io.File
import java.io.InputStream

typealias BitmapOptions = BitmapFactory.Options


interface BitmapDecoders {

companion object {

val default = Default()

var current: BitmapDecoders = default

}

fun decodeBitmap(source: File, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?

fun decodeBitmap(source: InputStream, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?

fun decodeBitmap(source: ByteArray, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?

fun recycle(bitmap: Bitmap)

fun clearMemory()

fun trimMemory(level: Int)

fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
// Raw height and width of image
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1

if (height > reqHeight || width > reqWidth) {

val halfHeight = height / 2
val halfWidth = width / 2

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while (halfHeight / inSampleSize > reqHeight && halfWidth / inSampleSize > reqWidth) {
inSampleSize *= 2
}
}

return inSampleSize
}


@TargetApi(Build.VERSION_CODES.KITKAT)
private fun getBitmapByteSize(bitmap: Bitmap): Int {
// The return value of getAllocationByteCount silently changes for recycled bitmaps from the
// internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
// instead assert here.
check(!bitmap.isRecycled) {
("Cannot obtain size for recycled Bitmap: " + bitmap
+ "[" + bitmap.width + "x" + bitmap.height + "] " + bitmap.config)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
try {
return bitmap.allocationByteCount
} catch (e: NullPointerException) {
// Do nothing.
}

}
return bitmap.height * bitmap.rowBytes
}

fun getBitmapByteSize(width: Int, height: Int, config: Bitmap.Config): Int {
return width * height * getBytesPerPixel(config)
}

fun getBytesPerPixel(config: Bitmap.Config?): Int {
return when (config) {
Bitmap.Config.ALPHA_8 -> 1
Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2
Bitmap.Config.ARGB_8888 -> 4
else -> 4
}
}

class Default : BitmapDecoders {

override fun decodeBitmap(source: File, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
return decodeBitmap(requiredWidth, requiredHeight) {
BitmapFactory.decodeFile(source.absolutePath, it)
}
}

override fun decodeBitmap(source: InputStream, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
return decodeBitmap(requiredWidth, requiredHeight) {
BitmapFactory.decodeStream(source, null, it)
}
}

override fun decodeBitmap(source: ByteArray, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
return decodeBitmap(requiredWidth, requiredHeight) {
BitmapFactory.decodeByteArray(source, 0, source.size, it)
}
}

private inline fun decodeBitmap(reqWidth: Int?, reqHeight: Int?, bitmapFactory: (BitmapFactory.Options?) -> Bitmap?): Bitmap? {
val options = if (reqWidth != null && reqHeight != null) {
BitmapOptions().apply {
inJustDecodeBounds = true
bitmapFactory(this)
inJustDecodeBounds = false
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
}
} else null

return bitmapFactory(options)

}

override fun recycle(bitmap: Bitmap) {
bitmap.recycle()
}

override fun clearMemory() {
// nothing
}

override fun trimMemory(level: Int) {
// nothing
}

}

}

@JvmOverloads
fun File.toBitmap(
requiredWidth: Int? = null,
requiredHeight: Int? = null,
decoders: BitmapDecoders = BitmapDecoders.current
): Bitmap? {
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
}

@JvmOverloads
fun InputStream.toBitmap(
requiredWidth: Int? = null,
requiredHeight: Int? = null,
decoders: BitmapDecoders = BitmapDecoders.current
): Bitmap? {
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
}

@JvmOverloads
fun ByteArray.toBitmap(
requiredWidth: Int? = null,
requiredHeight: Int? = null,
decoders: BitmapDecoders = BitmapDecoders.current
): Bitmap? {
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
}
Loading

0 comments on commit 2c98ed6

Please sign in to comment.