Make style from a copy of style dict (providing all necessary default values for the element to operate) and then overwrite these values with any specific arguments. If style is None, then create a new style dict. In that case all the element style values need to be defined by argument.
(style=None, raiseError=True, **kwargs)
| 34 | return dict(**kwargs) |
| 35 | |
| 36 | def makeStyle(style=None, raiseError=True, **kwargs): |
| 37 | """Make style from a copy of style dict (providing all necessary default |
| 38 | values for the element to operate) and then overwrite these values with any |
| 39 | specific arguments. If style is None, then create a new style dict. In |
| 40 | that case all the element style values need to be defined by argument. The |
| 41 | calling element must test if its minimum set (such as self.w and self.h) |
| 42 | are properly defined. |
| 43 | |
| 44 | >>> style = makeStyle() |
| 45 | >>> style |
| 46 | {} |
| 47 | >>> style = makeStyle(style=style) |
| 48 | >>> style['fontSize'] |
| 49 | 12pt |
| 50 | >>> style = {'bogus': 'bla'} |
| 51 | >>> style = makeStyle(style=style, raiseError=False) |
| 52 | [makeStyle] Attribute “bogus” not allowed in (root) style! |
| 53 | >>> style = {'fontSize': pt(24), 'leading': em(1.2)} |
| 54 | >>> style = makeStyle(style=style, raiseError=False) |
| 55 | >>> style['fontSize'] |
| 56 | 24pt |
| 57 | >>> style['leading'] |
| 58 | 1.2em |
| 59 | """ |
| 60 | if style is None: |
| 61 | new = newStyle(**kwargs) # Copy arguments in new style. |
| 62 | else: |
| 63 | rs = getRootStyle() |
| 64 | new = dict() |
| 65 | |
| 66 | # Check for illegal arguments. |
| 67 | for key, value in style.items(): |
| 68 | if key not in rs: |
| 69 | warning = '[makeStyle] Attribute “%s” not allowed in (root) style!' % key |
| 70 | if raiseError: |
| 71 | raise ValueError(warning) |
| 72 | print(warning) |
| 73 | else: |
| 74 | new[key] = value |
| 75 | |
| 76 | # Add kwargs. |
| 77 | for name, v in kwargs.items(): |
| 78 | if name not in rs: |
| 79 | warning = '[makeStyle] %s not allowed in (root) style!' % name |
| 80 | if raiseError: |
| 81 | raise ValueError(warning) |
| 82 | print(warning) |
| 83 | else: |
| 84 | new[name] = v # Overwrite value by any arguments, if defined. |
| 85 | |
| 86 | # FIXME: defaults cause Cocoa error, need to do some more conversions: |
| 87 | # File "/../pdfContext.py", line 371, in _nsColorToCGColor |
| 88 | # if c.numberOfComponents() == 5: |
| 89 | # AttributeError: 'NSNull' object has no attribute 'numberOfComponents' |
| 90 | # |
| 91 | # Add missing as defaults from root style. |
| 92 | for name in DEFAULTS: |
| 93 | v = rs[name] |