Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jun 26, 2021
1 parent 5eed3bb commit cdd2791
Show file tree
Hide file tree
Showing 27 changed files with 188 additions and 101 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# sytleguide
kotlin.code.style=official

# Kotlin 1.5.0: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.15.2
# Kotlin 1.5.20: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
easyPluginVersion=0.16.0

# version
group=com.soywiz.korlibs.korim
version=2.0.0-SNAPSHOT

# kotlinx
korioVersion=2.1.1
kormaVersion=2.1.1
korioVersion=2.2.0
kormaVersion=2.2.1

# bintray location
project.bintray.org=korlibs
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
5 changes: 2 additions & 3 deletions korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.soywiz.kmem.*
import com.soywiz.korim.annotation.*
import com.soywiz.korim.color.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.min2
import com.soywiz.korim.vector.*
import com.soywiz.korio.lang.*
import com.soywiz.korio.resources.*
Expand Down Expand Up @@ -110,8 +109,8 @@ abstract class Bitmap(
val dstX = dstX0
val dstY = dstY0

val width = min2(srcX1 - srcX0, dstX1 - dstX0)
val height = min2(srcY1 - srcY0, dstY1 - dstY0)
val width = min(srcX1 - srcX0, dstX1 - dstX0)
val height = min(srcY1 - srcY0, dstY1 - dstY0)

copyUnchecked(srcX, srcY, dst, dstX, dstY, width, height)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Bitmap32(

val intData get() = data.ints

private val temp = RgbaArray(max2(width, height))
private val temp = RgbaArray(max(width, height))
val bounds: IRectangleInt = RectangleInt(0, 0, width, height)

constructor(width: Int, height: Int, value: RGBA, premultiplied: Boolean = false) : this(width, height, premultiplied = premultiplied) { data.fill(value) }
Expand Down Expand Up @@ -110,8 +110,8 @@ class Bitmap32(
}
val availableWidth = width - dx
val availableHeight = height - dy
val awidth = min2(availableWidth, sright - sleft)
val aheight = min2(availableHeight, sbottom - stop)
val awidth = min(availableWidth, sright - sleft)
val aheight = min(availableHeight, sbottom - stop)
_drawUnchecked(src, dx, dy, sleft, stop, sleft + awidth, stop + aheight, mix)
}

Expand Down
34 changes: 19 additions & 15 deletions korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,27 @@ fun Bitmap32.setAlpha(value: Int) {
}

fun <T : Bitmap> T.putWithBorder(x: Int, y: Int, bmp: T, border: Int = 1) {
val width = bmp.width
val height = bmp.height
return putSliceWithBorder(x, y, bmp.slice(), border)
}

// Block copy
bmp.copy(0, 0, this, x, y, width, height)
fun Bitmap.putSliceWithBorder(x: Int, y: Int, bmp: BmpSlice, border: Int = 1) {
val width = bmp.width
val height = bmp.height

// Horizontal replicate
for (n in 1..border) {
this.copy(x, y, this, x - n, y, 1, height)
this.copy(x + width - 1, y, this, x + width - 1 + n, y, 1, height)
}
// Vertical replicate
for (n in 1..border) {
val rwidth = width + border * 2
this.copy(x, y, this, x, y - n, rwidth, 1)
this.copy(x, y + height - 1, this, x, y + height - 1 + n, rwidth, 1)
}
// Block copy
bmp.bmpBase.copy(bmp.left, bmp.top, this, x, y, width, height)

// Horizontal replicate
for (n in 1..border) {
this.copy(x, y, this, x - n, y, 1, height)
this.copy(x + width - 1, y, this, x + width - 1 + n, y, 1, height)
}
// Vertical replicate
for (n in 1..border) {
val rwidth = width + border * 2
this.copy(x, y, this, x, y - n, rwidth, 1)
this.copy(x, y + height - 1, this, x, y + height - 1 + n, rwidth, 1)
}
}

fun Bitmap.resized(out: Bitmap, scale: ScaleMode, anchor: Anchor): Bitmap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.soywiz.korim.bitmap
import com.soywiz.kmem.*
import com.soywiz.korim.color.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2

import kotlin.math.*

abstract class BitmapIndexed(
Expand All @@ -18,7 +18,7 @@ abstract class BitmapIndexed(

override fun toString() = "BitmapIndexed(bpp=$bpp, width=$width, height=$height, clut=${palette.size})"

protected val temp = ByteArray(max2(width, height))
protected val temp = ByteArray(max(width, height))

val datau = UByteArrayInt(data)
private val n8_dbpp: Int = 8 / bpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.soywiz.korim.bitmap

import com.soywiz.kmem.*
import com.soywiz.korim.color.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.min2
import com.soywiz.korio.lang.*
import kotlin.math.*

Expand Down Expand Up @@ -135,8 +133,8 @@ class DistanceBitmap(
var min = Float.MAX_VALUE
var max = Float.MIN_VALUE
for (n in 0 until area) {
min = min2(min, d[n])
max = max2(max, d[n])
min = min(min, d[n])
max = max(max, d[n])
}
for (n in 0 until area) {
out.data[n] = d[n].convertRange(min, max, 0f, 255f).toInt().toByte()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import com.soywiz.kmem.*
import com.soywiz.korim.color.*
import com.soywiz.korim.format.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2

import com.soywiz.korio.file.*
import com.soywiz.korio.util.*
import com.soywiz.korma.geom.*
import com.soywiz.korma.geom.vector.*
import kotlin.math.*

class NinePatchInfo(
val xranges: List<Pair<Boolean, IntRange>>,
Expand Down Expand Up @@ -44,8 +45,8 @@ class NinePatchInfo(

class AxisInfo(ranges: List<Pair<Boolean, IntRange>>, val totalLen: Int) {
val segments = ranges.map { AxisSegment(it.first, it.second) }
val fixedLen = max2(1, segments.filter { it.fixed }.map { it.length }.sum())
val scaledLen = max2(1, segments.filter { it.scaled }.map { it.length }.sum())
val fixedLen = max(1, segments.filter { it.fixed }.map { it.length }.sum())
val scaledLen = max(1, segments.filter { it.scaled }.map { it.length }.sum())
}

val xaxis = AxisInfo(xranges, width)
Expand Down Expand Up @@ -125,12 +126,12 @@ class NinePatchInfo(
if (x.fixed && y.fixed) {
val xScale = segWidth / x.length.toDouble()
val yScale = segHeight / y.length.toDouble()
val minScale = min2(xScale, yScale)
xComputed[xindex] = min2(xComputed[xindex], (x.length * minScale).toInt())
yComputed[yindex] = min2(yComputed[yindex], (y.length * minScale).toInt())
val minScale = min(xScale, yScale)
xComputed[xindex] = min(xComputed[xindex], (x.length * minScale).toInt())
yComputed[yindex] = min(yComputed[yindex], (y.length * minScale).toInt())
} else {
xComputed[xindex] = min2(xComputed[xindex], segWidth.toInt())
yComputed[yindex] = min2(yComputed[yindex], segHeight.toInt())
xComputed[xindex] = min(xComputed[xindex], segWidth.toInt())
yComputed[yindex] = min(yComputed[yindex], segHeight.toInt())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import com.soywiz.korim.bitmap.slice
import com.soywiz.korim.color.Colors
import com.soywiz.korim.color.RGBA
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2
import kotlin.math.absoluteValue
import kotlin.math.*

data class BitmapEffect(
// Blur
Expand All @@ -22,7 +21,7 @@ data class BitmapEffect(
var borderSize: Int = 0,
var borderColor: RGBA = Colors.BLACK
) {
val safeBorder get() = max2(max2(max2(blurRadius, dropShadowX.absoluteValue), dropShadowY.absoluteValue), borderSize)
val safeBorder get() = max(max(max(blurRadius, dropShadowX.absoluteValue), dropShadowY.absoluteValue), borderSize)
}

val BitmapEffect?.safeBorder get() = this?.safeBorder ?: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.soywiz.korim.bitmap.effect
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2

import kotlin.math.*

fun Bitmap32.dropShadow(x: Int, y: Int, r: Int, color: RGBA = Colors.BLACK): Bitmap32 {
val pcolor = color.premultiplied
val add = max2(x.absoluteValue, y.absoluteValue) + r
val add = max(x.absoluteValue, y.absoluteValue) + r
val out = Bitmap32(width + add * 2, height + add * 2, premultiplied = true)
val shadow = this.extractChannel(BitmapChannel.ALPHA).blur(r).also { shadow ->
shadow.premultiplied = true
Expand Down
2 changes: 1 addition & 1 deletion korim/src/commonMain/kotlin/com/soywiz/korim/color/CMYK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inline class CMYK(val value: Int) {
val r0 = rgba.rf
val g0 = rgba.gf
val b0 = rgba.bf
val k = 1f - max2(max2(r0, g0), b0)
val k = 1f - max(max(r0, g0), b0)
val ik = 1f / (1 - k)
val c = (1f - r0 - k) * ik
val m = (1f - g0 - k) * ik
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.soywiz.korim.color.Colors
import com.soywiz.korim.color.RGBA
import com.soywiz.korim.format.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2

import com.soywiz.korim.paint.DefaultPaint
import com.soywiz.korim.paint.Paint
import com.soywiz.korim.text.*
Expand Down Expand Up @@ -49,7 +49,7 @@ class BitmapFont(
fontSize, ascent, ascent, baseline, -descent, -descent, 0.0,
maxWidth = run {
var width = 0.0
for (glyph in glyphs.values) if (glyph != null) width = max2(width, glyph.texture.width.toDouble())
for (glyph in glyphs.values) if (glyph != null) width = max(width, glyph.texture.width.toDouble())
width
}
)
Expand Down
51 changes: 39 additions & 12 deletions korim/src/commonMain/kotlin/com/soywiz/korim/font/Font.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.soywiz.korim.font

import com.soywiz.kds.*
import com.soywiz.kmem.toIntCeil
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.bitmap.effect.BitmapEffect
import com.soywiz.korim.bitmap.effect.applyEffect
import com.soywiz.korim.color.*
import com.soywiz.korim.paint.*
import com.soywiz.korim.vector.*
import com.soywiz.korim.text.*
Expand All @@ -29,11 +27,23 @@ interface Font : Resourceable<Font> {

data class TextToBitmapResult(
val bmp: Bitmap,
val fmetrics: FontMetrics,
val metrics: TextMetrics,
val glyphs: List<PlacedGlyph>
) {
data class PlacedGlyph(val codePoint: Int, val x: Double, val y: Double, val metrics: GlyphMetrics, val transform: Matrix)
override val fmetrics: FontMetrics,
override val metrics: TextMetrics,
override val glyphs: List<PlacedGlyphMetrics>
) : BaseTextMetricsResult

data class TextMetricsResult(
override val fmetrics: FontMetrics,
override val metrics: TextMetrics,
override val glyphs: List<PlacedGlyphMetrics>
) : BaseTextMetricsResult

data class PlacedGlyphMetrics(val codePoint: Int, val x: Double, val y: Double, val metrics: GlyphMetrics, val transform: Matrix, val index: Int)

interface BaseTextMetricsResult {
val fmetrics: FontMetrics
val metrics: TextMetrics
val glyphs: List<PlacedGlyphMetrics>
}

fun Font.renderGlyphToBitmap(
Expand All @@ -57,7 +67,7 @@ fun Font.renderGlyphToBitmap(
}
val imageOut = image.toBMP32IfRequired().applyEffect(effect)
return TextToBitmapResult(imageOut, fmetrics, TextMetrics(), listOf(
TextToBitmapResult.PlacedGlyph(codePoint, gx + border, gy + border, gmetrics, Matrix())
PlacedGlyphMetrics(codePoint, gx + border, gy + border, gmetrics, Matrix(), 0)
))
}

Expand All @@ -77,7 +87,7 @@ fun <T> Font.renderTextToBitmap(
val font = this
val bounds = getTextBounds(size, text, renderer = renderer)
//println("BOUNDS: $bounds")
val glyphs = arrayListOf<TextToBitmapResult.PlacedGlyph>()
val glyphs = arrayListOf<PlacedGlyphMetrics>()
val iwidth = bounds.width.toIntCeil() + border * 2 + 1
val iheight = (if (drawBorder) bounds.allLineHeight else bounds.height).toIntCeil() + border * 2 + 1
val image = if (nativeRendering) NativeImage(iwidth, iheight) else Bitmap32(iwidth, iheight, premultiplied = true)
Expand All @@ -90,18 +100,35 @@ fun <T> Font.renderTextToBitmap(
}
}
//font.drawText(this, size, text, paint, bounds.drawLeft, bounds.drawTop, fill, renderer = renderer, placed = { codePoint, x, y, size, metrics, transform ->
var index = 0
font.drawText(this, size, text, paint, bounds.drawLeft, bounds.ascent, fill, renderer = renderer, placed = { codePoint, x, y, size, metrics, transform ->
if (returnGlyphs) {
glyphs += TextToBitmapResult.PlacedGlyph(codePoint, x, y, metrics.clone(), transform.clone())
glyphs += PlacedGlyphMetrics(codePoint, x, y, metrics.clone(), transform.clone(), index++)
}
})
}
return TextToBitmapResult(image, bounds.fontMetrics, bounds, glyphs)
}

fun <T> Font.measureTextGlyphs(
size: Double,
text: T,
renderer: TextRenderer<T> = DefaultStringTextRenderer as TextRenderer<T>,
): TextMetricsResult {
val font = this
val bounds = getTextBounds(size, text, renderer = renderer)
//println("BOUNDS: $bounds")
val glyphs = arrayListOf<PlacedGlyphMetrics>()
var index = 0
font.drawText(null, size, text, null, bounds.drawLeft, bounds.ascent, false, renderer = renderer, placed = { codePoint, x, y, size, metrics, transform ->
glyphs += PlacedGlyphMetrics(codePoint, x, y, metrics.clone(), transform.clone(), index++)
})
return TextMetricsResult(bounds.fontMetrics, bounds, glyphs)
}

fun <T> Font.drawText(
ctx: Context2d?, size: Double,
text: T, paint: Paint,
text: T, paint: Paint?,
x: Double = 0.0, y: Double = 0.0,
fill: Boolean = true,
renderer: TextRenderer<T> = DefaultStringTextRenderer as TextRenderer<T>,
Expand All @@ -116,7 +143,7 @@ fun <T> Font.drawText(
//ctx.translate(-m.width * transformAnchor.sx, +m.height * transformAnchor.sy)
ctx.transform(this.transform)
//ctx.translate(+m.width * transformAnchor.sx, -m.height * transformAnchor.sy)
ctx.fillStyle = this.paint ?: paint
ctx.fillStyle = this.paint ?: paint ?: NonePaint
font.renderGlyph(ctx, size, codePoint, 0.0, 0.0, true, glyphMetrics)
placed?.invoke(codePoint, this.x + x, this.y + y, size, glyphMetrics, this.transform)
if (fill) ctx.fill() else ctx.stroke()
Expand Down
6 changes: 3 additions & 3 deletions korim/src/commonMain/kotlin/com/soywiz/korim/format/GIF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.soywiz.klock.*
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
import com.soywiz.korim.internal.*
import com.soywiz.korim.internal.max2

import com.soywiz.korio.file.std.*
import com.soywiz.korio.lang.*
import com.soywiz.korio.stream.*
Expand Down Expand Up @@ -261,7 +261,7 @@ object GifDec {
}

fun new_table(key_size: Int): Table {
val init_bulk = max2(1 shl (key_size + 1), 0x100);
val init_bulk = max(1 shl (key_size + 1), 0x100);
return Table(
init_bulk,
(1 shl key_size) + 2,
Expand Down Expand Up @@ -304,7 +304,7 @@ object GifDec {
s.byte = gif.fd.readU8()
s.sub_len--
}
val frag_size = min2(key_size - bits_read, 8 - rpad);
val frag_size = min(key_size - bits_read, 8 - rpad);
key = key or (( 0xFFFF and ((s.byte) ushr rpad)) shl bits_read)
bits_read += frag_size
}
Expand Down
Loading

0 comments on commit cdd2791

Please sign in to comment.