Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

Office engine: real fontconfig setup (bundled Office-metric fonts, aliases, cache dir); show when the engine rendered a file

Chethan6160060226b18bf
diff --git a/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineClient.kt b/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineClient.kt
index ab98f30..0b9153d 100644
--- a/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineClient.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineClient.kt
@@ -83,7 +83,7 @@ object OfficeEngineClient {
}
}
} ?: return Result(null, "Cannot open file")
- digest.update("$ext|${OfficeEngineStore.installedVersion(app)}".toByteArray())
+ digest.update("$ext|${OfficeEngineStore.installedVersion(app)}|fonts2".toByteArray())
val key = digest.digest().joinToString("") { "%02x".format(it) }.take(40)
// Password-protected output is never cached: the PDF is unencrypted.
val cached = File(cacheDir, "$key.pdf")
diff --git a/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineRuntime.kt b/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineRuntime.kt
index 3ddaf1f..fd72d68 100644
--- a/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineRuntime.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/office/OfficeEngineRuntime.kt
@@ -32,6 +32,7 @@ internal object OfficeEngineRuntime {
System.load(File(lib, "liblo-native-code.so").absolutePath)
val tmp = File(app.cacheDir, "office-tmp").apply { mkdirs() }
+ configureFonts(app, engineDir)
LibreOfficeKit.putenv("SAL_LOG=-WARN-INFO")
LibreOfficeKit.putenv("SAL_LOK_OPTIONS=compact_fonts")
LibreOfficeKit.putenv("TMPDIR=${tmp.absolutePath}")
@@ -48,6 +49,55 @@ internal object OfficeEngineRuntime {
return Office(handle).also { office = it }
}
+ /**
+ * The bundle ships its fontconfig file as `etc/fonts/fonts.conf.txt` (the name the upstream
+ * Android bootstrap renames on unpack), so fontconfig found no config at all, had no writable
+ * cache, and never saw the bundle's metric-compatible Office fonts — every document fell back
+ * to arbitrary system faces and looked no better than the built-in renderer. Write a real
+ * config that lists the system fonts *and* the engine's own fonts, maps the Microsoft core
+ * families to their metric twins, and gives fontconfig a cache dir; then point the engine at it.
+ */
+ private fun configureFonts(app: Context, engineDir: File) {
+ val fontsDir = File(engineDir, "etc/fonts").apply { mkdirs() }
+ val cache = File(app.cacheDir, "fontconfig").apply { mkdirs() }
+ val home = File(app.filesDir, "office-home").apply { mkdirs() }
+ val dirs = listOf("/system/fonts", "/product/fonts", "/system/product/fonts") +
+ listOf("user/fonts", "share/fonts", "share/fonts/truetype").map { File(engineDir, it) }
+ .filter { it.isDirectory }.map { it.absolutePath }
+ fun alias(from: String, to: String) =
+ " <alias binding=\"same\"><family>$from</family><prefer><family>$to</family></prefer></alias>\n"
+ val conf = buildString {
+ append("<?xml version=\"1.0\"?>\n<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n<fontconfig>\n")
+ dirs.forEach { append(" <dir>").append(it).append("</dir>\n") }
+ append(" <cachedir>").append(cache.absolutePath).append("</cachedir>\n")
+ append(alias("Calibri", "Carlito"))
+ append(alias("Cambria", "Caladea"))
+ append(alias("Arial", "Liberation Sans"))
+ append(alias("Helvetica", "Liberation Sans"))
+ append(alias("Arial Narrow", "Liberation Sans Narrow"))
+ append(alias("Times New Roman", "Liberation Serif"))
+ append(alias("Times", "Liberation Serif"))
+ append(alias("Courier New", "Liberation Mono"))
+ append(alias("Courier", "Liberation Mono"))
+ append(alias("sans-serif", "Roboto"))
+ append(alias("serif", "Noto Serif"))
+ append(alias("monospace", "Droid Sans Mono"))
+ append("</fontconfig>\n")
+ }
+ val confFile = File(fontsDir, "fonts.conf")
+ if (!confFile.isFile || confFile.readText() != conf) confFile.writeText(conf)
+ val env = listOf(
+ "FONTCONFIG_FILE" to confFile.absolutePath,
+ "FONTCONFIG_PATH" to fontsDir.absolutePath,
+ "XDG_CACHE_HOME" to app.cacheDir.absolutePath,
+ "HOME" to home.absolutePath
+ )
+ for ((k, v) in env) {
+ runCatching { android.system.Os.setenv(k, v, true) }
+ LibreOfficeKit.putenv("$k=$v")
+ }
+ }
+
/** Converts [input] to PDF at [output]. The input's extension drives format detection. */
fun convertToPdf(context: Context, input: File, output: File, password: String?) {
val office = ensureStarted(context)
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
index b065bf9..4ba6e0f 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
@@ -71,6 +71,8 @@ import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
+import com.chethan616.clearpdf.ui.theme.LiquidGlassColors
+import androidx.compose.material.icons.rounded.AutoAwesome
import androidx.compose.material.icons.rounded.ArrowBackIosNew
import androidx.compose.material.icons.rounded.PictureAsPdf
import androidx.compose.material.icons.rounded.Search
@@ -1095,6 +1097,35 @@ fun PdfViewerScreen(
}
}
+ // Brief confirmation that the optional Office engine rendered this file, so installing
+ // it has a visible effect (before, nothing told the user which renderer was used).
+ var showEngineChip by remember(state.renderedByOfficeEngine, state.document) {
+ mutableStateOf(state.renderedByOfficeEngine)
+ }
+ LaunchedEffect(showEngineChip) {
+ if (showEngineChip) { kotlinx.coroutines.delay(2800L); showEngineChip = false }
+ }
+ AnimatedVisibility(
+ visible = showEngineChip,
+ enter = fadeIn(spring(stiffness = 300f)),
+ exit = fadeOut(tween(220)),
+ modifier = Modifier.align(Alignment.TopCenter).statusBarsPadding().padding(top = 76.dp)
+ ) {
+ Row(
+ Modifier
+ .viewerGlass(contentBackdrop, viewerChromeGlass(isDarkMode), shape = { com.kyant.shapes.Capsule })
+ .padding(horizontal = 14.dp, vertical = 8.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ horizontalArrangement = Arrangement.spacedBy(7.dp)
+ ) {
+ Icon(Icons.Rounded.AutoAwesome, null, Modifier.size(16.dp), LiquidGlassColors.Teal)
+ BasicText(
+ stringResource(R.string.office_engine_rendered),
+ style = TextStyle(topFg, 13.sp, fontWeight = FontWeight.SemiBold)
+ )
+ }
+ }
+
// One-time Office engine suggestion after a built-in Office render. Glass fades only
// (translating glass re-runs its blur + lens).
AnimatedVisibility(
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
index 26367a1..06a74d6 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
@@ -165,7 +165,9 @@ data class PdfViewerUiState(
val currentMatchIndex: Int = -1,
// One-time "Improve fidelity with the Office engine" hint, shown after an Office file was
// rendered by the built-in renderers on a device that supports the optional engine.
- val showOfficeEngineHint: Boolean = false
+ val showOfficeEngineHint: Boolean = false,
+ /** True when the optional Office engine (LibreOffice) produced the pages on screen. */
+ val renderedByOfficeEngine: Boolean = false
)
class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModel() {
@@ -259,7 +261,7 @@ class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModel
else -> readableUri
}
}
- val (doc, _) = withContext(Dispatchers.IO) {
+ val (doc, renderedUri) = withContext(Dispatchers.IO) {
openDocumentWithFallback(context, sourceUri)
}
// A revoked share-intent grant fails the DISPLAY_NAME query on `uri` exactly the way
@@ -291,7 +293,8 @@ class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModel
exportMessage = null,
exportError = null,
lastExportedUri = null,
- showOfficeEngineHint = com.chethan616.clearpdf.office.OfficeEngine.shouldOfferHint(context, displayName)
+ showOfficeEngineHint = com.chethan616.clearpdf.office.OfficeEngine.shouldOfferHint(context, displayName),
+ renderedByOfficeEngine = renderedUri.path?.contains("/office-pdf/") == true
)
// The ORIGINAL uri, deliberately — not `openedUri`.
//
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index b480d5a..d6a44a6 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -676,4 +676,5 @@
<string name="sheet_formula_hint">Enter a value or =formula</string>
<string name="sheet_read_only">This file can be viewed but not edited</string>
<string name="sheet_invalid_value">That value isn\'t in this cell\'s list</string>
+ <string name="office_engine_rendered">Rendered by Office engine</string>
</resources>