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.service
18 
19 import android.graphics.Bitmap
20 import android.graphics.Matrix
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 
33 @RestrictTo(RestrictTo.Scope.LIBRARY)
34 internal interface PdfPageWrapper : AutoCloseable {
35 
36     val height: Int
37     val width: Int
38 
renderPagenull39     fun renderPage(bitmap: Bitmap)
40 
41     fun renderTile(bitmap: Bitmap, left: Int, top: Int, scaledPageWidth: Int, scaledPageHeight: Int)
42 
43     fun getPageTextContents(): List<PdfPageTextContent>
44 
45     fun getPageImageContents(): List<PdfPageImageContent>
46 
47     fun selectPageText(start: SelectionBoundary, stop: SelectionBoundary): PageSelection?
48 
49     fun searchPageText(query: String): List<PageMatchBounds>
50 
51     fun getPageLinks(): List<PdfPageLinkContent>
52 
53     fun getPageGotoLinks(): List<PdfPageGotoLinkContent>
54 
55     fun getTransformationMatrix(
56         left: Int,
57         top: Int,
58         scaledPageWidth: Float,
59         scaledPageHeight: Float,
60         pageWidth: Int,
61         pageHeight: Int
62     ): Matrix {
63         return Matrix().apply {
64             setScale(scaledPageWidth / pageWidth, scaledPageHeight / pageHeight)
65             postTranslate(-left.toFloat(), -top.toFloat())
66         }
67     }
68 
69     @RequiresExtension(extension = Build.VERSION_CODES.S, version = 13)
getRenderParamsnull70     fun getRenderParams(): RenderParams {
71         return RenderParams.Builder(RenderParams.RENDER_MODE_FOR_DISPLAY)
72             .setRenderFlags(
73                 RenderParams.FLAG_RENDER_HIGHLIGHT_ANNOTATIONS or
74                     RenderParams.FLAG_RENDER_TEXT_ANNOTATIONS
75             )
76             .build()
77     }
78 }
79