The hardware-based right justification is known to be buggy.
Given the existence of 1px-wide spaces (as discussed in #1), it is possible to fake right-justification (or indeed draw characters at any X position and with any desired spacing).
I prepared the following kludge to fix the right justification in a production environment:
_DISPLAY_WIDTH=120_CHARS_BY_WIDTH= {
3: b'.',
4: b'I1: ',
5: b'0-',
6: b'ABCDEFGHJKLMNOPQRSTUVWXYZ23456789',
}
_WIDTHS_BY_CHAR= {}
forwidth, charsin_CHARS_BY_WIDTH.items():
forcharinchars:
_WIDTHS_BY_CHAR[char] =widthdefpixel_width(string):
width=0forcharinstring:
ifcharnotin_WIDTHS_BY_CHAR:
raiseValueError('unknown width for character "%s"'%char)
width+=_WIDTHS_BY_CHAR[char]
returnwidthdeffix_right_justification(bytestring):
ifrb'\R'notinbytestring:
returnbytestringleft_start=7left_end=bytestring.index(rb'\R', left_start)
right_start=left_end+len(rb'\R')
right_end=bytestring.index(
b'\n', right_start
) ifb'\n'inbytestring[right_start:] elsebytestring.index(
b'\r', right_start)
left=bytestring[left_start:left_end]
right=bytestring[right_start:right_end]
left_width=pixel_width(left)
right_width=pixel_width(right)
padding_width=_DISPLAY_WIDTH-2-left_width-right_widthpadding=b''space_width=_WIDTHS_BY_CHAR[ord(' ')]
whilepadding_width>=space_width:
padding+=b' 'padding_width-=space_widthwhilepadding_width>0:
padding+=b'\xff'padding_width-=1returnbytestring[:left_end] +padding+bytestring[right_start:]fix_right_justification takes bytes as returned by DisplayMessage.to_bytes() and further modifies them.
Think about how best this could be incorporated into the library.
The hardware-based right justification is known to be buggy.
Given the existence of 1px-wide spaces (as discussed in #1), it is possible to fake right-justification (or indeed draw characters at any X position and with any desired spacing).
I prepared the following kludge to fix the right justification in a production environment:
fix_right_justificationtakes bytes as returned byDisplayMessage.to_bytes()and further modifies them.Think about how best this could be incorporated into the library.