chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.5.0 Latest 5 packages
0
0
Android: filled icons and a tile's many mark as on iOS and the web
.changeset/android_draws_its_filled_icons_and_many_mark_as_ios_and_the_web.mdnew+7−0
| @@ -0,0 +1,7 @@ | ||
--- | ||
default: patch | ||
--- | ||
| ||
# Android draws its filled icons and a tile's many mark as iOS and the web do | ||
| ||
HUD's Android kit draws an icon filled (a `Panes` icon chosen, an action done, an `Icon` glyph) in its filled form: a grid as nine squares and a clock with its hands cut out of a disc, as iOS's `square.grid.3x3.fill` and `clock.fill` draw them, and a release's tag with its hole. Before, it filled the outline over what was inside, so a chosen grid or clock was a blank square or circle. An icon with no filled form is drawn as it is plain. Android draws every icon the catalog allows, the frame's `imagePlus`, `logIn` and `logOut` too. An `Icon`'s `reply` is the replied-to arrow, as on the web. A `Covers` tile holding more than one picture is marked with the web's stacked squares, white with a soft shadow, where Android drew a box like a checkbox. | ||
android/hudkit/src/main/java/place/hud/kit/support/Glyphs.kt+71−7
| @@ -6,6 +6,7 @@ import androidx.compose.material3.LocalContentColor | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.PathFillType | ||
import androidx.compose.ui.graphics.SolidColor | ||
import androidx.compose.ui.graphics.StrokeCap | ||
import androidx.compose.ui.graphics.StrokeJoin | ||
| @@ -32,6 +33,7 @@ object Glyphs { | ||
"pin" to listOf("M12 17v5", "M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z"), | ||
"star" to listOf("M12 2.5l2.94 5.96 6.56.95-4.75 4.63 1.12 6.54L12 17.5l-5.87 3.08 1.12-6.54L2.5 9.41l6.56-.95L12 2.5Z"), | ||
"play" to listOf("M6 3.5v17l14-8.5-14-8.5Z"), | ||
"pause" to listOf("M6 4h4v16H6Z", "M14 4h4v16h-4Z"), | ||
"comment" to listOf("M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"), | ||
"quote" to listOf("M16 3a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2 1 1 0 0 1 1 1v1a2 2 0 0 1-2 2 1 1 0 0 0-1 1v2a1 1 0 0 0 1 1 6 6 0 0 0 6-6V5a2 2 0 0 0-2-2z", "M5 3a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2 1 1 0 0 1 1 1v1a2 2 0 0 1-2 2 1 1 0 0 0-1 1v2a1 1 0 0 0 1 1 6 6 0 0 0 6-6V5a2 2 0 0 0-2-2z"), | ||
"photo" to listOf("M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2Z", "M7 9a2 2 0 1 0 4 0a2 2 0 1 0-4 0", "m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"), | ||
| @@ -61,6 +63,36 @@ object Glyphs { | ||
"chevronUp" to listOf("m18 15-6-6-6 6"), | ||
"chevronRight" to listOf("m9 18 6-6-6-6"), | ||
"settings" to listOf("M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z", "M9 12a3 3 0 1 0 6 0a3 3 0 1 0-6 0"), | ||
"imagePlus" to listOf("M16 5h6", "M19 2v6", "M21 11.5V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h7.5", "m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21", "M7 9a2 2 0 1 0 4 0a2 2 0 1 0-4 0"), | ||
"logOut" to listOf("m16 17 5-5-5-5", "M21 12H9", "M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"), | ||
"logIn" to listOf("m10 17 5-5-5-5", "M15 12H3", "M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"), | ||
) | ||
| ||
/** | ||
* The stroked icons whose filled form is their own outline filled: one | ||
* closed shape each, with nothing inside it that filling would hide. | ||
*/ | ||
private val fillsAsDrawn = setOf("heart", "star", "play", "pause", "comment", "reply", "pin", "notification", "people", "userPlus") | ||
| ||
/** | ||
* Filled forms of their own, for the stroked icons with a detail inside | ||
* their outline (a clock's hands, a grid's lines, a tag's hole), as SF | ||
* Symbols' `.fill` symbols cut it out: what filling their outline would | ||
* hide is a hole instead (even-odd). Each part filled, and stroked as the | ||
* plain icon is where `stroke` says, so it spans what the plain one does. | ||
*/ | ||
private val solids: Map<String, List<Part>> = mapOf( | ||
// Nine squares, as square.grid.3x3.fill. | ||
"grid" to listOf(Part(gridCells(), stroke = false)), | ||
// A disc reaching the stroked circle's outer edge, its hands cut out, as clock.fill. | ||
"clock" to listOf( | ||
Part("M12 1a11 11 0 1 0 0 22a11 11 0 1 0 0-22Z M11 6a1 1 0 0 1 2 0v5.383l3.447 1.723a1 1 0 0 1-.894 1.788l-4-2A1 1 0 0 1 11 12Z", stroke = false), | ||
), | ||
// The tag, its hole cut out, as tag.fill. | ||
"release" to listOf( | ||
Part("$TAG M8.5 7a1.5 1.5 0 1 1-3 0a1.5 1.5 0 1 1 3 0Z", stroke = false), | ||
Part(TAG, fill = false), | ||
), | ||
) | ||
| ||
/** Octicons', filled. */ | ||
| @@ -85,25 +117,39 @@ object Glyphs { | ||
| ||
/** | ||
* The icon of a catalog name, or null where there is none; `filled`, a | ||
* heart or a star the account gave, filled in the text's colour. | ||
* heart or a star the account gave, a pane chosen, in its filled form | ||
* where it has one (`fillsAsDrawn`, `solids`), else as it is drawn | ||
* plain: never its outline filled over what is inside it. | ||
*/ | ||
fun vector(name: String?, filled: Boolean = false): ImageVector? { | ||
if (name == null) return null | ||
solid[name]?.let { return vector(it, filled = true) } | ||
val key = if (filled) "$name:filled" else name | ||
made[key]?.let { return it } | ||
val built = strokes[name]?.let { stroked(name, it, filled) } ?: fills[name]?.let { octicon(name, it) } ?: return null | ||
val built = fills[name]?.let { octicon(name, it) } ?: run { | ||
val paths = strokes[name] ?: return null | ||
val parts = when { | ||
filled && name in solids -> solids.getValue(name) | ||
filled && name in fillsAsDrawn -> paths.map { Part(it) } | ||
else -> paths.map { Part(it, fill = false) } | ||
} | ||
drawn(name, parts) | ||
} | ||
made[key] = built | ||
return built | ||
} | ||
| ||
private fun stroked(name: String, paths: List<String>, filled: Boolean): ImageVector { | ||
/** One path of an icon on Lucide's 24-unit grid: filled (even-odd) and stroked as Lucide's are. */ | ||
private class Part(val d: String, val fill: Boolean = true, val stroke: Boolean = true) | ||
| ||
private fun drawn(name: String, parts: List<Part>): ImageVector { | ||
val builder = ImageVector.Builder(name, 24.dp, 24.dp, 24f, 24f) | ||
for (d in paths) { | ||
for (part in parts) { | ||
builder.addPath( | ||
pathData = PathParser().parsePathString(d).toNodes(), | ||
fill = if (filled) SolidColor(Color.Black) else null, | ||
stroke = SolidColor(Color.Black), | ||
pathData = PathParser().parsePathString(part.d).toNodes(), | ||
pathFillType = PathFillType.EvenOdd, | ||
fill = if (part.fill) SolidColor(Color.Black) else null, | ||
stroke = if (part.stroke) SolidColor(Color.Black) else null, | ||
strokeLineWidth = 2f, | ||
strokeLineCap = StrokeCap.Round, | ||
strokeLineJoin = StrokeJoin.Round, | ||
| @@ -121,6 +167,24 @@ object Glyphs { | ||
} | ||
} | ||
| ||
/** Lucide's tag, the release icon's outline. */ | ||
private const val TAG = "M12 2H2v10l9.29 9.29c.94.94 2.48.94 3.42 0l6.58-6.58c.94-.94.94-2.48 0-3.42L12 2Z" | ||
| ||
/** A filled grid's nine squares, three by three across the 20 units its stroked one spans, 1.5 apart. */ | ||
private fun gridCells(): String { | ||
val gap = 1.5f | ||
val side = (20f - 2 * gap) / 3 | ||
val r = 1.5f | ||
val s = side - 2 * r | ||
return buildString { | ||
for (row in 0 until 3) for (col in 0 until 3) { | ||
val x = 2f + col * (side + gap) | ||
val y = 2f + row * (side + gap) | ||
append("M${x + r} ${y}h${s}a$r $r 0 0 1 $r ${r}v${s}a$r $r 0 0 1-$r ${r}h-${s}a$r $r 0 0 1-$r-${r}v-${s}a$r $r 0 0 1 $r-${r}Z") | ||
} | ||
} | ||
} | ||
| ||
/** | ||
* An icon of a catalog name in the text's colour (or `tint`), sized to sit | ||
* with text; nothing where there is no icon of that name. Read aloud as | ||
android/hudkit/src/main/java/place/hud/kit/tree/Marks.kt+8−4
| @@ -124,19 +124,23 @@ private fun hexColor(hex: String): Color? { | ||
return Color(0xFF000000 or rgb) | ||
} | ||
| ||
/** Each glyph's name, as the web renderer's `Icon` draws Lucide's; a pull, an issue and a repository the Octicons knotless draws them with. */ | ||
private val iconGlyphs = mapOf( | ||
/** | ||
* Each glyph's name, as the web renderer's `Icon` draws Lucide's (a reply | ||
* the replied-to arrow, as there); a pull, an issue and a repository the | ||
* Octicons knotless draws them with. | ||
*/ | ||
internal val iconGlyphs = mapOf( | ||
"issue" to "issue", "pull" to "pull", "review" to "review", "notification" to "notification", "repo" to "repo", | ||
"star" to "star", "check" to "check", "release" to "release", "activity" to "activity", "search" to "search", | ||
"people" to "people", "feed" to "feed", "heart" to "heart", "follow" to "userPlus", "comment" to "comment", | ||
"reply" to "reply", "mention" to "atSign", | ||
"reply" to "repliedTo", "mention" to "atSign", | ||
) | ||
| ||
/** The glyphs drawn filled on their square, as iOS's `.fill` symbols are. */ | ||
private val filledIcons = setOf("notification", "star", "release", "people") | ||
| ||
/** The glyphs a glyph alone fills, where its symbol has a fill, as Grain draws a notification's reason. */ | ||
private val filledGlyphs = setOf("heart", "follow", "comment", "reply", "notification", "star", "release", "people") | ||
private val filledGlyphs = setOf("heart", "follow", "comment", "notification", "star", "release", "people") | ||
| ||
/** | ||
* A glyph's colour by its tint, as text in that tint: red the danger colour | ||
android/hudkit/src/main/java/place/hud/kit/tree/Media.kt+47−29
| @@ -37,7 +37,6 @@ import place.hud.kit.parts.widenedBy | ||
import place.hud.kit.support.Glyph | ||
import java.time.Instant | ||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.border | ||
import androidx.compose.foundation.clickable | ||
| @@ -67,11 +66,12 @@ import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.draw.clipToBounds | ||
import androidx.compose.ui.geometry.CornerRadius | ||
import androidx.compose.ui.draw.drawWithCache | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.drawscope.DrawScope | ||
import androidx.compose.ui.graphics.asAndroidPath | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.graphics.vector.PathParser | ||
import androidx.compose.ui.semantics.Role | ||
import androidx.compose.ui.semantics.clearAndSetSemantics | ||
import androidx.compose.ui.semantics.contentDescription | ||
| @@ -696,38 +696,56 @@ private fun CoverTile(cover: JsonElement, name: String) { | ||
val source = Source.of(cover["src"]) | ||
Picture(source, Modifier.fillMaxSize(), placeholder = if (source == null) "No picture" else "Image unavailable") | ||
if (cover["many"].isTrue) { | ||
ManyMark(Modifier.align(Alignment.TopEnd).padding(6.dp).size(14.dp)) | ||
ManyMark(Modifier.align(Alignment.TopEnd).padding(Space.s2).size(18.dp)) | ||
} | ||
} | ||
} | ||
} | ||
| ||
/** Two squares, one over the other: a cover that holds more than one picture. */ | ||
/** | ||
* Stacked squares, as the web's tile draws them (`CoverTile`): the back | ||
* one's corner stroked, the front one filled, white with a soft shadow, so | ||
* it reads on any picture; on Lucide's 24-unit grid. Drawn once a size into | ||
* a bitmap, its shadow's blur with it, so a frame only copies it. | ||
*/ | ||
@Composable | ||
private fun ManyMark(modifier: Modifier) { | ||
Canvas(modifier) { | ||
val side = size.minDimension * 0.72f | ||
val shadow = Color.Black.copy(alpha = 0.45f) | ||
fun square(at: Offset, color: Color) = | ||
drawRoundRect(color, at, Size(side, side), CornerRadius(side * 0.18f)) | ||
val back = Offset(size.width - side, 0f) | ||
val front = Offset(0f, size.height - side) | ||
square(back + Offset(0f, 1f), shadow) | ||
square(back, Color.White) | ||
square(front + Offset(0f, 1f), shadow) | ||
square(front, Color.White) | ||
drawFrontEdge(front, side) | ||
} | ||
} | ||
| ||
/** The line between the two squares, so the front reads as in front. */ | ||
private fun DrawScope.drawFrontEdge(at: Offset, side: Float) { | ||
drawRoundRect( | ||
Color.Black.copy(alpha = 0.3f), | ||
at, | ||
Size(side, side), | ||
CornerRadius(side * 0.18f), | ||
style = androidx.compose.ui.graphics.drawscope.Stroke(width = 1f), | ||
Spacer( | ||
modifier.drawWithCache { | ||
val unit = size.minDimension / 24f | ||
val blur = 2.dp.toPx() | ||
// Room for the shadow around the squares. | ||
val pad = 2 * blur | ||
val place = android.graphics.Matrix().apply { | ||
setScale(unit, unit) | ||
postTranslate(pad, pad) | ||
} | ||
fun path(d: String) = PathParser().parsePathString(d).toPath().asAndroidPath().apply { transform(place) } | ||
val back = path("M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2") | ||
val front = path("M10 8h10a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H10a2 2 0 0 1-2-2V10a2 2 0 0 1 2-2Z") | ||
val paint = android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG).apply { | ||
color = android.graphics.Color.WHITE | ||
strokeWidth = 2f * unit | ||
strokeCap = android.graphics.Paint.Cap.ROUND | ||
strokeJoin = android.graphics.Paint.Join.ROUND | ||
} | ||
val bitmap = android.graphics.Bitmap.createBitmap( | ||
(size.width + 2 * pad).toInt().coerceAtLeast(1), | ||
(size.height + 2 * pad).toInt().coerceAtLeast(1), | ||
android.graphics.Bitmap.Config.ARGB_8888, | ||
) | ||
val canvas = android.graphics.Canvas(bitmap) | ||
// The shadows first, under both squares, then the squares over them. | ||
for (shadowed in listOf(true, false)) { | ||
if (shadowed) paint.setShadowLayer(blur, 0f, 0f, 0x80000000.toInt()) else paint.clearShadowLayer() | ||
paint.style = android.graphics.Paint.Style.STROKE | ||
canvas.drawPath(back, paint) | ||
paint.style = android.graphics.Paint.Style.FILL_AND_STROKE | ||
canvas.drawPath(front, paint) | ||
} | ||
val image = bitmap.asImageBitmap() | ||
onDrawBehind { drawImage(image, topLeft = Offset(-pad, -pad)) } | ||
}, | ||
) | ||
} | ||
| ||
android/hudkit/src/test/java/place/hud/kit/GlyphsTest.ktnew+223−0
| @@ -0,0 +1,223 @@ | ||
package place.hud.kit | ||
| ||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import android.graphics.Matrix | ||
import android.graphics.Paint | ||
import androidx.compose.ui.graphics.PathFillType | ||
import androidx.compose.ui.graphics.StrokeCap | ||
import androidx.compose.ui.graphics.StrokeJoin | ||
import androidx.compose.ui.graphics.asAndroidPath | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.graphics.vector.VectorGroup | ||
import androidx.compose.ui.graphics.vector.VectorPath | ||
import androidx.compose.ui.graphics.vector.toPath | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.jsonArray | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.GraphicsMode | ||
import place.hud.kit.support.Glyphs | ||
import place.hud.kit.tree.iconGlyphs | ||
import java.io.File | ||
| ||
// Every icon the catalog lets a view or an app file name, as the JSON | ||
// Schemas list them (schemas/json, written from catalog.rs), drawn on | ||
// Android plain and filled (a pane chosen, an action done), and drawn | ||
// through Robolectric's native graphics to check its filled form: never its | ||
// outline filled over the details inside it (a chosen grid a white square). | ||
| ||
@RunWith(RobolectricTestRunner::class) | ||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
class GlyphsTest { | ||
private fun schema(name: String): JsonElement = | ||
Json.parseToJsonElement(File(System.getProperty("hud.schemas"), "$name.json").readText()) | ||
| ||
/** Each icon a view's components may name, by the glyph Android draws it with: an `Icon`'s `name` through its own names. */ | ||
private fun viewIcons(): Set<String> { | ||
val names = mutableSetOf<String>() | ||
for (rule in schema("view").jsonObject["\$defs"]!!.jsonObject["node"]!!.jsonObject["allOf"]!!.jsonArray) { | ||
val component = rule.jsonObject["if"]?.jsonObject?.get("properties")?.jsonObject?.get("component")?.jsonObject?.get("const")?.jsonPrimitive?.content | ||
val props = rule.jsonObject["then"]?.jsonObject?.get("properties")?.jsonObject?.get("props")?.jsonObject?.get("properties")?.jsonObject ?: continue | ||
if (component == "Icon") { | ||
val allowed = props["name"]!!.jsonObject["anyOf"]!!.jsonArray[0].jsonObject["enum"]!!.jsonArray.map { it.jsonPrimitive.content } | ||
assertTrue(allowed.isNotEmpty()) | ||
for (name in allowed) names += iconGlyphs[name] ?: error("No glyph for Icon's $name") | ||
} else { | ||
for (key in listOf("icon", "statIcon")) { | ||
props[key]?.jsonObject?.get("enum")?.jsonArray?.forEach { names += it.jsonPrimitive.content } | ||
} | ||
} | ||
} | ||
return names | ||
} | ||
| ||
/** Each icon an app file's frame may give its action and its account's items (`catalog::FRAME_ICONS`). */ | ||
private fun frameIcons(): Set<String> { | ||
val names = mutableSetOf<String>() | ||
fun walk(e: JsonElement) { | ||
when (e) { | ||
is JsonObject -> { | ||
(e["icon"] as? JsonObject)?.get("examples")?.jsonArray?.forEach { names += it.jsonPrimitive.content } | ||
e.values.forEach(::walk) | ||
} | ||
is JsonArray -> e.forEach(::walk) | ||
else -> {} | ||
} | ||
} | ||
walk(schema("app")) | ||
return names | ||
} | ||
| ||
@Test | ||
fun everyCatalogIconIsDrawnPlainAndFilled() { | ||
val view = viewIcons() | ||
val frame = frameIcons() | ||
// The schemas' lists as the catalog has them: Panes' icons, Action's, and the frame's. | ||
assertTrue(view.containsAll(listOf("grid", "heart", "clock", "feed", "people", "pinned", "repliedTo", "atSign", "userPlus"))) | ||
assertTrue(frame.containsAll(listOf("imagePlus", "logOut", "logIn", "compass", "settings"))) | ||
for (name in view + frame) { | ||
assertTrue("no glyph for $name", Glyphs.has(name)) | ||
assertNotNull("no plain $name", Glyphs.vector(name)) | ||
assertNotNull("no filled $name", Glyphs.vector(name, filled = true)) | ||
} | ||
} | ||
| ||
@Test | ||
fun noFilledIconHidesItsDetails() { | ||
for (name in viewIcons() + frameIcons()) { | ||
val plain = Ink(Glyphs.vector(name)!!) | ||
val filled = Ink(Glyphs.vector(name, filled = true)!!) | ||
if (!plain.detailed) continue | ||
// Of what the plain icon's outline holds, how much the filled one leaves unpainted: none is a blob. | ||
val inside = plain.silhouette | ||
val open = inside.indices.count { inside[it] && !filled.ink[it] } | ||
assertTrue("$name filled is a blob: ${open * 100 / inside.count { it }}% open", open * 100 >= inside.count { it }) | ||
} | ||
} | ||
| ||
@Test | ||
fun aChosenGridAndClockAreTheirFilledForms() { | ||
// Nine squares, as square.grid.3x3.fill; a disc with its hands cut out, as clock.fill. | ||
assertEquals(9, Ink(Glyphs.vector("grid", filled = true)!!).components) | ||
val clock = Ink(Glyphs.vector("clock", filled = true)!!) | ||
assertEquals(1, clock.components) | ||
assertEquals(1, clock.holes) | ||
// A tag with its hole, as tag.fill. | ||
assertEquals(1, Ink(Glyphs.vector("release", filled = true)!!).holes) | ||
// A heart filled whole; one with no filled form drawn as it is plain. | ||
assertEquals(0, Ink(Glyphs.vector("heart", filled = true)!!).holes) | ||
assertTrue(Ink(Glyphs.vector("feed", filled = true)!!).ink.contentEquals(Ink(Glyphs.vector("feed")!!).ink)) | ||
} | ||
} | ||
| ||
/** An icon drawn at four times its 24 units, and what its painted pixels make. */ | ||
private class Ink(vector: ImageVector) { | ||
val ink = BooleanArray(SIDE * SIDE) | ||
| ||
init { | ||
val bitmap = Bitmap.createBitmap(SIDE, SIDE, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(bitmap) | ||
draw(canvas, vector.root, Matrix().apply { setScale(SIDE / vector.viewportWidth, SIDE / vector.viewportHeight) }) | ||
for (y in 0 until SIDE) for (x in 0 until SIDE) ink[y * SIDE + x] = (bitmap.getPixel(x, y) ushr 24) > 127 | ||
} | ||
| ||
private fun draw(canvas: Canvas, group: VectorGroup, outer: Matrix) { | ||
val m = Matrix().apply { | ||
setScale(group.scaleX, group.scaleY) | ||
postTranslate(group.translationX, group.translationY) | ||
postConcat(outer) | ||
} | ||
for (node in group) { | ||
when (node) { | ||
is VectorGroup -> draw(canvas, node, m) | ||
is VectorPath -> { | ||
val path = node.pathData.toPath().asAndroidPath().apply { | ||
fillType = if (node.pathFillType == PathFillType.EvenOdd) android.graphics.Path.FillType.EVEN_ODD else android.graphics.Path.FillType.WINDING | ||
transform(m) | ||
} | ||
val scale = m.mapRadius(1f) | ||
if (node.fill != null) canvas.drawPath(path, Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }) | ||
if (node.stroke != null) { | ||
canvas.drawPath( | ||
path, | ||
Paint(Paint.ANTI_ALIAS_FLAG).apply { | ||
style = Paint.Style.STROKE | ||
strokeWidth = node.strokeLineWidth * scale | ||
strokeCap = if (node.strokeLineCap == StrokeCap.Round) Paint.Cap.ROUND else Paint.Cap.BUTT | ||
strokeJoin = if (node.strokeLineJoin == StrokeJoin.Round) Paint.Join.ROUND else Paint.Join.MITER | ||
}, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
| ||
/** The unpainted pixels reached from the edge. */ | ||
private val outside: BooleanArray = BooleanArray(SIDE * SIDE).also { out -> | ||
val queue = ArrayDeque<Int>() | ||
for (i in 0 until SIDE) for (at in listOf(i, (SIDE - 1) * SIDE + i, i * SIDE, i * SIDE + SIDE - 1)) { | ||
if (!ink[at] && !out[at]) { out[at] = true; queue += at } | ||
} | ||
while (queue.isNotEmpty()) { | ||
for (n in neighbours(queue.removeFirst())) if (!ink[n] && !out[n]) { out[n] = true; queue += n } | ||
} | ||
} | ||
| ||
/** What its outline holds: everything but what is outside it. */ | ||
val silhouette: BooleanArray get() = BooleanArray(SIDE * SIDE) { !outside[it] } | ||
| ||
/** Its painted pieces, and those no unpainted pixel outside it touches (a clock's hands). */ | ||
private val pieces: List<Set<Int>> = regions { ink[it] } | ||
| ||
/** Its unpainted pieces inside it: a ring's middle, a grid's cells. */ | ||
private val holePieces: List<Set<Int>> = regions { !ink[it] && !outside[it] } | ||
| ||
val components: Int get() = pieces.size | ||
val holes: Int get() = holePieces.size | ||
| ||
/** Whether filling its outline would hide something inside it: more than one hole, or a piece standing in one. */ | ||
val detailed: Boolean get() = holes >= 2 || pieces.any { piece -> piece.none { at -> neighbours(at).any { outside[it] } } } | ||
| ||
private fun regions(member: (Int) -> Boolean): List<Set<Int>> { | ||
val seen = BooleanArray(SIDE * SIDE) | ||
val found = mutableListOf<Set<Int>>() | ||
for (start in 0 until SIDE * SIDE) { | ||
if (seen[start] || !member(start)) continue | ||
val region = mutableSetOf(start) | ||
val queue = ArrayDeque(listOf(start)) | ||
seen[start] = true | ||
while (queue.isNotEmpty()) { | ||
for (n in neighbours(queue.removeFirst())) if (!seen[n] && member(n)) { seen[n] = true; region += n; queue += n } | ||
} | ||
// A speck of anti-aliasing is no piece. | ||
if (region.size > 4) found += region | ||
} | ||
return found | ||
} | ||
| ||
private fun neighbours(at: Int): List<Int> { | ||
val x = at % SIDE | ||
val y = at / SIDE | ||
return buildList { | ||
if (x > 0) add(at - 1) | ||
if (x < SIDE - 1) add(at + 1) | ||
if (y > 0) add(at - SIDE) | ||
if (y < SIDE - 1) add(at + SIDE) | ||
} | ||
} | ||
| ||
companion object { | ||
const val SIDE = 96 | ||
} | ||
} | ||
android/hudkit/build.gradle.kts+6−2
| @@ -41,8 +41,12 @@ android { | ||
unitTests { | ||
isReturnDefaultValues = true | ||
isIncludeAndroidResources = true | ||
// The tests read the answers iOS's tests read: one fixture for both ports. | ||
all { it.systemProperty("hud.fixtures", rootProject.file("../ios/HUDKit/Tests/HUDKitTests/Fixtures").path) } | ||
// The tests read the answers iOS's tests read: one fixture for both ports; and | ||
// the JSON Schemas of a view and an app file, for the icons the catalog allows. | ||
all { | ||
it.systemProperty("hud.fixtures", rootProject.file("../ios/HUDKit/Tests/HUDKitTests/Fixtures").path) | ||
it.systemProperty("hud.schemas", rootProject.file("../schemas/json").path) | ||
} | ||
} | ||
} | ||
| ||