From d0a5b4b2a5f785ef87b7032900ad62ffce99be26 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 30 Jul 2020 17:58:21 +0530 Subject: [PATCH] DOCX Input: Leave undefined text colors in the input document as undefined (currentColor in CSS). Fixes #1888897 [Text color mangling converting from DOCX to EPUB/AZW3](https://bugs.launchpad.net/calibre/+bug/1888897) --- src/calibre/ebooks/docx/block_styles.py | 2 +- src/calibre/ebooks/docx/styles.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/docx/block_styles.py b/src/calibre/ebooks/docx/block_styles.py index fdd61add54..77c73508a0 100644 --- a/src/calibre/ebooks/docx/block_styles.py +++ b/src/calibre/ebooks/docx/block_styles.py @@ -46,7 +46,7 @@ def binary_property(parent, name, XPath, get): return True if val in {'on', '1', 'true'} else False -def simple_color(col, auto='black'): +def simple_color(col, auto='currentColor'): if not col or col == 'auto' or len(col) != 6: return auto return '#'+col diff --git a/src/calibre/ebooks/docx/styles.py b/src/calibre/ebooks/docx/styles.py index bf2eb930f1..a2400122b2 100644 --- a/src/calibre/ebooks/docx/styles.py +++ b/src/calibre/ebooks/docx/styles.py @@ -333,7 +333,7 @@ class Styles(object): def cascade(self, layers): self.body_font_family = 'serif' self.body_font_size = '10pt' - self.body_color = 'black' + self.body_color = 'currentColor' def promote_property(char_styles, block_style, prop): vals = {getattr(s, prop) for s in char_styles} @@ -359,10 +359,12 @@ class Styles(object): # The default text decoration is 'none' s.text_decoration = inherit - def promote_most_common(block_styles, prop, default): + def promote_most_common(block_styles, prop, default, inherit_means=None): c = Counter() for s in block_styles: val = getattr(s, prop) + if val is inherit and inherit_means is not None: + val = inherit_means if val is not inherit: c[val] += 1 val = None @@ -370,6 +372,8 @@ class Styles(object): val = c.most_common(1)[0][0] for s in block_styles: oval = getattr(s, prop) + if oval is inherit and inherit_means is not None: + oval = inherit_means if oval is inherit: if default != val: setattr(s, prop, default) @@ -387,7 +391,7 @@ class Styles(object): if fs is not None: self.body_font_size = '%.3gpt' % fs - color = promote_most_common(block_styles, 'color', self.body_color) + color = promote_most_common(block_styles, 'color', self.body_color, inherit_means='currentColor') if color is not None: self.body_color = color