Skip to content

Commit 2c98ed6

Browse files
Merge pull request #1 from Omega-R/feature/text_core
Feature/text core
2 parents 67e84b6 + accd743 commit 2c98ed6

File tree

170 files changed

+5219
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+5219
-270
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ buildscript {
2626
dependencies {
2727
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
2828
classpath("com.android.tools.build:gradle:$android_tools_version")
29+
classpath("com.github.dcendents:android-maven-gradle-plugin:2.1")
2930
}
3031
}
Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import java.util.*
1+
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
2+
import org.jetbrains.kotlin.konan.properties.Properties
23

34
buildscript {
45
val kotlin_version: String by project
@@ -26,6 +27,7 @@ buildscript {
2627

2728
plugins {
2829
id("org.jetbrains.kotlin.multiplatform")
30+
id("com.github.dcendents.android-maven")
2931
id("maven-publish")
3032
}
3133

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

5153
kotlin {
54+
val ktor_version: String by project
5255
//need to use jvm because android doesnt export type alias
5356
jvm("android") {
5457
compilations.all {
@@ -58,24 +61,26 @@ kotlin {
5861
}
5962
}
6063

61-
js {
62-
val main by compilations.getting {
63-
kotlinOptions {
64-
metaInfo = true
65-
sourceMap = true
66-
sourceMapEmbedSources = "always"
67-
moduleKind = "commonjs"
64+
//select iOS target platform depending on the Xcode environment variables
65+
val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
66+
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
67+
::iosArm64
68+
else
69+
::iosX64
70+
71+
iOSTarget("ios") {
72+
binaries {
73+
framework {
74+
baseName = "core"
6875
}
6976
}
7077
}
7178

72-
iosArm64()
73-
iosX64()
74-
7579
sourceSets {
7680
val commonMain by getting {
7781
dependencies {
7882
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
83+
implementation("io.ktor:ktor-client-core:$ktor_version")
7984
}
8085
}
8186

@@ -89,6 +94,7 @@ kotlin {
8994
val androidMain by getting {
9095
dependencies {
9196
implementation("org.jetbrains.kotlin:kotlin-stdlib")
97+
implementation("io.ktor:ktor-client-android:$ktor_version")
9298
compileOnly("org.robolectric:android-all:9-robolectric-4913185-2")
9399
}
94100
}
@@ -101,22 +107,44 @@ kotlin {
101107
}
102108
}
103109

104-
val jsMain by getting {
110+
val iosMain by getting {
105111
dependencies {
106-
implementation("org.jetbrains.kotlin:kotlin-stdlib-js")
107-
implementation("org.jetbrains.kotlin:kotlin-stdlib")
112+
implementation("io.ktor:ktor-client-ios:$ktor_version")
108113
}
109114
}
110115

111-
//JS tests currently not working, need to wait for jetbrains to release support
112-
val jsTest by getting {
113-
dependencies {
114-
implementation("org.jetbrains.kotlin:kotlin-test-js")
115-
}
116-
}
117116
}
118117
}
119118

120119
val javadocJar by tasks.creating(Jar::class) {
121120
archiveClassifier.value("javadoc")
122-
}
121+
}
122+
123+
val packForXcode by tasks.creating(Sync::class) {
124+
val targetDir = File(buildDir, "xcode-frameworks")
125+
126+
/// selecting the right configuration for the iOS
127+
/// framework depending on the environment
128+
/// variables set by Xcode build
129+
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
130+
val framework = kotlin.targets
131+
.getByName<KotlinNativeTarget>("ios")
132+
.binaries.getFramework(mode)
133+
inputs.property("mode", mode)
134+
dependsOn(framework.linkTask)
135+
136+
from({ framework.outputDirectory })
137+
into(targetDir)
138+
139+
/// generate a helpful ./gradlew wrapper with embedded Java path
140+
doLast {
141+
val gradlew = File(targetDir, "gradlew")
142+
gradlew.writeText("#!/bin/bash\n"
143+
+ "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
144+
+ "cd '${rootProject.rootDir}'\n"
145+
+ "./gradlew \$@\n")
146+
gradlew.setExecutable(true)
147+
}
148+
}
149+
150+
tasks.getByName("build").dependsOn(packForXcode)
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest package="com.omega_r.libs.entities" />
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.omega_r.libs.entities.decoders
2+
3+
import android.annotation.TargetApi
4+
import android.graphics.Bitmap
5+
import android.graphics.BitmapFactory
6+
import android.os.Build
7+
import java.io.File
8+
import java.io.InputStream
9+
10+
typealias BitmapOptions = BitmapFactory.Options
11+
12+
13+
interface BitmapDecoders {
14+
15+
companion object {
16+
17+
val default = Default()
18+
19+
var current: BitmapDecoders = default
20+
21+
}
22+
23+
fun decodeBitmap(source: File, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?
24+
25+
fun decodeBitmap(source: InputStream, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?
26+
27+
fun decodeBitmap(source: ByteArray, requiredWidth: Int? = null, requiredHeight: Int? = null): Bitmap?
28+
29+
fun recycle(bitmap: Bitmap)
30+
31+
fun clearMemory()
32+
33+
fun trimMemory(level: Int)
34+
35+
fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
36+
// Raw height and width of image
37+
val height = options.outHeight
38+
val width = options.outWidth
39+
var inSampleSize = 1
40+
41+
if (height > reqHeight || width > reqWidth) {
42+
43+
val halfHeight = height / 2
44+
val halfWidth = width / 2
45+
46+
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
47+
// height and width larger than the requested height and width.
48+
while (halfHeight / inSampleSize > reqHeight && halfWidth / inSampleSize > reqWidth) {
49+
inSampleSize *= 2
50+
}
51+
}
52+
53+
return inSampleSize
54+
}
55+
56+
57+
@TargetApi(Build.VERSION_CODES.KITKAT)
58+
private fun getBitmapByteSize(bitmap: Bitmap): Int {
59+
// The return value of getAllocationByteCount silently changes for recycled bitmaps from the
60+
// internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
61+
// instead assert here.
62+
check(!bitmap.isRecycled) {
63+
("Cannot obtain size for recycled Bitmap: " + bitmap
64+
+ "[" + bitmap.width + "x" + bitmap.height + "] " + bitmap.config)
65+
}
66+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
67+
// Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
68+
try {
69+
return bitmap.allocationByteCount
70+
} catch (e: NullPointerException) {
71+
// Do nothing.
72+
}
73+
74+
}
75+
return bitmap.height * bitmap.rowBytes
76+
}
77+
78+
fun getBitmapByteSize(width: Int, height: Int, config: Bitmap.Config): Int {
79+
return width * height * getBytesPerPixel(config)
80+
}
81+
82+
fun getBytesPerPixel(config: Bitmap.Config?): Int {
83+
return when (config) {
84+
Bitmap.Config.ALPHA_8 -> 1
85+
Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2
86+
Bitmap.Config.ARGB_8888 -> 4
87+
else -> 4
88+
}
89+
}
90+
91+
class Default : BitmapDecoders {
92+
93+
override fun decodeBitmap(source: File, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
94+
return decodeBitmap(requiredWidth, requiredHeight) {
95+
BitmapFactory.decodeFile(source.absolutePath, it)
96+
}
97+
}
98+
99+
override fun decodeBitmap(source: InputStream, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
100+
return decodeBitmap(requiredWidth, requiredHeight) {
101+
BitmapFactory.decodeStream(source, null, it)
102+
}
103+
}
104+
105+
override fun decodeBitmap(source: ByteArray, requiredWidth: Int?, requiredHeight: Int?): Bitmap? {
106+
return decodeBitmap(requiredWidth, requiredHeight) {
107+
BitmapFactory.decodeByteArray(source, 0, source.size, it)
108+
}
109+
}
110+
111+
private inline fun decodeBitmap(reqWidth: Int?, reqHeight: Int?, bitmapFactory: (BitmapFactory.Options?) -> Bitmap?): Bitmap? {
112+
val options = if (reqWidth != null && reqHeight != null) {
113+
BitmapOptions().apply {
114+
inJustDecodeBounds = true
115+
bitmapFactory(this)
116+
inJustDecodeBounds = false
117+
inSampleSize = calculateInSampleSize(this, reqWidth, reqHeight)
118+
}
119+
} else null
120+
121+
return bitmapFactory(options)
122+
123+
}
124+
125+
override fun recycle(bitmap: Bitmap) {
126+
bitmap.recycle()
127+
}
128+
129+
override fun clearMemory() {
130+
// nothing
131+
}
132+
133+
override fun trimMemory(level: Int) {
134+
// nothing
135+
}
136+
137+
}
138+
139+
}
140+
141+
@JvmOverloads
142+
fun File.toBitmap(
143+
requiredWidth: Int? = null,
144+
requiredHeight: Int? = null,
145+
decoders: BitmapDecoders = BitmapDecoders.current
146+
): Bitmap? {
147+
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
148+
}
149+
150+
@JvmOverloads
151+
fun InputStream.toBitmap(
152+
requiredWidth: Int? = null,
153+
requiredHeight: Int? = null,
154+
decoders: BitmapDecoders = BitmapDecoders.current
155+
): Bitmap? {
156+
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
157+
}
158+
159+
@JvmOverloads
160+
fun ByteArray.toBitmap(
161+
requiredWidth: Int? = null,
162+
requiredHeight: Int? = null,
163+
decoders: BitmapDecoders = BitmapDecoders.current
164+
): Bitmap? {
165+
return decoders.decodeBitmap(this, requiredWidth, requiredHeight)
166+
}

0 commit comments

Comments
 (0)