1 /*
   2  * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.io.File;
  29 import java.awt.Font;
  30 import java.util.Collection;
  31 import java.util.HashMap;
  32 import java.util.concurrent.ConcurrentHashMap;
  33 import java.util.Locale;
  34 
  35 public class FontFamily {
  36 
  37     private static ConcurrentHashMap<String, FontFamily>
  38         familyNameMap = new ConcurrentHashMap<String, FontFamily>();
  39     private static HashMap<String, FontFamily> allLocaleNames;
  40 
  41     protected String familyName;
  42     protected Font2D plain;
  43     protected Font2D bold;
  44     protected Font2D italic;
  45     protected Font2D bolditalic;
  46     protected boolean logicalFont = false;
  47     protected int familyRank;
  48 
  49     public static FontFamily getFamily(String name) {
  50         return familyNameMap.get(name.toLowerCase(Locale.ENGLISH));
  51     }
  52 
  53     public static String[] getAllFamilyNames() {
  54         return null;
  55     }
  56 
  57     /* Only for use by FontManager.deRegisterBadFont(..).
  58      * If this was the only font in the family, the family is removed
  59      * from the map
  60      */
  61     static void remove(Font2D font2D) {
  62 
  63         String name = font2D.getFamilyName(Locale.ENGLISH);
  64         FontFamily family = getFamily(name);
  65         if (family == null) {
  66             return;
  67         }
  68         if (family.plain == font2D) {
  69             family.plain = null;
  70         }
  71         if (family.bold == font2D) {
  72             family.bold = null;
  73         }
  74         if (family.italic == font2D) {
  75             family.italic = null;
  76         }
  77         if (family.bolditalic == font2D) {
  78             family.bolditalic = null;
  79         }
  80         if (family.plain == null && family.bold == null &&
  81             family.italic == null && family.bolditalic == null) {
  82             familyNameMap.remove(name);
  83         }
  84     }
  85 
  86     public FontFamily(String name, boolean isLogFont, int rank) {
  87         logicalFont = isLogFont;
  88         familyName = name;
  89         familyRank = rank;
  90         familyNameMap.put(name.toLowerCase(Locale.ENGLISH), this);
  91     }
  92 
  93     /* Create a family for created fonts which aren't listed in the
  94      * main map.
  95      */
  96     FontFamily(String name) {
  97         logicalFont = false;
  98         familyName = name;
  99         familyRank = Font2D.DEFAULT_RANK;
 100     }
 101 
 102     public String getFamilyName() {
 103         return familyName;
 104     }
 105 
 106     public int getRank() {
 107         return familyRank;
 108     }
 109 
 110     private boolean isFromSameSource(Font2D font) {
 111         if (!(font instanceof FileFont)) {
 112             return false;
 113         }
 114 
 115         FileFont existingFont = null;
 116         if (plain instanceof FileFont) {
 117             existingFont = (FileFont)plain;
 118         } else if (bold instanceof FileFont) {
 119             existingFont = (FileFont)bold;
 120         } else if (italic instanceof FileFont) {
 121              existingFont = (FileFont)italic;
 122         } else if (bolditalic instanceof FileFont) {
 123              existingFont = (FileFont)bolditalic;
 124         }
 125         // A family isn't created until there's a font.
 126         // So if we didn't find a file font it means this
 127         // isn't a file-based family.
 128         if (existingFont == null) {
 129             return false;
 130         }
 131         File existDir = (new File(existingFont.platName)).getParentFile();
 132 
 133         FileFont newFont = (FileFont)font;
 134         File newDir = (new File(newFont.platName)).getParentFile();
 135         return java.util.Objects.equals(newDir, existDir);
 136     }
 137 
 138     /*
 139      * We want a family to be of the same width and prefer medium/normal width.
 140      * Once we find a particular width we accept more of the same width
 141      * until we find one closer to normal when we 'evict' all existing fonts.
 142      * So once we see a 'normal' width font we evict all members that are not
 143      * normal width and then accept only new ones that are normal width.
 144      *
 145      * Once a font passes the width test we subject it to the weight test.
 146      * For Plain we target the weight the closest that is <= NORMAL (400)
 147      * For Bold we target the weight that is closest to BOLD (700).
 148      *
 149      * In the future, rather than discarding these fonts, we should
 150      * extend the family to include these so lookups on these properties
 151      * can locate them, as presently they will only be located by full name
 152      * based lookup.
 153      */
 154 
 155     private int familyWidth = 0;
 156     private boolean preferredWidth(Font2D font) {
 157 
 158         int newWidth = font.getWidth();
 159 
 160         if (familyWidth == 0) {
 161             familyWidth = newWidth;
 162             return true;
 163         }
 164 
 165         if (newWidth == familyWidth) {
 166             return true;
 167         }
 168 
 169         if (Math.abs(Font2D.FWIDTH_NORMAL - newWidth) <
 170             Math.abs(Font2D.FWIDTH_NORMAL - familyWidth))
 171         {
 172            if (FontUtilities.debugFonts()) {
 173                FontUtilities.getLogger().info(
 174                "Found more preferred width. New width = " + newWidth +
 175                " Old width = " + familyWidth + " in font " + font +
 176                " nulling out fonts plain: " + plain + " bold: " + bold +
 177                " italic: " + italic + " bolditalic: " + bolditalic);
 178            }
 179            familyWidth = newWidth;
 180            plain = bold = italic = bolditalic = null;
 181            return true;
 182         } else if (FontUtilities.debugFonts()) {
 183                FontUtilities.getLogger().info(
 184                "Family rejecting font " + font +
 185                " of less preferred width " + newWidth);
 186         }
 187         return false;
 188     }
 189 
 190     private boolean closerWeight(Font2D currFont, Font2D font, int style) {
 191         if (familyWidth != font.getWidth()) {
 192             return false;
 193         }
 194 
 195         if (currFont == null) {
 196             return true;
 197         }
 198 
 199         if (FontUtilities.debugFonts()) {
 200             FontUtilities.getLogger().info(
 201             "New weight for style " + style + ". Curr.font=" + currFont +
 202             " New font="+font+" Curr.weight="+ + currFont.getWeight()+
 203             " New weight="+font.getWeight());
 204         }
 205 
 206         int newWeight = font.getWeight();
 207         switch (style) {
 208             case Font.PLAIN:
 209             case Font.ITALIC:
 210                 return (newWeight <= Font2D.FWEIGHT_NORMAL &&
 211                         newWeight > currFont.getWeight());
 212 
 213             case Font.BOLD:
 214             case Font.BOLD|Font.ITALIC:
 215                 return (Math.abs(newWeight - Font2D.FWEIGHT_BOLD) <
 216                         Math.abs(currFont.getWeight() - Font2D.FWEIGHT_BOLD));
 217 
 218             default:
 219                return false;
 220         }
 221     }
 222 
 223     public void setFont(Font2D font, int style) {
 224 
 225         if (FontUtilities.isLogging()) {
 226             String msg;
 227             if (font instanceof CompositeFont) {
 228                 msg = "Request to add " + font.getFamilyName(null) +
 229                       " with style " + style + " to family " + familyName;
 230             } else {
 231                 msg = "Request to add " + font +
 232                       " with style " + style + " to family " + this;
 233             }
 234             FontUtilities.getLogger().info(msg);
 235         }
 236         /* Allow a lower-rank font only if its a file font
 237          * from the exact same source as any previous font.
 238          */
 239         if ((font.getRank() > familyRank) && !isFromSameSource(font)) {
 240             if (FontUtilities.isLogging()) {
 241                 FontUtilities.getLogger()
 242                                   .warning("Rejecting adding " + font +
 243                                            " of lower rank " + font.getRank() +
 244                                            " to family " + this +
 245                                            " of rank " + familyRank);
 246             }
 247             return;
 248         }
 249 
 250         switch (style) {
 251 
 252         case Font.PLAIN:
 253             if (preferredWidth(font) && closerWeight(plain, font, style)) {
 254                 plain = font;
 255             }
 256             break;
 257 
 258         case Font.BOLD:
 259             if (preferredWidth(font) && closerWeight(bold, font, style)) {
 260                 bold = font;
 261             }
 262             break;
 263 
 264         case Font.ITALIC:
 265             if (preferredWidth(font) && closerWeight(italic, font, style)) {
 266                 italic = font;
 267             }
 268             break;
 269 
 270         case Font.BOLD|Font.ITALIC:
 271             if (preferredWidth(font) && closerWeight(bolditalic, font, style)) {
 272                 bolditalic = font;
 273             }
 274             break;
 275 
 276         default:
 277             break;
 278         }
 279     }
 280 
 281     public Font2D getFontWithExactStyleMatch(int style) {
 282 
 283         switch (style) {
 284 
 285         case Font.PLAIN:
 286             return plain;
 287 
 288         case Font.BOLD:
 289             return bold;
 290 
 291         case Font.ITALIC:
 292             return italic;
 293 
 294         case Font.BOLD|Font.ITALIC:
 295             return bolditalic;
 296 
 297         default:
 298             return null;
 299         }
 300     }
 301 
 302     /* REMIND: if the callers of this method are operating in an
 303      * environment in which not all fonts are registered, the returned
 304      * font may be a algorithmically styled one, where in fact if loadfonts
 305      * were executed, a styled font may be located. Our present "solution"
 306      * to this is to register all fonts in a directory and assume that this
 307      * registered all the styles of a font, since they would all be in the
 308      * same location.
 309      */
 310     public Font2D getFont(int style) {
 311 
 312         switch (style) {
 313 
 314         case Font.PLAIN:
 315             return plain;
 316 
 317         case Font.BOLD:
 318             if (bold != null) {
 319                 return bold;
 320             } else if (plain != null && plain.canDoStyle(style)) {
 321                     return plain;
 322             } else {
 323                 return null;
 324             }
 325 
 326         case Font.ITALIC:
 327             if (italic != null) {
 328                 return italic;
 329             } else if (plain != null && plain.canDoStyle(style)) {
 330                     return plain;
 331             } else {
 332                 return null;
 333             }
 334 
 335         case Font.BOLD|Font.ITALIC:
 336             if (bolditalic != null) {
 337                 return bolditalic;
 338             } else if (italic != null && italic.canDoStyle(style)) {
 339                     return italic;
 340             } else if (bold != null && bold.canDoStyle(style)) {
 341                     return italic;
 342             } else if (plain != null && plain.canDoStyle(style)) {
 343                     return plain;
 344             } else {
 345                 return null;
 346             }
 347         default:
 348             return null;
 349         }
 350     }
 351 
 352     /* Only to be called if getFont(style) returns null
 353      * This method will only return null if the family is completely empty!
 354      * Note that it assumes the font of the style you need isn't in the
 355      * family. The logic here is that if we must substitute something
 356      * it might as well be from the same family.
 357      */
 358      Font2D getClosestStyle(int style) {
 359 
 360         switch (style) {
 361             /* if you ask for a plain font try to return a non-italic one,
 362              * then a italic one, finally a bold italic one */
 363         case Font.PLAIN:
 364             if (bold != null) {
 365                 return bold;
 366             } else if (italic != null) {
 367                 return italic;
 368             } else {
 369                 return bolditalic;
 370             }
 371 
 372             /* if you ask for a bold font try to return a non-italic one,
 373              * then a bold italic one, finally an italic one */
 374         case Font.BOLD:
 375             if (plain != null) {
 376                 return plain;
 377             } else if (bolditalic != null) {
 378                 return bolditalic;
 379             } else {
 380                 return italic;
 381             }
 382 
 383             /* if you ask for a italic font try to return a  bold italic one,
 384              * then a plain one, finally an bold one */
 385         case Font.ITALIC:
 386             if (bolditalic != null) {
 387                 return bolditalic;
 388             } else if (plain != null) {
 389                 return plain;
 390             } else {
 391                 return bold;
 392             }
 393 
 394         case Font.BOLD|Font.ITALIC:
 395             if (italic != null) {
 396                 return italic;
 397             } else if (bold != null) {
 398                 return bold;
 399             } else {
 400                 return plain;
 401             }
 402         }
 403         return null;
 404     }
 405 
 406     /* Font may have localized names. Store these in a separate map, so
 407      * that only clients who use these names need be affected.
 408      */
 409     static synchronized void addLocaleNames(FontFamily family, String[] names){
 410         if (allLocaleNames == null) {
 411             allLocaleNames = new HashMap<String, FontFamily>();
 412         }
 413         for (int i=0; i<names.length; i++) {
 414             allLocaleNames.put(names[i].toLowerCase(), family);
 415         }
 416     }
 417 
 418     public static synchronized FontFamily getLocaleFamily(String name) {
 419         if (allLocaleNames == null) {
 420             return null;
 421         }
 422         return allLocaleNames.get(name.toLowerCase());
 423     }
 424 
 425     public static FontFamily[] getAllFontFamilies() {
 426        Collection<FontFamily> families = familyNameMap.values();
 427        return families.toArray(new FontFamily[0]);
 428     }
 429 
 430     public String toString() {
 431         return
 432             "Font family: " + familyName +
 433             " plain="+plain+
 434             " bold=" + bold +
 435             " italic=" + italic +
 436             " bolditalic=" + bolditalic;
 437 
 438     }
 439 
 440 }