diff --git a/libraries/pico_graphics/pico_graphics.cpp b/libraries/pico_graphics/pico_graphics.cpp index 1995573d..3fcc6491 100644 --- a/libraries/pico_graphics/pico_graphics.cpp +++ b/libraries/pico_graphics/pico_graphics.cpp @@ -50,6 +50,10 @@ namespace pimoroni { } } + void PicoGraphics::set_thickness(uint t) { + thickness = t; + } + void PicoGraphics::set_clip(const Rect &r) { clip = bounds.intersection(r); } @@ -284,6 +288,24 @@ namespace pimoroni { } void PicoGraphics::thick_line(Point p1, Point p2, uint thickness) { + // fast horizontal line + if(p1.y == p2.y) { + int32_t ht = thickness / 2; + int32_t start = std::min(p1.x, p2.x); + int32_t end = std::max(p1.x, p2.x); + rectangle(Rect(start, p1.y - ht, end - start, thickness)); + return; + } + + // fast vertical line + if(p1.x == p2.x) { + int32_t ht = thickness / 2; + int32_t start = std::min(p1.y, p2.y); + int32_t length = std::max(p1.y, p2.y) - start; + rectangle(Rect(p1.x - ht, start, thickness, length)); + return; + } + // general purpose line // lines are either "shallow" or "steep" based on whether the x delta // is greater than the y delta diff --git a/libraries/pico_graphics/pico_graphics.hpp b/libraries/pico_graphics/pico_graphics.hpp index 08229461..491716de 100644 --- a/libraries/pico_graphics/pico_graphics.hpp +++ b/libraries/pico_graphics/pico_graphics.hpp @@ -251,7 +251,7 @@ namespace pimoroni { virtual void set_pen(uint8_t r, uint8_t g, uint8_t b) = 0; virtual void set_pixel(const Point &p) = 0; virtual void set_pixel_span(const Point &p, uint l) = 0; - virtual void set_thickness(uint t) = 0; + void set_thickness(uint t); virtual int get_palette_size(); virtual RGB* get_palette(); @@ -304,7 +304,6 @@ namespace pimoroni { PicoGraphics_Pen1Bit(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override; void set_pixel(const Point &p) override; void set_pixel_span(const Point &p, uint l) override; @@ -321,7 +320,6 @@ namespace pimoroni { PicoGraphics_Pen1BitY(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override; void set_pixel(const Point &p) override; void set_pixel_span(const Point &p, uint l) override; @@ -364,7 +362,6 @@ namespace pimoroni { void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; @@ -397,7 +394,6 @@ namespace pimoroni { PicoGraphics_PenP4(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override {}; int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; @@ -431,7 +427,6 @@ namespace pimoroni { PicoGraphics_PenP8(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override {}; int update_pen(uint8_t i, uint8_t r, uint8_t g, uint8_t b) override; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; @@ -457,7 +452,6 @@ namespace pimoroni { PicoGraphics_PenRGB332(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override {}; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; void set_pixel(const Point &p) override; @@ -480,7 +474,6 @@ namespace pimoroni { PicoGraphics_PenRGB565(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override {}; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; void set_pixel(const Point &p) override; @@ -497,7 +490,6 @@ namespace pimoroni { PicoGraphics_PenRGB888(uint16_t width, uint16_t height, void *frame_buffer); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override {}; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; void set_pixel(const Point &p) override; @@ -570,7 +562,6 @@ namespace pimoroni { PicoGraphics_PenInky7(uint16_t width, uint16_t height, IDirectDisplayDriver &direct_display_driver); void set_pen(uint c) override; void set_pen(uint8_t r, uint8_t g, uint8_t b) override; - void set_thickness(uint t) override; int create_pen(uint8_t r, uint8_t g, uint8_t b) override; int create_pen_hsv(float h, float s, float v) override; void set_pixel(const Point &p) override; diff --git a/libraries/pico_graphics/pico_graphics_pen_1bit.cpp b/libraries/pico_graphics/pico_graphics_pen_1bit.cpp index 59f2a4af..30fb1e53 100644 --- a/libraries/pico_graphics/pico_graphics_pen_1bit.cpp +++ b/libraries/pico_graphics/pico_graphics_pen_1bit.cpp @@ -18,10 +18,6 @@ namespace pimoroni { color = std::max(r, std::max(g, b)) >> 4; } - void PicoGraphics_Pen1Bit::set_thickness(uint t) { - thickness = t; - } - void PicoGraphics_Pen1Bit::set_pixel(const Point &p) { // pointer to byte in framebuffer that contains this pixel uint8_t *buf = (uint8_t *)frame_buffer; diff --git a/libraries/pico_graphics/pico_graphics_pen_1bitY.cpp b/libraries/pico_graphics/pico_graphics_pen_1bitY.cpp index b391022d..ea4afe99 100644 --- a/libraries/pico_graphics/pico_graphics_pen_1bitY.cpp +++ b/libraries/pico_graphics/pico_graphics_pen_1bitY.cpp @@ -18,10 +18,6 @@ namespace pimoroni { color = std::max(r, std::max(g, b)); } - void PicoGraphics_Pen1BitY::set_thickness(uint t) { - thickness = t; - } - void PicoGraphics_Pen1BitY::set_pixel(const Point &p) { // pointer to byte in framebuffer that contains this pixel uint8_t *buf = (uint8_t *)frame_buffer; diff --git a/libraries/pico_graphics/pico_graphics_pen_3bit.cpp b/libraries/pico_graphics/pico_graphics_pen_3bit.cpp index 0a16c152..5faba1cd 100644 --- a/libraries/pico_graphics/pico_graphics_pen_3bit.cpp +++ b/libraries/pico_graphics/pico_graphics_pen_3bit.cpp @@ -44,9 +44,6 @@ namespace pimoroni { int PicoGraphics_Pen3Bit::create_pen_hsv(float h, float s, float v) { return RGB::from_hsv(h, s, v).to_rgb888() | 0x7f000000; } - void PicoGraphics_Pen3Bit::set_thickness(uint t) { - thickness = t; - } void PicoGraphics_Pen3Bit::set_pixel(const Point &p) { if ((color & 0x7f000000) == 0x7f000000) { set_pixel_dither(p, RGB(color)); diff --git a/libraries/pico_graphics/pico_graphics_pen_inky7.cpp b/libraries/pico_graphics/pico_graphics_pen_inky7.cpp index 8b026b96..9a6ae5b0 100644 --- a/libraries/pico_graphics/pico_graphics_pen_inky7.cpp +++ b/libraries/pico_graphics/pico_graphics_pen_inky7.cpp @@ -18,9 +18,6 @@ namespace pimoroni { int PicoGraphics_PenInky7::create_pen_hsv(float h, float s, float v) { return RGB::from_hsv(h, s, v).to_rgb888() | 0x7f000000; } - void PicoGraphics_PenInky7::set_thickness(uint t) { - thickness = t; - } void PicoGraphics_PenInky7::set_pixel(const Point &p) { if ((color & 0x7f000000) == 0x7f000000) { set_pixel_dither(p, RGB(color)); diff --git a/micropython/modules/picographics/README.md b/micropython/modules/picographics/README.md index cb42579d..030e8196 100644 --- a/micropython/modules/picographics/README.md +++ b/micropython/modules/picographics/README.md @@ -25,6 +25,7 @@ Pico Graphics replaces the individual drivers for displays- if you're been using - [Get Bounds](#get-bounds) - [Text](#text) - [Changing The Font](#changing-the-font) + - [Changing The Thickness](#changing-the-thickness) - [Drawing Text](#drawing-text) - [Basic Shapes](#basic-shapes) - [Line](#line) @@ -311,6 +312,17 @@ These are aligned horizontally (x) to their left edge, but vertically (y) to the * `serif_italic` * `serif` +#### Changing The Thickness + +Vector (Hershey) fonts are drawn with individual lines. By default these are 1px thick, making for very thin and typically illegible text. + +To change the thickness of lines used for Vector fonts, use the `set_thickness` method: + +```python +display.set_thickness(n) +``` + +Drawing thick text involves setting a lot more pixels and may slow your drawing down considerably. Be careful how and where you use this. #### Drawing Text @@ -371,7 +383,13 @@ To draw a straight line at any angle between two specified points: display.line(x1, y1, x2, y2) ``` -The X1/Y1 and X2/Y2 coordinates describe the start and end of the line respectively. +The X1/Y1 and X2/Y2 coordinates describe the start and end of the line respectively. + +If you need a thicker line, for an outline or UI elements you can supply a fifth parameter - thickness - like so: + +```python +display.line(x1, y1, x2, y2, thickness) +``` #### Circle diff --git a/micropython/modules/picographics/picographics.cpp b/micropython/modules/picographics/picographics.cpp index 0b3ad4fb..6d3bbe25 100644 --- a/micropython/modules/picographics/picographics.cpp +++ b/micropython/modules/picographics/picographics.cpp @@ -758,13 +758,6 @@ mp_obj_t ModPicoGraphics_create_pen_hsv(size_t n_args, const mp_obj_t *args) { mp_obj_t ModPicoGraphics_set_thickness(mp_obj_t self_in, mp_obj_t pen) { ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(self_in, ModPicoGraphics_obj_t); - if( - self->graphics->pen_type != PicoGraphics::PEN_1BIT - && self->graphics->pen_type != PicoGraphics::PEN_3BIT - && self->graphics->pen_type != PicoGraphics::PEN_INKY7) { - mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Thickness not supported!")); - } - self->graphics->set_thickness(mp_obj_get_int(pen)); return mp_const_none;