Skip to content

Commit

Permalink
Add somewhat glitchy but valid word wrapping. See issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-London committed Dec 26, 2022
1 parent a2d5821 commit 53562da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pwn_zero/include/pwnagotchi.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum PwnagotchiMode {
typedef struct {
/// Current face
enum PwnagotchiFace face;
char* faceStr;
// char* faceStr;
/// CH channel display at top left
char channel[PWNAGOTCHI_MAX_CHANNEL_LEN];
/// AP text shown at the top
Expand Down
48 changes: 45 additions & 3 deletions pwn_zero/src/pwnagotchi.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <furi.h>

Pwnagotchi* pwnagotchi_alloc() {
Pwnagotchi* pwn = malloc(sizeof(Pwnagotchi));

pwn->face = Grateful;
pwn->faceStr = GRATEFUL;
pwn->face = Cool;
pwn->friendFace = NoFace;

strncpy(pwn->channel, "*", 2);
Expand Down Expand Up @@ -246,7 +246,49 @@ void pwnagotchi_draw_mode(Pwnagotchi* pwn, Canvas* canvas) {

void pwnagotchi_draw_message(Pwnagotchi* pwn, Canvas* canvas) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, PWNAGOTCHI_MESSAGE_J, PWNAGOTCHI_MESSAGE_I, pwn->message);
int fontHeight = canvas_current_font_height(canvas);

// Apparently W is the widest character (USING a for a more average approach)
size_t charLength = canvas_string_width(canvas, "a");

size_t horizSpace = FLIPPER_SCREEN_WIDTH - PWNAGOTCHI_MESSAGE_J;
size_t charSpaces = floor(((double) horizSpace) / charLength);
size_t messagePixLen = canvas_string_width(canvas, pwn->message);
size_t maxLines = floor((PWNAGOTCHI_MESSAGE_I - PWNAGOTCHI_LINE2_END_I) / ((double) fontHeight));

size_t requiredLines = ceil(((double) messagePixLen) / horizSpace);

size_t charIndex = 0;
for (size_t i = 0; i < requiredLines && i < maxLines - 1; i++) {
// Allocate the line with room for two more characters (a space and then another char)
size_t allocSize = charSpaces + 2;
char* line = malloc(sizeof(char) * allocSize);

// Copy the allotted characters into line
memcpy(line, (pwn->message + charIndex), allocSize);

// Now loop backwards and cut it off at a space if we end with a letter
size_t backspaceCount = 0;
if (line[allocSize - 1] != ' ' && line[allocSize - 1] != '\0') {
for (int j = allocSize - 1; j >= 0; j--) {
if (line[j] == ' ') {
line[j] = '\0';
break;
}
backspaceCount++;

}
}

// Lets make sure if backspaceCount is too large that we cut the word instead of drawing off the screen
if (backspaceCount >= charSpaces) {backspaceCount = 0;}

canvas_draw_str(canvas, PWNAGOTCHI_MESSAGE_J, PWNAGOTCHI_MESSAGE_I + (i * fontHeight), line);

charIndex += (charSpaces - backspaceCount + 1);
free(line);
}

}

void pwnagotchi_free(Pwnagotchi* pwn) {
Expand Down

0 comments on commit 53562da

Please sign in to comment.