blob: 7fb0f950f583b1340e29e293c8e5971bb66ef682 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.text.style;
18
Haoyu Zhang444be8b2018-08-23 17:44:18 -070019import android.annotation.IntRange;
20import android.annotation.NonNull;
21import android.annotation.Px;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.graphics.Paint;
Haoyu Zhang63a5efb2018-11-26 15:36:29 -080023import android.os.Parcel;
24import android.text.ParcelableSpan;
Eric Fischera9f1dd02009-08-12 15:00:10 -070025import android.text.TextPaint;
Haoyu Zhang63a5efb2018-11-26 15:36:29 -080026import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Haoyu Zhang444be8b2018-08-23 17:44:18 -070028import com.android.internal.util.Preconditions;
29
Florina Muntenescu139e1b52018-01-24 15:52:00 +000030/**
Haoyu Zhang444be8b2018-08-23 17:44:18 -070031 * The classes that affect the line height of paragraph should implement this interface.
Florina Muntenescu139e1b52018-01-24 15:52:00 +000032 */
33public 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 Project9066cfe2009-03-03 19:31:44 -080044 public void chooseHeight(CharSequence text, int start, int end,
Florina Muntenescu139e1b52018-01-24 15:52:00 +000045 int spanstartv, int lineHeight,
46 Paint.FontMetricsInt fm);
Eric Fischera9f1dd02009-08-12 15:00:10 -070047
Florina Muntenescu139e1b52018-01-24 15:52:00 +000048 /**
Haoyu Zhang444be8b2018-08-23 17:44:18 -070049 * The classes that affect the line height of paragraph with respect to density,
50 * should implement this interface.
Florina Muntenescu139e1b52018-01-24 15:52:00 +000051 */
Eric Fischera9f1dd02009-08-12 15:00:10 -070052 public interface WithDensity extends LineHeightSpan {
Florina Muntenescu139e1b52018-01-24 15:52:00 +000053
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 Fischera9f1dd02009-08-12 15:00:10 -070064 public void chooseHeight(CharSequence text, int start, int end,
Florina Muntenescu139e1b52018-01-24 15:52:00 +000065 int spanstartv, int lineHeight,
66 Paint.FontMetricsInt fm, TextPaint paint);
Eric Fischera9f1dd02009-08-12 15:00:10 -070067 }
Haoyu Zhang444be8b2018-08-23 17:44:18 -070068
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 Zhang63a5efb2018-11-26 15:36:29 -080077 class Standard implements LineHeightSpan, ParcelableSpan {
Haoyu Zhang444be8b2018-08-23 17:44:18 -070078
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 Zhang63a5efb2018-11-26 15:36:29 -080088 /**
89 * Constructor called from {@link TextUtils} to restore the span from a parcel
90 */
Haoyu Zhang7c894602019-03-04 13:23:13 -080091 public Standard(@NonNull Parcel src) {
Haoyu Zhang63a5efb2018-11-26 15:36:29 -080092 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 Zhang444be8b2018-08-23 17:44:18 -0700130 @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 Project9066cfe2009-03-03 19:31:44 -0800144}