|
25 | 25 |
|
26 | 26 | using namespace yup; |
27 | 27 |
|
| 28 | +namespace |
| 29 | +{ |
| 30 | +File getValidFontFile() |
| 31 | +{ |
| 32 | + return File (__FILE__) |
| 33 | + .getParentDirectory() |
| 34 | + .getParentDirectory() |
| 35 | + .getChildFile ("data") |
| 36 | + .getChildFile ("fonts") |
| 37 | + .getChildFile ("Linefont-VariableFont_wdth,wght.ttf"); |
| 38 | +} |
| 39 | +} // namespace |
| 40 | + |
28 | 41 | // ============================================================================== |
29 | 42 | // Constructor and Assignment Tests |
30 | 43 | // ============================================================================== |
@@ -137,6 +150,342 @@ TEST (FontTests, LoadFromDirectory) |
137 | 150 | EXPECT_FALSE (result.wasOk()); |
138 | 151 | } |
139 | 152 |
|
| 153 | +TEST (FontTests, LoadFromFileWithValidFile) |
| 154 | +{ |
| 155 | + Font font; |
| 156 | + File fontFile = getValidFontFile(); |
| 157 | + |
| 158 | + Result result = font.loadFromFile (fontFile); |
| 159 | + |
| 160 | + EXPECT_TRUE (result.wasOk()); |
| 161 | + EXPECT_TRUE (result.getErrorMessage().isEmpty()); |
| 162 | +} |
| 163 | + |
| 164 | +// ============================================================================== |
| 165 | +// Variable Font Tests |
| 166 | +// ============================================================================== |
| 167 | + |
| 168 | +TEST (FontTests, VariableFont_HasCorrectNumberOfAxes) |
| 169 | +{ |
| 170 | + Font font; |
| 171 | + File fontFile = getValidFontFile(); |
| 172 | + |
| 173 | + font.loadFromFile (fontFile); |
| 174 | + |
| 175 | + // The font should have 2 axes: wdth and wght |
| 176 | + EXPECT_EQ (2, font.getNumAxis()); |
| 177 | +} |
| 178 | + |
| 179 | +TEST (FontTests, VariableFont_GetAxisDescriptionByIndex) |
| 180 | +{ |
| 181 | + Font font; |
| 182 | + File fontFile = getValidFontFile(); |
| 183 | + |
| 184 | + font.loadFromFile (fontFile); |
| 185 | + |
| 186 | + // Get axis descriptions by index |
| 187 | + auto axis0 = font.getAxisDescription (0); |
| 188 | + auto axis1 = font.getAxisDescription (1); |
| 189 | + |
| 190 | + ASSERT_TRUE (axis0.has_value()); |
| 191 | + ASSERT_TRUE (axis1.has_value()); |
| 192 | + |
| 193 | + // Check that we have wdth and wght axes (order may vary) |
| 194 | + bool hasWdth = axis0->tagName == "wdth" || axis1->tagName == "wdth"; |
| 195 | + bool hasWght = axis0->tagName == "wght" || axis1->tagName == "wght"; |
| 196 | + |
| 197 | + EXPECT_TRUE (hasWdth); |
| 198 | + EXPECT_TRUE (hasWght); |
| 199 | +} |
| 200 | + |
| 201 | +TEST (FontTests, VariableFont_GetAxisDescriptionByTag) |
| 202 | +{ |
| 203 | + Font font; |
| 204 | + File fontFile = getValidFontFile(); |
| 205 | + |
| 206 | + font.loadFromFile (fontFile); |
| 207 | + |
| 208 | + // Get wdth axis description |
| 209 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 210 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 211 | + EXPECT_EQ ("wdth", wdthAxis->tagName); |
| 212 | + EXPECT_GT (wdthAxis->maximumValue, wdthAxis->minimumValue); |
| 213 | + EXPECT_GE (wdthAxis->defaultValue, wdthAxis->minimumValue); |
| 214 | + EXPECT_LE (wdthAxis->defaultValue, wdthAxis->maximumValue); |
| 215 | + |
| 216 | + // Get wght axis description |
| 217 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 218 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 219 | + EXPECT_EQ ("wght", wghtAxis->tagName); |
| 220 | + EXPECT_GT (wghtAxis->maximumValue, wghtAxis->minimumValue); |
| 221 | + EXPECT_GE (wghtAxis->defaultValue, wghtAxis->minimumValue); |
| 222 | + EXPECT_LE (wghtAxis->defaultValue, wghtAxis->maximumValue); |
| 223 | +} |
| 224 | + |
| 225 | +TEST (FontTests, VariableFont_GetAxisDescriptionForInvalidTag) |
| 226 | +{ |
| 227 | + Font font; |
| 228 | + File fontFile = getValidFontFile(); |
| 229 | + |
| 230 | + font.loadFromFile (fontFile); |
| 231 | + |
| 232 | + // Try to get description for non-existent axis |
| 233 | + auto invalidAxis = font.getAxisDescription ("slnt"); |
| 234 | + |
| 235 | + EXPECT_FALSE (invalidAxis.has_value()); |
| 236 | +} |
| 237 | + |
| 238 | +TEST (FontTests, VariableFont_GetAxisValueReturnsDefaultValue) |
| 239 | +{ |
| 240 | + Font font; |
| 241 | + File fontFile = getValidFontFile(); |
| 242 | + |
| 243 | + font.loadFromFile (fontFile); |
| 244 | + |
| 245 | + // Get default values |
| 246 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 247 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 248 | + |
| 249 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 250 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 251 | + |
| 252 | + // Initially, axis values should be at their defaults |
| 253 | + EXPECT_FLOAT_EQ (wdthAxis->defaultValue, font.getAxisValue ("wdth")); |
| 254 | + EXPECT_FLOAT_EQ (wghtAxis->defaultValue, font.getAxisValue ("wght")); |
| 255 | +} |
| 256 | + |
| 257 | +TEST (FontTests, VariableFont_SetAxisValueByTag) |
| 258 | +{ |
| 259 | + Font font; |
| 260 | + File fontFile = getValidFontFile(); |
| 261 | + |
| 262 | + font.loadFromFile (fontFile); |
| 263 | + |
| 264 | + // Get axis ranges |
| 265 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 266 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 267 | + |
| 268 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 269 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 270 | + |
| 271 | + // Set wdth to maximum |
| 272 | + font.setAxisValue ("wdth", wdthAxis->maximumValue); |
| 273 | + EXPECT_FLOAT_EQ (wdthAxis->maximumValue, font.getAxisValue ("wdth")); |
| 274 | + |
| 275 | + // Set wght to minimum |
| 276 | + font.setAxisValue ("wght", wghtAxis->minimumValue); |
| 277 | + EXPECT_FLOAT_EQ (wghtAxis->minimumValue, font.getAxisValue ("wght")); |
| 278 | +} |
| 279 | + |
| 280 | +TEST (FontTests, VariableFont_SetAxisValueByIndex) |
| 281 | +{ |
| 282 | + Font font; |
| 283 | + File fontFile = getValidFontFile(); |
| 284 | + |
| 285 | + font.loadFromFile (fontFile); |
| 286 | + |
| 287 | + // Get axis descriptions to find which index is which |
| 288 | + auto axis0 = font.getAxisDescription (0); |
| 289 | + ASSERT_TRUE (axis0.has_value()); |
| 290 | + |
| 291 | + // Set axis 0 to its maximum value |
| 292 | + font.setAxisValue (0, axis0->maximumValue); |
| 293 | + EXPECT_FLOAT_EQ (axis0->maximumValue, font.getAxisValue (0)); |
| 294 | +} |
| 295 | + |
| 296 | +TEST (FontTests, VariableFont_WithAxisValueByTag) |
| 297 | +{ |
| 298 | + Font font; |
| 299 | + File fontFile = getValidFontFile(); |
| 300 | + |
| 301 | + font.loadFromFile (fontFile); |
| 302 | + |
| 303 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 304 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 305 | + |
| 306 | + // Create new font with modified wght |
| 307 | + Font newFont = font.withAxisValue ("wght", wghtAxis->maximumValue); |
| 308 | + |
| 309 | + // Original font should be unchanged |
| 310 | + EXPECT_FLOAT_EQ (wghtAxis->defaultValue, font.getAxisValue ("wght")); |
| 311 | + |
| 312 | + // New font should have the modified value |
| 313 | + EXPECT_FLOAT_EQ (wghtAxis->maximumValue, newFont.getAxisValue ("wght")); |
| 314 | +} |
| 315 | + |
| 316 | +TEST (FontTests, VariableFont_WithAxisValueByIndex) |
| 317 | +{ |
| 318 | + Font font; |
| 319 | + File fontFile = getValidFontFile(); |
| 320 | + |
| 321 | + font.loadFromFile (fontFile); |
| 322 | + |
| 323 | + auto axis0 = font.getAxisDescription (0); |
| 324 | + ASSERT_TRUE (axis0.has_value()); |
| 325 | + |
| 326 | + // Create new font with modified axis value |
| 327 | + Font newFont = font.withAxisValue (0, axis0->maximumValue); |
| 328 | + |
| 329 | + // Original font should be unchanged |
| 330 | + EXPECT_FLOAT_EQ (axis0->defaultValue, font.getAxisValue (0)); |
| 331 | + |
| 332 | + // New font should have the modified value |
| 333 | + EXPECT_FLOAT_EQ (axis0->maximumValue, newFont.getAxisValue (0)); |
| 334 | +} |
| 335 | + |
| 336 | +TEST (FontTests, VariableFont_ResetAxisValueByTag) |
| 337 | +{ |
| 338 | + Font font; |
| 339 | + File fontFile = getValidFontFile(); |
| 340 | + |
| 341 | + font.loadFromFile (fontFile); |
| 342 | + |
| 343 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 344 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 345 | + |
| 346 | + // Set to non-default value |
| 347 | + font.setAxisValue ("wdth", wdthAxis->maximumValue); |
| 348 | + EXPECT_FLOAT_EQ (wdthAxis->maximumValue, font.getAxisValue ("wdth")); |
| 349 | + |
| 350 | + // Reset to default |
| 351 | + font.resetAxisValue ("wdth"); |
| 352 | + EXPECT_FLOAT_EQ (wdthAxis->defaultValue, font.getAxisValue ("wdth")); |
| 353 | +} |
| 354 | + |
| 355 | +TEST (FontTests, VariableFont_ResetAxisValueByIndex) |
| 356 | +{ |
| 357 | + Font font; |
| 358 | + File fontFile = getValidFontFile(); |
| 359 | + |
| 360 | + font.loadFromFile (fontFile); |
| 361 | + |
| 362 | + auto axis0 = font.getAxisDescription (0); |
| 363 | + ASSERT_TRUE (axis0.has_value()); |
| 364 | + |
| 365 | + // Set to non-default value |
| 366 | + font.setAxisValue (0, axis0->maximumValue); |
| 367 | + EXPECT_FLOAT_EQ (axis0->maximumValue, font.getAxisValue (0)); |
| 368 | + |
| 369 | + // Reset to default |
| 370 | + font.resetAxisValue (0); |
| 371 | + EXPECT_FLOAT_EQ (axis0->defaultValue, font.getAxisValue (0)); |
| 372 | +} |
| 373 | + |
| 374 | +TEST (FontTests, VariableFont_ResetAllAxisValues) |
| 375 | +{ |
| 376 | + Font font; |
| 377 | + File fontFile = getValidFontFile(); |
| 378 | + |
| 379 | + font.loadFromFile (fontFile); |
| 380 | + |
| 381 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 382 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 383 | + |
| 384 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 385 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 386 | + |
| 387 | + // Set both axes to non-default values |
| 388 | + font.setAxisValue ("wdth", wdthAxis->maximumValue); |
| 389 | + font.setAxisValue ("wght", wghtAxis->minimumValue); |
| 390 | + |
| 391 | + // Reset all axes |
| 392 | + font.resetAllAxisValues(); |
| 393 | + |
| 394 | + // Both should be back to defaults |
| 395 | + EXPECT_FLOAT_EQ (wdthAxis->defaultValue, font.getAxisValue ("wdth")); |
| 396 | + EXPECT_FLOAT_EQ (wghtAxis->defaultValue, font.getAxisValue ("wght")); |
| 397 | +} |
| 398 | + |
| 399 | +TEST (FontTests, VariableFont_SetAxisValues) |
| 400 | +{ |
| 401 | + Font font; |
| 402 | + File fontFile = getValidFontFile(); |
| 403 | + |
| 404 | + font.loadFromFile (fontFile); |
| 405 | + |
| 406 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 407 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 408 | + |
| 409 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 410 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 411 | + |
| 412 | + // Set multiple axes at once |
| 413 | + font.setAxisValues ({ { "wdth", wdthAxis->maximumValue }, |
| 414 | + { "wght", wghtAxis->minimumValue } }); |
| 415 | + |
| 416 | + EXPECT_FLOAT_EQ (wdthAxis->maximumValue, font.getAxisValue ("wdth")); |
| 417 | + EXPECT_FLOAT_EQ (wghtAxis->minimumValue, font.getAxisValue ("wght")); |
| 418 | +} |
| 419 | + |
| 420 | +TEST (FontTests, VariableFont_WithAxisValues) |
| 421 | +{ |
| 422 | + Font font; |
| 423 | + File fontFile = getValidFontFile(); |
| 424 | + |
| 425 | + font.loadFromFile (fontFile); |
| 426 | + |
| 427 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 428 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 429 | + |
| 430 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 431 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 432 | + |
| 433 | + // Create new font with multiple axis modifications |
| 434 | + Font newFont = font.withAxisValues ({ { "wdth", wdthAxis->minimumValue }, |
| 435 | + { "wght", wghtAxis->maximumValue } }); |
| 436 | + |
| 437 | + // Original font should be unchanged |
| 438 | + EXPECT_FLOAT_EQ (wdthAxis->defaultValue, font.getAxisValue ("wdth")); |
| 439 | + EXPECT_FLOAT_EQ (wghtAxis->defaultValue, font.getAxisValue ("wght")); |
| 440 | + |
| 441 | + // New font should have the modified values |
| 442 | + EXPECT_FLOAT_EQ (wdthAxis->minimumValue, newFont.getAxisValue ("wdth")); |
| 443 | + EXPECT_FLOAT_EQ (wghtAxis->maximumValue, newFont.getAxisValue ("wght")); |
| 444 | +} |
| 445 | + |
| 446 | +TEST (FontTests, VariableFont_ChainedAxisOperations) |
| 447 | +{ |
| 448 | + Font font; |
| 449 | + File fontFile = getValidFontFile(); |
| 450 | + |
| 451 | + font.loadFromFile (fontFile); |
| 452 | + |
| 453 | + auto wdthAxis = font.getAxisDescription ("wdth"); |
| 454 | + auto wghtAxis = font.getAxisDescription ("wght"); |
| 455 | + |
| 456 | + ASSERT_TRUE (wdthAxis.has_value()); |
| 457 | + ASSERT_TRUE (wghtAxis.has_value()); |
| 458 | + |
| 459 | + // Chain multiple operations |
| 460 | + Font newFont = font |
| 461 | + .withAxisValue ("wdth", wdthAxis->maximumValue) |
| 462 | + .withAxisValue ("wght", wghtAxis->minimumValue) |
| 463 | + .withHeight (24.0f); |
| 464 | + |
| 465 | + // Original font should be unchanged |
| 466 | + EXPECT_FLOAT_EQ (wdthAxis->defaultValue, font.getAxisValue ("wdth")); |
| 467 | + EXPECT_FLOAT_EQ (wghtAxis->defaultValue, font.getAxisValue ("wght")); |
| 468 | + EXPECT_EQ (12.0f, font.getHeight()); |
| 469 | + |
| 470 | + // New font should have all modifications |
| 471 | + EXPECT_FLOAT_EQ (wdthAxis->maximumValue, newFont.getAxisValue ("wdth")); |
| 472 | + EXPECT_FLOAT_EQ (wghtAxis->minimumValue, newFont.getAxisValue ("wght")); |
| 473 | + EXPECT_EQ (24.0f, newFont.getHeight()); |
| 474 | +} |
| 475 | + |
| 476 | +TEST (FontTests, VariableFont_FontMetrics) |
| 477 | +{ |
| 478 | + Font font; |
| 479 | + File fontFile = getValidFontFile(); |
| 480 | + |
| 481 | + font.loadFromFile (fontFile); |
| 482 | + |
| 483 | + // Variable font should have valid metrics |
| 484 | + EXPECT_NE (0.0f, font.getAscent()); |
| 485 | + EXPECT_NE (0.0f, font.getDescent()); |
| 486 | + EXPECT_GT (font.getWeight(), 0); |
| 487 | +} |
| 488 | + |
140 | 489 | // ============================================================================== |
141 | 490 | // Height Tests |
142 | 491 | // ============================================================================== |
|
0 commit comments