Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9e4de6b3 authored by Fynn Godau's avatar Fynn Godau
Browse files

Use pattern's width as stroke pattern bitmap width

parent d49828be
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -352,7 +352,10 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
                circle.line.update(lineManager)
            }
            circle.strokePattern?.let {
                addBitmap(it.getName(circle.strokeColor), it.makeBitmap(circle.strokeColor, circle.strokeWidth))
                addBitmap(
                    it.getName(circle.strokeColor, circle.strokeWidth),
                    it.makeBitmap(circle.strokeColor, circle.strokeWidth)
                )
            }
        }
        return circle
+20 −25
Original line number Diff line number Diff line
@@ -3,15 +3,10 @@ package org.microg.gms.maps.mapbox
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.util.Log
import com.google.android.gms.maps.model.Dash
import com.google.android.gms.maps.model.Dot
import com.google.android.gms.maps.model.Gap
import com.google.android.gms.maps.model.PatternItem
import org.microg.gms.maps.mapbox.model.CircleImpl
import kotlin.math.max

const val BITMAP_WIDTH = 128

fun PatternItem.getName(): String = when (this) {
    is Dash -> "dash${this.length}"
@@ -23,15 +18,15 @@ fun PatternItem.getName(): String = when (this) {
/**
 * Name of pattern, to identify it after it is added to map
 */
fun MutableList<PatternItem>.getName(color: Int) = joinToString("-") {
fun MutableList<PatternItem>.getName(color: Int, strokeWidth: Float) = joinToString("-") {
    it.getName()
} + "-${color}"
} + "-${color}-width${strokeWidth}"

/**
 * Gets width that a bitmap for this pattern item would have if the pattern's
 * bitmap were to have height 1.
 * Gets width that a bitmap for this pattern item would have if the pattern's bitmap
 * were to be drawn with respect to aspect ratio onto a canvas with height 1.
 */
fun PatternItem.getVirtualWidth(strokeWidth: Float): Float = when (this) {
fun PatternItem.getWidth(strokeWidth: Float): Float = when (this) {
    is Dash -> this.length
    is Gap -> this.length
    is Dot -> strokeWidth
@@ -39,24 +34,24 @@ fun PatternItem.getVirtualWidth(strokeWidth: Float): Float = when (this) {
}

/**
 * Gets width that a bitmap for this pattern would have if it were to have
 * height 1.
 * Gets width that a bitmap for this pattern would have if it were to be drawn
 * with respect to aspect ratio onto a canvas with height 1.
 */
fun MutableList<PatternItem>.getVirtualWidth(strokeWidth: Float) = map { it.getVirtualWidth(strokeWidth) }.sum()
fun MutableList<PatternItem>.getWidth(strokeWidth: Float) = map { it.getWidth(strokeWidth) }.sum()

fun MutableList<PatternItem>.makeBitmap(color: Int, width: Float): Bitmap = makeBitmap(Paint().apply {
fun MutableList<PatternItem>.makeBitmap(color: Int, strokeWidth: Float): Bitmap = makeBitmap(Paint().apply {
    setColor(color)
    style = Paint.Style.FILL
}, width)
}, strokeWidth)


fun MutableList<PatternItem>.makeBitmap(paint: Paint, width: Float): Bitmap {
fun MutableList<PatternItem>.makeBitmap(paint: Paint, strokeWidth: Float): Bitmap {

    val virtualWidth = getVirtualWidth(width)
    val scale = BITMAP_WIDTH / virtualWidth
    Log.d("GmsMapPattern", "vWidth: $virtualWidth, scale: $scale")
    // Pattern aspect ratio is not respected by renderer
    val width = getWidth(strokeWidth).toInt()
    val height = strokeWidth.toInt() // avoids squished image bugs

    val bitmap = Bitmap.createBitmap(BITMAP_WIDTH, width.toInt(), Bitmap.Config.ARGB_8888)
    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmap)

    var drawCursor = 0f
@@ -65,8 +60,8 @@ fun MutableList<PatternItem>.makeBitmap(paint: Paint, width: Float): Bitmap {
            is Dash -> canvas.drawRect(
                drawCursor,
                0f,
                drawCursor + item.length * scale,
                width,
                drawCursor + item.length,
                strokeWidth,
                paint
            )

@@ -75,13 +70,13 @@ fun MutableList<PatternItem>.makeBitmap(paint: Paint, width: Float): Bitmap {
            is Dot -> canvas.drawOval(
                drawCursor,
                0f,
                drawCursor + item.getVirtualWidth(width) * scale,
                width,
                drawCursor + item.getWidth(strokeWidth),
                strokeWidth,
                paint
            )
        }

        drawCursor += item.getVirtualWidth(width) * scale
        drawCursor += item.getWidth(strokeWidth)
    }

    return bitmap
+9 −7
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ class CircleImpl(private val map: GoogleMapImpl, private val id: String, options
                .withLineOpacity(if (visible) 1f else 0f)
                .apply {
                    strokePattern?.let {
                        withLinePattern(it.getName(strokeColor))
                        withLinePattern(it.getName(strokeColor, strokeWidth))
                    }
                }

@@ -188,8 +188,9 @@ class CircleImpl(private val map: GoogleMapImpl, private val id: String, options
        this.strokeWidth = width
        line.annotation?.lineWidth = width / map.dpiFactor
        strokePattern?.let {
            map.addBitmap(it.getName(strokeColor), it.makeBitmap(strokeColor, width))
            line.annotation?.linePattern = it.getName(strokeColor)
            val bitmapName = it.getName(strokeColor, strokeWidth)
            map.addBitmap(bitmapName, it.makeBitmap(strokeColor, width))
            line.annotation?.linePattern = bitmapName
        }
        map.lineManager?.let { line.update(it) }
    }
@@ -200,8 +201,9 @@ class CircleImpl(private val map: GoogleMapImpl, private val id: String, options
        this.strokeColor = color
        line.annotation?.setLineColor(color)
        strokePattern?.let {
            map.addBitmap(it.getName(color), it.makeBitmap(color, strokeWidth))
            line.annotation?.linePattern = it.getName(color)
            val bitmapName = it.getName(color, strokeWidth)
            map.addBitmap(bitmapName, it.makeBitmap(color, strokeWidth))
            line.annotation?.linePattern = bitmapName
        }
        map.lineManager?.let { line.update(it) }
    }
@@ -261,9 +263,9 @@ class CircleImpl(private val map: GoogleMapImpl, private val id: String, options

    override fun setStrokePattern(pattern: MutableList<PatternItem>?) {
        this.strokePattern = pattern
        line.annotation?.linePattern = pattern?.getName(strokeColor)
        line.annotation?.linePattern = pattern?.getName(strokeColor, strokeWidth)
        pattern?.let {
            map.addBitmap(it.getName(strokeColor), it.makeBitmap(strokeColor, strokeWidth))
                map.addBitmap(it.getName(strokeColor, strokeWidth), it.makeBitmap(strokeColor, strokeWidth))
        }
        map.lineManager?.let { line.update(it) }
    }