Ejemplo n.º 1
0
 private static void PutFontNamesToCache(FontCacheKey key, FontProgramDescriptor descriptor)
 {
     if (descriptor != null)
     {
         fontNamesCache.Put(key, descriptor);
     }
 }
Ejemplo n.º 2
0
        public static iText.Layout.Font.FontInfo Create(FontProgram fontProgram, String encoding, String alias, Range
                                                        range)
        {
            FontProgramDescriptor descriptor = FontProgramDescriptorFactory.FetchDescriptor(fontProgram);

            return(new iText.Layout.Font.FontInfo(descriptor.GetFontName(), null, encoding, descriptor, range, alias));
        }
Ejemplo n.º 3
0
        private FontInfo(String fontName, byte[] fontData, String encoding, FontProgramDescriptor descriptor, String
                         alias)
        {
            this.fontName   = fontName;
            this.fontData   = fontData;
            this.encoding   = encoding;
            this.descriptor = descriptor;
            this.alias      = alias != null?alias.ToLowerInvariant() : null;

            this.hash = CalculateHashCode(fontName, fontData, encoding);
        }
Ejemplo n.º 4
0
        private FontInfo(String fontName, byte[] fontData, String encoding, FontProgramDescriptor descriptor, Range
                         unicodeRange, String alias)
        {
            this.fontName   = fontName;
            this.fontData   = fontData;
            this.encoding   = encoding;
            this.descriptor = descriptor;
            this.range      = unicodeRange != null ? unicodeRange : RangeBuilder.GetFullRange();
            this.alias      = alias != null?alias.ToLowerInvariant() : null;

            this.hash = CalculateHashCode(this.fontName, this.fontData, this.encoding, this.range);
        }
Ejemplo n.º 5
0
        internal static iText.Layout.Font.FontInfo Create(byte[] fontProgram, String encoding, String alias)
        {
            FontCacheKey          cacheKey   = FontCacheKey.Create(fontProgram);
            FontProgramDescriptor descriptor = GetFontNamesFromCache(cacheKey);

            if (descriptor == null)
            {
                descriptor = FontProgramDescriptorFactory.FetchDescriptor(fontProgram);
                PutFontNamesToCache(cacheKey, descriptor);
            }
            return(descriptor != null ? new iText.Layout.Font.FontInfo(null, fontProgram, encoding, descriptor, alias)
                 : null);
        }
Ejemplo n.º 6
0
            /// <summary>
            /// This method is used to compare two fonts (the first is described by fontInfo,
            /// the second is described by fc and fontName) and measure their similarity.
            /// </summary>
            /// <remarks>
            /// This method is used to compare two fonts (the first is described by fontInfo,
            /// the second is described by fc and fontName) and measure their similarity.
            /// The more the fonts are similar the higher the score is.
            /// We check whether the fonts are both:
            /// a) bold
            /// b) italic
            /// c) monospaced
            /// We also check whether the font names are identical. There are two blocks of conditions:
            /// "equals" and "contains". They cannot be satisfied simultaneously.
            /// Some remarks about these checks:
            /// a) "contains" block checks are much easier to be satisfied so one can get award from this block
            /// higher than from "equals" block only if all "contains" conditions are satisfied.
            /// b) since ideally all conditions of a certain block are satisfied simultaneously, it may result
            /// in highly inflated score. So we decrease an award for other conditions of the block
            /// if one has been already satisfied.
            /// </remarks>
            private static int CharacteristicsSimilarity(String fontName, FontCharacteristics fc, FontInfo fontInfo)
            {
                bool isFontBold      = fontInfo.GetDescriptor().IsBold() || fontInfo.GetDescriptor().GetFontWeight() > 500;
                bool isFontItalic    = fontInfo.GetDescriptor().IsItalic() || fontInfo.GetDescriptor().GetItalicAngle() < 0;
                bool isFontMonospace = fontInfo.GetDescriptor().IsMonospace();
                int  score           = 0;

                if (fc.IsBold())
                {
                    if (isFontBold)
                    {
                        score += EXPECTED_FONT_IS_BOLD_AWARD;
                    }
                    else
                    {
                        score -= EXPECTED_FONT_IS_BOLD_AWARD;
                    }
                }
                else
                {
                    if (isFontBold)
                    {
                        score -= EXPECTED_FONT_IS_NOT_BOLD_AWARD;
                    }
                }
                if (fc.IsItalic())
                {
                    if (isFontItalic)
                    {
                        score += EXPECTED_FONT_IS_ITALIC_AWARD;
                    }
                    else
                    {
                        score -= EXPECTED_FONT_IS_ITALIC_AWARD;
                    }
                }
                else
                {
                    if (isFontItalic)
                    {
                        score -= EXPECTED_FONT_IS_NOT_ITALIC_AWARD;
                    }
                }
                if (fc.IsMonospace())
                {
                    if (isFontMonospace)
                    {
                        score += EXPECTED_FONT_IS_MONOSPACED_AWARD;
                    }
                    else
                    {
                        score -= EXPECTED_FONT_IS_MONOSPACED_AWARD;
                    }
                }
                else
                {
                    if (isFontMonospace)
                    {
                        score -= EXPECTED_FONT_IS_NOT_MONOSPACED_AWARD;
                    }
                }
                // empty font name means that font family wasn't detected. in that case one should compare only style characteristics
                if (!"".Equals(fontName))
                {
                    FontProgramDescriptor descriptor = fontInfo.GetDescriptor();
                    // Note, aliases are custom behaviour, so in FontSelector will find only exact name,
                    // it should not be any 'contains' with aliases.
                    bool checkContains = true;
                    if (fontName.Equals(descriptor.GetFullNameLowerCase()))
                    {
                        // the next condition can be simplified. it's been written that way to prevent mistakes if the condition is moved.
                        score        += checkContains ? FULL_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD;
                        checkContains = false;
                    }
                    if (fontName.Equals(descriptor.GetFontNameLowerCase()))
                    {
                        score        += checkContains ? FONT_NAME_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD;
                        checkContains = false;
                    }
                    if (fontName.Equals(fontInfo.GetAlias()))
                    {
                        score        += checkContains ? ALIAS_EQUALS_AWARD : EQUALS_ADDITIONAL_AWARD;
                        checkContains = false;
                    }
                    if (checkContains)
                    {
                        bool conditionHasBeenSatisfied = false;
                        if (descriptor.GetFullNameLowerCase().Contains(fontName))
                        {
                            // the next condition can be simplified. it's been written that way to prevent mistakes if the condition is moved.
                            score += conditionHasBeenSatisfied ? FULL_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD;
                            conditionHasBeenSatisfied = true;
                        }
                        if (descriptor.GetFontNameLowerCase().Contains(fontName))
                        {
                            score += conditionHasBeenSatisfied ? FONT_NAME_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD;
                            conditionHasBeenSatisfied = true;
                        }
                        if (null != fontInfo.GetAlias() && fontInfo.GetAlias().Contains(fontName))
                        {
                            score += conditionHasBeenSatisfied ? ALIAS_CONTAINS_AWARD : CONTAINS_ADDITIONAL_AWARD;
                            conditionHasBeenSatisfied = true;
                        }
                    }
                }
                // this line is redundant. it's added to prevent mistakes if other condition is added.
                return(score);
            }