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 | */
|
---|
6 | package com.kitfox.svg.util;
|
---|
7 |
|
---|
8 | import com.kitfox.svg.Font;
|
---|
9 | import com.kitfox.svg.FontFace;
|
---|
10 | import com.kitfox.svg.Glyph;
|
---|
11 | import com.kitfox.svg.MissingGlyph;
|
---|
12 | import java.awt.Canvas;
|
---|
13 | import java.awt.FontMetrics;
|
---|
14 | import java.awt.font.FontRenderContext;
|
---|
15 | import java.awt.font.GlyphMetrics;
|
---|
16 | import java.awt.font.GlyphVector;
|
---|
17 | import java.util.HashMap;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | *
|
---|
21 | * @author kitfox
|
---|
22 | */
|
---|
23 | public 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 | }
|
---|