public Font GetFont(ChainedProperties props)
        {
            String face = props[ElementTags.FACE];

            if (face != null)
            {
                StringTokenizer tok = new StringTokenizer(face, ",");
                while (tok.HasMoreTokens())
                {
                    face = tok.NextToken().Trim();
                    if (face.StartsWith("\""))
                    {
                        face = face.Substring(1);
                    }
                    if (face.EndsWith("\""))
                    {
                        face = face.Substring(0, face.Length - 1);
                    }
                    if (fontImp.IsRegistered(face))
                    {
                        break;
                    }
                }
            }
            int style = 0;

            if (props.HasProperty(HtmlTags.I))
            {
                style |= Font.ITALIC;
            }
            if (props.HasProperty(HtmlTags.B))
            {
                style |= Font.BOLD;
            }
            if (props.HasProperty(HtmlTags.U))
            {
                style |= Font.UNDERLINE;
            }
            if (props.HasProperty(HtmlTags.S))
            {
                style |= Font.STRIKETHRU;
            }

            String value = props[ElementTags.SIZE];
            float  size  = 12;

            if (value != null)
            {
                size = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
            }
            BaseColor color    = Markup.DecodeColor(props["color"]);
            String    encoding = props["encoding"];

            if (encoding == null)
            {
                encoding = BaseFont.WINANSI;
            }
            return(fontImp.GetFont(face, encoding, true, size, style, color));
        }
示例#2
0
        public Font ApplyFontStyles(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(CSS.Value.ITALIC, value))
                    {
                        if (style == Font.BOLD)
                        {
                            style = Font.BOLDITALIC;
                        }
                        else
                        {
                            style = Font.ITALIC;
                        }
                    }
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.FONT_FAMILY, key))
                {
                    // TODO improve fontfamily parsing (what if a font family has a comma in the name ? )
                    fontName = value;
                }
                else if (Util.EqualsIgnoreCase(CSS.Property.COLOR, key))
                {
                    color = HtmlUtilities.DecodeColor(value);
                }
            }
            if (fontName != null)
            {
                if (fontName.Contains(","))
                {
                    String[] fonts     = fontName.Split(new Char[] { ',' });
                    Font     firstFont = null;
                    foreach (String s in fonts)
                    {
                        String trimmedS = utils.TrimAndRemoveQuoutes(s);
                        if (fontProvider.IsRegistered(trimmedS))
                        {
                            Font f = fontProvider.GetFont(trimmedS, encoding, BaseFont.EMBEDDED, size, style, color);
                            if (f != null &&
                                (style == Font.NORMAL || style == Font.UNDEFINED || (f.Style & style) == 0))
                            {
                                return(f);
                            }
                            if (firstFont == null)
                            {
                                firstFont = f;
                            }
                        }
                    }
                    if (firstFont != null)
                    {
                        return(firstFont);
                    }
                    else
                    {
                        if (fonts.Length > 0)
                        {
                            fontName = utils.TrimAndRemoveQuoutes(fontName.Split(new Char[] { ',' })[0]);
                        }
                        else
                        {
                            fontName = null;
                        }
                    }
                }
                else
                {
                    fontName = utils.TrimAndRemoveQuoutes(fontName);
                }
            }

            return(fontProvider.GetFont(fontName, encoding, BaseFont.EMBEDDED, size, style, color));
        }
