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

Commit 77e80b86 authored by Kohsuke Yatoh's avatar Kohsuke Yatoh
Browse files

Remove unused IFontManager#updateFontFile().

The caller was removed in
commit 3de07491
This CL also does some cleanup:
- Extract OtfFontFileParser to a separate file.
- Remove boilerplate in UpdatableFontDirTest.

Bug: 187879195
Test: m -j
Test: atest FrameworksServicesTests:UpdatableFontDirTest
Change-Id: Id3e369222c35f550c6d53e8649ab07647701c070
parent f476d931
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -30,7 +30,5 @@ import java.util.List;
interface IFontManager {
    FontConfig getFontConfig();

    int updateFontFile(in FontUpdateRequest request, int baseVersion);

    int updateFontFamily(in List<FontUpdateRequest> request, int baseVersion);
}
+0 −113
Original line number Diff line number Diff line
@@ -20,12 +20,8 @@ import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontFileUtil;
import android.graphics.fonts.FontManager;
import android.graphics.fonts.FontUpdateRequest;
import android.graphics.fonts.SystemFonts;
@@ -34,10 +30,6 @@ import android.os.SharedMemory;
import android.os.ShellCallback;
import android.system.ErrnoException;
import android.text.FontConfig;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AndroidException;
import android.util.IndentingPrintWriter;
import android.util.Slog;
@@ -52,19 +44,14 @@ import com.android.server.SystemService;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.NioUtils;
import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/** A service for managing system fonts. */
// TODO(b/173619554): Add API to update fonts.
public final class FontManagerService extends IFontManager.Stub {
    private static final String TAG = "FontManagerService";

@@ -77,23 +64,6 @@ public final class FontManagerService extends IFontManager.Stub {
        return getSystemFontConfig();
    }

    @Override
    public int updateFontFile(@NonNull FontUpdateRequest request, int baseVersion) {
        Preconditions.checkArgumentNonnegative(baseVersion);
        Objects.requireNonNull(request);
        Objects.requireNonNull(request.getFd());
        Objects.requireNonNull(request.getSignature());
        getContext().enforceCallingPermission(Manifest.permission.UPDATE_FONTS,
                "UPDATE_FONTS permission required.");
        try {
            update(baseVersion, Collections.singletonList(request));
            return FontManager.RESULT_SUCCESS;
        } catch (SystemFontException e) {
            Slog.e(TAG, "Failed to update font file", e);
            return e.getErrorCode();
        }
    }

    @Override
    public int updateFontFamily(@NonNull List<FontUpdateRequest> requests, int baseVersion) {
        Preconditions.checkArgumentNonnegative(baseVersion);
@@ -154,89 +124,6 @@ public final class FontManagerService extends IFontManager.Stub {
        }
    }

    /* package */ static class OtfFontFileParser implements UpdatableFontDir.FontFileParser {
        @Override
        public String getPostScriptName(File file) throws IOException {
            ByteBuffer buffer = mmap(file);
            try {
                return FontFileUtil.getPostScriptName(buffer, 0);
            } finally {
                NioUtils.freeDirectBuffer(buffer);
            }
        }

        @Override
        public String buildFontFileName(File file) throws IOException {
            ByteBuffer buffer = mmap(file);
            try {
                String psName = FontFileUtil.getPostScriptName(buffer, 0);
                int isType1Font = FontFileUtil.isPostScriptType1Font(buffer, 0);
                int isCollection = FontFileUtil.isCollectionFont(buffer);

                if (TextUtils.isEmpty(psName) || isType1Font == -1 || isCollection == -1) {
                    return null;
                }

                String extension;
                if (isCollection == 1) {
                    extension = isType1Font == 1 ? ".otc" : ".ttc";
                } else {
                    extension = isType1Font == 1 ? ".otf" : ".ttf";
                }
                return psName + extension;
            } finally {
                NioUtils.freeDirectBuffer(buffer);
            }

        }

        @Override
        public long getRevision(File file) throws IOException {
            ByteBuffer buffer = mmap(file);
            try {
                return FontFileUtil.getRevision(buffer, 0);
            } finally {
                NioUtils.freeDirectBuffer(buffer);
            }
        }

        @Override
        public void tryToCreateTypeface(File file) throws Throwable {
            Font font = new Font.Builder(file).build();
            FontFamily family = new FontFamily.Builder(font).build();
            Typeface typeface = new Typeface.CustomFallbackBuilder(family).build();

            TextPaint p = new TextPaint();
            p.setTextSize(24f);
            p.setTypeface(typeface);

            // Test string to try with the passed font.
            // TODO: Good to extract from font file.
            String testTextToDraw = "abcXYZ@- "
                    + "\uD83E\uDED6" // Emoji E13.0
                    + "\uD83C\uDDFA\uD83C\uDDF8" // Emoji Flags
                    + "\uD83D\uDC8F\uD83C\uDFFB" // Emoji Skin tone Sequence
                    // ZWJ Sequence
                    + "\uD83D\uDC68\uD83C\uDFFC\u200D\u2764\uFE0F\u200D\uD83D\uDC8B\u200D"
                    + "\uD83D\uDC68\uD83C\uDFFF";

            int width = (int) Math.ceil(Layout.getDesiredWidth(testTextToDraw, p));
            StaticLayout layout = StaticLayout.Builder.obtain(
                    testTextToDraw, 0, testTextToDraw.length(), p, width).build();
            Bitmap bmp = Bitmap.createBitmap(
                    layout.getWidth(), layout.getHeight(), Bitmap.Config.ALPHA_8);
            Canvas canvas = new Canvas(bmp);
            layout.draw(canvas);
        }

        private static ByteBuffer mmap(File file) throws IOException {
            try (FileInputStream in = new FileInputStream(file)) {
                FileChannel fileChannel = in.getChannel();
                return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
            }
        }
    }

