Skip to content

Commit

Permalink
Replace Apache POI by two smaller Excel libraries
Browse files Browse the repository at this point in the history
This essentially reverts 4e22e1a to reduce the installation size, but
improves upon that old implementation.
  • Loading branch information
LoadingByte committed Jul 21, 2024
1 parent 63fd527 commit 8eb1e1a
Show file tree
Hide file tree
Showing 8 changed files with 645 additions and 147 deletions.
20 changes: 7 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ version = "1.6.0-SNAPSHOT"

val jdkVersion = 21
val slf4jVersion = "2.0.13"
val poiVersion = "5.3.0"
val twelveMonkeysVersion = "3.11.0"
val javacppVersion = "1.5.10"
val ffmpegVersion = "6.1.1-$javacppVersion"
Expand Down Expand Up @@ -66,13 +65,13 @@ dependencies {
// Log to java.util.logging
implementation("org.slf4j", "slf4j-jdk14", slf4jVersion)
// Redirect other logging frameworks to slf4j.
// PDFBox uses Jakarta Commons Logging. POI uses log4j2.
// JExcelAPI uses log4j. PDFBox uses Jakarta Commons Logging.
implementation("org.slf4j", "log4j-over-slf4j", slf4jVersion)
implementation("org.slf4j", "jcl-over-slf4j", slf4jVersion)
implementation("org.apache.logging.log4j", "log4j-to-slf4j", "2.20.0")

// Spreadsheet IO
implementation("org.apache.poi", "poi", poiVersion)
implementation("org.apache.poi", "poi-ooxml", poiVersion)
implementation("ch.rabanti", "nanoxlsx4j", "2.4.0")
implementation("net.sourceforge.jexcelapi", "jxl", "2.6.12")
implementation("com.github.miachm.sods", "SODS", "1.6.7")
implementation("de.siegmar", "fastcsv", "3.2.0")

Expand Down Expand Up @@ -112,20 +111,15 @@ dependencies {
}

configurations.configureEach {
// POI:
// We don't re-evaluate formulas, and as only that code calls Commons Math, we can omit the dependency.
exclude("org.apache.commons", "commons-math3")
// This is only required for adding pictures to workbooks via code, which we don't do.
exclude("commons-codec", "commons-codec")
// JExcelAPI/PDFBox: We replace their log4j and commons-logging dependencies with slf4j bridges.
exclude("log4j", "log4j")
exclude("commons-logging", "commons-logging")

// Google Client: This dependency is totally empty and only serves to avoid some conflict not relevant to us.
exclude("com.google.guava", "listenablefuture")

// JAI ImageIO JPEG2000: Core reimplements already supported formats. We copied the few actually required sources.
exclude("com.github.jai-imageio", "jai-imageio-core")

// PDFBox: We replace this commons-logging dependency by the slf4j bridge.
exclude("commons-logging", "commons-logging")
}


Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/loadingbyte/cinecred/common/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ fun Path.walkSafely(): List<Path> {
}


/** @throws IOException */
fun Path.cleanDirectory() {
Files.walkFileTree(this, object : SimpleFileVisitor<Path>() {
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
file.deleteIfExists()
return FileVisitResult.CONTINUE
}

override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
if (exc != null)
throw exc
if (dir != this@cleanDirectory)
dir.deleteIfExists()
return FileVisitResult.CONTINUE
}
})
}


fun readToml(file: Path): MutableMap<String, Any> =
Toml.read(file.toFile())

Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/com/loadingbyte/cinecred/common/ReflectionExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package com.loadingbyte.cinecred.common

