Dictionary <string, TextStyleParameters> MergeStyles(string defaultStyleID, List <CssTag> customTags, bool mergeExistingStyles = true, bool includeExistingStyles = true) { var customCSS = new StringBuilder(); foreach (var customTag in customTags) { customCSS.AppendLine(customTag.CSS); } var customStyles = CssTextStyleParser.Parse(customCSS.ToString()); var defaultStyle = _textStyles[defaultStyleID]; if (defaultStyle == null) { throw new Exception("Default Style ID not found: " + defaultStyleID); } TextStyleParameters existingStyle; foreach (var style in customStyles) { if (mergeExistingStyles) { _textStyles.TryGetValue(style.Key, out existingStyle); if (existingStyle != null) { style.Value.Merge(existingStyle, false); } else { style.Value.Merge(defaultStyle, false); } } // If no font, use the default one if (string.IsNullOrEmpty(style.Value.Font)) { style.Value.Font = defaultStyle.Font; } } return(customStyles); }
/// <summary> /// Sets the CSS string /// </summary> /// <param name="css">Css Style Sheet</param> public virtual void SetCSS(string css) { var styles = CssTextStyleParser.Parse(css); SetStyles(styles); }
/// <summary> /// Styles the string adding all necessary HTML information /// </summary> /// <returns>Formatted HTML string</returns> /// <param name="source">Source text</param> /// <param name="textStyles">Dictionary of TextStyles</param> /// <param name="customTags">Custom CSS tags.</param> /// <param name="useExistingStyles">If set to <c>true</c> use existing styles.</param> public static string StyleString(string source, Dictionary <string, TextStyleParameters> textStyles, List <CssTag> customTags, bool useExistingStyles) { // TODO move this into the iOS library as it creates HTML specific to iOS / OR update this so it displays full HTML? var styles = @"<style>"; var specifiedTags = new List <string>(); if (customTags != null) { foreach (var customTag in customTags) { if (!string.IsNullOrEmpty(customTag.Name)) { var curStyle = textStyles[customTag.Name]; if (curStyle != null) { // Append any CSS to the style if (!String.IsNullOrEmpty(customTag.CSS)) { curStyle = CssTextStyleParser.MergeRule(curStyle, customTag.CSS, true); } styles += CssTextStyleParser.ParseToCSSString(customTag.Tag, curStyle); specifiedTags.Add(customTag.Tag); } } else if (!string.IsNullOrEmpty(customTag.CSS)) { // See if this style exists already, if it does CLONE the TextStyleParameter and merge the new CSS in styles += customTag.CSS; specifiedTags.Add(customTag.Tag); } } } // Update any missing tags with existing styles if (useExistingStyles) { // Find all tags specified in the source var regexResults = tagNamesRegex.Matches(source); var tagNames = regexResults.Cast <Match>() .Where(x => x.Groups.Count > 1) .Select(x => x.Groups[1].Value) .Distinct().ToList(); var missingTags = tagNames.Except(specifiedTags); foreach (var tagName in missingTags) { if (textStyles.ContainsKey(tagName)) { var curStyle = textStyles[tagName]; var newStyle = CssTextStyleParser.ParseToCSSString(tagName, curStyle); styles += newStyle; } } // Add body if not declared var bodyTagExists = customTags != null && customTags.Any(x => x.Tag == BODYTAG); if (!bodyTagExists && textStyles.ContainsKey(BODYTAG)) { var bodyStyle = textStyles[BODYTAG]; var bodyCSS = CssTextStyleParser.ParseToCSSString(BODYTAG, bodyStyle); styles += bodyCSS; } } styles += @"</style>"; source += styles; return(source); }