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.component; 18 19 import android.annotation.ColorInt; 20 import android.annotation.FlaggedApi; 21 import android.annotation.NonNull; 22 import android.graphics.pdf.flags.Flags; 23 24 /** 25 * Represents a text object on a PDF page. 26 * This class extends PageObject and provides methods to access and modify the text content. 27 */ 28 @FlaggedApi(Flags.FLAG_ENABLE_EDIT_PDF_TEXT_OBJECTS) 29 public final class PdfPageTextObject extends PdfPageObject { 30 private String mText; 31 private final PdfPageTextObjectFont mFont; 32 private final float mFontSize; 33 private @ColorInt int mStrokeColor; 34 private float mStrokeWidth = 1.0f; 35 private @ColorInt int mFillColor; 36 private @PdfPageObjectRenderMode.Type int mRenderMode; 37 38 /** 39 * Constructor for the PdfPageTextObject. 40 * Sets the object type to TEXT and initializes the text color to black. 41 * 42 * @param font The font of the text. 43 * @param fontSize The font size of the text. 44 */ PdfPageTextObject(@onNull String text, @NonNull PdfPageTextObjectFont font, float fontSize)45 public PdfPageTextObject(@NonNull String text, @NonNull PdfPageTextObjectFont font, 46 float fontSize) { 47 super(PdfPageObjectType.TEXT); 48 this.mText = text; 49 this.mFont = font; 50 this.mFontSize = fontSize; 51 if (Flags.enableEditPdfPageObjects()) { 52 this.mRenderMode = PdfPageObjectRenderMode.FILL; 53 } 54 } 55 56 /** 57 * Returns the text content of the object. 58 * 59 * @return The text content. 60 */ 61 @NonNull getText()62 public String getText() { 63 return mText; 64 } 65 66 /** 67 * Sets the text content of the object. 68 * 69 * @param text The text content to set. 70 */ setText(@onNull String text)71 public void setText(@NonNull String text) { 72 this.mText = text; 73 } 74 75 /** 76 * Returns the font size of the object. 77 * 78 * @return The font size. 79 */ getFontSize()80 public float getFontSize() { 81 return mFontSize; 82 } 83 84 /** 85 * Returns the font of the text. 86 * 87 * @return A copy of the font object. 88 */ 89 @NonNull getFont()90 public PdfPageTextObjectFont getFont() { 91 return new PdfPageTextObjectFont(mFont); 92 } 93 94 /** 95 * Returns the fill color of the object. 96 * 97 * @return The fill color of the object. 98 */ getFillColor()99 public @ColorInt int getFillColor() { 100 return mFillColor; 101 } 102 103 /** 104 * Sets the fill color of the object. 105 * 106 * @param fillColor The fill color of the object. 107 */ setFillColor(@olorInt int fillColor)108 public void setFillColor(@ColorInt int fillColor) { 109 this.mFillColor = fillColor; 110 } 111 112 /** 113 * Returns the stroke width of the object. 114 * 115 * @return The stroke width of the object. 116 */ getStrokeWidth()117 public float getStrokeWidth() { 118 return mStrokeWidth; 119 } 120 121 /** 122 * Sets the stroke width of the object. 123 * 124 * @param strokeWidth The stroke width of the object. 125 */ setStrokeWidth(float strokeWidth)126 public void setStrokeWidth(float strokeWidth) { 127 mStrokeWidth = strokeWidth; 128 } 129 130 /** 131 * Returns the stroke color of the object. 132 * 133 * @return The stroke color of the object. 134 */ getStrokeColor()135 public @ColorInt int getStrokeColor() { 136 return mStrokeColor; 137 } 138 139 /** 140 * Sets the stroke color of the object. 141 * 142 * @param strokeColor The stroke color of the object. 143 */ setStrokeColor(@olorInt int strokeColor)144 public void setStrokeColor(@ColorInt int strokeColor) { 145 this.mStrokeColor = strokeColor; 146 } 147 148 /** 149 * Returns the render mode of the object. 150 * 151 * @return The render mode of the object. 152 */ getRenderMode()153 public @PdfPageObjectRenderMode.Type int getRenderMode() { 154 return mRenderMode; 155 } 156 157 /** 158 * Sets the render mode of the object. 159 * 160 * @param renderMode The render mode to be set. 161 */ setRenderMode(@dfPageObjectRenderMode.Type int renderMode)162 public void setRenderMode(@PdfPageObjectRenderMode.Type int renderMode) { 163 mRenderMode = renderMode; 164 } 165 } 166