summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2024-10-24 01:15:12 +0200
committerLouis Burda <quent.burda@gmail.com>2024-10-24 01:15:44 +0200
commite607f78b4e86ad6d4e6c90f8b5136517d5c4ceb4 (patch)
treec97118539e224d37bd888a4def3bf262a0796f1c
parentf05fff6f3fce40a786454fe3c38e53d43bab7bfe (diff)
downloadxnote-e607f78b4e86ad6d4e6c90f8b5136517d5c4ceb4.tar.gz
xnote-e607f78b4e86ad6d4e6c90f8b5136517d5c4ceb4.zip
Add support for line breaks in text
-rw-r--r--xnote.c115
1 files changed, 69 insertions, 46 deletions
diff --git a/xnote.c b/xnote.c
index b8c1a44..47acfa4 100644
--- a/xnote.c
+++ b/xnote.c
@@ -139,6 +139,7 @@ pop_event(void)
enum event event = events[event_count];
switch (event) {
case EVENT_START_LINE:
+ case EVENT_START_RECT:
line_count -= 1;
free(lines[line_count].points);
break;
@@ -162,6 +163,7 @@ undo(void)
switch (pop_event()) {
case EVENT_START_LINE:
case EVENT_START_NOTE:
+ case EVENT_START_RECT:
case EVENT_START:
break;
default:
@@ -257,10 +259,34 @@ mouse_press_cb(GLFWwindow *window, int button, int action, int mods)
}
static void
+key_char_cb(GLFWwindow *window, unsigned int codepoint)
+{
+ if (codepoint < 10 || codepoint >= 128)
+ return;
+
+ if (last_event() != EVENT_START_NOTE) {
+ struct note *note = append(&notes, &note_count, &note_cap, sizeof(struct note));
+ note->text = NULL;
+ note->align = CENTER;
+ note->len = note->cap = 0;
+ note->x = canvas_x + cursor_x;
+ note->y = canvas_y + cursor_y;
+ push_event(EVENT_START_NOTE);
+ }
+
+ struct note *note = &notes[note_count - 1];
+ *(char *)append(&note->text, &note->len, &note->cap, 1) = codepoint;
+}
+
+static void
key_press_cb(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
const char *keysym = glfwGetKeyName(key, scancode);
+ if (key == GLFW_KEY_ENTER) {
+ key_char_cb(window, '\n');
+ return;
+ }
if ((mods & GLFW_MOD_CONTROL) && keysym && !strcmp(keysym, "z")) {
if (event_count) undo();
} else if ((mods & GLFW_MOD_CONTROL)
@@ -294,6 +320,8 @@ key_press_cb(GLFWwindow *window, int key, int scancode, int action, int mods)
free(notes[i].text);
note_count = 0;
event_count = 0;
+ } else if ((mods & GLFW_MOD_CONTROL) && keysym && !strcmp(keysym, "q")) {
+ exit(0);
} else if (key == GLFW_KEY_BACKSPACE && last_event() == EVENT_START_NOTE) {
if (note_count) {
struct note *note = &notes[note_count - 1];
@@ -309,25 +337,6 @@ key_press_cb(GLFWwindow *window, int key, int scancode, int action, int mods)
}
static void
-key_char_cb(GLFWwindow *window, unsigned int codepoint)
-{
- if (codepoint <= 10 || codepoint >= 128)
- return;
-
- if (last_event() != EVENT_START_NOTE) {
- struct note *note = append(&notes, &note_count, &note_cap, sizeof(struct note));
- note->text = NULL;
- note->len = note->cap = 0;
- note->x = canvas_x + cursor_x;
- note->y = canvas_y + cursor_y;
- push_event(EVENT_START_NOTE);
- }
-
- struct note *note = &notes[note_count - 1];
- *(char *)append(&note->text, &note->len, &note->cap, 1) = codepoint;
-}
-
-static void
draw_texture(int texture, double x, double y)
{
glBindTexture(GL_TEXTURE_2D, texture);
@@ -339,36 +348,50 @@ draw_text(const char *text, enum align align, size_t len, GLint x, GLint y)
{
if (len == 0) return;
- double width = glyph_offset[text[0]].x;
- for (size_t i = 0; i < len; i++) {
- width += glyph_advance[text[i]].x;
- }
- width -= glyph_advance[text[len-1]].x;
- width += glyph_size[text[len-1]].x;
+ GLint sx = x;
+ size_t i = 0;
+ while (i < len) {
+ size_t k = i;
- if (align == CENTER) {
- x -= width / 2;
- } else if (align == RIGHT) {
- x -= width;
- }
+ double width = glyph_offset[text[0]].x;
+ for (; k < len; k++) {
+ if (text[k] == '\n') break;
+ width += glyph_advance[text[k]].x;
+ }
- glEnable(GL_TEXTURE_2D);
- for (size_t i = 0; i < len; i++) {
- GLint gx = x + glyph_offset[text[i]].x;
- GLint gy = y - glyph_offset[text[i]].y;
- GLint sx = glyph_size[text[i]].x;
- GLint sy = glyph_size[text[i]].y;
- glBindTexture(GL_TEXTURE_2D, glyphs[text[i]]);
- glBegin(GL_QUADS);
- glTexCoord2d(0, 1); glVertex2i(gx, gy + sy);
- glTexCoord2d(1, 1); glVertex2i(gx + sx, gy + sy);
- glTexCoord2d(1, 0); glVertex2i(gx + sx, gy);
- glTexCoord2d(0, 0); glVertex2i(gx, gy);
- glEnd();
- x += glyph_advance[text[i]].x;
- y += glyph_advance[text[i]].y;
+ if (k > i) {
+ width -= glyph_advance[text[k-1]].x;
+ width += glyph_size[text[k-1]].x;
+
+ x = sx;
+ if (align == CENTER) {
+ x -= width / 2;
+ } else if (align == RIGHT) {
+ x -= width;
+ }
+
+ glEnable(GL_TEXTURE_2D);
+ for (k = i; k < len; k++) {
+ if (text[k] == '\n') break;
+ GLint gx = x + glyph_offset[text[k]].x;
+ GLint gy = y - glyph_offset[text[k]].y;
+ GLint sx = glyph_size[text[k]].x;
+ GLint sy = glyph_size[text[k]].y;
+ glBindTexture(GL_TEXTURE_2D, glyphs[text[k]]);
+ glBegin(GL_QUADS);
+ glTexCoord2d(0, 1); glVertex2i(gx, gy + sy);
+ glTexCoord2d(1, 1); glVertex2i(gx + sx, gy + sy);
+ glTexCoord2d(1, 0); glVertex2i(gx + sx, gy);
+ glTexCoord2d(0, 0); glVertex2i(gx, gy);
+ glEnd();
+ x += glyph_advance[text[k]].x;
+ }
+ glDisable(GL_TEXTURE_2D);
+ }
+
+ y += glyph_size['0'].y * 1.4;
+ i = k + 1;
}
- glDisable(GL_TEXTURE_2D);
}
int