Skip to content

Commit

Permalink
moving page border size to parameter, also making it optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mspnr committed Aug 15, 2020
1 parent 4a0c587 commit 3f57f4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions pdfview-library/src/main/java/com/pdfview/PDFRegionDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.io.File
internal class PDFRegionDecoder(private val view: PDFView,
private val file: File,
private val scale: Float,
private val borderSize: Float = 0.0f, // size in mm, zero to skip drawing
@param:ColorInt private val backgroundColorPdf: Int = Color.WHITE) : ImageRegionDecoder {

private lateinit var descriptor: ParcelFileDescriptor
Expand Down Expand Up @@ -41,12 +42,6 @@ internal class PDFRegionDecoder(private val view: PDFView,
val numPageAtEnd = Math.ceil(rect.bottom.toDouble() / pageHeight).toInt() - 1
val bitmap = Bitmap.createBitmap(rect.width() / sampleSize, rect.height() / sampleSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val pageBorderPaint = Paint().apply() {
color = Color.BLACK
style = Paint.Style.STROKE
isAntiAlias = true // for lines thinner than 1
strokeWidth = 0.5f * 160f / 25.4f / sampleSize // = 0.5 mm
}
canvas.drawColor(backgroundColorPdf)
canvas.drawBitmap(bitmap, 0f, 0f, null)
for ((iteration, pageIndex) in (numPageAtStart..numPageAtEnd).withIndex()) {
Expand All @@ -58,9 +53,16 @@ internal class PDFRegionDecoder(private val view: PDFView,
(-rect.left / sampleSize).toFloat(), -((rect.top - pageHeight * numPageAtStart) / sampleSize).toFloat() + (pageHeight.toFloat() / sampleSize) * iteration)
page.render(bitmap,null, matrix, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
page.close()
val pageBorderRect = RectF(0f, 0f, page.width.toFloat(), page.height.toFloat())
matrix.mapRect(pageBorderRect)
canvas.drawRect(pageBorderRect, pageBorderPaint)
if (borderSize > 0) {
val pageBorderRect = RectF(0f, 0f, page.width.toFloat(), page.height.toFloat())
matrix.mapRect(pageBorderRect)
canvas.drawRect(pageBorderRect, Paint().apply() {
color = Color.BLACK
style = Paint.Style.STROKE
isAntiAlias = true // for lines thinner than 1
strokeWidth = borderSize * 160f / 25.4f / sampleSize // = 0.5 mm
})
}
}
}
return bitmap
Expand Down
2 changes: 1 addition & 1 deletion pdfview-library/src/main/java/com/pdfview/PDFView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PDFView @JvmOverloads constructor(context: Context, attrs: AttributeSet? =

fun show() {
val source = ImageSource.uri(mfile!!.path)
setRegionDecoderFactory { PDFRegionDecoder(view = this, file = mfile!!, scale = mScale) }
setRegionDecoderFactory { PDFRegionDecoder(view = this, file = mfile!!, scale = mScale, borderSize = 0.5f) }
setImage(source)
}

Expand Down

0 comments on commit 3f57f4a

Please sign in to comment.