Skip to content

Commit a63490a

Browse files
committed
Fix text measurement outside of screen bug
1 parent 8b50045 commit a63490a

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- PNG export
88

99
### Changed
10+
- Features are now sealed. New `CustomFeature` is not drawn by default draw.
1011
- avoid drawing features with VisibleAttribute false
1112
- Move SVG export to `features` and make it usable for maps as well
1213
- Kotlin 2.1
@@ -16,9 +17,11 @@
1617
### Removed
1718

1819
### Fixed
20+
- Text measurement outside of screen errors
1921
- Add alpha attribute comprehension for all standard features.
2022
- Package name for SerializeableAttribute
2123

24+
2225
### Security
2326

2427
## 0.3.0 - 2024-06-04

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ val kmathVersion: String by extra("0.4.0")
99

1010
allprojects {
1111
group = "space.kscience"
12-
version = "0.4.0-dev-6"
12+
version = "0.4.0-dev-7"
1313

1414
repositories {
1515
mavenLocal()

maps-kt-compose/src/commonMain/kotlin/space/kscience/maps/compose/MapView.kt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
1313
import kotlinx.coroutines.CancellationException
1414
import kotlinx.coroutines.flow.map
1515
import kotlinx.coroutines.launch
16-
import kotlinx.coroutines.supervisorScope
1716
import org.jetbrains.skia.Image
1817
import space.kscience.attributes.Attributes
1918
import space.kscience.maps.coordinates.Gmc
@@ -75,20 +74,14 @@ public fun MapView(
7574
for (j in verticalIndices) {
7675
for (i in horizontalIndices) {
7776
val id = TileId(intZoom, i, j)
78-
//ensure that failed tiles do not fail the application
79-
supervisorScope {
80-
//start all
81-
val deferred = loadTileAsync(id)
82-
//wait asynchronously for it to finish
83-
launch {
84-
try {
85-
val tile = deferred.await()
86-
tiles[tile.id] = tile.image
87-
} catch (ex: Exception) {
88-
//displaying the error is maps responsibility
89-
if (ex !is CancellationException) {
90-
logger.error(ex) { "Failed to load tile with id=$id" }
91-
}
77+
launch {
78+
try {
79+
val tile = loadTileAsync(id).await()
80+
tiles[tile.id] = tile.image
81+
} catch (ex: Exception) {
82+
//displaying the error is maps responsibility
83+
if (ex !is CancellationException) {
84+
logger.error(ex) { "Failed to load tile with id=$id" }
9285
}
9386
}
9487
}

maps-kt-features/src/commonMain/kotlin/space/kscience/maps/features/FeatureDrawScope.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public abstract class FeatureDrawScope<T : Any>(
4949

5050
public abstract fun drawText(text: String, position: Offset, attributes: Attributes)
5151

52-
public companion object{
53-
public val logger: KLogger = KotlinLogging.logger("FeatureDrawScope")
52+
public companion object {
53+
public val logger: KLogger = KotlinLogging.logger("FeatureDrawScope")
5454
}
5555
}
5656

@@ -64,12 +64,14 @@ public class ComposeFeatureDrawScope<T : Any>(
6464
private val painterCache: Map<PainterFeature<T>, Painter>,
6565
private val textMeasurer: TextMeasurer?,
6666
) : FeatureDrawScope<T>(state), DrawScope by drawScope {
67+
6768
override fun drawText(text: String, position: Offset, attributes: Attributes) {
68-
try {
69-
//TODO don't draw text that is not on screen
70-
drawText(textMeasurer ?: error("Text measurer not defined"), text, position)
71-
} catch (ex: Exception) {
72-
logger.error(ex) { "Failed to measure text" }
69+
if (position.x in 0f..size.width && position.y in 0f..size.height) {
70+
try {
71+
drawText(textMeasurer ?: error("Text measurer not defined"), text, position)
72+
} catch (ex: Exception) {
73+
logger.error(ex) { "Failed to measure text" }
74+
}
7375
}
7476
}
7577

@@ -82,7 +84,7 @@ public class ComposeFeatureDrawScope<T : Any>(
8284
}
8385

8486
@Composable
85-
public fun <T: Any> FeatureSet<T>.pointerCache(): Map<PainterFeature<T>, Painter> = key(features) {
87+
public fun <T : Any> FeatureSet<T>.pointerCache(): Map<PainterFeature<T>, Painter> = key(features) {
8688
features.values.filterIsInstance<PainterFeature<T>>().associateWith { it.getPainter() }
8789
}
8890

0 commit comments

Comments
 (0)