示例#3
0
        /**
         * Creates a Font object based on a chain of properties.
         * @param   chain   chain of properties
         * @return  an iText Font object
         */
        virtual public Font GetFont(ChainedProperties chain)
        {
            // [1] font name

            String face = chain[HtmlTags.FACE];

            // try again, under the CSS key.
            //ISSUE: If both are present, we always go with face, even if font-family was
            //  defined more recently in our ChainedProperties.  One solution would go like this:
            //    Map all our supported style attributes to the 'normal' tag name, so we could
            //    look everything up under that one tag, retrieving the most current value.
            if (face == null || face.Trim().Length == 0)
            {
                face = chain[HtmlTags.FONTFAMILY];
            }
            // if the font consists of a comma separated list,
            // take the first font that is registered
            if (face != null)
            {
                StringTokenizer tok = new StringTokenizer(face, ",");
                while (tok.HasMoreTokens())
                {
                    face = tok.NextToken().Trim();
                    if (face.StartsWith("\""))
                    {
                        face = face.Substring(1);
                    }
                    if (face.EndsWith("\""))
                    {
                        face = face.Substring(0, face.Length - 1);
                    }
                    if (provider.IsRegistered(face))
                    {
                        break;
                    }
                }
            }

            // [2] encoding
            String encoding = chain[HtmlTags.ENCODING];

            if (encoding == null)
            {
                encoding = BaseFont.WINANSI;
            }

            // [3] embedded

            // [4] font size
            String value = chain[HtmlTags.SIZE];
            float  size  = 12;

            if (value != null)
            {
                size = float.Parse(value, CultureInfo.InvariantCulture);
            }

            // [5] font style
            int style = 0;

            // text-decoration
            String decoration = chain[HtmlTags.TEXTDECORATION];

            if (decoration != null && decoration.Trim().Length != 0)
            {
                if (HtmlTags.UNDERLINE.Equals(decoration))
                {
                    style |= Font.UNDERLINE;
                }
                else if (HtmlTags.LINETHROUGH.Equals(decoration))
                {
                    style |= Font.STRIKETHRU;
                }
            }
            // italic
            if (chain.HasProperty(HtmlTags.I))
            {
                style |= Font.ITALIC;
            }
            // bold
            if (chain.HasProperty(HtmlTags.B))
            {
                style |= Font.BOLD;
            }
            // underline
            if (chain.HasProperty(HtmlTags.U))
            {
                style |= Font.UNDERLINE;
            }
            // strikethru
            if (chain.HasProperty(HtmlTags.S))
            {
                style |= Font.STRIKETHRU;
            }

            // [6] Color
            BaseColor color = HtmlUtilities.DecodeColor(chain[HtmlTags.COLOR]);

            // Get the font object from the provider
            return(provider.GetFont(face, encoding, true, size, style, color));
        }
示例#4
0
        public Font GetFont(ChainedProperties props)
        {
            String face = props[ElementTags.FACE];

            // try again, under the CSS key.
            //ISSUE: If both are present, we always go with face, even if font-family was
            //  defined more recently in our ChainedProperties.  One solution would go like this:
            //    Map all our supported style attributes to the 'normal' tag name, so we could
            //    look everything up under that one tag, retrieving the most current value.
            if (face == null || face.Trim().Length == 0)
            {
                face = props[Markup.CSS_KEY_FONTFAMILY];
            }
            if (face != null)
            {
                StringTokenizer tok = new StringTokenizer(face, ",");
                while (tok.HasMoreTokens())
                {
                    face = tok.NextToken().Trim();
                    if (face.StartsWith("\""))
                    {
                        face = face.Substring(1);
                    }
                    if (face.EndsWith("\""))
                    {
                        face = face.Substring(0, face.Length - 1);
                    }
                    if (fontImp.IsRegistered(face))
                    {
                        break;
                    }
                }
            }
            int    style   = 0;
            String textDec = props[Markup.CSS_KEY_TEXTDECORATION];

            if (textDec != null && textDec.Trim().Length != 0)
            {
                if (Markup.CSS_VALUE_UNDERLINE.Equals(textDec))
                {
                    style |= Font.UNDERLINE;
                }
                else if (Markup.CSS_VALUE_LINETHROUGH.Equals(textDec))
                {
                    style |= Font.STRIKETHRU;
                }
            }
            if (props.HasProperty(HtmlTags.I))
            {
                style |= Font.ITALIC;
            }
            if (props.HasProperty(HtmlTags.B))
            {
                style |= Font.BOLD;
            }
            if (props.HasProperty(HtmlTags.U))
            {
                style |= Font.UNDERLINE;
            }
            if (props.HasProperty(HtmlTags.S))
            {
                style |= Font.STRIKETHRU;
            }

            String value = props[ElementTags.SIZE];
            float  size  = 12;

            if (value != null)
            {
                size = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
            }
            BaseColor color    = Markup.DecodeColor(props["color"]);
            String    encoding = props["encoding"];

            if (encoding == null)
            {
                encoding = BaseFont.WINANSI;
            }
            return(fontImp.GetFont(face, encoding, true, size, style, color));
        }