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

Commit 27045609 authored by Shai Barack's avatar Shai Barack Committed by Android (Google) Code Review
Browse files

Merge "Add flag to not compress fonts in the APK" into main

parents 5fcb4812 c6161c28
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -2649,6 +2649,10 @@ int LinkCommand::Action(const std::vector<std::string>& args) {
      ".mpg", ".mpeg", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2", ".wma", ".wmv",
      ".webm", ".mkv"});

  if (options_.no_compress_fonts) {
    options_.extensions_to_not_compress.insert({".ttf", ".otf", ".ttc"});
  }

  // Turn off auto versioning for static-libs.
  if (context.GetPackageType() == PackageType::kStaticLib) {
    options_.no_auto_version = true;
+9 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ struct LinkOptions {
  bool use_sparse_encoding = false;
  std::unordered_set<std::string> extensions_to_not_compress;
  std::optional<std::regex> regex_to_not_compress;
  bool no_compress_fonts = false;
  FeatureFlagValues feature_flag_values;

  // Static lib options.
@@ -300,6 +301,14 @@ class LinkCommand : public Command {
            "use the '$' symbol for end of line. Uses a case-sensitive ECMAScript"
            "regular expression grammar.",
        &no_compress_regex);
    AddOptionalSwitch("--no-compress-fonts",
        "Do not compress files with common extensions for fonts.\n"
            "This allows loading fonts directly from the APK, without needing to\n"
            "decompress them first. Loading fonts will be faster and use less memory.\n"
            "The downside is that the APK will be larger.\n"
            "Passing this flag is functionally equivalent to passing the following flags:\n"
            "-0 .ttf -0 .otf -0 .ttc",
        &options_.no_compress_fonts);
    AddOptionalSwitch("--warn-manifest-validation",
        "Treat manifest validation errors as warnings.",
        &options_.manifest_fixer_options.warn_validation);
+41 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ TEST_F(LinkTest, NoCompressAssets) {
  WriteFile(GetTestPath("assets/test.txt"), content);
  WriteFile(GetTestPath("assets/test.hello.txt"), content);
  WriteFile(GetTestPath("assets/test.hello.xml"), content);
  WriteFile(GetTestPath("assets/fonts/myfont.ttf"), content);

  const std::string out_apk = GetTestPath("out.apk");
  std::vector<std::string> link_args = {
@@ -136,6 +137,10 @@ TEST_F(LinkTest, NoCompressAssets) {
  file = zip->FindFile("assets/test.hello.xml");
  ASSERT_THAT(file, Ne(nullptr));
  EXPECT_FALSE(file->WasCompressed());

  file = zip->FindFile("assets/fonts/myfont.ttf");
  ASSERT_THAT(file, Ne(nullptr));
  EXPECT_TRUE(file->WasCompressed());
}

TEST_F(LinkTest, NoCompressResources) {
@@ -182,6 +187,42 @@ TEST_F(LinkTest, NoCompressResources) {
  EXPECT_FALSE(file->WasCompressed());
}

TEST_F(LinkTest, NoCompressFonts) {
  StdErrDiagnostics diag;
  std::string content(500, 'a');
  const std::string compiled_files_dir = GetTestPath("compiled");
  ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test.txt"), content, compiled_files_dir, &diag));
  WriteFile(GetTestPath("assets/fonts/myfont1.ttf"), content);
  WriteFile(GetTestPath("assets/fonts/myfont2.ttf"), content);

  const std::string out_apk = GetTestPath("out.apk");
  std::vector<std::string> link_args = {
      "--manifest", GetDefaultManifest(),
      "-o", out_apk,
      "--no-compress-fonts",
      "-A", GetTestPath("assets")
  };

  ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag));

  std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag);
  ASSERT_THAT(apk, Ne(nullptr));
  io::IFileCollection* zip = apk->GetFileCollection();
  ASSERT_THAT(zip, Ne(nullptr));

  auto file = zip->FindFile("res/raw/test.txt");
  ASSERT_THAT(file, Ne(nullptr));
  EXPECT_TRUE(file->WasCompressed());

  file = zip->FindFile("assets/fonts/myfont1.ttf");
  ASSERT_THAT(file, Ne(nullptr));
  EXPECT_FALSE(file->WasCompressed());

  file = zip->FindFile("assets/fonts/myfont2.ttf");
  ASSERT_THAT(file, Ne(nullptr));
  EXPECT_FALSE(file->WasCompressed());
}

TEST_F(LinkTest, OverlayStyles) {
  StdErrDiagnostics diag;
  const std::string compiled_files_dir = GetTestPath("compiled");
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
## Version 2.20
- Too many features, bug fixes, and improvements to list since the last minor version update in
  2017. This README will be updated more frequently in the future.
- Added a new flag `--no-compress-fonts`. This can significantly speed up loading fonts from APK
  assets, at the cost of increasing the storage size of the APK.

## Version 2.19
- Added navigation resource type.