From 17e6e04c79ec0590e1e633af807aec6c6a177967 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Fri, 31 Jul 2026 06:55:25 -0400 Subject: [PATCH] Measure rendered text without drawing it akbasic needs the advance width of one character cell to build a terminal-style text surface, and the wrapped size to know where a string crosses the right margin. akgl_text_measure and akgl_text_measure_wrapped report both over TTF_GetStringSize and TTF_GetStringSizeWrapped; neither needs a renderer, so this half of the text subsystem is testable without the offscreen harness. A negative wrap length is refused with AKERR_OUTOFBOUNDS: SDL_ttf reads it as a very large unsigned width and quietly stops wrapping, which would return a measurement that is wrong rather than an error the caller can see. Also fixes akgl_text_loadfont, which checked name twice and passed an unchecked filepath to TTF_OpenFont (TODO item 39). The fixture font is a 10 KB monospaced ASCII subset of Liberation Mono, renamed because "Liberation" is a Reserved Font Name; see tests/assets/akgl_test_mono.LICENSE.txt. Monospaced so the suite can assert width("AAAA") == 4 * width("A") rather than hardcode glyph metrics. Co-Authored-By: Claude Opus 5 (1M context) --- CMakeLists.txt | 1 + TODO.md | 15 ++ include/akgl/text.h | 37 ++++ src/text.c | 45 +++- tests/assets/akgl_test_mono.LICENSE.txt | 121 +++++++++++ tests/assets/akgl_test_mono.ttf | Bin 0 -> 10160 bytes tests/text.c | 259 ++++++++++++++++++++++++ 7 files changed, 477 insertions(+), 1 deletion(-) create mode 100644 tests/assets/akgl_test_mono.LICENSE.txt create mode 100644 tests/assets/akgl_test_mono.ttf create mode 100644 tests/text.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dd7596..930ecbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,6 +165,7 @@ set(AKGL_TEST_SUITES registry sprite staticstring + text tilemap util version diff --git a/TODO.md b/TODO.md index 40e3c14..00bc591 100644 --- a/TODO.md +++ b/TODO.md @@ -352,6 +352,9 @@ overlap the existing **Defects** list are cross-referenced rather than repeated. `name` a second time; `filepath` is never checked and is passed straight to `TTF_OpenFont`. Copy-paste of line 18. + **Resolved** alongside the text measurement work; `tests/text.c` asserts a + NULL filepath reports `AKERR_NULLPOINTER`. + 40. **`akgl_path_relative` and `akgl_path_relative_from` disagree on the output parameter.** `akgl_path_relative` and `akgl_path_relative_root` take `akgl_String *dst`; `akgl_path_relative_from` takes `akgl_String **dst` @@ -654,6 +657,18 @@ a consumer is the wanted outcome. Each entry says what the BASIC verb needs, wha This is the only one of the four that blocks work already designed and waiting. **`akbasic` cannot render any output through `libakgl` until it lands.** + **Resolved.** `akgl_text_measure(font, text, w, h)` and + `akgl_text_measure_wrapped(font, text, wraplength, w, h)` are in + `include/akgl/text.h`, over `TTF_GetStringSize` and `TTF_GetStringSizeWrapped`. + Neither needs a renderer. A negative `wraplength` is refused with + `AKERR_OUTOFBOUNDS` rather than passed through, because SDL_ttf reads it as a + very large unsigned width and silently stops wrapping. `tests/text.c` covers + both against `tests/assets/akgl_test_mono.ttf`, a 10 KB monospaced ASCII + subset added for the purpose — being monospaced, it lets the suite assert + `width("AAAA") == 4 * width("A")` instead of hardcoding glyph metrics that + FreeType is free to round differently. Same change fixed item 39 below: + `akgl_text_loadfont` checked `name` twice and never checked `filepath`. + 2. **No immediate-mode drawing.** `include/akgl/draw.h` declares exactly one function, `akgl_draw_background(int w, int h)`, and `src/draw.c` is at 0% coverage. BASIC 7.0's graphics verbs are all immediate-mode plotting against the current screen: `DRAW` (line and diff --git a/include/akgl/text.h b/include/akgl/text.h index 4cdd4d6..f5bbd4a 100644 --- a/include/akgl/text.h +++ b/include/akgl/text.h @@ -6,7 +6,9 @@ #ifndef _TEXT_H_ #define _TEXT_H_ +#include #include +#include /** * @brief Text loadfont. @@ -31,5 +33,40 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_loadfont(char *name, char *filepath * @throws AKERR_NULLPOINTER When the corresponding validation or operation fails. */ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_rendertextat(TTF_Font *font, char *text, SDL_Color color, int wraplength, int x, int y); +/** + * @brief Report the size, in pixels, that @p text would occupy on one line. + * + * Nothing is drawn and no renderer is required. A caller building a character + * grid measures one cell with this -- the advance width of a single glyph in a + * monospaced font -- and derives the rest of the grid from it. + * + * @param font Font used to render the text. + * @param text UTF-8 text to measure. + * @param w Output destination populated with the rendered width in pixels. + * @param h Output destination populated with the rendered height in pixels. + * @return `NULL` on success, otherwise an error context owned by the caller. + * @throws AKERR_NULLPOINTER When the corresponding validation or operation fails. + * @throws AKGL_ERR_SDL When the corresponding validation or operation fails. + */ +akerr_ErrorContext AKERR_NOIGNORE *akgl_text_measure(TTF_Font *font, char *text, int *w, int *h); +/** + * @brief Report the size, in pixels, that @p text would occupy when wrapped. + * + * The companion to akgl_text_measure() for the wrapping case, matching the + * @p wraplength argument akgl_text_rendertextat() already takes: a string + * longer than @p wraplength reports the height of every line it breaks onto. + * A @p wraplength of zero wraps only on newlines in @p text. + * + * @param font Font used to render the text. + * @param text UTF-8 text to measure. + * @param wraplength Maximum rendered line width; zero wraps on newlines only. + * @param w Output destination populated with the rendered width in pixels. + * @param h Output destination populated with the rendered height in pixels. + * @return `NULL` on success, otherwise an error context owned by the caller. + * @throws AKERR_NULLPOINTER When the corresponding validation or operation fails. + * @throws AKERR_OUTOFBOUNDS When the corresponding validation or operation fails. + * @throws AKGL_ERR_SDL When the corresponding validation or operation fails. + */ +akerr_ErrorContext AKERR_NOIGNORE *akgl_text_measure_wrapped(TTF_Font *font, char *text, int wraplength, int *w, int *h); #endif // _TEXT_H_ diff --git a/src/text.c b/src/text.c index 4bf6dfe..275dadb 100644 --- a/src/text.c +++ b/src/text.c @@ -16,7 +16,7 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_loadfont(char *name, char *filepath PREPARE_ERROR(errctx); FAIL_ZERO_RETURN(errctx, name, AKERR_NULLPOINTER, "Null font name"); - FAIL_ZERO_RETURN(errctx, name, AKERR_NULLPOINTER, "Null filepath"); + FAIL_ZERO_RETURN(errctx, filepath, AKERR_NULLPOINTER, "Null filepath"); font = TTF_OpenFont(filepath, size); FAIL_ZERO_RETURN(errctx, font, AKGL_ERR_SDL, "%s", SDL_GetError()); FAIL_ZERO_RETURN( @@ -64,3 +64,46 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_text_rendertextat(TTF_Font *font, char * SDL_DestroySurface(textsurf); SUCCEED_RETURN(errctx); } + +akerr_ErrorContext AKERR_NOIGNORE *akgl_text_measure(TTF_Font *font, char *text, int *w, int *h) +{ + PREPARE_ERROR(errctx); + FAIL_ZERO_RETURN(errctx, font, AKERR_NULLPOINTER, "NULL font"); + FAIL_ZERO_RETURN(errctx, text, AKERR_NULLPOINTER, "NULL text string"); + FAIL_ZERO_RETURN(errctx, w, AKERR_NULLPOINTER, "NULL width destination"); + FAIL_ZERO_RETURN(errctx, h, AKERR_NULLPOINTER, "NULL height destination"); + // A zero length means "the string is null terminated", not "the empty + // string" -- an empty text measures 0 wide and one line high. + FAIL_ZERO_RETURN( + errctx, + TTF_GetStringSize(font, text, 0, w, h), + AKGL_ERR_SDL, + "%s", + SDL_GetError()); + SUCCEED_RETURN(errctx); +} + +akerr_ErrorContext AKERR_NOIGNORE *akgl_text_measure_wrapped(TTF_Font *font, char *text, int wraplength, int *w, int *h) +{ + PREPARE_ERROR(errctx); + FAIL_ZERO_RETURN(errctx, font, AKERR_NULLPOINTER, "NULL font"); + FAIL_ZERO_RETURN(errctx, text, AKERR_NULLPOINTER, "NULL text string"); + FAIL_ZERO_RETURN(errctx, w, AKERR_NULLPOINTER, "NULL width destination"); + FAIL_ZERO_RETURN(errctx, h, AKERR_NULLPOINTER, "NULL height destination"); + // SDL_ttf takes the wrap width as an int and reads a negative one as a + // very large unsigned width, which silently disables wrapping instead of + // reporting anything. Refuse it here rather than return a wrong measurement. + FAIL_NONZERO_RETURN( + errctx, + (wraplength < 0), + AKERR_OUTOFBOUNDS, + "Wrap length %d is negative", + wraplength); + FAIL_ZERO_RETURN( + errctx, + TTF_GetStringSizeWrapped(font, text, 0, wraplength, w, h), + AKGL_ERR_SDL, + "%s", + SDL_GetError()); + SUCCEED_RETURN(errctx); +} diff --git a/tests/assets/akgl_test_mono.LICENSE.txt b/tests/assets/akgl_test_mono.LICENSE.txt new file mode 100644 index 0000000..e7194a6 --- /dev/null +++ b/tests/assets/akgl_test_mono.LICENSE.txt @@ -0,0 +1,121 @@ +tests/assets/akgl_test_mono.ttf +================================ + +A subset of Liberation Mono Regular, cut down to printable ASCII (U+0020 to +U+007E) so the text suite has a font fixture that is 10 KB rather than 320 KB. +It is monospaced, which is what the measurement tests rely on: the width of an +N-character string is exactly N times the width of one character, in any font +size, so the assertions do not have to hardcode glyph metrics. + +Generated with: + + pyftsubset /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf \ + --unicodes=U+0020-007E --layout-features='' --no-hinting \ + --desubroutinize --name-IDs='*' --output-file=akgl_test_mono.ttf + +then renamed to "AKGL Test Mono" through the fontTools name table. The rename is +required, not cosmetic: "Liberation" is a Reserved Font Name under the license +below, and a modified copy may not carry it. + +Copyright (c) 2012 Red Hat, Inc. with Reserved Font Name Liberation. +Digitized data copyright (c) 2010 Google Corporation with Reserved Font Arimo, +Tinos and Cousine. + +Licensed under the SIL Open Font License, Version 1.1, reproduced in full below. + +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 + +PREAMBLE The goals of the Open Font License (OFL) are to stimulate +worldwide development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to provide +a free and open framework in which fonts may be shared and improved in +partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. +The fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + + + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. +This may include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components +as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting ? in part or in whole ? +any of the components of the Original Version, by changing formats or +by porting the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical writer +or other person who contributed to the Font Software. + + +PERMISSION & CONDITIONS + +Permission is hereby granted, free of charge, to any person obtaining a +copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components,in + Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, + redistributed and/or sold with any software, provided that each copy + contains the above copyright notice and this license. These can be + included either as stand-alone text files, human-readable headers or + in the appropriate machine-readable metadata fields within text or + binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font + Name(s) unless explicit written permission is granted by the + corresponding Copyright Holder. This restriction only applies to the + primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font + Software shall not be used to promote, endorse or advertise any + Modified Version, except to acknowledge the contribution(s) of the + Copyright Holder(s) and the Author(s) or with their explicit written + permission. + +5) The Font Software, modified or unmodified, in part or in whole, must + be distributed entirely under this license, and must not be distributed + under any other license. The requirement for fonts to remain under + this license does not apply to any document created using the Font + Software. + + + +TERMINATION +This license becomes null and void if any of the above conditions are not met. + + + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER +DEALINGS IN THE FONT SOFTWARE. diff --git a/tests/assets/akgl_test_mono.ttf b/tests/assets/akgl_test_mono.ttf new file mode 100644 index 0000000000000000000000000000000000000000..67dfff2b3a8d49e0ae8de4372f1b42a46ac3b394 GIT binary patch literal 10160 zcmbVy2Ut``*Z-WkcXt5+SztjdxJwZWqObz7H&C%*Y!r<&snVo~AR=Hz6s0H;D~bgr zibi8cB*s|4M5D?3#l#X5Q$%B8i3&S>XYMZP@Av$l|H9q9Gk4CJ->GwEb_ozd%tkr0+lOv!aZ=o)Mfhz?6lCY^{ej#p#bB04@Zs<5nPAR)psLb|Y+$gsH=(uVVAF_D-it4!lUM6D5h6CVI={)1{UUMFKhwX1)DsF0WE`1AsHKsNDnw&SWmFXP^ZmixedM&!WCWtm5x-|S90`qX zA0rNup?2_@x!6Qd4^#jZ@gk$JuMfP0nJ|UCB55GNWOzzaNH!@W>q#z&#;crcBg@HJ zcnYB;P5PTdT#;@knUB{_(ja|aA+>n_nB?Jml;QFx6G;&<&u|9Cs6tqtGRZEbD$r8YLCmo@1{!pRAcK?o#3AymUvcmU5Rp=PuzokHi) zcj*`OrO-nN6&4B~3OZ3K_7sPRQ^W|dLS`iEC7UB#EjuK;DEmroB6pAvk_XAt<;&!U z)`yv_kZbE~Gnh6f`Q0;Aq7!M^7zY8Y`{9FD>?Ktc0DTL1f_*yI3p*YR(a1YSYHO&JI@+dR+>YQbIxJ~ z{AkoGY}K3%dlmWlisEWbT)cwK>^*LFCUqM)N!uN^$|7~WN6j4Q$a)!LI>RVzdoEs+ zy^W~NNoV3r+zHV-g4S8Bw3a(M^gyUEpogP_TxD&e_3)DYd-vDHLRD$&4(L*{c5TTT z=stY>q*0?LjvY;n{?R4#Gh(%S57t!gt!A|u32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vIOin1kCFP+ zBn~yn{UjB6a=LcQr{c^bv6s{G`8bFF<|WgRDv~U~OHC$8Sa~~DvhgTvb31Xw+@th7 z(y@J!>|yA`@e)EFeF+#b z`X~xF#8(E6;`HtEb1J<~C*x;^#zC?)kF`H^qxYaFX&gruEAdT{Wcov+eGzelBoDWg zUcN9y9)=o=#o771TytE%Jj%lGPVa5p>fGKDlHBcnrS}cBBtas^m9OL>JO@M~EuSI4 z5YPYQsMp?~y_9=Fyrg?BK|615Q!WAT(?DC1Ll7J-*nufTuXoUPrc6cSP~E; z&sh5CZI8cnl#^3%4Ypfi8s=%9oBbtz+`3~7np6Lc1CO1*=TV+R-Tx+q-Wy|)AJ@Jc zUfc`aB-x`;JN`JsL|o0J7t6pr#_NZFz69x)U|2unZ;x +#include +#include +#include + +#include +#include +#include + +#include "testutil.h" + +/** @brief The monospaced ASCII subset described in assets/akgl_test_mono.LICENSE.txt. */ +#define TEST_FONT_PATH "assets/akgl_test_mono.ttf" +/** @brief Point size every test in this file opens the fixture font at. */ +#define TEST_FONT_SIZE 16 + +/** @brief The fixture font, opened once by main() and shared by every test. */ +static TTF_Font *testfont = NULL; + +akerr_ErrorContext *test_text_loadfont(void) +{ + PREPARE_ERROR(errctx); + TTF_Font *registered = NULL; + + ATTEMPT { + CATCH(errctx, akgl_registry_init_font()); + + TEST_EXPECT_OK( + errctx, + akgl_text_loadfont("testfont", TEST_FONT_PATH, TEST_FONT_SIZE), + "loading the fixture font"); + + registered = SDL_GetPointerProperty(AKGL_REGISTRY_FONT, "testfont", NULL); + TEST_ASSERT(errctx, registered != NULL, + "akgl_text_loadfont did not place the font in AKGL_REGISTRY_FONT"); + TEST_ASSERT(errctx, TTF_GetFontHeight(registered) > 0, + "the registered font reports height %d, expected a positive height", + TTF_GetFontHeight(registered)); + + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_loadfont(NULL, TEST_FONT_PATH, TEST_FONT_SIZE), + "loading a font under a NULL name"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_loadfont("nullpath", NULL, TEST_FONT_SIZE), + "loading a font from a NULL path"); + TEST_EXPECT_STATUS(errctx, AKGL_ERR_SDL, + akgl_text_loadfont("missing", "assets/no_such_font.ttf", TEST_FONT_SIZE), + "loading a font that does not exist"); + } CLEANUP { + } PROCESS(errctx) { + } FINISH(errctx, true); + SUCCEED_RETURN(errctx); +} + +akerr_ErrorContext *test_text_measure(void) +{ + PREPARE_ERROR(errctx); + int w = 0; + int h = 0; + int onecharw = 0; + int onecharh = 0; + int emptyw = 0; + int emptyh = 0; + + ATTEMPT { + // One cell. This is the measurement a character grid is built from. + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "A", &onecharw, &onecharh), + "measuring a single character"); + TEST_ASSERT(errctx, onecharw > 0, + "one character measured %d wide, expected a positive width", onecharw); + TEST_ASSERT(errctx, onecharh == TTF_GetFontHeight(testfont), + "one character measured %d high, expected the font height %d", + onecharh, TTF_GetFontHeight(testfont)); + + // The font is monospaced, so four cells are exactly four times one. + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "AAAA", &w, &h), + "measuring four characters"); + TEST_ASSERT(errctx, w == (onecharw * 4), + "four characters measured %d wide, expected %d", w, onecharw * 4); + TEST_ASSERT(errctx, h == onecharh, + "four characters on one line measured %d high, expected %d", h, onecharh); + + // ...and every character advances by the same amount, which is what + // makes a fixed grid legitimate in the first place. + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "W", &w, &h), "measuring a wide glyph"); + TEST_ASSERT(errctx, w == onecharw, + "'W' measured %d wide but 'A' measured %d in a monospaced font", w, onecharw); + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "i", &w, &h), "measuring a narrow glyph"); + TEST_ASSERT(errctx, w == onecharw, + "'i' measured %d wide but 'A' measured %d in a monospaced font", w, onecharw); + + // The empty string is zero wide and still one line high, so a cursor + // sitting on an empty line has somewhere to be. + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "", &emptyw, &emptyh), + "measuring the empty string"); + TEST_ASSERT(errctx, emptyw == 0, + "the empty string measured %d wide, expected 0", emptyw); + TEST_ASSERT(errctx, emptyh == onecharh, + "the empty string measured %d high, expected the font height %d", + emptyh, onecharh); + + // Measuring does not disturb the destinations it was not asked about. + w = -1; + h = -1; + TEST_EXPECT_OK(errctx, akgl_text_measure(testfont, "hello", &w, &h), + "measuring a word"); + TEST_ASSERT(errctx, w == (onecharw * 5), + "\"hello\" measured %d wide, expected %d", w, onecharw * 5); + + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, akgl_text_measure(NULL, "A", &w, &h), + "measuring with a NULL font"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, akgl_text_measure(testfont, NULL, &w, &h), + "measuring a NULL string"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, akgl_text_measure(testfont, "A", NULL, &h), + "measuring into a NULL width"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, akgl_text_measure(testfont, "A", &w, NULL), + "measuring into a NULL height"); + } CLEANUP { + } PROCESS(errctx) { + } FINISH(errctx, true); + SUCCEED_RETURN(errctx); +} + +akerr_ErrorContext *test_text_measure_wrapped(void) +{ + PREPARE_ERROR(errctx); + int w = 0; + int h = 0; + int onecharw = 0; + int flatw = 0; + int flath = 0; + int lineskip = 0; + + ATTEMPT { + CATCH(errctx, akgl_text_measure(testfont, "A", &onecharw, &flath)); + lineskip = TTF_GetFontLineSkip(testfont); + TEST_ASSERT(errctx, lineskip > 0, "the font reports a line skip of %d", lineskip); + + // A wrap length wide enough for the whole string measures the same as + // the unwrapped call. + CATCH(errctx, akgl_text_measure(testfont, "one two", &flatw, &flath)); + TEST_EXPECT_OK(errctx, + akgl_text_measure_wrapped(testfont, "one two", flatw + onecharw, &w, &h), + "measuring a string that fits inside the wrap length"); + TEST_ASSERT(errctx, w == flatw, + "an unwrapped measurement gave %d wide, expected %d", w, flatw); + TEST_ASSERT(errctx, h == flath, + "an unwrapped measurement gave %d high, expected %d", h, flath); + + // Narrow enough to force a break at the space: two lines, and nothing + // wider than the wrap length. + TEST_EXPECT_OK(errctx, + akgl_text_measure_wrapped(testfont, "one two", onecharw * 4, &w, &h), + "measuring a string that has to wrap"); + TEST_ASSERT(errctx, w <= (onecharw * 4), + "a wrapped measurement gave %d wide, past the %d wrap length", + w, onecharw * 4); + TEST_ASSERT(errctx, h >= (lineskip * 2), + "a wrapped measurement gave %d high, expected at least two lines (%d)", + h, lineskip * 2); + + // Zero wraps on newlines only, so an embedded newline still costs a line + // and a long unbroken string does not. + TEST_EXPECT_OK(errctx, akgl_text_measure_wrapped(testfont, "one\ntwo", 0, &w, &h), + "measuring a string with an embedded newline"); + TEST_ASSERT(errctx, h >= (lineskip * 2), + "an embedded newline measured %d high, expected at least two lines (%d)", + h, lineskip * 2); + TEST_ASSERT(errctx, w == (onecharw * 3), + "the longer of two three-character lines measured %d wide, expected %d", + w, onecharw * 3); + + TEST_EXPECT_OK(errctx, akgl_text_measure_wrapped(testfont, "one two", 0, &w, &h), + "measuring a string with no newline at wrap length zero"); + TEST_ASSERT(errctx, h == flath, + "a string with no newline measured %d high at wrap length 0, expected %d", + h, flath); + + // A negative wrap length is refused rather than silently treated as + // "never wrap", which is what SDL_ttf does with it. + TEST_EXPECT_STATUS(errctx, AKERR_OUTOFBOUNDS, + akgl_text_measure_wrapped(testfont, "one two", -1, &w, &h), + "measuring at a negative wrap length"); + + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_measure_wrapped(NULL, "A", 100, &w, &h), + "measuring wrapped with a NULL font"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_measure_wrapped(testfont, NULL, 100, &w, &h), + "measuring a NULL string wrapped"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_measure_wrapped(testfont, "A", 100, NULL, &h), + "measuring wrapped into a NULL width"); + TEST_EXPECT_STATUS(errctx, AKERR_NULLPOINTER, + akgl_text_measure_wrapped(testfont, "A", 100, &w, NULL), + "measuring wrapped into a NULL height"); + } CLEANUP { + } PROCESS(errctx) { + } FINISH(errctx, true); + SUCCEED_RETURN(errctx); +} + +int main(void) +{ + PREPARE_ERROR(errctx); + + SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "dummy"); + SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy"); + + ATTEMPT { + CATCH(errctx, akgl_error_init()); + FAIL_ZERO_BREAK( + errctx, + SDL_Init(0), + AKGL_ERR_SDL, + "Couldn't initialize SDL: %s", + SDL_GetError()); + FAIL_ZERO_BREAK( + errctx, + TTF_Init(), + AKGL_ERR_SDL, + "Couldn't initialize the font engine: %s", + SDL_GetError()); + + testfont = TTF_OpenFont(TEST_FONT_PATH, TEST_FONT_SIZE); + FAIL_ZERO_BREAK( + errctx, + testfont, + AKGL_ERR_SDL, + "Couldn't open %s: %s", + TEST_FONT_PATH, + SDL_GetError()); + + CATCH(errctx, test_text_loadfont()); + CATCH(errctx, test_text_measure()); + CATCH(errctx, test_text_measure_wrapped()); + } CLEANUP { + if ( testfont != NULL ) { + TTF_CloseFont(testfont); + } + TTF_Quit(); + SDL_Quit(); + } PROCESS(errctx) { + } FINISH_NORETURN(errctx); +}