kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Porównaj commity
8 Commity
fc0e6d66d2
...
2a8be11945
Autor | SHA1 | Data |
---|---|---|
Philip Howard | 2a8be11945 | |
Phil Howard | 88bd71e56f | |
Phil Howard | 7cd9187d80 | |
Phil Howard | 21e9817347 | |
Phil Howard | 4ff7a93e3e | |
Phil Howard | 0174ca57a5 | |
Phil Howard | 223fb0d291 | |
Phil Howard | 0717c57fe7 |
|
@ -25,3 +25,6 @@
|
|||
[submodule "drivers/mlx90640/src"]
|
||||
path = drivers/mlx90640/src
|
||||
url = https://github.com/melexis/mlx90640-library
|
||||
[submodule "libraries/pico_vector/pretty_poly"]
|
||||
path = libraries/pico_vector/pretty_poly
|
||||
url = https://github.com/lowfatcode/pretty-poly/
|
||||
|
|
|
@ -62,3 +62,4 @@ add_subdirectory(galactic_unicorn)
|
|||
add_subdirectory(gfx_pack)
|
||||
add_subdirectory(cosmic_unicorn)
|
||||
add_subdirectory(stellar_unicorn)
|
||||
add_subdirectory(pico_w_explorer)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
include(pico_w_explorer_vector.cmake)
|
|
@ -0,0 +1,10 @@
|
|||
add_executable(
|
||||
pico_w_explorer_vector
|
||||
pico_w_explorer_vector.cpp
|
||||
)
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(pico_w_explorer_vector pico_stdlib pico_graphics pico_vector st7789)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(pico_w_explorer_vector)
|
|
@ -0,0 +1,55 @@
|
|||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "drivers/st7789/st7789.hpp"
|
||||
#include "libraries/pico_graphics/pico_graphics.hpp"
|
||||
#include "libraries/pico_vector/pico_vector.hpp"
|
||||
|
||||
using namespace pimoroni;
|
||||
|
||||
|
||||
ST7789 st7789(320, 240, ROTATE_0, false, {PIMORONI_SPI_DEFAULT_INSTANCE, 17, SPI_DEFAULT_SCK, SPI_DEFAULT_MOSI, PIN_UNUSED, SPI_DEFAULT_MISO, 9});
|
||||
PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr);
|
||||
PicoVector vector(&graphics);
|
||||
|
||||
int main() {
|
||||
st7789.set_backlight(255);
|
||||
|
||||
Pen WHITE = graphics.create_pen(255, 255, 255);
|
||||
Pen BLACK = graphics.create_pen(0, 0, 0);
|
||||
|
||||
float angle = 0.0f;
|
||||
|
||||
while(true) {
|
||||
graphics.set_pen(BLACK);
|
||||
graphics.clear();
|
||||
|
||||
graphics.set_pen(WHITE);
|
||||
graphics.text("Hello World", Point(0, 0), 320);
|
||||
|
||||
pp_point_t outline[] = {{-128, -128}, {128, -128}, {128, 128}, {-128, 128}};
|
||||
pp_point_t hole[] = {{ -64, 64}, { 64, 64}, { 64, -64}, { -64, -64}};
|
||||
pp_path_t paths[] = {
|
||||
{.points = outline, .count = 4},
|
||||
{.points = hole, .count = 4}
|
||||
};
|
||||
pp_poly_t poly = {.paths = paths, .count = 2};
|
||||
|
||||
vector.rotate(&poly, {0, 0}, angle);
|
||||
vector.translate(&poly, {160, 120});
|
||||
|
||||
vector.draw(&poly);
|
||||
|
||||
//pp_mat3_t t = pp_mat3_identity();
|
||||
//vector.text("Hello World", {0, 0}, &t);
|
||||
|
||||
// update screen
|
||||
st7789.update(&graphics);
|
||||
|
||||
angle += 1.0f;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -44,3 +44,4 @@ add_subdirectory(gfx_pack)
|
|||
add_subdirectory(interstate75)
|
||||
add_subdirectory(cosmic_unicorn)
|
||||
add_subdirectory(stellar_unicorn)
|
||||
add_subdirectory(pico_vector)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
include(pico_vector.cmake)
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" {
|
||||
void* fileio_open(const char* filename);
|
||||
|
||||
void fileio_close(void* fhandle);
|
||||
|
||||
size_t fileio_read(void* fhandle, void *buf, size_t len);
|
||||
|
||||
int fileio_getc(void* fhandle);
|
||||
|
||||
size_t fileio_tell(void* fhandle);
|
||||
|
||||
size_t fileio_seek(void* fhandle, size_t pos);
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
extern "C" {
|
||||
void *af_malloc(size_t size);
|
||||
void *af_realloc(void *p, size_t size);
|
||||
void af_free(void *p);
|
||||
void af_debug(const char *fmt, ...);
|
||||
}
|
|
@ -0,0 +1,370 @@
|
|||
/*
|
||||
|
||||
Alright Fonts 🖍 - a font format for embedded and low resource platforms.
|
||||
|
||||
Jonathan Williamson, August 2022
|
||||
Examples, source, and more: https://github.com/lowfatcode/pretty-poly
|
||||
MIT License https://github.com/lowfatcode/pretty-poly/blob/main/LICENSE
|
||||
|
||||
An easy way to render high quality text in embedded applications running
|
||||
on resource constrained microcontrollers such as the Cortex M0 and up.
|
||||
|
||||
- OTF and TTF support: generate efficient packed fonts easily
|
||||
- Minimal data: ~4kB (40 bytes per char) for printable ASCII set (Roboto)
|
||||
- Tunable: trade off file size, contour complexity, and visual quality
|
||||
- Metrics: advance and bounding box for fast layout
|
||||
- UTF-8 or ASCII: support for non ASCII like Kanji or Cyrillic
|
||||
- Fixed scale: coords scaled to ^2 bounds for fast scaling (no divide)
|
||||
- C17 header only library: simply copy the header file into your project
|
||||
- Customised font packs: include only the characters you need
|
||||
- Simple outlines: all paths are simply polylines for easy rendering
|
||||
- Easy antialiasing: combine with Pretty Poly for quick results!
|
||||
|
||||
*/
|
||||
|
||||
#ifndef AF_INCLUDE_H
|
||||
#define AF_INCLUDE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef AF_MALLOC
|
||||
#ifndef PP_MALLOC
|
||||
#define PP_MALLOC(size) AF_MALLOC(size)
|
||||
#define PP_REALLOC(p, size) AF_REALLOC(p, size)
|
||||
#define PP_FREE(p) AF_FREE(p)
|
||||
#endif // PP_MALLOC
|
||||
#endif // AF_MALLOC
|
||||
|
||||
#ifndef AF_MALLOC
|
||||
#define AF_MALLOC(size) malloc(size)
|
||||
#define AF_REALLOC(p, size) realloc(p, size)
|
||||
#define AF_FREE(p) free(p)
|
||||
#endif // AF_MALLOC
|
||||
|
||||
#ifndef AF_FILE
|
||||
#define AF_FILE FILE*
|
||||
#define AF_FREAD(p, size, nmemb, stream) fread(p, size, nmemb, stream)
|
||||
#define AF_FGETC(stream) fgetc(stream)
|
||||
#endif
|
||||
|
||||
#ifndef AF_DEBUG
|
||||
#define AF_DEBUG(...)
|
||||
#endif
|
||||
|
||||
#include "pretty-poly.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int8_t x, y;
|
||||
} af_point_t;
|
||||
pp_point_t af_point_transform(pp_point_t *p, pp_mat3_t *m);
|
||||
|
||||
typedef struct {
|
||||
uint8_t point_count;
|
||||
af_point_t *points;
|
||||
} af_path_t;
|
||||
|
||||
typedef struct {
|
||||
char codepoint;
|
||||
int8_t x, y, w, h;
|
||||
int8_t advance;
|
||||
uint8_t path_count;
|
||||
af_path_t *paths;
|
||||
} af_glyph_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t flags;
|
||||
uint16_t glyph_count;
|
||||
af_glyph_t *glyphs;
|
||||
} af_face_t;
|
||||
|
||||
typedef enum {
|
||||
AF_H_ALIGN_LEFT = 0, AF_H_ALIGN_CENTER = 1, AF_H_ALIGN_RIGHT = 2,
|
||||
AF_H_ALIGN_JUSTIFY = 4,
|
||||
AF_V_ALIGN_TOP = 8, AF_V_ALIGN_MIDDLE = 16, AF_V_ALIGN_BOTTOM = 32
|
||||
} af_align_t;
|
||||
|
||||
typedef struct {
|
||||
af_face_t *face; // font
|
||||
float size; // text size in pixels
|
||||
float line_height; // spacing between lines (%)
|
||||
float letter_spacing; // spacing between characters (%)
|
||||
float word_spacing; // spacing between words (%)
|
||||
af_align_t align; // horizontal and vertical alignment
|
||||
pp_mat3_t *transform; // arbitrary transformation
|
||||
} af_text_metrics_t;
|
||||
|
||||
bool af_load_font_file(AF_FILE file, af_face_t *face);
|
||||
void af_render_character(af_face_t *face, const char codepoint, af_text_metrics_t *tm);
|
||||
void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm);
|
||||
pp_rect_t af_measure(af_face_t *face, const char *text, af_text_metrics_t *tm);
|
||||
|
||||
#ifdef AF_USE_PRETTY_POLY
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef AF_IMPLEMENTATION
|
||||
|
||||
|
||||
/*
|
||||
helper functions
|
||||
*/
|
||||
|
||||
// big endian file reading helpers
|
||||
uint16_t ru16(AF_FILE file) {uint8_t w[2]; AF_FREAD((char *) w, 1, 2, file); return (uint16_t)w[0] << 8 | w[1];}
|
||||
int16_t rs16(AF_FILE file) {uint8_t w[2]; AF_FREAD((char *) w, 1, 2, file); return (uint16_t)w[0] << 8 | w[1];}
|
||||
uint32_t ru32(AF_FILE file) {uint8_t dw[4]; AF_FREAD((char *)dw, 1, 4, file); return (uint32_t)dw[0] << 24 | (uint32_t)dw[1] << 16 | (uint32_t)dw[2] << 8 | dw[3];}
|
||||
uint8_t ru8(AF_FILE file) {return AF_FGETC(file);}
|
||||
int8_t rs8(AF_FILE file) {return AF_FGETC(file);}
|
||||
|
||||
bool af_load_font_file(AF_FILE file, af_face_t *face) {
|
||||
// check header magic bytes are present
|
||||
char marker[4]; AF_FREAD(marker, 1, 4, file);
|
||||
if(memcmp(marker, "af!?", 4) != 0) {
|
||||
return false; // doesn't start with magic marker
|
||||
}
|
||||
|
||||
// extract flags and ensure none set
|
||||
face->flags = ru16(file);
|
||||
if(face->flags != 0) {
|
||||
return false; // unknown flags set
|
||||
}
|
||||
|
||||
// number of glyphs, paths, and points in font
|
||||
uint16_t glyph_count = ru16(file);
|
||||
uint16_t path_count = ru16(file);
|
||||
uint16_t point_count = ru16(file);
|
||||
|
||||
// allocate buffer to store font glyph, path, and point data
|
||||
void *buffer = AF_MALLOC(sizeof(af_glyph_t) * glyph_count + \
|
||||
sizeof( af_path_t) * path_count + \
|
||||
sizeof(af_point_t) * point_count);
|
||||
|
||||
if(!buffer) {
|
||||
return false; // failed memory allocation
|
||||
}
|
||||
|
||||
af_glyph_t *glyphs = (af_glyph_t *) buffer;
|
||||
af_path_t *paths = ( af_path_t *)(glyphs + (sizeof(af_glyph_t) * glyph_count));
|
||||
af_point_t *points = (af_point_t *)( paths + (sizeof( af_path_t) * path_count));
|
||||
|
||||
// load glyph dictionary
|
||||
face->glyph_count = glyph_count;
|
||||
face->glyphs = glyphs;
|
||||
for(int i = 0; i < glyph_count; i++) {
|
||||
af_glyph_t *glyph = &face->glyphs[i];
|
||||
glyph->codepoint = ru16(file);
|
||||
glyph->x = rs8(file);
|
||||
glyph->y = rs8(file);
|
||||
glyph->w = ru8(file);
|
||||
glyph->h = ru8(file);
|
||||
glyph->advance = ru8(file);
|
||||
glyph->path_count = ru8(file);
|
||||
glyph->paths = paths;
|
||||
paths += sizeof(af_path_t) * glyph->path_count;
|
||||
}
|
||||
|
||||
// load the glyph paths
|
||||
for(int i = 0; i < glyph_count; i++) {
|
||||
af_glyph_t *glyph = &face->glyphs[i];
|
||||
for(int j = 0; j < glyph->path_count; j++) {
|
||||
af_path_t *path = &glyph->paths[j];
|
||||
path->point_count = ru8(file);
|
||||
path->points = points;
|
||||
points += sizeof(af_point_t) * path->point_count;
|
||||
}
|
||||
}
|
||||
|
||||
// load the glyph points
|
||||
for(int i = 0; i < glyph_count; i++) {
|
||||
af_glyph_t *glyph = &face->glyphs[i];
|
||||
for(int j = 0; j < glyph->path_count; j++) {
|
||||
af_path_t *path = &glyph->paths[j];
|
||||
for(int k = 0; k < path->point_count; k++) {
|
||||
af_point_t *point = &path->points[k];
|
||||
point->x = ru8(file);
|
||||
point->y = ru8(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
af_glyph_t *find_glyph(af_face_t *face, char c) {
|
||||
for(int i = 0; i < face->glyph_count; i++) {
|
||||
if(face->glyphs[i].codepoint == c) {
|
||||
return &face->glyphs[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void af_render_glyph(af_glyph_t* glyph, af_text_metrics_t *tm) {
|
||||
assert(glyph != NULL);
|
||||
|
||||
pp_poly_t poly;
|
||||
poly.count = glyph->path_count;
|
||||
poly.paths = (pp_path_t *)AF_MALLOC(poly.count * sizeof(pp_path_t));
|
||||
for(uint32_t i = 0; i < poly.count; i++) {
|
||||
pp_path_t *path = &poly.paths[i];
|
||||
path->count = glyph->paths[i].point_count;
|
||||
path->points = (pp_point_t *)AF_MALLOC(glyph->paths[i].point_count * sizeof(pp_point_t));
|
||||
for(uint32_t j = 0; j < path->count; j++) {
|
||||
pp_point_t *point = &path->points[j];
|
||||
point->x = glyph->paths[i].points[j].x;
|
||||
point->y = glyph->paths[i].points[j].y;
|
||||
}
|
||||
}
|
||||
|
||||
pp_render(&poly);
|
||||
|
||||
for(uint32_t i = 0; i < poly.count; i++) {
|
||||
pp_path_t *path = &poly.paths[i];
|
||||
AF_FREE(path->points);
|
||||
}
|
||||
AF_FREE(poly.paths);
|
||||
}
|
||||
|
||||
void af_render_character(af_face_t *face, const char c, af_text_metrics_t *tm) {
|
||||
af_glyph_t *glyph = find_glyph(face, c);
|
||||
if(!glyph) {
|
||||
return;
|
||||
}
|
||||
af_render_glyph(glyph, tm);
|
||||
}
|
||||
|
||||
int get_line_width(af_face_t *face, const char *text, af_text_metrics_t *tm) {
|
||||
int line_width = 0;
|
||||
char *end = strchr(text, '\n');
|
||||
for(char c = *text; text < end; text++, c = *text) {
|
||||
af_glyph_t *glyph = find_glyph(face, c);
|
||||
if(!glyph) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == L' ') {
|
||||
line_width += (glyph->advance * tm->word_spacing) / 100.0f;
|
||||
} else {
|
||||
line_width += (glyph->advance * tm->letter_spacing) / 100.0f;
|
||||
}
|
||||
}
|
||||
return line_width;
|
||||
}
|
||||
|
||||
int get_max_line_width(af_face_t *face, const char *text, af_text_metrics_t *tm) {
|
||||
int max_width = 0;
|
||||
|
||||
char *end = strchr(text, '\n');
|
||||
while(end) {
|
||||
int width = get_line_width(face, text, tm);
|
||||
max_width = max_width < width ? width : max_width;
|
||||
text = end + 1;
|
||||
end = strchr(text, '\n');
|
||||
}
|
||||
|
||||
return max_width;
|
||||
}
|
||||
|
||||
|
||||
void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
|
||||
pp_mat3_t *old = pp_transform(NULL);
|
||||
|
||||
float line_height = (tm->line_height * 128.0f) / 100.0f;
|
||||
float scale = tm->size / 128.0f;
|
||||
|
||||
// find maximum line length
|
||||
int max_line_width = get_max_line_width(face, text, tm);
|
||||
|
||||
struct {
|
||||
float x, y;
|
||||
} caret;
|
||||
|
||||
caret.x = 0;
|
||||
caret.y = 0;
|
||||
|
||||
char *end = strchr(text, '\n');
|
||||
while(end) {
|
||||
int line_width = get_line_width(face, text, tm);
|
||||
|
||||
for(char c = *text; text < end; text++, c = *text) {
|
||||
af_glyph_t *glyph = find_glyph(face, c);
|
||||
if(!glyph) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pp_mat3_t caret_transform = *tm->transform;
|
||||
pp_mat3_scale(&caret_transform, scale, scale);
|
||||
pp_mat3_translate(&caret_transform, caret.x, caret.y);
|
||||
|
||||
if(tm->align == AF_H_ALIGN_CENTER) {
|
||||
pp_mat3_translate(&caret_transform, (max_line_width - line_width) / 2, 0);
|
||||
}
|
||||
|
||||
if(tm->align == AF_H_ALIGN_RIGHT) {
|
||||
pp_mat3_translate(&caret_transform, (max_line_width - line_width), 0);
|
||||
}
|
||||
|
||||
pp_transform(&caret_transform);
|
||||
|
||||
af_render_glyph(glyph, tm);
|
||||
|
||||
if(c == L' ') {
|
||||
caret.x += (glyph->advance * tm->word_spacing) / 100.0f;
|
||||
} else {
|
||||
caret.x += (glyph->advance * tm->letter_spacing) / 100.0f;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
text = end + 1;
|
||||
end = strchr(text, '\n');
|
||||
|
||||
caret.x = 0;
|
||||
caret.y += line_height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
pp_transform(old);
|
||||
}
|
||||
|
||||
pp_rect_t af_measure(af_face_t *face, const char *text, af_text_metrics_t *tm) {
|
||||
pp_rect_t result;
|
||||
bool first = true;
|
||||
pp_mat3_t t = *tm->transform;
|
||||
|
||||
for(size_t i = 0; i < strlen(text); i++) {
|
||||
af_glyph_t *glyph = find_glyph(face, text[i]);
|
||||
if(!glyph) {
|
||||
continue;
|
||||
}
|
||||
pp_rect_t r = {glyph->x, glyph->y, glyph->x + glyph->w, glyph->y + glyph->h};
|
||||
r = pp_rect_transform(&r, &t);
|
||||
pp_mat3_translate(&t, glyph->advance, 0);
|
||||
|
||||
if(first) {
|
||||
result = r;
|
||||
first = false;
|
||||
}else{
|
||||
result = pp_rect_merge(&result, &r);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // AF_IMPLEMENTATION
|
||||
|
||||
#endif // AF_INCLUDE_H
|
|
@ -8,13 +8,11 @@
|
|||
|
||||
#include "alright_fonts.hpp"
|
||||
|
||||
using namespace pretty_poly;
|
||||
|
||||
namespace alright_fonts {
|
||||
/*
|
||||
utility functions
|
||||
*/
|
||||
pretty_poly::rect_t measure_character(text_metrics_t &tm, uint16_t codepoint) {
|
||||
pp_rect_t measure_character(text_metrics_t &tm, uint16_t codepoint) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
|
@ -28,59 +26,13 @@ namespace alright_fonts {
|
|||
render functions
|
||||
*/
|
||||
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin) {
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pp_point_t origin, pp_mat3_t *transform) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
// scale is a fixed point 16:16 value, our font data is already scaled to
|
||||
// -128..127 so to get the pixel size we want we can just shift the
|
||||
// users requested size up one bit
|
||||
unsigned scale = tm.size << 9;
|
||||
|
||||
pretty_poly::draw_polygon<int8_t>(glyph.contours, origin, scale);
|
||||
pp_transform(transform);
|
||||
pp_render(&glyph.contours);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename mat_t>
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, mat_t transform) {
|
||||
if(tm.face.glyphs.count(codepoint) == 1) {
|
||||
glyph_t glyph = tm.face.glyphs[codepoint];
|
||||
|
||||
// scale is a fixed point 16:16 value, our font data is already scaled to
|
||||
// -128..127 so to get the pixel size we want we can just shift the
|
||||
// users requested size up one bit
|
||||
unsigned scale = tm.size << 9;
|
||||
|
||||
std::vector<pretty_poly::contour_t<int8_t>> contours;
|
||||
contours.reserve(glyph.contours.size());
|
||||
|
||||
unsigned int total_points = 0;
|
||||
for(auto i = 0u; i < glyph.contours.size(); i++) {
|
||||
total_points += glyph.contours[i].count;;
|
||||
}
|
||||
|
||||
point_t<int8_t> *points = (point_t<int8_t> *)malloc(sizeof(point_t<int8_t>) * total_points);
|
||||
|
||||
for(auto i = 0u; i < glyph.contours.size(); i++) {
|
||||
const unsigned int count = glyph.contours[i].count;
|
||||
for(auto j = 0u; j < count; j++) {
|
||||
point_t<float> point(glyph.contours[i].points[j].x, glyph.contours[i].points[j].y);
|
||||
point *= transform;
|
||||
points[j] = point_t<int8_t>(point.x, point.y);
|
||||
}
|
||||
contours.emplace_back(points, count);
|
||||
points += count;
|
||||
}
|
||||
|
||||
pretty_poly::draw_polygon<int8_t>(contours, origin, scale);
|
||||
|
||||
free(contours[0].points);
|
||||
}
|
||||
}
|
||||
|
||||
template void render_character<pretty_poly::mat3_t>(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, pretty_poly::mat3_t transform);
|
||||
template void render_character<pretty_poly::mat2_t>(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, pretty_poly::mat2_t transform);
|
||||
|
||||
/*
|
||||
load functions
|
||||
*/
|
||||
|
@ -124,6 +76,8 @@ namespace alright_fonts {
|
|||
g.bounds.h = ru8(ifs);
|
||||
g.advance = ru8(ifs);
|
||||
|
||||
g.contours.paths = (pp_path_t *)malloc(sizeof(pp_path_t) * 10);
|
||||
|
||||
if(ifs.fail()) {
|
||||
// could not read glyph dictionary entry
|
||||
return false;
|
||||
|
@ -148,10 +102,11 @@ namespace alright_fonts {
|
|||
|
||||
// allocate space to store point data for contour and read
|
||||
// from file
|
||||
pretty_poly::point_t<int8_t> *points = new pretty_poly::point_t<int8_t>[count];
|
||||
ifs.read((char *)points, count * 2);
|
||||
g.contours.paths[g.contours.count].points = (pp_point_t *)malloc(sizeof(pp_point_t) * count);
|
||||
|
||||
g.contours.push_back({points, count});
|
||||
ifs.read((char *)g.contours.paths[g.contours.count].points, sizeof(pp_point_t) * count);
|
||||
|
||||
g.contours.count ++;
|
||||
}
|
||||
|
||||
// return back to position in dictionary
|
||||
|
|
|
@ -6,15 +6,16 @@
|
|||
#include <optional>
|
||||
#include <map>
|
||||
|
||||
#include "pretty_poly.hpp"
|
||||
#include "pretty-poly.h"
|
||||
#include "file_io.hpp"
|
||||
|
||||
namespace alright_fonts {
|
||||
|
||||
struct glyph_t {
|
||||
uint16_t codepoint;
|
||||
pretty_poly::rect_t bounds;
|
||||
pp_rect_t bounds;
|
||||
uint8_t advance;
|
||||
std::vector<pretty_poly::contour_t<int8_t>> contours;
|
||||
pp_poly_t contours;
|
||||
};
|
||||
|
||||
struct face_t {
|
||||
|
@ -23,10 +24,10 @@ namespace alright_fonts {
|
|||
std::map<uint16_t, glyph_t> glyphs;
|
||||
|
||||
face_t() {};
|
||||
face_t(pretty_poly::file_io &ifs) {load(ifs);}
|
||||
face_t(file_io &ifs) {load(ifs);}
|
||||
face_t(std::string_view path) {load(path);}
|
||||
|
||||
bool load(pretty_poly::file_io &ifs);
|
||||
bool load(file_io &ifs);
|
||||
bool load(std::string_view path);
|
||||
};
|
||||
|
||||
|
@ -48,7 +49,7 @@ namespace alright_fonts {
|
|||
int word_spacing; // spacing between words
|
||||
alignment_t align; // horizontal and vertical alignment
|
||||
//optional<mat3_t> transform; // arbitrary transformation
|
||||
pretty_poly::antialias_t antialiasing = pretty_poly::X4; // level of antialiasing to apply
|
||||
pp_antialias_t antialiasing = PP_AA_X4; // level of antialiasing to apply
|
||||
|
||||
void set_size(int s) {
|
||||
size = s;
|
||||
|
@ -63,13 +64,11 @@ namespace alright_fonts {
|
|||
/*
|
||||
utility functions
|
||||
*/
|
||||
pretty_poly::rect_t measure_character(text_metrics_t &tm, uint16_t codepoint);
|
||||
pp_rect_t measure_character(text_metrics_t &tm, uint16_t codepoint);
|
||||
|
||||
/*
|
||||
render functions
|
||||
*/
|
||||
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin);
|
||||
template<typename mat_t>
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pretty_poly::point_t<int> origin, mat_t transform);
|
||||
void render_character(text_metrics_t &tm, uint16_t codepoint, pp_point_t origin, pp_mat3_t *transform);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
#include <string_view>
|
||||
#include <cstdint>
|
||||
|
||||
class file_io {
|
||||
private:
|
||||
void *state;
|
||||
size_t filesize = 0;
|
||||
|
||||
public:
|
||||
file_io(std::string_view path);
|
||||
~file_io();
|
||||
size_t seek(size_t pos);
|
||||
size_t read(void *buf, size_t len);
|
||||
size_t tell();
|
||||
bool fail();
|
||||
};
|
|
@ -1,9 +1,18 @@
|
|||
if(NOT TARGET pico_graphics)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/../pico_graphics/pico_graphics.cmake)
|
||||
endif()
|
||||
|
||||
add_library(pico_vector
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_vector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/pretty_poly.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/alright_fonts.cpp
|
||||
)
|
||||
|
||||
target_include_directories(pico_vector INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
target_link_libraries(pico_vector pico_stdlib hardware_interp)
|
||||
target_link_libraries(pico_vector pico_graphics pico_stdlib hardware_interp)
|
||||
|
||||
set_source_files_properties(
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_vector.cpp
|
||||
PROPERTIES COMPILE_FLAGS
|
||||
"-Wno-narrowing"
|
||||
)
|
|
@ -1,141 +1,91 @@
|
|||
#define PP_IMPLEMENTATION
|
||||
#define AF_IMPLEMENTATION
|
||||
#include "pico_vector.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace pimoroni {
|
||||
void PicoVector::polygon(std::vector<pretty_poly::contour_t<picovector_point_type>> contours, Point origin, int scale) {
|
||||
pretty_poly::settings::clip = {graphics->clip.x, graphics->clip.y, graphics->clip.w, graphics->clip.h};
|
||||
pretty_poly::draw_polygon<picovector_point_type>(
|
||||
contours,
|
||||
pretty_poly::point_t<int>(origin.x, origin.y),
|
||||
scale);
|
||||
PicoGraphics *PicoVector::graphics = nullptr;
|
||||
|
||||
void PicoVector::draw(pp_poly_t *poly) {
|
||||
pp_transform(NULL);
|
||||
pp_render(poly);
|
||||
}
|
||||
|
||||
void PicoVector::rotate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point origin, float angle) {
|
||||
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)origin.x, (picovector_point_type)origin.y};
|
||||
angle = (2 * (float)M_PI / 360.f) * angle;
|
||||
pretty_poly::mat2_t r = pretty_poly::mat2_t::rotation(angle);
|
||||
for(auto &contour : contours) {
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] -= t;
|
||||
contour.points[i] *= r;
|
||||
contour.points[i] += t;
|
||||
void PicoVector::draw(pp_poly_t *poly, pp_mat3_t *t) {
|
||||
pp_transform(t);
|
||||
pp_render(poly);
|
||||
}
|
||||
|
||||
void PicoVector::rotate(pp_path_t *path, pp_point_t origin, float angle) {
|
||||
pp_mat3_t t = pp_mat3_identity();
|
||||
pp_mat3_translate(&t, origin.x, origin.y);
|
||||
pp_mat3_rotate(&t, angle);
|
||||
pp_mat3_translate(&t, -origin.x, -origin.y);
|
||||
transform(path, &t);
|
||||
}
|
||||
|
||||
void PicoVector::translate(pp_path_t *path, pp_point_t translation) {
|
||||
pp_mat3_t t = pp_mat3_identity();
|
||||
pp_mat3_translate(&t, translation.x, translation.y);
|
||||
transform(path, &t);
|
||||
}
|
||||
|
||||
void PicoVector::transform(pp_path_t *path, pp_mat3_t *t) {
|
||||
for (auto j = 0u; j < path->count; j++) {
|
||||
path->points[j] = pp_point_transform(&path->points[j], t);
|
||||
}
|
||||
}
|
||||
|
||||
void PicoVector::translate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point translation) {
|
||||
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)translation.x, (picovector_point_type)translation.y};
|
||||
for(auto &contour : contours) {
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] += t;
|
||||
void PicoVector::rotate(pp_poly_t *poly, pp_point_t origin, float angle) {
|
||||
pp_mat3_t t = pp_mat3_identity();
|
||||
pp_mat3_translate(&t, origin.x, origin.y);
|
||||
pp_mat3_rotate(&t, angle);
|
||||
pp_mat3_translate(&t, -origin.x, -origin.y);
|
||||
transform(poly, &t);
|
||||
}
|
||||
|
||||
void PicoVector::translate(pp_poly_t *poly, pp_point_t translation) {
|
||||
pp_mat3_t t = pp_mat3_identity();
|
||||
pp_mat3_translate(&t, translation.x, translation.y);
|
||||
transform(poly, &t);
|
||||
}
|
||||
|
||||
void PicoVector::transform(pp_poly_t *poly, pp_mat3_t *t) {
|
||||
for (auto i = 0u; i < poly->count; i++) {
|
||||
transform(&poly->paths[i], t);
|
||||
}
|
||||
}
|
||||
|
||||
void PicoVector::rotate(pretty_poly::contour_t<picovector_point_type> &contour, Point origin, float angle) {
|
||||
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)origin.x, (picovector_point_type)origin.y};
|
||||
angle = (2 * (float)M_PI / 360.f) * angle;
|
||||
pretty_poly::mat2_t r = pretty_poly::mat2_t::rotation(angle);
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] -= t;
|
||||
contour.points[i] *= r;
|
||||
contour.points[i] += t;
|
||||
}
|
||||
}
|
||||
pp_point_t PicoVector::text(std::string_view text, pp_mat3_t *t) {
|
||||
pp_point_t caret = {0, 0};
|
||||
|
||||
void PicoVector::translate(pretty_poly::contour_t<picovector_point_type> &contour, Point translation) {
|
||||
pretty_poly::point_t<picovector_point_type> t{(picovector_point_type)translation.x, (picovector_point_type)translation.y};
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
contour.points[i] += t;
|
||||
}
|
||||
}
|
||||
text_metrics.transform = t;
|
||||
|
||||
Point PicoVector::text(std::string_view text, Point origin) {
|
||||
// Copy clipping bounds from the PicoGraphics instance
|
||||
pretty_poly::settings::clip = {graphics->clip.x, graphics->clip.y, graphics->clip.w, graphics->clip.h};
|
||||
// TODO: Normalize types somehow, so we're not converting?
|
||||
pretty_poly::point_t<int> caret = pretty_poly::point_t<int>(origin.x, origin.y);
|
||||
af_render(text_metrics.face, text.data(), &text_metrics);
|
||||
|
||||
return caret;
|
||||
/*
|
||||
// Align text from the bottom left
|
||||
caret.y += text_metrics.size;
|
||||
caret.y += (PP_COORD_TYPE)text_metrics.line_height;
|
||||
|
||||
int16_t space_width = alright_fonts::measure_character(text_metrics, ' ').w;
|
||||
if (space_width == 0) {
|
||||
space_width = text_metrics.word_spacing;
|
||||
}
|
||||
caret = pp_point_transform(&caret, t);
|
||||
caret.x += offset.x;
|
||||
caret.y += offset.y;
|
||||
|
||||
size_t i = 0;
|
||||
pp_point_t space;
|
||||
pp_point_t carriage_return = {0, -(PP_COORD_TYPE)text_metrics.line_height};
|
||||
|
||||
while(i < text.length()) {
|
||||
size_t next_space = text.find(' ', i + 1);
|
||||
char spc = ' ';
|
||||
|
||||
if(next_space == std::string::npos) {
|
||||
next_space = text.length();
|
||||
}
|
||||
|
||||
size_t next_linebreak = text.find('\n', i + 1);
|
||||
|
||||
if(next_linebreak == std::string::npos) {
|
||||
next_linebreak = text.length();
|
||||
}
|
||||
|
||||
size_t next_break = std::min(next_space, next_linebreak);
|
||||
|
||||
uint16_t word_width = 0;
|
||||
for(size_t j = i; j < next_break; j++) {
|
||||
word_width += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
word_width += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
if(caret.x != 0 && caret.x + word_width > graphics->clip.w) {
|
||||
caret.x = origin.x;
|
||||
caret.y += text_metrics.line_height;
|
||||
}
|
||||
|
||||
for(size_t j = i; j < std::min(next_break + 1, text.length()); j++) {
|
||||
if (text[j] == '\n') { // Linebreak
|
||||
caret.x = origin.x;
|
||||
caret.y += text_metrics.line_height;
|
||||
} else if (text[j] == ' ') { // Space
|
||||
caret.x += space_width;
|
||||
} else {
|
||||
alright_fonts::render_character(text_metrics, text[j], caret);
|
||||
}
|
||||
caret.x += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
caret.x += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
i = next_break + 1;
|
||||
}
|
||||
|
||||
return Point(caret.x, caret.y);
|
||||
}
|
||||
|
||||
Point PicoVector::text(std::string_view text, Point origin, float angle) {
|
||||
// Copy clipping bounds from the PicoGraphics instance
|
||||
pretty_poly::settings::clip = {graphics->clip.x, graphics->clip.y, graphics->clip.w, graphics->clip.h};
|
||||
// TODO: Normalize types somehow, so we're not converting?
|
||||
pretty_poly::point_t<float> caret(0, 0);
|
||||
|
||||
// Prepare a transformation matrix for character and offset rotation
|
||||
angle = (2 * (float)M_PI / 360.f) * angle;
|
||||
pretty_poly::mat2_t transform = pretty_poly::mat2_t::rotation(angle);
|
||||
|
||||
// Align text from the bottom left
|
||||
caret.y += text_metrics.line_height;
|
||||
caret *= transform;
|
||||
|
||||
pretty_poly::point_t<float> space;
|
||||
pretty_poly::point_t<float> carriage_return(0, -text_metrics.line_height);
|
||||
|
||||
space.x = alright_fonts::measure_character(text_metrics, ' ').w;
|
||||
space.x = af_measure(text_metrics.face, &spc, &text_metrics).w;
|
||||
if (space.x == 0) {
|
||||
space.x = text_metrics.word_spacing;
|
||||
}
|
||||
|
||||
space *= transform;
|
||||
carriage_return *= transform;
|
||||
const pretty_poly::point_t<float> initial_carriage_return = carriage_return;
|
||||
space = pp_point_transform(&space, t);
|
||||
carriage_return = pp_point_transform(&carriage_return, t);
|
||||
|
||||
pp_point_t initial_carriage_return = carriage_return;
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
|
@ -156,37 +106,43 @@ namespace pimoroni {
|
|||
|
||||
uint16_t word_width = 0;
|
||||
for(size_t j = i; j < next_break; j++) {
|
||||
word_width += alright_fonts::measure_character(text_metrics, text[j]).w;
|
||||
word_width += af_measure(text_metrics.face, &text[j], &text_metrics).w;
|
||||
word_width += text_metrics.letter_spacing;
|
||||
}
|
||||
|
||||
if(caret.x != 0 && caret.x + word_width > graphics->clip.w) {
|
||||
caret -= carriage_return;
|
||||
caret = pp_point_sub(&caret, &carriage_return);
|
||||
carriage_return = initial_carriage_return;
|
||||
}
|
||||
|
||||
for(size_t j = i; j < std::min(next_break + 1, text.length()); j++) {
|
||||
if (text[j] == '\n') { // Linebreak
|
||||
caret -= carriage_return;
|
||||
caret = pp_point_sub(&caret, &carriage_return);
|
||||
carriage_return = initial_carriage_return;
|
||||
} else if (text[j] == ' ') { // Space
|
||||
caret += space;
|
||||
carriage_return += space;
|
||||
caret = pp_point_add(&caret, &space);
|
||||
carriage_return = pp_point_add(&carriage_return, &space);
|
||||
} else {
|
||||
alright_fonts::render_character(text_metrics, text[j], pretty_poly::point_t<int>(origin.x + caret.x, origin.y + caret.y), transform);
|
||||
// apply the caret offset...
|
||||
pp_mat3_t pos = pp_mat3_identity();
|
||||
pp_mat3_mul(&pos, t);
|
||||
pp_mat3_translate(&pos, caret.x, caret.y);
|
||||
text_metrics.transform = &pos;
|
||||
af_render_character(text_metrics.face, text[j], &text_metrics);
|
||||
}
|
||||
pretty_poly::point_t<float> advance(
|
||||
alright_fonts::measure_character(text_metrics, text[j]).w + text_metrics.letter_spacing,
|
||||
0
|
||||
);
|
||||
advance *= transform;
|
||||
caret += advance;
|
||||
carriage_return += advance;
|
||||
pp_point_t advance = {
|
||||
(PP_COORD_TYPE)af_measure(text_metrics.face, &text[j], &text_metrics).w + text_metrics.letter_spacing,
|
||||
(PP_COORD_TYPE)0
|
||||
};
|
||||
advance = pp_point_transform(&advance, t);
|
||||
caret = pp_point_add(&caret, &advance);
|
||||
carriage_return = pp_point_add(&carriage_return, &advance);
|
||||
}
|
||||
|
||||
i = next_break + 1;
|
||||
}
|
||||
|
||||
return Point(caret.x, caret.y);
|
||||
return {caret.x, caret.y};
|
||||
*/
|
||||
}
|
||||
}
|
|
@ -1,7 +1,27 @@
|
|||
#include "pretty_poly.hpp"
|
||||
#include "alright_fonts.hpp"
|
||||
|
||||
#include "af-file-io.h"
|
||||
#include "af-memory.h"
|
||||
|
||||
#define AF_FILE void*
|
||||
#define AF_FREAD(p, size, nmemb, stream) fileio_read(stream, p, nmemb)
|
||||
#define AF_FGETC(stream) fileio_getc(stream)
|
||||
|
||||
#define AF_MALLOC(size) af_malloc(size)
|
||||
#define AF_REALLOC(p, size) af_realloc(p, size)
|
||||
#define AF_FREE(p) af_free(p)
|
||||
|
||||
#define PP_MALLOC(size) af_malloc(size)
|
||||
#define PP_REALLOC(p, size) af_realloc(p, size)
|
||||
#define PP_FREE(p) af_free(p)
|
||||
|
||||
#define AF_DEBUG(...) af_debug(__VA_ARGS__)
|
||||
|
||||
#include "pretty-poly.h"
|
||||
#include "alright-fonts.h"
|
||||
#include "pico_graphics.hpp"
|
||||
|
||||
pp_rect_t pp_contour_bounds(const pp_path_t *c);
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
// Integer point types cause compound error in transformations
|
||||
|
@ -9,79 +29,114 @@ namespace pimoroni {
|
|||
|
||||
class PicoVector {
|
||||
private:
|
||||
PicoGraphics *graphics;
|
||||
alright_fonts::text_metrics_t text_metrics;
|
||||
const uint8_t alpha_map[4] {0, 128, 192, 255};
|
||||
static PicoGraphics *graphics;
|
||||
af_text_metrics_t text_metrics;
|
||||
static constexpr uint8_t alpha_map[4] {0, 128, 192, 255};
|
||||
|
||||
public:
|
||||
PicoVector(PicoGraphics *graphics, void *mem = nullptr) : graphics(graphics) {
|
||||
pretty_poly::init(mem);
|
||||
PicoVector(PicoGraphics *graphics, void *mem = nullptr) {
|
||||
PicoVector::graphics = graphics;
|
||||
|
||||
set_options([this](const pretty_poly::tile_t &tile) -> void {
|
||||
uint8_t *tile_data = tile.data;
|
||||
pp_tile_callback(PicoVector::tile_callback);
|
||||
|
||||
if(this->graphics->supports_alpha_blend() && pretty_poly::settings::antialias != pretty_poly::NONE) {
|
||||
if (this->graphics->render_pico_vector_tile({tile.bounds.x, tile.bounds.y, tile.bounds.w, tile.bounds.h},
|
||||
tile.data,
|
||||
tile.stride,
|
||||
(uint8_t)pretty_poly::settings::antialias)) {
|
||||
pp_antialias(graphics->supports_alpha_blend() ? PP_AA_X4 : PP_AA_NONE);
|
||||
|
||||
pp_clip(graphics->clip.x, graphics->clip.y, graphics->clip.w, graphics->clip.h);
|
||||
|
||||
text_metrics.align = AF_H_ALIGN_LEFT;
|
||||
text_metrics.line_height = 110;
|
||||
text_metrics.letter_spacing = 95;
|
||||
text_metrics.word_spacing = 200;
|
||||
text_metrics.size = 48;
|
||||
// Shoud be set before rendering chars
|
||||
//text_metrics.transform = (pp_mat3_t *)af_malloc(sizeof(pp_mat3_t));
|
||||
//*text_metrics.transform = pp_mat3_identity();
|
||||
}
|
||||
|
||||
static void tile_callback(const pp_tile_t *tile) {
|
||||
uint8_t *tile_data = tile->data;
|
||||
|
||||
if(PicoVector::graphics->supports_alpha_blend() && _pp_antialias != PP_AA_NONE) {
|
||||
if (PicoVector::graphics->render_pico_vector_tile({tile->x, tile->y, tile->w, tile->h},
|
||||
tile->data,
|
||||
tile->stride,
|
||||
(uint8_t)_pp_antialias)) {
|
||||
return;
|
||||
}
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
for(auto y = 0; y < tile->h; y++) {
|
||||
for(auto x = 0; x < tile->w; x++) {
|
||||
uint8_t alpha = *tile_data++;
|
||||
if (alpha >= 4) {
|
||||
this->graphics->set_pixel({x + tile.bounds.x, y + tile.bounds.y});
|
||||
PicoVector::graphics->set_pixel({x + tile->x, y + tile->y});
|
||||
} else if (alpha > 0) {
|
||||
alpha = alpha_map[alpha];
|
||||
this->graphics->set_pixel_alpha({x + tile.bounds.x, y + tile.bounds.y}, alpha);
|
||||
PicoVector::graphics->set_pixel_alpha({x + tile->x, y + tile->y}, alpha);
|
||||
}
|
||||
}
|
||||
tile_data += tile.stride - tile.bounds.w;
|
||||
tile_data += tile->stride - tile->w;
|
||||
}
|
||||
} else {
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
for(auto y = 0; y < tile->h; y++) {
|
||||
for(auto x = 0; x < tile->w; x++) {
|
||||
uint8_t alpha = *tile_data++;
|
||||
if (alpha) {
|
||||
this->graphics->set_pixel({x + tile.bounds.x, y + tile.bounds.y});
|
||||
PicoVector::graphics->set_pixel({x + tile->x, y + tile->y});
|
||||
}
|
||||
}
|
||||
tile_data += tile.stride - tile.bounds.w;
|
||||
tile_data += tile->stride - tile->w;
|
||||
}
|
||||
}
|
||||
}, graphics->supports_alpha_blend() ? pretty_poly::X4 : pretty_poly::NONE, {graphics->clip.x, graphics->clip.y, graphics->clip.w, graphics->clip.h});
|
||||
}
|
||||
|
||||
void set_antialiasing(pretty_poly::antialias_t antialias) {
|
||||
set_options(pretty_poly::settings::callback, antialias, pretty_poly::settings::clip);
|
||||
void set_antialiasing(pp_antialias_t antialias) {
|
||||
pp_antialias(antialias);
|
||||
}
|
||||
|
||||
void set_font_size(unsigned int font_size) {
|
||||
text_metrics.set_size(font_size);
|
||||
text_metrics.size = font_size;
|
||||
}
|
||||
|
||||
bool set_font(std::string_view font_path, unsigned int font_size) {
|
||||
bool result = text_metrics.face.load(font_path);
|
||||
if(text_metrics.face) {
|
||||
af_free(text_metrics.face->glyphs);
|
||||
af_free(text_metrics.face);
|
||||
}
|
||||
text_metrics.face = (af_face_t *)af_malloc(sizeof(af_face_t));
|
||||
//bool result = text_metrics.face.load(font_path);
|
||||
void* font = fileio_open(font_path.data());
|
||||
bool result = af_load_font_file(font, text_metrics.face);
|
||||
|
||||
set_font_size(font_size);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void rotate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point origin, float angle);
|
||||
void translate(std::vector<pretty_poly::contour_t<picovector_point_type>> &contours, Point translation);
|
||||
pp_point_t text(std::string_view text, pp_mat3_t *t);
|
||||
|
||||
void rotate(pretty_poly::contour_t<picovector_point_type> &contour, Point origin, float angle);
|
||||
void translate(pretty_poly::contour_t<picovector_point_type> &contour, Point translation);
|
||||
void transform(pp_path_t *path, pp_mat3_t *t);
|
||||
void transform(pp_poly_t *poly, pp_mat3_t *t);
|
||||
|
||||
Point text(std::string_view text, Point origin);
|
||||
Point text(std::string_view text, Point origin, float angle);
|
||||
void rotate(pp_path_t *path, pp_point_t origin, float angle);
|
||||
void rotate(pp_poly_t *poly, pp_point_t origin, float angle);
|
||||
|
||||
void polygon(std::vector<pretty_poly::contour_t<picovector_point_type>> contours, Point origin = Point(0, 0), int scale=65536);
|
||||
void translate(pp_path_t *path, pp_point_t translation);
|
||||
void translate(pp_poly_t *poly, pp_point_t translation);
|
||||
|
||||
void draw(pp_poly_t *poly);
|
||||
void draw(pp_poly_t *poly, pp_mat3_t *t);
|
||||
|
||||
void draw(pp_path_t *path) {
|
||||
pp_poly_t poly = {.paths = path, .count = 1};
|
||||
draw(&poly);
|
||||
};
|
||||
|
||||
void draw(pp_path_t *path, pp_mat3_t *t) {
|
||||
pp_poly_t poly = {.paths = path, .count = 1};
|
||||
draw(&poly, t);
|
||||
};
|
||||
|
||||
static constexpr size_t pretty_poly_buffer_size() {
|
||||
return pretty_poly::buffer_size();
|
||||
return 0;
|
||||
};
|
||||
};
|
||||
}
|
|
@ -0,0 +1,619 @@
|
|||
/*
|
||||
|
||||
Pretty Poly 🦜 - super-sampling polygon renderer for low resource platforms.
|
||||
|
||||
Jonathan Williamson, August 2022
|
||||
Examples, source, and more: https://github.com/lowfatcode/pretty-poly
|
||||
MIT License https://github.com/lowfatcode/pretty-poly/blob/main/LICENSE
|
||||
|
||||
An easy way to render high quality graphics in embedded applications running
|
||||
on resource constrained microcontrollers such as the Cortex M0 and up.
|
||||
|
||||
- Renders polygons: concave, self-intersecting, multi contour, holes, etc.
|
||||
- C11 header only library: simply copy the header file into your project
|
||||
- Tile based renderer: low memory footprint, cache coherency
|
||||
- Low memory usage: ~4kB of heap memory required
|
||||
- High speed on low resource platforms: optionally no floating point
|
||||
- Antialiasing modes: X1 (none), X4 and X16 super sampling
|
||||
- Bounds clipping: all results clipped to supplied clip rectangle
|
||||
- Pixel format agnostic: renders a "tile" to blend into your framebuffer
|
||||
- Support for hardware interpolators on rp2040 (thanks @MichaelBell!)
|
||||
|
||||
Contributor bwaaaaaarks! 🦜
|
||||
|
||||
@MichaelBell - lots of bug fixes, performance boosts, and suggestions.
|
||||
@gadgetoid - integrating into the PicoVector library and testing.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef PP_INCLUDE_H
|
||||
#define PP_INCLUDE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef PP_MALLOC
|
||||
#define PP_MALLOC(size) malloc(size)
|
||||
#define PP_REALLOC(p, size) realloc(p, size)
|
||||
#define PP_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
#ifndef PP_COORD_TYPE
|
||||
#define PP_COORD_TYPE float
|
||||
#endif
|
||||
|
||||
#ifndef PP_NODE_BUFFER_HEIGHT
|
||||
#define PP_NODE_BUFFER_HEIGHT 16
|
||||
#endif
|
||||
|
||||
#ifndef PP_MAX_NODES_PER_SCANLINE
|
||||
#define PP_MAX_NODES_PER_SCANLINE 16
|
||||
#endif
|
||||
|
||||
#ifndef PP_TILE_BUFFER_SIZE
|
||||
#define PP_TILE_BUFFER_SIZE 4096
|
||||
#endif
|
||||
|
||||
#if defined(PICO_ON_DEVICE) && PICO_ON_DEVICE
|
||||
#define USE_RP2040_INTERP
|
||||
#include "hardware/interp.h"
|
||||
#endif
|
||||
|
||||
#ifdef PP_DEBUG
|
||||
#define debug(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// 3x3 matrix type allows for optional transformation of polygon during render
|
||||
typedef struct {
|
||||
float v00, v10, v20, v01, v11, v21, v02, v12, v22;
|
||||
} pp_mat3_t;
|
||||
pp_mat3_t pp_mat3_identity();
|
||||
void pp_mat3_rotate(pp_mat3_t *m, float a);
|
||||
void pp_mat3_rotate_rad(pp_mat3_t *m, float a);
|
||||
void pp_mat3_translate(pp_mat3_t *m, float x, float y);
|
||||
void pp_mat3_scale(pp_mat3_t *m, float x, float y);
|
||||
void pp_mat3_mul(pp_mat3_t *m1, pp_mat3_t *m2);
|
||||
|
||||
// point type used to hold polygon vertex coordinates
|
||||
typedef struct __attribute__((__packed__)) pp_point_t {
|
||||
PP_COORD_TYPE x, y;
|
||||
} pp_point_t;
|
||||
pp_point_t pp_point_add(pp_point_t *p1, pp_point_t *p2);
|
||||
pp_point_t pp_point_sub(pp_point_t *p1, pp_point_t *p2);
|
||||
pp_point_t pp_point_mul(pp_point_t *p1, pp_point_t *p2);
|
||||
pp_point_t pp_point_div(pp_point_t *p1, pp_point_t *p2);
|
||||
pp_point_t pp_point_transform(pp_point_t *p, pp_mat3_t *m);
|
||||
|
||||
// rect type
|
||||
typedef struct {
|
||||
int32_t x, y, w, h;
|
||||
} pp_rect_t;
|
||||
bool pp_rect_empty(pp_rect_t *r);
|
||||
pp_rect_t pp_rect_intersection(pp_rect_t *r1, pp_rect_t *r2);
|
||||
pp_rect_t pp_rect_merge(pp_rect_t *r1, pp_rect_t *r2);
|
||||
pp_rect_t pp_rect_transform(pp_rect_t *r, pp_mat3_t *m);
|
||||
|
||||
// antialias levels
|
||||
typedef enum {PP_AA_NONE = 0, PP_AA_X4 = 1, PP_AA_X16 = 2} pp_antialias_t;
|
||||
|
||||
typedef struct {
|
||||
int32_t x, y, w, h;
|
||||
uint32_t stride;
|
||||
uint8_t *data;
|
||||
} pp_tile_t;
|
||||
|
||||
typedef struct {
|
||||
pp_point_t *points;
|
||||
uint32_t count;
|
||||
} pp_path_t;
|
||||
|
||||
typedef struct {
|
||||
pp_path_t *paths;
|
||||
uint32_t count;
|
||||
} pp_poly_t;
|
||||
|
||||
// user settings
|
||||
typedef void (*pp_tile_callback_t)(const pp_tile_t *tile);
|
||||
|
||||
extern pp_rect_t _pp_clip;
|
||||
extern pp_tile_callback_t _pp_tile_callback;
|
||||
extern pp_antialias_t _pp_antialias;
|
||||
extern pp_mat3_t *_pp_transform;
|
||||
|
||||
void pp_clip(int32_t x, int32_t y, int32_t w, int32_t h);
|
||||
void pp_tile_callback(pp_tile_callback_t callback);
|
||||
void pp_antialias(pp_antialias_t antialias);
|
||||
pp_mat3_t *pp_transform(pp_mat3_t *transform);
|
||||
void pp_render(pp_poly_t *polygon);
|
||||
|
||||
pp_rect_t pp_contour_bounds(const pp_path_t *c);
|
||||
pp_rect_t pp_polygon_bounds(pp_poly_t *p);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef PP_IMPLEMENTATION
|
||||
|
||||
pp_rect_t _pp_clip = (pp_rect_t){0, 0, 320, 240};
|
||||
pp_tile_callback_t _pp_tile_callback = NULL;
|
||||
pp_antialias_t _pp_antialias = PP_AA_X4;
|
||||
pp_mat3_t *_pp_transform = NULL;
|
||||
|
||||
int _pp_max(int a, int b) { return a > b ? a : b; }
|
||||
int _pp_min(int a, int b) { return a < b ? a : b; }
|
||||
int _pp_sign(int v) {return (v > 0) - (v < 0);}
|
||||
void _pp_swap(int *a, int *b) {int t = *a; *a = *b; *b = t;}
|
||||
|
||||
// pp_mat3_t implementation
|
||||
pp_mat3_t pp_mat3_identity() {
|
||||
pp_mat3_t m; memset(&m, 0, sizeof(pp_mat3_t)); m.v00 = m.v11 = m.v22 = 1.0f; return m;}
|
||||
void pp_mat3_rotate(pp_mat3_t *m, float a) {
|
||||
pp_mat3_rotate_rad(m, a * M_PI / 180.0f);}
|
||||
void pp_mat3_rotate_rad(pp_mat3_t *m, float a) {
|
||||
float c = cosf(a), s = sinf(a); pp_mat3_t r = pp_mat3_identity();
|
||||
r.v00 = c; r.v01 = -s; r.v10 = s; r.v11 = c; pp_mat3_mul(m, &r); }
|
||||
void pp_mat3_translate(pp_mat3_t *m, float x, float y) {
|
||||
pp_mat3_t r = pp_mat3_identity(); r.v02 = x; r.v12 = y; pp_mat3_mul(m, &r);}
|
||||
void pp_mat3_scale(pp_mat3_t *m, float x, float y) {
|
||||
pp_mat3_t r = pp_mat3_identity(); r.v00 = x; r.v11 = y; pp_mat3_mul(m, &r);}
|
||||
void pp_mat3_mul(pp_mat3_t *m1, pp_mat3_t *m2) {
|
||||
pp_mat3_t r;
|
||||
r.v00 = m1->v00 * m2->v00 + m1->v01 * m2->v10 + m1->v02 * m2->v20;
|
||||
r.v01 = m1->v00 * m2->v01 + m1->v01 * m2->v11 + m1->v02 * m2->v21;
|
||||
r.v02 = m1->v00 * m2->v02 + m1->v01 * m2->v12 + m1->v02 * m2->v22;
|
||||
r.v10 = m1->v10 * m2->v00 + m1->v11 * m2->v10 + m1->v12 * m2->v20;
|
||||
r.v11 = m1->v10 * m2->v01 + m1->v11 * m2->v11 + m1->v12 * m2->v21;
|
||||
r.v12 = m1->v10 * m2->v02 + m1->v11 * m2->v12 + m1->v12 * m2->v22;
|
||||
r.v20 = m1->v20 * m2->v00 + m1->v21 * m2->v10 + m1->v22 * m2->v20;
|
||||
r.v21 = m1->v20 * m2->v01 + m1->v21 * m2->v11 + m1->v22 * m2->v21;
|
||||
r.v22 = m1->v20 * m2->v02 + m1->v21 * m2->v12 + m1->v22 * m2->v22;
|
||||
*m1 = r;
|
||||
}
|
||||
|
||||
// pp_point_t implementation
|
||||
pp_point_t pp_point_add(pp_point_t *p1, pp_point_t *p2) {
|
||||
return (pp_point_t){.x = p1->x + p2->x, .y = p1->y + p2->y};
|
||||
}
|
||||
pp_point_t pp_point_sub(pp_point_t *p1, pp_point_t *p2) {
|
||||
return (pp_point_t){.x = p1->x - p2->x, .y = p1->y - p2->y};
|
||||
}
|
||||
pp_point_t pp_point_mul(pp_point_t *p1, pp_point_t *p2) {
|
||||
return (pp_point_t){.x = p1->x * p2->x, .y = p1->y * p2->y};
|
||||
}
|
||||
pp_point_t pp_point_div(pp_point_t *p1, pp_point_t *p2) {
|
||||
return (pp_point_t){.x = p1->x / p2->x, .y = p1->y / p2->y};
|
||||
}
|
||||
pp_point_t pp_point_transform(pp_point_t *p, pp_mat3_t *m) {
|
||||
return (pp_point_t){
|
||||
.x = (m->v00 * p->x + m->v01 * p->y + m->v02),
|
||||
.y = (m->v10 * p->x + m->v11 * p->y + m->v12)
|
||||
};
|
||||
}
|
||||
|
||||
// pp_rect_t implementation
|
||||
bool pp_rect_empty(pp_rect_t *r) {
|
||||
return r->w == 0 || r->h == 0;
|
||||
}
|
||||
pp_rect_t pp_rect_intersection(pp_rect_t *r1, pp_rect_t *r2) {
|
||||
return (pp_rect_t){
|
||||
.x = _pp_max(r1->x, r2->x), .y = _pp_max(r1->y, r2->y),
|
||||
.w = _pp_max(0, _pp_min(r1->x + r1->w, r2->x + r2->w) - _pp_max(r1->x, r2->x)),
|
||||
.h = _pp_max(0, _pp_min(r1->y + r1->h, r2->y + r2->h) - _pp_max(r1->y, r2->y))
|
||||
};
|
||||
}
|
||||
pp_rect_t pp_rect_merge(pp_rect_t *r1, pp_rect_t *r2) {
|
||||
return (pp_rect_t){
|
||||
.x = _pp_min(r1->x, r2->x),
|
||||
.y = _pp_min(r1->y, r2->y),
|
||||
.w = _pp_max(r1->x + r1->w, r2->x + r2->w) - _pp_min(r1->x, r2->x),
|
||||
.h = _pp_max(r1->y + r1->h, r2->y + r2->h) - _pp_min(r1->y, r2->y)
|
||||
};
|
||||
}
|
||||
pp_rect_t pp_rect_transform(pp_rect_t *r, pp_mat3_t *m) {
|
||||
pp_point_t tl = {.x = (PP_COORD_TYPE)r->x, .y = (PP_COORD_TYPE)r->y};
|
||||
pp_point_t tr = {.x = (PP_COORD_TYPE)r->x + (PP_COORD_TYPE)r->w, .y = (PP_COORD_TYPE)r->y};
|
||||
pp_point_t bl = {.x = (PP_COORD_TYPE)r->x, .y = (PP_COORD_TYPE)r->y + (PP_COORD_TYPE)r->h};
|
||||
pp_point_t br = {.x = (PP_COORD_TYPE)r->x + (PP_COORD_TYPE)r->w, .y = (PP_COORD_TYPE)r->y + (PP_COORD_TYPE)r->h};
|
||||
|
||||
tl = pp_point_transform(&tl, m);
|
||||
tr = pp_point_transform(&tr, m);
|
||||
bl = pp_point_transform(&bl, m);
|
||||
br = pp_point_transform(&br, m);
|
||||
|
||||
PP_COORD_TYPE minx = _pp_min(tl.x, _pp_min(tr.x, _pp_min(bl.x, br.x)));
|
||||
PP_COORD_TYPE miny = _pp_min(tl.y, _pp_min(tr.y, _pp_min(bl.y, br.y)));
|
||||
PP_COORD_TYPE maxx = _pp_max(tl.x, _pp_max(tr.x, _pp_max(bl.x, br.x)));
|
||||
PP_COORD_TYPE maxy = _pp_max(tl.y, _pp_max(tr.y, _pp_max(bl.y, br.y)));
|
||||
|
||||
return (pp_rect_t){
|
||||
.x = (int32_t)minx,
|
||||
.y = (int32_t)miny,
|
||||
.w = (int32_t)(maxx - minx),
|
||||
.h = (int32_t)(maxy - miny)
|
||||
};
|
||||
}
|
||||
|
||||
// pp_tile_t implementation
|
||||
uint8_t pp_tile_get(const pp_tile_t *tile, const int32_t x, const int32_t y) {
|
||||
return tile->data[(x - tile->x) + (y - tile->y) * tile->stride] * (255 >> _pp_antialias >> _pp_antialias);
|
||||
}
|
||||
|
||||
// pp_contour_t implementation
|
||||
pp_rect_t pp_contour_bounds(const pp_path_t *c) {
|
||||
int minx = c->points[0].x, maxx = minx;
|
||||
int miny = c->points[0].y, maxy = miny;
|
||||
for(uint32_t i = 1; i < c->count; i++) {
|
||||
minx = _pp_min(minx, c->points[i].x);
|
||||
miny = _pp_min(miny, c->points[i].y);
|
||||
maxx = _pp_max(maxx, c->points[i].x);
|
||||
maxy = _pp_max(maxy, c->points[i].y);
|
||||
}
|
||||
return (pp_rect_t){.x = minx, .y = miny, .w = maxx - minx, .h = maxy - miny};
|
||||
}
|
||||
|
||||
pp_rect_t pp_polygon_bounds(pp_poly_t *p) {
|
||||
if(p->count == 0) {return (pp_rect_t){};}
|
||||
pp_rect_t b = pp_contour_bounds(&p->paths[0]);
|
||||
for(uint32_t i = 1; i < p->count; i++) {
|
||||
pp_rect_t cb = pp_contour_bounds(&p->paths[i]);
|
||||
b = pp_rect_merge(&b, &cb);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
// buffer that each tile is rendered into before callback
|
||||
// allocate one extra byte to allow a small optimization in the row renderer
|
||||
const uint32_t tile_buffer_size = PP_TILE_BUFFER_SIZE;
|
||||
uint8_t tile_buffer[PP_TILE_BUFFER_SIZE + 1];
|
||||
|
||||
// polygon node buffer handles at most 16 line intersections per scanline
|
||||
// is this enough for cjk/emoji? (requires a 2kB buffer)
|
||||
int32_t nodes[PP_NODE_BUFFER_HEIGHT][PP_MAX_NODES_PER_SCANLINE * 2];
|
||||
uint32_t node_counts[PP_NODE_BUFFER_HEIGHT];
|
||||
|
||||
|
||||
void pp_clip(int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
_pp_clip = (pp_rect_t){.x = x, .y = y, .w = w, .h = h};
|
||||
}
|
||||
|
||||
void pp_tile_callback(pp_tile_callback_t callback) {
|
||||
_pp_tile_callback = callback;
|
||||
}
|
||||
|
||||
// maximum tile bounds determined by antialias level
|
||||
uint32_t _pp_tile_width, _pp_tile_height;
|
||||
void pp_antialias(pp_antialias_t antialias) {
|
||||
_pp_antialias = antialias;
|
||||
// recalculate the tile size for rendering based on antialiasing level
|
||||
_pp_tile_height = PP_NODE_BUFFER_HEIGHT >> _pp_antialias;
|
||||
_pp_tile_width = (int)(tile_buffer_size / _pp_tile_height);
|
||||
}
|
||||
|
||||
pp_mat3_t *pp_transform(pp_mat3_t *transform) {
|
||||
pp_mat3_t *old = _pp_transform;
|
||||
_pp_transform = transform;
|
||||
return old;
|
||||
}
|
||||
|
||||
// write out the tile bits
|
||||
void debug_tile(const pp_tile_t *tile) {
|
||||
debug(" - tile %d, %d (%d x %d)\n", tile->x, tile->y, tile->w, tile->h);
|
||||
for(int32_t y = 0; y < tile->h; y++) {
|
||||
debug("[%3d]: ", y);
|
||||
for(int32_t x = 0; x < tile->w; x++) {
|
||||
debug("%02x", pp_tile_get(tile, x, y));
|
||||
}
|
||||
debug("\n");
|
||||
}
|
||||
debug("-----------------------\n");
|
||||
}
|
||||
|
||||
void add_line_segment_to_nodes(const pp_point_t start, const pp_point_t end) {
|
||||
int32_t sx = start.x, sy = start.y, ex = end.x, ey = end.y;
|
||||
|
||||
if(ey < sy) {
|
||||
// swap endpoints if line "pointing up", we do this because we
|
||||
// alway skip the last scanline (so that polygons can but cleanly
|
||||
// up against each other without overlap)
|
||||
int32_t ty = sy; sy = ey; ey = ty;
|
||||
int32_t tx = sx; sx = ex; ex = tx;
|
||||
}
|
||||
|
||||
// Early out if line is completely outside the tile, or has no lines
|
||||
if (ey < 0 || sy >= (int)PP_NODE_BUFFER_HEIGHT || sy == ey) return;
|
||||
|
||||
debug(" + line segment from %d, %d to %d, %d\n", sx, sy, ex, ey);
|
||||
|
||||
// Determine how many in-bounds lines to render
|
||||
int y = _pp_max(0, sy);
|
||||
int count = _pp_min((int)PP_NODE_BUFFER_HEIGHT, ey) - y;
|
||||
|
||||
// Handle cases where x is completely off to one side or other
|
||||
if (_pp_max(sx, ex) <= 0) {
|
||||
while (count--) {
|
||||
nodes[y][node_counts[y]++] = 0;
|
||||
++y;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const int full_tile_width = (_pp_tile_width << _pp_antialias);
|
||||
if (_pp_min(sx, ex) >= full_tile_width) {
|
||||
while (count--) {
|
||||
nodes[y][node_counts[y]++] = full_tile_width;
|
||||
++y;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal case
|
||||
int x = sx;
|
||||
int e = 0;
|
||||
|
||||
const int xinc = _pp_sign(ex - sx);
|
||||
const int einc = abs(ex - sx) + 1;
|
||||
const int dy = ey - sy;
|
||||
|
||||
// If sy < 0 jump to the start, note this does use a divide
|
||||
// but potentially saves many wasted loops below, so is likely worth it.
|
||||
if (sy < 0) {
|
||||
e = einc * -sy;
|
||||
int xjump = e / dy;
|
||||
e -= dy * xjump;
|
||||
x += xinc * xjump;
|
||||
}
|
||||
|
||||
#ifdef USE_RP2040_INTERP
|
||||
interp1->base[1] = full_tile_width;
|
||||
interp1->accum[0] = x;
|
||||
|
||||
// loop over scanlines
|
||||
while(count--) {
|
||||
// consume accumulated error
|
||||
while(e > dy) {e -= dy; interp1->add_raw[0] = xinc;}
|
||||
|
||||
// clamp node x value to tile bounds
|
||||
const int nx = interp1->peek[0];
|
||||
debug(" + adding node at %d, %d\n", x, y);
|
||||
// add node to node list
|
||||
nodes[y][node_counts[y]++] = nx;
|
||||
|
||||
// step to next scanline and accumulate error
|
||||
y++;
|
||||
e += einc;
|
||||
}
|
||||
#else
|
||||
// loop over scanlines
|
||||
while(count--) {
|
||||
// consume accumulated error
|
||||
while(e > dy) {e -= dy; x += xinc;}
|
||||
|
||||
// clamp node x value to tile bounds
|
||||
int nx = _pp_max(_pp_min(x, full_tile_width), 0);
|
||||
debug(" + adding node at %d, %d\n", x, y);
|
||||
// add node to node list
|
||||
nodes[y][node_counts[y]++] = nx;
|
||||
|
||||
// step to next scanline and accumulate error
|
||||
y++;
|
||||
e += einc;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void build_nodes(pp_path_t *contour, pp_rect_t *bounds) {
|
||||
PP_COORD_TYPE aa_scale = (PP_COORD_TYPE)(1 << _pp_antialias);
|
||||
|
||||
pp_point_t tile_origin = (pp_point_t) {
|
||||
.x = bounds->x * aa_scale,
|
||||
.y = bounds->y * aa_scale
|
||||
};
|
||||
|
||||
// start with the last point to close the loop
|
||||
pp_point_t last = {
|
||||
.x = (contour->points[contour->count - 1].x),
|
||||
.y = (contour->points[contour->count - 1].y)
|
||||
};
|
||||
|
||||
if(_pp_transform) {
|
||||
last = pp_point_transform(&last, _pp_transform);
|
||||
}
|
||||
|
||||
last.x *= aa_scale;
|
||||
last.y *= aa_scale;
|
||||
|
||||
last = pp_point_sub(&last, &tile_origin);
|
||||
|
||||
for(uint32_t i = 0; i < contour->count; i++) {
|
||||
pp_point_t point = {
|
||||
.x = (contour->points[i].x),
|
||||
.y = (contour->points[i].y)
|
||||
};
|
||||
|
||||
if(_pp_transform) {
|
||||
point = pp_point_transform(&point, _pp_transform);
|
||||
}
|
||||
|
||||
point.x *= aa_scale;
|
||||
point.y *= aa_scale;
|
||||
|
||||
point = pp_point_sub(&point, &tile_origin);
|
||||
|
||||
add_line_segment_to_nodes(last, point);
|
||||
|
||||
last = point;
|
||||
}
|
||||
}
|
||||
|
||||
int compare_nodes(const void* a, const void* b) {
|
||||
return *((int*)a) - *((int*)b);
|
||||
}
|
||||
|
||||
pp_rect_t render_nodes(uint8_t *buffer, pp_rect_t *tb) {
|
||||
int maxy = -1;
|
||||
|
||||
pp_rect_t rb; // render bounds
|
||||
rb.y = 0;
|
||||
rb.x = tb->w;
|
||||
int maxx = 0;
|
||||
PP_COORD_TYPE aa_scale = (PP_COORD_TYPE)(1 << _pp_antialias);
|
||||
int anitialias_mask = (1 << _pp_antialias) - 1;
|
||||
|
||||
for(int32_t y = 0; y < PP_NODE_BUFFER_HEIGHT; y++) {
|
||||
if(node_counts[y] == 0) {
|
||||
if (y == rb.y) ++rb.y;
|
||||
continue;
|
||||
}
|
||||
|
||||
qsort(&nodes[y][0], node_counts[y], sizeof(int), compare_nodes);
|
||||
|
||||
unsigned char* row_data = &buffer[(y >> _pp_antialias) * _pp_tile_width];
|
||||
bool rendered_any = false;
|
||||
for(uint32_t i = 0; i < node_counts[y]; i += 2) {
|
||||
int sx = nodes[y][i + 0];
|
||||
int ex = nodes[y][i + 1];
|
||||
|
||||
if(sx == ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
rendered_any = true;
|
||||
|
||||
maxx = _pp_max((ex - 1) >> _pp_antialias, maxx);
|
||||
|
||||
debug(" - render span at %d from %d to %d\n", y, sx, ex);
|
||||
|
||||
if (_pp_antialias) {
|
||||
int ax = sx / aa_scale;
|
||||
const int aex = ex / aa_scale;
|
||||
|
||||
rb.x = _pp_min(ax, rb.x);
|
||||
|
||||
if (ax == aex) {
|
||||
row_data[ax] += ex - sx;
|
||||
continue;
|
||||
}
|
||||
|
||||
row_data[ax] += aa_scale - (sx & anitialias_mask);
|
||||
for(ax++; ax < aex; ax++) {
|
||||
row_data[ax] += aa_scale;
|
||||
}
|
||||
|
||||
// This might add 0 to the byte after the end of the row, we pad the tile data
|
||||
// by 1 byte to ensure that is OK
|
||||
row_data[ax] += ex & anitialias_mask;
|
||||
} else {
|
||||
rb.x = _pp_min(sx, rb.x);
|
||||
for(int x = sx; x < ex; x++) {
|
||||
row_data[x]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rendered_any) {
|
||||
debug(" - rendered line %d\n", y);
|
||||
maxy = y;
|
||||
}
|
||||
else if (y == rb.y) {
|
||||
debug(" - render nothing on line %d\n", y);
|
||||
++rb.y;
|
||||
}
|
||||
}
|
||||
|
||||
rb.y >>= _pp_antialias;
|
||||
maxy >>= _pp_antialias;
|
||||
rb.w = (maxx >= rb.x) ? maxx + 1 - rb.x : 0;
|
||||
rb.h = (maxy >= rb.y) ? maxy + 1 - rb.y : 0;
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
void pp_render(pp_poly_t *polygon) {
|
||||
|
||||
debug("> draw polygon with %u contours\n", polygon->count);
|
||||
|
||||
if(polygon->count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// determine extreme bounds
|
||||
pp_rect_t polygon_bounds = pp_polygon_bounds(polygon);
|
||||
|
||||
if(_pp_transform) {
|
||||
polygon_bounds = pp_rect_transform(&polygon_bounds, _pp_transform);
|
||||
}
|
||||
|
||||
debug(" - bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
|
||||
debug(" - clip %d, %d (%d x %d)\n", _pp_clip.x, _pp_clip.y, _pp_clip.w, _pp_clip.h);
|
||||
|
||||
#ifdef USE_RP2040_INTERP
|
||||
interp_hw_save_t interp1_save;
|
||||
interp_save(interp1, &interp1_save);
|
||||
|
||||
interp_config cfg = interp_default_config();
|
||||
interp_config_set_clamp(&cfg, true);
|
||||
interp_config_set_signed(&cfg, true);
|
||||
interp_set_config(interp1, 0, &cfg);
|
||||
interp1->base[0] = 0;
|
||||
#endif
|
||||
|
||||
// iterate over tiles
|
||||
debug(" - processing tiles\n");
|
||||
for(int32_t y = polygon_bounds.y; y < polygon_bounds.y + polygon_bounds.h; y += _pp_tile_height) {
|
||||
for(int32_t x = polygon_bounds.x; x < polygon_bounds.x + polygon_bounds.w; x += _pp_tile_width) {
|
||||
pp_rect_t tb = (pp_rect_t){.x = x, .y = y, .w = _pp_tile_width, .h = _pp_tile_height};
|
||||
tb = pp_rect_intersection(&tb, &_pp_clip);
|
||||
debug(" : %d, %d (%d x %d)\n", tb.x, tb.y, tb.w, tb.h);
|
||||
|
||||
// if no intersection then skip tile
|
||||
if(pp_rect_empty(&tb)) { debug(" : empty when clipped, skipping\n"); continue; }
|
||||
|
||||
// clear existing tile data and nodes
|
||||
memset(node_counts, 0, sizeof(node_counts));
|
||||
memset(tile_buffer, 0, tile_buffer_size);
|
||||
|
||||
// build the nodes for each pp_path_t
|
||||
for(uint32_t i = 0; i < polygon->count; i++) {
|
||||
pp_path_t pp_path_t = polygon->paths[i];
|
||||
debug(" : build nodes for path\n");
|
||||
build_nodes(&pp_path_t, &tb);
|
||||
}
|
||||
|
||||
debug(" : render the tile\n");
|
||||
// render the tile
|
||||
|
||||
pp_rect_t rb = render_nodes(tile_buffer, &tb);
|
||||
tb.x += rb.x; tb.y += rb.y; tb.w = rb.w; tb.h = rb.h;
|
||||
|
||||
debug(" - adjusted render tile bounds %d, %d (%d x %d)\n", rb.x, rb.y, rb.w, rb.h);
|
||||
|
||||
if(pp_rect_empty(&tb)) { debug(" : empty after rendering, skipping\n"); continue; }
|
||||
|
||||
pp_tile_t tile = {
|
||||
.x = tb.x, .y = tb.y, .w = tb.w, .h = tb.h,
|
||||
.stride = (uint32_t)_pp_tile_width,
|
||||
.data = tile_buffer + rb.x + _pp_tile_width * rb.y
|
||||
};
|
||||
|
||||
_pp_tile_callback(&tile);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_RP2040_INTERP
|
||||
interp_restore(interp1, &interp1_save);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // PP_IMPLEMENTATION
|
||||
|
||||
#endif // PP_INCLUDE_H
|
|
@ -1,339 +0,0 @@
|
|||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include "pretty_poly.hpp"
|
||||
|
||||
#include "hardware/interp.h"
|
||||
|
||||
#ifdef PP_DEBUG
|
||||
#define debug(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
uint8_t *tile_buffer;
|
||||
|
||||
int (*nodes)[32];
|
||||
unsigned *node_counts;
|
||||
|
||||
// default tile bounds to X1 antialiasing
|
||||
rect_t tile_bounds(0, 0, tile_buffer_size / node_buffer_size, node_buffer_size);
|
||||
|
||||
// user settings
|
||||
namespace settings {
|
||||
rect_t clip(0, 0, 320, 240);
|
||||
tile_callback_t callback;
|
||||
antialias_t antialias = antialias_t::NONE;
|
||||
}
|
||||
|
||||
void init(void *memory) {
|
||||
uintptr_t m = (uintptr_t)memory;
|
||||
tile_buffer = new(memory) uint8_t[tile_buffer_size];
|
||||
node_counts = new((void *)(m + tile_buffer_size)) unsigned[node_buffer_size];
|
||||
nodes = new((void *)(m + tile_buffer_size + (node_buffer_size * sizeof(unsigned)))) int[node_buffer_size][32];
|
||||
}
|
||||
|
||||
void set_options(tile_callback_t callback, antialias_t antialias, rect_t clip) {
|
||||
settings::callback = callback;
|
||||
settings::antialias = antialias;
|
||||
settings::clip = clip;
|
||||
|
||||
// recalculate the tile size for rendering based on antialiasing level
|
||||
int tile_height = node_buffer_size >> antialias;
|
||||
tile_bounds = rect_t(0, 0, tile_buffer_size / tile_height, tile_height);
|
||||
}
|
||||
|
||||
// dy step (returns 1, 0, or -1 if the supplied value is > 0, == 0, < 0)
|
||||
inline constexpr int sign(int v) {
|
||||
// assumes 32-bit int/unsigned
|
||||
return ((unsigned)-v >> 31) - ((unsigned)v >> 31);
|
||||
}
|
||||
|
||||
// write out the tile bits
|
||||
void debug_tile(const tile_t &tile) {
|
||||
debug(" - tile %d, %d (%d x %d)\n", tile.bounds.x, tile.bounds.y, tile.bounds.w, tile.bounds.h);
|
||||
for(auto y = 0; y < tile.bounds.h; y++) {
|
||||
debug("[%3d]: ", y);
|
||||
for(auto x = 0; x < tile.bounds.w; x++) {
|
||||
debug("%d", tile.get_value(x, y));
|
||||
}
|
||||
debug("\n");
|
||||
}
|
||||
debug("-----------------------\n");
|
||||
}
|
||||
|
||||
void add_line_segment_to_nodes(const point_t<int> &start, const point_t<int> &end) {
|
||||
// swap endpoints if line "pointing up", we do this because we
|
||||
// alway skip the last scanline (so that polygons can but cleanly
|
||||
// up against each other without overlap)
|
||||
int sx = start.x, sy = start.y, ex = end.x, ey = end.y;
|
||||
|
||||
if(ey < sy) {
|
||||
std::swap(sy, ey);
|
||||
std::swap(sx, ex);
|
||||
}
|
||||
|
||||
// Early out if line is completely outside the tile, or has no lines
|
||||
if (ey < 0 || sy >= (int)node_buffer_size || sy == ey) return;
|
||||
|
||||
debug(" + line segment from %d, %d to %d, %d\n", sx, sy, ex, ey);
|
||||
|
||||
// Determine how many in-bounds lines to render
|
||||
int y = std::max(0, sy);
|
||||
int count = std::min((int)node_buffer_size, ey) - y;
|
||||
|
||||
// Handle cases where x is completely off to one side or other
|
||||
if (std::max(sx, ex) <= 0) {
|
||||
while (count--) {
|
||||
nodes[y][node_counts[y]++] = 0;
|
||||
++y;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const int full_tile_width = (tile_bounds.w << settings::antialias);
|
||||
if (std::min(sx, ex) >= full_tile_width) {
|
||||
while (count--) {
|
||||
nodes[y][node_counts[y]++] = full_tile_width;
|
||||
++y;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal case
|
||||
int x = sx;
|
||||
int e = 0;
|
||||
|
||||
const int xinc = sign(ex - sx);
|
||||
const int einc = abs(ex - sx) + 1;
|
||||
const int dy = ey - sy;
|
||||
|
||||
// If sy < 0 jump to the start, note this does use a divide
|
||||
// but potentially saves many wasted loops below, so is likely worth it.
|
||||
if (sy < 0) {
|
||||
e = einc * -sy;
|
||||
int xjump = e / dy;
|
||||
e -= dy * xjump;
|
||||
x += xinc * xjump;
|
||||
}
|
||||
|
||||
interp1->base[1] = full_tile_width;
|
||||
interp1->accum[0] = x;
|
||||
|
||||
// loop over scanlines
|
||||
while(count--) {
|
||||
// consume accumulated error
|
||||
while(e > dy) {e -= dy; interp1->add_raw[0] = xinc;}
|
||||
|
||||
// clamp node x value to tile bounds
|
||||
const int nx = interp1->peek[0];
|
||||
debug(" + adding node at %d, %d\n", x, y);
|
||||
// add node to node list
|
||||
nodes[y][node_counts[y]++] = nx;
|
||||
|
||||
// step to next scanline and accumulate error
|
||||
y++;
|
||||
e += einc;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void build_nodes(const contour_t<T> &contour, const tile_t &tile, point_t<int> origin, int scale) {
|
||||
int ox = (origin.x - tile.bounds.x) << settings::antialias;
|
||||
int oy = (origin.y - tile.bounds.y) << settings::antialias;
|
||||
|
||||
// start with the last point to close the loop
|
||||
point_t<int> last(
|
||||
(((int(contour.points[contour.count - 1].x) * scale) << settings::antialias) / 65536) + ox,
|
||||
(((int(contour.points[contour.count - 1].y) * scale) << settings::antialias) / 65536) + oy
|
||||
);
|
||||
|
||||
for(auto i = 0u; i < contour.count; i++) {
|
||||
point_t<int> point(
|
||||
(((int(contour.points[i].x) * scale) << settings::antialias) / 65536) + ox,
|
||||
(((int(contour.points[i].y) * scale) << settings::antialias) / 65536) + oy
|
||||
);
|
||||
|
||||
add_line_segment_to_nodes(last, point);
|
||||
|
||||
last = point;
|
||||
}
|
||||
}
|
||||
|
||||
void render_nodes(const tile_t &tile, rect_t &bounds) {
|
||||
int maxy = -1;
|
||||
bounds.y = 0;
|
||||
bounds.x = tile.bounds.w;
|
||||
int maxx = 0;
|
||||
int anitialias_mask = (1 << settings::antialias) - 1;
|
||||
|
||||
for(auto y = 0; y < (int)node_buffer_size; y++) {
|
||||
if(node_counts[y] == 0) {
|
||||
if (y == bounds.y) ++bounds.y;
|
||||
continue;
|
||||
}
|
||||
|
||||
std::sort(&nodes[y][0], &nodes[y][0] + node_counts[y]);
|
||||
|
||||
uint8_t* row_data = &tile.data[(y >> settings::antialias) * tile.stride];
|
||||
bool rendered_any = false;
|
||||
for(auto i = 0u; i < node_counts[y]; i += 2) {
|
||||
int sx = nodes[y][i + 0];
|
||||
int ex = nodes[y][i + 1];
|
||||
|
||||
if(sx == ex) {
|
||||
continue;
|
||||
}
|
||||
|
||||
rendered_any = true;
|
||||
|
||||
maxx = std::max((ex - 1) >> settings::antialias, maxx);
|
||||
|
||||
debug(" - render span at %d from %d to %d\n", y, sx, ex);
|
||||
|
||||
if (settings::antialias) {
|
||||
int ax = sx >> settings::antialias;
|
||||
const int aex = ex >> settings::antialias;
|
||||
|
||||
bounds.x = std::min(ax, bounds.x);
|
||||
|
||||
if (ax == aex) {
|
||||
row_data[ax] += ex - sx;
|
||||
continue;
|
||||
}
|
||||
|
||||
row_data[ax] += (1 << settings::antialias) - (sx & anitialias_mask);
|
||||
for(ax++; ax < aex; ax++) {
|
||||
row_data[ax] += (1 << settings::antialias);
|
||||
}
|
||||
|
||||
// This might add 0 to the byte after the end of the row, we pad the tile data
|
||||
// by 1 byte to ensure that is OK
|
||||
row_data[ax] += ex & anitialias_mask;
|
||||
}
|
||||
else {
|
||||
bounds.x = std::min(sx, bounds.x);
|
||||
for(int x = sx; x < ex; x++) {
|
||||
row_data[x]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rendered_any) {
|
||||
debug(" - rendered line %d\n", y);
|
||||
maxy = y;
|
||||
}
|
||||
else if (y == bounds.y) {
|
||||
debug(" - render nothing on line %d\n", y);
|
||||
++bounds.y;
|
||||
}
|
||||
}
|
||||
|
||||
bounds.y >>= settings::antialias;
|
||||
maxy >>= settings::antialias;
|
||||
bounds.w = (maxx >= bounds.x) ? maxx + 1 - bounds.x : 0;
|
||||
bounds.h = (maxy >= bounds.y) ? maxy + 1 - bounds.y : 0;
|
||||
debug(" - rendered tile bounds %d, %d (%d x %d)\n", bounds.x, bounds.y, bounds.w, bounds.h);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(T *points, unsigned count) {
|
||||
std::vector<contour_t<T>> contours;
|
||||
contour_t<T> c(points, count);
|
||||
contours.push_back(c);
|
||||
draw_polygon<T>(contours);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(const std::vector<contour_t<T>>& contours, point_t<int> origin, int scale) {
|
||||
|
||||
debug("> draw polygon with %lu contours\n", contours.size());
|
||||
|
||||
if(contours.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// determine extreme bounds
|
||||
rect_t polygon_bounds = contours[0].bounds();
|
||||
for(auto &contour : contours) {
|
||||
polygon_bounds = polygon_bounds.merge(contour.bounds());
|
||||
}
|
||||
|
||||
polygon_bounds.x = ((polygon_bounds.x * scale) / 65536) + origin.x;
|
||||
polygon_bounds.y = ((polygon_bounds.y * scale) / 65536) + origin.y;
|
||||
polygon_bounds.w = ((polygon_bounds.w * scale) / 65536);
|
||||
polygon_bounds.h = ((polygon_bounds.h * scale) / 65536);
|
||||
|
||||
debug(" - bounds %d, %d (%d x %d)\n", polygon_bounds.x, polygon_bounds.y, polygon_bounds.w, polygon_bounds.h);
|
||||
debug(" - clip %d, %d (%d x %d)\n", settings::clip.x, settings::clip.y, settings::clip.w, settings::clip.h);
|
||||
|
||||
interp_hw_save_t interp1_save;
|
||||
interp_save(interp1, &interp1_save);
|
||||
|
||||
interp_config cfg = interp_default_config();
|
||||
interp_config_set_clamp(&cfg, true);
|
||||
interp_config_set_signed(&cfg, true);
|
||||
interp_set_config(interp1, 0, &cfg);
|
||||
interp1->base[0] = 0;
|
||||
|
||||
//memset(nodes, 0, node_buffer_size * sizeof(unsigned) * 32);
|
||||
|
||||
// iterate over tiles
|
||||
debug(" - processing tiles\n");
|
||||
for(auto y = polygon_bounds.y; y < polygon_bounds.y + polygon_bounds.h; y += tile_bounds.h) {
|
||||
for(auto x = polygon_bounds.x; x < polygon_bounds.x + polygon_bounds.w; x += tile_bounds.w) {
|
||||
tile_t tile;
|
||||
tile.bounds = rect_t(x, y, tile_bounds.w, tile_bounds.h).intersection(settings::clip);
|
||||
tile.stride = tile_bounds.w;
|
||||
tile.data = tile_buffer;
|
||||
debug(" : %d, %d (%d x %d)\n", tile.bounds.x, tile.bounds.y, tile.bounds.w, tile.bounds.h);
|
||||
|
||||
// if no intersection then skip tile
|
||||
if(tile.bounds.empty()) {
|
||||
debug(" : empty when clipped, skipping\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
// clear existing tile data and nodes
|
||||
memset(node_counts, 0, node_buffer_size * sizeof(unsigned));
|
||||
memset(tile.data, 0, tile_buffer_size);
|
||||
|
||||
// build the nodes for each contour
|
||||
for(const contour_t<T> &contour : contours) {
|
||||
debug(" : build nodes for contour\n");
|
||||
build_nodes(contour, tile, origin, scale);
|
||||
}
|
||||
|
||||
debug(" : render the tile\n");
|
||||
// render the tile
|
||||
rect_t bounds;
|
||||
render_nodes(tile, bounds);
|
||||
if (bounds.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
tile.data += bounds.x + tile.stride * bounds.y;
|
||||
tile.bounds.x += bounds.x;
|
||||
tile.bounds.y += bounds.y;
|
||||
tile.bounds.w = bounds.w;
|
||||
tile.bounds.h = bounds.h;
|
||||
|
||||
settings::callback(tile);
|
||||
}
|
||||
}
|
||||
|
||||
interp_restore(interp1, &interp1_save);
|
||||
}
|
||||
}
|
||||
|
||||
template void pretty_poly::draw_polygon<int>(const std::vector<contour_t<int>>& contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<float>(const std::vector<contour_t<float>>& contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<uint8_t>(const std::vector<contour_t<uint8_t>>& contours, point_t<int> origin, int scale);
|
||||
template void pretty_poly::draw_polygon<int8_t>(const std::vector<contour_t<int8_t>>& contours, point_t<int> origin, int scale);
|
|
@ -1,73 +0,0 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
|
||||
#include "pretty_poly_types.hpp"
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
class file_io {
|
||||
private:
|
||||
void *state;
|
||||
size_t filesize = 0;
|
||||
|
||||
public:
|
||||
file_io(std::string_view path);
|
||||
~file_io();
|
||||
size_t seek(size_t pos);
|
||||
size_t read(void *buf, size_t len);
|
||||
size_t tell();
|
||||
bool fail();
|
||||
};
|
||||
|
||||
// buffer that each tile is rendered into before callback
|
||||
constexpr unsigned tile_buffer_size = 1024;
|
||||
|
||||
// polygon node buffer handles at most 16 line intersections per scanline
|
||||
// is this enough for cjk/emoji? (requires a 2kB buffer)
|
||||
constexpr unsigned node_buffer_size = 32;
|
||||
|
||||
typedef std::function<void(const tile_t &tile)> tile_callback_t;
|
||||
|
||||
// user settings
|
||||
namespace settings {
|
||||
extern rect_t clip;
|
||||
extern tile_callback_t callback;
|
||||
extern antialias_t antialias;
|
||||
}
|
||||
|
||||
constexpr size_t buffer_size() {
|
||||
return tile_buffer_size + (node_buffer_size * sizeof(unsigned)) + (node_buffer_size * 32 * sizeof(int));
|
||||
}
|
||||
|
||||
constexpr size_t buffer_size();
|
||||
|
||||
void init(void *memory);
|
||||
|
||||
void set_options(tile_callback_t callback, antialias_t antialias, rect_t clip);
|
||||
|
||||
// dy step (returns 1, 0, or -1 if the supplied value is > 0, == 0, < 0)
|
||||
inline constexpr int sign(int v);
|
||||
|
||||
// write out the tile bits
|
||||
void debug_tile(const tile_t &tile);
|
||||
|
||||
void add_line_segment_to_nodes(const point_t<int> &start, const point_t<int> &end);
|
||||
|
||||
template<typename T>
|
||||
void build_nodes(const contour_t<T> &contour, const tile_t &tile, point_t<int> origin = point_t<int>(0, 0), int scale = 65536);
|
||||
|
||||
void render_nodes(const tile_t &tile, rect_t &bounds);
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(T *points, unsigned count);
|
||||
|
||||
template<typename T>
|
||||
void draw_polygon(const std::vector<contour_t<T>>& contours, point_t<int> origin = point_t<int>(0, 0), int scale = 65536);
|
||||
}
|
|
@ -1,162 +0,0 @@
|
|||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef PP_DEBUG
|
||||
#define debug(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
|
||||
namespace pretty_poly {
|
||||
|
||||
enum antialias_t {NONE = 0, X4 = 1, X16 = 2};
|
||||
|
||||
// 3x3 matrix for coordinate transformations
|
||||
struct mat3_t {
|
||||
float v00 = 0.0f, v10 = 0.0f, v20 = 0.0f, v01 = 0.0f, v11 = 0.0f, v21 = 0.0f, v02 = 0.0f, v12 = 0.0f, v22 = 0.0f;
|
||||
mat3_t() = default;
|
||||
mat3_t(const mat3_t &m) = default;
|
||||
inline mat3_t& operator*= (const mat3_t &m) {
|
||||
float r00 = this->v00 * m.v00 + this->v01 * m.v10 + this->v02 * m.v20;
|
||||
float r01 = this->v00 * m.v01 + this->v01 * m.v11 + this->v02 * m.v21;
|
||||
float r02 = this->v00 * m.v02 + this->v01 * m.v12 + this->v02 * m.v22;
|
||||
float r10 = this->v10 * m.v00 + this->v11 * m.v10 + this->v12 * m.v20;
|
||||
float r11 = this->v10 * m.v01 + this->v11 * m.v11 + this->v12 * m.v21;
|
||||
float r12 = this->v10 * m.v02 + this->v11 * m.v12 + this->v12 * m.v22;
|
||||
float r20 = this->v20 * m.v00 + this->v21 * m.v10 + this->v22 * m.v20;
|
||||
float r21 = this->v20 * m.v01 + this->v21 * m.v11 + this->v22 * m.v21;
|
||||
float r22 = this->v20 * m.v02 + this->v21 * m.v12 + this->v22 * m.v22;
|
||||
this->v00 = r00; this->v01 = r01; this->v02 = r02;
|
||||
this->v10 = r10; this->v11 = r11; this->v12 = r12;
|
||||
this->v20 = r20; this->v21 = r21; this->v22 = r22;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static mat3_t identity() {mat3_t m; m.v00 = m.v11 = m.v22 = 1.0f; return m;}
|
||||
static mat3_t rotation(float a) {
|
||||
float c = cosf(a), s = sinf(a); mat3_t r = mat3_t::identity();
|
||||
r.v00 = c; r.v01 = -s; r.v10 = s; r.v11 = c; return r;}
|
||||
static mat3_t translation(float x, float y) {
|
||||
mat3_t r = mat3_t::identity(); r.v02 = x; r.v12 = y; return r;}
|
||||
static mat3_t scale(float x, float y) {
|
||||
mat3_t r = mat3_t::identity(); r.v00 = x; r.v11 = y; return r;}
|
||||
};
|
||||
|
||||
// 2x2 matrix for rotations and scales
|
||||
struct mat2_t {
|
||||
float v00 = 0.0f, v10 = 0.0f, v01 = 0.0f, v11 = 0.0f;
|
||||
mat2_t() = default;
|
||||
mat2_t(const mat2_t &m) = default;
|
||||
inline mat2_t& operator*= (const mat2_t &m) {
|
||||
float r00 = this->v00 * m.v00 + this->v01 * m.v10;
|
||||
float r01 = this->v00 * m.v01 + this->v01 * m.v11;
|
||||
float r10 = this->v10 * m.v00 + this->v11 * m.v10;
|
||||
float r11 = this->v10 * m.v01 + this->v11 * m.v11;
|
||||
this->v00 = r00; this->v01 = r01;
|
||||
this->v10 = r10; this->v11 = r11;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static mat2_t identity() {mat2_t m; m.v00 = m.v11 = 1.0f; return m;}
|
||||
static mat2_t rotation(float a) {
|
||||
float c = cosf(a), s = sinf(a); mat2_t r;
|
||||
r.v00 = c; r.v01 = -s; r.v10 = s; r.v11 = c; return r;}
|
||||
static mat2_t scale(float x, float y) {
|
||||
mat2_t r; r.v00 = x; r.v11 = y; return r;}
|
||||
};
|
||||
|
||||
// point type for contour points
|
||||
template<typename T = int>
|
||||
struct __attribute__ ((packed)) point_t {
|
||||
T x, y;
|
||||
point_t(T x, T y) : x(x), y(y) {}
|
||||
point_t() : x(0), y(0) {}
|
||||
inline point_t& operator-= (const point_t &a) {x -= a.x; y -= a.y; return *this;}
|
||||
inline point_t& operator+= (const point_t &a) {x += a.x; y += a.y; return *this;}
|
||||
inline point_t& operator*= (const float a) {x *= a; y *= a; return *this;}
|
||||
inline point_t& operator*= (const mat2_t &a) {this->transform(a); return *this;}
|
||||
inline point_t& operator*= (const mat3_t &a) {this->transform(a); return *this;}
|
||||
inline point_t& operator/= (const float a) {x /= a; y /= a; return *this;}
|
||||
inline point_t& operator/= (const point_t &a) {x /= a.x; y /= a.y; return *this;}
|
||||
void transform(const mat3_t &m) {
|
||||
float tx = x, ty = y;
|
||||
this->x = (m.v00 * tx + m.v01 * ty + m.v02);
|
||||
this->y = (m.v10 * tx + m.v11 * ty + m.v12);
|
||||
}
|
||||
void transform(const mat2_t &m) {
|
||||
float tx = x, ty = y;
|
||||
this->x = (m.v00 * tx + m.v01 * ty);
|
||||
this->y = (m.v10 * tx + m.v11 * ty);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T> inline point_t<T> operator- (point_t<T> lhs, const point_t<T> &rhs) { lhs -= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator- (const point_t<T> &rhs) { return point_t<T>(-rhs.x, -rhs.y); }
|
||||
template<typename T> inline point_t<T> operator+ (point_t<T> lhs, const point_t<T> &rhs) { lhs += rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const float rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const point_t<T> &rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator* (point_t<T> lhs, const mat3_t &rhs) { lhs *= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator/ (point_t<T> lhs, const float rhs) { lhs /= rhs; return lhs; }
|
||||
template<typename T> inline point_t<T> operator/ (point_t<T> lhs, const point_t<T> &rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; }
|
||||
|
||||
|
||||
// rect type for bounds and clipping rectangles
|
||||
struct rect_t {
|
||||
int x, y, w, h;
|
||||
rect_t() : x(0), y(0), w(0), h(0) {}
|
||||
rect_t(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
|
||||
bool empty() const {return this->w == 0 || this->h == 0;}
|
||||
rect_t intersection(const rect_t &c) {
|
||||
return rect_t(std::max(this->x, c.x), std::max(this->y, c.y),
|
||||
std::max(0, std::min(this->x + this->w, c.x + c.w) - std::max(this->x, c.x)),
|
||||
std::max(0, std::min(this->y + this->h, c.y + c.h) - std::max(this->y, c.y)));
|
||||
}
|
||||
rect_t merge(const rect_t &c) {
|
||||
return rect_t(std::min(this->x, c.x), std::min(this->y, c.y),
|
||||
std::max(this->x + this->w, c.x + c.w) - std::min(this->x, c.x),
|
||||
std::max(this->y + this->h, c.y + c.h) - std::min(this->y, c.y));
|
||||
}
|
||||
};
|
||||
|
||||
struct tile_t {
|
||||
rect_t bounds;
|
||||
unsigned stride;
|
||||
uint8_t *data;
|
||||
|
||||
tile_t() {};
|
||||
|
||||
inline int get_value(int x, int y) const {
|
||||
return this->data[x + y * this->stride];
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct contour_t {
|
||||
point_t<T> *points;
|
||||
unsigned count;
|
||||
|
||||
contour_t() {}
|
||||
contour_t(const std::vector<point_t<T>>& v) : points(v.data()), count(v.size()) {};
|
||||
contour_t(point_t<T> *points, unsigned count) : points(points), count(count) {};
|
||||
|
||||
// TODO: Make this work, it's so much nicer to use auto point : contour
|
||||
//point_t<T> *begin() const { return points; };
|
||||
//point_t<T> *end() const { return points + count * sizeof(point_t<T>); };
|
||||
|
||||
rect_t bounds() const {
|
||||
T minx = this->points[0].x, maxx = minx;
|
||||
T miny = this->points[0].y, maxy = miny;
|
||||
for(auto i = 1u; i < this->count; i++) {
|
||||
minx = std::min(minx, this->points[i].x);
|
||||
miny = std::min(miny, this->points[i].y);
|
||||
maxx = std::max(maxx, this->points[i].x);
|
||||
maxy = std::max(maxy, this->points[i].y);
|
||||
}
|
||||
return rect_t(minx, miny, maxx - minx, maxy - miny);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -2,7 +2,6 @@ add_library(usermod_picovector INTERFACE)
|
|||
|
||||
target_sources(usermod_picovector INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_vector/pico_vector.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_vector/pretty_poly.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_vector/alright_fonts.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/picovector.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/picovector.cpp
|
||||
|
@ -21,3 +20,9 @@ target_compile_definitions(usermod_picovector INTERFACE
|
|||
target_link_libraries(usermod_picovector INTERFACE hardware_interp)
|
||||
|
||||
target_link_libraries(usermod INTERFACE usermod_picovector)
|
||||
|
||||
set_source_files_properties(
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/pico_vector/pico_vector.cpp
|
||||
PROPERTIES COMPILE_FLAGS
|
||||
"-Wno-narrowing"
|
||||
)
|
||||
|
|
|
@ -22,7 +22,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
|
|||
MP_TYPE_FLAG_NONE,
|
||||
make_new, POLYGON_make_new,
|
||||
print, POLYGON_print,
|
||||
iter, POLYGON_getiter,
|
||||
iter, PATH_getiter,
|
||||
locals_dict, (mp_obj_dict_t*)&POLYGON_locals_dict
|
||||
);
|
||||
MP_DEFINE_CONST_OBJ_TYPE(
|
||||
|
@ -45,7 +45,7 @@ const mp_obj_type_t POLYGON_type = {
|
|||
.name = MP_QSTR_polygon,
|
||||
.make_new = POLYGON_make_new,
|
||||
.print = POLYGON_print,
|
||||
.iter = POLYGON_getiter,
|
||||
.iter = PATH_getiter,
|
||||
.locals_dict = (mp_obj_dict_t*)&POLYGON_locals_dict,
|
||||
};
|
||||
const mp_obj_type_t REGULAR_POLYGON_type = {
|
||||
|
|
|
@ -10,6 +10,7 @@ extern "C" {
|
|||
#include "py/stream.h"
|
||||
#include "py/reader.h"
|
||||
#include "extmod/vfs.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef struct _ModPicoGraphics_obj_t {
|
||||
mp_obj_base_t base;
|
||||
|
@ -24,15 +25,53 @@ typedef struct _VECTOR_obj_t {
|
|||
PicoVector *vector;
|
||||
} _VECTOR_obj_t;
|
||||
|
||||
typedef struct _POLYGON_obj_t {
|
||||
typedef struct _PATH_obj_t {
|
||||
mp_obj_base_t base;
|
||||
pretty_poly::contour_t<picovector_point_type> contour;
|
||||
} _POLYGON_obj_t;
|
||||
pp_path_t path;
|
||||
} _PATH_obj_t;
|
||||
|
||||
pretty_poly::file_io::file_io(std::string_view filename) {
|
||||
mp_obj_t fn = mp_obj_new_str(filename.data(), (mp_uint_t)filename.size());
|
||||
void __printf_debug_flush() {
|
||||
for(auto i = 0u; i < 10; i++) {
|
||||
sleep_ms(1);
|
||||
mp_event_handle_nowait();
|
||||
}
|
||||
}
|
||||
|
||||
//mp_printf(&mp_plat_print, "Opening file %s\n", filename.data());
|
||||
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
|
||||
|
||||
void af_debug(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int ret = mp_vprintf(&mp_plat_print, fmt, ap);
|
||||
va_end(ap);
|
||||
__printf_debug_flush();
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
void *af_malloc(size_t size) {
|
||||
//mp_printf(&mp_plat_print, "af_malloc %lu\n", size);
|
||||
//__printf_debug_flush();
|
||||
void *addr = m_tracked_calloc(sizeof(uint8_t), size);
|
||||
//mp_printf(&mp_plat_print, "addr %lu\n", addr);
|
||||
//__printf_debug_flush();
|
||||
return addr;
|
||||
}
|
||||
|
||||
void *af_realloc(void *p, size_t size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void af_free(void *p) {
|
||||
//mp_printf(&mp_plat_print, "af_free\n");
|
||||
//__printf_debug_flush();
|
||||
m_tracked_free(p);
|
||||
}
|
||||
|
||||
void* fileio_open(const char *filename) {
|
||||
mp_obj_t fn = mp_obj_new_str(filename, (mp_uint_t)strlen(filename));
|
||||
|
||||
//mp_printf(&mp_plat_print, "Opening file %s\n", filename);
|
||||
//__printf_debug_flush();
|
||||
|
||||
mp_obj_t args[2] = {
|
||||
fn,
|
||||
|
@ -41,36 +80,44 @@ pretty_poly::file_io::file_io(std::string_view filename) {
|
|||
|
||||
// Stat the file to get its size
|
||||
// example tuple response: (32768, 0, 0, 0, 0, 0, 5153, 1654709815, 1654709815, 1654709815)
|
||||
mp_obj_t stat = mp_vfs_stat(fn);
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(stat, mp_obj_tuple_t);
|
||||
filesize = mp_obj_get_int(tuple->items[6]);
|
||||
//mp_obj_t stat = mp_vfs_stat(fn);
|
||||
//mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(stat, mp_obj_tuple_t);
|
||||
//int filesize = mp_obj_get_int(tuple->items[6]);
|
||||
//mp_printf(&mp_plat_print, "Size %lu\n", filesize);
|
||||
|
||||
mp_obj_t fhandle = mp_vfs_open(MP_ARRAY_SIZE(args), &args[0], (mp_map_t *)&mp_const_empty_map);
|
||||
|
||||
this->state = (void *)fhandle;
|
||||
return (void*)fhandle;
|
||||
}
|
||||
|
||||
pretty_poly::file_io::~file_io() {
|
||||
mp_stream_close((mp_obj_t)this->state);
|
||||
void fileio_close(void* fhandle) {
|
||||
mp_stream_close((mp_obj_t)fhandle);
|
||||
}
|
||||
|
||||
size_t pretty_poly::file_io::read(void *buf, size_t len) {
|
||||
size_t fileio_read(void* fhandle, void *buf, size_t len) {
|
||||
//mp_printf(&mp_plat_print, "Reading %lu bytes\n", len);
|
||||
mp_obj_t fhandle = this->state;
|
||||
//__printf_debug_flush();
|
||||
int error;
|
||||
return mp_stream_read_exactly(fhandle, buf, len, &error);
|
||||
return mp_stream_read_exactly((mp_obj_t)fhandle, buf, len, &error);
|
||||
}
|
||||
|
||||
size_t pretty_poly::file_io::tell() {
|
||||
mp_obj_t fhandle = this->state;
|
||||
int fileio_getc(void* fhandle) {
|
||||
unsigned char buf;
|
||||
//mp_printf(&mp_plat_print, "Reading char\n");
|
||||
//__printf_debug_flush();
|
||||
fileio_read(fhandle, (void *)&buf, 1);
|
||||
return (int)buf;
|
||||
}
|
||||
|
||||
size_t fileio_tell(void* fhandle) {
|
||||
struct mp_stream_seek_t seek_s;
|
||||
seek_s.offset = 0;
|
||||
seek_s.whence = SEEK_CUR;
|
||||
|
||||
const mp_stream_p_t *stream_p = mp_get_stream(fhandle);
|
||||
const mp_stream_p_t *stream_p = mp_get_stream((mp_obj_t)fhandle);
|
||||
|
||||
int error;
|
||||
mp_uint_t res = stream_p->ioctl(fhandle, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
|
||||
mp_uint_t res = stream_p->ioctl((mp_obj_t)fhandle, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
|
||||
if (res == MP_STREAM_ERROR) {
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
|
@ -78,21 +125,16 @@ size_t pretty_poly::file_io::tell() {
|
|||
return seek_s.offset;
|
||||
}
|
||||
|
||||
bool pretty_poly::file_io::fail() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Re-implementation of stream.c/STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args)
|
||||
size_t pretty_poly::file_io::seek(size_t pos) {
|
||||
mp_obj_t fhandle = this->state;
|
||||
size_t fileio_seek(void* fhandle, size_t pos) {
|
||||
struct mp_stream_seek_t seek_s;
|
||||
seek_s.offset = pos;
|
||||
seek_s.whence = SEEK_SET;
|
||||
|
||||
const mp_stream_p_t *stream_p = mp_get_stream(fhandle);
|
||||
const mp_stream_p_t *stream_p = mp_get_stream((mp_obj_t)fhandle);
|
||||
|
||||
int error;
|
||||
mp_uint_t res = stream_p->ioctl(fhandle, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
|
||||
mp_uint_t res = stream_p->ioctl((mp_obj_t)fhandle, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
|
||||
if (res == MP_STREAM_ERROR) {
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
|
@ -100,6 +142,7 @@ size_t pretty_poly::file_io::seek(size_t pos) {
|
|||
return seek_s.offset;
|
||||
}
|
||||
|
||||
/*
|
||||
static const std::string_view mp_obj_to_string_r(const mp_obj_t &obj) {
|
||||
if(mp_obj_is_str_or_bytes(obj)) {
|
||||
GET_STR_DATA_LEN(obj, str, str_len);
|
||||
|
@ -107,6 +150,7 @@ static const std::string_view mp_obj_to_string_r(const mp_obj_t &obj) {
|
|||
}
|
||||
mp_raise_TypeError("can't convert object to str implicitly");
|
||||
}
|
||||
*/
|
||||
|
||||
/* POLYGON */
|
||||
|
||||
|
@ -122,7 +166,7 @@ mp_obj_t RECTANGLE_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_k
|
|||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t);
|
||||
_PATH_obj_t *self = m_new_obj_with_finaliser(_PATH_obj_t);
|
||||
self->base.type = &POLYGON_type;
|
||||
|
||||
int x = args[ARG_x].u_int;
|
||||
|
@ -130,13 +174,13 @@ mp_obj_t RECTANGLE_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_k
|
|||
int w = args[ARG_w].u_int;
|
||||
int h = args[ARG_h].u_int;
|
||||
|
||||
self->contour.points = m_new(pretty_poly::point_t<picovector_point_type>, 4);
|
||||
self->contour.count = 4;
|
||||
self->path.points = m_new(pp_point_t, 4);
|
||||
self->path.count = 4;
|
||||
|
||||
self->contour.points[0] = {picovector_point_type(x), picovector_point_type(y)};
|
||||
self->contour.points[1] = {picovector_point_type(x + w), picovector_point_type(y)};
|
||||
self->contour.points[2] = {picovector_point_type(x + w), picovector_point_type(y + h)};
|
||||
self->contour.points[3] = {picovector_point_type(x), picovector_point_type(y + h)};
|
||||
self->path.points[0] = {picovector_point_type(x), picovector_point_type(y)};
|
||||
self->path.points[1] = {picovector_point_type(x + w), picovector_point_type(y)};
|
||||
self->path.points[2] = {picovector_point_type(x + w), picovector_point_type(y + h)};
|
||||
self->path.points[3] = {picovector_point_type(x), picovector_point_type(y + h)};
|
||||
|
||||
return self;
|
||||
}
|
||||
|
@ -154,7 +198,7 @@ mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size
|
|||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t);
|
||||
_PATH_obj_t *self = m_new_obj_with_finaliser(_PATH_obj_t);
|
||||
self->base.type = &POLYGON_type;
|
||||
|
||||
Point origin(args[ARG_x].u_int, args[ARG_y].u_int);
|
||||
|
@ -170,12 +214,12 @@ mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size
|
|||
|
||||
float angle = (360.0f / sides) * (M_PI / 180.0f);
|
||||
|
||||
self->contour.points = m_new(pretty_poly::point_t<picovector_point_type>, sides);
|
||||
self->contour.count = sides;
|
||||
self->path.points = m_new(pp_point_t, sides);
|
||||
self->path.count = sides;
|
||||
|
||||
for(auto s = 0u; s < sides; s++) {
|
||||
float current_angle = angle * s + rotation;
|
||||
self->contour.points[s] = {
|
||||
self->path.points[s] = {
|
||||
(picovector_point_type)(cos(current_angle) * radius) + o_x,
|
||||
(picovector_point_type)(sin(current_angle) * radius) + o_y
|
||||
};
|
||||
|
@ -185,7 +229,7 @@ mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size
|
|||
}
|
||||
|
||||
mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||
_POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t);
|
||||
_PATH_obj_t *self = m_new_obj_with_finaliser(_PATH_obj_t);
|
||||
self->base.type = &POLYGON_type;
|
||||
|
||||
size_t num_points = n_args;
|
||||
|
@ -193,8 +237,8 @@ mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
|||
|
||||
if(num_points < 3) mp_raise_ValueError("Polygon: At least 3 points required.");
|
||||
|
||||
self->contour.points = m_new(pretty_poly::point_t<picovector_point_type>, num_points);
|
||||
self->contour.count = num_points;
|
||||
self->path.points = m_new(pp_point_t, num_points);
|
||||
self->path.count = num_points;
|
||||
|
||||
for(auto i = 0u; i < num_points; i++) {
|
||||
mp_obj_t c_obj = points[i];
|
||||
|
@ -205,7 +249,7 @@ mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
|||
|
||||
if(t_point->len != 2) mp_raise_ValueError("Tuple must have X, Y");
|
||||
|
||||
self->contour.points[i] = {
|
||||
self->path.points[i] = {
|
||||
(picovector_point_type)mp_obj_get_int(t_point->items[0]),
|
||||
(picovector_point_type)mp_obj_get_int(t_point->items[1]),
|
||||
};
|
||||
|
@ -215,54 +259,61 @@ mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
|||
}
|
||||
|
||||
mp_obj_t POLYGON_centroid(mp_obj_t self_in) {
|
||||
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
|
||||
_PATH_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PATH_obj_t);
|
||||
|
||||
pretty_poly::point_t<picovector_point_type> sum(0, 0);
|
||||
PP_COORD_TYPE sum_x = (PP_COORD_TYPE)0;
|
||||
PP_COORD_TYPE sum_y = (PP_COORD_TYPE)0;
|
||||
|
||||
for(auto i = 0u; i < self->contour.count; i++) {
|
||||
sum += self->contour.points[i];
|
||||
for(auto i = 0u; i < self->path.count; i++) {
|
||||
sum_x += self->path.points[i].x;
|
||||
sum_y += self->path.points[i].y;
|
||||
}
|
||||
|
||||
sum /= (float)self->contour.count;
|
||||
sum_x /= (float)self->path.count;
|
||||
sum_y /= (float)self->path.count;
|
||||
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_int((int)(sum.x));
|
||||
tuple[1] = mp_obj_new_int((int)(sum.y));
|
||||
tuple[0] = mp_obj_new_int((int)(sum_x));
|
||||
tuple[1] = mp_obj_new_int((int)(sum_y));
|
||||
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
|
||||
mp_obj_t POLYGON_bounds(mp_obj_t self_in) {
|
||||
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
|
||||
_PATH_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PATH_obj_t);
|
||||
|
||||
pp_rect_t bounds = pp_contour_bounds(&self->path);
|
||||
|
||||
mp_obj_t tuple[4];
|
||||
tuple[0] = mp_obj_new_int((int)(self->contour.bounds().x));
|
||||
tuple[1] = mp_obj_new_int((int)(self->contour.bounds().y));
|
||||
tuple[2] = mp_obj_new_int((int)(self->contour.bounds().w));
|
||||
tuple[3] = mp_obj_new_int((int)(self->contour.bounds().h));
|
||||
tuple[0] = mp_obj_new_int((int)(bounds.x));
|
||||
tuple[1] = mp_obj_new_int((int)(bounds.y));
|
||||
tuple[2] = mp_obj_new_int((int)(bounds.w));
|
||||
tuple[3] = mp_obj_new_int((int)(bounds.h));
|
||||
|
||||
return mp_obj_new_tuple(4, tuple);
|
||||
}
|
||||
|
||||
void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
(void)kind;
|
||||
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
|
||||
_PATH_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PATH_obj_t);
|
||||
|
||||
pp_rect_t bounds = pp_contour_bounds(&self->path);
|
||||
|
||||
mp_print_str(print, "Polygon(points = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->contour.count), PRINT_REPR);
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->path.count), PRINT_REPR);
|
||||
mp_print_str(print, ", bounds = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->contour.bounds().x), PRINT_REPR);
|
||||
mp_obj_print_helper(print, mp_obj_new_int(bounds.x), PRINT_REPR);
|
||||
mp_print_str(print, ", ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->contour.bounds().y), PRINT_REPR);
|
||||
mp_obj_print_helper(print, mp_obj_new_int(bounds.y), PRINT_REPR);
|
||||
mp_print_str(print, ", ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->contour.bounds().w), PRINT_REPR);
|
||||
mp_obj_print_helper(print, mp_obj_new_int(bounds.w), PRINT_REPR);
|
||||
mp_print_str(print, ", ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->contour.bounds().h), PRINT_REPR);
|
||||
mp_obj_print_helper(print, mp_obj_new_int(bounds.h), PRINT_REPR);
|
||||
mp_print_str(print, ")");
|
||||
}
|
||||
|
||||
mp_obj_t POLYGON__del__(mp_obj_t self_in) {
|
||||
_POLYGON_obj_t *self = MP_OBJ_TO_PTR2(self_in, _POLYGON_obj_t);
|
||||
_PATH_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PATH_obj_t);
|
||||
(void)self;
|
||||
// TODO: Do we actually need to free anything here, if it's on GC heap it should get collected
|
||||
return mp_const_none;
|
||||
|
@ -275,26 +326,26 @@ typedef struct _mp_obj_polygon_it_t {
|
|||
size_t cur;
|
||||
} mp_obj_polygon_it_t;
|
||||
|
||||
STATIC mp_obj_t py_image_it_iternext(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t py_path_it_iternext(mp_obj_t self_in) {
|
||||
mp_obj_polygon_it_t *self = MP_OBJ_TO_PTR2(self_in, mp_obj_polygon_it_t);
|
||||
_POLYGON_obj_t *polygon = MP_OBJ_TO_PTR2(self->polygon, _POLYGON_obj_t);
|
||||
_PATH_obj_t *path = MP_OBJ_TO_PTR2(self->polygon, _PATH_obj_t);
|
||||
|
||||
//mp_printf(&mp_plat_print, "points: %d, current: %d\n", polygon->contour.count, self->cur);
|
||||
|
||||
if(self->cur >= polygon->contour.count) return MP_OBJ_STOP_ITERATION;
|
||||
if(self->cur >= path->path.count) return MP_OBJ_STOP_ITERATION;
|
||||
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_int((int)(polygon->contour.points[self->cur].x));
|
||||
tuple[1] = mp_obj_new_int((int)(polygon->contour.points[self->cur].y));
|
||||
tuple[0] = mp_obj_new_int((int)(path->path.points[self->cur].x));
|
||||
tuple[1] = mp_obj_new_int((int)(path->path.points[self->cur].y));
|
||||
|
||||
self->cur++;
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
|
||||
mp_obj_t POLYGON_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
mp_obj_t PATH_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
mp_obj_polygon_it_t *o = (mp_obj_polygon_it_t *)iter_buf;
|
||||
o->base.type = &mp_type_polymorph_iter;
|
||||
o->iternext = py_image_it_iternext;
|
||||
o->iternext = py_path_it_iternext;
|
||||
o->polygon = o_in;
|
||||
o->cur = 0;
|
||||
return MP_OBJ_FROM_PTR(o);
|
||||
|
@ -320,7 +371,8 @@ mp_obj_t VECTOR_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
|||
|
||||
// The PicoVector class calls `pretty_poly::init()` with the memory region
|
||||
// it does not store a pointer to this, so we need to store one ourselves
|
||||
self->mem = m_new(uint8_t, PicoVector::pretty_poly_buffer_size());
|
||||
// TODO: C Pretty Poly does not support runtime memory allocation
|
||||
//self->mem = m_new(uint8_t, PicoVector::pretty_poly_buffer_size());
|
||||
|
||||
self->vector = m_new_class(PicoVector, graphics->graphics, self->mem);
|
||||
|
||||
|
@ -329,12 +381,16 @@ mp_obj_t VECTOR_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
|
|||
|
||||
mp_obj_t VECTOR_set_font(mp_obj_t self_in, mp_obj_t font, mp_obj_t size) {
|
||||
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
|
||||
(void)self;
|
||||
|
||||
int font_size = mp_obj_get_int(size);
|
||||
(void)font_size;
|
||||
bool result = false;
|
||||
|
||||
if (mp_obj_is_str(font)) {
|
||||
result = self->vector->set_font(mp_obj_to_string_r(font), font_size);
|
||||
// TODO: Implement when Alright Fonts rewrite is ready
|
||||
GET_STR_DATA_LEN(font, str, str_len);
|
||||
result = self->vector->set_font((const char*)str, font_size);
|
||||
}
|
||||
else {
|
||||
|
||||
|
@ -344,8 +400,11 @@ mp_obj_t VECTOR_set_font(mp_obj_t self_in, mp_obj_t font, mp_obj_t size) {
|
|||
|
||||
mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size) {
|
||||
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
|
||||
(void)self;
|
||||
|
||||
int font_size = mp_obj_get_int(size);
|
||||
(void)font_size;
|
||||
// TODO: Implement when Alright Fonts rewrite is ready
|
||||
self->vector->set_font_size(font_size);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
@ -353,7 +412,7 @@ mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size) {
|
|||
mp_obj_t VECTOR_set_antialiasing(mp_obj_t self_in, mp_obj_t aa) {
|
||||
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
|
||||
|
||||
self->vector->set_antialiasing((pretty_poly::antialias_t)mp_obj_get_int(aa));
|
||||
self->vector->set_antialiasing((pp_antialias_t)mp_obj_get_int(aa));
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
|
@ -371,6 +430,7 @@ mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
|||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _VECTOR_obj_t);
|
||||
(void)self;
|
||||
|
||||
mp_obj_t text_obj = args[ARG_text].u_obj;
|
||||
|
||||
|
@ -382,13 +442,22 @@ mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
|||
|
||||
int x = args[ARG_x].u_int;
|
||||
int y = args[ARG_y].u_int;
|
||||
(void)x;
|
||||
(void)y;
|
||||
|
||||
if(args[ARG_angle].u_obj == mp_const_none) {
|
||||
self->vector->text(t, Point(x, y));
|
||||
} else {
|
||||
self->vector->text(t, Point(x, y), mp_obj_get_float(args[ARG_angle].u_obj));
|
||||
pp_mat3_t tt = pp_mat3_identity();
|
||||
|
||||
if(args[ARG_angle].u_obj != mp_const_none) {
|
||||
pp_mat3_rotate(&tt, mp_obj_get_float(args[ARG_angle].u_obj));
|
||||
}
|
||||
|
||||
pp_mat3_translate(&tt, (float)x, (float)y);
|
||||
|
||||
//mp_printf(&mp_plat_print, "self->vector->text()\n");
|
||||
//__printf_debug_flush();
|
||||
|
||||
self->vector->text(t, &tt);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
|
@ -409,13 +478,13 @@ mp_obj_t VECTOR_rotate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
|
|||
|
||||
if(!MP_OBJ_IS_TYPE(args[ARG_polygon].u_obj, &POLYGON_type)) mp_raise_TypeError("rotate: polygon required");
|
||||
|
||||
_POLYGON_obj_t *poly = MP_OBJ_TO_PTR2(args[ARG_polygon].u_obj, _POLYGON_obj_t);
|
||||
_PATH_obj_t *poly = MP_OBJ_TO_PTR2(args[ARG_polygon].u_obj, _PATH_obj_t);
|
||||
|
||||
Point origin = Point(args[ARG_origin_x].u_int, args[ARG_origin_y].u_int);
|
||||
pp_point_t origin = {(PP_COORD_TYPE)args[ARG_origin_x].u_int, (PP_COORD_TYPE)args[ARG_origin_y].u_int};
|
||||
|
||||
float angle = mp_obj_get_float(args[ARG_angle].u_obj);
|
||||
|
||||
self->vector->rotate(poly->contour, origin, angle);
|
||||
self->vector->rotate(&poly->path, origin, angle);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
@ -436,11 +505,11 @@ mp_obj_t VECTOR_translate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
|
|||
|
||||
if(!MP_OBJ_IS_TYPE(args[ARG_polygon].u_obj, &POLYGON_type)) mp_raise_TypeError("rotate: polygon required");
|
||||
|
||||
_POLYGON_obj_t *poly = MP_OBJ_TO_PTR2(args[ARG_polygon].u_obj, _POLYGON_obj_t);
|
||||
_PATH_obj_t *poly = MP_OBJ_TO_PTR2(args[ARG_polygon].u_obj, _PATH_obj_t);
|
||||
|
||||
Point translate = Point(args[ARG_x].u_int, args[ARG_y].u_int);
|
||||
pp_point_t translate = {(PP_COORD_TYPE)args[ARG_x].u_int, (PP_COORD_TYPE)args[ARG_y].u_int};
|
||||
|
||||
self->vector->translate(poly->contour, translate);
|
||||
self->vector->translate(&poly->path, translate);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
@ -452,18 +521,23 @@ mp_obj_t VECTOR_draw(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
|||
|
||||
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(pos_args[0], _VECTOR_obj_t);
|
||||
|
||||
std::vector<pretty_poly::contour_t<picovector_point_type>> contours;
|
||||
pp_poly_t group;
|
||||
group.count = num_polygons;
|
||||
group.paths = (pp_path_t *)malloc(sizeof(pp_path_t) * num_polygons);
|
||||
|
||||
for(auto i = 0u; i < num_polygons; i++) {
|
||||
mp_obj_t poly_obj = polygons[i];
|
||||
|
||||
if(!MP_OBJ_IS_TYPE(poly_obj, &POLYGON_type)) mp_raise_TypeError("draw: Polygon required.");
|
||||
|
||||
_POLYGON_obj_t *poly = MP_OBJ_TO_PTR2(poly_obj, _POLYGON_obj_t);
|
||||
contours.emplace_back(poly->contour.points, poly->contour.count);
|
||||
_PATH_obj_t *poly = MP_OBJ_TO_PTR2(poly_obj, _PATH_obj_t);
|
||||
group.paths[i].points = poly->path.points;
|
||||
group.paths[i].count = poly->path.count;
|
||||
}
|
||||
|
||||
self->vector->polygon(contours);
|
||||
self->vector->draw(&group);
|
||||
|
||||
free(group.paths);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ extern mp_obj_t RECTANGLE_make_new(const mp_obj_type_t *type, size_t n_args, siz
|
|||
extern void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t POLYGON_centroid(mp_obj_t self_in);
|
||||
extern mp_obj_t POLYGON_bounds(mp_obj_t self_in);
|
||||
extern mp_obj_t POLYGON_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf);
|
||||
extern mp_obj_t PATH_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf);
|
||||
|
||||
extern mp_obj_t POLYGON__del__(mp_obj_t self_in);
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue