/// <summary> /// Create a new instance of /// <see cref="FontSelector"/> /// . While caching is main responsibility of /// <see cref="GetFontSelector(System.Collections.Generic.IList{E}, FontCharacteristics, FontSet)"/> /// . /// This method just create a new instance of /// <see cref="FontSelector"/> /// . /// </summary> /// <param name="fonts">Set of all available fonts in current context.</param> /// <param name="fontFamilies">target font families</param> /// <param name="fc"> /// instance of /// <see cref="FontCharacteristics"/> /// . /// </param> /// <returns> /// an instance of /// <see cref="FontSelector"/> /// . /// </returns> protected internal virtual FontSelector CreateFontSelector(ICollection <FontInfo> fonts, IList <String> fontFamilies , FontCharacteristics fc) { return(new FontSelector(fonts, fontFamilies, fc)); }
public virtual FontSelectorStrategy GetStrategy(String text, IList <String> fontFamilies, FontCharacteristics fc, FontSet additonalFonts) { return(new ComplexFontSelectorStrategy(text, GetFontSelector(fontFamilies, fc, additonalFonts), this, additonalFonts )); }
public virtual FontSelectorStrategy GetStrategy(String text, IList <String> fontFamilies, FontCharacteristics fc) { return(GetStrategy(text, fontFamilies, fc, null)); }
internal FontSelectorKey(IList <String> fontFamilies, FontCharacteristics fc) { this.fontFamilies = new List <String>(fontFamilies); this.fc = fc; }
/// <summary>Create new FontSelector instance.</summary> /// <param name="allFonts">unsorted set of all available fonts.</param> /// <param name="fontFamilies">sorted list of preferred font families.</param> /// <param name="fc"> /// instance of /// <see cref="FontCharacteristics"/>. /// </param> public FontSelector(ICollection <FontInfo> allFonts, IList <String> fontFamilies, FontCharacteristics fc) { this.fonts = new List <FontInfo>(allFonts); //Possible issue in .NET, virtual protected member in constructor. JavaCollectionsUtil.Sort(this.fonts, GetComparator(fontFamilies, fc)); }
/// <summary> /// This method is used to compare two fonts (the required one which is described by fontInfo and /// the one to be examined which is described by fc and fontFamily) and measure their similarity. /// </summary> /// <remarks> /// This method is used to compare two fonts (the required one which is described by fontInfo and /// the one to be examined which is described by fc and fontFamily) and measure their similarity. /// The more the fonts are similar the higher the score is. /// <para /> /// Firstly we check if the font-family described by fontInfo equals to the required one. /// If it's not true the examination fails, it continues otherwise. /// If the required font-family is monospace, serif or sans serif we check whether /// the font under examination is monospace, serif or sans serif resp. Its font-family is not /// taking into considerations. /// <para /> /// If font-family is respected, we consider the next font-style characteristics to select the required font /// of the respected font-family: /// a) bold /// b) italic /// </remarks> private static int CharacteristicsSimilarity(String fontFamily, FontCharacteristics fc, FontInfo fontInfo, bool isLastFontFamilyToBeProcessed) { // TODO DEVSIX-2120 Update javadoc if necessary 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 font-family is monospace, serif or sans-serif, actual font's name shouldn't be checked bool fontFamilySetByCharacteristics = false; // check whether we want to select a monospace, TODO DEVSIX-1034 serif or sans-serif font if (fc.IsMonospace()) { fontFamilySetByCharacteristics = true; 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; } } if (!fontFamilySetByCharacteristics) { // if alias is set, fontInfo's descriptor should not be checked if (!"".Equals(fontFamily) && (null == fontInfo.GetAlias() && null != fontInfo.GetDescriptor().GetFamilyNameLowerCase () && fontInfo.GetDescriptor().GetFamilyNameLowerCase().Equals(fontFamily) || (null != fontInfo.GetAlias () && fontInfo.GetAlias().ToLowerInvariant().Equals(fontFamily)))) { score += FONT_FAMILY_EQUALS_AWARD; } else { if (!isLastFontFamilyToBeProcessed) { return(score); } } } // calculate style characteristics 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; } } return(score); }
protected internal virtual IComparer <FontInfo> GetComparator(IList <String> fontFamilies, FontCharacteristics fc) { return(new FontSelector.PdfFontComparator(fontFamilies, fc)); }
/// <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); }