Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed May 15, 2020
1 parent b104d76 commit 395d178
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 98 deletions.
28 changes: 14 additions & 14 deletions src/lgfx/lgfx_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ namespace lgfx
template<typename T> void drawXBitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, const T& color ) { draw_xbitmap(x, y, bitmap, w, h, _write_conv.convert(color)); }
template<typename T> void drawXBitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, const T& fgcolor, const T& bgcolor) { draw_xbitmap(x, y, bitmap, w, h, _write_conv.convert(fgcolor), _write_conv.convert(bgcolor)); }

void draw_bitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, uint32_t fg_rawcolor, uint32_t bg_rawcolor = ~0)
void draw_bitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, uint32_t fg_rawcolor, uint32_t bg_rawcolor = ~0u)
{
if (w < 1 || h < 1) return;
setRawColor(fg_rawcolor);
Expand All @@ -813,18 +813,18 @@ namespace lgfx
if (fg != (bool)(byte & 0x80) || (++i >= w)) break;
byte <<= 1;
}
if ((ip != i) && (fg || bg_rawcolor != ~0)) {
if ((ip != i) && (fg || bg_rawcolor != ~0u)) {
writeFastHLine(x + ip, y + j, i - ip);
}
fg = !fg;
if (bg_rawcolor != ~0) setRawColor(fg ? fg_rawcolor : bg_rawcolor);
if (bg_rawcolor != ~0u) setRawColor(fg ? fg_rawcolor : bg_rawcolor);
} while (i < w);
bitmap += byteWidth;
} while (++j < h);
endWrite();
}

void draw_xbitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, uint32_t fg_rawcolor, uint32_t bg_rawcolor = ~0)
void draw_xbitmap(int32_t x, int32_t y, const uint8_t *bitmap, int32_t w, int32_t h, uint32_t fg_rawcolor, uint32_t bg_rawcolor = ~0u)
{
if (w < 1 || h < 1) return;
setRawColor(fg_rawcolor);
Expand All @@ -843,11 +843,11 @@ namespace lgfx
if (fg != (bool)(byte & 0x01) || (++i >= w)) break;
byte >>= 1;
}
if ((ip != i) && (fg || bg_rawcolor != ~0)) {
if ((ip != i) && (fg || bg_rawcolor != ~0u)) {
writeFastHLine(x + ip, y + j, i - ip);
}
fg = !fg;
if (bg_rawcolor != ~0) setRawColor(fg ? fg_rawcolor : bg_rawcolor);
if (bg_rawcolor != ~0u) setRawColor(fg ? fg_rawcolor : bg_rawcolor);
} while (i < w);
bitmap += byteWidth;
} while (++j < h);
Expand Down Expand Up @@ -960,18 +960,18 @@ namespace lgfx
push_image(x, y, w, h, &p);
}

