| 1 | /** |
|---|
| 2 | * |
|---|
| 3 | */ |
|---|
| 4 | package org.docx4j.fonts; |
|---|
| 5 | |
|---|
| 6 | import org.apache.log4j.Logger; |
|---|
| 7 | import org.docx4j.fonts.fop.fonts.EmbedFontInfo; |
|---|
| 8 | import org.docx4j.fonts.fop.fonts.FontResolver; |
|---|
| 9 | import org.docx4j.fonts.fop.fonts.LazyFont; |
|---|
| 10 | import org.docx4j.fonts.fop.fonts.Typeface; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * This class represents a font which is |
|---|
| 14 | * available on the system. |
|---|
| 15 | * |
|---|
| 16 | * It essentially wraps fop's EmbedFontInfo, |
|---|
| 17 | * but names it using the name from the |
|---|
| 18 | * triplet. |
|---|
| 19 | * |
|---|
| 20 | * However, it extends that with Panose |
|---|
| 21 | * info. TODO: use reflection, so that |
|---|
| 22 | * things don't fail if the fop jar |
|---|
| 23 | * doesn't include fontInfo.getPanose() |
|---|
| 24 | * or fontInfo.getEmbedFile() |
|---|
| 25 | * |
|---|
| 26 | * @author dev |
|---|
| 27 | * |
|---|
| 28 | */ |
|---|
| 29 | public class PhysicalFont { |
|---|
| 30 | protected static Logger log = Logger.getLogger(PhysicalFont.class); |
|---|
| 31 | protected FontResolver fontResolver = null; |
|---|
| 32 | protected boolean loadTypefaceFailed = false; |
|---|
| 33 | protected Typeface typeface = null; |
|---|
| 34 | |
|---|
| 35 | PhysicalFont(String name, EmbedFontInfo embedFontInfo, FontResolver fontResolver) { |
|---|
| 36 | |
|---|
| 37 | try { |
|---|
| 38 | // Sanity check |
|---|
| 39 | if (embedFontInfo.getPostScriptName()==null) { |
|---|
| 40 | log.error("Not set!"); |
|---|
| 41 | //log.error(((org.apache.fop.fonts.FontTriplet)fontInfo.getFontTriplets().get(0)).getName()); |
|---|
| 42 | } |
|---|
| 43 | } catch (Exception e1) { |
|---|
| 44 | // NB getPanose() only exists in our patched FOP |
|---|
| 45 | if (!loggedWarningAlready) { |
|---|
| 46 | log.warn("Not using patched FOP; getPostScriptName() method missing."); |
|---|
| 47 | loggedWarningAlready = true; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | this.embedFontInfo = embedFontInfo; |
|---|
| 52 | this.fontResolver = fontResolver; |
|---|
| 53 | |
|---|
| 54 | setName(name); |
|---|
| 55 | |
|---|
| 56 | //familyName = embedFontInfo. |
|---|
| 57 | |
|---|
| 58 | // setName(fontInfo.getPostScriptName()); |
|---|
| 59 | |
|---|
| 60 | setEmbeddedFile(embedFontInfo.getEmbedFile()); |
|---|
| 61 | try { |
|---|
| 62 | setPanose(embedFontInfo.getPanose()); |
|---|
| 63 | } catch (Exception e) { |
|---|
| 64 | // NB getPanose() only exists in our patched FOP |
|---|
| 65 | if (!loggedWarningAlready) { |
|---|
| 66 | log.warn("Not using patched FOP; getPanose() method missing."); |
|---|
| 67 | loggedWarningAlready = true; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | private static boolean loggedWarningAlready = false; |
|---|
| 73 | |
|---|
| 74 | // postscript name eg |
|---|
| 75 | String name; |
|---|
| 76 | public String getName() { |
|---|
| 77 | return name; |
|---|
| 78 | } |
|---|
| 79 | public void setName(String name) { |
|---|
| 80 | this.name = name; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | private EmbedFontInfo embedFontInfo; |
|---|
| 84 | public EmbedFontInfo getEmbedFontInfo() { |
|---|
| 85 | return embedFontInfo; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // // For example: Times New Roman - note this is an array; |
|---|
| 89 | // FOP doesn't ordinarily include it in EmbedFontInfo, |
|---|
| 90 | // instead it makes a font triplet to represent it |
|---|
| 91 | // String familyName; |
|---|
| 92 | // public String getFamilyName() { |
|---|
| 93 | // return familyName; |
|---|
| 94 | // } |
|---|
| 95 | // public void setFamilyName(String familyName) { |
|---|
| 96 | // this.familyName = familyName; |
|---|
| 97 | // } |
|---|
| 98 | |
|---|
| 99 | String embeddedFile; |
|---|
| 100 | public String getEmbeddedFile() { |
|---|
| 101 | return embeddedFile; |
|---|
| 102 | } |
|---|
| 103 | public void setEmbeddedFile(String embeddedFile) { |
|---|
| 104 | this.embeddedFile = embeddedFile; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | org.docx4j.fonts.foray.font.format.Panose panose; |
|---|
| 108 | public org.docx4j.fonts.foray.font.format.Panose getPanose() { |
|---|
| 109 | return panose; |
|---|
| 110 | } |
|---|
| 111 | public void setPanose(org.docx4j.fonts.foray.font.format.Panose panose) { |
|---|
| 112 | this.panose = panose; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public Typeface getTypeface() { |
|---|
| 116 | LazyFont lazyFont = null; |
|---|
| 117 | if (typeface == null) { |
|---|
| 118 | if (!loadTypefaceFailed) { |
|---|
| 119 | lazyFont = new LazyFont(embedFontInfo, fontResolver); |
|---|
| 120 | typeface = lazyFont.getRealFont(); |
|---|
| 121 | loadTypefaceFailed = (typeface == null); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | return typeface; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|