How to copy font settings from one run to another?

Hi,

I want to copy the font settings from one run to another (in the same document). What isn’t clear is in which order the font settings have to be copied.

I’m assuming the correct order is to first copy the theme settings, then set style, and then copy the remaining font properties, given that theme settings, style and font settings interact. Here’s the code I have right now:

public static void CopyFrom(this Font target, Font source) {

    // First set style, then font properties...
    target.ThemeFont                        = source.ThemeFont;
    target.ThemeFontAscii                   = source.ThemeFontAscii;
    target.ThemeFontBi                      = source.ThemeFontBi;
    target.ThemeFontFarEast                 = source.ThemeFontFarEast;
    target.ThemeFontOther                   = source.ThemeFontOther;
    target.ThemeColor                       = source.ThemeColor;

    target.Style                            = source.Style;

    target.AllCaps                          = source.AllCaps;
    target.Bidi                             = source.Bidi;
    target.Bold                             = source.Bold;
    target.BoldBi                           = source.BoldBi;
    target.Border.Color                     = source.Border.Color;
    target.Border.DistanceFromText          = source.Border.DistanceFromText;
    target.Border.LineStyle                 = source.Border.LineStyle;
    target.Border.LineWidth                 = source.Border.LineWidth;
    target.Border.Shadow                    = source.Border.Shadow;
    target.Color                            = source.Color;
    target.ComplexScript                    = source.ComplexScript;
    target.DoubleStrikeThrough              = source.DoubleStrikeThrough;
    target.Emboss                           = source.Emboss;
    target.EmphasisMark                     = source.EmphasisMark;
    target.Engrave                          = source.Engrave;
    target.Hidden                           = source.Hidden;
    target.HighlightColor                   = source.HighlightColor;
    target.Italic                           = source.Italic;
    target.ItalicBi                         = source.ItalicBi;
    target.Kerning                          = source.Kerning;
    target.LocaleId                         = source.LocaleId;
    target.LocaleIdBi                       = source.LocaleIdBi;
    target.LocaleIdFarEast                  = source.LocaleIdFarEast;
    target.Name                             = source.Name;
    target.NameAscii                        = source.NameAscii;
    target.NameBi                           = source.NameBi;
    target.NameFarEast                      = source.NameFarEast;
    target.NameOther                        = source.NameOther;
    target.NoProofing                       = source.NoProofing;
    target.Outline                          = source.Outline;
    target.Position                         = source.Position;
    target.Scaling                          = source.Scaling;
    target.Shading.BackgroundPatternColor   = source.Shading.BackgroundPatternColor;
    target.Shading.ForegroundPatternColor   = source.Shading.ForegroundPatternColor;
    target.Shading.Texture                  = source.Shading.Texture;
    target.Shadow                           = source.Shadow;
    target.Size                             = source.Size;
    target.SizeBi                           = source.SizeBi;
    target.SmallCaps                        = source.SmallCaps;
    target.SnapToGrid                       = source.SnapToGrid;
    target.Spacing                          = source.Spacing;
    target.StrikeThrough                    = source.StrikeThrough;
    target.Subscript                        = source.Subscript;
    target.Superscript                      = source.Superscript;
    target.TextEffect                       = source.TextEffect;
    try {
    target.TintAndShade                     = source.TintAndShade;  // Fails if ThemeColor is not set
    } catch {}  
    target.Underline                        = source.Underline;
    target.UnderlineColor                   = source.UnderlineColor;
}

Is my approach correct?

Thanks,

Christophe

@info.pts.lu

To copy font settings from one run to another in the same document, your approach is generally correct. You should indeed consider the order of copying the properties, as the theme settings, style, and font properties can interact with each other.

Here’s a refined version of your method that maintains the order of copying properties effectively:

public static void CopyFrom(this Font target, Font source) {
    // First, copy theme settings
    target.ThemeFont = source.ThemeFont;
    target.ThemeFontAscii = source.ThemeFontAscii;
    target.ThemeFontBi = source.ThemeFontBi;
    target.ThemeFontFarEast = source.ThemeFontFarEast;
    target.ThemeFontOther = source.ThemeFontOther;
    target.ThemeColor = source.ThemeColor;

    // Then, set the style
    target.Style = source.Style;

    // Finally, copy the remaining font properties
    target.AllCaps = source.AllCaps;
    target.Bidi = source.Bidi;
    target.Bold = source.Bold;
    target.BoldBi = source.BoldBi;
    target.Border.Color = source.Border.Color;
    target.Border.DistanceFromText = source.Border.DistanceFromText;
    target.Border.LineStyle = source.Border.LineStyle;
    target.Border.LineWidth = source.Border.LineWidth;
    target.Border.Shadow = source.Border.Shadow;
    target.Color = source.Color;
    target.ComplexScript = source.ComplexScript;
    target.DoubleStrikeThrough = source.DoubleStrikeThrough;
    target.Emboss = source.Emboss;
    target.EmphasisMark = source.EmphasisMark;
    target.Engrave = source.Engrave;
    target.Hidden = source.Hidden;
    target.HighlightColor = source.HighlightColor;
    target.Italic = source.Italic;
    target.ItalicBi = source.ItalicBi;
    target.Kerning = source.Kerning;
    target.LocaleId = source.LocaleId;
    target.LocaleIdBi = source.LocaleIdBi;
    target.LocaleIdFarEast = source.LocaleIdFarEast;
    target.Name = source.Name;
    target.NameAscii = source.NameAscii;
    target.NameBi = source.NameBi;
    target.NameFarEast = source.NameFarEast;
    target.NameOther = source.NameOther;
    target.NoProofing = source.NoProofing;
    target.Outline = source.Outline;
    target.Position = source.Position;
    target.Scaling = source.Scaling;
    target.Shading.BackgroundPatternColor = source.Shading.BackgroundPatternColor;
    target.Shading.ForegroundPatternColor = source.Shading.ForegroundPatternColor;
    target.Shading.Texture = source.Shading.Texture;
    target.Shadow = source.Shadow;
    target.Size = source.Size;
    target.SizeBi = source.SizeBi;
    target.SmallCaps = source.SmallCaps;
    target.SnapToGrid = source.SnapToGrid;
    target.Spacing = source.Spacing;
    target.StrikeThrough = source.StrikeThrough;
    target.Subscript = source.Subscript;
    target.Superscript = source.Superscript;
    target.TextEffect = source.TextEffect;

    // Handle potential exceptions when copying TintAndShade
    try {
        target.TintAndShade = source.TintAndShade;  // This may fail if ThemeColor is not set
    } catch {}

    target.Underline = source.Underline;
    target.UnderlineColor = source.UnderlineColor;
}

This method first copies the theme settings, then the style, and finally the individual font properties. This order ensures that the properties are applied correctly, considering their interdependencies.

If you have any further questions or need additional assistance, feel free to ask!

This bot just copy/pasted my question. Not helpful.

@info.pts.lu We have a feature request logged as WORDSNET-26633 to provide a feature to copy formatting from one Font object to another. We will keep you updated and let you know once it is available.

Unfortunately, currently there is no elegant way to copy all formatting from one Font object to another. The only way to process Font properties one by one.

Hi Alexey,

Is my approach correct (first the theme settings, then the style, then the remaining font properties)?

Thanks,

Christophe

@info.pts.lu I am afraid currently there is no a silver bullet solution for this, since some Font properties can be inherited, i.e. are not actually set in the original Font object, but are get from the object’s parent or style. When you set the properly explicitly, the result might be not the same as in the original Font object.

OK thanks Alexey!

1 Like