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