1 /*
2  * Copyright (C) 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 android.graphics.pdf;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.graphics.pdf.flags.Flags;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Represents a set of parameters that will be used to render a page of the PDF document.
29  */
30 @FlaggedApi(Flags.FLAG_ENABLE_PDF_VIEWER)
31 public final class RenderParams {
32     /**
33      * Mode to render the content for display on a screen.
34      */
35     public static final int RENDER_MODE_FOR_DISPLAY = 1;
36 
37     /**
38      * Mode to render the content for printing.
39      */
40     public static final int RENDER_MODE_FOR_PRINT = 2;
41 
42     // LINT.IfChange
43     /**
44      * Flag to enable rendering of text annotation on the page.
45      *
46      * @see RenderParams#getRenderFlags()
47      * @see RenderParams.Builder#setRenderFlags(int)
48      */
49     public static final int FLAG_RENDER_TEXT_ANNOTATIONS = 1 << 1;
50 
51     /**
52      * Flag to enable rendering of highlight annotation on the page.
53      *
54      * @see RenderParams#getRenderFlags()
55      * @see RenderParams.Builder#setRenderFlags(int)
56      */
57     public static final int FLAG_RENDER_HIGHLIGHT_ANNOTATIONS = 1 << 2;
58     // LINT.ThenChange(packages/providers/MediaProvider/pdf/framework/libs/pdfClient/page.h)
59 
60     private static final int RENDER_FLAGS_MASK =
61             FLAG_RENDER_TEXT_ANNOTATIONS | FLAG_RENDER_HIGHLIGHT_ANNOTATIONS;
62     private final int mRenderMode;
63 
64     private final int mRenderFlags;
65 
RenderParams(int renderMode, int renderFlags)66     private RenderParams(int renderMode, int renderFlags) {
67         this.mRenderMode = renderMode;
68         this.mRenderFlags = renderFlags;
69     }
70 
71     /**
72      * Returns the render mode.
73      */
74     @RenderMode
getRenderMode()75     public int getRenderMode() {
76         return mRenderMode;
77     }
78 
79     /**
80      * Returns the bitmask of the render flags.
81      */
82     @RenderFlags
getRenderFlags()83     public int getRenderFlags() {
84         return mRenderFlags;
85     }
86 
87     /** @hide */
getRenderAnnotations()88     public int getRenderAnnotations() {
89         return mRenderFlags & RENDER_FLAGS_MASK;
90     }
91 
92     /** @hide */
93     @IntDef({
94             RENDER_MODE_FOR_DISPLAY,
95             RENDER_MODE_FOR_PRINT
96     })
97     @Retention(RetentionPolicy.SOURCE)
98     public @interface RenderMode {
99     }
100 
101     /** @hide */
102     @IntDef(flag = true, prefix = {"FLAG_"}, value = {
103             FLAG_RENDER_TEXT_ANNOTATIONS,
104             FLAG_RENDER_HIGHLIGHT_ANNOTATIONS
105     })
106     @Retention(RetentionPolicy.SOURCE)
107     public @interface RenderFlags {
108     }
109 
110     /**
111      * Builder for constructing {@link RenderParams}.
112      */
113     public static final class Builder {
114 
115         private final int mRenderMode;
116 
117         @RenderFlags
118         private int mRenderFlags;
119 
120         /**
121          * Create a builder for constructing a {@link RenderParams} object with the render mode.
122          *
123          * @param renderMode render mode for the content.
124          */
Builder(@enderMode int renderMode)125         public Builder(@RenderMode int renderMode) {
126             this.mRenderMode = renderMode;
127         }
128 
129         /**
130          * Sets the state of the render flag.
131          * See {@link #setRenderFlags(int, int)} for usage information.
132          *
133          * @param renderFlags the bitmask of the render flag should be enabled, or {@code 0} to
134          *                    disable all flags.
135          * @see #setRenderFlags(int, int)
136          * @see #getRenderFlags()
137          */
138         @NonNull
setRenderFlags(@enderFlags int renderFlags)139         public Builder setRenderFlags(@RenderFlags int renderFlags) {
140             setRenderFlags(renderFlags, RENDER_FLAGS_MASK);
141             return this;
142         }
143 
144         /**
145          * Sets the state of the render flag specified by the mask. To change all render flags at
146          * once, see {@link #setRenderFlags(int)}.
147          * <p>
148          * When a render flag is enabled, it will be displayed on the updated
149          * {@link android.graphics.Bitmap} of the renderer.
150          * <p>
151          * Multiple indicator types may be enabled or disabled by passing the logical OR of the
152          * desired flags. If multiple flags are specified, they
153          * will all be set to the same enabled state.
154          * <p>
155          * For example, to enable the render text annotations flag:
156          * {@code setRenderFlags(FLAG_RENDER_TEXT_ANNOTATIONS, FLAG_RENDER_TEXT_ANNOTATIONS)}
157          * <p>
158          * To disable the render text annotations flag:
159          * {@code setRenderFlags(0, FLAG_RENDER_TEXT_ANNOTATIONS)}
160          *
161          * @param renderFlags the render flag, or the logical OR of multiple
162          *                    render flags. One or more of:
163          *                    <ul>
164          *                      <li>{@link #FLAG_RENDER_TEXT_ANNOTATIONS}</li>
165          *                      <li>{@link #FLAG_RENDER_HIGHLIGHT_ANNOTATIONS}</li>
166          *                    </ul>
167          * @see #setRenderFlags(int)
168          * @see #getRenderFlags()
169          */
170         @NonNull
setRenderFlags(@enderFlags int renderFlags, @RenderFlags int mask)171         public Builder setRenderFlags(@RenderFlags int renderFlags, @RenderFlags int mask) {
172             // Sanitize the mask
173             mask &= RENDER_FLAGS_MASK;
174 
175             // Mask the flags
176             renderFlags &= mask;
177 
178             // Merge with non-masked flags
179             this.mRenderFlags = renderFlags | (this.mRenderFlags & ~mask);
180             return this;
181         }
182 
183         /**
184          * Builds the {@link RenderParams} after the optional values has been set.
185          *
186          * @return the newly constructed {@link RenderParams} object
187          */
188         @NonNull
build()189         public RenderParams build() {
190             return new RenderParams(this.mRenderMode, this.mRenderFlags);
191         }
192     }
193 }
194