Browse Source

Implementation and test for textsize()

[#9]
James Bowman 6 years ago
parent
commit
a3276e49c8
3 changed files with 27 additions and 1 deletions
  1. 9 0
      GD2.cpp
  2. 2 0
      GD2.h
  3. 16 1
      apitest.ino

+ 9 - 0
GD2.cpp

@@ -1366,6 +1366,15 @@ void GDClass::safeload(const char *filename)
   }
 }
 
+void GDClass::textsize(int &w, int &h, int font, const char *s)
+{
+  uint32_t font_addr = rd32(FONT_ROOT) + 148 * (font - 16);
+  w = 0;
+  while (*s)
+    w += GD.rd(font_addr + *s++);
+  h = GD.rd(font_addr + 140);
+}
+
 #define REG_SCREENSHOT_EN    (ft8xx_model ? 0x302010UL : 0x102410UL) // Set to enable screenshot mode
 #define REG_SCREENSHOT_Y     (ft8xx_model ? 0x302014UL : 0x102414UL) // Y line register
 #define REG_SCREENSHOT_START (ft8xx_model ? 0x302018UL : 0x102418UL) // Screenshot start trigger

+ 2 - 0
GD2.h

@@ -596,6 +596,7 @@ public:
   byte load(const char *filename, void (*progress)(long, long) = NULL);
   void safeload(const char *filename);
   void alert(const char *message);
+  void textsize(int &w, int &h, int font, const char *s);
 
   sdcard SD;
 
@@ -1002,6 +1003,7 @@ typedef struct {
 #define REG_VSIZE             (ft8xx_model ? 0x302048UL : 0x102444UL)
 #define REG_VSYNC0            (ft8xx_model ? 0x30204cUL : 0x102448UL)
 #define REG_VSYNC1            (ft8xx_model ? 0x302050UL : 0x10244cUL)
+#define FONT_ROOT             (ft8xx_model ? 0x2ffffcUL : 0x0ffffcUL)
 
 #define REG_MEDIAFIFO_READ    0x309014
 #define REG_MEDIAFIFO_WRITE   0x309018

+ 16 - 1
apitest.ino

@@ -72,6 +72,20 @@ static int randrange()
   return (total == (N * 100)) ? 0 : 1;
 }
 
+static int textsize()
+{
+  int w, h;
+
+  GD.textsize(w, h, 31, "");
+  if ((w != 0) || (h != 49))
+    return 1;
+  GD.textsize(w, h, 26, "hello world");
+  if ((w != 68) || (h != 16))
+    return 1;
+
+  return 0;
+}
+
 #define DOTEST(NAME) \
   do { \
   GD.cmd_text(110, y, 20, OPT_RIGHTX, #NAME); \
@@ -84,7 +98,7 @@ static int randrange()
 
 void setup()
 {
-  Serial.begin(115200); // JCB
+  Serial.begin(1000000); // JCB
   GD.begin(0);
   GD.cmd_memset(0, 0x40000UL, 0);
   GD.Clear();
@@ -94,6 +108,7 @@ void setup()
   DOTEST(setfont);
   DOTEST(atan2);
   DOTEST(randrange);
+  DOTEST(textsize);
 
   GD.swap();
 }