/* * Copyright (C) 2013 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; import com.android.ide.common.rendering.api.LayoutLog; import com.android.layoutlib.bridge.Bridge; import android.annotation.NonNull; import android.annotation.Nullable; import android.graphics.Paint_Delegate.FontInfo; import android.icu.lang.UScriptRun; import android.icu.text.Bidi; import android.icu.text.BidiRun; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Toolkit; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /** * Render the text by breaking it into various scripts and using the right font for each script. * Can be used to measure the text without actually drawing it. */ @SuppressWarnings("deprecation") public class BidiRenderer { private static final String JETBRAINS_VENDOR_ID = "JetBrains s.r.o"; private static final String JAVA_VENDOR = System.getProperty("java.vendor"); /** When scaleX is bigger than this, we need to apply the workaround for http://b.android.com/211659 */ private static final double SCALEX_WORKAROUND_LIMIT = 9; private static class ScriptRun { private final int start; private final int limit; private final Font font; private ScriptRun(int start, int limit, @NonNull Font font) { this.start = start; this.limit = limit; this.font = font; } } private final Graphics2D mGraphics; private final Paint_Delegate mPaint; private char[] mText; // Bounds of the text drawn so far. private RectF mBounds; private float mBaseline; private final Bidi mBidi = new Bidi(); /** * @param graphics May be null. * @param paint The Paint to use to get the fonts. Should not be null. * @param text Unidirectional text. Should not be null. */ public BidiRenderer(Graphics2D graphics, Paint_Delegate paint, char[] text) { assert (paint != null); mGraphics = graphics; mPaint = paint; mText = text; mBounds = new RectF(); } /** * * @param x The x-coordinate of the left edge of where the text should be drawn on the given * graphics. * @param y The y-coordinate at which to draw the text on the given mGraphics. * */ public BidiRenderer setRenderLocation(float x, float y) { mBounds.set(x, y, x, y); mBaseline = y; return this; } /** * Perform Bidi Analysis on the text and then render it. *
* To skip the analysis and render unidirectional text, see {@link * #renderText(int, int, boolean, float[], int, boolean)} */ public RectF renderText(int start, int limit, int bidiFlags, float[] advances, int advancesIndex, boolean draw) { mBidi.setPara(Arrays.copyOfRange(mText, start, limit), (byte)getIcuFlags(bidiFlags), null); mText = mBidi.getText(); for (int i = 0; i < mBidi.countRuns(); i++) { BidiRun visualRun = mBidi.getVisualRun(i); boolean isRtl = visualRun.getDirection() == Bidi.RTL; renderText(visualRun.getStart(), visualRun.getLimit(), isRtl, advances, advancesIndex, draw); } return mBounds; } /** * Render unidirectional text. * * This method can also be used to measure the width of the text without actually drawing it. * * @param start index of the first character * @param limit index of the first character that should not be rendered. * @param isRtl is the text right-to-left * @param advances If not null, then advances for each character to be rendered are returned * here. * @param advancesIndex index into advances from where the advances need to be filled. * @param draw If true and {@code graphics} is not null, draw the rendered text on the graphics * at the given co-ordinates * @return A rectangle specifying the bounds of the text drawn. */ public RectF renderText(int start, int limit, boolean isRtl, float[] advances, int advancesIndex, boolean draw) { // We break the text into scripts and then select font based on it and then render each of // the script runs. for (ScriptRun run : getScriptRuns(mText, start, limit, mPaint.getFonts())) { int flag = Font.LAYOUT_NO_LIMIT_CONTEXT | Font.LAYOUT_NO_START_CONTEXT; flag |= isRtl ? Font.LAYOUT_RIGHT_TO_LEFT : Font.LAYOUT_LEFT_TO_RIGHT; renderScript(run.start, run.limit, run.font, flag, advances, advancesIndex, draw); advancesIndex += run.limit - run.start; } return mBounds; } /** * Render a script run to the right of the bounds passed. Use the preferred font to render as * much as possible. This also implements a fallback mechanism to render characters that cannot * be drawn using the preferred font. */ private void renderScript(int start, int limit, Font preferredFont, int flag, float[] advances, int advancesIndex, boolean draw) { if (mPaint.getFonts().size() == 0 || preferredFont == null) { return; } while (start < limit) { boolean foundFont = false; int canDisplayUpTo = preferredFont.canDisplayUpTo(mText, start, limit); if (canDisplayUpTo == -1) { // We can draw all characters in the text. render(start, limit, preferredFont, flag, advances, advancesIndex, draw); return; } if (canDisplayUpTo > start) { // We can draw something. render(start, canDisplayUpTo, preferredFont, flag, advances, advancesIndex, draw); advancesIndex += canDisplayUpTo - start; start = canDisplayUpTo; } // The current character cannot be drawn with the preferred font. Cycle through all the // fonts to check which one can draw it. int charCount = Character.isHighSurrogate(mText[start]) ? 2 : 1; List