Skip to content

Commit

Permalink
Merge pull request #22 from lovyan03/lgfx
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
tobozo authored May 28, 2020
2 parents b8e9d12 + bf0697a commit 1a9a929
Show file tree
Hide file tree
Showing 19 changed files with 2,422 additions and 1,686 deletions.
20 changes: 18 additions & 2 deletions src/Fonts/lgfx_fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
#endif

namespace lgfx {
void BaseFont::getDefaultMetric(FontMetrics *metrics) const
{
metrics->width = width;
metrics->x_advance = width;
metrics->x_offset = 0;
metrics->height = height;
metrics->y_advance = height;
metrics->y_offset = 0;
metrics->baseline = baseline;
}
void BDFfont::getDefaultMetric(FontMetrics *metrics) const
{
BaseFont::getDefaultMetric(metrics);
metrics->y_advance = y_advance;
}

bool GLCDfont::updateFontMetric(FontMetrics*, std::uint16_t uniCode) const {
return uniCode < 256;
}
Expand Down Expand Up @@ -182,9 +198,9 @@ namespace fonts {
namespace lgfx
{
// deprecated array.
PROGMEM const IFont* fontdata [] = {
const IFont* fontdata [] = {
&fonts::Font0, // GLCD font (Font 0)
&fonts::Font0, // GLCD font (or GFX font)
&fonts::Font0, // Font 1 current unused
&fonts::Font2,
&fonts::Font0, // Font 3 current unused
&fonts::Font4,
Expand Down
156 changes: 53 additions & 103 deletions src/Fonts/lgfx_fonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@

namespace lgfx
{
enum textdatum_t : std::uint8_t
// 0:left 1:centre 2:right
// 0:top 4:middle 8:bottom 16:baseline
{ top_left = 0 // Top left (default)
, top_center = 1 // Top center
, top_centre = 1 // Top center
, top_right = 2 // Top right
, middle_left = 4 // Middle left
, middle_center = 5 // Middle center
, middle_centre = 5 // Middle center
, middle_right = 6 // Middle right
, bottom_left = 8 // Bottom left
, bottom_center = 9 // Bottom center
, bottom_centre = 9 // Bottom center
, bottom_right = 10 // Bottom right
, baseline_left = 16 // Baseline left (Line the 'A' character would sit on)
, baseline_center = 17 // Baseline center
, baseline_centre = 17 // Baseline center
, baseline_right = 18 // Baseline right
};

struct FontMetrics {
std::int16_t width;
std::int16_t x_advance;
Expand All @@ -36,16 +15,6 @@ namespace lgfx
std::int16_t baseline;
};

struct TextStyle {
std::uint32_t fore_rgb888 = 0xFFFFFFU;
std::uint32_t back_rgb888 = 0;
std::int_fast8_t size_x = 1;
std::int_fast8_t size_y = 1;
textdatum_t datum = textdatum_t::top_left;
bool utf8 = true;
bool cp437 = false;
};

struct IFont
{
enum font_type_t
Expand All @@ -62,16 +31,6 @@ namespace lgfx
virtual void getDefaultMetric(FontMetrics *metrics) const = 0;
virtual bool updateFontMetric(FontMetrics *metrics, std::uint16_t uniCode) const = 0;
virtual bool unloadFont(void) { return false; }
/*
struct param {
int32_t clip_left ;
int32_t clip_right ;
int32_t clip_top ;
int32_t clip_bottom;
int32_t filled_x ;
TextStyle* style ;
};
//*/
};

struct RunTimeFont : public IFont {
Expand All @@ -92,15 +51,7 @@ namespace lgfx
, height (height )
, baseline (baseline )
{}
void getDefaultMetric(FontMetrics *metrics) const override {
metrics->width = width;
metrics->x_advance = width;
metrics->x_offset = 0;
metrics->height = height;
metrics->y_advance = height;
metrics->y_offset = 0;
metrics->baseline = baseline;
}
void getDefaultMetric(FontMetrics *metrics) const override;
};

struct GLCDfont : public BaseFont {
Expand All @@ -126,15 +77,18 @@ namespace lgfx
const std::uint16_t *indextbl;
std::uint16_t indexsize;
std::uint8_t halfwidth;
std::uint8_t y_advance;
BDFfont() = default;
constexpr BDFfont(const std::uint8_t *chartbl, const std::uint16_t *indextbl, std::uint16_t indexsize, std::uint8_t width, std::uint8_t halfwidth, std::uint8_t height, std::uint8_t baseline)
constexpr BDFfont(const std::uint8_t *chartbl, const std::uint16_t *indextbl, std::uint16_t indexsize, std::uint8_t width, std::uint8_t halfwidth, std::uint8_t height, std::uint8_t baseline, std::uint8_t y_advance)
: BaseFont(chartbl, nullptr, width, height, baseline )
, indextbl(indextbl)
, indexsize(indexsize)
, halfwidth(halfwidth)
, y_advance(y_advance)
{}
constexpr font_type_t getType(void) const override { return ft_bdf; }

void getDefaultMetric(FontMetrics *metrics) const override;
bool updateFontMetric(FontMetrics *metrics, std::uint16_t uniCode) const override;
};

Expand All @@ -144,64 +98,60 @@ namespace lgfx
//----------------------------------------------------------------------------
// Adafruit GFX font

struct GFXglyph { // Data stored PER GLYPH
std::uint32_t bitmapOffset; // Pointer into GFXfont->bitmap
std::uint8_t width, height; // Bitmap dimensions in pixels
std::uint8_t xAdvance; // Distance to advance cursor (x axis)
std::int8_t xOffset, yOffset; // Dist from cursor pos to UL corner
};

struct GFXfont : public lgfx::IFont { // Data stored for FONT AS A WHOLE:
struct EncodeRange {
std::uint16_t start;
std::uint16_t end;
std::uint16_t base;
};

std::uint8_t *bitmap; // Glyph bitmaps, concatenated
GFXglyph *glyph; // Glyph array
std::uint16_t first, last; // ASCII extents
std::uint8_t yAdvance; // Newline distance (y axis)

std::uint16_t range_num; // Number of EncodeRange
EncodeRange *range; // Array ofEncodeRange

constexpr GFXfont ( std::uint8_t *bitmap
, GFXglyph *glyph
, std::uint16_t first
, std::uint16_t last
, std::uint8_t yAdvance
, std::uint16_t range_num = 0
, EncodeRange *range = nullptr
)
: bitmap (bitmap )
, glyph (glyph )
, first (first )
, last (last )
, yAdvance (yAdvance )
, range_num(range_num)
, range (range )
{}

GFXglyph* getGlyph(std::uint16_t uniCode) const;

constexpr font_type_t getType(void) const override { return font_type_t::ft_gfx; }

void getDefaultMetric(lgfx::FontMetrics *metrics) const;

bool updateFontMetric(lgfx::FontMetrics *metrics, std::uint16_t uniCode) const override;
};
struct EncodeRange {
std::uint16_t start;
std::uint16_t end;
std::uint16_t base;
};

struct GFXglyph { // Data stored PER GLYPH
std::uint32_t bitmapOffset; // Pointer into GFXfont->bitmap
std::uint8_t width, height; // Bitmap dimensions in pixels
std::uint8_t xAdvance; // Distance to advance cursor (x axis)
std::int8_t xOffset, yOffset; // Dist from cursor pos to UL corner
};

struct GFXfont : public lgfx::IFont { // Data stored for FONT AS A WHOLE:
std::uint8_t *bitmap; // Glyph bitmaps, concatenated
GFXglyph *glyph; // Glyph array
std::uint16_t first, last; // ASCII extents
std::uint8_t yAdvance; // Newline distance (y axis)

std::uint16_t range_num; // Number of EncodeRange
EncodeRange *range; // Array ofEncodeRange

constexpr GFXfont ( std::uint8_t *bitmap
, GFXglyph *glyph
, std::uint16_t first
, std::uint16_t last
, std::uint8_t yAdvance
, std::uint16_t range_num = 0
, EncodeRange *range = nullptr
)
: bitmap (bitmap )
, glyph (glyph )
, first (first )
, last (last )
, yAdvance (yAdvance )
, range_num(range_num)
, range (range )
{}

GFXglyph* getGlyph(std::uint16_t uniCode) const;

constexpr font_type_t getType(void) const override { return font_type_t::ft_gfx; }

void getDefaultMetric(lgfx::FontMetrics *metrics) const;

bool updateFontMetric(lgfx::FontMetrics *metrics, std::uint16_t uniCode) const override;
};


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

namespace fonts {
#ifdef __EFONT_FONT_DATA_H__
static constexpr lgfx::BDFfont efont = { (const std::uint8_t *)efontFontData, efontFontList, sizeof(efontFontList)>>1, 16, 8, 16, 14 };
#endif

#ifdef misakiUTF16FontData_h
static constexpr lgfx::BDFfont misaki = { (const std::uint8_t *)fdata, ftable, sizeof(ftable)>>1, 8, 4, 7, 6 };
static constexpr lgfx::BDFfont efont = { (const std::uint8_t *)efontFontData, efontFontList, sizeof(efontFontList)>>1, 16, 8, 16, 14, 16 };
#endif

extern const lgfx::GLCDfont Font0;
Expand Down
Loading

0 comments on commit 1a9a929

Please sign in to comment.