diff --git a/app/src/main/java/space/bloggin/scribe/Export.kt b/app/src/main/java/space/bloggin/scribe/Export.kt new file mode 100644 index 0000000..f802f2a --- /dev/null +++ b/app/src/main/java/space/bloggin/scribe/Export.kt @@ -0,0 +1,118 @@ +package space.bloggin.scribe + +import android.app.Activity +import android.content.Intent +import android.graphics.Paint +import android.graphics.Typeface +import android.graphics.pdf.PdfDocument +import androidx.core.content.FileProvider +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** Turns notes into a PDF or Word (.docx) file and hands it to the share sheet. */ +object Export { + const val PDF_MIME = "application/pdf" + const val DOCX_MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" + + private fun heading(n: Note) = Ui.fmtDate(n.created) + + fun share(act: Activity, notes: List, name: String, docx: Boolean) { + val list = notes.filter { it.text.isNotBlank() } + if (list.isEmpty()) { + android.widget.Toast.makeText(act, "Nothing to export yet", android.widget.Toast.LENGTH_SHORT).show() + return + } + val dir = File(act.cacheDir, "exports").also { it.mkdirs() } + dir.listFiles()?.forEach { it.delete() } + val safe = name.replace(Regex("[^A-Za-z0-9 ._-]"), "").trim().ifEmpty { "Notes" } + val f = File(dir, safe + if (docx) ".docx" else ".pdf") + if (docx) writeDocx(f, list) else writePdf(f, list) + val uri = FileProvider.getUriForFile(act, act.packageName + ".files", f) + val i = Intent(Intent.ACTION_SEND) + .setType(if (docx) DOCX_MIME else PDF_MIME) + .putExtra(Intent.EXTRA_STREAM, uri) + .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + act.startActivity(Intent.createChooser(i, if (docx) "Save or send Word file" else "Save or send PDF")) + } + + fun fileName(notes: List): String = + if (notes.size == 1) "Note " + java.text.SimpleDateFormat("yyyy-MM-dd HHmm", java.util.Locale.US).format(java.util.Date(notes[0].created)) + else "Hey Dumby notes " + java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.US).format(java.util.Date()) + + // ---------- PDF ---------- + private fun writePdf(out: File, notes: List) { + val pageW = 612 + val pageH = 792 + val margin = 54f + val body = Paint().apply { textSize = 12f; color = 0xFF111111.toInt(); isAntiAlias = true } + val head = Paint().apply { textSize = 11f; color = 0xFF6B6B73.toInt(); isAntiAlias = true; typeface = Typeface.DEFAULT_BOLD } + val lineH = 17f + val maxW = pageW - margin * 2 + val doc = PdfDocument() + var pageNo = 0 + var page: PdfDocument.Page? = null + var y = 0f + fun newPage() { + page?.let { doc.finishPage(it) } + pageNo++ + page = doc.startPage(PdfDocument.PageInfo.Builder(pageW, pageH, pageNo).create()) + y = margin + } + fun ensure(h: Float) { if (page == null || y + h > pageH - margin) newPage() } + + for ((idx, n) in notes.withIndex()) { + ensure(lineH * 3) + if (idx > 0) y += lineH + page!!.canvas.drawText(heading(n), margin, y + 11f, head) + y += lineH + 4f + for (para in n.text.split("\n")) { + if (para.isBlank()) { ensure(lineH); y += lineH; continue } + var line = StringBuilder() + for (word in para.split(" ")) { + val trial = if (line.isEmpty()) word else "$line $word" + if (body.measureText(trial) <= maxW) { line = StringBuilder(trial); continue } + if (line.isNotEmpty()) { + ensure(lineH); page!!.canvas.drawText(line.toString(), margin, y + 12f, body); y += lineH + } + line = StringBuilder(word) + } + if (line.isNotEmpty()) { ensure(lineH); page!!.canvas.drawText(line.toString(), margin, y + 12f, body); y += lineH } + } + } + page?.let { doc.finishPage(it) } + out.outputStream().use { doc.writeTo(it) } + doc.close() + } + + // ---------- Word (.docx) ---------- + private fun esc(s: String) = s.replace("&", "&").replace("<", "<").replace(">", ">") + + private fun writeDocx(out: File, notes: List) { + val sb = StringBuilder() + sb.append("") + sb.append("") + for ((idx, n) in notes.withIndex()) { + if (idx > 0) sb.append("") + sb.append("") + .append(esc(heading(n))).append("") + for (para in n.text.split("\n")) { + sb.append("").append(esc(para)).append("") + } + } + sb.append("") + sb.append("") + ZipOutputStream(out.outputStream()).use { z -> + fun put(name: String, text: String) { z.putNextEntry(ZipEntry(name)); z.write(text.toByteArray(Charsets.UTF_8)); z.closeEntry() } + put("[Content_Types].xml", + "" + + "" + + "" + + "") + put("_rels/.rels", + "" + + "") + put("word/document.xml", sb.toString()) + } + } +}