/// <summary> /// The proper font means that the return font has same font family /// if the font style is not existed. /// For example, to search font "Arial Black(family)" + Italic(style) /// if "Arial Black Italic" is existed, will return "Arial Black Italic" /// if "Arial Black Italic" is not existed, will return "Arial Black Regular". /// </summary> private InDesign.Font GetProperFont(string p_family, string p_style) { InDesign.Font resultFont = null; if (fontTable == null) { InitializeFontTable(); } string key = GenerateFontKey(p_family, p_style); if (fontTable.ContainsKey(key)) { resultFont = (InDesign.Font)fontTable[key]; } else { foreach (object obj in fontTable.Keys) { string fontkey = (string)obj; string familykey = GenerateFontFamilyKey(p_family); if (fontkey.StartsWith(familykey)) { resultFont = (InDesign.Font)fontTable[fontkey]; break; } } } return(resultFont); }
/// <summary> /// Get each paragraph xml element's attributes to update their style. /// </summary> private void UpdateElementStyle(InDesign.XMLElement p_element) { InDesign.XMLAttribute xmlAttr = null; InDesign.Paragraph paragraph = null; InDesign.Font font = null; string fontfamily = null; string fontstylename = null; if (p_element.Paragraphs.Count > 0) { paragraph = (InDesign.Paragraph)p_element.Paragraphs.FirstItem(); for (int i = 0; i < p_element.XMLAttributes.Count; i++) { if (i == 0) { xmlAttr = (InDesign.XMLAttribute)p_element.XMLAttributes.FirstItem(); } else { xmlAttr = (InDesign.XMLAttribute)p_element.XMLAttributes.NextItem(xmlAttr); } if (FONT_FAMILY_ATTRIBUTE.Equals(xmlAttr.Name)) { fontfamily = xmlAttr.Value; } if (FONT_STYLE_ATTRIBUTE.Equals(xmlAttr.Name)) { fontstylename = xmlAttr.Value; } if (FONT_SIZE_ATTRIBUTE.Equals(xmlAttr.Name)) { paragraph.PointSize = xmlAttr.Value; } } font = GetProperFont(fontfamily, fontstylename); if (font != null) { paragraph.AppliedFont = font; } } }
/// <summary> /// Mark up the paragraph and style of indd file with special xml tags. /// </summary> private void MarkupInddParagraph(InDesign.XMLElement p_parentElm, InDesign.Paragraph p_paragraph) { InDesign.XMLElement xmlElement = null; InDesign.Font font = null; // Add a new xml element which will be used to mark up a paragraph. InDesign.XMLElements subElements = p_parentElm.XMLElements; xmlElement = subElements.Add(INDD_PARAGRAPH_TAG, INDD_XMLCONTENT); // Get the paragraph's attributes to be translated. // For example // FontFamily: Times New Roman // PointSize: 12 font = (InDesign.Font)p_paragraph.AppliedFont; xmlElement.XMLAttributes.Add(FONT_FAMILY_ATTRIBUTE, font.FontFamily); xmlElement.XMLAttributes.Add(FONT_STYLE_ATTRIBUTE, p_paragraph.FontStyle); xmlElement.XMLAttributes.Add(FONT_SIZE_ATTRIBUTE, p_paragraph.PointSize.ToString()); // Markup the paragraph with xml element // so that it can be exported into xml file. p_paragraph.Markup(xmlElement); }
/// <summary> /// Get all fonts supported by InDesign and store them in a hashtable, /// so that the proper font can be find to update paragraph's style. /// </summary> private void InitializeFontTable() { InDesign.Font font = null; if (fontTable == null) { fontTable = new Hashtable(); } for (int i = 0; i < m_inDesignApp.Fonts.Count; i++) { if (i == 0) { font = (InDesign.Font)m_inDesignApp.Fonts.FirstItem(); } else { font = (InDesign.Font)m_inDesignApp.Fonts.NextItem(font); } string key = GenerateFontKey(font.FontFamily, font.FontStyleName); fontTable.Add(key, font); } }