public virtual void TestItalicFlag() { FontCharacteristics fontCharacteristics = new FontCharacteristics(); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics.SetItalicFlag(true); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsUndefined()); fontCharacteristics = new FontCharacteristics(); fontCharacteristics.SetItalicFlag(false); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); }
public virtual void TestDefaultFontCharacteristics() { FontCharacteristics fontCharacteristics = new FontCharacteristics(); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsBold()); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsMonospace()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); NUnit.Framework.Assert.AreEqual(400, fontCharacteristics.GetFontWeight()); }
public virtual void TestIncorrectFontStyle() { FontCharacteristics fontCharacteristics = new FontCharacteristics(); fontCharacteristics.SetFontStyle(null); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics.SetFontStyle("dsodkodkopsdkod"); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics.SetFontStyle(""); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics.SetFontStyle("-1"); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics.SetFontStyle("bold"); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); }
public virtual void TestAllowedFontStyle() { FontCharacteristics fontCharacteristics = new FontCharacteristics(); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics = new FontCharacteristics(); fontCharacteristics.SetFontStyle("normal"); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsUndefined()); fontCharacteristics = new FontCharacteristics(); fontCharacteristics.SetFontStyle("oblique"); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsUndefined()); fontCharacteristics = new FontCharacteristics(); fontCharacteristics.SetFontStyle("italic"); NUnit.Framework.Assert.IsTrue(fontCharacteristics.IsItalic()); NUnit.Framework.Assert.IsFalse(fontCharacteristics.IsUndefined()); }
/// <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. /// 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. /// 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); }
/// <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); }