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

Last change on this file since 10746 was 8084, checked in by bastiK, 10 years ago

add svn:eol-style=native for svgsalamander

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package com.kitfox.svg.util;
7
8import com.kitfox.svg.Font;
9import com.kitfox.svg.FontFace;
10import com.kitfox.svg.Glyph;
11import com.kitfox.svg.MissingGlyph;
12import java.awt.Canvas;
13import java.awt.FontMetrics;
14import java.awt.font.FontRenderContext;
15import java.awt.font.GlyphMetrics;
16import java.awt.font.GlyphVector;
17import java.util.HashMap;
18
19/**
20 *
21 * @author kitfox
22 */
23public class FontSystem extends Font
24{
25 java.awt.Font sysFont;
26 FontMetrics fm;
27
28 HashMap glyphCache = new HashMap();
29
30 public FontSystem(String fontFamily, int fontStyle, int fontWeight, int fontSize)
31 {
32 int style;
33 switch (fontStyle)
34 {
35 case com.kitfox.svg.Text.TXST_ITALIC:
36 style = java.awt.Font.ITALIC;
37 break;
38 default:
39 style = java.awt.Font.PLAIN;
40 break;
41 }
42
43 int weight;
44 switch (fontWeight)
45 {
46 case com.kitfox.svg.Text.TXWE_BOLD:
47 case com.kitfox.svg.Text.TXWE_BOLDER:
48 weight = java.awt.Font.BOLD;
49 break;
50 default:
51 weight = java.awt.Font.PLAIN;
52 break;
53 }
54 sysFont = new java.awt.Font(fontFamily, style | weight, (int) fontSize);
55
56 Canvas c = new Canvas();
57 fm = c.getFontMetrics(sysFont);
58
59 FontFace face = new FontFace();
60 face.setAscent(fm.getAscent());
61 face.setDescent(fm.getDescent());
62 face.setUnitsPerEm(fm.charWidth('M'));
63 setFontFace(face);
64 }
65
66 public MissingGlyph getGlyph(String unicode)
67 {
68 FontRenderContext frc = new FontRenderContext(null, true, true);
69 GlyphVector vec = sysFont.createGlyphVector(frc, unicode);
70
71 Glyph glyph = (Glyph)glyphCache.get(unicode);
72 if (glyph == null)
73 {
74 glyph = new Glyph();
75 glyph.setPath(vec.getGlyphOutline(0));
76
77 GlyphMetrics gm = vec.getGlyphMetrics(0);
78 glyph.setHorizAdvX((int)gm.getAdvanceX());
79 glyph.setVertAdvY((int)gm.getAdvanceY());
80 glyph.setVertOriginX(0);
81 glyph.setVertOriginY(0);
82
83 glyphCache.put(unicode, glyph);
84 }
85
86 return glyph;
87 }
88
89
90}
Note: See TracBrowser for help on using the repository browser.