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

Last change on this file since 14328 was 14328, checked in by Don-vip, 7 years ago

see #14319, see #16838 - update to svgSalamander 1.1.2

  • Property svn:eol-style set to native
File size: 4.7 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;
50
51/**
52 *
53 * @author kitfox
54 */
55public class FontSystem extends Font
56{
57 java.awt.Font sysFont;
58 FontMetrics fm;
59
60 HashMap<String, Glyph> glyphCache = new HashMap<String, Glyph>();
61
62 static HashSet<String> sysFontNames = new HashSet<String>();
63
64 public static boolean checkIfSystemFontExists(String fontName)
65 {
66 if (sysFontNames.isEmpty())
67 {
68 for (String name: GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())
69 {
70 sysFontNames.add(name);
71 }
72 }
73
74 return sysFontNames.contains(fontName);
75 }
76
77 public static FontSystem createFont(String fontFamily, int fontStyle, int fontWeight, int fontSize)
78 {
79 String[] families = fontFamily.split(",");
80 for (String fontName: families)
81 {
82 if (checkIfSystemFontExists(fontName))
83 {
84 return new FontSystem(fontName, fontStyle, fontWeight, fontSize);
85 }
86 }
87
88 return null;
89 }
90
91 private FontSystem(String fontFamily, int fontStyle, int fontWeight, int fontSize)
92 {
93 int style;
94 switch (fontStyle)
95 {
96 case com.kitfox.svg.Text.TXST_ITALIC:
97 style = java.awt.Font.ITALIC;
98 break;
99 default:
100 style = java.awt.Font.PLAIN;
101 break;
102 }
103
104 int weight;
105 switch (fontWeight)
106 {
107 case com.kitfox.svg.Text.TXWE_BOLD:
108 case com.kitfox.svg.Text.TXWE_BOLDER:
109 weight = java.awt.Font.BOLD;
110 break;
111 default:
112 weight = java.awt.Font.PLAIN;
113 break;
114 }
115
116 sysFont = new java.awt.Font(fontFamily, style | weight, fontSize);
117
118 Canvas c = new Canvas();
119 fm = c.getFontMetrics(sysFont);
120
121 FontFace face = new FontFace();
122 face.setAscent(fm.getAscent());
123 face.setDescent(fm.getDescent());
124 face.setUnitsPerEm(fm.charWidth('M'));
125 setFontFace(face);
126 }
127
128 @Override
129 public MissingGlyph getGlyph(String unicode)
130 {
131 FontRenderContext frc = new FontRenderContext(null, true, true);
132 GlyphVector vec = sysFont.createGlyphVector(frc, unicode);
133
134 Glyph glyph = glyphCache.get(unicode);
135 if (glyph == null)
136 {
137 glyph = new Glyph();
138 glyph.setPath(vec.getGlyphOutline(0));
139
140 GlyphMetrics gm = vec.getGlyphMetrics(0);
141 glyph.setHorizAdvX(gm.getAdvanceX());
142 glyph.setVertAdvY(gm.getAdvanceY());
143 glyph.setVertOriginX(0);
144 glyph.setVertOriginY(0);
145
146 glyphCache.put(unicode, glyph);
147 }
148
149 return glyph;
150 }
151
152
153}
Note: See TracBrowser for help on using the repository browser.