1 /*
2  * Copyright (C) 2018 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.text;
18 
19 import com.android.layoutlib.bridge.impl.DelegateManager;
20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
21 
22 import android.graphics.Rect;
23 import android.graphics.text.LineBreaker_Delegate.Builder;
24 import android.graphics.text.LineBreaker_Delegate.Run;
25 
26 import libcore.util.NativeAllocationRegistry_Delegate;
27 
28 /**
29  * Delegate that provides implementation for native methods in
30  * {@link android.graphics.text.MeasuredText}
31  * <p/>
32  * Through the layoutlib_create tool, selected methods of StaticLayout have been replaced
33  * by calls to methods of the same name in this delegate class.
34  *
35  */
36 public class MeasuredText_Delegate {
37 
38     // ---- Builder delegate manager ----
39     protected static final DelegateManager<MeasuredText_Delegate> sManager =
40             new DelegateManager<>(MeasuredText_Delegate.class);
41     private static long sFinalizer = -1;
42 
43     protected long mNativeBuilderPtr;
44 
45     @LayoutlibDelegate
nGetWidth(long nativePtr, int start, int end)46     /*package*/ static float nGetWidth(long nativePtr, int start, int end) {
47         // Ignore as it is not used for the layoutlib implementation
48         return 0.0f;
49     }
50 
51     @LayoutlibDelegate
nGetReleaseFunc()52     /*package*/ static long nGetReleaseFunc() {
53         synchronized (MeasuredText_Delegate.class) {
54             if (sFinalizer == -1) {
55                 sFinalizer = NativeAllocationRegistry_Delegate.createFinalizer(
56                         sManager::removeJavaReferenceFor);
57             }
58         }
59         return sFinalizer;
60     }
61 
62     @LayoutlibDelegate
nGetMemoryUsage(long nativePtr)63     /*package*/ static int nGetMemoryUsage(long nativePtr) {
64         // Ignore as it is not used for the layoutlib implementation
65         return 0;
66     }
67 
68     @LayoutlibDelegate
nGetBounds(long nativePtr, char[] buf, int start, int end, Rect rect)69     /*package*/ static void nGetBounds(long nativePtr, char[] buf, int start, int end, Rect rect) {
70         // Ignore as it is not used for the layoutlib implementation
71     }
72 
73     @LayoutlibDelegate
nGetCharWidthAt(long nativePtr, int offset)74     /*package*/ static float nGetCharWidthAt(long nativePtr, int offset) {
75         // Ignore as it is not used for the layoutlib implementation
76         return 0.0f;
77     }
78 
computeRuns(long measuredTextPtr, Builder staticLayoutBuilder)79     public static void computeRuns(long measuredTextPtr, Builder staticLayoutBuilder) {
80         MeasuredText_Delegate delegate = sManager.getDelegate(measuredTextPtr);
81         if (delegate == null) {
82             return;
83         }
84         MeasuredText_Builder_Delegate builder =
85                 MeasuredText_Builder_Delegate.sBuilderManager.getDelegate(delegate.mNativeBuilderPtr);
86         if (builder == null) {
87             return;
88         }
89         for (Run run: builder.mRuns) {
90             run.addTo(staticLayoutBuilder);
91         }
92     }
93 }