New Lib: Text Interceptor (#13859)

* move TextInterceptor to `lib`

to be used for author notes, image alt texts and what not

* refactor Tapastic to use `lib:extensions-lib`

* Refactor Webtoons to use `:lib-textinterceptor`

this turned out to be more complicated than I thought it'd be
TextInterceptor was used for Author Notes which, looking at previous messages,
was only intended to be added to Webtoons Source and not the entire webtoons
multisrc (i.e. WebtoonsTranslate and DongmanManhua don't seem to be making use
of the Show Author's Notes setting). This was in my favor, since having
to deal with additional.gradle to add dependencies to multisrc files doesn't
seem to work... I'll ask previous contributors just in case

* Fix `json` access

missed this while copying over code from `generated-src` to `multisrc/{overrides,src}`

* remove unused import

* make HOST name more clear

couldve used a better schema but that's something for some other time
also put the HOST in the lib itself so that one doesn't lose track of it in the extensions

* use android provided methods instead of hardcoding

based on https://github.com/tachiyomiorg/tachiyomi-extensions/pull/13859/files#r996276738
that suggested the following SO answer: https://github.com/tachiyomiorg/tachiyomi-extensions/pull/13859/files

* remove unused import

* move url generation to helper function

* fix error

oops sorry for that
happened when I was copy pasting back and forth between two sources and one generated source

Co-authored-by: Navaneeth M Nambiar <nmnambiar@hornbill>
This commit is contained in:
nicki
2023-01-11 11:34:06 -06:00
committed by GitHub
parent 2f4658b3b3
commit 951ca60b56
11 changed files with 263 additions and 278 deletions

View File

@@ -0,0 +1,22 @@
plugins {
id("com.android.library")
kotlin("android")
}
android {
compileSdk = AndroidConfig.compileSdk
defaultConfig {
minSdk = AndroidConfig.minSdk
targetSdk = AndroidConfig.targetSdk
}
}
repositories {
mavenCentral()
}
dependencies {
compileOnly(libs.kotlin.stdlib)
compileOnly(libs.okhttp)
}

View File

@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.kanade.tachiyomi.lib.textinterceptor" />

View File

@@ -0,0 +1,121 @@
package eu.kanade.tachiyomi.lib.textinterceptor
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Typeface
import android.net.Uri
import android.os.Build
import android.text.Html
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import java.io.ByteArrayOutputStream
class TextInterceptor : Interceptor {
// With help from:
// https://github.com/tachiyomiorg/tachiyomi-extensions/pull/13304#issuecomment-1234532897
// https://medium.com/over-engineering/drawing-multiline-text-to-canvas-on-android-9b98f0bfa16a
companion object {
// Designer values:
private const val WIDTH: Int = 1000
private const val X_PADDING: Float = 50f
private const val Y_PADDING: Float = 25f
private const val HEADING_FONT_SIZE: Float = 36f
private const val BODY_FONT_SIZE: Float = 30f
private const val SPACING_MULT: Float = 1.1f
private const val SPACING_ADD: Float = 2f
// No need to touch this one:
private const val HOST = TextInterceptorHelper.HOST
}
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val url = request.url
if (url.host != HOST) return chain.proceed(request)
val creator = textFixer("Author's Notes from ${url.pathSegments[0]}")
val story = textFixer(url.pathSegments[1])
// Heading
val paintHeading = TextPaint().apply {
color = Color.BLACK
textSize = HEADING_FONT_SIZE
typeface = Typeface.DEFAULT_BOLD
isAntiAlias = true
}
@Suppress("DEPRECATION")
val heading: StaticLayout = StaticLayout(
creator, paintHeading, (WIDTH - 2 * X_PADDING).toInt(),
Layout.Alignment.ALIGN_NORMAL, SPACING_MULT, SPACING_ADD, true
)
// Body
val paintBody = TextPaint().apply {
color = Color.BLACK
textSize = BODY_FONT_SIZE
typeface = Typeface.DEFAULT
isAntiAlias = true
}
@Suppress("DEPRECATION")
val body: StaticLayout = StaticLayout(
story, paintBody, (WIDTH - 2 * X_PADDING).toInt(),
Layout.Alignment.ALIGN_NORMAL, SPACING_MULT, SPACING_ADD, true
)
// Image building
val imgHeight: Int = (heading.height + body.height + 2 * Y_PADDING).toInt()
val bitmap: Bitmap = Bitmap.createBitmap(WIDTH, imgHeight, Bitmap.Config.ARGB_8888)
val canvas: Canvas = Canvas(bitmap)
// Image drawing
canvas.drawColor(Color.WHITE)
heading.draw(canvas, X_PADDING, Y_PADDING)
body.draw(canvas, X_PADDING, Y_PADDING + heading.height.toFloat())
// Image converting & returning
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream)
val responseBody = stream.toByteArray().toResponseBody("image/png".toMediaType())
return Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(responseBody)
.build()
}
private fun textFixer(htmlString: String): String {
return if (Build.VERSION.SDK_INT >= 24) {
Html.fromHtml(htmlString , Html.FROM_HTML_MODE_LEGACY).toString()
} else {
Html.fromHtml(htmlString).toString()
}
}
private fun StaticLayout.draw(canvas: Canvas, x: Float, y: Float) {
canvas.save()
canvas.translate(x, y)
this.draw(canvas)
canvas.restore()
}
}
object TextInterceptorHelper {
const val HOST = "tachiyomi-lib-textinterceptor"
fun createUrl(creator: String, text: String): String {
return "http://$HOST/" + Uri.encode(creator) + "/" + Uri.encode(text)
}
}