Skip to content
Myr

stb_textedit: better support for baseline at bottom

Before adoption: this commit came in with the history the project adopted, so no landing records it.

stb_textedit: end-of-line handling places cursor before newline stb_truetype: avoid compiler warning due to local vars hiding local vars readme: add list of one-file libs

authored by Sean Barrett · committed by Sean Barrett · parent ff116a4 · Browse files · 3 files+64 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+64 added−20 removed

  • other:+64 added−20 removed

3 files+64 added−20 removed

modified:tools/README.footer.md+34 added-1 removed
modified:stb_textedit.h+18 added-7 removed
1// stb_textedit.h - v1.6 - public domain - Sean Barrett1// stb_textedit.h - v1.7 - public domain - Sean Barrett2// Development of this library was sponsored by RAD Game Tools3//4// This C header file implements the guts of a multi-line text-editing5// widget; you implement display, word-wrapping, and low-level string
24 unmodified lines
30//31//32// VERSION HISTORY33//34// 1.7 (2015-09-13) change y range handling in case baseline is non-035// 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove36// 1.5 (2014-09-10) add support for secondary keys for OS X37// 1.4 (2014-08-17) fix signed/unsigned warnings38// 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary
6 unmodified lines
45//46// ADDITIONAL CONTRIBUTORS47//48// Ulf Winklemann: move-by-word in 1.148// Scott Graham: mouse selection bugfix in 1.349// Fabian Giesen: secondary key inputs in 1.550// Martins Mozeiko: STB_TEXTEDIT_memmove51//52// Bugfixes:53// Scott Graham54// Daniel Keller55// Omar Cornut56//57// USAGE58//59// This file behaves differently depending on what symbols you define60// before including it.
323 unmodified lines
384 int n = STB_TEXTEDIT_STRINGLEN(str);385 float base_y = 0, prev_x;386 int i=0, k;387383 if (y < 0)384 return 0;385388 r.x0 = r.x1 = 0;389 r.ymin = r.ymax = 0;390 r.num_chars = 0;391
2 unmodified lines
394 STB_TEXTEDIT_LAYOUTROW(&r, str, i);395 if (r.num_chars <= 0)396 return n;397398 if (i==0 && y < base_y + r.ymin)399 return 0;400401 if (y < base_y + r.ymax)402 break;403404 i += r.num_chars;
585 unmodified lines
990 StbFindState find;991 stb_textedit_clamp(str, state);992 stb_textedit_move_to_first(state);993 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);989 state->cursor = find.first_char + find.length;994995 state->has_preferred_x = 0;996 state->cursor = find.first_char + find.length;997 if (find.length > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) == STB_TEXTEDIT_NEWLINE)998 --state->cursor;999 break;1000 }10011002#ifdef STB_TEXTEDIT_K_LINESTART2
16 unmodified lines
1019 StbFindState find;1020 stb_textedit_clamp(str, state);1021 stb_textedit_prep_selection_at_cursor(state);1022 stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);1015 state->cursor = state->select_end = find.first_char + find.length;1023 state->has_preferred_x = 0;1024 state->cursor = find.first_char + find.length;1025 if (find.length > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) == STB_TEXTEDIT_NEWLINE)1026 --state->cursor;1027 state->select_end = state->cursor;1028 break;1029 }10301031// @TODO:
270 unmodified lines
modified:stb_truetype.h+12 added-12 removed
1917 unmodified lines
1918 float x0 = e->fx;1919 float dx = e->fdx;1920 float xb = x0 + dx;1921 float x_top, x_bottom;1922 float y0,y1;1922 float sy0,sy1;1923 float dy = e->fdy;1924 STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);19251926 // compute endpoints of line segment clipped to this scanline (if the1927 // line segment starts on this scanline. x0 is the intersection of the1928 // line with y_top, but that may be off the line segment.1929 if (e->sy > y_top) {1930 x_top = x0 + dx * (e->sy - y_top);1931 y0 = e->sy;1931 sy0 = e->sy;1932 } else {1933 x_top = x0;1934 y0 = y_top;1934 sy0 = y_top;1935 }1936 if (e->ey < y_bottom) {1937 x_bottom = x0 + dx * (e->ey - y_top);1938 y1 = e->ey;1938 sy1 = e->ey;1939 } else {1940 x_bottom = xb;1941 y1 = y_bottom;1941 sy1 = y_bottom;1942 }19431944 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {1945 // from here on, we don't have to range check x values
1 unmodified line
1947 if ((int) x_top == (int) x_bottom) {1948 float height;1949 // simple case, only spans one pixel1950 int x = (int) x_top;1951 height = y1 - y0;1951 height = sy1 - sy0;1952 STBTT_assert(x >= 0 && x < len);1953 scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height;1954 scanline_fill[x] += e->direction * height; // everything right of this pixel is filled1955 } else {
2 unmodified lines
1958 // covers 2+ pixels1959 if (x_top > x_bottom) {1960 // flip scanline vertically; signed area is the same1961 float t;1962 y0 = y_bottom - (y0 - y_top);1963 y1 = y_bottom - (y1 - y_top);1964 t = y0, y0 = y1, y1 = t;1962 sy0 = y_bottom - (sy0 - y_top);1963 sy1 = y_bottom - (sy1 - y_top);1964 t = sy0, sy0 = sy1, sy1 = t;1965 t = x_bottom, x_bottom = x_top, x_top = t;1966 dx = -dx;1967 dy = -dy;1968 t = x0, x0 = xb, xb = t;
5 unmodified lines
1974 y_crossing = (x1+1 - x0) * dy + y_top;19751976 sign = e->direction;1977 // area of the rectangle covered from y0..y_crossing1978 area = sign * (y_crossing-y0);1978 area = sign * (y_crossing-sy0);1979 // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing)1980 scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);19811982 step = sign * dy;
4 unmodified lines
1987 y_crossing += dy * (x2 - (x1+1));19881989 STBTT_assert(fabs(area) <= 1.01f);19901991 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (y1-y_crossing);1991 scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);19921993 scanline_fill[x2] += sign * (y1-y0);1993 scanline_fill[x2] += sign * (sy1-sy0);1994 }1995 } else {1996 // if edge goes outside of box we're drawing, we require1997 // clipping logic. since this does not match the intended use
1238 unmodified lines