Class StyleContext

java.lang.Object
dev.jcputney.mjml.context.StyleContext

public class StyleContext extends Object
CSS and font state gathered during head processing. Contains fonts, font URL overrides, styles, component styles, inline styles, media queries, and fluid-on-mobile tracking.

This is one of three focused sub-contexts extracted from GlobalContext. See also MetadataContext and AttributeContext.

Thread safety: This class is not thread-safe. Each render pipeline creates its own instance.

  • Constructor Details

    • StyleContext

      public StyleContext()
      Creates a new empty style context with no fonts, styles, or media queries registered.
  • Method Details

    • addFont

      public void addFont(String name, String href)
      Registers a font with the given name and URL.
      Parameters:
      name - the font family name
      href - the URL to the font resource
    • getFonts

      public Set<StyleContext.FontDef> getFonts()
      Returns an unmodifiable set of all registered font definitions.
      Returns:
      the registered fonts
    • registerFontOverride

      public void registerFontOverride(String name, String href)
      Registers a font URL override, replacing the default URL for the given font name.
      Parameters:
      name - the font family name to override
      href - the overriding URL for the font resource
    • getFontUrlOverride

      public String getFontUrlOverride(String name)
      Returns the overridden URL for the given font name, or null if no override exists.
      Parameters:
      name - the font family name to look up
      Returns:
      the overridden URL, or null if not overridden
    • getFontUrlOverrides

      public Map<String,String> getFontUrlOverrides()
      Returns an unmodifiable map of all font URL overrides, keyed by font name.
      Returns:
      the font URL overrides map
    • addStyle

      public void addStyle(String css)
      Adds a CSS style block. Blank or null values are ignored.
      Parameters:
      css - the CSS style string to add
    • addStyleOnce

      public boolean addStyleOnce(String key, String css)
      Adds a CSS style block only if a style with the given key has not already been registered. This prevents duplicate style blocks for the same component type.
      Parameters:
      key - the unique key identifying this style block
      css - the CSS style string to add
      Returns:
      true if the style was added, false if the key was already registered
    • addComponentStyle

      public void addComponentStyle(String css)
      Adds a component-specific CSS style block. Blank or null values are ignored.
      Parameters:
      css - the component CSS style string to add
    • getComponentStyles

      public List<String> getComponentStyles()
      Returns an unmodifiable list of all registered component-specific CSS style blocks.
      Returns:
      the component styles
    • addInlineStyle

      public void addInlineStyle(String css)
      Adds an inline CSS style block. Blank or null values are ignored.
      Parameters:
      css - the inline CSS style string to add
    • getStyles

      public List<String> getStyles()
      Returns an unmodifiable list of all registered CSS style blocks.
      Returns:
      the styles
    • getInlineStyles

      public List<String> getInlineStyles()
      Returns an unmodifiable list of all registered inline CSS style blocks.
      Returns:
      the inline styles
    • addMediaQuery

      public void addMediaQuery(String className, String widthValue, String widthUnit)
      Adds a media query for responsive column width targeting the given CSS class.
      Parameters:
      className - the CSS class name for the responsive column
      widthValue - the numeric width value
      widthUnit - the unit for the width value (e.g. "%" or "px")
    • getMediaQueries

      public Set<StyleContext.MediaQuery> getMediaQueries()
      Returns an unmodifiable set of all registered media queries.
      Returns:
      the media queries
    • isFluidOnMobileUsed

      public boolean isFluidOnMobileUsed()
      Returns whether any component has indicated that fluid-on-mobile behavior is used.
      Returns:
      true if fluid-on-mobile is used
    • setFluidOnMobileUsed

      public void setFluidOnMobileUsed(boolean fluidOnMobileUsed)
      Sets whether fluid-on-mobile behavior is used in the current render.
      Parameters:
      fluidOnMobileUsed - true if fluid-on-mobile is used