void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t* data, uint32_t transp = ~0)
void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t* data, uint32_t transp = ~0u)
{
pixelcopy_t p(data, _write_conv.depth, rgb565_2Byte, _palette_count, nullptr, transp == ~0 ? ~0 : _write_conv.convert((uint16_t)transp));
pixelcopy_t p(data, _write_conv.depth, rgb565_2Byte, _palette_count, nullptr, transp == ~0u ? ~0u : _write_conv.convert((uint16_t)transp));
if (_swapBytes && !_palette_count && _write_conv.depth >= 8) {
p.no_convert = false;
p.fp_copy = pixelcopy_t::get_fp_normalcopy<rgb565_t>(_write_conv.depth);
}
push_image(x, y, w, h, &p);
}
void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const void* data, uint32_t transp = ~0)
void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const void* data, uint32_t transp = ~0u)
{
pixelcopy_t p(data, _write_conv.depth, rgb888_3Byte, _palette_count, nullptr, transp == ~0 ? ~0 : _write_conv.convert(transp));
pixelcopy_t p(data, _write_conv.depth, rgb888_3Byte, _palette_count, nullptr, transp == ~0u ? ~0u : _write_conv.convert(transp));
if (_swapBytes && !_palette_count && _write_conv.depth >= 8) {
p.no_convert = false;
p.fp_copy = pixelcopy_t::get_fp_normalcopy<rgb888_t>(_write_conv.depth);
Expand All @@ -984,18 +984,18 @@ namespace lgfx
template<typename T> void pushImageDMA( int32_t x, int32_t y, int32_t w, int32_t h, const void* data , const uint8_t bits, const T* palette) { pixelcopy_t p(data, _write_conv.depth, (color_depth_t)bits, _palette_count, palette ); push_image(x, y, w, h, &p, true); }
template<typename T> void pushImageDMA( int32_t x, int32_t y, int32_t w, int32_t h, const void* data, uint32_t transparent, const uint8_t bits, const T* palette) { pixelcopy_t p(data, _write_conv.depth, (color_depth_t)bits, _palette_count, palette, transparent ); push_image(x, y, w, h, &p, true); }

void pushImageDMA(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t* data, uint32_t transp = ~0)
void pushImageDMA(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t* data, uint32_t transp = ~0u)
{
pixelcopy_t p(data, _write_conv.depth, rgb565_2Byte, _palette_count, nullptr, transp == ~0 ? ~0 : _write_conv.convert((uint16_t)transp));
pixelcopy_t p(data, _write_conv.depth, rgb565_2Byte, _palette_count, nullptr, transp == ~0u? ~0u : _write_conv.convert((uint16_t)transp));
if (_swapBytes && !_palette_count && _write_conv.depth >= 8) {
p.no_convert = false;
p.fp_copy = pixelcopy_t::get_fp_normalcopy<rgb565_t>(_write_conv.depth);
}
push_image(x, y, w, h, &p, true);
}
void pushImageDMA(int32_t x, int32_t y, int32_t w, int32_t h, const void* data, uint32_t transp = ~0)
void pushImageDMA(int32_t x, int32_t y, int32_t w, int32_t h, const void* data, uint32_t transp = ~0u)
{
pixelcopy_t p(data, _write_conv.depth, rgb888_3Byte, _palette_count, nullptr, transp == ~0 ? ~0 : _write_conv.convert(transp));
pixelcopy_t p(data, _write_conv.depth, rgb888_3Byte, _palette_count, nullptr, transp == ~0u ? ~0u : _write_conv.convert(transp));
if (_swapBytes && !_palette_count && _write_conv.depth >= 8) {
p.no_convert = false;
p.fp_copy = pixelcopy_t::get_fp_normalcopy<rgb888_t>(_write_conv.depth);
Expand Down
1 change: 1 addition & 0 deletions src/lgfx/lgfx_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace lgfx
__attribute__ ((always_inline)) inline static uint32_t convert_rgb332_to_rgb888( uint32_t c) { return ((((c>>5)*0x49) >> 1)<<8 | ((c&0x1C)*0x49)>>3)<<8 | ((c&3)*0x55); }
__attribute__ ((always_inline)) inline static uint32_t convert_rgb888_to_rgb565( uint32_t c) { return (c>>19) << 11 | (((uint16_t)c)>>10)<<5 | ((uint8_t)c) >> 3; }

__attribute__((__used__))
static uint32_t convert_rgb565_to_rgb888( uint32_t c) { return ((((c>>11)*0x21)>>2)<<8 | ((((c>>5)&0x3F)*0x41)>>4))<<8 | (((c&0x1F)*0x21)>>2); }
static uint32_t convert_rgb888_to_bgr888( uint32_t c) { return getSwap24(c); }
static uint32_t convert_rgb888_to_bgr666( uint32_t c) { return ((c>>2) & 0x3F) << 16 | ((c >> 10) & 0x3F) << 8 | ((c>>18)&0x3F); }
Expand Down
151 changes: 86 additions & 65 deletions src/lgfx/lgfx_font_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Original Source:
#include <cmath>
#include <stdarg.h>

#if defined (ARDUINO)
#include <Print.h>
#endif

namespace lgfx
{
enum attribute_t
Expand Down Expand Up @@ -128,6 +132,11 @@ namespace lgfx
}
return true;
}
if (uniCode == 0x20) {
metrics->width = metrics->x_advance = metrics->y_advance * 2 / 7;
metrics->x_offset = 0;
return true;
}
return false;
}

Expand Down Expand Up @@ -166,11 +175,11 @@ namespace lgfx
gxAdvance = (uint8_t*)heap_alloc_psram( gCount ); // xAdvance - to move x cursor
gdX = (int8_t*)heap_alloc_psram( gCount ); // offset for bitmap left edge relative to cursor X

if (gBitmap ) gBitmap = (uint32_t*)heap_alloc( gCount * 4); // seek pointer to glyph bitmap in the file
if (gUnicode ) gUnicode = (uint16_t*)heap_alloc( gCount * 2); // Unicode 16 bit Basic Multilingual Plane (0-FFFF)
if (gWidth ) gWidth = (uint8_t*)heap_alloc( gCount ); // Width of glyph
if (gxAdvance) gxAdvance = (uint8_t*)heap_alloc( gCount ); // xAdvance - to move x cursor
if (gdX ) gdX = (int8_t*)heap_alloc( gCount ); // offset for bitmap left edge relative to cursor X
if (nullptr == gBitmap ) gBitmap = (uint32_t*)heap_alloc( gCount * 4); // seek pointer to glyph bitmap in the file
if (nullptr == gUnicode ) gUnicode = (uint16_t*)heap_alloc( gCount * 2); // Unicode 16 bit Basic Multilingual Plane (0-FFFF)
if (nullptr == gWidth ) gWidth = (uint8_t*)heap_alloc( gCount ); // Width of glyph
if (nullptr == gxAdvance) gxAdvance = (uint8_t*)heap_alloc( gCount ); // xAdvance - to move x cursor
if (nullptr == gdX ) gdX = (int8_t*)heap_alloc( gCount ); // offset for bitmap left edge relative to cursor X

if (!gUnicode
|| !gBitmap
Expand Down Expand Up @@ -198,12 +207,12 @@ namespace lgfx
uint16_t height = __builtin_bswap32(buffer[1]); // Height of glyph
if ((unicode > 0xFF) || ((unicode > 0x20) && (unicode < 0xA0) && (unicode != 0x7F))) {
int16_t dY = (int16_t)__builtin_bswap32(buffer[4]); // y delta from baseline
//ESP_LOGI("LGFX", "unicode:%x dY:%d", unicode, dY);
if (maxAscent < dY) {
//Serial.printf("LGFX:unicode:%x dY:%d\r\n", unicode, dY);
if (maxAscent < dY && unicode != 0x3000) {
maxAscent = dY;
}
if (maxDescent < (height - dY)) {
//ESP_LOGI("LGFX", "maxDescent:%d", maxDescent);
if (maxDescent < (height - dY) && unicode != 0x3000) {
//Serial.printf("LGFX:maxDescent:%d\r\n", maxDescent);
maxDescent = height - dY;
}
}
Expand All @@ -214,15 +223,19 @@ namespace lgfx

yAdvance = maxAscent + maxDescent;

//ESP_LOGI("LGFX", "maxDescent:%d", maxDescent);
//Serial.printf("LGFX:maxDescent:%d\r\n", maxDescent);
return true;
}
};

//----------------------------------------------------------------------------

template <class Base>
class LGFX_Font_Support : public Base {
class LGFX_Font_Support : public Base
#if defined (ARDUINO)
, public Print
#endif
{
public:
virtual ~LGFX_Font_Support() { unloadFont(); }

Expand Down Expand Up @@ -444,11 +457,13 @@ namespace lgfx
}


#if defined (ARDUINO) && defined (FS_H)
#if defined (ARDUINO)
#if defined (FS_H) || defined (__SEEED_FS__)
void loadFont(const char *path, fs::FS &fs) {
_font_file.setFS(fs);
loadFont(path);
}
#endif
#endif

void loadFont(const uint8_t* array) {
Expand All @@ -471,13 +486,16 @@ namespace lgfx
this->prepareTmpTransaction(&_font_file);
_font_file.preRead();

bool result = _font_file.open(path, "rb");
bool result = _font_file.open(path, "r");
if (!result) {
std::string filename = "/";
if (path[0] == '/') filename = path;
else filename += path;
filename += ".vlw";
result = _font_file.open(filename.c_str(), "rb");
int len = strlen(path);
if (memcmp(&path[len - 4], ".vlw", 4)) {
filename += ".vlw";
}
result = _font_file.open(filename.c_str(), "r");
}
auto font = new VLWfont();
this->_dynamic_font = font;
Expand Down Expand Up @@ -536,6 +554,7 @@ namespace lgfx
// print & text support
//----------------------------------------------------------------------------
// Arduino Print.h compatible
#if !defined (ARDUINO)
size_t print(const char str[]) { return write(str); }
size_t print(char c) { return write(c); }
size_t print(int n, int base = 10) { return print((long)n, base); }
Expand Down Expand Up @@ -567,12 +586,10 @@ namespace lgfx
size_t println(unsigned long n, int base = 10) { size_t t = print(n,base); return println() + t; }
size_t println(double n, int digits= 2) { size_t t = print(n, digits); return println() + t; }

#if defined (ARDUINO)
size_t print(const String &s) { return write(s.c_str(), s.length()); }
size_t print(const __FlashStringHelper *s) { return print(reinterpret_cast<const char *>(s)); }
size_t println(const String &s) { size_t t = print(s); return println() + t; }
size_t println(const __FlashStringHelper *s) { size_t t = print(s); return println() + t; }
#endif

size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)))
{
Expand Down Expand Up @@ -603,6 +620,7 @@ namespace lgfx

size_t write(const char* str) { return (!str) ? 0 : write((const uint8_t*)str, strlen(str)); }
size_t write(const char *buf, size_t size) { return write((const uint8_t *) buf, size); }
#endif
size_t write(const uint8_t *buf, size_t size) { size_t n = 0; this->startWrite(); while (size--) { n += write(*buf++); } this->endWrite(); return n; }
size_t write(uint8_t utf8)
{
Expand Down Expand Up @@ -1263,33 +1281,35 @@ namespace lgfx
{
auto me = (LGFX_Font_Support*)lgfxbase;
auto font = (const VLWfont*)me->_font;
auto file = font->_fontData;

uint32_t buffer[6] = {0};
uint16_t gNum = 0;
if (!font->getUnicodeIndex(code, &gNum)) {

if (code == 0x20) {
gNum = 0xFFFF;
buffer[2] = __builtin_bswap32(font->spaceWidth);
} else if (!font->getUnicodeIndex(code, &gNum)) {
return 0;
} else {
file->preRead();
file->seek(28 + gNum * 28);
file->read((uint8_t*)buffer, 24);
file->seek(font->gBitmap[gNum]);
}

auto file = font->_fontData;

file->preRead();

file->seek(28 + gNum * 28); // headerPtr
uint32_t buffer[6];
file->read((uint8_t*)buffer, 24);
uint32_t h = __builtin_bswap32(buffer[0]); // Height of glyph
uint32_t w = __builtin_bswap32(buffer[1]); // Width of glyph
uint32_t xAdvance = __builtin_bswap32(buffer[2]) * style->size_x; // xAdvance - to move x cursor
int32_t h = __builtin_bswap32(buffer[0]); // Height of glyph
int32_t w = __builtin_bswap32(buffer[1]); // Width of glyph
int32_t xAdvance = __builtin_bswap32(buffer[2]) * style->size_x; // xAdvance - to move x cursor
int32_t xoffset = (int32_t)((int8_t)__builtin_bswap32(buffer[4])) * style->size_x; // x delta from cursor
int32_t dY = (int16_t)__builtin_bswap32(buffer[3]); // y delta from baseline
int32_t yoffset = ((int32_t)font->maxAscent - dY) * (int32_t)style->size_y;

uint8_t pbuffer[w * h];
uint8_t* pixel = pbuffer;

file->seek(font->gBitmap[gNum]); // headerPtr
file->read(pixel, w * h);

file->postRead();
if (gNum != 0xFFFF) {
file->read(pixel, w * h);
file->postRead();
}

me->startWrite();

Expand All @@ -1303,16 +1323,16 @@ namespace lgfx
}
me->_filled_x = right;

y += yoffset;
x += xoffset;
y += yoffset;
int32_t l = 0;
int32_t bx = x;
int32_t bw = w * style->size_x;
int32_t clip_left = me->_clip_l;
if (x < clip_left) { l = -((x - clip_left) / style->size_x); bw += (x - clip_left); bx = clip_left; }
int32_t clip_right = me->_clip_r + 1;
if (bw > clip_right - bx) bw = clip_right - bx;
if (bw > 0 && (y <= me->_clip_b) && (me->_clip_t < (y + h * style->size_y))) {
if (bw >= 0 && (y <= me->_clip_b) && (me->_clip_t < (int32_t)(y + h * style->size_y))) {
int32_t fore_r = ((style->fore_rgb888>>16)&0xFF);
int32_t fore_g = ((style->fore_rgb888>> 8)&0xFF);
int32_t fore_b = ((style->fore_rgb888) &0xFF);
Expand All @@ -1329,39 +1349,40 @@ namespace lgfx
me->writeFillRect(left, y + h * style->size_y, right - left, tmp);
}

int32_t back_r = ((style->back_rgb888>>16)&0xFF);
int32_t back_g = ((style->back_rgb888>> 8)&0xFF);
int32_t back_b = ((style->back_rgb888) &0xFF);
int32_t r = (clip_right - x + style->size_x - 1) / style->size_x;
if (r > w) r = w;
do {
if (right > left) {
me->setRawColor(colortbl[0]);
me->writeFillRect(left, y, right - left, style->size_y);
}
int32_t i = l;
if (l < r) {
int32_t back_r = ((style->back_rgb888>>16)&0xFF);
int32_t back_g = ((style->back_rgb888>> 8)&0xFF);
int32_t back_b = ((style->back_rgb888) &0xFF);
do {
while (pixel[i] != 0xFF) {
if (pixel[i] != 0) {
int32_t p = 1 + (uint32_t)pixel[i];
me->setColor(color888( ( fore_r * p + back_r * (257 - p)) >> 8
, ( fore_g * p + back_g * (257 - p)) >> 8
, ( fore_b * p + back_b * (257 - p)) >> 8 ));
me->writeFillRect(i * style->size_x + x, y, style->size_x, style->size_y);
}
if (++i == r) break;
if (right > left) {
me->setRawColor(colortbl[0]);
me->writeFillRect(left, y, right - left, style->size_y);
}
if (i == r) break;
int32_t dl = 1;
while (i + dl != r && pixel[i + dl] == 0xFF) { ++dl; }
me->setRawColor(colortbl[1]);
me->writeFillRect(x + i * style->size_x, y, dl * style->size_x, style->size_y);
i += dl;
} while (i != r);
pixel += w;
y += style->size_y;
} while (--h);

int32_t i = l;
do {
while (pixel[i] != 0xFF) {
if (pixel[i] != 0) {
int32_t p = 1 + (uint32_t)pixel[i];
me->setColor(color888( ( fore_r * p + back_r * (257 - p)) >> 8
, ( fore_g * p + back_g * (257 - p)) >> 8
, ( fore_b * p + back_b * (257 - p)) >> 8 ));
me->writeFillRect(i * style->size_x + x, y, style->size_x, style->size_y);
}
if (++i == r) break;
}
if (i == r) break;
int32_t dl = 1;
while (i + dl != r && pixel[i + dl] == 0xFF) { ++dl; }
me->setRawColor(colortbl[1]);
me->writeFillRect(x + i * style->size_x, y, dl * style->size_x, style->size_y);
i += dl;
} while (i != r);
pixel += w;
y += style->size_y;
} while (--h);
}
} else { // alpha blend mode

int32_t xshift = (bx - x) % style->size_x;
Expand Down
Loading

0 comments on commit 395d178

Please sign in to comment.