1/*
2 * Copyright (C) 2006 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
17package android.text;
18
19/**
20 * This is the interface for text that has markup objects attached to
21 * ranges of it.  Not all text classes have mutable markup or text;
22 * see {@link Spannable} for mutable markup and {@link Editable} for
23 * mutable text.
24 */
25public interface Spanned
26extends CharSequence
27{
28    /**
29     * Bitmask of bits that are relevent for controlling point/mark behavior
30     * of spans.
31     *
32     * MARK and POINT are conceptually located <i>between</i> two adjacent characters.
33     * A MARK is "attached" to the character before, while a POINT will stick to the character
34     * after. The insertion cursor is conceptually located between the MARK and the POINT.
35     *
36     * As a result, inserting a new character between a MARK and a POINT will leave the MARK
37     * unchanged, while the POINT will be shifted, now located after the inserted character and
38     * still glued to the same character after it.
39     *
40     * Depending on whether the insertion happens at the beginning or the end of a span, the span
41     * will hence be expanded to <i>include</i> the new character (when the span is using a MARK at
42     * its beginning or a POINT at its end) or it will be <i>excluded</i>.
43     *
44     * Note that <i>before</i> and <i>after</i> here refer to offsets in the String, which are
45     * independent from the visual representation of the text (left-to-right or right-to-left).
46     */
47    public static final int SPAN_POINT_MARK_MASK = 0x33;
48
49    /**
50     * 0-length spans with type SPAN_MARK_MARK behave like text marks:
51     * they remain at their original offset when text is inserted
52     * at that offset. Conceptually, the text is added after the mark.
53     */
54    public static final int SPAN_MARK_MARK =   0x11;
55    /**
56     * SPAN_MARK_POINT is a synonym for {@link #SPAN_INCLUSIVE_INCLUSIVE}.
57     */
58    public static final int SPAN_MARK_POINT =  0x12;
59    /**
60     * SPAN_POINT_MARK is a synonym for {@link #SPAN_EXCLUSIVE_EXCLUSIVE}.
61     */
62    public static final int SPAN_POINT_MARK =  0x21;
63
64    /**
65     * 0-length spans with type SPAN_POINT_POINT behave like cursors:
66     * they are pushed forward by the length of the insertion when text
67     * is inserted at their offset.
68     * The text is conceptually inserted before the point.
69     */
70    public static final int SPAN_POINT_POINT = 0x22;
71
72    /**
73     * SPAN_PARAGRAPH behaves like SPAN_INCLUSIVE_EXCLUSIVE
74     * (SPAN_MARK_MARK), except that if either end of the span is
75     * at the end of the buffer, that end behaves like _POINT
76     * instead (so SPAN_INCLUSIVE_INCLUSIVE if it starts in the
77     * middle and ends at the end, or SPAN_EXCLUSIVE_INCLUSIVE
78     * if it both starts and ends at the end).
79     * <p>
80     * Its endpoints must be the start or end of the buffer or
81     * immediately after a \n character, and if the \n
82     * that anchors it is deleted, the endpoint is pulled to the
83     * next \n that follows in the buffer (or to the end of
84     * the buffer). If a span with SPAN_PARAGRAPH flag is pasted
85     * into another text and the paragraph boundary constraint
86     * is not satisfied, the span is discarded.
87     */
88    public static final int SPAN_PARAGRAPH =   0x33;
89
90    /**
91     * Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand
92     * to include text inserted at their starting point but not at their
93     * ending point.  When 0-length, they behave like marks.
94     */
95    public static final int SPAN_INCLUSIVE_EXCLUSIVE = SPAN_MARK_MARK;
96
97    /**
98     * Spans of type SPAN_INCLUSIVE_INCLUSIVE expand
99     * to include text inserted at either their starting or ending point.
100     */
101    public static final int SPAN_INCLUSIVE_INCLUSIVE = SPAN_MARK_POINT;
102
103    /**
104     * Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand
105     * to include text inserted at either their starting or ending point.
106     * They can never have a length of 0 and are automatically removed
107     * from the buffer if all the text they cover is removed.
108     */
109    public static final int SPAN_EXCLUSIVE_EXCLUSIVE = SPAN_POINT_MARK;
110
111    /**
112     * Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand
113     * to include text inserted at their ending point but not at their
114     * starting point.  When 0-length, they behave like points.
115     */
116    public static final int SPAN_EXCLUSIVE_INCLUSIVE = SPAN_POINT_POINT;
117
118    /**
119     * This flag is set on spans that are being used to apply temporary
120     * styling information on the composing text of an input method, so that
121     * they can be found and removed when the composing text is being
122     * replaced.
123     */
124    public static final int SPAN_COMPOSING = 0x100;
125
126    /**
127     * This flag will be set for intermediate span changes, meaning there
128     * is guaranteed to be another change following it.  Typically it is
129     * used for {@link Selection} which automatically uses this with the first
130     * offset it sets when updating the selection.
131     */
132    public static final int SPAN_INTERMEDIATE = 0x200;
133
134    /**
135     * The bits numbered SPAN_USER_SHIFT and above are available
136     * for callers to use to store scalar data associated with their
137     * span object.
138     */
139    public static final int SPAN_USER_SHIFT = 24;
140    /**
141     * The bits specified by the SPAN_USER bitfield are available
142     * for callers to use to store scalar data associated with their
143     * span object.
144     */
145    public static final int SPAN_USER = 0xFFFFFFFF << SPAN_USER_SHIFT;
146
147    /**
148     * The bits numbered just above SPAN_PRIORITY_SHIFT determine the order
149     * of change notifications -- higher numbers go first.  You probably
150     * don't need to set this; it is used so that when text changes, the
151     * text layout gets the chance to update itself before any other
152     * callbacks can inquire about the layout of the text.
153     */
154    public static final int SPAN_PRIORITY_SHIFT = 16;
155    /**
156     * The bits specified by the SPAN_PRIORITY bitmap determine the order
157     * of change notifications -- higher numbers go first.  You probably
158     * don't need to set this; it is used so that when text changes, the
159     * text layout gets the chance to update itself before any other
160     * callbacks can inquire about the layout of the text.
161     */
162    public static final int SPAN_PRIORITY = 0xFF << SPAN_PRIORITY_SHIFT;
163
164    /**
165     * Return an array of the markup objects attached to the specified
166     * slice of this CharSequence and whose type is the specified type
167     * or a subclass of it.  Specify Object.class for the type if you
168     * want all the objects regardless of type.
169     */
170    public <T> T[] getSpans(int start, int end, Class<T> type);
171
172    /**
173     * Return the beginning of the range of text to which the specified
174     * markup object is attached, or -1 if the object is not attached.
175     */
176    public int getSpanStart(Object tag);
177
178    /**
179     * Return the end of the range of text to which the specified
180     * markup object is attached, or -1 if the object is not attached.
181     */
182    public int getSpanEnd(Object tag);
183
184    /**
185     * Return the flags that were specified when {@link Spannable#setSpan} was
186     * used to attach the specified markup object, or 0 if the specified
187     * object has not been attached.
188     */
189    public int getSpanFlags(Object tag);
190
191    /**
192     * Return the first offset greater than <code>start</code> where a markup
193     * object of class <code>type</code> begins or ends, or <code>limit</code>
194     * if there are no starts or ends greater than <code>start</code> but less
195     * than <code>limit</code>. Specify <code>null</code> or Object.class for
196     * the type if you want every transition regardless of type.
197     */
198    public int nextSpanTransition(int start, int limit, Class type);
199}
200