Develop and Download Open Source Software

Browse Subversion Repository

Contents of /dvibrowser-1.2.0/trunk/dvicore/src/main/java/jp/sourceforge/dvibrowser/dvicore/font/AWTDynamicPkFont.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 123 - (show annotations) (download) (as text)
Fri Mar 4 13:56:55 2011 UTC (13 years, 2 months ago) by nagaotakeyuki
File MIME type: text/x-java
File size: 7053 byte(s)
Applet support.
1 /*
2 * Copyright (c) 2009, Takeyuki Nagao
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * * Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the
11 * following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 */
32
33 package jp.sourceforge.dvibrowser.dvicore.font;
34 import java.awt.Font;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics2D;
37 import java.awt.geom.Rectangle2D;
38 import java.awt.image.BufferedImage;
39 import java.awt.image.DataBufferByte;
40 import java.awt.image.Raster;
41 import java.util.logging.Logger;
42
43 import jp.sourceforge.dvibrowser.dvicore.DviException;
44 import jp.sourceforge.dvibrowser.dvicore.DviFontSpec;
45 import jp.sourceforge.dvibrowser.dvicore.DviResolution;
46 import jp.sourceforge.dvibrowser.dvicore.DviUnit;
47 import jp.sourceforge.dvibrowser.dvicore.api.CharacterCodeMapper;
48 import jp.sourceforge.dvibrowser.dvicore.api.DviContextSupport;
49 import jp.sourceforge.dvibrowser.dvicore.util.DviUtils;
50
51
52 public class AWTDynamicPkFont
53 extends AbstractDynamicPkFont
54 {
55 private static final Logger LOGGER = Logger.getLogger(AWTDynamicPkFont.class.getName());
56
57 private static final long serialVersionUID = 6218737476095318238L;
58 private final Font font;
59 private CharacterCodeMapper mapper;
60 private boolean renderBoundigBoxes = false;
61
62 public AWTDynamicPkFont(DviContextSupport dcs, Font font)
63 throws DviException
64 {
65 this(dcs, font, null);
66 }
67 public AWTDynamicPkFont(DviContextSupport dcs, Font font, CharacterCodeMapper mapper)
68 throws DviException
69 {
70 super(dcs);
71 if (font == null)
72 throw new NullPointerException
73 ("font is null");
74 this.font = font;
75 this.setCharacterCodeMapper(mapper);
76 }
77
78 public boolean hasChar(int code) { return font.canDisplay(code); }
79
80 protected String mapToUnicode(LogicalFont lf, int code) throws DviException
81 {
82 if (getCharacterCodeMapper() != null) {
83 return getCharacterCodeMapper().mapCharacterCodeToUnicode(lf, code);
84 } else {
85 return getDviContext().getCharacterCodeMapper(lf)
86 .mapCharacterCodeToUnicode(lf, code);
87 }
88 }
89
90 protected PkGlyph generatePkGlyph(LogicalFont lf, int code)
91 throws DviException
92 {
93 String unicode = mapToUnicode(lf, code);
94
95 LOGGER.finest("str=(" + unicode + ") code=0x" + Integer.toHexString(code)
96 + " hex=" + DviUtils.hexDump(unicode));
97
98 Graphics2D g;
99 BufferedImage img;
100
101 // We instantiate an image to get Graphics2D.
102 img = new BufferedImage(
103 1, 1,
104 BufferedImage.TYPE_BYTE_GRAY
105 );
106
107 DviUnit dviUnit = lf.dviUnit();
108 DviFontSpec fs = lf.fontSpec();
109 DviResolution res = lf.resolution();
110 float fontSize = (float) dviUnit.mapToPixelDouble(fs.spaceSize(), res.dpi());
111 Font derivedFont = font.deriveFont(fontSize);
112
113 // System.out.println(Integer.toHexString(code) + "(" + unicode + "): " + derivedFont);
114
115 g = img.createGraphics();
116 FontMetrics fm = g.getFontMetrics(derivedFont);
117 int descent = fm.getMaxDescent();
118 int ascent = fm.getMaxAscent();
119 int leading = fm.getLeading();
120 int maxAdvance = fm.getMaxAdvance();
121 int maxAscent = fm.getMaxAscent();
122 int maxDescent = fm.getMaxDescent();
123
124 Rectangle2D charBounds = fm.getMaxCharBounds(g);
125 Rectangle2D bounds = fm.getStringBounds(unicode, g);
126 Rectangle2D boundsByChars = fm.getStringBounds(unicode.toCharArray(), 0, 1, g);
127 int x = (int) Math.floor(bounds.getMinX());
128 int y = (int) Math.floor(bounds.getMinY());
129 int width = (int) (bounds.getWidth() + 0.5);
130 int height = (int) (bounds.getHeight() + 0.5);
131 //height = maxAscent + maxDescent;
132 height = (int)(charBounds.getHeight() + 0.5);
133
134 int padding = 600;
135
136 // padding = Math.max(maxAscent, maxDescent);
137 padding = (int) (charBounds.getHeight() + 0.5);
138
139 y = - maxAscent;
140
141 x += -padding - maxAdvance;
142 y += -padding;
143 width += padding * 2 + maxAdvance * 2;
144 height += padding * 2;
145
146 int bw = width + 1;
147 int bh = height + 1;
148
149 // System.out.println("codePoint=" + String.format("x0%06x", code));
150 // System.out.println("bounds=" + bounds);
151 // System.out.println("charBounds=" + charBounds);
152 // System.out.println("padding=" + padding);
153 // System.out.println("boundsByChars=" + boundsByChars);
154 // System.out.println("maxAdvance=" + maxAdvance);
155 // System.out.println("ascent=" + ascent + " max=" + fm.getMaxAscent());
156 // System.out.println("descent=" + descent + " max=" + fm.getMaxDescent());
157 // System.out.println("leading=" + leading);
158 // System.out.println("width=" + width);
159 // System.out.println("height=" + height);
160 // System.out.println("(x, y)=(" + x + "," + y + ")");
161 g.dispose();
162 g = null;
163
164 img = new BufferedImage(
165 bw, bh,
166 BufferedImage.TYPE_BYTE_GRAY
167 );
168 g = img.createGraphics();
169 g.setFont(derivedFont);
170 g.drawString(unicode, -x, -y);
171 if (renderBoundigBoxes()) {
172 g.drawRect(0, 0, width - 1, height - 1);
173 g.drawRect(0, 0, width - 1 , -y - 1);
174 g.drawRect(0, 0, -x - 1, height -1);
175 }
176 g.dispose();
177
178 Raster raster = img.getRaster();
179 DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
180 RunLengthEncodedGlyph rlg = RunLengthEncodedGlyph.readByteGray(
181 data.getData(),
182 bw, bh,
183 -x, -y
184 );
185 PkGlyph glyph = rlg.toPkGlyph();
186 // System.out.println(code);
187 // BinaryDevice out = new dvi.render.DumpBinaryDevice(System.out);
188 // glyph.rasterizeTo(out);
189 return glyph;
190 }
191 public void setRenderBoundigBoxes(boolean renderBoundigBoxes) {
192 this.renderBoundigBoxes = renderBoundigBoxes;
193 }
194 public boolean renderBoundigBoxes() {
195 return renderBoundigBoxes;
196 }
197 public CharacterCodeMapper getCharacterCodeMapper() {
198 return mapper;
199 }
200 public void setCharacterCodeMapper(CharacterCodeMapper mapper) {
201 this.mapper = mapper;
202 }
203 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26