Skip to content

Commit

Permalink
Font test (#170)
Browse files Browse the repository at this point in the history
Create font samples.
  • Loading branch information
weisJ authored May 24, 2020
1 parent cd83859 commit d3fd5de
Show file tree
Hide file tree
Showing 9 changed files with 958 additions and 9 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
name: Documentation
on:
push:
paths:
- '**.properties'
- '**/CreateUITable.java'
pull_request:
branches:
- 'master'
paths:
- '**.properties'
- '**/CreateUITable.java'
Expand Down
51 changes: 51 additions & 0 deletions .github/workflows/font.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# MIT License
#
# Copyright (c) 2020 Jannis Weis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#
name: Fonts

on:
push:
branches:
- 'master'
jobs:
gradle:
strategy:
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 10
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build
run: ./gradlew :darklaf-core:fontTest -PskipAutostyle
- name: Upload Results
uses: actions/upload-artifact@v1
with:
name: ${{ matrix.os }}-font_test
path: build/font_test
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ name: CI
on:
push:
branches:
- '*'
- 'master'
pull_request:
branches:
- '*'
Expand Down
13 changes: 12 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ val makeDocumentation by tasks.registering(JavaExec::class) {
classpath(sourceSets.main.get().runtimeClasspath, sourceSets.test.get().runtimeClasspath)
}

val fontTest by tasks.registering(JavaExec::class) {
group = "Verification"
description = "Creates font sample images"
dependsOn(tasks.testClasses)

workingDir = File(project.rootDir, "build")
workingDir.mkdirs()
main = "FontTest"
classpath(sourceSets.main.get().runtimeClasspath, sourceSets.test.get().runtimeClasspath)
}

tasks.shadowJar {
exclude("help/")
exclude("icons/")
Expand Down Expand Up @@ -63,7 +74,7 @@ abstract class DemoTask : JavaExec() {

val runDemo by tasks.registering(DemoTask::class) {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Launches demo (e.g. UIDemo, ui.table.TableDemo, ui.button.ButtonDemo, ...)"
description = "Launches demo (e.g. DemoLauncher, ui.table.TableDemo, ui.button.ButtonDemo, ...)"

classpath(sourceSets.test.map { it.runtimeClasspath })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public static BufferedImage scaledImageFromComponent(final Component c, final Re
}
g2d.translate(-bounds.x, -bounds.y);
if (print) {
c.printAll(g2d);
c.print(g2d);
} else {
c.paintAll(g2d);
c.paint(g2d);
}

g2d.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void patchOSFonts(final UIDefaults defaults, final Function<Map.Entry<Ob

private Font mapMacOSFont(final Map.Entry<Object, Font> entry) {
Font font = entry.getValue();
String fontName = SystemInfo.isMacOSCatalina ? MAC_OS_CATALINA_FONT_NAME : MAC_OS_FONT_NAME;
String fontName = SystemInfo.isMacOSCatalina ? MAC_OS_CATALINA_FONT_NAME_FALLBACK : MAC_OS_FONT_NAME;
Font macFont = new Font(fontName, font.getStyle(), font.getSize());
if (SystemInfo.isMacOSMojave) macFont = macFont.deriveFont(ENABLE_KERNING);
if (font instanceof UIResource) {
Expand Down
143 changes: 143 additions & 0 deletions core/src/test/java/FontTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.*;

import ui.DemoResources;

import com.github.weisj.darklaf.LafManager;
import com.github.weisj.darklaf.graphics.ImageUtil;
import com.github.weisj.darklaf.theme.IntelliJTheme;
import com.github.weisj.darklaf.util.SystemInfo;

public class FontTest {

private static final Map<AttributedCharacterIterator.Attribute, Integer> kerning;
private static final int SCALING_FACTOR = 3;
private static final String WORKING_DIR = "font_test";

static {
kerning = Collections.singletonMap(TextAttribute.KERNING, TextAttribute.KERNING_ON);
}

public static void main(final String[] args) throws IOException {
LafManager.install(new IntelliJTheme());
JTextArea textArea = new JTextArea();
textArea.setText(DemoResources.KERNING_TEST);
textArea.setSize(textArea.getPreferredSize());
textArea.doLayout();

if (SystemInfo.isMac) {
List<FontSpec> fontSpecs = new ArrayList<>();
if (SystemInfo.isMacOSMojave) {
fontSpecs.add(new FontSpec(".AppleSystemUIFont", false));
fontSpecs.add(new FontSpec(".AppleSystemUIFont", true));
}
fontSpecs.add(new FontSpec(".SF NS Text", false));
fontSpecs.add(new FontSpec(".SF NS Text", true));
fontSpecs.add(new FontSpec("Helvetica Neue", false));
fontSpecs.add(new FontSpec("Helvetica Neue", true));
createImages("macOS", textArea, fontSpecs);
} else if (SystemInfo.isWindows) {
List<FontSpec> fontSpecs = new ArrayList<>();
fontSpecs.add(new FontSpec("Segoe UI", false));
fontSpecs.add(new FontSpec("Segoe UI", true));
createImages("windows", textArea, fontSpecs);
} else if (SystemInfo.isLinux) {
List<FontSpec> fontSpecs = new ArrayList<>();
fontSpecs.add(new FontSpec("Ubuntu", false));
fontSpecs.add(new FontSpec("Ubuntu", true));
createImages("linux", textArea, fontSpecs);
}
}

private static void createImages(final String folder, final JComponent c,
final List<FontSpec> fontSpecs) throws IOException {
createFolder(folder);
for (FontSpec spec : fontSpecs) {
c.setFont(createFont(spec.fontName));
setFractionalMetrics(spec.useFractionalMetrics);
saveScreenShot(getPath(folder, spec), c);
}
}

private static void createFolder(final String folder) throws IOException {
Files.createDirectories(new File(WORKING_DIR + "/" + folder).toPath());
}

private static String getPath(final String folder, final FontSpec spec) {
return WORKING_DIR + "/" + folder + "/" + spec.getImageName();
}

private static void saveScreenShot(final String name, final JComponent c) {
try {
Rectangle rect = new Rectangle(0, 0, c.getWidth(), c.getHeight());
BufferedImage image = ImageUtil.scaledImageFromComponent(c, rect, SCALING_FACTOR, SCALING_FACTOR, false);
ImageIO.write(image, "png", new File(name + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}

private static void setFractionalMetrics(final boolean enabled) {
UIManager.put(RenderingHints.KEY_FRACTIONALMETRICS,
enabled ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
: RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
}

private static Font createFont(final String name) {
return new Font(name, Font.PLAIN, 12).deriveFont(kerning);
}

private static class FontSpec {
private final String fontName;
private final boolean useFractionalMetrics;

private FontSpec(final String fontName, final boolean useFractionalMetrics) {
this.fontName = fontName;
this.useFractionalMetrics = useFractionalMetrics;
}

private String getImageName() {
String name = fontName.replace(".", "").replace(" ", "_").toLowerCase();
if (useFractionalMetrics) {
name += "_frac_metrics";
}
return name;
}
}
}
Loading

0 comments on commit d3fd5de

Please sign in to comment.