1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file    except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.pdf.adapter
18 
19 import android.graphics.Bitmap
20 import android.graphics.pdf.PdfRendererPreV
21 import android.graphics.pdf.RenderParams
22 import android.graphics.pdf.content.PdfPageGotoLinkContent
23 import android.graphics.pdf.content.PdfPageImageContent
24 import android.graphics.pdf.content.PdfPageLinkContent
25 import android.graphics.pdf.content.PdfPageTextContent
26 import android.graphics.pdf.models.PageMatchBounds
27 import android.graphics.pdf.models.selection.PageSelection
28 import android.graphics.pdf.models.selection.SelectionBoundary
29 import android.os.Build
30 import androidx.annotation.RequiresExtension
31 import androidx.annotation.RestrictTo
32 import androidx.pdf.utils.getTransformationMatrix
33 
34 /**
35  * A [PdfPage] implementation that uses the [PdfRendererPreV.Page] class for rendering.
36  *
37  * This adapter provides a consistent interface for interacting with [PdfRendererPreV.Page],
38  * allowing it to be used interchangeably with other PDF page rendering implementations.
39  *
40  * @param page The [PdfRendererPreV.Page] instance to render.
41  * @constructor Creates a new [PdfPagePreVAdapter] instance.
42  */
43 @RestrictTo(RestrictTo.Scope.LIBRARY)
44 @RequiresExtension(extension = Build.VERSION_CODES.S, version = 13)
45 internal class PdfPagePreVAdapter(private val page: PdfRendererPreV.Page) : PdfPage {
46     override val height = page.height
47     override val width = page.width
48 
renderPagenull49     override fun renderPage(bitmap: Bitmap) {
50         page.render(bitmap, null, null, getRenderParams())
51     }
52 
renderTilenull53     override fun renderTile(
54         bitmap: Bitmap,
55         left: Int,
56         top: Int,
57         scaledPageWidth: Int,
58         scaledPageHeight: Int
59     ) {
60         val transformationMatrix =
61             getTransformationMatrix(
62                 left,
63                 top,
64                 scaledPageWidth.toFloat(),
65                 scaledPageHeight.toFloat(),
66                 width,
67                 height
68             )
69         page.render(bitmap, null, transformationMatrix, getRenderParams())
70     }
71 
getPageTextContentsnull72     override fun getPageTextContents(): List<PdfPageTextContent> {
73         return page.textContents
74     }
75 
getPageImageContentsnull76     override fun getPageImageContents(): List<PdfPageImageContent> {
77         return page.imageContents
78     }
79 
selectPageTextnull80     override fun selectPageText(start: SelectionBoundary, stop: SelectionBoundary): PageSelection? {
81         return page.selectContent(start, stop)
82     }
83 
searchPageTextnull84     override fun searchPageText(query: String): List<PageMatchBounds> {
85         return page.searchText(query)
86     }
87 
getPageLinksnull88     override fun getPageLinks(): List<PdfPageLinkContent> {
89         return page.linkContents
90     }
91 
getPageGotoLinksnull92     override fun getPageGotoLinks(): List<PdfPageGotoLinkContent> {
93         return page.gotoLinks
94     }
95 
getRenderParamsnull96     override fun getRenderParams(): RenderParams {
97         return RenderParams.Builder(RenderParams.RENDER_MODE_FOR_DISPLAY)
98             .setRenderFlags(
99                 RenderParams.FLAG_RENDER_HIGHLIGHT_ANNOTATIONS or
100                     RenderParams.FLAG_RENDER_TEXT_ANNOTATIONS
101             )
102             .build()
103     }
104 
closenull105     override fun close() {
106         page.close()
107     }
108 }
109