LeadingMarginSpan.java revision 7951eaa92a962e39ebba0366fdcafc4a0a78cc98
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.style;
18
19import android.graphics.Paint;
20import android.graphics.Canvas;
21import android.os.Parcel;
22import android.text.Layout;
23import android.text.ParcelableSpan;
24import 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 */
32public interface LeadingMarginSpan
33extends ParagraphStyle
34{
35    /**
36     * Returns the amount by which to adjust the leading margin. Positive values
37     * move away from the leading edge of the paragraph, negative values move
38     * towards it.
39     *
40     * @param first true if the request is for the first line of a paragraph,
41     * false for subsequent lines
42     * @return the offset for the margin.
43     */
44    public int getLeadingMargin(boolean first);
45
46    /**
47     * Renders the leading margin.  This is called before the margin has been
48     * adjusted by the value returned by {@link #getLeadingMargin(boolean)}.
49     *
50     * @param c the canvas
51     * @param p the paint. The this should be left unchanged on exit.
52     * @param x the current position of the margin
53     * @param dir the base direction of the paragraph; if negative, the margin
54     * is to the right of the text, otherwise it is to the left.
55     * @param top the top of the line
56     * @param baseline the baseline of the line
57     * @param bottom the bottom of the line
58     * @param text the text
59     * @param start the start of the line
60     * @param end the end of the line
61     * @param first true if this is the first line of its paragraph
62     * @param layout the layout containing this line
63     */
64    public void drawLeadingMargin(Canvas c, Paint p,
65                                  int x, int dir,
66                                  int top, int baseline, int bottom,
67                                  CharSequence text, int start, int end,
68                                  boolean first, Layout layout);
69
70
71    public interface LeadingMarginSpan2 extends LeadingMarginSpan, WrapTogetherSpan {
72        public int getLeadingMarginLineCount();
73    };
74
75    /**
76     * The standard implementation of LeadingMarginSpan, which adjusts the
77     * margin but does not do any rendering.
78     */
79    public static class Standard implements LeadingMarginSpan, ParcelableSpan {
80        private final int mFirst, mRest;
81
82        /**
83         * Constructor taking separate indents for the first and subsequent
84         * lines.
85         *
86         * @param first the indent for the first line of the paragraph
87         * @param rest the indent for the remaining lines of the paragraph
88         */
89        public Standard(int first, int rest) {
90            mFirst = first;
91            mRest = rest;
92        }
93
94        /**
95         * Constructor taking an indent for all lines.
96         * @param every the indent of each line
97         */
98        public Standard(int every) {
99            this(every, every);
100        }
101
102        public Standard(Parcel src) {
103            mFirst = src.readInt();
104            mRest = src.readInt();
105        }
106
107        public int getSpanTypeId() {
108            return TextUtils.LEADING_MARGIN_SPAN;
109        }
110
111        public int describeContents() {
112            return 0;
113        }
114
115        public void writeToParcel(Parcel dest, int flags) {
116            dest.writeInt(mFirst);
117            dest.writeInt(mRest);
118        }
119
120        public int getLeadingMargin(boolean first) {
121            return first ? mFirst : mRest;
122        }
123
124        public void drawLeadingMargin(Canvas c, Paint p,
125                                      int x, int dir,
126                                      int top, int baseline, int bottom,
127                                      CharSequence text, int start, int end,
128                                      boolean first, Layout layout) {
129            ;
130        }
131    }
132}
133