1 /*
<lambda>null2  * Copyright 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 androidx.compose.ui.text
18 
19 import androidx.compose.ui.geometry.Rect
20 
21 /**
22  * The text inclusion strategy used by [Paragraph.getRangeForRect], it specifies when a range of
23  * text is inside the given rect based on the geometric relation between the text range's bounding
24  * box and the given rect.
25  *
26  * @see Paragraph.getRangeForRect
27  */
28 fun interface TextInclusionStrategy {
29     /**
30      * Returns true if this [TextInclusionStrategy] considers the text range's [textBounds] to be
31      * inside the given [rect].
32      *
33      * @param textBounds the bounding box of a range of the text.
34      * @param rect a rectangle area.
35      */
36     fun isIncluded(textBounds: Rect, rect: Rect): Boolean
37 
38     companion object {
39         /**
40          * The [TextInclusionStrategy] that includes the text range whose bounds has any overlap
41          * with the given rect.
42          */
43         val AnyOverlap = TextInclusionStrategy { textBounds, rect -> textBounds.overlaps(rect) }
44 
45         /**
46          * The [TextInclusionStrategy] that includes the text range whose bounds is completely
47          * contained by the given rect.
48          */
49         val ContainsAll = TextInclusionStrategy { textBounds, rect ->
50             !rect.isEmpty &&
51                 textBounds.left >= rect.left &&
52                 textBounds.right <= rect.right &&
53                 textBounds.top >= rect.top &&
54                 textBounds.bottom <= rect.bottom
55         }
56 
57         /**
58          * The [TextInclusionStrategy] that includes the text range whose bounds' center is
59          * contained by the given rect.
60          */
61         val ContainsCenter = TextInclusionStrategy { textBounds, rect ->
62             rect.contains(textBounds.center)
63         }
64     }
65 }
66