001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on April 24, 2015
035 */
036package com.kitfox.svg.util;
037
038import com.kitfox.svg.Font;
039import com.kitfox.svg.FontFace;
040import com.kitfox.svg.Glyph;
041import com.kitfox.svg.MissingGlyph;
042import java.awt.Canvas;
043import java.awt.FontMetrics;
044import java.awt.font.FontRenderContext;
045import java.awt.font.GlyphMetrics;
046import java.awt.font.GlyphVector;
047import java.util.HashMap;
048
049/**
050 *
051 * @author kitfox
052 */
053public class FontSystem extends Font
054{
055    java.awt.Font sysFont;
056    FontMetrics fm;
057
058    HashMap glyphCache = new HashMap();
059    
060    public FontSystem(String fontFamily, int fontStyle, int fontWeight, int fontSize)
061    {
062        int style;
063        switch (fontStyle)
064        {
065            case com.kitfox.svg.Text.TXST_ITALIC:
066                style = java.awt.Font.ITALIC;
067                break;
068            default:
069                style = java.awt.Font.PLAIN;
070                break;
071        }
072
073        int weight;
074        switch (fontWeight)
075        {
076            case com.kitfox.svg.Text.TXWE_BOLD:
077            case com.kitfox.svg.Text.TXWE_BOLDER:
078                weight = java.awt.Font.BOLD;
079                break;
080            default:
081                weight = java.awt.Font.PLAIN;
082                break;
083        }
084        sysFont = new java.awt.Font(fontFamily, style | weight, (int) fontSize);
085        
086        Canvas c = new Canvas();
087        fm = c.getFontMetrics(sysFont);
088        
089        FontFace face = new FontFace();
090        face.setAscent(fm.getAscent());
091        face.setDescent(fm.getDescent());
092        face.setUnitsPerEm(fm.charWidth('M'));
093        setFontFace(face);
094    }
095
096    public MissingGlyph getGlyph(String unicode)
097    {
098        FontRenderContext frc = new FontRenderContext(null, true, true);
099        GlyphVector vec = sysFont.createGlyphVector(frc, unicode);
100        
101        Glyph glyph = (Glyph)glyphCache.get(unicode);
102        if (glyph == null)
103        {
104            glyph = new Glyph();
105            glyph.setPath(vec.getGlyphOutline(0));
106
107            GlyphMetrics gm = vec.getGlyphMetrics(0);
108            glyph.setHorizAdvX((int)gm.getAdvanceX());
109            glyph.setVertAdvY((int)gm.getAdvanceY());
110            glyph.setVertOriginX(0);
111            glyph.setVertOriginY(0);
112            
113            glyphCache.put(unicode, glyph);
114        }
115        
116        return glyph;
117    }
118    
119    
120}