use of android.text.style.LeadingMarginSpan in project android_frameworks_base by crdroidandroid.
the class Layout method getParagraphLeadingMargin.
/**
* Returns the effective leading margin (unsigned) for this line,
* taking into account LeadingMarginSpan and LeadingMarginSpan2.
* @param line the line index
* @return the leading margin of this line
*/
private int getParagraphLeadingMargin(int line) {
if (!mSpannedText) {
return 0;
}
Spanned spanned = (Spanned) mText;
int lineStart = getLineStart(line);
int lineEnd = getLineEnd(line);
int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd, LeadingMarginSpan.class);
LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd, LeadingMarginSpan.class);
if (spans.length == 0) {
// no leading margin span;
return 0;
}
int margin = 0;
boolean isFirstParaLine = lineStart == 0 || spanned.charAt(lineStart - 1) == '\n';
boolean useFirstLineMargin = isFirstParaLine;
for (int i = 0; i < spans.length; i++) {
if (spans[i] instanceof LeadingMarginSpan2) {
int spStart = spanned.getSpanStart(spans[i]);
int spanLine = getLineForOffset(spStart);
int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
// if there is more than one LeadingMarginSpan2, use the count that is greatest
useFirstLineMargin |= line < spanLine + count;
}
}
for (int i = 0; i < spans.length; i++) {
LeadingMarginSpan span = spans[i];
margin += span.getLeadingMargin(useFirstLineMargin);
}
return margin;
}
use of android.text.style.LeadingMarginSpan in project android_frameworks_base by crdroidandroid.
the class Layout method measurePara.
/* package */
static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
MeasuredText mt = MeasuredText.obtain();
TextLine tl = TextLine.obtain();
try {
mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
Directions directions;
int dir;
if (mt.mEasy) {
directions = DIRS_ALL_LEFT_TO_RIGHT;
dir = Layout.DIR_LEFT_TO_RIGHT;
} else {
directions = AndroidBidi.directions(mt.mDir, mt.mLevels, 0, mt.mChars, 0, mt.mLen);
dir = mt.mDir;
}
char[] chars = mt.mChars;
int len = mt.mLen;
boolean hasTabs = false;
TabStops tabStops = null;
// leading margins should be taken into account when measuring a paragraph
int margin = 0;
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
LeadingMarginSpan[] spans = getParagraphSpans(spanned, start, end, LeadingMarginSpan.class);
for (LeadingMarginSpan lms : spans) {
margin += lms.getLeadingMargin(true);
}
}
for (int i = 0; i < len; ++i) {
if (chars[i] == '\t') {
hasTabs = true;
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
int spanEnd = spanned.nextSpanTransition(start, end, TabStopSpan.class);
TabStopSpan[] spans = getParagraphSpans(spanned, start, spanEnd, TabStopSpan.class);
if (spans.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, spans);
}
}
break;
}
}
tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
return margin + tl.metrics(null);
} finally {
TextLine.recycle(tl);
MeasuredText.recycle(mt);
}
}
use of android.text.style.LeadingMarginSpan in project BBS-Android by bdpqchen.
the class HtmlTagHandler method handleTag.
@Override
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
if (opening) {
// opening tag
if (HtmlTextView.DEBUG) {
Log.d(HtmlTextView.TAG, "opening, output: " + output.toString());
}
if (tag.equalsIgnoreCase(UNORDERED_LIST)) {
lists.push(tag);
} else if (tag.equalsIgnoreCase(ORDERED_LIST)) {
lists.push(tag);
olNextIndex.push(1);
} else if (tag.equalsIgnoreCase(LIST_ITEM)) {
if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
output.append("\n");
}
if (!lists.isEmpty()) {
String parentList = lists.peek();
if (parentList.equalsIgnoreCase(ORDERED_LIST)) {
start(output, new Ol());
olNextIndex.push(olNextIndex.pop() + 1);
} else if (parentList.equalsIgnoreCase(UNORDERED_LIST)) {
start(output, new Ul());
}
}
} else if (tag.equalsIgnoreCase("code")) {
start(output, new Code());
} else if (tag.equalsIgnoreCase("center")) {
start(output, new Center());
} else if (tag.equalsIgnoreCase("s") || tag.equalsIgnoreCase("strike")) {
start(output, new Strike());
} else if (tag.equalsIgnoreCase("table")) {
start(output, new Table());
if (tableTagLevel == 0) {
tableHtmlBuilder = new StringBuilder();
// We need some text for the table to be replaced by the span because
// the other tags will remove their text when their text is extracted
output.append("table placeholder");
}
tableTagLevel++;
} else if (tag.equalsIgnoreCase("tr")) {
start(output, new Tr());
} else if (tag.equalsIgnoreCase("th")) {
start(output, new Th());
} else if (tag.equalsIgnoreCase("td")) {
start(output, new Td());
}
} else {
// closing tag
if (HtmlTextView.DEBUG) {
Log.d(HtmlTextView.TAG, "closing, output: " + output.toString());
}
if (tag.equalsIgnoreCase(UNORDERED_LIST)) {
lists.pop();
} else if (tag.equalsIgnoreCase(ORDERED_LIST)) {
lists.pop();
olNextIndex.pop();
} else if (tag.equalsIgnoreCase(LIST_ITEM)) {
if (!lists.isEmpty()) {
if (lists.peek().equalsIgnoreCase(UNORDERED_LIST)) {
if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
output.append("\n");
}
// Nested BulletSpans increases distance between bullet and text, so we must prevent it.
int bulletMargin = indent;
if (lists.size() > 1) {
bulletMargin = indent - bullet.getLeadingMargin(true);
if (lists.size() > 2) {
// This get's more complicated when we add a LeadingMarginSpan into the same line:
// we have also counter it's effect to BulletSpan
bulletMargin -= (lists.size() - 2) * listItemIndent;
}
}
BulletSpan newBullet = new BulletSpan(bulletMargin);
end(output, Ul.class, false, new LeadingMarginSpan.Standard(listItemIndent * (lists.size() - 1)), newBullet);
} else if (lists.peek().equalsIgnoreCase(ORDERED_LIST)) {
if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') {
output.append("\n");
}
int numberMargin = listItemIndent * (lists.size() - 1);
if (lists.size() > 2) {
// Same as in ordered lists: counter the effect of nested Spans
numberMargin -= (lists.size() - 2) * listItemIndent;
}
NumberSpan numberSpan = new NumberSpan(mTextPaint, olNextIndex.lastElement() - 1);
end(output, Ol.class, false, new LeadingMarginSpan.Standard(numberMargin), numberSpan);
}
}
} else if (tag.equalsIgnoreCase("code")) {
end(output, Code.class, false, new TypefaceSpan("monospace"));
} else if (tag.equalsIgnoreCase("center")) {
end(output, Center.class, true, new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER));
} else if (tag.equalsIgnoreCase("s") || tag.equalsIgnoreCase("strike")) {
end(output, Strike.class, false, new StrikethroughSpan());
} else if (tag.equalsIgnoreCase("table")) {
tableTagLevel--;
// When we're back at the root-level table
if (tableTagLevel == 0) {
final String tableHtml = tableHtmlBuilder.toString();
ClickableTableSpan myClickableTableSpan = null;
if (clickableTableSpan != null) {
myClickableTableSpan = clickableTableSpan.newInstance();
myClickableTableSpan.setTableHtml(tableHtml);
}
DrawTableLinkSpan myDrawTableLinkSpan = null;
if (drawTableLinkSpan != null) {
myDrawTableLinkSpan = drawTableLinkSpan.newInstance();
}
end(output, Table.class, false, myDrawTableLinkSpan, myClickableTableSpan);
} else {
end(output, Table.class, false);
}
} else if (tag.equalsIgnoreCase("tr")) {
end(output, Tr.class, false);
} else if (tag.equalsIgnoreCase("th")) {
end(output, Th.class, false);
} else if (tag.equalsIgnoreCase("td")) {
end(output, Td.class, false);
}
}
storeTableTags(opening, tag);
}
use of android.text.style.LeadingMarginSpan in project android_frameworks_base by DirtyUnicorns.
the class Layout method drawText.
/**
* @hide
*/
public void drawText(Canvas canvas, int firstLine, int lastLine) {
int previousLineBottom = getLineTop(firstLine);
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spanEnd = 0;
TextPaint paint = mPaint;
CharSequence buf = mText;
Alignment paraAlign = mAlignment;
TabStops tabStops = null;
boolean tabStopsIsInitialized = false;
TextLine tl = TextLine.obtain();
// The baseline is the top of the following line minus the current line's descent.
for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
int start = previousLineEnd;
previousLineEnd = getLineStart(lineNum + 1);
int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
int ltop = previousLineBottom;
int lbottom = getLineTop(lineNum + 1);
previousLineBottom = lbottom;
int lbaseline = lbottom - getLineDescent(lineNum);
int dir = getParagraphDirection(lineNum);
int left = 0;
int right = mWidth;
if (mSpannedText) {
Spanned sp = (Spanned) buf;
int textLength = buf.length();
boolean isFirstParaLine = (start == 0 || buf.charAt(start - 1) == '\n');
// our problem.
if (start >= spanEnd && (lineNum == firstLine || isFirstParaLine)) {
spanEnd = sp.nextSpanTransition(start, textLength, ParagraphStyle.class);
spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
paraAlign = mAlignment;
for (int n = spans.length - 1; n >= 0; n--) {
if (spans[n] instanceof AlignmentSpan) {
paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
break;
}
}
tabStopsIsInitialized = false;
}
// Draw all leading margin spans. Adjust left or right according
// to the paragraph direction of the line.
final int length = spans.length;
boolean useFirstLineMargin = isFirstParaLine;
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan2) {
int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
// the count that is greatest
if (lineNum < startLine + count) {
useFirstLineMargin = true;
break;
}
}
}
for (int n = 0; n < length; n++) {
if (spans[n] instanceof LeadingMarginSpan) {
LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
if (dir == DIR_RIGHT_TO_LEFT) {
margin.drawLeadingMargin(canvas, paint, right, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
right -= margin.getLeadingMargin(useFirstLineMargin);
} else {
margin.drawLeadingMargin(canvas, paint, left, dir, ltop, lbaseline, lbottom, buf, start, end, isFirstParaLine, this);
left += margin.getLeadingMargin(useFirstLineMargin);
}
}
}
}
boolean hasTab = getLineContainsTab(lineNum);
// Can't tell if we have tabs for sure, currently
if (hasTab && !tabStopsIsInitialized) {
if (tabStops == null) {
tabStops = new TabStops(TAB_INCREMENT, spans);
} else {
tabStops.reset(TAB_INCREMENT, spans);
}
tabStopsIsInitialized = true;
}
// Determine whether the line aligns to normal, opposite, or center.
Alignment align = paraAlign;
if (align == Alignment.ALIGN_LEFT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
} else if (align == Alignment.ALIGN_RIGHT) {
align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
}
int x;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
} else {
x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
}
} else {
int max = (int) getLineExtent(lineNum, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
} else {
x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
}
} else {
// Alignment.ALIGN_CENTER
max = max & ~1;
x = ((right + left - max) >> 1) + getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
}
}
paint.setHyphenEdit(getHyphen(lineNum));
Directions directions = getLineDirections(lineNum);
if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
// XXX: assumes there's nothing additional to be done
canvas.drawText(buf, start, end, x, lbaseline, paint);
} else {
tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
tl.draw(canvas, x, ltop, lbaseline, lbottom);
}
paint.setHyphenEdit(0);
}
TextLine.recycle(tl);
}
use of android.text.style.LeadingMarginSpan in project android_frameworks_base by ParanoidAndroid.
the class StaticLayout method generate.
/* package */
void generate(CharSequence source, int bufStart, int bufEnd, TextPaint paint, int outerWidth, TextDirectionHeuristic textDir, float spacingmult, float spacingadd, boolean includepad, boolean trackpad, float ellipsizedWidth, TextUtils.TruncateAt ellipsize) {
mLineCount = 0;
int v = 0;
boolean needMultiply = (spacingmult != 1 || spacingadd != 0);
Paint.FontMetricsInt fm = mFontMetricsInt;
int[] chooseHtv = null;
MeasuredText measured = mMeasured;
Spanned spanned = null;
if (source instanceof Spanned)
spanned = (Spanned) source;
// XXX
int DEFAULT_DIR = DIR_LEFT_TO_RIGHT;
int paraEnd;
for (int paraStart = bufStart; paraStart <= bufEnd; paraStart = paraEnd) {
paraEnd = TextUtils.indexOf(source, CHAR_NEW_LINE, paraStart, bufEnd);
if (paraEnd < 0)
paraEnd = bufEnd;
else
paraEnd++;
int firstWidthLineLimit = mLineCount + 1;
int firstWidth = outerWidth;
int restWidth = outerWidth;
LineHeightSpan[] chooseHt = null;
if (spanned != null) {
LeadingMarginSpan[] sp = getParagraphSpans(spanned, paraStart, paraEnd, LeadingMarginSpan.class);
for (int i = 0; i < sp.length; i++) {
LeadingMarginSpan lms = sp[i];
firstWidth -= sp[i].getLeadingMargin(true);
restWidth -= sp[i].getLeadingMargin(false);
// paragraph.
if (lms instanceof LeadingMarginSpan2) {
LeadingMarginSpan2 lms2 = (LeadingMarginSpan2) lms;
int lmsFirstLine = getLineForOffset(spanned.getSpanStart(lms2));
firstWidthLineLimit = lmsFirstLine + lms2.getLeadingMarginLineCount();
}
}
chooseHt = getParagraphSpans(spanned, paraStart, paraEnd, LineHeightSpan.class);
if (chooseHt.length != 0) {
if (chooseHtv == null || chooseHtv.length < chooseHt.length) {
chooseHtv = new int[ArrayUtils.idealIntArraySize(chooseHt.length)];
}
for (int i = 0; i < chooseHt.length; i++) {
int o = spanned.getSpanStart(chooseHt[i]);
if (o < paraStart) {
// starts in this layout, before the
// current paragraph
chooseHtv[i] = getLineTop(getLineForOffset(o));
} else {
// starts in this paragraph
chooseHtv[i] = v;
}
}
}
}
measured.setPara(source, paraStart, paraEnd, textDir);
char[] chs = measured.mChars;
float[] widths = measured.mWidths;
byte[] chdirs = measured.mLevels;
int dir = measured.mDir;
boolean easy = measured.mEasy;
int width = firstWidth;
float w = 0;
// here is the offset of the starting character of the line we are currently measuring
int here = paraStart;
// ok is a character offset located after a word separator (space, tab, number...) where
// we would prefer to cut the current line. Equals to here when no such break was found.
int ok = paraStart;
float okWidth = w;
int okAscent = 0, okDescent = 0, okTop = 0, okBottom = 0;
// fit is a character offset such that the [here, fit[ range fits in the allowed width.
// We will cut the line there if no ok position is found.
int fit = paraStart;
float fitWidth = w;
int fitAscent = 0, fitDescent = 0, fitTop = 0, fitBottom = 0;
boolean hasTabOrEmoji = false;
boolean hasTab = false;
TabStops tabStops = null;
for (int spanStart = paraStart, spanEnd; spanStart < paraEnd; spanStart = spanEnd) {
if (spanned == null) {
spanEnd = paraEnd;
int spanLen = spanEnd - spanStart;
measured.addStyleRun(paint, spanLen, fm);
} else {
spanEnd = spanned.nextSpanTransition(spanStart, paraEnd, MetricAffectingSpan.class);
int spanLen = spanEnd - spanStart;
MetricAffectingSpan[] spans = spanned.getSpans(spanStart, spanEnd, MetricAffectingSpan.class);
spans = TextUtils.removeEmptySpans(spans, spanned, MetricAffectingSpan.class);
measured.addStyleRun(paint, spans, spanLen, fm);
}
int fmTop = fm.top;
int fmBottom = fm.bottom;
int fmAscent = fm.ascent;
int fmDescent = fm.descent;
for (int j = spanStart; j < spanEnd; j++) {
char c = chs[j - paraStart];
if (c == CHAR_NEW_LINE) {
// intentionally left empty
} else if (c == CHAR_TAB) {
if (hasTab == false) {
hasTab = true;
hasTabOrEmoji = true;
if (spanned != null) {
// First tab this para, check for tabstops
TabStopSpan[] spans = getParagraphSpans(spanned, paraStart, paraEnd, TabStopSpan.class);
if (spans.length > 0) {
tabStops = new TabStops(TAB_INCREMENT, spans);
}
}
}
if (tabStops != null) {
w = tabStops.nextTab(w);
} else {
w = TabStops.nextDefaultStop(w, TAB_INCREMENT);
}
} else if (c >= CHAR_FIRST_HIGH_SURROGATE && c <= CHAR_LAST_LOW_SURROGATE && j + 1 < spanEnd) {
int emoji = Character.codePointAt(chs, j - paraStart);
if (emoji >= MIN_EMOJI && emoji <= MAX_EMOJI) {
Bitmap bm = EMOJI_FACTORY.getBitmapFromAndroidPua(emoji);
if (bm != null) {
Paint whichPaint;
if (spanned == null) {
whichPaint = paint;
} else {
whichPaint = mWorkPaint;
}
float wid = bm.getWidth() * -whichPaint.ascent() / bm.getHeight();
w += wid;
hasTabOrEmoji = true;
j++;
} else {
w += widths[j - paraStart];
}
} else {
w += widths[j - paraStart];
}
} else {
w += widths[j - paraStart];
}
boolean isSpaceOrTab = c == CHAR_SPACE || c == CHAR_TAB || c == CHAR_ZWSP;
if (w <= width || isSpaceOrTab) {
fitWidth = w;
fit = j + 1;
if (fmTop < fitTop)
fitTop = fmTop;
if (fmAscent < fitAscent)
fitAscent = fmAscent;
if (fmDescent > fitDescent)
fitDescent = fmDescent;
if (fmBottom > fitBottom)
fitBottom = fmBottom;
// From the Unicode Line Breaking Algorithm (at least approximately)
boolean isLineBreak = isSpaceOrTab || // / is class SY and - is class HY, except when followed by a digit
((c == CHAR_SLASH || c == CHAR_HYPHEN) && (j + 1 >= spanEnd || !Character.isDigit(chs[j + 1 - paraStart]))) || // (non-starters), which can be broken after but not before
(c >= CHAR_FIRST_CJK && isIdeographic(c, true) && j + 1 < spanEnd && isIdeographic(chs[j + 1 - paraStart], false));
if (isLineBreak) {
okWidth = w;
ok = j + 1;
if (fitTop < okTop)
okTop = fitTop;
if (fitAscent < okAscent)
okAscent = fitAscent;
if (fitDescent > okDescent)
okDescent = fitDescent;
if (fitBottom > okBottom)
okBottom = fitBottom;
}
} else {
final boolean moreChars = (j + 1 < spanEnd);
int endPos;
int above, below, top, bottom;
float currentTextWidth;
if (ok != here) {
endPos = ok;
above = okAscent;
below = okDescent;
top = okTop;
bottom = okBottom;
currentTextWidth = okWidth;
} else if (fit != here) {
endPos = fit;
above = fitAscent;
below = fitDescent;
top = fitTop;
bottom = fitBottom;
currentTextWidth = fitWidth;
} else {
endPos = here + 1;
above = fm.ascent;
below = fm.descent;
top = fm.top;
bottom = fm.bottom;
currentTextWidth = widths[here - paraStart];
}
v = out(source, here, endPos, above, below, top, bottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, currentTextWidth, paint, moreChars);
here = endPos;
// restart j-span loop from here, compensating for the j++
j = here - 1;
ok = fit = here;
w = 0;
fitAscent = fitDescent = fitTop = fitBottom = 0;
okAscent = okDescent = okTop = okBottom = 0;
if (--firstWidthLineLimit <= 0) {
width = restWidth;
}
if (here < spanStart) {
// The text was cut before the beginning of the current span range.
// Exit the span loop, and get spanStart to start over from here.
measured.setPos(here);
spanEnd = here;
break;
}
if (mLineCount >= mMaximumVisibleLineCount) {
break;
}
}
}
}
if (paraEnd != here && mLineCount < mMaximumVisibleLineCount) {
if ((fitTop | fitBottom | fitDescent | fitAscent) == 0) {
paint.getFontMetricsInt(fm);
fitTop = fm.top;
fitBottom = fm.bottom;
fitAscent = fm.ascent;
fitDescent = fm.descent;
}
// Log.e("text", "output rest " + here + " to " + end);
v = out(source, here, paraEnd, fitAscent, fitDescent, fitTop, fitBottom, v, spacingmult, spacingadd, chooseHt, chooseHtv, fm, hasTabOrEmoji, needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad, chs, widths, paraStart, ellipsize, ellipsizedWidth, w, paint, paraEnd != bufEnd);
}
paraStart = paraEnd;
if (paraEnd == bufEnd)
break;
}
if ((bufEnd == bufStart || source.charAt(bufEnd - 1) == CHAR_NEW_LINE) && mLineCount < mMaximumVisibleLineCount) {
// Log.e("text", "output last " + bufEnd);
paint.getFontMetricsInt(fm);
v = out(source, bufEnd, bufEnd, fm.ascent, fm.descent, fm.top, fm.bottom, v, spacingmult, spacingadd, null, null, fm, false, needMultiply, null, DEFAULT_DIR, true, bufEnd, includepad, trackpad, null, null, bufStart, ellipsize, ellipsizedWidth, 0, paint, false);
}
}
Aggregations