import de.siegmar.fastcsv.reader.CommentStrategy
import de.siegmar.fastcsv.reader.CsvReader
import de.siegmar.fastcsv.reader.StringArrayHandler
import org.apache.pdfbox.contentstream.operator.OperatorName
import org.apache.pdfbox.cos.COSName
import org.apache.pdfbox.pdfwriter.COSWriter
Expand All @@ -10,7 +13,6 @@ import org.apache.pdfbox.pdmodel.common.function.PDFunction
import org.apache.pdfbox.pdmodel.graphics.color.*
import org.apache.pdfbox.pdmodel.graphics.shading.PDShading
import org.apache.pdfbox.util.Matrix
import org.apache.poi.util.LocaleID
import sun.font.*
import java.awt.Font
import java.awt.Point
Expand Down Expand Up @@ -113,8 +115,10 @@ private const val MAC_ROMAN_ENCODING = TrueTypeFont.MACROMAN_SPECIFIC_ID.toShort
private const val MAC_ENGLISH_LANG = TrueTypeFont.MACROMAN_ENGLISH_LANG.toShort()

private val LCID_TO_LOCALE: Map<Short, Locale> = HashMap<Short, Locale>().apply {
for (id in LocaleID.entries)
put(id.lcid.toShort(), Locale.forLanguageTag(id.languageTag))
useResourceStream("/lcid.csv") { s ->
CsvReader.builder().commentStrategy(CommentStrategy.SKIP).build(StringArrayHandler(), s.bufferedReader())
.forEach { (lcid, tag) -> put(Integer.decode(lcid).toShort(), Locale.forLanguageTag(tag)) }
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.loadingbyte.cinecred.delivery

import com.loadingbyte.cinecred.common.cleanDirectory
import com.loadingbyte.cinecred.common.createDirectoriesSafely
import com.loadingbyte.cinecred.common.throwableAwareTask
import com.loadingbyte.cinecred.delivery.RenderFormat.Channels.*
Expand All @@ -26,7 +27,6 @@ import com.loadingbyte.cinecred.imaging.ColorSpace.Transfer.Companion.LINEAR
import com.loadingbyte.cinecred.imaging.DeferredImage.Companion.STATIC
import com.loadingbyte.cinecred.imaging.DeferredImage.Companion.TAPES
import com.loadingbyte.cinecred.project.Project
import org.apache.commons.io.FileUtils
import org.bytedeco.ffmpeg.global.avutil.*
import java.nio.file.Path
import java.util.concurrent.CountDownLatch
Expand All @@ -51,7 +51,7 @@ class ImageSequenceRenderJob private constructor(

override fun render(progressCallback: (Int) -> Unit) {
if (dir.exists())
FileUtils.cleanDirectory(dir.toFile())
dir.cleanDirectory()
dir.createDirectoriesSafely()

val embedAlpha = config[CHANNELS] == COLOR_AND_ALPHA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.loadingbyte.cinecred.imaging.DeferredImage.Companion.STATIC
import com.loadingbyte.cinecred.imaging.DeferredImage.Companion.TAPES
import com.loadingbyte.cinecred.imaging.Y.Companion.toY
import com.loadingbyte.cinecred.project.Project
import org.apache.commons.io.FileUtils
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.PDPageContentStream
Expand Down Expand Up @@ -65,7 +64,7 @@ class WholePageSequenceRenderJob private constructor(

override fun render(progressCallback: (Int) -> Unit) {
if (dir.exists())
FileUtils.cleanDirectory(dir.toFile())
dir.cleanDirectory()
dir.createDirectoriesSafely()

val ground = config[CHANNELS] == COLOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode
import org.apache.pdfbox.rendering.*
import org.apache.pdfbox.util.Matrix
import org.apache.pdfbox.util.Vector
import org.apache.poi.util.Dimension2DDouble
import org.bytedeco.ffmpeg.global.avutil.AV_PIX_FMT_GRAY16LE
import org.bytedeco.ffmpeg.global.avutil.AV_PIX_FMT_RGBAF32
import java.awt.BasicStroke
Expand Down Expand Up @@ -1054,4 +1053,14 @@ class PDFDrawer(

}


private class Dimension2DDouble(private var width: Double, private var height: Double) : Dimension2D() {
override fun getWidth() = width
override fun getHeight() = height
override fun setSize(width: Double, height: Double) {
this.width = width
this.height = height
}
}

}
Loading

0 comments on commit 8eb1e1a

Please sign in to comment.