/** * @return Singleton instance of FontSizeTranslater. */ public static FontSizeTranslator GetInstance() { if (myself != null) return myself; lock (syncroot) { if (null == myself) { myself = new FontSizeTranslator(); } return myself; } }
/** * @return Singleton instance of FontSizeTranslater. */ public static FontSizeTranslator GetInstance() { if (myself != null) { return(myself); } lock (syncroot) { if (null == myself) { myself = new FontSizeTranslator(); } return(myself); } }
/** * */ public AbstractTagProcessor() { fontsizeTrans = FontSizeTranslator.GetInstance(); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, * com.itextpdf.tool.xml.Tag) */ public Chunk Apply(Chunk c, Tag t) { String fontName = null; String encoding = BaseFont.CP1252; float size = new FontSizeTranslator().GetFontSize(t); int style = Font.UNDEFINED; BaseColor color = null; IDictionary<String, String> rules = t.CSS; foreach (KeyValuePair<String, String> entry in rules) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_WEIGHT, key)) { if (CSS.Value.BOLD.Contains(value)) { if (style == Font.ITALIC) { style = Font.BOLDITALIC; } else { style = Font.BOLD; } } else { if (style == Font.BOLDITALIC) { style = Font.ITALIC; } else { style = Font.NORMAL; } } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ITALIC)) { if (style == Font.BOLD) style = Font.BOLDITALIC; else style = Font.ITALIC; } if (Util.EqualsIgnoreCase(value, CSS.Value.OBLIQUE)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_FAMILY, key)) { if (value.Contains(",")){ String[] fonts = value.Split(','); foreach (String s in fonts) { string s2 = s.Trim(); if (!Util.EqualsIgnoreCase(FontFactory.GetFont(s2).Familyname, "unknown")){ fontName = s2; break; } } } else { fontName = value; } } else if (Util.EqualsIgnoreCase(CSS.Property.COLOR, key)) { color = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (rules.ContainsKey(CSS.Property.XFA_FONT_HORIZONTAL_SCALE)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling(float.Parse(rules[CSS.Property.XFA_FONT_HORIZONTAL_SCALE].Replace("%", ""), CultureInfo.InvariantCulture)/100f); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.ContainsKey(CSS.Property.VERTICAL_ALIGN)) { String value = rules[CSS.Property.VERTICAL_ALIGN]; if (Util.EqualsIgnoreCase(value, CSS.Value.SUPER)||Util.EqualsIgnoreCase(value, CSS.Value.TOP)||Util.EqualsIgnoreCase(value, CSS.Value.TEXT_TOP)) { c.SetTextRise((float) (size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(value, CSS.Value.SUB)||Util.EqualsIgnoreCase(value, CSS.Value.BOTTOM)||Util.EqualsIgnoreCase(value, CSS.Value.TEXT_BOTTOM)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale); if (null != xfaVertScale) { // only % allowed; need a catch block NumberFormatExc? if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)/100; c.SetHorizontalScaling(100/float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)); } } if (rules.ContainsKey(CSS.Property.TEXT_DECORATION)) { // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. String value = rules[CSS.Property.TEXT_DECORATION]; if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size/8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size/4f); } } if (rules.ContainsKey(CSS.Property.BACKGROUND_COLOR)) { c.SetBackground(HtmlUtilities.DecodeColor(rules[CSS.Property.BACKGROUND_COLOR])); } Font f = FontFactory.GetFont(fontName, encoding, BaseFont.EMBEDDED, size, style, color); c.Font = f; return c; }