Add example rendering tool

This commit is contained in:
Chris van Marle 2022-01-21 16:30:56 +01:00
parent 30b2e1b99a
commit a804accc81
7 changed files with 291 additions and 0 deletions

25
render/Arduino.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __ARDUINO_H__
#define __ARDUINO_H__
// Minimal Arduino.h implementation
// to support compilation of Adafruit_GFX
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h> // for strlen
#define PROGMEM
typedef const char *__FlashStringHelper;
// Empty String class
// Doesn't really work, but isn't used either
// Only defined to make Adafruit_GFX compile
class String {
public:
inline unsigned int length(void) const { return 0; }
const char *c_str() const { return ""; }
};
#endif

20
render/Makefile Normal file
View File

@ -0,0 +1,20 @@
CPP=g++
CPPFLAGS=-Wall -g -I. -DARDUINO=100
OBJS=Adafruit_GFX.o RenderGFX.o render.o
LIBS=-lgd -lpng
TARGET=render
GFX_PATH=../
all: $(TARGET)
render: $(OBJS)
$(CPP) -o $@ $^ $(LIBS)
%.o: %.c
$(CPP) -c -o $@ $< $(CPPFLAGS)
Adafruit_GFX.o: $(GFX_PATH)/Adafruit_GFX.cpp
$(CPP) -c -o $@ $< $(CPPFLAGS)
clean:
rm -rf $(OBJS) $(TARGET)

28
render/Print.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __PRINT_H__
#define __PRINT_H__
#include <cstdio>
class Print {
public:
virtual size_t write(uint8_t) = 0;
virtual size_t write(const uint8_t *buffer, size_t size) {
size_t n = 0;
while (size--) {
if (write(*buffer++))
n++;
else
break;
}
return n;
}
size_t print(const char *str) { return write(str, strlen(str)); }
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}
};
#endif

15
render/README.md Normal file
View File

@ -0,0 +1,15 @@
# Adafruit GFX Examples Rendering
A little tool to render examples of all the fonts included in the Adafruit GFX library.
## Compile
```
$ make
```
## Run
```
$ ./render
```

22
render/RenderGFX.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "RenderGFX.h"
#include <cstdio>
RenderGFX::RenderGFX(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
im = gdImageCreate(w, h);
}
void RenderGFX::drawPixel(int16_t x, int16_t y, uint16_t color) {
gdImageSetPixel(im, x, y, color);
}
uint16_t RenderGFX::color(uint8_t r, uint8_t g, uint8_t b) {
return gdImageColorAllocate(im, r, g, b);
}
void RenderGFX::save(const char *name) {
FILE *pngout = fopen(name, "wb");
gdImagePng(im, pngout);
fclose(pngout);
}

22
render/RenderGFX.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __RENDERGFX_H
#define __RENDERGFX_H
#include "../Adafruit_GFX.h"
#include "Arduino.h"
#include <gd.h>
class RenderGFX : public Adafruit_GFX {
public:
RenderGFX(uint16_t w, uint16_t h);
virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
virtual uint16_t color(uint8_t r, uint8_t g, uint8_t b);
virtual void save(const char *name);
protected:
gdImagePtr im;
};
#endif

159
render/render.cpp Normal file
View File

