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.content; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.graphics.RectF; 22 import android.graphics.pdf.flags.Flags; 23 import android.graphics.pdf.utils.Preconditions; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.util.List; 28 29 /** 30 * <p> 31 * Represents a continuous stream of text in a page of a PDF document in the order of viewing. 32 */ 33 @FlaggedApi(Flags.FLAG_ENABLE_PDF_VIEWER) 34 public final class PdfPageTextContent implements Parcelable { 35 private final String mText; 36 private final List<RectF> mBounds; 37 38 39 /** 40 * Creates a new instance of {@link PdfPageTextContent} using the raw text on the page of the 41 * document. By default, the bounds will be an empty list. 42 * 43 * @param text Text content on the page. 44 * @throws NullPointerException If text is null. 45 */ PdfPageTextContent(@onNull String text)46 public PdfPageTextContent(@NonNull String text) { 47 Preconditions.checkNotNull(text, "Text cannot be null"); 48 this.mText = text; 49 this.mBounds = List.of(); 50 } 51 52 /** 53 * Creates a new instance of {@link PdfPageTextContent} to represent text content within defined 54 * bounds represented by a non-empty list of {@link RectF} on the page of the document. 55 * 56 * @param text Text content within the bounds. 57 * @param bounds Bounds for the text content 58 * @throws NullPointerException If text or bounds is null. 59 */ PdfPageTextContent(@onNull String text, @NonNull List<RectF> bounds)60 public PdfPageTextContent(@NonNull String text, @NonNull List<RectF> bounds) { 61 Preconditions.checkNotNull(text, "Text cannot be null"); 62 Preconditions.checkNotNull(bounds, "Bounds cannot be null"); 63 Preconditions.checkArgument(!bounds.isEmpty(), "Bounds cannot be empty"); 64 this.mText = text; 65 this.mBounds = bounds; 66 } 67 PdfPageTextContent(Parcel in)68 private PdfPageTextContent(Parcel in) { 69 mText = in.readString(); 70 mBounds = in.createTypedArrayList(RectF.CREATOR); 71 } 72 73 @NonNull 74 public static final Creator<PdfPageTextContent> CREATOR = new Creator<PdfPageTextContent>() { 75 @Override 76 public PdfPageTextContent createFromParcel(Parcel in) { 77 return new PdfPageTextContent(in); 78 } 79 80 @Override 81 public PdfPageTextContent[] newArray(int size) { 82 return new PdfPageTextContent[size]; 83 } 84 }; 85 86 /** 87 * Gets the text content on the document. 88 * 89 * @return The text content on the page. 90 */ 91 @NonNull getText()92 public String getText() { 93 return mText; 94 } 95 96 /** 97 * Gets the bounds for the text content represented as a list of {@link RectF}. Each 98 * {@link RectF} represents text content in a single line defined in points (1/72") for its 4 99 * corners. Content spread across multiple lines is represented by list of {@link RectF} in the 100 * order of viewing (left to right and top to bottom). If the text content is unbounded then the 101 * list will be empty. 102 * 103 * @return The bounds of the text content. 104 */ 105 @NonNull getBounds()106 public List<RectF> getBounds() { 107 return mBounds; 108 } 109 110 @Override describeContents()111 public int describeContents() { 112 return 0; 113 } 114 115 @Override writeToParcel(@ndroidx.annotation.NonNull Parcel dest, int flags)116 public void writeToParcel(@androidx.annotation.NonNull Parcel dest, int flags) { 117 dest.writeString(mText); 118 dest.writeTypedList(mBounds); 119 } 120 } 121