Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / awt / peer / ClasspathFontPeer.java
blob78ab3a9de21144ce6e276e905ebd0085566bd71f
1 /* ClasspathFontPeer.java -- Font peer used by GNU Classpath.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.awt.peer;
41 import gnu.java.awt.ClasspathToolkit;
43 import java.awt.Font;
44 import java.awt.FontMetrics;
45 import java.awt.Toolkit;
46 import java.awt.font.FontRenderContext;
47 import java.awt.font.GlyphVector;
48 import java.awt.font.LineMetrics;
49 import java.awt.font.TextAttribute;
50 import java.awt.font.TransformAttribute;
51 import java.awt.geom.AffineTransform;
52 import java.awt.geom.Rectangle2D;
53 import java.awt.peer.FontPeer;
54 import java.text.AttributedCharacterIterator;
55 import java.text.CharacterIterator;
56 import java.util.HashMap;
57 import java.util.Locale;
58 import java.util.Map;
60 /**
61 * A peer for fonts that are used inside Classpath. The purpose of
62 * this interface is to abstract from platform-specific font handling
63 * in the Classpath implementation of java.awt.Font and related
64 * classes.
66 * <p><b>State kept by the peer:</b> a peer is generated for each Font
67 * object in the default implementation. If you wish to share peers between
68 * fonts, you will need to subclass both ClasspathFontPeer and
69 * {@link ClasspathToolKit}.</p>
71 * <p><b>Thread Safety:</b> Methods of this interface may be called
72 * from arbitrary threads at any time. Implementations of the
73 * <code>ClasspathFontPeer</code> interface are required to perform
74 * the necessary synchronization.</p>
76 * @see java.awt.Font#getPeer
77 * @see java.awt.Toolkit#getFontPeer
79 * @author Sascha Brawer (brawer@dandelis.ch)
80 * @author Graydon Hoare (graydon@redhat.com)
82 public abstract class ClasspathFontPeer
83 implements FontPeer
86 /*************************************************************************/
89 * Instance Variables
92 /**
93 * The 3 names of this font. all fonts have 3 names, some of which
94 * may be equal:
96 * logical -- name the font was constructed from
97 * family -- a designer or brand name (Helvetica)
98 * face -- specific instance of a design (Helvetica Regular)
100 * @see isLogicalFontName
103 protected String logicalName;
104 protected String familyName;
105 protected String faceName;
108 * The font style, which is a combination (by OR-ing) of the font style
109 * constants PLAIN, BOLD and ITALIC, in this class.
111 protected int style;
114 * The font point size. A point is 1/72 of an inch.
116 protected float size;
119 * The affine transformation the font is currently subject to.
121 protected AffineTransform transform;
123 protected static ClasspathToolkit tk()
125 return (ClasspathToolkit)(Toolkit.getDefaultToolkit ());
129 * Confusingly, a Logical Font is a concept unrelated to
130 * a Font's Logical Name.
132 * A Logical Font is one of 6 built-in, abstract font types
133 * which must be supported by any java environment: SansSerif,
134 * Serif, Monospaced, Dialog, and DialogInput.
136 * A Font's Logical Name is the name the font was constructed
137 * from. This might be the name of a Logical Font, or it might
138 * be the name of a Font Face.
141 protected static boolean isLogicalFontName(String name)
143 String uname = name.toUpperCase ();
144 return (uname.equals ("SANSSERIF") ||
145 uname.equals ("SERIF") ||
146 uname.equals ("MONOSPACED") ||
147 uname.equals ("DIALOG") ||
148 uname.equals ("DIALOGINPUT"));
151 protected static String logicalFontNameToFaceName (String name)
153 String uname = name.toUpperCase ();
154 if (uname.equals("SANSSERIF"))
155 return "Helvetica";
156 else if (uname.equals ("SERIF"))
157 return "Times";
158 else if (uname.equals ("MONOSPACED"))
159 return "Courier";
160 else if (uname.equals ("DIALOG"))
161 return "Helvetica";
162 else if (uname.equals ("DIALOGINPUT"))
163 return "Helvetica";
164 else
165 return "Helvetica";
168 protected static String faceNameToFamilyName (String name)
170 return name;
173 public static void copyStyleToAttrs (int style, Map attrs)
175 if ((style & Font.BOLD) == Font.BOLD)
176 attrs.put (TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
177 else
178 attrs.put (TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
180 if ((style & Font.ITALIC) == Font.ITALIC)
181 attrs.put (TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
182 else
183 attrs.put (TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
186 protected static void copyFamilyToAttrs (String fam, Map attrs)
188 if (fam != null)
189 attrs.put (TextAttribute.FAMILY, fam);
192 public static void copySizeToAttrs (float size, Map attrs)
194 attrs.put (TextAttribute.SIZE, new Float (size));
197 protected static void copyTransformToAttrs (AffineTransform trans, Map attrs)
199 if (trans != null)
200 attrs.put(TextAttribute.TRANSFORM, new TransformAttribute (trans));
204 protected void setStandardAttributes (String name, String family, int style,
205 float size, AffineTransform trans)
207 this.logicalName = name;
209 if (isLogicalFontName (name))
210 this.faceName = logicalFontNameToFaceName (name);
211 else
212 this.faceName = name;
214 if (family != null)
215 this.familyName = family;
216 else
217 this.familyName = faceNameToFamilyName (faceName);
219 this.style = style;
220 this.size = size;
221 this.transform = trans;
225 protected void setStandardAttributes (String name, Map attribs)
227 String family = this.familyName;
228 AffineTransform trans = this.transform;
229 float size = this.size;
230 int style = this.style;
232 if (attribs.containsKey (TextAttribute.FAMILY))
233 family = (String) attribs.get (TextAttribute.FAMILY);
235 if (name == null)
236 name = "SansSerif";
238 if (attribs.containsKey (TextAttribute.WEIGHT))
240 Float weight = (Float) attribs.get (TextAttribute.WEIGHT);
241 if (weight.floatValue () >= TextAttribute.WEIGHT_BOLD.floatValue ())
242 style += Font.BOLD;
245 if (attribs.containsKey (TextAttribute.POSTURE))
247 Float posture = (Float) attribs.get (TextAttribute.POSTURE);
248 if (posture.floatValue () >= TextAttribute.POSTURE_OBLIQUE.floatValue ())
249 style += Font.ITALIC;
252 if (attribs.containsKey (TextAttribute.SIZE))
254 Float sz = (Float) attribs.get (TextAttribute.SIZE);
255 size = sz.floatValue ();
257 // Pango doesn't accept 0 as a font size.
258 if (size < 1)
259 size = 1;
261 else
262 size = 12;
264 if (attribs.containsKey (TextAttribute.TRANSFORM))
266 TransformAttribute ta = (TransformAttribute)
267 attribs.get(TextAttribute.TRANSFORM);
268 trans = ta.getTransform ();
271 setStandardAttributes (name, family, style, size, trans);
274 protected void getStandardAttributes (Map attrs)
276 copyFamilyToAttrs (this.familyName, attrs);
277 copySizeToAttrs (this.size, attrs);
278 copyStyleToAttrs (this.style, attrs);
279 copyTransformToAttrs (this.transform, attrs);
283 /* Begin public API */
285 public ClasspathFontPeer (String name, Map attrs)
287 setStandardAttributes (name, attrs);
290 public ClasspathFontPeer (String name, int style, int size)
292 setStandardAttributes (name, (String)null, style,
293 (float)size, (AffineTransform)null);
296 /**
297 * Implementation of {@link Font#getName}
299 * @param font the font this peer is being called from. This may be
300 * useful if you are sharing peers between Font objects. Otherwise it may
301 * be ignored.
304 public String getName (Font font)
306 return logicalName;
309 /**
310 * Implementation of {@link Font#getFamily()}
312 * @param font the font this peer is being called from. This may be
313 * useful if you are sharing peers between Font objects. Otherwise it may
314 * be ignored.
317 public String getFamily (Font font)
319 return familyName;
322 /**
323 * Implementation of {@link Font#getFamily(Locale)}
325 * @param font the font this peer is being called from. This may be
326 * useful if you are sharing peers between Font objects. Otherwise it may
327 * be ignored.
330 public String getFamily (Font font, Locale lc)
332 return familyName;
335 /**
336 * Implementation of {@link Font#getFontName()}
338 * @param font the font this peer is being called from. This may be
339 * useful if you are sharing peers between Font objects. Otherwise it may
340 * be ignored.
343 public String getFontName (Font font)
345 return faceName;
348 /**
349 * Implementation of {@link Font#getFontName(Locale)}
351 * @param font the font this peer is being called from. This may be
352 * useful if you are sharing peers between Font objects. Otherwise it may
353 * be ignored.
356 public String getFontName (Font font, Locale lc)
358 return faceName;
361 /**
362 * Implementation of {@link Font#getSize}
364 * @param font the font this peer is being called from. This may be
365 * useful if you are sharing peers between Font objects. Otherwise it may
366 * be ignored.
369 public float getSize (Font font)
371 return size;
374 /**
375 * Implementation of {@link Font#isPlain}
377 * @param font the font this peer is being called from. This may be
378 * useful if you are sharing peers between Font objects. Otherwise it may
379 * be ignored.
382 public boolean isPlain (Font font)
384 return style == Font.PLAIN;
387 /**
388 * Implementation of {@link Font#isBold}
390 * @param font the font this peer is being called from. This may be
391 * useful if you are sharing peers between Font objects. Otherwise it may
392 * be ignored.
395 public boolean isBold (Font font)
397 return ((style & Font.BOLD) == Font.BOLD);
400 /**
401 * Implementation of {@link Font#isItalic}
403 * @param font the font this peer is being called from. This may be
404 * useful if you are sharing peers between Font objects. Otherwise it may
405 * be ignored.
408 public boolean isItalic (Font font)
410 return ((style & Font.ITALIC) == Font.ITALIC);
413 /**
414 * Implementation of {@link Font#deriveFont(int, float)}
416 * @param font the font this peer is being called from. This may be
417 * useful if you are sharing peers between Font objects. Otherwise it may
418 * be ignored.
421 public Font deriveFont (Font font, int style, float size)
423 Map attrs = new HashMap ();
424 getStandardAttributes (attrs);
425 copyStyleToAttrs (style, attrs);
426 copySizeToAttrs (size, attrs);
427 return tk().getFont (logicalName, attrs);
430 /**
431 * Implementation of {@link Font#deriveFont(float)}
433 * @param font the font this peer is being called from. This may be
434 * useful if you are sharing peers between Font objects. Otherwise it may
435 * be ignored.
438 public Font deriveFont (Font font, float size)
440 Map attrs = new HashMap ();
441 getStandardAttributes (attrs);
442 copySizeToAttrs (size, attrs);
443 return tk().getFont (logicalName, attrs);
446 /**
447 * Implementation of {@link Font#deriveFont(int)}
449 * @param font the font this peer is being called from. This may be
450 * useful if you are sharing peers between Font objects. Otherwise it may
451 * be ignored.
454 public Font deriveFont (Font font, int style)
456 Map attrs = new HashMap ();
457 getStandardAttributes (attrs);
458 copyStyleToAttrs (style, attrs);
459 return tk().getFont (logicalName, attrs);
462 /**
463 * Implementation of {@link Font#deriveFont(int, AffineTransform)}
465 * @param font the font this peer is being called from. This may be
466 * useful if you are sharing peers between Font objects. Otherwise it may
467 * be ignored.
470 public Font deriveFont (Font font, int style, AffineTransform t)
472 Map attrs = new HashMap ();
473 getStandardAttributes (attrs);
474 copyStyleToAttrs (style, attrs);
475 copyTransformToAttrs (t, attrs);
476 return tk().getFont (logicalName, attrs);
479 /**
480 * Implementation of {@link Font#deriveFont(AffineTransform)}
482 * @param font the font this peer is being called from. This may be
483 * useful if you are sharing peers between Font objects. Otherwise it may
484 * be ignored.
487 public Font deriveFont (Font font, AffineTransform t)
489 Map attrs = new HashMap ();
490 getStandardAttributes (attrs);
491 copyTransformToAttrs (t, attrs);
492 return tk().getFont (logicalName, attrs);
495 /**
496 * Implementation of {@link Font#deriveFont(Map)}
498 * @param font the font this peer is being called from. This may be
499 * useful if you are sharing peers between Font objects. Otherwise it may
500 * be ignored.
503 public Font deriveFont (Font font, Map attrs)
505 return tk().getFont (logicalName, attrs);
508 /**
509 * Implementation of {@link Font#getAttributes()}
511 * @param font the font this peer is being called from. This may be
512 * useful if you are sharing peers between Font objects. Otherwise it may
513 * be ignored.
516 public Map getAttributes (Font font)
518 HashMap h = new HashMap ();
519 getStandardAttributes (h);
520 return h;
523 /**
524 * Implementation of {@link Font#getAvailableAttributes()}
526 * @param font the font this peer is being called from. This may be
527 * useful if you are sharing peers between Font objects. Otherwise it may
528 * be ignored.
531 public AttributedCharacterIterator.Attribute[] getAvailableAttributes(Font font)
533 AttributedCharacterIterator.Attribute a[] =
534 new AttributedCharacterIterator.Attribute[5];
535 a[0] = TextAttribute.FAMILY;
536 a[1] = TextAttribute.SIZE;
537 a[2] = TextAttribute.POSTURE;
538 a[3] = TextAttribute.WEIGHT;
539 a[4] = TextAttribute.TRANSFORM;
540 return a;
543 /**
544 * Implementation of {@link Font#getTransform()}
546 * @param font the font this peer is being called from. This may be
547 * useful if you are sharing peers between Font objects. Otherwise it may
548 * be ignored.
551 public AffineTransform getTransform (Font font)
553 if (transform == null)
554 transform = new AffineTransform ();
555 return transform;
558 /**
559 * Implementation of {@link Font#isTransformed()}
561 * @param font the font this peer is being called from. This may be
562 * useful if you are sharing peers between Font objects. Otherwise it may
563 * be ignored.
566 public boolean isTransformed (Font font)
568 return ! transform.isIdentity ();
571 /**
572 * Implementation of {@link Font#getItalicAngle()}
574 * @param font the font this peer is being called from. This may be
575 * useful if you are sharing peers between Font objects. Otherwise it may
576 * be ignored.
579 public float getItalicAngle (Font font)
581 if ((style & Font.ITALIC) == Font.ITALIC)
582 return TextAttribute.POSTURE_OBLIQUE.floatValue ();
583 else
584 return TextAttribute.POSTURE_REGULAR.floatValue ();
588 /**
589 * Implementation of {@link Font#getStyle()}
591 * @param font the font this peer is being called from. This may be
592 * useful if you are sharing peers between Font objects. Otherwise it may
593 * be ignored.
596 public int getStyle (Font font)
598 return style;
604 /* Remaining methods are abstract */
606 /**
607 * Implementation of {@link Font#canDisplay(char)}
609 * @param font the font this peer is being called from. This may be
610 * useful if you are sharing peers between Font objects. Otherwise it may
611 * be ignored.
614 public abstract boolean canDisplay (Font font, char c);
616 /**
617 * Implementation of {@link Font#canDisplay(String)},
618 * {@link Font#canDisplay(char [], int, int)}, and
619 * {@link Font#canDisplay(CharacterIterator, int, int)}.
621 * @param font the font this peer is being called from. This may be
622 * useful if you are sharing peers between Font objects. Otherwise it may
623 * be ignored.
626 public abstract int canDisplayUpTo (Font font, CharacterIterator i, int start, int limit);
630 * Returns the name of this font face inside the family, for example
631 * <i>&#x201c;Light&#x201d;</i>.
633 * <p>This method is currently not used by {@link Font}. However,
634 * this name would be needed by any serious desktop publishing
635 * application.
637 * @param font the font whose sub-family name is requested.
639 * @param locale the locale for which to localize the name. If
640 * <code>locale</code> is <code>null</code>, the returned name is
641 * localized to the user&#x2019;s default locale.
643 * @return the name of the face inside its family, or
644 * <code>null</code> if the font does not provide a sub-family name.
647 public abstract String getSubFamilyName (Font font, Locale locale);
650 /**
651 * Implementation of {@link Font#getPSName()}
653 * @param font the font this peer is being called from. This may be
654 * useful if you are sharing peers between Font objects. Otherwise it may
655 * be ignored.
658 public abstract String getPostScriptName (Font font);
661 /**
662 * Implementation of {@link Font#getNumGlyphs()}
664 * @param font the font this peer is being called from. This may be
665 * useful if you are sharing peers between Font objects. Otherwise it may
666 * be ignored.
669 public abstract int getNumGlyphs (Font font);
672 /**
673 * Implementation of {@link Font#getMissingGlyphCode()}
675 * @param font the font this peer is being called from. This may be
676 * useful if you are sharing peers between Font objects. Otherwise it may
677 * be ignored.
680 public abstract int getMissingGlyphCode (Font font);
683 /**
684 * Implementation of {@link Font#getBaselineFor(char)}
686 * @param font the font this peer is being called from. This may be
687 * useful if you are sharing peers between Font objects. Otherwise it may
688 * be ignored.
691 public abstract byte getBaselineFor (Font font, char c);
695 * Returns a name for the specified glyph. This is useful for
696 * generating PostScript or PDF files that embed some glyphs of a
697 * font. If the implementation follows glyph naming conventions
698 * specified by Adobe, search engines can extract the original text
699 * from the generated PostScript and PDF files.
701 * <p>This method is currently not used by GNU Classpath. However,
702 * it would be very useful for someone wishing to write a good
703 * PostScript or PDF stream provider for the
704 * <code>javax.print</code> package.
706 * <p><b>Names are not unique:</b> Under some rare circumstances,
707 * the same name can be returned for different glyphs. It is
708 * therefore recommended that printer drivers check whether the same
709 * name has already been returned for antoher glyph, and make the
710 * name unique by adding the string ".alt" followed by the glyph
711 * index.</p>
713 * <p>This situation would occur for an OpenType or TrueType font
714 * that has a <code>post</code> table of format 3 and provides a
715 * mapping from glyph IDs to Unicode sequences through a
716 * <code>Zapf</code> table. If the same sequence of Unicode
717 * codepoints leads to different glyphs (depending on contextual
718 * position, for example, or on typographic sophistication level),
719 * the same name would get synthesized for those glyphs. To avoid
720 * this, the font peer would have to go through the names of all
721 * glyphs, which would make this operation very inefficient with
722 * large fonts.
724 * @param font the font containing the glyph whose name is
725 * requested.
727 * @param glyphIndex the glyph whose name the caller wants to
728 * retrieve.
730 * @return the glyph name, or <code>null</code> if a font does not
731 * provide glyph names.
734 public abstract String getGlyphName (Font font, int glyphIndex);
737 /**
738 * Implementation of {@link
739 * Font#createGlyphVector(FontRenderContext, String)}, {@link
740 * Font#createGlyphVector(FontRenderContext, char[])}, and {@link
741 * Font#createGlyphVector(FontRenderContext, CharacterIterator)}.
743 * @param font the font object that the created GlyphVector will return
744 * when it gets asked for its font. This argument is needed because the
745 * public API of {@link GlyphVector} works with {@link java.awt.Font},
746 * not with font peers.
749 public abstract GlyphVector createGlyphVector (Font font,
750 FontRenderContext frc,
751 CharacterIterator ci);
754 /**
755 * Implementation of {@link Font#createGlyphVector(FontRenderContext,
756 * int[])}.
758 * @param font the font object that the created GlyphVector will return
759 * when it gets asked for its font. This argument is needed because the
760 * public API of {@link GlyphVector} works with {@link java.awt.Font},
761 * not with font peers.
764 public abstract GlyphVector createGlyphVector (Font font,
765 FontRenderContext ctx,
766 int[] glyphCodes);
769 /**
770 * Implementation of {@link Font#layoutGlyphVector(FontRenderContext,
771 * char[], int, int, int)}.
773 * @param font the font object that the created GlyphVector will return
774 * when it gets asked for its font. This argument is needed because the
775 * public API of {@link GlyphVector} works with {@link java.awt.Font},
776 * not with font peers.
779 public abstract GlyphVector layoutGlyphVector (Font font,
780 FontRenderContext frc,
781 char[] chars, int start,
782 int limit, int flags);
785 /**
786 * Implementation of {@link Font#getFontMetrics()}
788 * @param font the font this peer is being called from. This may be
789 * useful if you are sharing peers between Font objects. Otherwise it may
790 * be ignored.
793 public abstract FontMetrics getFontMetrics (Font font);
796 /**
797 * Implementation of {@link Font#hasUniformLineMetrics()}
799 * @param font the font this peer is being called from. This may be
800 * useful if you are sharing peers between Font objects. Otherwise it may
801 * be ignored.
804 public abstract boolean hasUniformLineMetrics (Font font);
807 /**
808 * Implementation of {@link Font#getLineMetrics(CharacterIterator, int,
809 * int, FontRenderContext)}
811 * @param font the font this peer is being called from. This may be
812 * useful if you are sharing peers between Font objects. Otherwise it may
813 * be ignored.
816 public abstract LineMetrics getLineMetrics (Font font,
817 CharacterIterator ci,
818 int begin, int limit,
819 FontRenderContext rc);
821 /**
822 * Implementation of {@link Font#getMaxCharBounds(FontRenderContext)}
824 * @param font the font this peer is being called from. This may be
825 * useful if you are sharing peers between Font objects. Otherwise it may
826 * be ignored.
829 public abstract Rectangle2D getMaxCharBounds (Font font,
830 FontRenderContext rc);
832 /**
833 * Implementation of {@link Font#getStringBounds(CharacterIterator, int,
834 * int, FontRenderContext)}
836 * @param font the font this peer is being called from. This may be
837 * useful if you are sharing peers between Font objects. Otherwise it may
838 * be ignored.
841 public abstract Rectangle2D getStringBounds (Font font,
842 CharacterIterator ci,
843 int begin, int limit,
844 FontRenderContext frc);