刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package jnpf.fileStorage.dms;
 
import jnpf.dms.fonts.DmsWatermarkFonts;
import jnpf.exception.DataException;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
 
import java.awt.Font;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
 
import static org.junit.Assert.*;
 
public class DmsWatermarkFontsTest {
    @Rule public TemporaryFolder temporary = new TemporaryFolder();
 
    @Test public void unsupportedAndCorruptFilesNeverBecomeOptionsOrSystemFallbacks() throws Exception {
        Files.writeString(temporary.newFile("old.fon").toPath(), "legacy bitmap font");
        Files.writeString(temporary.newFile("invalid.ttf").toPath(), "not a font");
        DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath());
        assertTrue(fonts.options().isEmpty());
        assertThrows(DataException.class, () -> fonts.choose("SansSerif", "ABC", 24));
    }
 
    @Test public void missingDirectoryHasAnExplicitError() {
        DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot() + "/missing");
        assertThrows(DataException.class, fonts::options);
    }
 
    @Test public void physicalFontNamesAndLocalizedAliasesComeFromTheConfiguredFile() throws Exception {
        Path source = List.of(Path.of("/System/Library/Fonts/Supplemental/Arial.ttf"),
                Path.of("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"), Path.of("C:/Windows/Fonts/arial.ttf"))
                .stream().filter(Files::isRegularFile).findFirst().orElse(null);
        Assume.assumeNotNull(source);
        Files.copy(source, temporary.getRoot().toPath().resolve("renamed.TTF"));
        Font physical = Font.createFonts(source.toFile())[0];
        DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath());
        assertEquals(1, fonts.options().size());
        assertEquals(physical.getFamily(Locale.ENGLISH), fonts.options().get(0).family());
        assertEquals(physical.getFamily(Locale.SIMPLIFIED_CHINESE), fonts.options().get(0).displayName());
        Font chosen = fonts.choose(physical.getFamily(Locale.SIMPLIFIED_CHINESE), "Watermark", 28);
        assertEquals(physical.getFontName(Locale.ENGLISH), chosen.getFontName(Locale.ENGLISH));
        assertEquals(28F, chosen.getSize2D(), 0F);
        assertThrows(DataException.class, () -> fonts.choose("Font not in directory", "Watermark", 28));
    }
 
    @Test public void collectionsAreLoadedByFamilyRatherThanFileName() throws Exception {
        Path source = Path.of("/System/Library/Fonts/Helvetica.ttc");
        Assume.assumeTrue(Files.isRegularFile(source));
        Files.copy(source, temporary.getRoot().toPath().resolve("collection.ttc"));
        DmsWatermarkFonts fonts = new DmsWatermarkFonts(temporary.getRoot().getAbsolutePath());
        assertFalse(fonts.options().isEmpty());
        for (DmsWatermarkFonts.Option option : fonts.options()) {
            assertEquals(option.family(), fonts.choose(option.family(), "ABC", 24).getFamily(Locale.ENGLISH));
        }
    }
}