Ejemplo n.º 1
0
  public static PhysicalFont getItalicForm(PhysicalFont pf) {

    // look up the font in MicrosoftFontsRegistry
    MicrosoftFonts.Font msFont = MicrosoftFontsRegistry.getMsFonts().get(pf.getName());

    if (msFont == null) {
      log.debug("No entry in MicrosoftFontsRegistry for: " + pf.getName());
      return null;
    }

    if (msFont.getItalic() == null) {
      log.info("No italic form for: " + pf.getName());
      return null;
    } else {

      // We have to go via the file name, grrr..
      // since MicrosoftFonts.xml doesn't give the associate font name
      String filename;
      if (osName.startsWith("Mac")) {
        filename = msFont.getItalic().getMac().toLowerCase();
      } else {
        filename = msFont.getItalic().getFilename().toLowerCase();
      }
      log.debug("Fetching: " + filename);
      return physicalFontMapByFilenameLowercase.get(filename);
    }
  }
Ejemplo n.º 2
0
 public void getStyleMetrics(float pointSize, float[] metrics, int offset) {
   PhysicalFont font = getSlotFont(0);
   if (font == null) { // possible?
     super.getStyleMetrics(pointSize, metrics, offset);
   } else {
     font.getStyleMetrics(pointSize, metrics, offset);
   }
 }
Ejemplo n.º 3
0
  /* This isn't very efficient but its infrequently used.
   * StandardGlyphVector uses it when the client assigns the glyph codes.
   * These may not be valid. This validates them substituting the missing
   * glyph elsewhere.
   */
  protected int getValidatedGlyphCode(int glyphCode) {
    int slot = glyphCode >>> 24;
    if (slot >= numSlots) {
      return getMapper().getMissingGlyphCode();
    }

    int slotglyphCode = glyphCode & CompositeStrike.SLOTMASK;
    PhysicalFont slotFont = getSlotFont(slot);
    if (slotFont.getValidatedGlyphCode(slotglyphCode) == slotFont.getMissingGlyphCode()) {
      return getMapper().getMissingGlyphCode();
    } else {
      return glyphCode;
    }
  }
Ejemplo n.º 4
0
  public static String getPhysicalFont(OpcPackage wmlPackage, String fontName) {

    log.debug("looking for: " + fontName);

    if (!(wmlPackage instanceof WordprocessingMLPackage)) {
      log.error("Implement me for pptx4j");
      return null;
    }
    PhysicalFont pf =
        ((WordprocessingMLPackage) wmlPackage).getFontMapper().getFontMappings().get(fontName);
    if (pf != null) {
      log.debug("Font '" + fontName + "' maps to " + pf.getName());
      return pf.getName();
    } else {
      log.warn("Font '" + fontName + "' is not mapped to a physical font. ");
      return null;
    }
  }
Ejemplo n.º 5
0
 FontStrike createStrike(FontStrikeDesc desc) {
   if (isBitmapDelegate) {
     return new NativeStrike(this, desc);
   } else {
     if (delegateFont == null) {
       delegateFont = FontManager.getDefaultPhysicalFont();
     }
     FontStrike delegate = delegateFont.createStrike(desc);
     return new DelegateStrike(this, desc, delegate);
   }
 }
Ejemplo n.º 6
0
 /* To called only by FontManager.replaceFont */
 void replaceComponentFont(PhysicalFont oldFont, PhysicalFont newFont) {
   if (components == null) {
     return;
   }
   for (int slot = 0; slot < numSlots; slot++) {
     if (components[slot] == oldFont) {
       components[slot] = newFont;
       if (componentNames != null) {
         componentNames[slot] = newFont.getFontName(null);
       }
     }
   }
 }
Ejemplo n.º 7
0
 public CharToGlyphMapper getMapper() {
   if (mapper == null) {
     if (isBitmapDelegate) {
       /* we are a delegate */
       mapper = new NativeGlyphMapper(this);
     } else {
       /* we need to delegate */
       delegateFont = FontManager.getDefaultPhysicalFont();
       mapper = delegateFont.getMapper();
     }
   }
   return mapper;
 }
Ejemplo n.º 8
0
 FontStrike createStrike(FontStrikeDesc desc) {
   if (isBitmapDelegate) {
     return new NativeStrike(this, desc);
   } else {
     if (delegateFont == null) {
       SunFontManager fm = SunFontManager.getInstance();
       delegateFont = fm.getDefaultPhysicalFont();
     }
     /* If no FileFont's are found, delegate font may be
      * a NativeFont, so we need to avoid recursing here.
      */
     if (delegateFont instanceof NativeFont) {
       return new NativeStrike((NativeFont) delegateFont, desc);
     }
     FontStrike delegate = delegateFont.createStrike(desc);
     return new DelegateStrike(this, desc, delegate);
   }
 }
