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.

Menus unfold beside their button again; text to PDF and the new tools in the Tools tab

chethan616c5f4778b2187
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt
index cdd3801..8c00455 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/AppViewModel.kt
@@ -1178,6 +1178,14 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
fun openProtect(doc: OpenDocument) { toolScreen = ToolScreen.Protect(doc) }
/** Create PDF starts on its own page of sources (photos, files, a scan), as the reference's. */
fun openCreate() { toolScreen = ToolScreen.Create }
+
+ /** Create PDF opened straight on its text box (the Tools tab's Text to PDF). */
+ var createTyping = false
+
+ fun openCreateFromText() {
+ createTyping = true
+ toolScreen = ToolScreen.Create
+ }
fun openCrop(doc: OpenDocument) { toolScreen = ToolScreen.Crop(doc) }
fun startScan() { toolScreen = ToolScreen.Scan }
@@ -2366,6 +2374,35 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
open(uri)
}
+ /**
+ * Text to PDF: [words] (typed or pasted; Markdown headings, lists and emphasis are set as such)
+ * laid out as pages by the Word engine and saved to Downloads as "[name].pdf", then opened.
+ */
+ fun createFromText(name: String, words: String) = job(text(R.string.msg_busy_creating_pdf)) { eng ->
+ val title = name.trim().ifEmpty { words.lineSequence().firstOrNull { it.isNotBlank() }.orEmpty().trim().trimStart('#', ' ').take(40) }.ifEmpty { "Text" }
+ val dir = File(getApplication<Application>().cacheDir, "text-${System.nanoTime()}").apply { mkdirs() }
+ val source = File(dir, "text.md")
+ val out = tempFile("pdf")
+ try {
+ withContext(Dispatchers.IO) { source.writeText(words) }
+ eng.command {
+ val doc = open(source.absolutePath, null)
+ try {
+ exportOffice(doc.id, "pdf", out.absolutePath)
+ } finally {
+ runCatching { close(doc.id, true) }
+ }
+ }
+ val file = DocumentFiles.safeName("$title.pdf")
+ val uri = withContext(Dispatchers.IO) { files.saveToDownloads(out, file) }
+ tell(text(R.string.msg_saved_downloads, file))
+ open(uri)
+ } finally {
+ out.delete()
+ dir.deleteRecursively()
+ }
+ }
+
/** Pages [pages] (0-based) of [doc] as a new PDF in Downloads. */
fun extractPages(doc: OpenDocument, pages: List<Int>) = job(text(R.string.msg_busy_extracting)) { eng ->
val out = tempFile("pdf")
@@ -3039,6 +3076,9 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
FileTool.Protect -> openProtect(doc)
FileTool.Crop -> openCrop(doc)
FileTool.Ocr -> recognizeText(doc)
+ FileTool.ToText -> exportDocument(doc, "txt")
+ FileTool.FindFields -> detectFields(doc)
+ FileTool.Unlock -> if (doc.info.encrypted) removePassword(doc) else tell(text(R.string.msg_no_password))
}
}
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt
index 87fc4df..07e09e8 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/ToolScreens.kt
@@ -404,6 +404,7 @@ fun ExportScreen(vm: AppViewModel, doc: OpenDocument) {
@Composable
fun CreateSourcesScreen(vm: AppViewModel) {
val back = { vm.toolScreen = null }
+ var typing by remember { mutableStateOf(vm.createTyping.also { vm.createTyping = false }) }
ToolScaffold(stringResource(R.string.tools_create_title), back, action = null, actionEnabled = false, onAction = {}) {
Spacer(Modifier.height(8.dp))
com.chethan616.clearpdf.ui.glass.WashPanel(Modifier.padding(horizontal = 16.dp).fillMaxWidth()) {
@@ -415,9 +416,34 @@ fun CreateSourcesScreen(vm: AppViewModel) {
color = com.chethan616.clearpdf.ui.theme.clear.inkSoft, modifier = Modifier.padding(start = 26.dp, top = 24.dp, bottom = 8.dp),
)
com.chethan616.clearpdf.ui.glass.WashPanel(Modifier.padding(horizontal = 16.dp).fillMaxWidth()) {
+ SourceRow(LucideIcons.Type, stringResource(R.string.tools_create_from_text), com.chethan616.clearpdf.ui.theme.Candy.Sky) { typing = true }
SourceRow(LucideIcons.Camera, stringResource(R.string.tools_scan_document), com.chethan616.clearpdf.ui.theme.Candy.Tomato) { vm.toolScreen = null; vm.startScan() }
}
}
+ // Text to PDF: a name and the words, typed or pasted, set out as pages by the Word engine
+ if (typing) {
+ var name by remember { mutableStateOf("") }
+ var words by remember { mutableStateOf("") }
+ com.chethan616.clearpdf.ui.glass.GlassDialog(
+ onDismiss = { typing = false },
+ title = stringResource(R.string.tools_create_from_text),
+ confirm = stringResource(R.string.tools_create_action),
+ confirmEnabled = words.isNotBlank(),
+ onConfirm = {
+ typing = false
+ vm.toolScreen = null
+ vm.createFromText(name, words)
+ },
+ dismiss = stringResource(R.string.common_cancel),
+ ) {
+ com.chethan616.clearpdf.ui.glass.GlassTextField(name, { name = it }, stringResource(R.string.tools_text_name))
+ Spacer(Modifier.height(10.dp))
+ com.chethan616.clearpdf.ui.glass.GlassTextField(
+ words, { words = it }, stringResource(R.string.tools_text_hint),
+ modifier = Modifier.heightIn(min = 160.dp), singleLine = false,
+ )
+ }
+ }
}
@Composable
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt
index 9daf950..b48d18c 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/desk/Common.kt
@@ -95,7 +95,13 @@ object ToolCatalog {
val Protect = ToolDef("protect", LucideIcons.FileLock2, R.string.common_tool_set_password, R.string.home_about_protect, Candy.Grape, setOf("Edit")) { it.toolForFile(FileTool.Protect) }
val Photos = ToolDef("photos", LucideIcons.Images, R.string.desk_photos_to_pdf, R.string.home_about_create, Candy.Lemon, setOf("Create")) { it.pickPhotosToPdf() }
- val All = listOf(Edit, FillSign, Scan, Combine, Comment, ReadAloud, Organize, Compress, Export, Crop, Protect, Create)
+ // the tools asked for on the tracker, each one tap from the Tools tab
+ val TextToPdf = ToolDef("text2pdf", LucideIcons.Type, R.string.tool_text_to_pdf, R.string.home_about_text_to_pdf, Candy.Sky, setOf("Create", "Convert")) { it.openCreateFromText() }
+ val PdfToText = ToolDef("pdf2text", LucideIcons.FileText, R.string.tool_pdf_to_text, R.string.home_about_pdf_to_text, Candy.Lemon, setOf("Convert")) { it.toolForFile(FileTool.ToText) }
+ val FindFields = ToolDef("fields", LucideIcons.ScanText, R.string.tool_find_fields, R.string.home_about_find_fields, Candy.Bubblegum, setOf("Edit")) { it.toolForFile(FileTool.FindFields) }
+ val Unlock = ToolDef("unlock", LucideIcons.LockOpen, R.string.tools_unlock_action, R.string.home_about_unlock, Candy.Mint, setOf("Edit")) { it.toolForFile(FileTool.Unlock) }
+
+ val All = listOf(Edit, FillSign, Scan, Combine, Comment, ReadAloud, Organize, Compress, Export, Crop, Protect, Create, TextToPdf, PdfToText, FindFields, Unlock)
}
/**
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Motion.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Motion.kt
index 4424741..94fe20e 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Motion.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Motion.kt
@@ -212,22 +212,6 @@ object GlassMotion {
/** A small thing popping into place: fan chips, the + glyph. */
fun pop(): SpringSpec<Float> = spring(MotionSettings.damping(0.45f), MotionSettings.stiffness(500f), Rest)
- /**
- * A menu growing out of the button that opens it, as iOS 26's menus do: slower and heavier than
- * [unfold] (a spring of about 0.42 s response, 0.8 damping at the Normal setting), so the eye
- * follows the button becoming the menu; its height runs a touch softer behind ([growLag]).
- */
- fun grow(): SpringSpec<Float> = spring(MotionSettings.damping(0.82f), MotionSettings.stiffness(230f), Rest)
-
- fun growLag(): SpringSpec<Float> = spring(MotionSettings.damping(0.76f), MotionSettings.stiffness(190f), Rest)
-
- /**
- * The menu going back into its button, the same way it came: the glass draws in to the
- * button's box on the same slow spring, without the swing (a menu that bounced on its way out
- * would hold the screen while it settled).
- */
- fun shrink(): SpringSpec<Float> = spring(1f, MotionSettings.stiffness(230f), Rest)
-
/** A glass popover folding back into its button: quick, critically damped, so it lands without a step. */
fun fold(): SpringSpec<Float> = spring(1f, MotionSettings.stiffness(900f), Rest)
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Overlays.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Overlays.kt
index b125dbe..b80d8af 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Overlays.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/glass/Overlays.kt
@@ -410,25 +410,11 @@ private fun GlassMenuLayoutContent(
// folding back into the button is quick and never bounces
// (a spring both ways: closed mid-swing it carries its speed on instead of starting a new
// curve from a standstill, and the screens' curve from Settings no longer reaches the glass)
- //
- // Opened from a single button (⋯, More tools, a bar's glyph), the menu is that button grown:
- // it starts as the button's own box, at the button's place, and springs out to its size, over
- // the button, then folds back into it. Opened from anything larger (a card held down on the
- // desk) it unfolds beside its anchor as before.
- val buttonSide = with(LocalDensity.current) { 72.dp.toPx() }
- val morph = anchor.width > 0f && anchor.height > 0f && maxOf(anchor.width, anchor.height) <= buttonSide
- val appear = rememberOverlayAppear(
- enter = if (morph) GlassMotion.grow() else GlassMotion.unfold(),
- exit = if (morph) GlassMotion.shrink() else GlassMotion.fold(),
- )
+ val appear = rememberOverlayAppear(enter = GlassMotion.unfold(), exit = GlassMotion.fold())
val lag = remember { Animatable(0f) }
LaunchedEffect(leaving) {
- if (!leaving) lag.animateTo(1f, if (morph) GlassMotion.growLag() else GlassMotion.unfoldLag())
- else lag.animateTo(0f, if (morph) GlassMotion.shrink() else GlassMotion.fold())
+ if (!leaving) lag.animateTo(1f, GlassMotion.unfoldLag()) else lag.animateTo(0f, GlassMotion.fold())
}
- // where the card was put (the portal's space): the button's box inside it follows from this
- var placedAt by remember { mutableStateOf(Offset.Zero) }
- val menuCorner = with(LocalDensity.current) { 24.dp.toPx() }
Box(Modifier.fillMaxSize().clickable(remember { MutableInteractionSource() }, null, onClick = onDismiss))
val local = Rect(host.windowToLocal(anchor.topLeft), host.windowToLocal(anchor.bottomRight))
val margin = with(LocalDensity.current) { 12.dp.toPx() }
@@ -441,59 +427,13 @@ private fun GlassMenuLayoutContent(
.width(width)
.floatingLayer {
val p = appear.value
- if (morph && size.width > 0f && size.height > 0f) {
- // The glass itself grows: the card lies where it will rest, and what
- // shows of it is an outline that starts as the button's own box (its
- // place, its size, its roundness) and opens out to the card's, the
- // height a beat behind the width. Nothing is stretched, so the corners
- // stay true and the rows don't squash; only an outline on a finished
- // texture moves, which costs nothing.
- val px = p.coerceIn(0f, 1f)
- val py = lag.value.coerceIn(0f, 1f)
- val bl = (local.left - placedAt.x).coerceIn(0f, size.width)
- val bt = (local.top - placedAt.y).coerceIn(0f, size.height)
- val br = (local.right - placedAt.x).coerceIn(bl, size.width)
- val bb = (local.bottom - placedAt.y).coerceIn(bt, size.height)
- shape = GrownShape(
- lerp(bl, 0f, px), lerp(bt, 0f, py), lerp(br, size.width, px), lerp(bb, size.height, py),
- lerp(minOf(br - bl, bb - bt) / 2f, menuCorner, px),
- )
- clip = true
- // the swing past rest is the whole card swelling a little about the button
- scaleX = 1f + (p - px)
- scaleY = 1f + (lag.value - py)
- transformOrigin = TransformOrigin(
- if (size.width > 0f) ((bl + br) / 2f / size.width) else 0.5f,
- if (size.height > 0f) ((bt + bb) / 2f / size.height) else 0.5f,
- )
- // over the button within the first few frames (and off it in the last),
- // so the two glasses cross rather than swap
- alpha = (px * 5f).coerceIn(0f, 1f)
- } else {
- scaleX = lerp(0.5f, 1f, p).coerceAtLeast(0.2f)
- scaleY = lerp(0.35f, 1f, lag.value).coerceAtLeast(0.2f)
- alpha = (p * 1.6f).coerceIn(0f, 1f)
- transformOrigin = TransformOrigin(if (fromRight) 1f else 0f, if (fromBelow) 0f else 1f)
- }
+ scaleX = lerp(0.5f, 1f, p).coerceAtLeast(0.2f)
+ scaleY = lerp(0.35f, 1f, lag.value).coerceAtLeast(0.2f)
+ transformOrigin = TransformOrigin(if (fromRight) 1f else 0f, if (fromBelow) 0f else 1f)
+ alpha = (p * 1.6f).coerceIn(0f, 1f)
},
) {
- Column(
- Modifier
- .then(
- // what is written on the menu comes up out of the button as the glass
- // opens, and goes back first on the way in
- if (morph) Modifier.graphicsLayer {
- val px = appear.value.coerceIn(0f, 1f)
- alpha = ((px - 0.22f) / 0.5f).coerceIn(0f, 1f)
- val s = lerp(0.9f, 1f, px)
- scaleX = s
- scaleY = s
- transformOrigin = TransformOrigin(if (fromRight) 1f else 0f, if (fromBelow) 0f else 1f)
- } else Modifier,
- )
- .heightIn(max = maxHeight).verticalScroll(rememberScrollState()),
- content = content,
- )
+ Column(Modifier.heightIn(max = maxHeight).verticalScroll(rememberScrollState()), content = content)
}
},
modifier = Modifier.fillMaxSize(),
@@ -511,32 +451,19 @@ private fun GlassMenuLayoutContent(
val right = local.center.x > constraints.maxWidth / 2f
fromRight = right
val x = if (right) (local.right - p.width) else local.left
- // grown from a button, the menu lies over it (from its top edge down, or its bottom
- // edge up); beside anything else, a little clear of it
- val gap = if (morph) -local.height else 6f
- val below = local.bottom + gap + p.height < constraints.maxHeight - barBottom - margin
+ val below = local.bottom + 6f + p.height < constraints.maxHeight - barBottom - margin
fromBelow = below
- val y = if (below) local.bottom + gap else local.top - gap - p.height
+ val y = if (below) local.bottom + 6f else local.top - 6f - p.height
val minX = barLeft + margin
val maxX = (constraints.maxWidth - barRight - p.width - margin).coerceAtLeast(minX)
val minY = barTop + margin
val maxY = (constraints.maxHeight - barBottom - p.height - margin).coerceAtLeast(minY)
- val at = IntOffset(x.coerceIn(minX, maxX).roundToInt(), y.coerceIn(minY, maxY).roundToInt())
- placedAt = Offset(at.x.toFloat(), at.y.toFloat())
- p.place(at)
+ p.place(IntOffset(x.coerceIn(minX, maxX).roundToInt(), y.coerceIn(minY, maxY).roundToInt()))
}
}
}
}
-/** What shows of a menu as it grows out of its button: a rounded box, in the card's own space. */
-private class GrownShape(val left: Float, val top: Float, val right: Float, val bottom: Float, val radius: Float) : androidx.compose.ui.graphics.Shape {
- override fun createOutline(size: androidx.compose.ui.geometry.Size, layoutDirection: androidx.compose.ui.unit.LayoutDirection, density: androidx.compose.ui.unit.Density) =
- androidx.compose.ui.graphics.Outline.Rounded(
- androidx.compose.ui.geometry.RoundRect(left, top, right, bottom, androidx.compose.ui.geometry.CornerRadius(radius.coerceAtLeast(0f))),
- )
-}
-
/** A text field on glass: a rounded frosted well, a quiet [placeholder], the theme's ink. */
@Composable
fun GlassTextField(
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/home/HomeModel.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/HomeModel.kt
index 68273b4..7674c2d 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/home/HomeModel.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/HomeModel.kt
@@ -23,7 +23,7 @@ data class FileRow(
)
/** A tool that starts by choosing a PDF. */
-enum class FileTool { Edit, ReadAloud, FillSign, Organize, Compress, Export, Protect, Comment, Crop, Ocr }
+enum class FileTool { Edit, ReadAloud, FillSign, Organize, Compress, Export, Protect, Comment, Crop, Ocr, ToText, FindFields, Unlock }
/** "Just now", the time if today, "Yesterday", else a medium date. */
fun whenLabel(t: Long): String {
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 658b914..b75d5c1 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -168,6 +168,13 @@
<string name="home_about_compress">Make a PDF smaller</string>
<string name="home_about_export">Save a PDF as a document, spreadsheet, slides or images</string>
<string name="home_about_protect">Protect a PDF with a password</string>
+ <string name="tool_text_to_pdf">Text to PDF</string>
+ <string name="home_about_text_to_pdf">Type or paste text and save it as a PDF</string>
+ <string name="tool_pdf_to_text">PDF to text</string>
+ <string name="home_about_pdf_to_text">Save the text of a PDF as a plain text file</string>
+ <string name="home_about_find_fields">Turn the blanks of a drawn form into fields to fill</string>
+ <string name="home_about_unlock">Save a copy of a protected PDF that opens without its password</string>
+ <string name="msg_no_password">This PDF has no password to remove</string>
<!-- Search tab -->
<string name="home_search_your_files">Search your files</string>
@@ -260,6 +267,10 @@
<string name="tools_browse_files">Browse files</string>
<string name="tools_or_make_new">Or make a new one</string>
<string name="tools_scan_document">Scan a document</string>
+ <string name="tools_create_from_text">Type or paste text</string>
+ <string name="tools_create_action">Create PDF</string>
+ <string name="tools_text_name">Name (optional)</string>
+ <string name="tools_text_hint">Write or paste the text here</string>
<!-- Tool screens: Combine files -->
<!-- Title once two or more files are chosen -->