Skip to content

Commit

Permalink
Spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
gtcno committed Oct 30, 2023
1 parent d531873 commit 80ef8f2
Show file tree
Hide file tree
Showing 43 changed files with 1,019 additions and 780 deletions.
54 changes: 38 additions & 16 deletions image-utils/src/main/kotlin/no/nav/dagpenger/image/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import kotlin.math.max
import kotlin.math.min

object ImageConverter {

internal fun requireImage(input: ByteArray) {
require(input.isImage()) { "Only jpg and png is supported" }
}
Expand All @@ -47,7 +46,10 @@ object ImageConverter {
}
}

fun toPDF(input: InputStream, dimension: Dimension): ByteArray {
fun toPDF(
input: InputStream,
dimension: Dimension,
): ByteArray {
requireImage(input)
return ImageScaler.scale(input, dimension, SCALE_TO_FIT_INSIDE_BOX).toPNG().let { toPDF(it) }
}
Expand All @@ -60,7 +62,10 @@ object ImageConverter {
}
}

fun toPNG(input: ByteArray, dimension: Dimension): ByteArray {
fun toPNG(
input: ByteArray,
dimension: Dimension,
): ByteArray {
require(input.isImage() || input.isPdf()) {
"Kan kun konvertere PDF, JPG og PNG til PNG."
}
Expand All @@ -87,40 +92,57 @@ object ImageScaler {
CROP_TO_FILL_ENTIRE_BOX,
}

fun scale(input: ByteArray, dimension: Dimension, scaleMode: ScaleMode): BufferedImage {
fun scale(
input: ByteArray,
dimension: Dimension,
scaleMode: ScaleMode,
): BufferedImage {
requireImage(input)
return ByteArrayInputStream(input).use { stream ->
scale(ImageIO.read(stream), dimension, scaleMode)
}
}

fun scale(input: InputStream, dimension: Dimension, scaleMode: ScaleMode): BufferedImage {
fun scale(
input: InputStream,
dimension: Dimension,
scaleMode: ScaleMode,
): BufferedImage {
requireImage(input)
return scale(ImageIO.read(input), dimension, scaleMode)
}

fun scale(input: BufferedImage, dimension: Dimension, scaleMode: ScaleMode): BufferedImage {
fun scale(
input: BufferedImage,
dimension: Dimension,
scaleMode: ScaleMode,
): BufferedImage {
val scaleFactorWidth: Double = dimension.getWidth() / input.width
val scaleFactorHeight: Double = dimension.getHeight() / input.height

val scalingFactor = when (scaleMode) {
SCALE_TO_FIT_INSIDE_BOX -> min(scaleFactorWidth, scaleFactorHeight)
ScaleMode.CROP_TO_FILL_ENTIRE_BOX -> max(scaleFactorWidth, scaleFactorHeight)
}
val scalingFactor =
when (scaleMode) {
SCALE_TO_FIT_INSIDE_BOX -> min(scaleFactorWidth, scaleFactorHeight)
ScaleMode.CROP_TO_FILL_ENTIRE_BOX -> max(scaleFactorWidth, scaleFactorHeight)
}

val scaledImage = Scalr.resize(
input,
(scalingFactor * input.width).toInt(),
(scalingFactor * input.height).toInt(),
)
val scaledImage =
Scalr.resize(
input,
(scalingFactor * input.width).toInt(),
(scalingFactor * input.height).toInt(),
)

return when (scaleMode) {
SCALE_TO_FIT_INSIDE_BOX -> scaledImage
ScaleMode.CROP_TO_FILL_ENTIRE_BOX -> crop(scaledImage, dimension)
}
}

fun crop(image: BufferedImage, dimension: Dimension): BufferedImage {
fun crop(
image: BufferedImage,
dimension: Dimension,
): BufferedImage {
require(image.width >= dimension.width && image.height >= dimension.height) {
"Image must be at least as big as dimension"
}
Expand Down
6 changes: 6 additions & 0 deletions image-utils/src/main/kotlin/no/nav/dagpenger/io/Detect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ object Detect {
fun ByteArray.detect(): String = tika.detect(this)

fun InputStream.isPng(): Boolean = this.detect() == IMAGE_PNG

fun InputStream.isJpeg(): Boolean = this.detect() == IMAGE_JPEG

fun InputStream.isPdf(): Boolean {
return this.detect() == APPLICATON_PDF
}

fun ByteArray.isPng(): Boolean = this.detect() == IMAGE_PNG

fun ByteArray.isJpeg(): Boolean = this.detect() == IMAGE_JPEG

fun ByteArray.isPdf(): Boolean = this.detect() == APPLICATON_PDF

fun InputStream.isImage(): Boolean = this.isJpeg() || this.isPng()

fun ByteArray.isImage(): Boolean = this.isJpeg() || this.isPng()

fun List<ByteArray>.isPdf(): Boolean {
return this.isNotEmpty() && this.all { it.isPdf() }
}
Expand Down
5 changes: 4 additions & 1 deletion image-utils/src/main/kotlin/no/nav/dagpenger/pdf/Pdf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ sealed class PDFDocument constructor(val document: PDDocument) : Closeable {
}

@JvmOverloads
fun waterMark(ident: String, includeDate: LocalDateTime? = null) {
fun waterMark(
ident: String,
includeDate: LocalDateTime? = null,
) {
require(numberOfPages() > 0) {
"Document must have a least one page."
}
Expand Down
50 changes: 39 additions & 11 deletions image-utils/src/main/kotlin/no/nav/dagpenger/pdf/PdfWatermarker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ object PdfWatermarker {

private data class Placement(val x: Float, val y: Float, val width: Float, val height: Float)

private fun calculateWaterMarkPlacement(page: PDPage, linje1: String, linje2: String): Placement {
private fun calculateWaterMarkPlacement(
page: PDPage,
linje1: String,
linje2: String,
): Placement {
val dimensions: PDRectangle = page.mediaBox ?: page.trimBox
val pageHeight: Float = dimensions.getHeight()
val pageWidth: Float = dimensions.getWidth()
val lineWidth = Math.max(
findLineWidthForTextWithFontSize(linje1, FONT, FONT_SIZE),
findLineWidthForTextWithFontSize(linje2, FONT, FONT_SIZE),
) + PADDING_X + PADDING_Y
val lineWidth =
Math.max(
findLineWidthForTextWithFontSize(linje1, FONT, FONT_SIZE),
findLineWidthForTextWithFontSize(linje2, FONT, FONT_SIZE),
) + PADDING_X + PADDING_Y
val upperRightX = pageWidth - MARGIN
val upperRightY = pageHeight - MARGIN
val lowerLeftX = upperRightX - lineWidth - 2 * PADDING_X
Expand All @@ -66,7 +71,11 @@ object PdfWatermarker {
return Placement(lowerLeftX, lowerLeftY, lineWidth, HEIGHT)
}

private fun stampRectangleOnPdf(pdfDocument: PDDocument, linje1: String, linje2: String) {
private fun stampRectangleOnPdf(
pdfDocument: PDDocument,
linje1: String,
linje2: String,
) {
for (page: PDPage in pdfDocument.documentCatalog.pages) {
val (lowerLeftX, lowerLeftY, lineWidth, height) = calculateWaterMarkPlacement(page, linje1, linje2)
setOpacity(page.resources, BACKGROUND_OPACITY)
Expand All @@ -88,7 +97,11 @@ object PdfWatermarker {
}
}

private fun stampTextOnPdf(pdfDocument: PDDocument, linje1: String, linje2: String) {
private fun stampTextOnPdf(
pdfDocument: PDDocument,
linje1: String,
linje2: String,
) {
for (page in pdfDocument.documentCatalog.pages) {
val (lowerLeftX, lowerLeftY, lineWidth, height) = calculateWaterMarkPlacement(page, linje1, linje2)
setOpacity(page.getResources(), DEFAULT_BACKGROUND_OPACITY)
Expand Down Expand Up @@ -127,7 +140,10 @@ object PdfWatermarker {
}
}

private fun setOpacity(resources: PDResources, opacity: Float) {
private fun setOpacity(
resources: PDResources,
opacity: Float,
) {
val graphicsState = PDExtendedGraphicsState()
graphicsState.setNonStrokingAlphaConstant(opacity)
resources.add(graphicsState)
Expand All @@ -138,19 +154,31 @@ object PdfWatermarker {
return date.format(dateTimeFormatter)
}

private fun findLineWidthForTextWithFontSize(text: String, font: PDFont, fontSize: Int): Float {
private fun findLineWidthForTextWithFontSize(
text: String,
font: PDFont,
fontSize: Int,
): Float {
return font.getStringWidth(text) / 1000 * fontSize
}

fun applyOn(bytes: ByteArray, ident: String, includeDate: LocalDateTime?): ByteArray {
fun applyOn(
bytes: ByteArray,
ident: String,
includeDate: LocalDateTime?,
): ByteArray {
require(bytes.isPdf()) { "Kan kun vannmerke PDF-filer." }

return PDDocument.load(ByteArrayInputStream(bytes)).use { pdfDocument ->
applyOn(pdfDocument, ident, includeDate)
}
}

fun applyOn(pdfDocument: PDDocument, ident: String, includeDate: LocalDateTime?): ByteArray {
fun applyOn(
pdfDocument: PDDocument,
ident: String,
includeDate: LocalDateTime?,
): ByteArray {
val linje1 = if (includeDate == null) LINE_1_HEADER else LINE_1_HEADER + ": ${formatertDato(includeDate)}"
val linje2 = LINE_2_HEADER + ident

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import java.io.BufferedOutputStream
import java.io.FileOutputStream

internal class ImageConverterTest {

@Test
fun `konverter ikke pdf`() {
"/pdfs/minimal.pdf".fileAsByteArray().let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import javax.imageio.stream.FileImageOutputStream
import kotlin.math.abs

internal class ImageScalerTest {

@Test
fun `Detect images`() {
shouldThrow<IllegalArgumentException> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import no.nav.dagpenger.pdf.fileAsByteArray
import org.junit.jupiter.api.Test

class DetectTest {

@Test
fun `detect pdf`() {
"/pdfs/minimal.pdf".fileAsBufferedInputStream().use {
Expand Down
10 changes: 5 additions & 5 deletions image-utils/src/test/kotlin/no/nav/dagpenger/pdf/PdfTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import java.io.OutputStream
import java.time.LocalDateTime

class PdfTest {

@Test
fun `is not pdf`() {
PDFDocument.load("sfasf".toByteArray()).use { invalidPdf ->
Expand Down Expand Up @@ -68,10 +67,11 @@ class PdfTest {

@Test
fun `merge documents`() {
val pages: List<ByteArray> = listOf(
"/pdfs/minimal.pdf".fileAsByteArray(),
"/pdfs/minimal.pdf".fileAsByteArray(),
)
val pages: List<ByteArray> =
listOf(
"/pdfs/minimal.pdf".fileAsByteArray(),
"/pdfs/minimal.pdf".fileAsByteArray(),
)

PDFDocument.merge(pages).also {
it.shouldBeTypeOf<ValidPDFDocument>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal fun ByteArray.extractText(): String {
PDFTextStripper().getText(it)
}
}

internal fun ByteArray.writeToFile(filename: String) {
BufferedOutputStream(FileOutputStream(filename)).use {
it.write(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import io.ktor.http.auth.HttpAuthHeader

fun Auth.bearer(block: BearerAuthConfig.() -> Unit) {
with(BearerAuthConfig().apply(block)) {
providers += BearerAuthProvider(
tokenProvider,
realm,
sendWithoutRequest,
)
providers +=
BearerAuthProvider(
tokenProvider,
realm,
sendWithoutRequest,
)
}
}

Expand Down
Loading

0 comments on commit 80ef8f2

Please sign in to comment.