    private static class FsverityUtilImpl implements UpdatableFontDir.FsverityUtil {
        @Override
        public boolean hasFsverity(String filePath) {
+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 com.android.server.graphics.fonts;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontFileUtil;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.NioUtils;
import java.nio.channels.FileChannel;

/* package */  class OtfFontFileParser implements UpdatableFontDir.FontFileParser {
    @Override
    public String getPostScriptName(File file) throws IOException {
        ByteBuffer buffer = mmap(file);
        try {
            return FontFileUtil.getPostScriptName(buffer, 0);
        } finally {
            NioUtils.freeDirectBuffer(buffer);
        }
    }

    @Override
    public String buildFontFileName(File file) throws IOException {
        ByteBuffer buffer = mmap(file);
        try {
            String psName = FontFileUtil.getPostScriptName(buffer, 0);
            int isType1Font = FontFileUtil.isPostScriptType1Font(buffer, 0);
            int isCollection = FontFileUtil.isCollectionFont(buffer);

            if (TextUtils.isEmpty(psName) || isType1Font == -1 || isCollection == -1) {
                return null;
            }

            String extension;
            if (isCollection == 1) {
                extension = isType1Font == 1 ? ".otc" : ".ttc";
            } else {
                extension = isType1Font == 1 ? ".otf" : ".ttf";
            }
            return psName + extension;
        } finally {
            NioUtils.freeDirectBuffer(buffer);
        }

    }

    @Override
    public long getRevision(File file) throws IOException {
        ByteBuffer buffer = mmap(file);
        try {
            return FontFileUtil.getRevision(buffer, 0);
        } finally {
            NioUtils.freeDirectBuffer(buffer);
        }
    }

    @Override
    public void tryToCreateTypeface(File file) throws Throwable {
        Font font = new Font.Builder(file).build();
        FontFamily family = new FontFamily.Builder(font).build();
        Typeface typeface = new Typeface.CustomFallbackBuilder(family).build();

        TextPaint p = new TextPaint();
        p.setTextSize(24f);
        p.setTypeface(typeface);

        // Test string to try with the passed font.
        // TODO: Good to extract from font file.
        String testTextToDraw = "abcXYZ@- "
                + "\uD83E\uDED6" // Emoji E13.0
                + "\uD83C\uDDFA\uD83C\uDDF8" // Emoji Flags
                + "\uD83D\uDC8F\uD83C\uDFFB" // Emoji Skin tone Sequence
                // ZWJ Sequence
                + "\uD83D\uDC68\uD83C\uDFFC\u200D\u2764\uFE0F\u200D\uD83D\uDC8B\u200D"
                + "\uD83D\uDC68\uD83C\uDFFF";

        int width = (int) Math.ceil(Layout.getDesiredWidth(testTextToDraw, p));
        StaticLayout layout = StaticLayout.Builder.obtain(
                testTextToDraw, 0, testTextToDraw.length(), p, width).build();
        Bitmap bmp = Bitmap.createBitmap(
                layout.getWidth(), layout.getHeight(), Bitmap.Config.ALPHA_8);
        Canvas canvas = new Canvas(bmp);
        layout.draw(canvas);
    }

    private static ByteBuffer mmap(File file) throws IOException {
        try (FileInputStream in = new FileInputStream(file)) {
            FileChannel fileChannel = in.getChannel();
            return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
        }
    }
}
+68 −122

File changed.

Preview size limit exceeded, changes collapsed.