1 /* 2 * Copyright (C) 2023 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 package com.android.internal.widget.remotecompose.core.operations.paint; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 21 /** Provides a Builder pattern for a PaintBundle */ 22 class Painter { 23 PaintBundle mPaint; 24 25 /** Write the paint to the buffer */ commit()26 public PaintBundle commit() { 27 return mPaint; 28 } 29 30 @NonNull setAntiAlias(boolean aa)31 public Painter setAntiAlias(boolean aa) { 32 mPaint.setAntiAlias(aa); 33 return this; 34 } 35 36 @NonNull setColor(int color)37 public Painter setColor(int color) { 38 mPaint.setColor(color); 39 return this; 40 } 41 42 @NonNull setColorId(int colorId)43 public Painter setColorId(int colorId) { 44 mPaint.setColorId(colorId); 45 return this; 46 } 47 48 /** 49 * Set the paint's Join. 50 * 51 * @param join set the paint's Join, used whenever the paint's style is Stroke or StrokeAndFill. 52 */ 53 @NonNull setStrokeJoin(int join)54 public Painter setStrokeJoin(int join) { 55 mPaint.setStrokeJoin(join); 56 return this; 57 } 58 59 /** 60 * Set the width for stroking. Pass 0 to stroke in hairline mode. Hairlines always draws a 61 * single pixel independent of the canvas's matrix. 62 * 63 * @param width set the paint's stroke width, used whenever the paint's style is Stroke or 64 * StrokeAndFill. 65 */ 66 @NonNull setStrokeWidth(float width)67 public Painter setStrokeWidth(float width) { 68 mPaint.setStrokeWidth(width); 69 return this; 70 } 71 72 /** 73 * Set the paint's style, used for controlling how primitives' geometries are interpreted 74 * (except for drawBitmap, which always assumes Fill). 75 * 76 * @param style The new style to set in the paint 77 */ 78 @NonNull setStyle(int style)79 public Painter setStyle(int style) { 80 mPaint.setStyle(style); 81 return this; 82 } 83 84 /** 85 * Set the paint's Cap. 86 * 87 * @param cap set the paint's line cap style, used whenever the paint's style is Stroke or 88 * StrokeAndFill. 89 */ 90 @NonNull setStrokeCap(int cap)91 public Painter setStrokeCap(int cap) { 92 mPaint.setStrokeCap(cap); 93 return this; 94 } 95 96 /** 97 * Set the paint's stroke miter value. This is used to control the behavior of miter joins when 98 * the joins angle is sharp. This value must be >= 0. 99 * 100 * @param miter set the miter limit on the paint, used whenever the paint's style is Stroke or 101 * StrokeAndFill. 102 */ 103 @NonNull setStrokeMiter(float miter)104 public Painter setStrokeMiter(float miter) { 105 mPaint.setStrokeMiter(miter); 106 return this; 107 } 108 109 /** 110 * Helper to setColor(), that only assigns the color's alpha value, leaving its r,g,b values 111 * unchanged. Results are undefined if the alpha value is outside of the range [0..1.0] 112 * 113 * @param alpha set the alpha component [0..1.0] of the paint's color. 114 */ 115 @NonNull setAlpha(float alpha)116 public Painter setAlpha(float alpha) { 117 mPaint.setAlpha((alpha > 2) ? alpha / 255f : alpha); 118 return this; 119 } 120 121 /** 122 * Create a color filter that uses the specified color and Porter-Duff mode. 123 * 124 * @param color The ARGB source color used with the specified Porter-Duff mode 125 * @param mode The porter-duff mode that is applied 126 */ 127 @NonNull setPorterDuffColorFilter(int color, int mode)128 public Painter setPorterDuffColorFilter(int color, int mode) { 129 mPaint.setColorFilter(color, mode); 130 return this; 131 } 132 133 /** 134 * sets a shader that draws a linear gradient along a line. 135 * 136 * @param startX The x-coordinate for the start of the gradient line 137 * @param startY The y-coordinate for the start of the gradient line 138 * @param endX The x-coordinate for the end of the gradient line 139 * @param endY The y-coordinate for the end of the gradient line 140 * @param colors The sRGB colors to be distributed along the gradient line 141 * @param positions May be null. The relative positions [0..1] of each corresponding color in 142 * the colors array. If this is null, the colors are distributed evenly along the gradient 143 * line. 144 * @param tileMode The Shader tiling mode 145 */ 146 @NonNull setLinearGradient( float startX, float startY, float endX, float endY, int[] colors, float[] positions, int tileMode)147 public Painter setLinearGradient( 148 float startX, 149 float startY, 150 float endX, 151 float endY, 152 int[] colors, 153 float[] positions, 154 int tileMode) { 155 mPaint.setLinearGradient(colors, 0, positions, startX, startY, endX, endY, tileMode); 156 return this; 157 } 158 159 /** 160 * Sets a shader that draws a radial gradient given the center and radius. 161 * 162 * @param centerX The x-coordinate of the center of the radius 163 * @param centerY The y-coordinate of the center of the radius 164 * @param radius Must be positive. The radius of the circle for this gradient. 165 * @param colors The sRGB colors to be distributed between the center and edge of the circle 166 * @param positions May be <code>null</code>. Valid values are between <code>0.0f</code> and 167 * <code>1.0f</code>. The relative position of each corresponding color in the colors array. 168 * If <code>null</code>, colors are distributed evenly between the center and edge of the 169 * circle. 170 * @param tileMode The Shader tiling mode 171 */ 172 @NonNull setRadialGradient( float centerX, float centerY, float radius, @NonNull int[] colors, @NonNull float[] positions, int tileMode)173 public Painter setRadialGradient( 174 float centerX, 175 float centerY, 176 float radius, 177 @NonNull int[] colors, 178 @NonNull float[] positions, 179 int tileMode) { 180 mPaint.setRadialGradient(colors, 0, positions, centerX, centerY, radius, tileMode); 181 return this; 182 } 183 184 /** 185 * Set a shader that draws a sweep gradient around a center point. 186 * 187 * @param centerX The x-coordinate of the center 188 * @param centerY The y-coordinate of the center 189 * @param colors The sRGB colors to be distributed between around the center. There must be at 190 * least 2 colors in the array. 191 * @param positions May be NULL. The relative position of each corresponding color in the colors 192 * array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing 193 * may produce unexpected results. If positions is NULL, then the colors are automatically 194 * spaced evenly. 195 */ 196 @NonNull setSweepGradient( float centerX, float centerY, @NonNull int[] colors, @Nullable float[] positions)197 public Painter setSweepGradient( 198 float centerX, float centerY, @NonNull int[] colors, @Nullable float[] positions) { 199 mPaint.setSweepGradient(colors, 0, positions, centerX, centerY); 200 return this; 201 } 202 203 /** 204 * Set the paint's text size. This value must be > 0 205 * 206 * @param size set the paint's text size in pixel units. 207 */ 208 @NonNull setTextSize(float size)209 public Painter setTextSize(float size) { 210 mPaint.setTextSize(size); 211 return this; 212 } 213 214 /** 215 * sets a typeface object that best matches the specified existing typeface and the specified 216 * weight and italic style 217 * 218 * <p>Below are numerical values and corresponding common weight names. 219 * 220 * <table> <thead> 221 * <tr><th>Value</th><th>Common weight name</th></tr> </thead> <tbody> 222 * <tr><td>100</td><td>Thin</td></tr> 223 * <tr><td>200</td><td>Extra Light</td></tr> 224 * <tr><td>300</td><td>Light</td></tr> 225 * <tr><td>400</td><td>Normal</td></tr> 226 * <tr><td>500</td><td>Medium</td></tr> 227 * <tr><td>600</td><td>Semi Bold</td></tr> 228 * <tr><td>700</td><td>Bold</td></tr> 229 * <tr><td>800</td><td>Extra Bold</td></tr> 230 * <tr><td>900</td><td>Black</td></tr> </tbody> </table> 231 * 232 * @param fontType 0 = default 1 = sans serif 2 = serif 3 = monospace 233 * @param weight The desired weight to be drawn. 234 * @param italic {@code true} if italic style is desired to be drawn. Otherwise, {@code false} 235 */ 236 @NonNull setTypeface(int fontType, int weight, boolean italic)237 public Painter setTypeface(int fontType, int weight, boolean italic) { 238 mPaint.setTextStyle(fontType, weight, italic); 239 return this; 240 } 241 242 @NonNull setFilterBitmap(boolean filter)243 public Painter setFilterBitmap(boolean filter) { 244 mPaint.setFilterBitmap(filter); 245 return this; 246 } 247 248 @NonNull setShader(int id)249 public Painter setShader(int id) { 250 mPaint.setShader(id); 251 return this; 252 } 253 } 254