# Copyright 2018-2026 Jérôme Dumonteil
# Copyright (c) 2009-2010 Ars Aperta, Itaapy, Pierlis, Talend.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Authors (odfdo project): jerome.dumonteil@gmail.com
# The odfdo project is a derivative work of the lpod-python project:
# https://github.com/lpod/lpod-python
# Authors: David Versmisse <david.versmisse@itaapy.com>
#          Hervé Cauwelier <herve@itaapy.com>
#          Romain Gauthier <romain@itaapy.com>
"""Style-related constants and mappings for ODF documents.

This module defines constants and dictionaries that map style family names
to their corresponding ODF tag names, as specified in the ODF 1.2 standard.
It is used internally to manage and register different types of styles.
"""

from __future__ import annotations

# style:family as defined by ODF 1.2, e.g. xxx possibily for:
# 'style:style style:family="xxx"'
FAMILY_ODF_STD = {
    "chart",
    "drawing-page",
    "graphic",
    "paragraph",
    "presentation",
    "ruby",
    "section",
    "table",
    "table-cell",
    "table-column",
    "table-row",
    "text",
}

_BASE_FAMILY_MAP = {k: "style:style" for k in FAMILY_ODF_STD}  # noqa:C420

_FALSE_FAMILY_MAP = {
    "background-image": "style:background-image",
    "date": "number:date-style",
    "font-face": "style:font-face",
    "list": "text:list-style",
    "marker": "draw:marker",
    "number": "number:number-style",
    "outline": "text:outline-style",
    "page-layout": "style:page-layout",
    "percentage": "number:percentage-style",
    "presentation-page-layout": "style:presentation-page-layout",
    "time": "number:time-style",
    "boolean": "number:boolean-style",
    "currency": "number:currency-style",
    "master-page": "style:master-page",
}
# "tab-stop": "style:tab-stop", Defined in toc.py
# "master-page": "style:master-page",


OTHER_STYLES = {
    "style:default-style",
    "style:footer-style",
    "style:header-style",
    "text:list-level-style-bullet",
    "text:list-level-style-image",
    "text:list-level-style-number",
}

SUBCLASS_STYLES = {"background-image"}

FAMILY_MAPPING = {**_BASE_FAMILY_MAP, **_FALSE_FAMILY_MAP}

FALSE_FAMILY_MAP_REVERSE = {v: k for k, v in _FALSE_FAMILY_MAP.items()}
SUBCLASSED_STYLES = {
    "style:background-image",
    "style:master-page",
    "style:page-layout",
    "draw:marker",
}
STYLES_TO_REGISTER = (set(FAMILY_MAPPING.values()) | OTHER_STYLES) - SUBCLASSED_STYLES

# This mapping is not exhaustive, it only contains cases where
# replacing '_' with '-' and adding the "fo:" prefix is not enough
_BASE_PROPERTY_MAPPING = {  # text
    "family_generic": "style:font-family-generic",
    "font": "style:font-name",
    "outline": "style:text-outline",
    "pitch": "style:font-pitch",
    "size": "fo:font-size",
    "style": "fo:font-style",
    "underline": "style:text-underline-style",
    "weight": "fo:font-weight",
    # compliance with office suites
    "font_family": "fo:font-family",
    "font_style_name": "style:font-style-name",
    # paragraph
    "align-last": "fo:text-align-last",
    "align": "fo:text-align",
    "indent": "fo:text-indent",
    "together": "fo:keep-together",
    # style:paragraph-properties : built
    # frame position
    "horizontal_pos": "style:horizontal-pos",
    "horizontal_rel": "style:horizontal-rel",
    "vertical_pos": "style:vertical-pos",
    "vertical_rel": "style:vertical-rel",
    # TODO 'page-break-before': 'fo:page-break-before',
    # TODO 'page-break-after': 'fo:page-break-after',
    # Graphic
    "fill_color": "draw:fill-color",
    "fill_image_height": "draw:fill-image-height",
    "fill_image_width": "draw:fill-image-width",
    "guide_distance": "draw:guide-distance",
    "guide_overhang": "draw:guide-overhang",
    "line_distance": "draw:line-distance",
    "stroke": "draw:stroke",
    "textarea_vertical_align": "draw:textarea-vertical-align",
}