@ -0,0 +1,159 @@
#include "Arduino.h"
#include "RenderGFX.h"
#include <cstdio>
#include <stdint.h>
// Update includes:
// for FILE in ../Fonts/*.h; do printf '#include "%s"\n' "$FILE"; done
#include "../Fonts/FreeMono12pt7b.h"
#include "../Fonts/FreeMono18pt7b.h"
#include "../Fonts/FreeMono24pt7b.h"
#include "../Fonts/FreeMono9pt7b.h"
#include "../Fonts/FreeMonoBold12pt7b.h"
#include "../Fonts/FreeMonoBold18pt7b.h"
#include "../Fonts/FreeMonoBold24pt7b.h"
#include "../Fonts/FreeMonoBold9pt7b.h"
#include "../Fonts/FreeMonoBoldOblique12pt7b.h"
#include "../Fonts/FreeMonoBoldOblique18pt7b.h"
#include "../Fonts/FreeMonoBoldOblique24pt7b.h"
#include "../Fonts/FreeMonoBoldOblique9pt7b.h"
#include "../Fonts/FreeMonoOblique12pt7b.h"
#include "../Fonts/FreeMonoOblique18pt7b.h"
#include "../Fonts/FreeMonoOblique24pt7b.h"
#include "../Fonts/FreeMonoOblique9pt7b.h"
#include "../Fonts/FreeSans12pt7b.h"
#include "../Fonts/FreeSans18pt7b.h"
#include "../Fonts/FreeSans24pt7b.h"
#include "../Fonts/FreeSans9pt7b.h"
#include "../Fonts/FreeSansBold12pt7b.h"
#include "../Fonts/FreeSansBold18pt7b.h"
#include "../Fonts/FreeSansBold24pt7b.h"
#include "../Fonts/FreeSansBold9pt7b.h"
#include "../Fonts/FreeSansBoldOblique12pt7b.h"
#include "../Fonts/FreeSansBoldOblique18pt7b.h"
#include "../Fonts/FreeSansBoldOblique24pt7b.h"
#include "../Fonts/FreeSansBoldOblique9pt7b.h"
#include "../Fonts/FreeSansOblique12pt7b.h"
#include "../Fonts/FreeSansOblique18pt7b.h"
#include "../Fonts/FreeSansOblique24pt7b.h"
#include "../Fonts/FreeSansOblique9pt7b.h"
#include "../Fonts/FreeSerif12pt7b.h"
#include "../Fonts/FreeSerif18pt7b.h"
#include "../Fonts/FreeSerif24pt7b.h"
#include "../Fonts/FreeSerif9pt7b.h"
#include "../Fonts/FreeSerifBold12pt7b.h"
#include "../Fonts/FreeSerifBold18pt7b.h"
#include "../Fonts/FreeSerifBold24pt7b.h"
#include "../Fonts/FreeSerifBold9pt7b.h"
#include "../Fonts/FreeSerifBoldItalic12pt7b.h"
#include "../Fonts/FreeSerifBoldItalic18pt7b.h"
#include "../Fonts/FreeSerifBoldItalic24pt7b.h"
#include "../Fonts/FreeSerifBoldItalic9pt7b.h"
#include "../Fonts/FreeSerifItalic12pt7b.h"
#include "../Fonts/FreeSerifItalic18pt7b.h"
#include "../Fonts/FreeSerifItalic24pt7b.h"
#include "../Fonts/FreeSerifItalic9pt7b.h"
#include "../Fonts/Org_01.h"
#include "../Fonts/Picopixel.h"
#include "../Fonts/Tiny3x3a2pt7b.h"
#include "../Fonts/TomThumb.h"
#include "../gfxfont.h"
#define SCALE 2
#define BORDER_SIZE 5 * SCALE
void render(const char *filename, GFXfont *font, bool correctBox = true) {
printf("%s\n", filename);
const char *text = "The Quick Brown Fox Jumps Over The Lazy Dog";
int16_t sizex, sizey;
uint16_t sizew, sizeh;
RenderGFX render(1000, 1000);
render.setTextWrap(false);
render.setTextSize(SCALE);
render.setFont(font);
render.getTextBounds(text, 0, 0, &sizex, &sizey, &sizew, &sizeh);
printf("Size: %d, %d, %d, %d\n", sizex, sizey, sizew, sizeh);
render = RenderGFX(sizew + 2 * BORDER_SIZE, sizeh + 2 * BORDER_SIZE);
int black = render.color(0, 0, 0);
int white = render.color(255, 255, 255);
int lgrey = render.color(200, 200, 200);
int yoff = BORDER_SIZE - (correctBox ? sizey : 0);
render.fillScreen(white);
render.setFont(font);
render.setCursor(BORDER_SIZE, yoff);
render.setTextSize(SCALE);
render.setTextColor(black);
render.print(text);
render.drawRect(BORDER_SIZE, BORDER_SIZE, sizew, sizeh, lgrey);
render.save(filename);
}
void render(const char *filename, GFXfont font, bool correctBox = true) {
render(filename, &font, correctBox);
}
int main(int argc, char **argv) {
// Update render calls:
// for FILE in ../Fonts/*.h; do
// NAME=$(basename "$FILE" .h);
// printf ' render("%s.png", %s);\n' "$NAME" "$NAME";
// done
render("FreeMono12pt7b.png", FreeMono12pt7b);
render("FreeMono18pt7b.png", FreeMono18pt7b);
render("FreeMono24pt7b.png", FreeMono24pt7b);
render("FreeMono9pt7b.png", FreeMono9pt7b);
render("FreeMonoBold12pt7b.png", FreeMonoBold12pt7b);
render("FreeMonoBold18pt7b.png", FreeMonoBold18pt7b);
render("FreeMonoBold24pt7b.png", FreeMonoBold24pt7b);
render("FreeMonoBold9pt7b.png", FreeMonoBold9pt7b);
render("FreeMonoBoldOblique12pt7b.png", FreeMonoBoldOblique12pt7b);
render("FreeMonoBoldOblique18pt7b.png", FreeMonoBoldOblique18pt7b);
render("FreeMonoBoldOblique24pt7b.png", FreeMonoBoldOblique24pt7b);
render("FreeMonoBoldOblique9pt7b.png", FreeMonoBoldOblique9pt7b);
render("FreeMonoOblique12pt7b.png", FreeMonoOblique12pt7b);
render("FreeMonoOblique18pt7b.png", FreeMonoOblique18pt7b);
render("FreeMonoOblique24pt7b.png", FreeMonoOblique24pt7b);
render("FreeMonoOblique9pt7b.png", FreeMonoOblique9pt7b);
render("FreeSans12pt7b.png", FreeSans12pt7b);
render("FreeSans18pt7b.png", FreeSans18pt7b);
render("FreeSans24pt7b.png", FreeSans24pt7b);
render("FreeSans9pt7b.png", FreeSans9pt7b);
render("FreeSansBold12pt7b.png", FreeSansBold12pt7b);
render("FreeSansBold18pt7b.png", FreeSansBold18pt7b);
render("FreeSansBold24pt7b.png", FreeSansBold24pt7b);
render("FreeSansBold9pt7b.png", FreeSansBold9pt7b);
render("FreeSansBoldOblique12pt7b.png", FreeSansBoldOblique12pt7b);
render("FreeSansBoldOblique18pt7b.png", FreeSansBoldOblique18pt7b);
render("FreeSansBoldOblique24pt7b.png", FreeSansBoldOblique24pt7b);
render("FreeSansBoldOblique9pt7b.png", FreeSansBoldOblique9pt7b);
render("FreeSansOblique12pt7b.png", FreeSansOblique12pt7b);
render("FreeSansOblique18pt7b.png", FreeSansOblique18pt7b);
render("FreeSansOblique24pt7b.png", FreeSansOblique24pt7b);
render("FreeSansOblique9pt7b.png", FreeSansOblique9pt7b);
render("FreeSerif12pt7b.png", FreeSerif12pt7b);
render("FreeSerif18pt7b.png", FreeSerif18pt7b);
render("FreeSerif24pt7b.png", FreeSerif24pt7b);
render("FreeSerif9pt7b.png", FreeSerif9pt7b);
render("FreeSerifBold12pt7b.png", FreeSerifBold12pt7b);
render("FreeSerifBold18pt7b.png", FreeSerifBold18pt7b);
render("FreeSerifBold24pt7b.png", FreeSerifBold24pt7b);
render("FreeSerifBold9pt7b.png", FreeSerifBold9pt7b);
render("FreeSerifBoldItalic12pt7b.png", FreeSerifBoldItalic12pt7b);
render("FreeSerifBoldItalic18pt7b.png", FreeSerifBoldItalic18pt7b);
render("FreeSerifBoldItalic24pt7b.png", FreeSerifBoldItalic24pt7b);
render("FreeSerifBoldItalic9pt7b.png", FreeSerifBoldItalic9pt7b);
render("FreeSerifItalic12pt7b.png", FreeSerifItalic12pt7b);
render("FreeSerifItalic18pt7b.png", FreeSerifItalic18pt7b);
render("FreeSerifItalic24pt7b.png", FreeSerifItalic24pt7b);
render("FreeSerifItalic9pt7b.png", FreeSerifItalic9pt7b);
render("Org_01.png", Org_01);
render("Picopixel.png", Picopixel);
render("Tiny3x3a2pt7b.png", Tiny3x3a2pt7b);
render("TomThumb.png", TomThumb);
render("DefaultFont.png", NULL);
return 0;
}