Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package jr.brian.commandpannel.system

/**
* The shared fonts contract, version 2: which fonts the applied look uses, read from [ThemeProvider] next to the
* version 1 palette ([SharedThemeContract]). The full spec is `docs/SHARED_THEME.md` in the Command Panel repo.
*
* Constants only, with no Android or Command Panel imports, so a client can copy this file as is. Version 2 is frozen
* the same way version 1 is: any change to the row means a new [VERSION].
*/
object SharedFontsContract {

/** The contract version this file describes, also returned in [COLUMN_VERSION]. */
const val VERSION = 2

/** The fonts row: `content://<package>.theme/fonts`. Returns one row. */
const val PATH = "fonts"

/** An imported font's file: `content://<package>.theme/fonts/<id>`, opened read-only by a trusted app only. */
const val FILE_PATH = "fonts"

/** MIME type of an imported font file (TrueType or OpenType). */
const val FILE_MIME_TYPE = "font/sfnt"

// ---- columns ----

/** Integer, always [VERSION]. */
const val COLUMN_VERSION = "version"

/** The applied look: a built-in look's name (`TERMINAL`) or `custom:<id>` for a saved look. */
const val COLUMN_LOOK_ID = "look_id"

/** The applied look's name as the user sees it: a saved look's own name, or a built-in look's display name. */
const val COLUMN_LOOK_NAME = "look_name"

/** Font for big numbers and titles, as a font value (see [PREFIX_BUNDLED] and [PREFIX_USER]), or null. */
const val COLUMN_DISPLAY_FONT = "display_font"

/** SHA-256 of the display font's file (lowercase hex) when it's a `user:` font, otherwise null. */
const val COLUMN_DISPLAY_FONT_SHA256 = "display_font_sha256"

/** Font for labels and body text, as a font value, or null. */
const val COLUMN_LABEL_FONT = "label_font"

/** SHA-256 of the label font's file (lowercase hex) when it's a `user:` font, otherwise null. */
const val COLUMN_LABEL_FONT_SHA256 = "label_font_sha256"

/** Every column, in the order a query without a projection returns them. */
val COLUMNS = listOf(
COLUMN_VERSION, COLUMN_LOOK_ID, COLUMN_LOOK_NAME,
COLUMN_DISPLAY_FONT, COLUMN_DISPLAY_FONT_SHA256,
COLUMN_LABEL_FONT, COLUMN_LABEL_FONT_SHA256,
)

// ---- font values ----

/** `bundled:<key>`: one of the bundled families below. The client ships the same open font files. */
const val PREFIX_BUNDLED = "bundled:"

/** `user:<id>`: a font the user imported, read from [FILE_PATH]`/<id>`. */
const val PREFIX_USER = "user:"

/** Barlow Condensed SemiBold, Bold and ExtraBold. */
const val BUNDLED_CONDENSED = "condensed"

/** Barlow Condensed ExtraBold only, for big numbers. */
const val BUNDLED_CONDENSED_DISPLAY = "condensed_display"

/** JetBrains Mono Regular and Bold. */
const val BUNDLED_MONO = "mono"

/** DSEG7 Classic Bold, seven-segment digits. */
const val BUNDLED_SEGMENT = "segment"

/** Baloo 2 ExtraBold. */
const val BUNDLED_ROUNDED = "rounded"

/** Black Ops One. */
const val BUNDLED_STENCIL = "stencil"

/** Bangers. */
const val BUNDLED_COMIC = "comic"

/** Michroma. */
const val BUNDLED_CHROME_WIDE = "chrome_wide"

/** Literata Regular and Italic. */
const val BUNDLED_SERIF = "serif"

/** Every bundled key. A key is never renamed or reused; a new bundled family adds a key. */
val BUNDLED_KEYS = listOf(
BUNDLED_CONDENSED, BUNDLED_CONDENSED_DISPLAY, BUNDLED_MONO, BUNDLED_SEGMENT, BUNDLED_ROUNDED,
BUNDLED_STENCIL, BUNDLED_COMIC, BUNDLED_CHROME_WIDE, BUNDLED_SERIF,
)
}
117 changes: 117 additions & 0 deletions app/src/main/java/jr/brian/commandpannel/system/SharedLookFonts.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package jr.brian.commandpannel.system

import androidx.compose.ui.text.font.FontFamily
import jr.brian.commandpannel.plugin.PanelPluginTrust
import jr.brian.commandpannel.ui.theme.BundledFont
import jr.brian.commandpannel.ui.theme.FontRef
import jr.brian.commandpannel.ui.theme.TccUiLook
import jr.brian.commandpannel.ui.theme.specForLook

/** The contract key for [font], or `null` for the system font. */
internal fun sharedBundledKey(font: BundledFont): String? = when (font) {
BundledFont.DEFAULT -> null
BundledFont.CONDENSED -> SharedFontsContract.BUNDLED_CONDENSED
BundledFont.CONDENSED_DISPLAY -> SharedFontsContract.BUNDLED_CONDENSED_DISPLAY
BundledFont.MONO -> SharedFontsContract.BUNDLED_MONO
BundledFont.SEGMENT -> SharedFontsContract.BUNDLED_SEGMENT
BundledFont.ROUNDED -> SharedFontsContract.BUNDLED_ROUNDED
BundledFont.STENCIL -> SharedFontsContract.BUNDLED_STENCIL
BundledFont.COMIC -> SharedFontsContract.BUNDLED_COMIC
BundledFont.CHROME_WIDE -> SharedFontsContract.BUNDLED_CHROME_WIDE
BundledFont.SERIF -> SharedFontsContract.BUNDLED_SERIF
}

