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.Paint; 20 import android.graphics.Canvas; 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 */ 32 public interface LeadingMarginSpan 33 extends 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 /** 72 * An extended version of {@link LeadingMarginSpan}, which allows 73 * the implementor to specify the number of lines of text to which 74 * this object is attached that the "first line of paragraph" margin 75 * width will be applied to. 76 */ 77 public interface LeadingMarginSpan2 extends LeadingMarginSpan, WrapTogetherSpan { 78 /** 79 * Returns the number of lines of text to which this object is 80 * attached that the "first line" margin will apply to. 81 * Note that if this returns N, the first N lines of the region, 82 * not the first N lines of each paragraph, will be given the 83 * special margin width. 84 */ 85 public int getLeadingMarginLineCount(); 86 }; 87 88 /** 89 * The standard implementation of LeadingMarginSpan, which adjusts the 90 * margin but does not do any rendering. 91 */ 92 public static class Standard implements LeadingMarginSpan, ParcelableSpan { 93 private final int mFirst, mRest; 94 95 /** 96 * Constructor taking separate indents for the first and subsequent 97 * lines. 98 * 99 * @param first the indent for the first line of the paragraph 100 * @param rest the indent for the remaining lines of the paragraph 101 */ 102 public Standard(int first, int rest) { 103 mFirst = first; 104 mRest = rest; 105 } 106 107 /** 108 * Constructor taking an indent for all lines. 109 * @param every the indent of each line 110 */ 111 public Standard(int every) { 112 this(every, every); 113 } 114 115 public Standard(Parcel src) { 116 mFirst = src.readInt(); 117 mRest = src.readInt(); 118 } 119 120 public int getSpanTypeId() { 121 return TextUtils.LEADING_MARGIN_SPAN; 122 } 123 124 public int describeContents() { 125 return 0; 126 } 127 128 public void writeToParcel(Parcel dest, int flags) { 129 dest.writeInt(mFirst); 130 dest.writeInt(mRest); 131 } 132 133 public int getLeadingMargin(boolean first) { 134 return first ? mFirst : mRest; 135 } 136 137 public void drawLeadingMargin(Canvas c, Paint p, 138 int x, int dir, 139 int top, int baseline, int bottom, 140 CharSequence text, int start, int end, 141 boolean first, Layout layout) { 142 ; 143 } 144 } 145 } 146