1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: FontInfoBuilder.java 1885366 2021-01-11 15:00:20Z ssteiner $ */
19 
20 package org.apache.fop.svg.font;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 
27 import org.apache.fop.apps.io.InternalResourceResolver;
28 import org.apache.fop.apps.io.ResourceResolverFactory;
29 import org.apache.fop.fonts.EmbeddingMode;
30 import org.apache.fop.fonts.EncodingMode;
31 import org.apache.fop.fonts.Font;
32 import org.apache.fop.fonts.FontInfo;
33 import org.apache.fop.fonts.FontMetrics;
34 import org.apache.fop.fonts.truetype.OFFontLoader;
35 
36 class FontInfoBuilder {
37 
38     public static final String DEJAVU_LGC_SERIF = "DejaVu LGC Serif";
39 
40     public static final String DROID_SANS_MONO = "Droid Sans Mono";
41 
42     private static final boolean USE_ADVANCED_BY_DEFAULT = true;
43 
44     private FontInfo fontInfo;
45 
46     private int fontKey;
47 
FontInfoBuilder()48     public FontInfoBuilder() {
49         reset();
50     }
51 
reset()52     private void reset() {
53         fontInfo = new FontInfo();
54         fontKey = 1;
55     }
56 
useDejaVuLGCSerif()57     public FontInfoBuilder useDejaVuLGCSerif() {
58         return useDejaVuLGCSerif(USE_ADVANCED_BY_DEFAULT);
59     }
60 
useDejaVuLGCSerif(boolean useAdvanced)61     public FontInfoBuilder useDejaVuLGCSerif(boolean useAdvanced) {
62         try {
63             return useFont(DEJAVU_LGC_SERIF, "DejaVuLGCSerif.ttf", useAdvanced);
64         } catch (Exception e) {
65             throw new RuntimeException(e);
66         }
67     }
68 
useDroidSansMono()69     public FontInfoBuilder useDroidSansMono() {
70         return useDroidSansMono(USE_ADVANCED_BY_DEFAULT);
71     }
72 
useDroidSansMono(boolean useAdvanced)73     public FontInfoBuilder useDroidSansMono(boolean useAdvanced) {
74         try {
75             return useFont(DROID_SANS_MONO, "DroidSansMono.ttf", useAdvanced);
76         } catch (Exception e) {
77             throw new RuntimeException(e);
78         }
79     }
80 
useFont(String fontName, String filename, boolean useAdvanced)81     private FontInfoBuilder useFont(String fontName, String filename, boolean useAdvanced)
82             throws IOException, URISyntaxException {
83         URI baseURI = new File("test/resources/fonts/ttf").toURI();
84         InternalResourceResolver resolver = ResourceResolverFactory.createDefaultInternalResourceResolver(baseURI);
85         OFFontLoader fontLoader = new OFFontLoader(new URI(filename), null, true,
86                 EmbeddingMode.AUTO, EncodingMode.AUTO, true, useAdvanced, resolver, false, false, true);
87         FontMetrics font = fontLoader.getFont();
88         registerFont(font, "F" + fontKey++, fontName);
89         return this;
90     }
91 
registerFont(FontMetrics font, String key, String familyName)92     private void registerFont(FontMetrics font, String key, String familyName) {
93         fontInfo.addMetrics(key, font);
94         fontInfo.addFontProperties(key, familyName, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL);
95     }
96 
build()97     public FontInfo build() {
98         FontInfo fontInfo = this.fontInfo;
99         reset();
100         return fontInfo;
101     }
102 }
103