/** A built-in look's family as a [FontRef], or `null` when it uses the system font. */
private fun FontFamily?.asBundledRef(): FontRef? =
this?.let { family -> BundledFont.entries.firstOrNull { it.family == family } }?.let { FontRef.Bundled(it) }

/** The look's label and display font choices before resolving, the way `CustomLook.toSpec` layers them. */
private fun SharedLook.fontRefs(): Pair<FontRef?, FontRef?> {
val base = specForLook(custom?.baseLook ?: builtIn ?: TccUiLook.DEFAULT).traits
val label = custom?.labelFont ?: base.labelFontFamily.asBundledRef()
val display = custom?.displayFont ?: base.displayFontFamily.asBundledRef()
return label to display
}

/** One font role's published value and, for an imported font, its file's SHA-256. */
private data class SharedFontValue(val value: String, val sha256: String?)

/** `null` for the system font or an imported font whose file is gone, which the overlay also draws in the system font. */
private fun FontRef?.toShared(userFontSha256: (String) -> String?): SharedFontValue? = when (this) {
null -> null
is FontRef.Bundled -> sharedBundledKey(font)?.let { SharedFontValue(SharedFontsContract.PREFIX_BUNDLED + it, null) }
is FontRef.User -> userFontSha256(id)?.let { SharedFontValue(SharedFontsContract.PREFIX_USER + id, it) }
}

/**
* The fonts row for [look]. A role without its own font falls back to the label font, as the overlay's text styles do,
* so a client never has to know the fallback rule. [userFontSha256] gives an imported font's digest, or `null` when its
* file is gone. [builtInName] gives a built-in look's display name.
*/
internal fun sharedFontsRow(
look: SharedLook,
builtInName: (TccUiLook) -> String,
userFontSha256: (String) -> String?,
): Map<String, Any?> {
val (labelRef, displayRef) = look.fontRefs()
val label = labelRef.toShared(userFontSha256)
val display = displayRef.toShared(userFontSha256) ?: label
return mapOf(
SharedFontsContract.COLUMN_VERSION to SharedFontsContract.VERSION,
SharedFontsContract.COLUMN_LOOK_ID to look.id,
SharedFontsContract.COLUMN_LOOK_NAME to (look.custom?.name ?: builtInName(look.builtIn ?: TccUiLook.DEFAULT)),
SharedFontsContract.COLUMN_DISPLAY_FONT to display?.value,
SharedFontsContract.COLUMN_DISPLAY_FONT_SHA256 to display?.sha256,
SharedFontsContract.COLUMN_LABEL_FONT to label?.value,
SharedFontsContract.COLUMN_LABEL_FONT_SHA256 to label?.sha256,
)
}

/** [row]'s values for [columns], in that order. Values may be null; an unknown column is the caller's error. */
internal fun selectSharedColumns(row: Map<String, Any?>, columns: Array<out String>): Array<Any?> =
Array(columns.size) { i ->
val column = columns[i]
require(row.containsKey(column)) { "unknown column: $column" }
row[column]
}

// UserFontStore names files by UUID; anything else (dots, slashes, encoded separators) is refused outright.
private val USER_FONT_ID = Regex("[0-9A-Fa-f-]{1,64}")

/** The imported font id a `fonts/<id>` path asks for, or `null` for any other path. */
internal fun sharedFontFileId(pathSegments: List<String>): String? =
pathSegments.takeIf { it.size == 2 && it[0] == SharedFontsContract.FILE_PATH }
?.get(1)?.takeIf { USER_FONT_ID.matches(it) }

/**
* The apps allowed to open imported font files, by package and pinned signing certificate (SHA-256, lowercase hex).
* Only Jarngreipr's release build in a release Command Panel; a debug Command Panel also serves Jarngreipr's debug build.
*/
internal object SharedFontReaders {
const val JARN_PACKAGE = "jr.brian.home"
const val JARN_DEBUG_PACKAGE = "jr.brian.home.debug"

/** Jarngreipr's release key, from `apksigner verify --print-certs`. */
const val JARN_RELEASE_CERT_SHA256 = "a9a6e4628ff67b218be69f39dd4c7843cdcbd565f76b31f93f0c90f3433d56db"

/** Jarngreipr's committed `app/debug.keystore`. */
const val JARN_DEBUG_CERT_SHA256 = "d954c2d6139facf3acf555a6e28664e15b0b8f8e93b5339ab8e1fc9c001419a6"

private val RELEASE = mapOf(JARN_PACKAGE to JARN_RELEASE_CERT_SHA256)
private val DEBUG = RELEASE + (JARN_DEBUG_PACKAGE to JARN_DEBUG_CERT_SHA256)

fun allowed(debugBuild: Boolean): Map<String, String> = if (debugBuild) DEBUG else RELEASE

/**
* True when one of the calling UID's packages is on [allowed] and every current signer of that package matches its
* pin. [signerDigests] reads a package's signers; an empty list (not installed, unreadable) is never trusted.
*/
fun isTrusted(
callerPackages: Collection<String>,
allowed: Map<String, String>,
signerDigests: (String) -> List<String>,
): Boolean = callerPackages.any { pkg ->
val pin = allowed[pkg] ?: return@any false
PanelPluginTrust.isTrusted(signerDigests(pkg), setOf(pin))
}
}
Loading
Loading