Skip to content

Commit

Permalink
Use test suite own fonts for tests, not system ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Karm committed Jan 8, 2022
1 parent 11c644d commit c97a680
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.awt.runtime;

import java.awt.FontFormatException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.PropertyResourceBundle;

import com.oracle.svm.core.annotate.Substitute;
Expand All @@ -27,5 +29,24 @@ private static String getString(String className, String resource_name, String k
}
}

/**
* Getting .pfb/.pfa files to work would require additional runtime re-init adjustments.
* We are not doing that unless there is an explicit demand.
*/
@TargetClass(className = "sun.font.Type1Font")
final class Target_sun_font_Type1Font {
@Substitute
private void verifyPFA(ByteBuffer bb) throws FontFormatException {
throw new FontFormatException(
".pfa font files are not supported. Use TrueType fonts, i.e. .ttf files.");
}

@Substitute
private void verifyPFB(ByteBuffer bb) throws FontFormatException {
throw new FontFormatException(
".pfb font files are not supported. Use TrueType fonts, i.e. .ttf files.");
}
}

public class JDKSubstitutions {
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ public void init() throws IOException, FontFormatException {
final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

// Font source: https://ftp.gnu.org/gnu/freefont/
// Note those fonts binaries were altered to bear different names, "My" prefix.
// Note those fonts binaries were altered to bear different names, "My" prefix, "X" suffix.
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(
Application.class.getResourceAsStream("/MyFreeMono.ttf"),
"MyFreeMono.ttf not found.")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(
Application.class.getResourceAsStream("/MyFreeSerif.ttf"),
"MyFreeSerif.ttf not found.")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(
Application.class.getResourceAsStream("/DejaVuSansMonoX.ttf"),
"DejaVuSansMonoX.ttf not found.")));
}

