| 21 | from pagebot.toolbox.color import color |
| 22 | |
| 23 | class BabelRun: |
| 24 | |
| 25 | def __init__(self, s, style=None): |
| 26 | """Answers the storage for string + style in BabelString. Note that the |
| 27 | style values of sequential runs are *not* cascading. This is similar to |
| 28 | the behavior of the DrawBot FormattedString attributes. |
| 29 | |
| 30 | |
| 31 | >>> from pagebot.elements import * |
| 32 | >>> BabelRun('ABCD', dict(font='PageBot-Regular')) |
| 33 | <BabelRun "ABCD"> |
| 34 | >>> BabelRun('ABCD'*10) # Abbreviated for string > 10 characters. |
| 35 | <BabelRun "ABCDABCDAB..."> |
| 36 | >>> len(BabelRun('ABCD'*10)) |
| 37 | 40 |
| 38 | """ |
| 39 | assert isinstance(s, str) |
| 40 | self.s = s |
| 41 | |
| 42 | if style is None: |
| 43 | style = {} |
| 44 | self.style = style |
| 45 | |
| 46 | self._cr = None # Optional cache of native context run (e.g. CTRun or FlatRunData) |
| 47 | |
| 48 | def __len__(self): |
| 49 | return len(self.s) |
| 50 | |
| 51 | def __eq__(self, pbr): |
| 52 | """ |
| 53 | >>> from pagebot.toolbox.units import pt |
| 54 | >>> br1 = BabelRun('ABCD') |
| 55 | >>> br2 = BabelRun('ABCD') |
| 56 | >>> br1 == br2, br1 is br2 |
| 57 | (True, False) |
| 58 | >>> br2.style['fontSize'] = pt(12) |
| 59 | >>> br1 == br2 |
| 60 | False |
| 61 | """ |
| 62 | if not isinstance(pbr, self.__class__): |
| 63 | return False |
| 64 | return self.s == pbr.s and self.style == pbr.style |
| 65 | |
| 66 | def __repr__(self): |
| 67 | r = '<%s' % self.__class__.__name__ |
| 68 | if self.s: |
| 69 | s = self.s[:10] |
| 70 | if s != self.s: |
| 71 | s += '...' |
| 72 | r += ' "%s"' % s.replace('\n',' ') |
| 73 | return r + '>' |
| 74 | |
| 75 | def getFont(self, style=None): |
| 76 | if not style: |
| 77 | style = self.style |
| 78 | fontName = style.get('font', DEFAULT_FONT) |
| 79 | if fontName is None: |
| 80 | fontName = DEFAULT_FONT |
no outgoing calls
no test coverage detected