Ejemplo n.º 9
0
  /**
   * Add a physical font's EmbedFontInfo object.
   *
   * @param fontUrl eg new java.net.URL("file:" + path)
   */
  public static void addPhysicalFont(String nameAsInFontTablePart, URL fontUrl) {

    // List<EmbedFontInfo> embedFontInfoList = fontInfoFinder.find(fontUrl, fontResolver,
    // fontCache);
    EmbedFontInfo[] embedFontInfoList = fontInfoFinder.find(fontUrl, fontResolver, fontCache);
    /* FOP r644208 (Bugzilla #44737) 3/04/08 made this an array,
    // so if you are using non-patched FOP, it needs to be at least this revision
    // (but doesn't seem to be in FOP 0.95 binary?!) */

    if (embedFontInfoList == null) {
      // Quite a few fonts exist that we can't seem to get
      // EmbedFontInfo for. To be investigated.
      log.warn(
          "Aborting: "
              + fontUrl.toString()
              + " (can't get EmbedFontInfo[] .. try deleting fop-fonts.cache?)");
      return;
    }

    StringBuffer debug = new StringBuffer();

    for (EmbedFontInfo fontInfo : embedFontInfoList) {

      /* EmbedFontInfo has:
      * - subFontName (if the underlying CustomFont is a TTC)
      * - PostScriptName = CustomFont.getFontName()
      * - FontTriplets named:
      * 		- CustomFont.getFullName() with quotes stripped
      * 		- CustomFont.getFontName() with whitespace stripped
      * 		- each family name        (with quotes stripped)
      *
      * By creating one PhysicalFont object
      * per triplet, each referring to the same
      * EmbedFontInfo, we increase the chances
      * of a match
      *
      ComicSansMS
      .. triplet Comic Sans MS (priority + 0
      .. triplet ComicSansMS (priority + 0

      ComicSansMS-Bold
      .. triplet Comic Sans MS Bold (priority + 0
      .. triplet ComicSansMS-Bold (priority + 0
      .. triplet Comic Sans MS (priority + 5
      *
      * but the second triplet is what FOP creates where its
      * getPostScriptName()
      * does FontUtil.stripWhiteSpace(getFullName());.
      *
      * and the third is just the family name.
      *
      * So we only get the first.
      *
      */

      if (fontInfo == null) {
        //				return;
        continue;
      }

      debug.append("------- \n");

      try {
        debug.append(fontInfo.getPostScriptName() + "\n");
        if (!fontInfo.isEmbeddable()) {
          //	        	log.info(tokens[x] + " is not embeddable; skipping.");

          /*
          * No point looking at this font, since if we tried to use it,
          * later, we'd get:
          *
          * com.lowagie.text.DocumentException: file:/usr/share/fonts/truetype/ttf-tamil-fonts/lohit_ta.ttf cannot be embedded due to licensing restrictions.
          at com.lowagie.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:364)
          at com.lowagie.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:335)
          at com.lowagie.text.pdf.BaseFont.createFont(BaseFont.java:399)
          at com.lowagie.text.pdf.BaseFont.createFont(BaseFont.java:345)
          at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:164)

          will be thrown if os_2.fsType == 2

          */
          log.warn(fontInfo.getEmbedFile() + " is not embeddable; ignoring this font.");

          // return;
          continue;
        }
      } catch (Exception e1) {
        // NB isEmbeddable() only exists in our patched FOP
        if (!loggedWarningAlready) {
          log.warn("Not using patched FOP; isEmbeddable() method missing.");
          loggedWarningAlready = true;
        }
      }

      PhysicalFont pf;

      //			for (Iterator iterIn = fontInfo.getFontTriplets().iterator() ; iterIn.hasNext();) {
      //				FontTriplet triplet = (FontTriplet)iterIn.next();

      FontTriplet triplet = (FontTriplet) fontInfo.getFontTriplets().get(0);
      // There is one triplet for each of the font family names
      // this font has, and we create a PhysicalFont object
      // for each of them.  For our purposes though, each of
      // these physical font objects contains the same info

      String lower = fontInfo.getEmbedFile().toLowerCase();
      log.debug("Processing physical font: " + lower);
      debug.append(
          ".. triplet " + triplet.getName() + " (priority " + triplet.getPriority() + "\n");

      pf = null;
      // xhtmlrenderer's org.xhtmlrenderer.pdf.ITextFontResolver.addFont
      // can handle
      // .otf, .ttf, .ttc, .pfb
      if (lower.endsWith(".otf") || lower.endsWith(".ttf") || lower.endsWith(".ttc")) {
        pf = new PhysicalFont(triplet.getName(), fontInfo, fontResolver);
      } else if (lower.endsWith(".pfb")) {
        // See whether we have everything org.xhtmlrenderer.pdf.ITextFontResolver.addFont
        // will need - for a .pfb file, it needs a corresponding .afm or .pfm
        String afm = FontUtils.pathFromURL(lower);
        afm = afm.substring(0, afm.length() - 4) + ".afm"; // drop the 'file:'
        log.debug("Looking for: " + afm);
        File f = new File(afm);
        if (f.exists()) {

          log.debug(".. found");

          // Uncomment if you want to use the iText stuff in docx4j-extras
          //			        	// We're only interested if this font supports UTF-8 encoding
          //			        	// since otherwise iText can't use it (at least on a
          //			        	// UTF8 encoded XHTML document)
          //			        	try {
          //			    		    BaseFont bf = BaseFont.createFont(afm,
          //			    		    		BaseFont.IDENTITY_H,
          //									BaseFont.NOT_EMBEDDED);
          //						} catch (java.io.UnsupportedEncodingException uee) {
          //							log.error(afm + " does not support UTF encoding, so ignoring");
          //							continue;
          //						} catch (Exception e) {
          //							log.error(e.getMessage(), e);
          //							continue;
          //						}
          pf = new PhysicalFont(triplet.getName(), fontInfo, fontResolver);

        } else {
          // Should we be doing afm first, or pfm?
          String pfm = FontUtils.pathFromURL(lower);
          pfm = pfm.substring(0, pfm.length() - 4) + ".pfm"; // drop the 'file:'
          log.debug("Looking for: " + pfm);
          f = new File(pfm);
          if (f.exists()) {
            log.debug(".. found");

            // Uncomment if you want to use the iText stuff in docx4j-extras
            //				        	// We're only interested if this font supports UTF-8 encoding
            //				        	try {
            //				    		    BaseFont bf = BaseFont.createFont(pfm,
            //				    		    		BaseFont.IDENTITY_H,
            //										BaseFont.NOT_EMBEDDED);
            //							} catch (java.io.UnsupportedEncodingException uee) {
            //								log.error(pfm + " does not support UTF encoding, so ignoring");
            //								continue;
            //							} catch (Exception e) {
            //								log.error(e.getMessage(), e);
            //								continue;
            //							}
            pf = new PhysicalFont(triplet.getName(), fontInfo, fontResolver);
          } else {
            log.warn(
                "Skipping "
                    + triplet.getName()
                    + "; couldn't find .afm or .pfm for : "
                    + fontInfo.getEmbedFile());
          }
        }
      } else {
        log.warn(
            "Skipping " + triplet.getName() + "; unsupported type: " + fontInfo.getEmbedFile());
      }

      if (pf != null) {

        // Add it to the map
        physicalFontMap.put(pf.getName(), pf);
        log.debug("Added " + pf.getName() + " -> " + pf.getEmbeddedFile());

        if (nameAsInFontTablePart != null && !physicalFontMap.containsKey(nameAsInFontTablePart)) {
          physicalFontMap.put(nameAsInFontTablePart, pf);
          log.debug("Added '" + nameAsInFontTablePart + "' -> " + pf.getEmbeddedFile());
        }

        // We also need to add it to map by filename
        String filename = pf.getEmbeddedFile();
        filename = filename.substring(filename.lastIndexOf("/") + 1).toLowerCase();

        if (osName.startsWith("Mac")) {
          filename = filename.replace("%20", " ");
        }
        physicalFontMapByFilenameLowercase.put(filename, pf);
        log.debug("added to filename map: " + filename);

        //		        	String familyName = triplet.getName();
        //		        	pf.setFamilyName(familyName);
        //
        //		        	PhysicalFontFamily pff;
        //		        	if (physicalFontFamiliesMap.get(familyName)==null) {
        //		        		pff = new PhysicalFontFamily(familyName);
        //		        		physicalFontFamiliesMap.put(familyName, pff);
        //		        	} else {
        //		        		pff = physicalFontFamiliesMap.get(familyName);
        //		        	}
        //		        	pff.addFont(pf);

      }
    }

    log.debug(debug.toString());
  }
Ejemplo n.º 10
0
                public Typeface load(PhysicalFont key) {

                  return key.getTypeface();
                }