kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Separate Hershey Fonts into their own library.
rodzic
391647b667
commit
24819734b7
|
@ -1,3 +1,4 @@
|
|||
add_subdirectory(hershey_fonts)
|
||||
add_subdirectory(breakout_dotmatrix)
|
||||
add_subdirectory(breakout_encoder)
|
||||
add_subdirectory(breakout_ioexpander)
|
||||
|
|
|
@ -8,5 +8,4 @@ target_sources(${LIB_NAME} INTERFACE
|
|||
target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(${LIB_NAME} INTERFACE pico_stdlib hardware_pwm uc8151)
|
||||
|
||||
target_link_libraries(${LIB_NAME} INTERFACE hershey_fonts pico_stdlib hardware_pwm uc8151)
|
||||
|
|
|
@ -322,103 +322,34 @@ namespace pimoroni {
|
|||
uc8151.update(blocking);
|
||||
}
|
||||
|
||||
const hershey_font_glyph_t* Badger2040::glyph_data(unsigned char c) {
|
||||
if(c < 32 || c > 127) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &_font->chars[c - 32];
|
||||
}
|
||||
|
||||
inline float deg2rad(float degrees) {
|
||||
return (degrees * M_PI) / 180.0f;
|
||||
const hershey::font_glyph_t* Badger2040::glyph_data(unsigned char c) {
|
||||
return hershey::glyph_data(_font, c);
|
||||
}
|
||||
|
||||
int32_t Badger2040::glyph(unsigned char c, int32_t x, int32_t y, float s, float a) {
|
||||
const hershey_font_glyph_t *gd = glyph_data(c);
|
||||
|
||||
// if glyph data not found (id too great) then skip
|
||||
if(!gd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a = deg2rad(a);
|
||||
float as = sin(a);
|
||||
float ac = cos(a);
|
||||
|
||||
const int8_t *pv = gd->vertices;
|
||||
int8_t cx = (*pv++) * s;
|
||||
int8_t cy = (*pv++) * s;
|
||||
bool pen_down = true;
|
||||
|
||||
for(uint32_t i = 1; i < gd->vertex_count; i++) {
|
||||
if(pv[0] == -128 && pv[1] == -128) {
|
||||
pen_down = false;
|
||||
pv += 2;
|
||||
}else{
|
||||
int8_t nx = (*pv++) * s;
|
||||
int8_t ny = (*pv++) * s;
|
||||
|
||||
int rcx = (cx * ac - cy * as) + 0.5f;
|
||||
int rcy = (cx * as + cy * ac) + 0.5f;
|
||||
|
||||
int rnx = (nx * ac - ny * as) + 0.5f;
|
||||
int rny = (nx * as + ny * ac) + 0.5f;
|
||||
|
||||
if(pen_down) {
|
||||
line(rcx + x, rcy + y, rnx + x, rny + y);
|
||||
}
|
||||
|
||||
cx = nx;
|
||||
cy = ny;
|
||||
pen_down = true;
|
||||
}
|
||||
}
|
||||
|
||||
return gd->width * s;
|
||||
return hershey::glyph(_font, [this](int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
|
||||
line(x1, y1, x2, y2);
|
||||
}, c, x, y, s, a);
|
||||
}
|
||||
|
||||
void Badger2040::text(std::string message, int32_t x, int32_t y, float s, float a) {
|
||||
int32_t cx = x;
|
||||
int32_t cy = y;
|
||||
|
||||
int32_t ox = 0;
|
||||
|
||||
float as = sin(deg2rad(a));
|
||||
float ac = cos(deg2rad(a));
|
||||
|
||||
for(auto &c : message) {
|
||||
int rcx = (ox * ac) + 0.5f;
|
||||
int rcy = (ox * as) + 0.5f;
|
||||
|
||||
ox += glyph(c, cx + rcx, cy + rcy, s, a);
|
||||
}
|
||||
hershey::text(_font, [this](int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
|
||||
line(x1, y1, x2, y2);
|
||||
}, message, x, y, s, a);
|
||||
}
|
||||
|
||||
int32_t Badger2040::measure_text(std::string message, float s) {
|
||||
int32_t width = 0;
|
||||
for(auto &c : message) {
|
||||
width += measure_glyph(c, s);
|
||||
}
|
||||
return width;
|
||||
return hershey::measure_text(_font, message, s);
|
||||
}
|
||||
|
||||
int32_t Badger2040::measure_glyph(unsigned char c, float s) {
|
||||
const hershey_font_glyph_t *gd = glyph_data(c);
|
||||
|
||||
// if glyph data not found (id too great) then skip
|
||||
if(!gd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gd->width * s;
|
||||
return hershey::measure_glyph(_font, c, s);
|
||||
}
|
||||
|
||||
|
||||
void Badger2040::font(std::string name) {
|
||||
// check that font exists and assign it
|
||||
if(fonts.find(name) != fonts.end()) {
|
||||
_font = fonts[name];
|
||||
if(hershey::fonts.find(name) != hershey::fonts.end()) {
|
||||
_font = hershey::fonts[name];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
|
||||
#include "drivers/uc8151/uc8151.hpp"
|
||||
|
||||
#include "fonts.hpp"
|
||||
#include "libraries/hershey_fonts/hershey_fonts.hpp"
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
class Badger2040 {
|
||||
protected:
|
||||
UC8151 uc8151;
|
||||
const hershey_font_t *_font = &futural;
|
||||
const hershey::font_t *_font = &hershey::futural;
|
||||
uint8_t _pen = 0;
|
||||
uint8_t _thickness = 1;
|
||||
uint32_t _button_states = 0;
|
||||
|
@ -62,7 +62,7 @@ namespace pimoroni {
|
|||
void image(const uint8_t *data, int stride, int sx, int sy, int dw, int dh, int dx, int dy);
|
||||
|
||||
// text (fonts: sans, sans_bold, gothic, cursive_bold, cursive, serif_italic, serif, serif_bold)
|
||||
const hershey_font_glyph_t* glyph_data(unsigned char c);
|
||||
const hershey::font_glyph_t* glyph_data(unsigned char c);
|
||||
|
||||
void text(std::string message, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f);
|
||||
int32_t glyph(unsigned char c, int32_t x, int32_t y, float s = 1.0f, float a = 0.0f);
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1 @@
|
|||
include(hershey_fonts.cmake)
|
|
@ -0,0 +1,9 @@
|
|||
set(LIB_NAME hershey_fonts)
|
||||
add_library(${LIB_NAME} INTERFACE)
|
||||
|
||||
target_sources(${LIB_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/${LIB_NAME}_data.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
|
@ -0,0 +1,107 @@
|
|||
#include "hershey_fonts.hpp"
|
||||
#include <cmath>
|
||||
|
||||
namespace hershey {
|
||||
std::map<std::string, const font_t*> fonts = {
|
||||
{ "sans", &futural },
|
||||
//{ "sans_bold", &futuram },
|
||||
{ "gothic", &gothgbt },
|
||||
//{ "cursive_bold", &scriptc },
|
||||
{ "cursive", &scripts },
|
||||
{ "serif_italic", ×i },
|
||||
{ "serif", ×r },
|
||||
//{ "serif_bold", ×rb }
|
||||
};
|
||||
|
||||
inline float deg2rad(float degrees) {
|
||||
return (degrees * M_PI) / 180.0f;
|
||||
}
|
||||
|
||||
const font_glyph_t* glyph_data(const font_t* font, unsigned char c) {
|
||||
if(c < 32 || c > 127) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &font->chars[c - 32];
|
||||
}
|
||||
|
||||
int32_t measure_glyph(const font_t* font, unsigned char c, float s) {
|
||||
const font_glyph_t *gd = glyph_data(font, c);
|
||||
|
||||
// if glyph data not found (id too great) then skip
|
||||
if(!gd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return gd->width * s;
|
||||
}
|
||||
|
||||
int32_t measure_text(const font_t* font, std::string message, float s) {
|
||||
int32_t width = 0;
|
||||
for(auto &c : message) {
|
||||
width += measure_glyph(font, c, s);
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
int32_t glyph(const font_t* font, line_func line, unsigned char c, int32_t x, int32_t y, float s, float a) {
|
||||
const font_glyph_t *gd = glyph_data(font, c);
|
||||
|
||||
// if glyph data not found (id too great) then skip
|
||||
if(!gd) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a = deg2rad(a);
|
||||
float as = sin(a);
|
||||
float ac = cos(a);
|
||||
|
||||
const int8_t *pv = gd->vertices;
|
||||
int8_t cx = (*pv++) * s;
|
||||
int8_t cy = (*pv++) * s;
|
||||
bool pen_down = true;
|
||||
|
||||
for(uint32_t i = 1; i < gd->vertex_count; i++) {
|
||||
if(pv[0] == -128 && pv[1] == -128) {
|
||||
pen_down = false;
|
||||
pv += 2;
|
||||
}else{
|
||||
int8_t nx = (*pv++) * s;
|
||||
int8_t ny = (*pv++) * s;
|
||||
|
||||
int rcx = (cx * ac - cy * as) + 0.5f;
|
||||
int rcy = (cx * as + cy * ac) + 0.5f;
|
||||
|
||||
int rnx = (nx * ac - ny * as) + 0.5f;
|
||||
int rny = (nx * as + ny * ac) + 0.5f;
|
||||
|
||||
if(pen_down) {
|
||||
line(rcx + x, rcy + y, rnx + x, rny + y);
|
||||
}
|
||||
|
||||
cx = nx;
|
||||
cy = ny;
|
||||
pen_down = true;
|
||||
}
|
||||
}
|
||||
|
||||
return gd->width * s;
|
||||
}
|
||||
|
||||
void text(const font_t* font, line_func line, std::string message, int32_t x, int32_t y, float s, float a) {
|
||||
int32_t cx = x;
|
||||
int32_t cy = y;
|
||||
|
||||
int32_t ox = 0;
|
||||
|
||||
float as = sin(deg2rad(a));
|
||||
float ac = cos(deg2rad(a));
|
||||
|
||||
for(auto &c : message) {
|
||||
int rcx = (ox * ac) + 0.5f;
|
||||
int rcy = (ox * as) + 0.5f;
|
||||
|
||||
ox += glyph(font, line, c, cx + rcx, cy + rcy, s, a);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
namespace hershey {
|
||||
struct font_glyph_t {
|
||||
uint32_t width; // character width
|
||||
uint32_t vertex_count; // number of vertices
|
||||
const int8_t *vertices; // vertex data (indices: even = x, odd = y)
|
||||
};
|
||||
|
||||
struct font_t {
|
||||
font_glyph_t chars[95];
|
||||
};
|
||||
|
||||
extern const int8_t futural_vertices[2442];
|
||||
extern const font_t futural;
|
||||
|
||||
extern const int8_t futuram_vertices[4802];
|
||||
extern const font_t futuram;
|
||||
|
||||
extern const int8_t gothgbt_vertices[9046];
|
||||
extern const font_t gothgbt;
|
||||
|
||||
extern const int8_t scriptc_vertices[5530];
|
||||
extern const font_t scriptc;
|
||||
|
||||
extern const int8_t scripts_vertices[4472];
|
||||
extern const font_t scripts;
|
||||
|
||||
extern const int8_t timesi_vertices[4848];
|
||||
extern const font_t timesi;
|
||||
|
||||
extern const int8_t timesr_vertices[4600];
|
||||
extern const font_t timesr;
|
||||
|
||||
extern const int8_t timesrb_vertices[7994];
|
||||
extern const font_t timesrb;
|
||||
|
||||
typedef std::function<void(int32_t x1, int32_t y1, int32_t x2, int32_t y2)> line_func;
|
||||
|
||||
extern std::map<std::string, const font_t*> fonts;
|
||||
|
||||
inline float deg2rad(float degrees);
|
||||
const font_glyph_t* glyph_data(const font_t* font, unsigned char c);
|
||||
int32_t measure_glyph(const font_t* font, unsigned char c, float s);
|
||||
int32_t measure_text(const font_t* font, std::string message, float s);
|
||||
int32_t glyph(const font_t* font, line_func line, unsigned char c, int32_t x, int32_t y, float s, float a);
|
||||
void text(const font_t* font, line_func line, std::string message, int32_t x, int32_t y, float s, float a);
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -29,6 +29,7 @@ include(breakout_bmp280/micropython)
|
|||
include(breakout_icp10125/micropython)
|
||||
include(breakout_scd41/micropython)
|
||||
|
||||
include(hershey_fonts/micropython)
|
||||
include(badger2040/micropython)
|
||||
include(micropython/examples/badger2040/micropython-builtins)
|
||||
include(plasma/micropython)
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
set(MOD_NAME hershey_fonts)
|
||||
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
|
||||
add_library(usermod_${MOD_NAME} INTERFACE)
|
||||
|
||||
target_sources(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/hershey_fonts/hershey_fonts.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/hershey_fonts/hershey_fonts_data.cpp
|
||||
)
|
||||
|
||||
target_include_directories(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../libraries/hershey_fonts
|
||||
)
|
||||
|
||||
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
|
Ładowanie…
Reference in New Issue