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 
17 package android.text.style;
18 
19 import android.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.os.Parcel;
22 import android.text.Layout;
23 import android.text.ParcelableSpan;
24 import android.text.TextUtils;
25 
26 /**
27  * A paragraph style affecting the leading margin. There can be multiple leading
28  * margin spans on a single paragraph; they will be rendered in order, each
29  * adding its margin to the ones before it. The leading margin is on the right
30  * for lines in a right-to-left paragraph.
31  * <p>
32  * LeadingMarginSpans should be attached from the first character to the last
33  * character of a single paragraph.
34  */
35 public interface LeadingMarginSpan
36 extends ParagraphStyle
37 {
38     /**
39      * Returns the amount by which to adjust the leading margin. Positive values
40      * move away from the leading edge of the paragraph, negative values move
41      * towards it.
42      *
43      * @param first true if the request is for the first line of a paragraph,
44      * false for subsequent lines
45      * @return the offset for the margin.
46      */
getLeadingMargin(boolean first)47     public int getLeadingMargin(boolean first);
48 
49     /**
50      * Renders the leading margin.  This is called before the margin has been
51      * adjusted by the value returned by {@link #getLeadingMargin(boolean)}.
52      *
53      * @param c the canvas
54      * @param p the paint. The this should be left unchanged on exit.
55      * @param x the current position of the margin
56      * @param dir the base direction of the paragraph; if negative, the margin
57      * is to the right of the text, otherwise it is to the left.
58      * @param top the top of the line
59      * @param baseline the baseline of the line
60      * @param bottom the bottom of the line
61      * @param text the text
62      * @param start the start of the line
63      * @param end the end of the line
64      * @param first true if this is the first line of its paragraph
65      * @param layout the layout containing this line
66      */
drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout)67     public void drawLeadingMargin(Canvas c, Paint p,
68                                   int x, int dir,
69                                   int top, int baseline, int bottom,
70                                   CharSequence text, int start, int end,
71                                   boolean first, Layout layout);
72 
73 
74     /**
75      * An extended version of {@link LeadingMarginSpan}, which allows the
76      * implementor to specify the number of lines of the paragraph to which
77      * this object is attached that the "first line of paragraph" margin width
78      * will be applied to.
79      * <p>
80      * There should only be one LeadingMarginSpan2 per paragraph. The leading
81      * margin line count affects all LeadingMarginSpans in the paragraph,
82      * adjusting the number of lines to which the first line margin is applied.
83      * <p>
84      * As with LeadingMarginSpans, LeadingMarginSpan2s should be attached from
85      * the beginning to the end of a paragraph.
86      */
87     public interface LeadingMarginSpan2 extends LeadingMarginSpan, WrapTogetherSpan {
88         /**
89          * Returns the number of lines of the paragraph to which this object is
90          * attached that the "first line" margin will apply to.
91          */
getLeadingMarginLineCount()92         public int getLeadingMarginLineCount();
93     };
94 
95     /**
96      * The standard implementation of LeadingMarginSpan, which adjusts the
97      * margin but does not do any rendering.
98      */
99     public static class Standard implements LeadingMarginSpan, ParcelableSpan {
100         private final int mFirst, mRest;
101 
102         /**
103          * Constructor taking separate indents for the first and subsequent
104          * lines.
105          *
106          * @param first the indent for the first line of the paragraph
107          * @param rest the indent for the remaining lines of the paragraph
108          */
Standard(int first, int rest)109         public Standard(int first, int rest) {
110             mFirst = first;
111             mRest = rest;
112         }
113 
114         /**
115          * Constructor taking an indent for all lines.
116          * @param every the indent of each line
117          */
Standard(int every)118         public Standard(int every) {
119             this(every, every);
120         }
121 
Standard(Parcel src)122         public Standard(Parcel src) {
123             mFirst = src.readInt();
124             mRest = src.readInt();
125         }
126 
getSpanTypeId()127         public int getSpanTypeId() {
128         return getSpanTypeIdInternal();
129     }
130 
131     /** @hide */
getSpanTypeIdInternal()132     public int getSpanTypeIdInternal() {
133             return TextUtils.LEADING_MARGIN_SPAN;
134         }
135 
describeContents()136         public int describeContents() {
137             return 0;
138         }
139 
writeToParcel(Parcel dest, int flags)140         public void writeToParcel(Parcel dest, int flags) {
141             writeToParcelInternal(dest, flags);
142         }
143 
144         /** @hide */
writeToParcelInternal(Parcel dest, int flags)145         public void writeToParcelInternal(Parcel dest, int flags) {
146             dest.writeInt(mFirst);
147             dest.writeInt(mRest);
148         }
149 
getLeadingMargin(boolean first)150         public int getLeadingMargin(boolean first) {
151             return first ? mFirst : mRest;
152         }
153 
drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout layout)154         public void drawLeadingMargin(Canvas c, Paint p,
155                                       int x, int dir,
156                                       int top, int baseline, int bottom,
157                                       CharSequence text, int start, int end,
158                                       boolean first, Layout layout) {
159             ;
160         }
161     }
162 }
163