| 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 | */
|
|---|
| 36 | package com.kitfox.svg.util;
|
|---|
| 37 |
|
|---|
| 38 | import com.kitfox.svg.Font;
|
|---|
| 39 | import com.kitfox.svg.FontFace;
|
|---|
| 40 | import com.kitfox.svg.Glyph;
|
|---|
| 41 | import com.kitfox.svg.MissingGlyph;
|
|---|
| 42 | import java.awt.Canvas;
|
|---|
| 43 | import java.awt.FontMetrics;
|
|---|
| 44 | import java.awt.GraphicsEnvironment;
|
|---|
| 45 | import java.awt.font.FontRenderContext;
|
|---|
| 46 | import java.awt.font.GlyphMetrics;
|
|---|
| 47 | import java.awt.font.GlyphVector;
|
|---|
| 48 | import java.util.HashMap;
|
|---|
| 49 | import java.util.HashSet;
|
|---|
| 50 | import java.util.Locale;
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | *
|
|---|
| 54 | * @author kitfox
|
|---|
| 55 | */
|
|---|
| 56 | public 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(",", -1);
|
|---|
| 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 | }
|
|---|