adafruit / Adafruit_CircuitPython_Display_Text

Library to display text using displayio
MIT License
57 stars 38 forks source link

initial Commit for draft Bitmap Label directional label #145

Closed jposada202020 closed 3 years ago

jposada202020 commented 3 years ago

@kmatch98 I will need your help here, as under the hood labels are different, If we want to implement directional label for the bitmaplabel, we need to do a different approach as we do not have the same tools, what would you recommend? do we need a rotation matrix, we need do -1 and also a rotation.

Thanks šŸ§‘ā€šŸ”¬

kmatch98 commented 3 years ago

Probably the simplest approach is to generate the bitmap_label bitmap as usual and then do some transformation into the final bitmap.

Here are two approaches I can think of:

  1. Use the bitmaptools.rotozoom function to do a rotational "blit" into a new bitmap. (This function can do any angle you want.) You will need to calculate the size of the final "rotated" bitmap before you perform the "rotozoom" into it. If you are only allowing 90 degree increments, the the bitmap size calculation is easy. One downside is there could be some integer rounding errors that could make the final result not match the original bitmap.

  2. Write a bitmap transformation function to do the 90 degree angles. Since you are only doing 90 degree angles it is much simpler math to calculate each pixel's position. And you can write this (to avoid rounding erros) to ensure the rotated bitmap exactly matches the original.

If you want to get more complex, you could modify the character-by-character blitting function and then adjust the rotation and position of each character individually. This more complex approach would probably use the lowest amount of RAM during the process, since you won't need two bitmaps (like #1 and #2 above).

Let me know your thoughts or if you'd like other suggestions.

kmatch98 commented 3 years ago

Oh! I just remembered that TileGrid has a rotation function. Since the bitmap_label is in a TileGrid, it's probably easiest to just manipulate that.

jposada202020 commented 3 years ago

@kmatch98 Thanks for the info. For the RTL (Right-to-Left) text and TTB Top-To-Bottom I think I will use the same approach that I used in label. You could used in release 2.17.0 I think that for text upwards and downwards text I will explore the rotozoom option as you mentioned. I just want to preserve the memory efficiency and speed of the bitmap_label and I did not wanted to do a "Tilegrid" conversion, as this is in every glyph and the the bitmap one. Thanks for pointing me in the right direction you saved me a lot of time :). But I think it will be a win if at least people will have the Upwards and Downwards feature not too slow. Thanks

jposada202020 commented 3 years ago

Four directions are implemented here similar to label:

I am not seeing an easy way to implement TTB, so I think is better to maintain the memory efficiency and if people need text from top to bottom they can use label. Still having problems when anchoring positions, I try to troubleshoot a little but with no luck. I tried version 2.14 of the library and bitmap_label anchor characteristics were working. #140 . I am guessing that we are having problems in the setter, because we are receiving a None as a anchor_position.

image

@kmatch98 thanks for the suggestions, as you can see it make this a lot easier.

kmatch98 commented 3 years ago

I evaluated this and it works as expected.

Right now you are using a different method to achieve left-to-right "LTR" versus the other options. I recommend doing all of the transformation using just the tilegrid options (flip_x, flip_y and transpose_xy).

You can achieve "LTR" with self.tilegrid.flip_x=True. Also, if you want to achieve upside down, you can use self.tilegrid.flip_x = self.tilegrid.flip_y = True.

Otherwise, it looks good to me.

jposada202020 commented 3 years ago

@kmatch98 Thank you for the review, as previously mentioned thanks again for point me in the right direction, it was a breeze compared to do the same feature in label after your indications. Regarding the LTR, just to verify that I am understanding correctly, when you said flip_x I think that is more related to "RTL" that I used a string method. If so there is a difference in this

PA flip_x = AźŸ¼ PA "".join(reversed(PA)) = AP

For RTL languages we write normal just in the Left-to-right direction, and not mirroring the letters, that is why methods are different. Could you confirm your confirm that I am understanding correctly.

For the downwards, thank it was something that I discover when doing the PR, I was considering adding, but label does not have it, that is why I did not include it.

kmatch98 commented 3 years ago

Sorry that was a typo. You are right that it is RTL. Keep up the good work. When you are ready I will take another look.

jposada202020 commented 3 years ago

@kmatch98 what a ride :). As of you just a single little tiny, small tiny little space..... lol and a rabbit whole , anyway I include the UPD -Upside Down. Thanks.

FoamyGuy commented 3 years ago

I think we need to move this check for valid direction out of LabelBase and put it into each of the subclasses since they have slightly different options with UPD and TTL.

https://github.com/jposada202020/Adafruit_CircuitPython_Display_Text/blob/69909e75f3451fa1aa2730c635f95aaafaa5db79/adafruit_display_text/__init__.py#L239

I tried making an upside down label like this

text_area = bitmap_label.Label(terminalio.FONT, text=text, label_direction="UPD")

but I got this error:

code.py output:
Traceback (most recent call last):
  File "code.py", line 10, in <module>
  File "/lib/adafruit_display_text/bitmap_label.py", line 91, in __init__
  File "/lib/adafruit_display_text/__init__.py", line 239, in __init__
RuntimeError: Please provide a valid text direction
jposada202020 commented 3 years ago

@FoamyGuy good point thank you for reviewing, I'll fix that, Thanks