The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 19 | import android.annotation.IntRange; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Px; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.graphics.Paint; |
Haoyu Zhang | 63a5efb | 2018-11-26 15:36:29 -0800 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.text.ParcelableSpan; |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 25 | import android.text.TextPaint; |
Haoyu Zhang | 63a5efb | 2018-11-26 15:36:29 -0800 | [diff] [blame] | 26 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 28 | import com.android.internal.util.Preconditions; |
| 29 | |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 30 | /** |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 31 | * The classes that affect the line height of paragraph should implement this interface. |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 32 | */ |
| 33 | public interface LineHeightSpan extends ParagraphStyle, WrapTogetherSpan { |
| 34 | /** |
| 35 | * Classes that implement this should define how the height is being calculated. |
| 36 | * |
| 37 | * @param text the text |
| 38 | * @param start the start of the line |
| 39 | * @param end the end of the line |
| 40 | * @param spanstartv the start of the span |
| 41 | * @param lineHeight the line height |
| 42 | * @param fm font metrics of the paint, in integers |
| 43 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | public void chooseHeight(CharSequence text, int start, int end, |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 45 | int spanstartv, int lineHeight, |
| 46 | Paint.FontMetricsInt fm); |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 47 | |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 48 | /** |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 49 | * The classes that affect the line height of paragraph with respect to density, |
| 50 | * should implement this interface. |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 51 | */ |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 52 | public interface WithDensity extends LineHeightSpan { |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Classes that implement this should define how the height is being calculated. |
| 56 | * |
| 57 | * @param text the text |
| 58 | * @param start the start of the line |
| 59 | * @param end the end of the line |
| 60 | * @param spanstartv the start of the span |
| 61 | * @param lineHeight the line height |
| 62 | * @param paint the paint |
| 63 | */ |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 64 | public void chooseHeight(CharSequence text, int start, int end, |
Florina Muntenescu | 139e1b5 | 2018-01-24 15:52:00 +0000 | [diff] [blame] | 65 | int spanstartv, int lineHeight, |
| 66 | Paint.FontMetricsInt fm, TextPaint paint); |
Eric Fischer | a9f1dd0 | 2009-08-12 15:00:10 -0700 | [diff] [blame] | 67 | } |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Default implementation of the {@link LineHeightSpan}, which changes the line height of the |
| 71 | * attached paragraph. |
| 72 | * <p> |
| 73 | * LineHeightSpan will change the line height of the entire paragraph, even though it |
| 74 | * covers only part of the paragraph. |
| 75 | * </p> |
| 76 | */ |
Haoyu Zhang | 63a5efb | 2018-11-26 15:36:29 -0800 | [diff] [blame] | 77 | class Standard implements LineHeightSpan, ParcelableSpan { |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 78 | |
| 79 | private final @Px int mHeight; |
| 80 | /** |
| 81 | * Set the line height of the paragraph to <code>height</code> physical pixels. |
| 82 | */ |
| 83 | public Standard(@Px @IntRange(from = 1) int height) { |
| 84 | Preconditions.checkArgument(height > 0, "Height:" + height + "must be positive"); |
| 85 | mHeight = height; |
| 86 | } |
| 87 | |
Haoyu Zhang | 63a5efb | 2018-11-26 15:36:29 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Constructor called from {@link TextUtils} to restore the span from a parcel |
| 90 | */ |
Haoyu Zhang | 7c89460 | 2019-03-04 13:23:13 -0800 | [diff] [blame] | 91 | public Standard(@NonNull Parcel src) { |
Haoyu Zhang | 63a5efb | 2018-11-26 15:36:29 -0800 | [diff] [blame] | 92 | mHeight = src.readInt(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the line height specified by this span. |
| 97 | */ |
| 98 | @Px |
| 99 | public int getHeight() { |
| 100 | return mHeight; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int getSpanTypeId() { |
| 105 | return getSpanTypeIdInternal(); |
| 106 | } |
| 107 | |
| 108 | /** @hide */ |
| 109 | @Override |
| 110 | public int getSpanTypeIdInternal() { |
| 111 | return TextUtils.LINE_HEIGHT_SPAN; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int describeContents() { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void writeToParcel(Parcel dest, int flags) { |
| 121 | writeToParcelInternal(dest, flags); |
| 122 | } |
| 123 | |
| 124 | /** @hide */ |
| 125 | @Override |
| 126 | public void writeToParcelInternal(@NonNull Parcel dest, int flags) { |
| 127 | dest.writeInt(mHeight); |
| 128 | } |
| 129 | |
Haoyu Zhang | 444be8b | 2018-08-23 17:44:18 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public void chooseHeight(@NonNull CharSequence text, int start, int end, |
| 132 | int spanstartv, int lineHeight, |
| 133 | @NonNull Paint.FontMetricsInt fm) { |
| 134 | final int originHeight = fm.descent - fm.ascent; |
| 135 | // If original height is not positive, do nothing. |
| 136 | if (originHeight <= 0) { |
| 137 | return; |
| 138 | } |
| 139 | final float ratio = mHeight * 1.0f / originHeight; |
| 140 | fm.descent = Math.round(fm.descent * ratio); |
| 141 | fm.ascent = fm.descent - mHeight; |
| 142 | } |
| 143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |