package eu.faircode.email;
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static android.text.Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.Spanned;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.BulletSpan;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.ParagraphStyle;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.StringBuilderPrinter;
import java.util.ArrayList;
import java.util.List;
public class HtmlEx {
private Context context;
private static final int TO_HTML_PARAGRAPH_FLAG = 0x00000001;
public HtmlEx(Context context){
this.context = context;
}
/**
* @deprecated use {@link #toHtml(Spanned, int)} instead.
*/
@Deprecated
public /* static */ String toHtml(Spanned text) {
return toHtml(text, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);
}
/**
* Returns an HTML representation of the provided Spanned text. A best effort is
* made to add HTML tags corresponding to spans. Also note that HTML metacharacters
* (such as "<" and "&") within the input text are escaped.
*
* @param text input text to convert
* @param option one of {@link #TO_HTML_PARAGRAPH_LINES_CONSECUTIVE} or
* {@link #TO_HTML_PARAGRAPH_LINES_INDIVIDUAL}
* @return string containing input converted to HTML
*/
public /* static */ String toHtml(Spanned text, int option) {
StringBuilder out = new StringBuilder();
withinHtml(out, text, option);
return out.toString();
}
/**
* Returns an HTML escaped representation of the given plain text.
*/
public /* static */ String escapeHtml(CharSequence text) {
StringBuilder out = new StringBuilder();
withinStyle(out, text, 0, text.length());
return out.toString();
}
private /* static */ void withinHtml(StringBuilder out, Spanned text, int option) {
if ((option & TO_HTML_PARAGRAPH_FLAG) == TO_HTML_PARAGRAPH_LINES_CONSECUTIVE) {
encodeTextAlignmentByDiv(out, text, option);
return;
}
withinDiv(out, text, 0, text.length(), option);
}
private /* static */ void encodeTextAlignmentByDiv(StringBuilder out, Spanned text, int option) {
int len = text.length();
int next;
for (int i = 0; i < len; i = next) {
next = text.nextSpanTransition(i, len, ParagraphStyle.class);
ParagraphStyle[] style = getSpans(text, i, next, ParagraphStyle.class);
String elements = " ";
boolean needDiv = false;
for(int j = 0; j < style.length; j++) {
if (style[j] instanceof AlignmentSpan) {
Layout.Alignment align =
((AlignmentSpan) style[j]).getAlignment();
needDiv = true;
if (align == Layout.Alignment.ALIGN_CENTER) {
elements = "align=\"center\" " + elements;
} else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
elements = "align=\"right\" " + elements;
} else {
elements = "align=\"left\" " + elements;
}
}
}
if (needDiv) {
out.append("
");
}
withinDiv(out, text, i, next, option);
if (needDiv) {
out.append("
");
}
}
}
private /* static */ void withinDiv(StringBuilder out, Spanned text, int start, int end,
int option) {
int next;
for (int i = start; i < end; i = next) {
int n1 = text.nextSpanTransition(i, end, QuoteSpan.class);
int n2 = text.nextSpanTransition(i, end, eu.faircode.email.IndentSpan.class);
next = Math.min(n1, n2);
if (next > end) {
StringBuilder sb = new StringBuilder();
TextUtils.dumpSpans(text, new StringBuilderPrinter(sb), "withinDiv ");
sb.append(" next=").append(next);
sb.append(" start=").append(start);
sb.append(" end=").append(end);
eu.faircode.email.Log.e(sb.toString());
next = end;
}
List