kopia lustrzana https://github.com/pimoroni/pimoroni-pico
PicoGraphics: Switch to string_view.
MicroPython's GET_STR_DATA_LEN macro returns a const byte array and len, which std::string would copy into heap. Using string_view lets us wrap the existing const values.pull/711/head
rodzic
b74b371d2b
commit
375df60ff3
|
@ -21,7 +21,7 @@ namespace bitmap {
|
|||
return font->widths[char_index] * scale;
|
||||
}
|
||||
|
||||
int32_t measure_text(const font_t *font, const std::string &t, const uint8_t scale, const uint8_t letter_spacing) {
|
||||
int32_t measure_text(const font_t *font, const std::string_view &t, const uint8_t scale, const uint8_t letter_spacing) {
|
||||
int32_t text_width = 0;
|
||||
unicode_sorta::codepage_t codepage = unicode_sorta::PAGE_195;
|
||||
for(auto c : t) {
|
||||
|
@ -119,7 +119,7 @@ namespace bitmap {
|
|||
}
|
||||
}
|
||||
|
||||
void text(const font_t *font, rect_func rectangle, const std::string &t, const int32_t x, const int32_t y, const int32_t wrap, const uint8_t scale, const uint8_t letter_spacing) {
|
||||
void text(const font_t *font, rect_func rectangle, const std::string_view &t, const int32_t x, const int32_t y, const int32_t wrap, const uint8_t scale, const uint8_t letter_spacing) {
|
||||
uint32_t co = 0, lo = 0; // character and line (if wrapping) offset
|
||||
unicode_sorta::codepage_t codepage = unicode_sorta::PAGE_195;
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <cstdint>
|
||||
#include "common/unicode_sorta.hpp"
|
||||
|
||||
|
@ -19,8 +20,8 @@ namespace bitmap {
|
|||
typedef std::function<void(int32_t x, int32_t y, int32_t w, int32_t h)> rect_func;
|
||||
|
||||
int32_t measure_character(const font_t *font, const char c, const uint8_t scale, unicode_sorta::codepage_t codepage = unicode_sorta::PAGE_195);
|
||||
int32_t measure_text(const font_t *font, const std::string &t, const uint8_t scale = 2, const uint8_t letter_spacing = 1);
|
||||
int32_t measure_text(const font_t *font, const std::string_view &t, const uint8_t scale = 2, const uint8_t letter_spacing = 1);
|
||||
|
||||
void character(const font_t *font, rect_func rectangle, const char c, const int32_t x, const int32_t y, const uint8_t scale = 2, unicode_sorta::codepage_t codepage = unicode_sorta::PAGE_195);
|
||||
void text(const font_t *font, rect_func rectangle, const std::string &t, const int32_t x, const int32_t y, const int32_t wrap, const uint8_t scale = 2, const uint8_t letter_spacing = 1);
|
||||
void text(const font_t *font, rect_func rectangle, const std::string_view &t, const int32_t x, const int32_t y, const int32_t wrap, const uint8_t scale = 2, const uint8_t letter_spacing = 1);
|
||||
}
|
|
@ -41,7 +41,7 @@ namespace hershey {
|
|||
return gd->width * s;
|
||||
}
|
||||
|
||||
int32_t measure_text(const font_t* font, std::string message, float s) {
|
||||
int32_t measure_text(const font_t* font, std::string_view message, float s) {
|
||||
int32_t width = 0;
|
||||
for(auto &c : message) {
|
||||
width += measure_glyph(font, c, s);
|
||||
|
@ -93,7 +93,7 @@ namespace hershey {
|
|||
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) {
|
||||
void text(const font_t* font, line_func line, std::string_view message, int32_t x, int32_t y, float s, float a) {
|
||||
int32_t cx = x;
|
||||
int32_t cy = y;
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ namespace hershey {
|
|||
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 measure_text(const font_t* font, std::string_view 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);
|
||||
void text(const font_t* font, line_func line, std::string_view message, int32_t x, int32_t y, float s, float a);
|
||||
}
|
|
@ -35,7 +35,7 @@ namespace pimoroni {
|
|||
this->hershey_font = font;
|
||||
}
|
||||
|
||||
void PicoGraphics::set_font(std::string name){
|
||||
void PicoGraphics::set_font(std::string_view name){
|
||||
if (name == "bitmap6") {
|
||||
set_font(&font6);
|
||||
} else if (name == "bitmap8") {
|
||||
|
@ -44,8 +44,8 @@ namespace pimoroni {
|
|||
set_font(&font14_outline);
|
||||
} else {
|
||||
// check that font exists and assign it
|
||||
if(hershey::fonts.find(name) != hershey::fonts.end()) {
|
||||
set_font(hershey::fonts[name]);
|
||||
if(hershey::fonts.find((std::string)name) != hershey::fonts.end()) {
|
||||
set_font(hershey::fonts[(std::string)name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ namespace pimoroni {
|
|||
}
|
||||
}
|
||||
|
||||
void PicoGraphics::text(const std::string &t, const Point &p, int32_t wrap, float s, float a, uint8_t letter_spacing) {
|
||||
void PicoGraphics::text(const std::string_view &t, const Point &p, int32_t wrap, float s, float a, uint8_t letter_spacing) {
|
||||
if (bitmap_font) {
|
||||
bitmap::text(bitmap_font, [this](int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
rectangle(Rect(x, y, w, h));
|
||||
|
@ -166,7 +166,7 @@ namespace pimoroni {
|
|||
}
|
||||
}
|
||||
|
||||
int32_t PicoGraphics::measure_text(const std::string &t, float s, uint8_t letter_spacing) {
|
||||
int32_t PicoGraphics::measure_text(const std::string_view &t, float s, uint8_t letter_spacing) {
|
||||
if (bitmap_font) return bitmap::measure_text(bitmap_font, t, std::max(1.0f, s), letter_spacing);
|
||||
if (hershey_font) return hershey::measure_text(hershey_font, t, s);
|
||||
return 0;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
|
@ -268,7 +269,7 @@ namespace pimoroni {
|
|||
|
||||
void set_font(const bitmap::font_t *font);
|
||||
void set_font(const hershey::font_t *font);
|
||||
void set_font(std::string font);
|
||||
void set_font(std::string_view name);
|
||||
|
||||
void set_dimensions(int width, int height);
|
||||
void set_framebuffer(void *frame_buffer);
|
||||
|
@ -285,8 +286,8 @@ namespace pimoroni {
|
|||
void rectangle(const Rect &r);
|
||||
void circle(const Point &p, int32_t r);
|
||||
void character(const char c, const Point &p, float s = 2.0f, float a = 0.0f);
|
||||
void text(const std::string &t, const Point &p, int32_t wrap, float s = 2.0f, float a = 0.0f, uint8_t letter_spacing = 1);
|
||||
int32_t measure_text(const std::string &t, float s = 2.0f, uint8_t letter_spacing = 1);
|
||||
void text(const std::string_view &t, const Point &p, int32_t wrap, float s = 2.0f, float a = 0.0f, uint8_t letter_spacing = 1);
|
||||
int32_t measure_text(const std::string_view &t, float s = 2.0f, uint8_t letter_spacing = 1);
|
||||
void polygon(const std::vector<Point> &points);
|
||||
void triangle(Point p1, Point p2, Point p3);
|
||||
void line(Point p1, Point p2);
|
||||
|
|
|
@ -3,6 +3,9 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/../../)
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,4 +4,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
|
||||
enable_ulab()
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
|
||||
enable_ulab()
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -4,4 +4,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(micropython-common)
|
||||
|
|
|
@ -4,6 +4,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
|
|||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../")
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../")
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Essential
|
||||
include(pimoroni_i2c/micropython)
|
||||
include(pimoroni_bus/micropython)
|
||||
|
|
|
@ -23,10 +23,10 @@ extern "C" {
|
|||
#include "py/reader.h"
|
||||
#include "extmod/vfs.h"
|
||||
|
||||
std::string mp_obj_to_string_r(const mp_obj_t &obj) {
|
||||
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);
|
||||
return (const char*)str;
|
||||
return std::string_view((const char*)str, str_len);
|
||||
}
|
||||
mp_raise_TypeError("can't convert object to str implicitly");
|
||||
}
|
||||
|
@ -935,7 +935,7 @@ mp_obj_t ModPicoGraphics_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t
|
|||
|
||||
GET_STR_DATA_LEN(text_obj, str, str_len);
|
||||
|
||||
std::string t((const char*)str);
|
||||
const std::string_view t((const char*)str, str_len);
|
||||
|
||||
int x = args[ARG_x].u_int;
|
||||
int y = args[ARG_y].u_int;
|
||||
|
@ -969,7 +969,7 @@ mp_obj_t ModPicoGraphics_measure_text(size_t n_args, const mp_obj_t *pos_args, m
|
|||
|
||||
GET_STR_DATA_LEN(text_obj, str, str_len);
|
||||
|
||||
std::string t((const char*)str);
|
||||
const std::string_view t((const char*)str, str_len);
|
||||
|
||||
float scale = args[ARG_scale].u_obj == mp_const_none ? 2.0f : mp_obj_get_float(args[ARG_scale].u_obj);
|
||||
int letter_spacing = args[ARG_spacing].u_int;
|
||||
|
|
Ładowanie…
Reference in New Issue