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
Provenance
- Commit
4de75eb- Tree
d99c516- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
ff116a4
3 files changed+64 added−20 removed
modified:tools/README.footer.md+34 added-1 removed
68 unmodified lines697071727373747576777879808182838485868788899091929394959697989910010110210310410510610710810911011118 unmodified lines68 unmodified linestoo rare or if the size of implementation vs. apparent benefit is too low.
#### Are there other single-file public-domain libraries out there?
Yes. I'll put a list here when people remind me what they are.Yes. Here are some:
- [jo_gif.cpp](http://www.jonolick.com/home/gif-writer): tiny GIF writer (public domain)- [gif.h](https://github.com/ginsweater/gif-h): animated GIF writer (public domain)- [tiny_jpeg.h](https://github.com/serge-rgb/TinyJPEG/blob/master/tiny_jpeg.h): JPEG encoder (public domain)- [lodepng](http://lodev.org/lodepng/): PNG encoder/decoder (zlib license)
- [nanoSVG](https://github.com/memononen/nanosvg): 1-file SVG parser; 1-file SVG rasterizer (zlib license)- [tinyobjloader](https://github.com/syoyo/tinyobjloader): wavefront OBJ file loader (BSD license)
- [sdf.h](https://github.com/memononen/SDF): compute signed-distance field from antialiased image (MIT license)- [nv_voronoi.h](http://www.icculus.org/~mordred/nvlib/): find voronoi regions on lattice w/ integer inputs (public domain)- [nanoflann](https://github.com/jlblancoc/nanoflann): build KD trees for point clouds (BSD license)
- [DG_misc.h](https://github.com/DanielGibson/Snippets/): Daniel Gibson's stb.h-esque cross-platform helpers: path/file, strings (public domain)- [MakeID.h](http://www.humus.name/3D/MakeID.h): allocate/deallocate small integer IDs efficiently (public domain)- [utest](https://github.com/evolutional/utest): unit testing (MIT license)
There are some that have a source file and require a separate header file (which they maynot even supply). That's twice as many files, and we at nothings/stb cannot condonethis! But you might like them anyway:
- [picopng.cpp](http://lodev.org/lodepng/picopng.cpp): tiny PNG loader (zlib license)- [jpeg-compressor](https://github.com/richgel999/jpeg-compressor): 2-file jpeg compress, 2-file jpeg decompress (public domain)- [tinyexr](https://github.com/syoyo/tinyexr): EXR image read/write, uses miniz internally (BSD license)- [miniz.c](https://github.com/richgel999/miniz): zlib compression,decompression, zip file, png writing (public domain)- [Remotery](https://github.com/Celtoys/Remotery): CPU/GPU profiler Win/Mac/Linux, using web browser for viewer (Apache 2.0 license)- [Clipper](http://www.angusj.com/delphi/clipper.php): line & polygon clipping & offsetting (Boost license)- [json.h](https://github.com/sheredom/json.h): JSON parser (public domain)- [Zange](https://github.com/vurtun/zange/blob/master/json.c): another JSON parser (MIT license)
There is also this XML library, but if you're using XML, shame on you:
- [tinyxml2](https://github.com/leethomason/tinyxml2): XML (zlib license)
#### Do you have any advice on how to create my own single-file library?
Yes. https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
18 unmodified linesmodified: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 string24 unmodified lines30 //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 boundary6 unmodified lines45 //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 lines384 int n = STB_TEXTEDIT_STRINGLEN(str);385 float base_y = 0, prev_x;386 int i=0, k;387 383- if (y < 0)384- return 0;385-388 r.x0 = r.x1 = 0;389 r.ymin = r.ymax = 0;390 r.num_chars = 0;391 2 unmodified lines394 STB_TEXTEDIT_LAYOUTROW(&r, str, i);395 if (r.num_chars <= 0)396 return n;397 398+ if (i==0 && y < base_y + r.ymin)399+ return 0;400+401 if (y < base_y + r.ymax)402 break;403 404 i += r.num_chars;585 unmodified lines990 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;994+995 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 }1001 1002 #ifdef STB_TEXTEDIT_K_LINESTART216 unmodified lines1019 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 }1030 1031 // @TODO:270 unmodified linesmodified:stb_truetype.h+12 added-12 removed
1917 unmodified lines1918 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);1925 1926 // 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 }1943 1944 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 values1 unmodified line1947 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 lines1958 // 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 lines1974 y_crossing = (x1+1 - x0) * dy + y_top;1975 1976 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);1981 1982 step = sign * dy;4 unmodified lines1987 y_crossing += dy * (x2 - (x1+1));1988 1989 STBTT_assert(fabs(area) <= 1.01f);1990 1991- 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);1992 1993- 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 use1238 unmodified lines