Skip to content

Commit

Permalink
test: extend all test tools with freetype
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Aug 11, 2024
1 parent ab42799 commit ac56328
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
6 changes: 6 additions & 0 deletions build/Build.LibAlphaSkia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,16 @@ void BuildAlphaSkiaTest()
// run executable
if (LibAlphaSkiaCanRunTests)
{
Log.Information($"Running {TargetOs.RuntimeIdentifier}-{Architecture} tests on {TargetOperatingSystem.Current.RuntimeIdentifier}-{Architecture.Current} host system (OS fonts)");
ToolResolver.GetTool(exePath)(
"",
workingDirectory: outDir
);
Log.Information($"Running {TargetOs.RuntimeIdentifier}-{Architecture} tests on {TargetOperatingSystem.Current.RuntimeIdentifier}-{Architecture.Current} host system (FreeType fonts)");
ToolResolver.GetTool(exePath)(
"--freetype",
workingDirectory: outDir
);
}
else
{
Expand Down
18 changes: 18 additions & 0 deletions lib/node/alphaskia/src/AlphaSkiaCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ export class AlphaSkiaCanvas extends AlphaSkiaNative<AlphaSkiaCanvasHandle> {
return AlphaSkiaCanvas.#colorType;
}

/**
* Switches the rendering to use the operating system font manager and font rendering.
* This results in a platform specific display of any rendered texts and allows using of any
* fonts installed on the system.
*/
public static switchToOperatingSystemFonts(): void {
loadAddon().alphaskia_switch_to_operating_system_fonts();
}

/**
* Switches the rendering to use the FreeType font manager and font rendering.
* This results in a platform independent display of any rendered texts achieving consistent rendering.
* Operating system fonts cannot be used in this mode.
*/
public static SwitchToFreeTypeFonts(): void {
loadAddon().alphaskia_switch_to_freetype_fonts();
}

/**
* Gets the color to use for drawing operations in the native canvas.
* See also {@link rgbaToColor}
Expand Down
18 changes: 14 additions & 4 deletions test/dotnet/AlphaSkia.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ private static AlphaSkiaImage RenderFullImage()
return fullImage!;
}

public static int Main()
public static int Main(string[] args)
{
var isFreeType = args.Contains("--freetype");
if(isFreeType)
{
Console.WriteLine("Switching to FreeType Fonts");
AlphaSkiaCanvas.SwitchToFreeTypeFonts();
}

const double tolerancePercent = 1;
try
{
Expand Down Expand Up @@ -88,7 +95,10 @@ public static int Main()
var testOutputPath = Path.Combine(repositoryRoot, "test", "test-outputs", "dotnet");
Directory.CreateDirectory(testOutputPath);

var testOutputFile = Path.Combine(testOutputPath, AlphaSkiaTestRid + ".png");

var testOutputFileBase = isFreeType ? "freetype" : AlphaSkiaTestRid;

var testOutputFile = Path.Combine(testOutputPath, testOutputFileBase + ".png");
var pngData = actualImage.ToPng();
if (pngData == null)
{
Expand All @@ -99,7 +109,7 @@ public static int Main()
Console.WriteLine($"Image saved to {testOutputFile}");

// load reference image
var testReferencePath = Path.Combine(testDataPath, "reference", AlphaSkiaTestRid + ".png");
var testReferencePath = Path.Combine(testDataPath, "reference", testOutputFileBase + ".png");
Console.WriteLine($"Loading reference image {testReferencePath}");

var testReferenceData = File.ReadAllBytes(testReferencePath);
Expand Down Expand Up @@ -151,7 +161,7 @@ public static int Main()
using var diffImage = AlphaSkiaImage.FromPixels(actualWidth, actualHeight, diffPixels)!;
var diffImagePngData = diffImage.ToPng()!;

var diffOutputPath = Path.Combine(testOutputPath, AlphaSkiaTestRid + ".diff.png");
var diffOutputPath = Path.Combine(testOutputPath, testOutputFileBase + ".diff.png");
File.WriteAllBytes(diffOutputPath, diffImagePngData);
Console.WriteLine($"Error diff image saved to {diffOutputPath}");
return 1;
Expand Down
18 changes: 13 additions & 5 deletions test/java/src/main/java/alphaTab/alphaSkia/test/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ private static AlphaSkiaImage renderFullImage() {
}

public static void main(String[] args) {
System.exit(mainWithExitCode());
System.exit(mainWithExitCode(args));
}

private static int mainWithExitCode() {
private static int mainWithExitCode(String[] args) {
try {
initializeAlphaSkia();
boolean isFreeType = Arrays.stream(args).anyMatch("--freetype"::equals);
if(isFreeType) {
System.out.println("Switching to FreeType Fonts");
AlphaSkiaCanvas.switchToFreeTypeFonts();
}

double tolerancePercent = 1;

var repositoryRoot = findRepositoryRoot(Paths.get(".").toAbsolutePath());
Expand Down Expand Up @@ -97,7 +103,9 @@ private static int mainWithExitCode() {
//noinspection ResultOfMethodCallIgnored
testOutputPath.toFile().mkdirs();

var testOutputFile = testOutputPath.resolve(getAlphaSkiaTestRid() + ".png");
var testOutputFileBase = isFreeType ? "freetype" : getAlphaSkiaTestRid();

var testOutputFile = testOutputPath.resolve(testOutputFileBase + ".png");
var pngData = actualImage.toPng();
if (pngData == null) {
throw new IllegalStateException("Failed to encode final image to png");
Expand All @@ -107,7 +115,7 @@ private static int mainWithExitCode() {
System.out.println("Image saved to " + testOutputFile);

// load reference image
var testReferencePath = testDataPath.resolve("reference/" + getAlphaSkiaTestRid() + ".png");
var testReferencePath = testDataPath.resolve("reference/" + testOutputFileBase + ".png");
System.out.println("Loading reference image " + testReferencePath);

var testReferenceData = Files.readAllBytes(testReferencePath);
Expand Down Expand Up @@ -154,7 +162,7 @@ private static int mainWithExitCode() {
try (var diffImage = AlphaSkiaImage.fromPixels(actualWidth, actualHeight, diffPixels)) {
var diffImagePngData = diffImage.toPng();

var diffOutputPath = testOutputPath.resolve(getAlphaSkiaTestRid() + ".diff.png");
var diffOutputPath = testOutputPath.resolve(testOutputFileBase + ".diff.png");
Files.write(diffOutputPath, diffImagePngData);
System.out.println("Error diff image saved to " + diffOutputPath);
return 1;
Expand Down
26 changes: 20 additions & 6 deletions test/native/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ bool write_data_to_file_and_free(alphaskia_data_t data, std::string path)
return true;
}

int main()
int main(int argc, char **argv)
{
bool isFreeType = false;

for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "--freetype") == 0)
{
isFreeType = true;
std::cout << "Switching to FreeType Fonts" << std::endl;
alphaskia_switch_to_freetype_fonts();
}
}

const double tolerance_percent = 1;
bool found(false);
std::filesystem::path repository_root = find_repository_root(std::filesystem::current_path(), found);
Expand Down Expand Up @@ -116,7 +128,9 @@ int main()
std::filesystem::path test_output_path = repository_root / "test" / "test-outputs" / "native";
std::filesystem::create_directories(test_output_path);

std::filesystem::path test_output_file = test_output_path / (std::string(STRINGIFY(ALPHASKIA_TEST_RID)) + ".png");
std::string test_output_file_base = isFreeType ? "freetype" : STRINGIFY(ALPHASKIA_TEST_RID);

std::filesystem::path test_output_file = test_output_path / (test_output_file_base + ".png");
alphaskia_data_t png_data = alphaskia_image_encode_png(actual_image);
if (!png_data)
{
Expand All @@ -127,7 +141,7 @@ int main()
std::cout << "Image saved to " << test_output_file.generic_string() << std::endl;

// load reference image
std::filesystem::path test_reference_path = test_data_path / "reference" / (std::string(STRINGIFY(ALPHASKIA_TEST_RID)) + ".png");
std::filesystem::path test_reference_path = test_data_path / "reference" / (test_output_file_base + ".png");
std::cout << "Loading reference image " << test_reference_path.generic_string() << std::endl;
std::vector<uint8_t> test_reference_data;
read_file(test_reference_path.generic_string(), test_reference_data);
Expand Down Expand Up @@ -185,16 +199,16 @@ int main()
alphaskia_data_t diff_image_png_data = alphaskia_image_encode_png(diff_image);
alphaskia_image_free(diff_image);

auto diff_output_path = test_output_path / (std::string(STRINGIFY(ALPHASKIA_TEST_RID)) + ".diff.png");
auto diff_output_path = test_output_path / (test_output_file_base + ".diff.png");
write_data_to_file_and_free(diff_image_png_data, diff_output_path.generic_string());
std::cout << "Error diff image saved to " << diff_output_path.generic_string() << std::endl;

// for the sake of comparing directly, we also store the old image (we had cases where linux detected differences on the exact same file)
alphaskia_image_t old_image = alphaskia_image_from_pixels(actual_width, actual_height, expected_pixels.data());
alphaskia_data_t old_image_png_data = alphaskia_image_encode_png(old_image);
alphaskia_image_free(old_image);

auto old_output_path = test_output_path / (std::string(STRINGIFY(ALPHASKIA_TEST_RID)) + ".old.png");
auto old_output_path = test_output_path / (test_output_file_base + ".old.png");
write_data_to_file_and_free(old_image_png_data, old_output_path.generic_string());
std::cout << "Error old image saved to " << old_output_path.generic_string() << std::endl;

Expand Down
13 changes: 10 additions & 3 deletions test/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ function main(): number {
const tolerancePercent = 1;
try {
const repositoryRoot = findRepositoryRoot(path.resolve('.'));
const isFreeType = process.argv.includes("--freetype");
if(isFreeType) {
console.log("Switching to FreeType Fonts");
AlphaSkiaCanvas.switchToFreeTypeFonts();
}

// Load all fonts for rendering
console.log("Loading fonts");
Expand Down Expand Up @@ -101,7 +106,9 @@ function main(): number {
let testOutputPath = path.join(repositoryRoot, "test", "test-outputs", "dotnet");
fs.mkdirSync(testOutputPath, { recursive: true })

let testOutputFile = path.join(testOutputPath, alphaSkiaTestRid + ".png");
const testOutputFileBase = isFreeType ? "freetype" : alphaSkiaTestRid;

let testOutputFile = path.join(testOutputPath, testOutputFileBase + ".png");
let pngData = actualImage.toPng();
if (!pngData) {
throw new Error("Failed to encode final image to png");
Expand All @@ -111,7 +118,7 @@ function main(): number {
console.log(`Image saved to ${testOutputFile}`);

// load reference image
let testReferencePath = path.join(testDataPath, "reference", alphaSkiaTestRid + ".png");
let testReferencePath = path.join(testDataPath, "reference", testOutputFileBase + ".png");
console.log(`Loading reference image ${testReferencePath}`);

let testReferenceData = fs.readFileSync(testReferencePath);
Expand Down Expand Up @@ -159,7 +166,7 @@ function main(): number {
using diffImage = AlphaSkiaImage.fromPixels(actualWidth, actualHeight, diffPixels)!;
let diffImagePngData = diffImage.toPng()!;

let diffOutputPath = path.join(testOutputPath, alphaSkiaTestRid + ".diff.png");
let diffOutputPath = path.join(testOutputPath, testOutputFileBase + ".diff.png");
fs.writeFileSync(diffOutputPath, diffImagePngData);
console.log(`Error diff image saved to ${diffOutputPath}`);
return 1;
Expand Down
Binary file added test/test-data/reference/freetype.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ac56328

Please sign in to comment.