diff options
| author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
|---|---|---|
| committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
| commit | 54b6cfa9a9e5b861a9930af873580d6dc20f773c (patch) | |
| tree | 35051494d2af230dce54d6b31c6af8fc24091316 /core/java/android/text/style | |
| download | frameworks_base-54b6cfa9a9e5b861a9930af873580d6dc20f773c.zip frameworks_base-54b6cfa9a9e5b861a9930af873580d6dc20f773c.tar.gz frameworks_base-54b6cfa9a9e5b861a9930af873580d6dc20f773c.tar.bz2 | |
Initial Contribution
Diffstat (limited to 'core/java/android/text/style')
34 files changed, 1817 insertions, 0 deletions
diff --git a/core/java/android/text/style/AbsoluteSizeSpan.java b/core/java/android/text/style/AbsoluteSizeSpan.java new file mode 100644 index 0000000..8f6ed5a --- /dev/null +++ b/core/java/android/text/style/AbsoluteSizeSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +public class AbsoluteSizeSpan extends MetricAffectingSpan { + + private int mSize; + + public AbsoluteSizeSpan(int size) { + mSize = size; + } + + public int getSize() { + return mSize; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setTextSize(mSize); + } + + @Override + public void updateMeasureState(TextPaint ds) { + ds.setTextSize(mSize); + } +} diff --git a/core/java/android/text/style/AlignmentSpan.java b/core/java/android/text/style/AlignmentSpan.java new file mode 100644 index 0000000..d51edcc --- /dev/null +++ b/core/java/android/text/style/AlignmentSpan.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.Layout; + +public interface AlignmentSpan +extends ParagraphStyle +{ + public Layout.Alignment getAlignment(); + + public static class Standard + implements AlignmentSpan + { + public Standard(Layout.Alignment align) { + mAlignment = align; + } + + public Layout.Alignment getAlignment() { + return mAlignment; + } + + private Layout.Alignment mAlignment; + } +} diff --git a/core/java/android/text/style/BackgroundColorSpan.java b/core/java/android/text/style/BackgroundColorSpan.java new file mode 100644 index 0000000..be6ef77 --- /dev/null +++ b/core/java/android/text/style/BackgroundColorSpan.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.TextPaint; + +public class BackgroundColorSpan extends CharacterStyle { + + private int mColor; + + public BackgroundColorSpan(int color) { + mColor = color; + } + + public int getBackgroundColor() { + return mColor; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.bgColor = mColor; + } +} diff --git a/core/java/android/text/style/BulletSpan.java b/core/java/android/text/style/BulletSpan.java new file mode 100644 index 0000000..70c4d33 --- /dev/null +++ b/core/java/android/text/style/BulletSpan.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Canvas; +import android.graphics.Paint; +import android.text.Layout; +import android.text.Spanned; + +public class BulletSpan implements LeadingMarginSpan { + + public BulletSpan() { + mGapWidth = STANDARD_GAP_WIDTH; + } + + public BulletSpan(int gapWidth) { + mGapWidth = gapWidth; + } + + public BulletSpan(int gapWidth, int color) { + mGapWidth = gapWidth; + mWantColor = true; + mColor = color; + } + + public int getLeadingMargin(boolean first) { + return 2 * BULLET_RADIUS + mGapWidth; + } + + public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, + int top, int baseline, int bottom, + CharSequence text, int start, int end, + boolean first, Layout l) { + if (((Spanned) text).getSpanStart(this) == start) { + Paint.Style style = p.getStyle(); + int oldcolor = 0; + + if (mWantColor) { + oldcolor = p.getColor(); + p.setColor(mColor); + } + + p.setStyle(Paint.Style.FILL); + + c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, + BULLET_RADIUS, p); + + if (mWantColor) { + p.setColor(oldcolor); + } + + p.setStyle(style); + } + } + + private int mGapWidth; + private boolean mWantColor; + private int mColor; + + private static final int BULLET_RADIUS = 3; + public static final int STANDARD_GAP_WIDTH = 2; +} diff --git a/core/java/android/text/style/CharacterStyle.java b/core/java/android/text/style/CharacterStyle.java new file mode 100644 index 0000000..7620d29 --- /dev/null +++ b/core/java/android/text/style/CharacterStyle.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +/** + * The classes that affect character-level text formatting extend this + * class. Most also extend {@link MetricAffectingSpan}. + */ +public abstract class CharacterStyle { + public abstract void updateDrawState(TextPaint tp); + + /** + * A given CharacterStyle can only applied to a single region of a given + * Spanned. If you need to attach the same CharacterStyle to multiple + * regions, you can use this method to wrap it with a new object that + * will have the same effect but be a distinct object so that it can + * also be attached without conflict. + */ + public static CharacterStyle wrap(CharacterStyle cs) { + if (cs instanceof MetricAffectingSpan) { + return new MetricAffectingSpan.Passthrough((MetricAffectingSpan) cs); + } else { + return new Passthrough(cs); + } + } + + /** + * Returns "this" for most CharacterStyles, but for CharacterStyles + * that were generated by {@link #wrap}, returns the underlying + * CharacterStyle. + */ + public CharacterStyle getUnderlying() { + return this; + } + + /** + * A Passthrough CharacterStyle is one that + * passes {@link #updateDrawState} calls through to the + * specified CharacterStyle while still being a distinct object, + * and is therefore able to be attached to the same Spannable + * to which the specified CharacterStyle is already attached. + */ + private static class Passthrough extends CharacterStyle { + private CharacterStyle mStyle; + + /** + * Creates a new Passthrough of the specfied CharacterStyle. + */ + public Passthrough(CharacterStyle cs) { + mStyle = cs; + } + + /** + * Passes updateDrawState through to the underlying CharacterStyle. + */ + @Override + public void updateDrawState(TextPaint tp) { + mStyle.updateDrawState(tp); + } + + /** + * Returns the CharacterStyle underlying this one, or the one + * underlying it if it too is a Passthrough. + */ + @Override + public CharacterStyle getUnderlying() { + return mStyle.getUnderlying(); + } + } +} diff --git a/core/java/android/text/style/ClickableSpan.java b/core/java/android/text/style/ClickableSpan.java new file mode 100644 index 0000000..a232ed7 --- /dev/null +++ b/core/java/android/text/style/ClickableSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.text.style; + +import android.text.TextPaint; +import android.view.View; + +/** + * If an object of this type is attached to the text of a TextView + * with a movement method of LinkMovementMethod, the affected spans of + * text can be selected. If clicked, the {@link #onClick} method will + * be called. + */ +public abstract class ClickableSpan extends CharacterStyle { + + /** + * Performs the click action associated with this span. + */ + public abstract void onClick(View widget); + + /** + * Makes the text underlined and in the link color. + */ + @Override + public void updateDrawState(TextPaint ds) { + ds.setColor(ds.linkColor); + ds.setUnderlineText(true); + } +} diff --git a/core/java/android/text/style/DrawableMarginSpan.java b/core/java/android/text/style/DrawableMarginSpan.java new file mode 100644 index 0000000..3c471a5 --- /dev/null +++ b/core/java/android/text/style/DrawableMarginSpan.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.drawable.Drawable; +import android.graphics.Paint; +import android.graphics.Canvas; +import android.graphics.RectF; +import android.text.Spanned; +import android.text.Layout; + +public class DrawableMarginSpan +implements LeadingMarginSpan, LineHeightSpan +{ + public DrawableMarginSpan(Drawable b) { + mDrawable = b; + } + + public DrawableMarginSpan(Drawable b, int pad) { + mDrawable = b; + mPad = pad; + } + + public int getLeadingMargin(boolean first) { + return mDrawable.getIntrinsicWidth() + mPad; + } + + public void 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) { + int st = ((Spanned) text).getSpanStart(this); + int ix = (int)x; + int itop = (int)layout.getLineTop(layout.getLineForOffset(st)); + + int dw = mDrawable.getIntrinsicWidth(); + int dh = mDrawable.getIntrinsicHeight(); + + if (dir < 0) + x -= dw; + + // XXX What to do about Paint? + mDrawable.setBounds(ix, itop, ix+dw, itop+dh); + mDrawable.draw(c); + } + + public void chooseHeight(CharSequence text, int start, int end, + int istartv, int v, + Paint.FontMetricsInt fm) { + if (end == ((Spanned) text).getSpanEnd(this)) { + int ht = mDrawable.getIntrinsicHeight(); + + int need = ht - (v + fm.descent - fm.ascent - istartv); + if (need > 0) + fm.descent += need; + + need = ht - (v + fm.bottom - fm.top - istartv); + if (need > 0) + fm.bottom += need; + } + } + + private Drawable mDrawable; + private int mPad; +} diff --git a/core/java/android/text/style/DynamicDrawableSpan.java b/core/java/android/text/style/DynamicDrawableSpan.java new file mode 100644 index 0000000..3bcc335 --- /dev/null +++ b/core/java/android/text/style/DynamicDrawableSpan.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import java.lang.ref.WeakReference; + +import android.graphics.drawable.Drawable; +import android.graphics.Canvas; +import android.graphics.Paint; + +/** + * + */ +public abstract class DynamicDrawableSpan +extends ReplacementSpan +{ + /** + * Your subclass must implement this method to provide the bitmap + * to be drawn. The dimensions of the bitmap must be the same + * from each call to the next. + */ + public abstract Drawable getDrawable(); + + public int getSize(Paint paint, CharSequence text, + int start, int end, + Paint.FontMetricsInt fm) { + Drawable b = getCachedDrawable(); + + if (fm != null) { + fm.ascent = -b.getIntrinsicHeight(); + fm.descent = 0; + + fm.top = fm.ascent; + fm.bottom = 0; + } + + return b.getIntrinsicWidth(); + } + + public void draw(Canvas canvas, CharSequence text, + int start, int end, float x, + int top, int y, int bottom, Paint paint) { + Drawable b = getCachedDrawable(); + canvas.save(); + + canvas.translate(x, bottom-b.getIntrinsicHeight());; + b.draw(canvas); + canvas.restore(); + } + + private Drawable getCachedDrawable() { + WeakReference wr = mDrawableRef; + Drawable b = null; + + if (wr != null) + b = (Drawable) wr.get(); + + if (b == null) { + b = getDrawable(); + mDrawableRef = new WeakReference(b); + } + + return b; + } + + private WeakReference mDrawableRef; +} + diff --git a/core/java/android/text/style/ForegroundColorSpan.java b/core/java/android/text/style/ForegroundColorSpan.java new file mode 100644 index 0000000..5cccd9c --- /dev/null +++ b/core/java/android/text/style/ForegroundColorSpan.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +public class ForegroundColorSpan extends CharacterStyle { + + private int mColor; + + public ForegroundColorSpan(int color) { + mColor = color; + } + + public int getForegroundColor() { + return mColor; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setColor(mColor); + } +} diff --git a/core/java/android/text/style/IconMarginSpan.java b/core/java/android/text/style/IconMarginSpan.java new file mode 100644 index 0000000..c786a17 --- /dev/null +++ b/core/java/android/text/style/IconMarginSpan.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.RectF; +import android.text.Spanned; +import android.text.Layout; + +public class IconMarginSpan +implements LeadingMarginSpan, LineHeightSpan +{ + public IconMarginSpan(Bitmap b) { + mBitmap = b; + } + + public IconMarginSpan(Bitmap b, int pad) { + mBitmap = b; + mPad = pad; + } + + public int getLeadingMargin(boolean first) { + return mBitmap.getWidth() + mPad; + } + + public void 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) { + int st = ((Spanned) text).getSpanStart(this); + int itop = layout.getLineTop(layout.getLineForOffset(st)); + + if (dir < 0) + x -= mBitmap.getWidth(); + + c.drawBitmap(mBitmap, x, itop, p); + } + + public void chooseHeight(CharSequence text, int start, int end, + int istartv, int v, + Paint.FontMetricsInt fm) { + if (end == ((Spanned) text).getSpanEnd(this)) { + int ht = mBitmap.getHeight(); + + int need = ht - (v + fm.descent - fm.ascent - istartv); + if (need > 0) + fm.descent += need; + + need = ht - (v + fm.bottom - fm.top - istartv); + if (need > 0) + fm.bottom += need; + } + } + + private Bitmap mBitmap; + private int mPad; +} diff --git a/core/java/android/text/style/ImageSpan.java b/core/java/android/text/style/ImageSpan.java new file mode 100644 index 0000000..de067b1 --- /dev/null +++ b/core/java/android/text/style/ImageSpan.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.util.Log; + +import java.io.InputStream; + +public class ImageSpan extends DynamicDrawableSpan { + private Drawable mDrawable; + private Uri mContentUri; + private int mResourceId; + private Context mContext; + private String mSource; + + + public ImageSpan(Bitmap b) { + mDrawable = new BitmapDrawable(b); + mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), + mDrawable.getIntrinsicHeight()); + } + + public ImageSpan(Drawable d) { + mDrawable = d; + } + + public ImageSpan(Drawable d, String source) { + mDrawable = d; + mSource = source; + } + + public ImageSpan(Context context, Uri uri) { + mContext = context; + mContentUri = uri; + } + + public ImageSpan(Context context, int resourceId) { + mContext = context; + mResourceId = resourceId; + } + + @Override + public Drawable getDrawable() { + Drawable drawable = null; + + if (mDrawable != null) { + drawable = mDrawable; + } else if (mContentUri != null) { + Bitmap bitmap = null; + try { + InputStream is = mContext.getContentResolver().openInputStream( + mContentUri); + bitmap = BitmapFactory.decodeStream(is); + drawable = new BitmapDrawable(bitmap); + is.close(); + } catch (Exception e) { + Log.e("sms", "Failed to loaded content " + mContentUri, e); + } + } else { + try { + drawable = mContext.getResources().getDrawable(mResourceId); + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), + drawable.getIntrinsicHeight()); + } catch (Exception e) { + Log.e("sms", "Unable to find resource: " + mResourceId); + } + } + + return drawable; + } + + /** + * Returns the source string that was saved during construction. + */ + public String getSource() { + return mSource; + } + +} diff --git a/core/java/android/text/style/LeadingMarginSpan.java b/core/java/android/text/style/LeadingMarginSpan.java new file mode 100644 index 0000000..85a27dc --- /dev/null +++ b/core/java/android/text/style/LeadingMarginSpan.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Canvas; +import android.text.Layout; + +public interface LeadingMarginSpan +extends ParagraphStyle +{ + public int getLeadingMargin(boolean first); + public void 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); + + public static class Standard + implements LeadingMarginSpan + { + public Standard(int first, int rest) { + mFirst = first; + mRest = rest; + } + + public Standard(int every) { + this(every, every); + } + + public int getLeadingMargin(boolean first) { + return first ? mFirst : mRest; + } + + public void 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) { + ; + } + + private int mFirst, mRest; + } +} diff --git a/core/java/android/text/style/LineBackgroundSpan.java b/core/java/android/text/style/LineBackgroundSpan.java new file mode 100644 index 0000000..854aeaf --- /dev/null +++ b/core/java/android/text/style/LineBackgroundSpan.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Canvas; + +public interface LineBackgroundSpan +extends ParagraphStyle +{ + public void drawBackground(Canvas c, Paint p, + int left, int right, + int top, int baseline, int bottom, + CharSequence text, int start, int end, + int lnum); +} diff --git a/core/java/android/text/style/LineHeightSpan.java b/core/java/android/text/style/LineHeightSpan.java new file mode 100644 index 0000000..c0ef97c --- /dev/null +++ b/core/java/android/text/style/LineHeightSpan.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Canvas; +import android.text.Layout; + +public interface LineHeightSpan +extends ParagraphStyle, WrapTogetherSpan +{ + public void chooseHeight(CharSequence text, int start, int end, + int spanstartv, int v, + Paint.FontMetricsInt fm); +} diff --git a/core/java/android/text/style/MaskFilterSpan.java b/core/java/android/text/style/MaskFilterSpan.java new file mode 100644 index 0000000..781bcec --- /dev/null +++ b/core/java/android/text/style/MaskFilterSpan.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.MaskFilter; +import android.text.TextPaint; + +public class MaskFilterSpan extends CharacterStyle { + + private MaskFilter mFilter; + + public MaskFilterSpan(MaskFilter filter) { + mFilter = filter; + } + + public MaskFilter getMaskFilter() { + return mFilter; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setMaskFilter(mFilter); + } +} diff --git a/core/java/android/text/style/MetricAffectingSpan.java b/core/java/android/text/style/MetricAffectingSpan.java new file mode 100644 index 0000000..92558eb --- /dev/null +++ b/core/java/android/text/style/MetricAffectingSpan.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +/** + * The classes that affect character-level text formatting in a way that + * changes the width or height of characters extend this class. + */ +public abstract class MetricAffectingSpan +extends CharacterStyle +implements UpdateLayout { + + public abstract void updateMeasureState(TextPaint p); + + /** + * Returns "this" for most MetricAffectingSpans, but for + * MetricAffectingSpans that were generated by {@link #wrap}, + * returns the underlying MetricAffectingSpan. + */ + @Override + public MetricAffectingSpan getUnderlying() { + return this; + } + + /** + * A Passthrough MetricAffectingSpan is one that + * passes {@link #updateDrawState} and {@link #updateMeasureState} + * calls through to the specified MetricAffectingSpan + * while still being a distinct object, + * and is therefore able to be attached to the same Spannable + * to which the specified MetricAffectingSpan is already attached. + */ + /* package */ static class Passthrough extends MetricAffectingSpan { + private MetricAffectingSpan mStyle; + + /** + * Creates a new Passthrough of the specfied MetricAffectingSpan. + */ + public Passthrough(MetricAffectingSpan cs) { + mStyle = cs; + } + + /** + * Passes updateDrawState through to the underlying MetricAffectingSpan. + */ + @Override + public void updateDrawState(TextPaint tp) { + mStyle.updateDrawState(tp); + } + + /** + * Passes updateMeasureState through to the underlying MetricAffectingSpan. + */ + @Override + public void updateMeasureState(TextPaint tp) { + mStyle.updateMeasureState(tp); + } + + /** + * Returns the MetricAffectingSpan underlying this one, or the one + * underlying it if it too is a Passthrough. + */ + @Override + public MetricAffectingSpan getUnderlying() { + return mStyle.getUnderlying(); + } + } +} diff --git a/core/java/android/text/style/ParagraphStyle.java b/core/java/android/text/style/ParagraphStyle.java new file mode 100644 index 0000000..423156e --- /dev/null +++ b/core/java/android/text/style/ParagraphStyle.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +/** + * The classes that affect paragraph-level text formatting implement + * this interface. + */ +public interface ParagraphStyle +{ + +} diff --git a/core/java/android/text/style/QuoteSpan.java b/core/java/android/text/style/QuoteSpan.java new file mode 100644 index 0000000..3f4a32f --- /dev/null +++ b/core/java/android/text/style/QuoteSpan.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Canvas; +import android.graphics.RectF; +import android.text.Layout; + +public class QuoteSpan +implements LeadingMarginSpan +{ + private static final int STRIPE_WIDTH = 2; + private static final int GAP_WIDTH = 2; + + private int mColor = 0xff0000ff; + + public QuoteSpan() { + super(); + } + + public QuoteSpan(int color) { + this(); + mColor = color; + } + + public int getColor() { + return mColor; + } + + public int getLeadingMargin(boolean first) { + return STRIPE_WIDTH + GAP_WIDTH; + } + + public void 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) { + Paint.Style style = p.getStyle(); + int color = p.getColor(); + + p.setStyle(Paint.Style.FILL); + p.setColor(mColor); + + c.drawRect(x, top, x + dir * STRIPE_WIDTH, bottom, p); + + p.setStyle(style); + p.setColor(color); + } +} diff --git a/core/java/android/text/style/RasterizerSpan.java b/core/java/android/text/style/RasterizerSpan.java new file mode 100644 index 0000000..193c700 --- /dev/null +++ b/core/java/android/text/style/RasterizerSpan.java @@ -0,0 +1,39 @@ +/* + * 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Rasterizer; +import android.text.TextPaint; + +public class RasterizerSpan extends CharacterStyle { + + private Rasterizer mRasterizer; + + public RasterizerSpan(Rasterizer r) { + mRasterizer = r; + } + + public Rasterizer getRasterizer() { + return mRasterizer; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setRasterizer(mRasterizer); + } +} diff --git a/core/java/android/text/style/RelativeSizeSpan.java b/core/java/android/text/style/RelativeSizeSpan.java new file mode 100644 index 0000000..a8ad076 --- /dev/null +++ b/core/java/android/text/style/RelativeSizeSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +public class RelativeSizeSpan extends MetricAffectingSpan { + + private float mProportion; + + public RelativeSizeSpan(float proportion) { + mProportion = proportion; + } + + public float getSizeChange() { + return mProportion; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setTextSize(ds.getTextSize() * mProportion); + } + + @Override + public void updateMeasureState(TextPaint ds) { + ds.setTextSize(ds.getTextSize() * mProportion); + } +} diff --git a/core/java/android/text/style/ReplacementSpan.java b/core/java/android/text/style/ReplacementSpan.java new file mode 100644 index 0000000..26c725f --- /dev/null +++ b/core/java/android/text/style/ReplacementSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Canvas; +import android.text.TextPaint; + +public abstract class ReplacementSpan extends MetricAffectingSpan { + + public abstract int getSize(Paint paint, CharSequence text, + int start, int end, + Paint.FontMetricsInt fm); + public abstract void draw(Canvas canvas, CharSequence text, + int start, int end, float x, + int top, int y, int bottom, Paint paint); + + /** + * This method does nothing, since ReplacementSpans are measured + * explicitly instead of affecting Paint properties. + */ + public void updateMeasureState(TextPaint p) { } + + /** + * This method does nothing, since ReplacementSpans are drawn + * explicitly instead of affecting Paint properties. + */ + public void updateDrawState(TextPaint ds) { } +} diff --git a/core/java/android/text/style/ScaleXSpan.java b/core/java/android/text/style/ScaleXSpan.java new file mode 100644 index 0000000..ac9e35d --- /dev/null +++ b/core/java/android/text/style/ScaleXSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.text.TextPaint; + +public class ScaleXSpan extends MetricAffectingSpan { + + private float mProportion; + + public ScaleXSpan(float proportion) { + mProportion = proportion; + } + + public float getScaleX() { + return mProportion; + } + + @Override + public void updateDrawState(TextPaint ds) { + ds.setTextScaleX(ds.getTextScaleX() * mProportion); + } + + @Override + public void updateMeasureState(TextPaint ds) { + ds.setTextScaleX(ds.getTextScaleX() * mProportion); + } +} diff --git a/core/java/android/text/style/StrikethroughSpan.java b/core/java/android/text/style/StrikethroughSpan.java new file mode 100644 index 0000000..01ae38c --- /dev/null +++ b/core/java/android/text/style/StrikethroughSpan.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.TextPaint; + +public class StrikethroughSpan extends CharacterStyle { + + @Override + public void updateDrawState(TextPaint ds) { + ds.setStrikeThruText(true); + } +} diff --git a/core/java/android/text/style/StyleSpan.java b/core/java/android/text/style/StyleSpan.java new file mode 100644 index 0000000..cc8b06c --- /dev/null +++ b/core/java/android/text/style/StyleSpan.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; + +/** + * + * Describes a style in a span. + * Note that styles are cumulative -- if both bold and italic are set in + * separate spans, or if the base style is bold and a span calls for italic, + * you get bold italic. You can't turn off a style from the base style. + * + */ +public class StyleSpan extends MetricAffectingSpan { + + private int mStyle; + + /** + * + * @param style An integer constant describing the style for this span. Examples + * include bold, italic, and normal. Values are constants defined + * in {@link android.graphics.Typeface}. + */ + public StyleSpan(int style) { + mStyle = style; + } + + /** + * Returns the style constant defined in {@link android.graphics.Typeface}. + */ + public int getStyle() { + return mStyle; + } + + @Override + public void updateDrawState(TextPaint ds) { + apply(ds, mStyle); + } + + @Override + public void updateMeasureState(TextPaint paint) { + apply(paint, mStyle); + } + + private static void apply(Paint paint, int style) { + int oldStyle; + + Typeface old = paint.getTypeface(); + if (old == null) { + oldStyle = 0; + } else { + oldStyle = old.getStyle(); + } + + int want = oldStyle | style; + + Typeface tf; + if (old == null) { + tf = Typeface.defaultFromStyle(want); + } else { + tf = Typeface.create(old, want); + } + + int fake = want & ~tf.getStyle(); + + if ((fake & Typeface.BOLD) != 0) { + paint.setFakeBoldText(true); + } + + if ((fake & Typeface.ITALIC) != 0) { + paint.setTextSkewX(-0.25f); + } + + paint.setTypeface(tf); + } +} diff --git a/core/java/android/text/style/SubscriptSpan.java b/core/java/android/text/style/SubscriptSpan.java new file mode 100644 index 0000000..78d6ba9 --- /dev/null +++ b/core/java/android/text/style/SubscriptSpan.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.TextPaint; + +public class SubscriptSpan extends MetricAffectingSpan { + @Override + public void updateDrawState(TextPaint tp) { + tp.baselineShift -= (int) (tp.ascent() / 2); + } + + @Override + public void updateMeasureState(TextPaint tp) { + tp.baselineShift -= (int) (tp.ascent() / 2); + } +} diff --git a/core/java/android/text/style/SuperscriptSpan.java b/core/java/android/text/style/SuperscriptSpan.java new file mode 100644 index 0000000..79be4de --- /dev/null +++ b/core/java/android/text/style/SuperscriptSpan.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.TextPaint; + +public class SuperscriptSpan extends MetricAffectingSpan { + @Override + public void updateDrawState(TextPaint tp) { + tp.baselineShift += (int) (tp.ascent() / 2); + } + + @Override + public void updateMeasureState(TextPaint tp) { + tp.baselineShift += (int) (tp.ascent() / 2); + } +} diff --git a/core/java/android/text/style/TabStopSpan.java b/core/java/android/text/style/TabStopSpan.java new file mode 100644 index 0000000..e5b7644 --- /dev/null +++ b/core/java/android/text/style/TabStopSpan.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +public interface TabStopSpan +extends ParagraphStyle +{ + public int getTabStop(); + + public static class Standard + implements TabStopSpan + { + public Standard(int where) { + mTab = where; + } + + public int getTabStop() { + return mTab; + } + + private int mTab; + } +} diff --git a/core/java/android/text/style/TextAppearanceSpan.java b/core/java/android/text/style/TextAppearanceSpan.java new file mode 100644 index 0000000..c4ec976 --- /dev/null +++ b/core/java/android/text/style/TextAppearanceSpan.java @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2008 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. + */ + +package android.text.style; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.content.res.TypedArray; +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; + +/** + * Sets the text color, size, style, and typeface to match a TextAppearance + * resource. + */ +public class TextAppearanceSpan extends MetricAffectingSpan { + private String mTypeface; + private int mStyle; + private int mTextSize; + private ColorStateList mTextColor; + private ColorStateList mTextColorLink; + + /** + * Uses the specified TextAppearance resource to determine the + * text appearance. The <code>appearance</code> should be, for example, + * <code>android.R.style.TextAppearance_Small</code>. + */ + public TextAppearanceSpan(Context context, int appearance) { + this(context, appearance, -1); + } + + /** + * Uses the specified TextAppearance resource to determine the + * text appearance, and the specified text color resource + * to determine the color. The <code>appearance</code> should be, + * for example, <code>android.R.style.TextAppearance_Small</code>, + * and the <code>colorList</code> should be, for example, + * <code>android.R.styleable.Theme_textColorDim</code>. + */ + public TextAppearanceSpan(Context context, int appearance, + int colorList) { + TypedArray a = + context.obtainStyledAttributes(appearance, + com.android.internal.R.styleable.TextAppearance); + + mTextColor = a.getColorStateList(com.android.internal.R.styleable. + TextAppearance_textColor); + mTextColorLink = a.getColorStateList(com.android.internal.R.styleable. + TextAppearance_textColorLink); + mTextSize = a.getDimensionPixelSize(com.android.internal.R.styleable. + TextAppearance_textSize, -1); + + mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0); + int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0); + + switch (tf) { + case 1: + mTypeface = "sans"; + break; + + case 2: + mTypeface = "serif"; + break; + + case 3: + mTypeface = "monospace"; + break; + } + + a.recycle(); + + if (colorList >= 0) { + a = context.obtainStyledAttributes(com.android.internal.R.style.Theme, + com.android.internal.R.styleable.Theme); + + mTextColor = a.getColorStateList(colorList); + a.recycle(); + } + } + + /** + * Makes text be drawn with the specified typeface, size, style, + * and colors. + */ + public TextAppearanceSpan(String family, int style, int size, + ColorStateList color, ColorStateList linkColor) { + mTypeface = family; + mStyle = style; + mTextSize = size; + mTextColor = color; + mTextColorLink = linkColor; + } + + /** + * Returns the typeface family specified by this span, or <code>null</code> + * if it does not specify one. + */ + public String getFamily() { + return mTypeface; + } + + /** + * Returns the text color specified by this span, or <code>null</code> + * if it does not specify one. + */ + public ColorStateList getTextColor() { + return mTextColor; + } + + /** + * Returns the link color specified by this span, or <code>null</code> + * if it does not specify one. + */ + public ColorStateList getLinkTextColor() { + return mTextColorLink; + } + + /** + * Returns the text size specified by this span, or <code>-1</code> + * if it does not specify one. + */ + public int getTextSize() { + return mTextSize; + } + + /** + * Returns the text style specified by this span, or <code>0</code> + * if it does not specify one. + */ + public int getTextStyle() { + return mStyle; + } + + @Override + public void updateDrawState(TextPaint ds) { + updateMeasureState(ds); + + if (mTextColor != null) { + ds.setColor(mTextColor.getColorForState(ds.drawableState, 0)); + } + + if (mTextColorLink != null) { + ds.linkColor = mTextColor.getColorForState(ds.drawableState, 0); + } + } + + @Override + public void updateMeasureState(TextPaint ds) { + if (mTypeface != null || mStyle != 0) { + Typeface tf = ds.getTypeface(); + int style = 0; + + if (tf != null) { + style = tf.getStyle(); + } + + style |= mStyle; + + if (mTypeface != null) { + tf = Typeface.create(mTypeface, style); + } else if (tf == null) { + tf = Typeface.defaultFromStyle(style); + } else { + tf = Typeface.create(tf, style); + } + + int fake = style & ~tf.getStyle(); + + if ((fake & Typeface.BOLD) != 0) { + ds.setFakeBoldText(true); + } + + if ((fake & Typeface.ITALIC) != 0) { + ds.setTextSkewX(-0.25f); + } + + ds.setTypeface(tf); + } + + if (mTextSize > 0) { + ds.setTextSize(mTextSize); + } + } +} diff --git a/core/java/android/text/style/TypefaceSpan.java b/core/java/android/text/style/TypefaceSpan.java new file mode 100644 index 0000000..7519ac2 --- /dev/null +++ b/core/java/android/text/style/TypefaceSpan.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; + +/** + * Changes the typeface family of the text to which the span is attached. + */ +public class TypefaceSpan extends MetricAffectingSpan { + private String mFamily; + + /** + * @param family The font family for this typeface. Examples include + * "monospace", "serif", and "sans-serif". + */ + public TypefaceSpan(String family) { + mFamily = family; + } + + /** + * Returns the font family name. + */ + public String getFamily() { + return mFamily; + } + + @Override + public void updateDrawState(TextPaint ds) { + apply(ds, mFamily); + } + + @Override + public void updateMeasureState(TextPaint paint) { + apply(paint, mFamily); + } + + private static void apply(Paint paint, String family) { + int oldStyle; + + Typeface old = paint.getTypeface(); + if (old == null) { + oldStyle = 0; + } else { + oldStyle = old.getStyle(); + } + + Typeface tf = Typeface.create(family, oldStyle); + int fake = oldStyle & ~tf.getStyle(); + + if ((fake & Typeface.BOLD) != 0) { + paint.setFakeBoldText(true); + } + + if ((fake & Typeface.ITALIC) != 0) { + paint.setTextSkewX(-0.25f); + } + + paint.setTypeface(tf); + } +} diff --git a/core/java/android/text/style/URLSpan.java b/core/java/android/text/style/URLSpan.java new file mode 100644 index 0000000..79809b5 --- /dev/null +++ b/core/java/android/text/style/URLSpan.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.content.Intent; +import android.net.Uri; +import android.text.TextPaint; +import android.view.View; + +public class URLSpan extends ClickableSpan { + + private String mURL; + + public URLSpan(String url) { + mURL = url; + } + + public String getURL() { + return mURL; + } + + @Override + public void onClick(View widget) { + Uri uri = Uri.parse(getURL()); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + intent.addCategory(Intent.CATEGORY_BROWSABLE); + widget.getContext().startActivity(intent); + } +} diff --git a/core/java/android/text/style/UnderlineSpan.java b/core/java/android/text/style/UnderlineSpan.java new file mode 100644 index 0000000..5dce0f2 --- /dev/null +++ b/core/java/android/text/style/UnderlineSpan.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +import android.text.TextPaint; + +public class UnderlineSpan extends CharacterStyle { + + @Override + public void updateDrawState(TextPaint ds) { + ds.setUnderlineText(true); + } +} diff --git a/core/java/android/text/style/UpdateLayout.java b/core/java/android/text/style/UpdateLayout.java new file mode 100644 index 0000000..211685a --- /dev/null +++ b/core/java/android/text/style/UpdateLayout.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +/** + * The classes that affect character-level text formatting in a way that + * triggers a text layout update when one is added or remove must implement + * this interface. + */ +public interface UpdateLayout { } diff --git a/core/java/android/text/style/WrapTogetherSpan.java b/core/java/android/text/style/WrapTogetherSpan.java new file mode 100644 index 0000000..11721a8 --- /dev/null +++ b/core/java/android/text/style/WrapTogetherSpan.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2006 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. + */ + +package android.text.style; + +public interface WrapTogetherSpan +extends ParagraphStyle +{ + +} diff --git a/core/java/android/text/style/package.html b/core/java/android/text/style/package.html new file mode 100644 index 0000000..0a8520c --- /dev/null +++ b/core/java/android/text/style/package.html @@ -0,0 +1,10 @@ +<html> +<body> + +<p>Provides classes used to view or change the style of a span of text in a View object. +The classes with a subclass Standard are passed in to {@link android.text.SpannableString#setSpan(java.lang.Object, int, int, int) +SpannableString.setSpan()} or {@link android.text.SpannableStringBuilder#setSpan(java.lang.Object, int, int, int) +SpannableStringBuilder.setSpan()} to add a new styled span to a string in a View object. + +</body> +</html> |
