From 18cf15d9560e9bfc4faa31e355b3436c2e3328e1 Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Sat, 27 Mar 2021 22:02:41 +0100 Subject: [PATCH] FontSizeScene --- examples/scenes/src/FontSizeScene.java | 112 +++++++++++++++++++++++++ examples/scenes/src/Scenes.java | 5 +- 2 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 examples/scenes/src/FontSizeScene.java diff --git a/examples/scenes/src/FontSizeScene.java b/examples/scenes/src/FontSizeScene.java new file mode 100644 index 00000000..7aec1439 --- /dev/null +++ b/examples/scenes/src/FontSizeScene.java @@ -0,0 +1,112 @@ +package org.jetbrains.skija.examples.scenes; + +import java.util.*; +import org.jetbrains.skija.*; + +public class FontSizeScene extends Scene { + Paint grayStroke = new Paint().setColor(0x80808080).setMode(PaintMode.STROKE).setStrokeWidth(1); + Paint redStroke = new Paint().setColor(0x80CC3333).setMode(PaintMode.STROKE).setStrokeWidth(1); + Paint greenStroke = new Paint().setColor(0x8033CC33).setMode(PaintMode.STROKE).setStrokeWidth(1); + Paint blueStroke = new Paint().setColor(0x803333CC).setMode(PaintMode.STROKE).setStrokeWidth(1); + + Paint grayFill = new Paint().setColor(0xFF808080); + Paint redFill = new Paint().setColor(0xFFCC3333); + Paint greenFill = new Paint().setColor(0xFF33CC33); + Paint blueFill = new Paint().setColor(0xFF3333CC); + + Typeface[] faces; + Font inter9 = new Font(inter, 9); + + public FontSizeScene() { + var names = new String[] { + "Inconsolata", + "Anonymous Pro", + "Hasklig", + "Mono Lisa", + "Hack", + "PragmataPro Mono", + + "Anonymous Pro", + "Bitstream Vera Sans Mono", + "Cascadia Code", + "Consolas", + "Courier", + "Courier New", + "Fira Code", + "Hack", + "IBM Plex Mono", + "Input Mono", + "Iosevka", + "JetBrains Mono", + "Menlo", + "Monaco", + "Mono Lisa", + "M+ 2m", + "PragmataPro Mono", + "PT Mono", + "SF Mono", + "Source Code Pro", + "Ubuntu Mono", + "Victor Mono" + }; + + faces = Arrays.stream(names) + .map(name -> { + var face = Typeface.makeFromName(name, FontStyle.NORMAL); + return name.equals(face.getFamilyName()) ? face : null; + }) + .filter(face -> face != null) + .toArray(Typeface[]::new); + } + + @Override + public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int ypos) { + canvas.translate(20, 100); + + // canvas.drawString("Font Size = 32", 0, -50, inter13, blackFill); + + var fonts = Arrays.stream(faces) + .map(face -> new Font(face, 32)) + // .sorted((a, b) -> (int) Math.signum(b.getMetrics().getHeight() - a.getMetrics().getHeight())) + .toArray(Font[]::new); + + float x = width; + canvas.translate(0, -150); + for (var font: fonts) { + if (x + 100 > width) { + canvas.translate(0, 150); + x = 60; + canvas.drawString("Ascent", 0, 35, inter9, greenFill); + canvas.drawString("Cap Height", 0, 50, inter9, redFill); + canvas.drawString("x-Height", 0, 65, inter9, blueFill); + canvas.drawString("Descent", 0, 80, inter9, greenFill); + canvas.drawString("Height", 0, 95, inter9, grayFill); + } + + try (var line = TextLine.make("Andy", font);) { + canvas.drawLine(x - 10, 0, x + line.getWidth() + 10, 0, grayStroke); + canvas.drawLine(x - 10, font.getMetrics().getAscent(), x + line.getWidth() + 10, font.getMetrics().getAscent(), greenStroke); + canvas.drawLine(x - 10, -font.getMetrics().getCapHeight(), x + line.getWidth() + 10, -font.getMetrics().getCapHeight(), redStroke); + canvas.drawLine(x - 10, -font.getMetrics().getXHeight(), x + line.getWidth() + 10, -font.getMetrics().getXHeight(), blueStroke); + canvas.drawLine(x - 10, font.getMetrics().getDescent(), x + line.getWidth() + 10, font.getMetrics().getDescent(), greenStroke); + + canvas.drawTextLine(line, x, 0, blackFill); + + canvas.save(); + canvas.clipRect(Rect.makeXYWH(x, 0, line.getWidth() + 10, 25)); + canvas.drawString(font.getTypeface().getFamilyName(), x, 20, inter9, blackFill); + canvas.restore(); + + canvas.drawString(String.format("%.2f", (-font.getMetrics().getAscent())), x, 35, inter9, greenFill); + canvas.drawString(String.format("%.2f", font.getMetrics().getCapHeight()), x, 50, inter9, redFill); + canvas.drawString(String.format("%.2f", font.getMetrics().getXHeight()), x, 65, inter9, blueFill); + canvas.drawString(String.format("%.2f", font.getMetrics().getDescent()), x, 80, inter9, greenFill); + canvas.drawString(String.format("%.2f", font.getMetrics().getHeight()), x, 95, inter9, grayFill); + + x += line.getWidth() + 20; + } + font.close(); + } + canvas.translate(0, 40); + } +} \ No newline at end of file diff --git a/examples/scenes/src/Scenes.java b/examples/scenes/src/Scenes.java index 05410442..a1474359 100644 --- a/examples/scenes/src/Scenes.java +++ b/examples/scenes/src/Scenes.java @@ -6,7 +6,7 @@ public class Scenes { public static TreeMap scenes; - public static String currentScene = "Skottie"; + public static String currentScene = "Font Size"; public static HUD hud = new HUD(); public static boolean vsync = true; public static boolean stats = true; @@ -25,6 +25,7 @@ public class Scenes { scenes.put("Figma", null); scenes.put("Font", null); scenes.put("Font Rendering", null); + scenes.put("Font Size", null); scenes.put("Font Variations", null); scenes.put("Geometry", null); scenes.put("Images", null); @@ -106,4 +107,4 @@ public static void draw(Canvas canvas, int width, int height, float scale, int m } else hud.log(); } -} \ No newline at end of file +}