adafruit / Adafruit_CircuitPython_Display_Text

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

Updated documentation for label.py to clarify usage for background #75

Closed kmatch98 closed 3 years ago

kmatch98 commented 4 years ago

@FoamyGuy Here are my collection of thoughts on updates for the learn guide for adafruit_display_text\label.py. I just realized that maybe this belongs in a separate repository with the learn guides, but I couldn't identify the location. If you know an alternate location, let me know. If you have time to review this list, please add your suggestions and then we can discuss how you want to coordinate or divvy-up any work on the learn guide.

Documentation updates:

Explain background size options:

Special tips on BDF loaded fonts:

FoamyGuy commented 4 years ago

Sorry it took me a bit until I got back to this. I think this is a good place to coordinate for now. Other folks can chime in if they have idea's and we'll close the issue once the edits to the guide pages are complete.

There is a learn guide repo but it contains mostly projects and sample code used in learn guides. Not the content of the guide pages.

These all look like they'd be great additions to the current guide pages. I will work on making some minimal sample code to illustrate the background size options and get some screenshots taken so we can show examples of how they different options affect the look.

I can get make images illustrating how the origin point works with multi-line text strings while I'm at it.

jposada202020 commented 3 years ago

@FoamyGuy I was looking into @kmatch98 description and the examples that he mentions here. taking into account the work being done in https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout/issues/3 Maybe we can at least the documentation done here based on the work from kmatch98. And then when the display framework is merged it will be easier to incorporate to it. I can work in the doc/learning guide based on that if kmatch has not already done all the work if required.

FoamyGuy commented 3 years ago

@jposada202020 I am definitely on board with that idea. One thing that would be great is adding a few more examples that will illustrate some of the features that are less documented. We can use screenshots from those examples in the guide page once it gets created. Perhaps this example: https://github.com/adafruit/Adafruit_CircuitPython_Display_Text/blob/master/examples/display_text_background_color_padding.py could be modified a bit to illustrate the usage of background_tight and padding values. The code in there now is essentially a test case that we noticed a bug with at some point. It's a tad more of a test that proves the bug is fixed rather than an example meant to show how to use something as is.

kmatch98 commented 3 years ago

For completeness, I thought I would add a few more text-related items to the features that need documentation. I don’t have any experience with editing learn guides but let me know if you want any contribution from my end.

Since the issue was raised above, @foamyguy and @jepler have added several other features:

FoamyGuy commented 3 years ago

All really great suggestions! Thank you @kmatch98

I have access and experience modifying the learn guides so I can go in and make those final edits when we are ready to do them.

I think the best way to collaborate on it will be working on examples and writing into txt or md files for now. Those can be checked in to a branch somewhere that we can all see, or posted on threads like this one.

Then when it's finalized I can go in and put it into the actual guides system.

I think we want to try to get at least one example that illustrates each feature. That way we can include screenshots of them in the guide, and embed the github code links to those examples in the guides.

jposada202020 commented 3 years ago

Good, I worked already in the examples to illustrate the following:

Based on https://github.com/adafruit/Adafruit_CircuitPython_Display_Text/blob/master/examples/display_text_background_color_padding.py I will need to make the screen shots I could work during the weekend in the draft version, and maybe we can discuss/edit/correct/add/remove after. I will need to read also the new points that @kmatch98 mentioned. I have not looked at those yet.

FoamyGuy commented 3 years ago

@jposada202020 Awesome! Thank you for working on this. Check out BitmapSaver library if you haven't already for saving screenshots: https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver

My PyGameDisplay driver is good for screenshots as well on PC, but does require a bit more setup

jposada202020 commented 3 years ago

Thanks @FoamyGuy I have alread done the setup with your PyGameDisplay driver. I will use that one.

jposada202020 commented 3 years ago

@FoamyGuy @kmatch98 Included the proposal for the guide/doc, I just want to go too far into this if this is not good. Let me know if this make any sense.

Also, in doing this, I have a proposal to change the library to solve issues #100 and #103. The proposal will add a new parameter call yoffset that will mask/unmask the offset value calculated with get_ascend_descend function and probably let us solve those issue, it will need some tinkering for the background box as this value is also used there. Label change proposal and Code example are here https://github.com/jposada202020/Displaytextoffset Thanks,

