source: josm/trunk/src/com/kitfox/svg/util/FontSystem.java@ 16086

Last change on this file since 16086 was 14331, checked in by Don-vip, 6 years ago

see #14319, see #16838 - fix regressions introduced in svgSalamander 1.1.2

see https://github.com/blackears/svgSalamander/issues/29

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
33 *
34 * Created on April 24, 2015
35 */
36package com.kitfox.svg.util;
37
38import com.kitfox.svg.Font;
39import com.kitfox.svg.FontFace;
40import com.kitfox.svg.Glyph;
41import com.kitfox.svg.MissingGlyph;
42import java.awt.Canvas;
43import java.awt.FontMetrics;
44import java.awt.GraphicsEnvironment;
45import java.awt.font.FontRenderContext;
46import java.awt.font.GlyphMetrics;
47import java.awt.font.GlyphVector;
48import java.util.HashMap;
49import java.util.HashSet;
50import java.util.Locale;
51
52/**
53 *
54 * @author kitfox
55 */
56public class FontSystem extends Font
57{
58 java.awt.Font sysFont;
59 FontMetrics fm;
60
61 HashMap<String, Glyph> glyphCache = new HashMap<>();
62
63 static HashSet<String> sysFontNames = new HashSet<>();
64
65 public static boolean checkIfSystemFontExists(String fontName)
66 {
67 if (sysFontNames.isEmpty())
68 {
69 for (String name: GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.ENGLISH))
70 {
71 sysFontNames.add(name);
72 }
73 }
74
75 return sysFontNames.contains(fontName);
76 }
77
78 public static FontSystem createFont(String fontFamily, int fontStyle, int fontWeight, int fontSize)
79 {
80 String[] families = fontFamily.split(",");
81 for (String fontName: families)
82 {
83 String javaFontName = mapJavaFontName(fontName);
84 if (checkIfSystemFontExists(javaFontName))
85 {
86 return new FontSystem(javaFontName, fontStyle, fontWeight, fontSize);
87 }
88 }
89
90 return null;
91 }
92
93 private static String mapJavaFontName(String fontName) {
94 if ("serif".equals(fontName)) {
95 return java.awt.Font.SERIF;
96 } else if ("sans-serif".equals(fontName)) {
97 return java.awt.Font.SANS_SERIF;
98 } else if ("monospace".equals(fontName)) {
99 return java.awt.Font.MONOSPACED;
100 } else {
101 return fontName;
102 }
103 }
104
105 private FontSystem(String fontFamily, int fontStyle, int fontWeight, int fontSize)
106 {
107 int style;
108 switch (fontStyle)
109 {
110 case com.kitfox.svg.Text.TXST_ITALIC:
111 style = java.awt.Font.ITALIC;
112 break;
113 default:
114 style = java.awt.Font.PLAIN;
115 break;
116 }
117
118 int weight;
119 switch (fontWeight)
120 {
121 case com.kitfox.svg.Text.TXWE_BOLD:
122 case com.kitfox.svg.Text.TXWE_BOLDER:
123 weight = java.awt.Font.BOLD;
124 break;
125 default:
126 weight = java.awt.Font.PLAIN;
127 break;
128 }
129
130 sysFont = new java.awt.Font(fontFamily, style | weight, fontSize);
131
132 Canvas c = new Canvas();
133 fm = c.getFontMetrics(sysFont);
134
135 FontFace face = new FontFace();
136 face.setAscent(fm.getAscent());
137 face.setDescent(fm.getDescent());
138 face.setUnitsPerEm(fm.charWidth('M'));
139 setFontFace(face);
140 }
141
142 @Override
143 public MissingGlyph getGlyph(String unicode)
144 {
145 FontRenderContext frc = new FontRenderContext(null, true, true);
146 GlyphVector vec = sysFont.createGlyphVector(frc, unicode);
147
148 Glyph glyph = glyphCache.get(unicode);
149 if (glyph == null)
150 {
151 glyph = new Glyph();
152 glyph.setPath(vec.getGlyphOutline(0));
153
154 GlyphMetrics gm = vec.getGlyphMetrics(0);
155 glyph.setHorizAdvX(gm.getAdvanceX());
156 glyph.setVertAdvY(gm.getAdvanceY());
157 glyph.setVertOriginX(0);
158 glyph.setVertOriginY(0);
159
160 glyphCache.put(unicode, glyph);
161 }
162
163 return glyph;
164 }
165
166
167}
Note: See TracBrowser for help on using the repository browser.