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) <noreply@anthropic.com>
This commit is contained in:
@@ -165,6 +165,7 @@ set(AKGL_TEST_SUITES
|
||||
registry
|
||||
sprite
|
||||
staticstring
|
||||
text
|
||||
tilemap
|
||||
util
|
||||
version
|
||||
|
||||
15
TODO.md
15
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
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
#ifndef _TEXT_H_
|
||||
#define _TEXT_H_
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <akerror.h>
|
||||
|
||||
/**
|
||||
* @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_
|
||||
|
||||
45
src/text.c
45
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);
|
||||
}
|
||||
|
||||
121
tests/assets/akgl_test_mono.LICENSE.txt
Normal file
121
tests/assets/akgl_test_mono.LICENSE.txt
Normal file
@@ -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.
|
||||
BIN
tests/assets/akgl_test_mono.ttf
Normal file
BIN
tests/assets/akgl_test_mono.ttf
Normal file
Binary file not shown.
259
tests/text.c
Normal file
259
tests/text.c
Normal file
@@ -0,0 +1,259 @@
|
||||
/**
|
||||
* @file text.c
|
||||
* @brief Unit tests for font loading and text measurement.
|
||||
*
|
||||
* Measurement needs a font but no renderer, so this suite runs without a window
|
||||
* and without the offscreen harness the drawing half of src/text.c is waiting
|
||||
* on. akgl_text_rendertextat() is therefore not covered here.
|
||||
*
|
||||
* The fixture font is monospaced on purpose: the width of an N-character string
|
||||
* is exactly N times the width of one character, so every assertion below is a
|
||||
* relationship between measurements rather than a hardcoded pixel count that
|
||||
* would break when FreeType changes its rounding.
|
||||
*/
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <string.h>
|
||||
#include <akerror.h>
|
||||
|
||||
#include <akgl/error.h>
|
||||
#include <akgl/registry.h>
|
||||
#include <akgl/text.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user