STYLE_ATTRIBUTES = {
    # 17.6<style:paragraph-properties>
    "paragraph": {
        "fo:background-color",
        "fo:border",
        "fo:border-bottom",
        "fo:border-left",
        "fo:border-right",
        "fo:border-top",
        "fo:break-after",
        "fo:break-before",
        "fo:hyphenation-keep",
        "fo:hyphenation-ladder-count",
        "fo:keep-together",
        "fo:keep-with-next",
        "fo:line-height",
        "fo:margin",
        "fo:margin-bottom",
        "fo:margin-left",
        "fo:margin-right",
        "fo:margin-top",
        "fo:orphans",
        "fo:padding",
        "fo:padding-bottom",
        "fo:padding-left",
        "fo:padding-right",
        "fo:padding-top",
        "fo:text-align",
        "fo:text-align-last",
        "fo:text-indent",
        "fo:widows",
        "style:auto-text-indent",
        "style:background-transparency",
        "style:border-line-width",
        "style:border-line-width-bottom",
        "style:border-line-width-left",
        "style:border-line-width-right",
        "style:border-line-width-top",
        "style:font-independent-line-spacing",
        "style:join-border",
        "style:justify-single-word",
        "style:line-break",
        "style:line-height-at-least",
        "style:line-spacing",
        "style:page-number",
        "style:punctuation-wrap",
        "style:register-true",
        "style:shadow",
        "style:snap-to-layout-grid",
        "style:tab-stop-distance",
        "style:text-autospace",
        "style:vertical-align",
        "style:writing-mode",
        "style:writing-mode-automatic",
        "text:line-number",
        "text:number-lines",
    },
    # 16.27.28<style:text-properties>
    "text": {
        "fo:background-color",
        "fo:color",
        "fo:country",
        "fo:font-family",
        "fo:font-size",
        "fo:font-style",
        "fo:font-variant",
        "fo:font-weight",
        "fo:hyphenate",
        "fo:hyphenation-push-char-count",
        "fo:hyphenation-remain-char-count",
        "fo:language",
        "fo:letter-spacing",
        "fo:script",
        "fo:text-shadow",
        "fo:text-transform",
        "style:country-asian",
        "style:country-complex",
        "style:font-charset",
        "style:font-charset-asian",
        "style:font-charset-complex",
        "style:font-family-asian",
        "style:font-family-complex",
        "style:font-family-generic",
        "style:font-family-generic-asian",
        "style:font-family-generic-complex",
        "style:font-name",
        "style:font-name-asian",
        "style:font-name-complex",
        "style:font-pitch",
        "style:font-pitch-asian",
        "style:font-pitch-complex",
        "style:font-relief",
        "style:font-size-asian",
        "style:font-size-complex",
        "style:font-size-rel",
        "style:font-size-rel-asian",
        "style:font-size-rel-complex",
        "style:font-style-asian",
        "style:font-style-complex",
        "style:font-style-name",
        "style:font-style-name-asian",
        "style:font-style-name-complex",
        "style:font-weight-asian",
        "style:font-weight-complex",
        "style:language-asian",
        "style:language-complex",
        "style:letter-kerning",
        "style:rfc-language-tag",
        "style:rfc-language-tag-asian",
        "style:rfc-language-tag-complex",
        "style:script-asian",
        "style:script-complex",
        "style:script-type",
        "style:text-blinking",
        "style:text-combine",
        "style:text-combine-end-char",
        "style:text-combine-start-char",
        "style:text-emphasize",
        "style:text-line-through-color",
        "style:text-line-through-mode",
        "style:text-line-through-style",
        "style:text-line-through-text",
        "style:text-line-through-text-style",
        "style:text-line-through-type",
        "style:text-line-through-width",
        "style:text-outline",
        "style:text-overline-color",
        "style:text-overline-mode",
        "style:text-overline-style",
        "style:text-overline-type",
        "style:text-overline-width",
        "style:text-position",
        "style:text-rotation-angle",
        "style:text-rotation-scale",
        "style:text-scale",
        "style:text-underline-color",
        "style:text-underline-mode",
        "style:text-underline-style",
        "style:text-underline-type",
        "style:text-underline-width",
        "style:use-window-font-color",
        "text:condition",
        "text:display",
    },
    "page-layout": {
        "fo:background-color",
        "fo:border",
        "fo:border-bottom",
        "fo:border-left",
        "fo:border-right",
        "fo:border-top",
        "fo:margin",
        "fo:margin-bottom",
        "fo:margin-left",
        "fo:margin-right",
        "fo:margin-top",
        "fo:padding",
        "fo:padding-bottom",
        "fo:padding-left",
        "fo:padding-right",
        "fo:padding-top",
        "fo:page-height",
        "fo:page-width",
        "style:border-line-width",
        "style:border-line-width-bottom",
        "style:border-line-width-left",
        "style:border-line-width-right",
        "style:border-line-width-top",
        "style:first-page-number",
        "style:footnote-max-height",
        "style:footnote-sep",
        "style:layout-grid-base-height",
        "style:layout-grid-base-width",
        "style:layout-grid-color",
        "style:layout-grid-display",
        "style:layout-grid-lines",
        "style:layout-grid-mode",
        "style:layout-grid-print",
        "style:layout-grid-ruby-below",
        "style:layout-grid-ruby-height",
        "style:layout-grid-snap-to",
        "style:layout-grid-standard-mode",
        "style:num-format",
        "style:num-letter-sync",
        "style:num-prefix",
        "style:num-suffix",
        "style:paper-tray-name",
        "style:print",
        "style:print-orientation",
        "style:print-page-order",
        "style:register-truth-ref-style-name",
        "style:scale-to",
        "style:scale-to-pages",
        "style:shadow",
        "style:table-centering",
        "style:writing-mode",
    },
    # 17.10<style:ruby-properties>
    "ruby": {"style:ruby-align", "style:ruby-position"},
    # 17.11<style:section-properties>
    "section": {
        "fo:background-color",
        "fo:margin-left",
        "fo:margin-right",
        "style:editable",
        "style:protect",
        "style:writing-mode",
        "text:dont-balance-text-columns",
    },
    # 17.15<style:table-properties>
    "table": {
        "fo:background-color",
        "fo:break-after",
        "fo:break-before",
        "fo:keep-with-next",
        "fo:margin",
        "fo:margin-bottom",
        "fo:margin-left",
        "fo:margin-right",
        "fo:margin-top",
        "style:may-break-between-rows",
        "style:page-number",
        "style:rel-width",
        "style:shadow",
        "style:width",
        "style:writing-mode",
        "table:align",
        "table:border-model",
        "table:display",
    },
    # 17.16<style:table-column-properties>
    "table-column": {
        "fo:break-after",
        "fo:break-before",
        "style:column-width",
        "style:rel-column-width",
        "style:use-optimal-column-width",
    },
    # 17.17<style:table-row-properties>
    "table-row": {
        "fo:background-color",
        "fo:break-after",
        "fo:break-before",
        "fo:keep-together",
        "style:min-row-height",
        "style:row-height",
        "style:use-optimal-row-height",
    },
    # 17.18<style:table-cell-properties>
    "table-cell": {
        "fo:background-color",
        "fo:border",
        "fo:border-bottom",
        "fo:border-left",
        "fo:border-right",
        "fo:border-top",
        "fo:padding",
        "fo:padding-bottom",
        "fo:padding-left",
        "fo:padding-right",
        "fo:padding-top",
        "fo:wrap-option",
        "style:border-line-width",
        "style:border-line-width-bottom",
        "style:border-line-width-left",
        "style:border-line-width-right",
        "style:border-line-width-top",
        "style:cell-protect",
        "style:decimal-places",
        "style:diagonal-bl-tr",
        "style:diagonal-bl-tr-widths",
        "style:diagonal-tl-br",
        "style:diagonal-tl-br-widths",
        "style:direction",
        "style:glyph-orientation-vertical",
        "style:print-content",
        "style:repeat-content",
        "style:rotation-align",
        "style:rotation-angle",
        "style:shadow",
        "style:shrink-to-fit",
        "style:text-align-source",
        "style:vertical-align",
        "style:writing-mode",
    },
    # 17.21<style:graphic-properties>
    "graphic": {
        "dr3d:ambient-color",
        "dr3d:backface-culling",
        "dr3d:back-scale",
        "dr3d:close-back",
        "dr3d:close-front",
        "dr3d:depth",
        "dr3d:diffuse-color",
        "dr3d:edge-rounding",
        "dr3d:edge-rounding-mode",
        "dr3d:emissive-color",
        "dr3d:end-angle",
        "dr3d:horizontal-segments",
        "dr3d:lighting-mode",
        "dr3d:normals-direction",
        "dr3d:normals-kind",
        "dr3d:shadow",
        "dr3d:shininess",
        "dr3d:specular-color",
        "dr3d:texture-filter",
        "dr3d:texture-generation-mode-x",
        "dr3d:texture-generation-mode-y",
        "dr3d:texture-kind",
        "dr3d:texture-mode",
        "dr3d:vertical-segments",
        "draw:auto-grow-height",
        "draw:auto-grow-width",
        "draw:blue",
        "draw:caption-angle",
        "draw:caption-angle-type",
        "draw:caption-escape",
        "draw:caption-escape-direction",
        "draw:caption-fit-line-length",
        "draw:caption-gap",
        "draw:caption-line-length",
        "draw:caption-type",
        "draw:color-inversion",
        "draw:color-mode",
        "draw:contrast",
        "draw:decimal-places",
        "draw:draw-aspect",
        "draw:end-guide",
        "draw:end-line-spacing-horizontal",
        "draw:end-line-spacing-vertical",
        "draw:fill",
        "draw:fill-color",
        "draw:fill-gradient-name",
        "draw:fill-hatch-name",
        "draw:fill-hatch-solid",
        "draw:fill-image-height",
        "draw:fill-image-name",
        "draw:fill-image-ref-point",
        "draw:fill-image-ref-point-x",
        "draw:fill-image-ref-point-y",
        "draw:fill-image-width",
        "draw:fit-to-contour",
        "draw:fit-to-size",
        "draw:frame-display-border",
        "draw:frame-display-scrollbar",
        "draw:frame-margin-horizontal",
        "draw:frame-margin-vertical",
        "draw:gamma",
        "draw:gradient-step-count",
        "draw:green",
        "draw:guide-distance",
        "draw:guide-overhang",
        "draw:image-opacity",
        "draw:line-distance",
        "draw:luminance",
        "draw:marker-end",
        "draw:marker-end-center",
        "draw:marker-end-width",
        "draw:marker-start",
        "draw:marker-start-center",
        "draw:marker-start-width",
        "draw:measure-align",
        "draw:measure-vertical-align",
        "draw:ole-draw-aspect",
        "draw:opacity",
        "draw:opacity-name",
        "draw:parallel",
        "draw:placing",
        "draw:red",
        "draw:secondary-fill-color",
        "draw:shadow",
        "draw:shadow-color",
        "draw:shadow-offset-x",
        "draw:shadow-offset-y",
        "draw:shadow-opacity",
        "draw:show-unit",
        "draw:start-guide",
        "draw:start-line-spacing-horizontal",
        "draw:start-line-spacing-vertical",
        "draw:stroke",
        "draw:stroke-dash",
        "draw:stroke-dash-names",
        "draw:stroke-linejoin",
        "draw:symbol-color",
        "draw:textarea-horizontal-align",
        "draw:textarea-vertical-align",
        "draw:tile-repeat-offset",
        "draw:unit",
        "draw:visible-area-height",
        "draw:visible-area-left",
        "draw:visible-area-top",
        "draw:visible-area-width",
        "draw:wrap-influence-on-position",
        "fo:background-color",
        "fo:border",
        "fo:border-bottom",
        "fo:border-left",
        "fo:border-right",
        "fo:border-top",
        "fo:clip",
        "fo:margin",
        "fo:margin-bottom",
        "fo:margin-left",
        "fo:margin-right",
        "fo:margin-top",
        "fo:max-height",
        "fo:max-width",
        "fo:min-height",
        "fo:min-width",
        "fo:padding",
        "fo:padding-bottom",
        "fo:padding-left",
        "fo:padding-right",
        "fo:padding-top",
        "fo:wrap-option",
        "style:background-transparency",
        "style:border-line-width",
        "style:border-line-width-bottom",
        "style:border-line-width-left",
        "style:border-line-width-right",
        "style:border-line-width-top",
        "style:editable",
        "style:flow-with-text",
        "style:horizontal-pos",
        "style:horizontal-rel",
        "style:mirror",
        "style:number-wrapped-paragraphs",
        "style:overflow-behavior",
        "style:print-content",
        "style:protect",
        "style:rel-height",
        "style:rel-width",
        "style:repeat",
        "style:run-through",
        "style:shadow",
        "style:shrink-to-fit",
        "style:vertical-pos",
        "style:vertical-rel",
        "style:wrap",
        "style:wrap-contour",
        "style:wrap-contour-mode",
        "style:wrap-dynamic-threshold",
        "style:writing-mode",
        "svg:fill-rule",
        "svg:height",
        "svg:stroke-color",
        "svg:stroke-linecap",
        "svg:stroke-opacity",
        "svg:stroke-width",
        "svg:width",
        "svg:x",
        "svg:y",
        "text:anchor-page-number",
        "text:anchor-type",
        "text:animation",
        "text:animation-delay",
        "text:animation-direction",
        "text:animation-repeat",
        "text:animation-start-inside",
        "text:animation-steps",
        "text:animation-stop-inside",
    },
}
