Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54f1e21f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add Font file path if it is created from native."

parents 7518e291 c519ed8e
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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;

import static com.google.common.truth.Truth.assertThat;

import android.graphics.text.PositionedGlyphs;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class TextShaperTest {

    @Test
    public void testFontWithPath() {
        TextPaint p = new TextPaint();
        p.setFontFeatureSettings("'wght' 900");
        List<PositionedGlyphs> glyphs = StyledTextShaper.shapeText("a", 0, 1,
                TextDirectionHeuristics.LTR, p);
        assertThat(glyphs.size()).isEqualTo(1);
        // This test only passes if the font of the Latin font is variable font.
        assertThat(glyphs.get(0).getFont(0).getFile()).isNotNull();

    }
}
+6 −1
Original line number Diff line number Diff line
@@ -687,7 +687,9 @@ public final class Font {
                charBuffer[3] = (char) ((packedAxis & 0x0000_00FF_0000_0000L) >> 32);
                axes[i] = new FontVariationAxis(new String(charBuffer), value);
            }
            Font.Builder builder = new Font.Builder(buffer)
            String path = nGetFontPath(ptr);
            File file = (path == null) ? null : new File(path);
            Font.Builder builder = new Font.Builder(buffer, file, "")
                    .setWeight(weight)
                    .setSlant(italic ? FontStyle.FONT_SLANT_ITALIC : FontStyle.FONT_SLANT_UPRIGHT)
                    .setTtcIndex(ttcIndex)
@@ -711,6 +713,9 @@ public final class Font {
    @CriticalNative
    private static native long nGetAxisInfo(long ptr, int i);

    @FastNative
    private static native String nGetFontPath(long ptr);

    @FastNative
    private static native float nGetGlyphBounds(long font, int glyphId, long paint, RectF rect);

+12 −0
Original line number Diff line number Diff line
@@ -221,6 +221,17 @@ static jlong Font_getAxisInfo(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle, jint i
    return (static_cast<uint64_t>(var.axisTag) << 32) | static_cast<uint64_t>(floatBinary);
}

// FastNative
static jstring Font_getFontPath(JNIEnv* env, jobject, jlong fontHandle) {
    const minikin::Font* font = reinterpret_cast<minikin::Font*>(fontHandle);
    MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->typeface().get());
    const std::string& filePath = minikinSkia->getFilePath();
    if (filePath.empty()) {
        return nullptr;
    }
    return env->NewStringUTF(filePath.c_str());
}

// Critical Native
static jlong Font_getNativeFontPtr(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) {
    FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
@@ -274,6 +285,7 @@ static const JNINativeMethod gFontMethods[] = {
    { "nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", (void*) Font_getFontMetrics },
    { "nGetFontInfo", "(J)J", (void*) Font_getFontInfo },
    { "nGetAxisInfo", "(JI)J", (void*) Font_getAxisInfo },
    { "nGetFontPath", "(J)Ljava/lang/String;", (void*) Font_getFontPath },
    { "nGetNativeFontPtr", "(J)J", (void*) Font_getNativeFontPtr },
};