Label Library

The label library allows us to publish certain text into a display. There are different label parameters that allow us to set up the font, position, background characteristics among others. A Label is an artifact of an organized set of tilegrids.

Label position

There are different ways to set up the label position in the display. This includes:

x, y coordinates

x, y coordinates allows to define the label position. When setting text location using the x and y properties, you are setting the origin point. It is located relative to the text as shown below.

Anchor Points

The anchor_point property of the label is a (x, y) tuple. The values range from 0 to 1 with x being the horizontal and y being the vertical. The origin is in the upper left corner. A value of 0 is at the origin. A value of 1 is all the way to the right/down.

Anchor Position

The label position on the display could be located using the anchored_position property. The values are actual screen coordinates in pixels and defined by a tuple representing (x, y) coordinates

TileGrid Position

When you create a text label, the library will create an imaginary box that encloses all the glyphs in the text. All the glyphs present within the text will be located inside this box. This box is calculated inside the library taking into account the glyphs ascent and descent values in a particular text. When no text is provided the library will use the characters "M j ' " to calculate the boundingbox

Ascend

Is a value in pixels of the upper portion that extends from the median of a particular glyph

Descend

Is a value in pixels of the bottom portion that extends from the median of a particular glyph

Advanced text positioning

Label text positioning is calculated inside the private function

def _update_text()

Positioning in the y-axis is calculated as follows:

position_y = y - glyph.height - glyph.dy + y_offset

Label Background

Label background is an imaginary box that enclose the label. We can define the color and size of this box to create different cool effects. This library allows defining different characteristics for the label background:

Background tight

When we declare background_tight as True the background box will correspond exactly to the label boundingbox When False the ascent and descent will be added to the boundingbox dimensions.

Padding

Value in pixels of the background shift to the boundingbox dimensions values.

Other Topics

Builtin Font

When a font is not specified in Label parameters, CircuitPython uses the default font builtin. This is included in the core library fontio.

import fontio
from adafruit_display_text import label

display = board.DISPLAY
main_group = displayio.Group()

text = "CircuitPython"
text_area = label.Label(fontio.BuiltinFont(), text=text, color=0x0000FF, background_color=0xFFAA00)

Optimize Font File Size (Manually)

You could use a text editor to remove glyphs from a .bdf file. BDF files are just text!

Open a BDF file and search for “asciitilde” — this is usually the highest plain-ASCII-value glyph we want to preserve. A few lines down there will be an “ENDCHAR” line.

Delete everything after the ENDCHAR line, then add a line containing ENDFONT. That’s it! Save the file, which is usually just a small fraction of the original size.

You won’t get any accented characters or special punctuation this way, so it’s not always the right thing for every situation. For the majority of plain-text programs though, this can really help stretch your CIRCUITPY drive space!

FoamyGuy commented 3 years ago

On first read through all of this looks great! Thanks for taking the time to get this out @jposada202020. Over the weekend I'll take a deeper dive into this and start moving it into a draft page in the learn guide so we can get some feedback from kattni and/or anne.

jposada202020 commented 3 years ago

This is just a note to remind to include in the new documentation some mention regarding the Base alignment after discussion in discord. Also it would be a good to explain to people the space distance.

jposada202020 commented 3 years ago

Ascci representation the Ascent/Descent for read the docs, i cannot seem to paste it here the right way image

jposada202020 commented 3 years ago

I review the box and update the text source here: https://github.com/jposada202020/Test_Images/blob/main/version2.py Updated version up the above post: https://github.com/jposada202020/Test_Images/blob/main/label_documentation.md Please review and add, I am technical service person and not too much a teacher, so I simplified a lot everything because I start from the base that people will know a little, but for this I do not think is the case, so I am aware that changes are needed. Thanks

FoamyGuy commented 3 years ago

The new guide for display_text library went live today! https://learn.adafruit.com/circuitpython-display_text-library

kmatch98 commented 3 years ago

Super awesome. @FoamyGuy and @jposada202020 thanks for all your additions and documentation to make this even more useful for folks!

jposada202020 commented 3 years ago

@FoamyGuy Amazing guide. Nice graphics!!! and very detailed explanations, every possible parameter was described and account for. Excelent work here!

ladyada commented 3 years ago

yes! its great, good work :)