public BufferedImage getABGRTestImage() throws IOException {
Expand Down Expand Up @@ -143,11 +146,12 @@ private static BufferedImage createABGRTestImage() {
// Text as glyph vector, draw bytes
final FontRenderContext fontRenderContext = g.getFontRenderContext();
g.setColor(Color.CYAN);
final GlyphVector glyphVector = new Font("Arial", Font.PLAIN, 16)
final GlyphVector glyphVector = new Font("DejaVu Sans Mono X", Font.PLAIN, 16)
.createGlyphVector(fontRenderContext, "Mandrel 1");
g.drawGlyphVector(glyphVector, img.getWidth() / 2f, img.getHeight() / 2f);
final byte[] bytesToDraw = new byte[] { 'M', 'A', 'N', 'D', 'R', 'E', 'L' };
g.drawBytes(bytesToDraw, 0, bytesToDraw.length, img.getWidth() - 70, img.getHeight() - 10);
g.setFont(new Font("DejaVu Sans Mono X", Font.PLAIN, 16));
g.drawBytes(bytesToDraw, 0, bytesToDraw.length, img.getWidth() - 75, img.getHeight() - 10);

// Arcs
g.setColor(Color.DARK_GRAY);
Expand Down Expand Up @@ -175,12 +179,12 @@ private static BufferedImage createABGRTestImage() {
pathOrRectangles.append(outer, false);
pathOrRectangles.append(inner, false);
g.fill(pathOrRectangles);
g.setFont(new Font("Helvetica", Font.BOLD, 15));
g.drawString("Intersection: ", 120, 200);
g.setFont(new Font("Helvetica", Font.BOLD, 20));
g.setFont(new Font("DejaVu Sans Mono X", Font.BOLD, 15));
g.drawString("Intersection: ", 120, 205);
g.setFont(new Font("DejaVu Sans Mono X", Font.BOLD, 20));
g.setColor(Color.RED);
g.drawString((e.intersects(outer.getBounds2D()) || e.intersects(inner.getBounds2D())) ? "YES❤️" : "NO",
img.getWidth() - 125, img.getHeight() - 93);
g.drawString((e.intersects(outer.getBounds2D()) || e.intersects(inner.getBounds2D())) ? "YES" : "NO",
img.getWidth() - 110, img.getHeight() - 88);

// Polygons
g.setColor(Color.GREEN);
Expand All @@ -202,7 +206,7 @@ private static BufferedImage createABGRTestImage() {
g.drawString("Mandrel 3", 20, 20);
g.setFont(new Font("MyFreeSerif", Font.PLAIN, 15));
g.drawString("Mandrel 4", 20, 60);
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 15));
g.setFont(new Font("DejaVu Sans Mono X", Font.BOLD, 15));
g.drawString("Mandrel 5", 20, 100);
String text = "Quarkus Mandrel";
g.setFont(new Font("MyFreeSerif", Font.PLAIN, 40));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
Expand All @@ -17,6 +18,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.function.Supplier;

import javax.imageio.IIOImage;
Expand Down Expand Up @@ -224,6 +226,14 @@ public Response image(@PathParam("format") String format) throws IOException {
}
}

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/fonts")
public Response fonts() {
return Response.ok().entity(Arrays.toString(
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())).build();
}

/**
* Prepares a TIFF with two images and image description metadata.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -53,12 +57,12 @@ public class ImageGeometryFontsTest {
// @formatter:off
@ValueSource(strings = {
// Image format name followed by expected pixel values
"TIFF █46,14,32,255 █72,22,22,255 █255,200,0,255 █255,0,0,255 █0,255,0,255",
"GIF █2,0,0,0 █5,0,0,0 █213,0,0,0 █130,0,0,0 █63,0,0,0",
"PNG █46,14,32,255 █72,22,22,255 █255,200,0,255 █255,0,0,255 █0,255,0,255",
"JPG █73,0,44,0 █122,7,4,0 █255,178,26,0 █254,2,0,0 █24,238,16,0",
"BMP █46,14,32,0 █72,22,22,0 █255,200,0,0 █255,0,0,0 █0,255,0,0",
"WBMP █0,0,0,0 █0,0,0,0 █1,0,0,0 █0,0,0,0 █0,0,0,0"
"TIFF █46,14,32,255 █0,0,0,255 █255,200,0,255 █255,0,0,255 █255,0,0,255",
"GIF █2,0,0,0 █0,0,0,0 █204,0,0,0 █121,0,0,0 █121,0,0,0",
"PNG █46,14,32,255 █0,0,0,255 █255,200,0,255 █255,0,0,255 █255,0,0,255",
"JPG █73,0,44,0 █42,0,23,0 █255,199,20,0 █239,7,0,0 █249,1,2,0",
"BMP █46,14,32,0 █0,0,0,0 █255,200,0,0 █255,0,0,0 █255,0,0,0",
"WBMP █0,0,0,0 █0,0,0,0 █1,0,0,0 █0,0,0,0 █0,0,0,0"
})
// @formatter:on
public void testGeometryAndFonts(String testData) throws IOException {
Expand All @@ -77,7 +81,7 @@ public void testGeometryAndFonts(String testData) throws IOException {
formatName, 350, 300, image.getWidth(), image.getHeight()));

// Test pixels
final int[][] pixelsCoordinates = new int[][] { { 80, 56 }, { 79, 14 }, { 58, 171 }, { 275, 199 }, { 28, 280 } };
final int[][] pixelsCoordinates = new int[][] { { 80, 56 }, { 94, 92 }, { 50, 186 }, { 246, 204 }, { 294, 205 } };
for (int i = 0; i < pixelsCoordinates.length; i++) {
final int[] expected = decodeArray4(formatPixels[i + 1].trim());
final int[] actual = new int[4]; //4BYTE RGBA
Expand All @@ -90,4 +94,30 @@ public void testGeometryAndFonts(String testData) throws IOException {
}
checkLog(null, "Geometry and Fonts");
}

/**
* At least some system fonts are expected to be found. The number may vary wildly
* depending on the system, so it doesn't make sense to be checking for a particular
* number or even for font family names.
*/
@Test
void checkFonts() {
final String fonts = given()
.when()
.get("/fonts")
.asString();
final String[] actual = fonts.substring(1, fonts.length() - 1).split(", ");
Assertions.assertTrue(actual.length > 3,
"There are supposed to be at least some system fonts found beside those installed by this TS.");
final Map<String, Boolean> expected = new HashMap<>(Map.of(
"MyFreeMono", Boolean.FALSE,
"MyFreeSerif", Boolean.FALSE,
"DejaVu Sans Mono X", Boolean.FALSE));
for (String f : actual) {
expected.replace(f, Boolean.TRUE);
}
Assertions.assertTrue(expected.values().stream().allMatch(Boolean.TRUE::equals),
"Not all expected fonts were found: " + expected + ". " +
"These fonts were found though: " + Arrays.toString(actual));
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ quarkus.log.file.path=quarkus.log
quarkus.log.file.level=DEBUG
quarkus.log.file.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n

quarkus.native.resources.includes=MyFreeMono.ttf,MyFreeSerif.ttf
quarkus.native.resources.includes=MyFreeMono.ttf,MyFreeSerif.ttf,DejaVuSansMonoX.ttf

0 comments on commit c97a680

Please sign in to comment.