diff options
Diffstat (limited to 'graphics/java')
| -rw-r--r-- | graphics/java/android/graphics/text/LineBreakConfig.java | 110 | ||||
| -rw-r--r-- | graphics/java/android/graphics/text/MeasuredText.java | 26 |
2 files changed, 135 insertions, 1 deletions
diff --git a/graphics/java/android/graphics/text/LineBreakConfig.java b/graphics/java/android/graphics/text/LineBreakConfig.java new file mode 100644 index 000000000000..4d818583fd23 --- /dev/null +++ b/graphics/java/android/graphics/text/LineBreakConfig.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2022 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.graphics.text; + +import android.annotation.IntDef; +import android.annotation.Nullable; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Objects; + +/** + * Indicates the strategies can be used when calculating the text wrapping. + * + * See <a href="https://drafts.csswg.org/css-text/#line-break-property">the line-break property</a> + */ +public final class LineBreakConfig { + + /** + * No line break style specified. + */ + public static final int LINE_BREAK_STYLE_NONE = 0; + + /** + * Use the least restrictive rule for line-breaking. This is usually used for short lines. + */ + public static final int LINE_BREAK_STYLE_LOOSE = 1; + + /** + * Indicate breaking text with the most comment set of line-breaking rules. + */ + public static final int LINE_BREAK_STYLE_NORMAL = 2; + + /** + * Indicates breaking text with the most strictest line-breaking rules. + */ + public static final int LINE_BREAK_STYLE_STRICT = 3; + + /** @hide */ + @IntDef(prefix = { "LINE_BREAK_STYLE_" }, value = { + LINE_BREAK_STYLE_NONE, LINE_BREAK_STYLE_LOOSE, LINE_BREAK_STYLE_NORMAL, + LINE_BREAK_STYLE_STRICT + }) + @Retention(RetentionPolicy.SOURCE) + public @interface LineBreakStyle {} + + private @LineBreakStyle int mLineBreakStyle = LINE_BREAK_STYLE_NONE; + + public LineBreakConfig() { + } + + /** + * Set the line break configuration. + * + * @param config the new line break configuration. + */ + public void set(@Nullable LineBreakConfig config) { + if (config != null) { + mLineBreakStyle = config.getLineBreakStyle(); + } else { + mLineBreakStyle = LineBreakConfig.LINE_BREAK_STYLE_NONE; + } + } + + /** + * Get the line break style. + * + * @return The current line break style to be used for the text wrapping. + */ + public @LineBreakStyle int getLineBreakStyle() { + return mLineBreakStyle; + } + + /** + * Set the line break style. + * + * @param lineBreakStyle the new line break style. + */ + public void setLineBreakStyle(@LineBreakStyle int lineBreakStyle) { + mLineBreakStyle = lineBreakStyle; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof LineBreakConfig)) return false; + LineBreakConfig that = (LineBreakConfig) o; + return mLineBreakStyle == that.mLineBreakStyle; + } + + @Override + public int hashCode() { + return Objects.hash(mLineBreakStyle); + } +} diff --git a/graphics/java/android/graphics/text/MeasuredText.java b/graphics/java/android/graphics/text/MeasuredText.java index a34d0abcf753..5f4afb7b9888 100644 --- a/graphics/java/android/graphics/text/MeasuredText.java +++ b/graphics/java/android/graphics/text/MeasuredText.java @@ -239,11 +239,33 @@ public class MeasuredText { */ public @NonNull Builder appendStyleRun(@NonNull Paint paint, @IntRange(from = 0) int length, boolean isRtl) { + return appendStyleRun(paint, null, length, isRtl); + } + + /** + * Apply styles to the given length. + * + * Keeps an internal offset which increases at every append. The initial value for this + * offset is zero. After the style is applied the internal offset is moved to {@code offset + * + length}, and next call will start from this new position. + * + * @param paint a paint + * @param lineBreakConfig a line break configuration. + * @param length a length to be applied with a given paint, can not exceed the length of the + * text + * @param isRtl true if the text is in RTL context, otherwise false. + */ + public @NonNull Builder appendStyleRun(@NonNull Paint paint, + @Nullable LineBreakConfig lineBreakConfig, @IntRange(from = 0) int length, + boolean isRtl) { Preconditions.checkNotNull(paint); Preconditions.checkArgument(length > 0, "length can not be negative"); final int end = mCurrentOffset + length; Preconditions.checkArgument(end <= mText.length, "Style exceeds the text length"); - nAddStyleRun(mNativePtr, paint.getNativeInstance(), mCurrentOffset, end, isRtl); + int lbStyle = (lineBreakConfig != null) ? lineBreakConfig.getLineBreakStyle() : + LineBreakConfig.LINE_BREAK_STYLE_NONE; + nAddStyleRun(mNativePtr, paint.getNativeInstance(), lbStyle, mCurrentOffset, end, + isRtl); mCurrentOffset = end; return this; } @@ -423,12 +445,14 @@ public class MeasuredText { * * @param nativeBuilderPtr The native MeasuredParagraph builder pointer. * @param paintPtr The native paint pointer to be applied. + * @param lineBreakStyle The line break style of the text. * @param start The start offset in the copied buffer. * @param end The end offset in the copied buffer. * @param isRtl True if the text is RTL. */ private static native void nAddStyleRun(/* Non Zero */ long nativeBuilderPtr, /* Non Zero */ long paintPtr, + int lineBreakStyle, @IntRange(from = 0) int start, @IntRange(from = 0) int end, boolean isRtl); |