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 package com.android.internal.widget.remotecompose.core.operations;
17 
18 import com.android.internal.widget.remotecompose.core.CompanionOperation;
19 import com.android.internal.widget.remotecompose.core.Operation;
20 import com.android.internal.widget.remotecompose.core.Operations;
21 import com.android.internal.widget.remotecompose.core.PaintContext;
22 import com.android.internal.widget.remotecompose.core.PaintOperation;
23 import com.android.internal.widget.remotecompose.core.WireBuffer;
24 
25 import java.util.List;
26 
27 /**
28  * Draw Text
29  */
30 public class DrawText extends PaintOperation {
31     public static final Companion COMPANION = new Companion();
32     int mTextID;
33     int mStart = 0;
34     int mEnd = 0;
35     int mContextStart = 0;
36     int mContextEnd = 0;
37     float mX = 0f;
38     float mY = 0f;
39     boolean mRtl = false;
40 
DrawText(int textID, int start, int end, int contextStart, int contextEnd, float x, float y, boolean rtl)41     public DrawText(int textID,
42                     int start,
43                     int end,
44                     int contextStart,
45                     int contextEnd,
46                     float x,
47                     float y,
48                     boolean rtl) {
49         mTextID = textID;
50         mStart = start;
51         mEnd = end;
52         mContextStart = contextStart;
53         mContextEnd = contextEnd;
54         mX = x;
55         mY = y;
56         mRtl = rtl;
57     }
58 
59     @Override
write(WireBuffer buffer)60     public void write(WireBuffer buffer) {
61         COMPANION.apply(buffer, mTextID, mStart, mEnd, mContextStart, mContextEnd, mX, mY, mRtl);
62 
63     }
64 
65     @Override
toString()66     public String toString() {
67         return "DrawTextRun [" + mTextID + "] " + mStart + ", " + mEnd + ", " + mX + ", " + mY;
68     }
69 
70     public static class Companion implements CompanionOperation {
Companion()71         private Companion() {
72         }
73 
74         @Override
read(WireBuffer buffer, List<Operation> operations)75         public void read(WireBuffer buffer, List<Operation> operations) {
76             int text = buffer.readInt();
77             int start = buffer.readInt();
78             int end = buffer.readInt();
79             int contextStart = buffer.readInt();
80             int contextEnd = buffer.readInt();
81             float x = buffer.readFloat();
82             float y = buffer.readFloat();
83             boolean rtl = buffer.readBoolean();
84             DrawText op = new DrawText(text, start, end, contextStart, contextEnd, x, y, rtl);
85 
86             operations.add(op);
87         }
88 
89         @Override
name()90         public String name() {
91             return "";
92         }
93 
94         @Override
id()95         public int id() {
96             return 0;
97         }
98 
99         /**
100          * Writes out the operation to the buffer
101          * @param buffer
102          * @param textID
103          * @param start
104          * @param end
105          * @param contextStart
106          * @param contextEnd
107          * @param x
108          * @param y
109          * @param rtl
110          */
apply(WireBuffer buffer, int textID, int start, int end, int contextStart, int contextEnd, float x, float y, boolean rtl)111         public void apply(WireBuffer buffer,
112                           int textID,
113                           int start,
114                           int end,
115                           int contextStart,
116                           int contextEnd,
117                           float x,
118                           float y,
119                           boolean rtl) {
120             buffer.start(Operations.DRAW_TEXT_RUN);
121             buffer.writeInt(textID);
122             buffer.writeInt(start);
123             buffer.writeInt(end);
124             buffer.writeInt(contextStart);
125             buffer.writeInt(contextEnd);
126             buffer.writeFloat(x);
127             buffer.writeFloat(y);
128             buffer.writeBoolean(rtl);
129         }
130     }
131 
132     @Override
paint(PaintContext context)133     public void paint(PaintContext context) {
134         context.drawTextRun(mTextID, mStart, mEnd, mContextStart, mContextEnd, mX, mY, mRtl);
135     }
136 }
137