Skip to content

Commit

Permalink
Add comprehensive font test
Browse files Browse the repository at this point in the history
  • Loading branch information
a740g committed May 12, 2024
1 parent a212bf4 commit 6469def
Show file tree
Hide file tree
Showing 41 changed files with 1,169 additions and 99 deletions.
4 changes: 4 additions & 0 deletions internal/c/libqb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include "shell.h"
#include "thread.h"

// These are here because they are used in func__loadfont()
#include <string>
#include <algorithm>

int32 disableEvents = 0;

// Global console vvalues
Expand Down
12 changes: 10 additions & 2 deletions internal/c/parts/video/font/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,17 @@ int32_t FontLoad(const uint8_t *content_original, int32_t content_bytes, int32_t
fontManager.fonts[h]->baseline = FT_MulDiv(FT_MulFix(fontManager.fonts[h]->face->ascender, fontManager.fonts[h]->face->size->metrics.y_scale),
default_pixel_height, fontManager.fonts[h]->face->size->metrics.height);

FONT_DEBUG_PRINT("Font baseline = %d", fontManager.fonts[h]->baseline);

FONT_DEBUG_PRINT("AUTOMONO requested: %s", (options & FONT_LOAD_AUTOMONO) ? "yes" : "no");

// Check if automatic fixed width font detection was requested
if ((options & FONT_LOAD_AUTOMONO) && FT_IS_FIXED_WIDTH(fontManager.fonts[h]->face))
options |= FONT_LOAD_MONOSPACE; // force set monospace flag and pass it upstream if the font is fixed width
if ((options & FONT_LOAD_AUTOMONO) && FT_IS_FIXED_WIDTH(fontManager.fonts[h]->face)) {
FONT_DEBUG_PRINT("Fixed-width font detected. Setting MONOSPACE flag");

// Force set monospace flag and pass it upstream if the font is fixed width
options |= FONT_LOAD_MONOSPACE;
}

if (options & FONT_LOAD_MONOSPACE) {
const FT_ULong testCP = 'W'; // since W is usually the widest
Expand Down
4 changes: 3 additions & 1 deletion tests/compile_tests/font/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
!slick.ttf
!LiberationMono-Regular.ttf
!LiberationSans-Regular.ttf
!LiberationSerif-Regular.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed tests/compile_tests/font/slick.ttf
Binary file not shown.
776 changes: 750 additions & 26 deletions tests/compile_tests/font/test.bas

Large diffs are not rendered by default.

188 changes: 184 additions & 4 deletions tests/compile_tests/font/test.output
Original file line number Diff line number Diff line change
@@ -1,4 +1,184 @@
Loading font using filename...done.
Font height = 10
Loading font from memory...done.
Font height = 20
Current font: 16
Changing font to 14
New font: 14

Built-in font 8.
_FONTWIDTH = 8
_FONTHEIGHT = 8
_UFONTHEIGHT = 8
_ULINESPACING = 8
_PRINTWIDTH = 760
_UPRINTWIDTH = 760
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Built-in font 14.
_FONTWIDTH = 8
_FONTHEIGHT = 14
_UFONTHEIGHT = 14
_ULINESPACING = 14
_PRINTWIDTH = 760
_UPRINTWIDTH = 760
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Built-in font 16.
_FONTWIDTH = 8
_FONTHEIGHT = 16
_UFONTHEIGHT = 16
_ULINESPACING = 16
_PRINTWIDTH = 760
_UPRINTWIDTH = 760
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font from memory () ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 12
_UFONTHEIGHT = 15
_ULINESPACING = 15
_PRINTWIDTH = 597
_UPRINTWIDTH = 597
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font from memory (monospace) ... done.
_FONTWIDTH = 16
_FONTHEIGHT = 16
_UFONTHEIGHT = 20
_ULINESPACING = 20
_PRINTWIDTH = 1520
_UPRINTWIDTH = 1520
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font from memory (automono) ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 22
_UFONTHEIGHT = 27
_ULINESPACING = 27
_PRINTWIDTH = 1088
_UPRINTWIDTH = 1088
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSans-Regular.ttf () ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 12
_UFONTHEIGHT = 13
_ULINESPACING = 13
_PRINTWIDTH = 606
_UPRINTWIDTH = 606
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSans-Regular.ttf (monospace) ... done.
_FONTWIDTH = 15
_FONTHEIGHT = 16
_UFONTHEIGHT = 17
_ULINESPACING = 18
_PRINTWIDTH = 1425
_UPRINTWIDTH = 1425
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSans-Regular.ttf (automono) ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 22
_UFONTHEIGHT = 24
_ULINESPACING = 25
_PRINTWIDTH = 1094
_UPRINTWIDTH = 1094
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSerif-Regular.ttf () ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 12
_UFONTHEIGHT = 13
_ULINESPACING = 13
_PRINTWIDTH = 583
_UPRINTWIDTH = 583
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSerif-Regular.ttf (monospace) ... done.
_FONTWIDTH = 15
_FONTHEIGHT = 16
_UFONTHEIGHT = 17
_ULINESPACING = 18
_PRINTWIDTH = 1425
_UPRINTWIDTH = 1425
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationSerif-Regular.ttf (automono) ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 22
_UFONTHEIGHT = 24
_ULINESPACING = 25
_PRINTWIDTH = 1063
_UPRINTWIDTH = 1063
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationMono-Regular.ttf () ... done.
_FONTWIDTH = 0
_FONTHEIGHT = 12
_UFONTHEIGHT = 13
_ULINESPACING = 13
_PRINTWIDTH = 665
_UPRINTWIDTH = 665
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationMono-Regular.ttf (monospace) ... done.
_FONTWIDTH = 10
_FONTHEIGHT = 16
_UFONTHEIGHT = 18
_ULINESPACING = 18
_PRINTWIDTH = 950
_UPRINTWIDTH = 950
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Loading font LiberationMono-Regular.ttf (automono) ... done.
_FONTWIDTH = 13
_FONTHEIGHT = 22
_UFONTHEIGHT = 24
_ULINESPACING = 24
_PRINTWIDTH = 1235
_UPRINTWIDTH = 1235
_UCHARPOS = 95

Success, images are identical!
Success, images are identical!

Binary file added tests/compile_tests/font/test01.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test01u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test02.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test02u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test03.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test03u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test04.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test04u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test05.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test05u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test06.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test06u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test07.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test07u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test08.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test08u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test09.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test09u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test10.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test10u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test11.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test11u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test12.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test12u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test13.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test13u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test14.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test14u.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test15.bmp
Binary file not shown.
Binary file added tests/compile_tests/font/test15u.bmp
Binary file not shown.
146 changes: 146 additions & 0 deletions tests/compile_tests/utilities/base64.bm
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'-----------------------------------------------------------------------------------------------------------------------
' Base64 Encoder and Decoder library
' a740g
'-----------------------------------------------------------------------------------------------------------------------

' Converts a normal string or binary data to a base64 string
Function Base64_Encode$ (s As String)
Const BASE64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Dim srcSize As _Unsigned Long: srcSize = Len(s)
Dim srcSize3rem As _Unsigned Long: srcSize3rem = srcSize Mod 3
Dim srcSize3mul As _Unsigned Long: srcSize3mul = srcSize - srcSize3rem
Dim buffer As String: buffer = Space$(((srcSize + 2) \ 3) * 4) ' preallocate complete buffer
Dim j As _Unsigned Long: j = 1

Dim i As _Unsigned Long: For i = 1 To srcSize3mul Step 3
Dim char1 As _Unsigned _Byte: char1 = Asc(s, i)
Dim char2 As _Unsigned _Byte: char2 = Asc(s, i + 1)
Dim char3 As _Unsigned _Byte: char3 = Asc(s, i + 2)

Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShR(char1, 2)))
j = j + 1
Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShL((char1 And 3), 4) Or _ShR(char2, 4)))
j = j + 1
Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShL((char2 And 15), 2) Or _ShR(char3, 6)))
j = j + 1
Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (char3 And 63))
j = j + 1
Next i

' Add padding
If srcSize3rem > 0 Then
char1 = Asc(s, i)

Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShR(char1, 2)))
j = j + 1

If srcSize3rem = 1 Then
Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShL(char1 And 3, 4)))
j = j + 1
Asc(buffer, j) = 61 ' "="
j = j + 1
Asc(buffer, j) = 61 ' "="
Else ' srcSize3rem = 2
char2 = Asc(s, i + 1)

Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShL((char1 And 3), 4) Or _ShR(char2, 4)))
j = j + 1
Asc(buffer, j) = Asc(BASE64_CHARACTERS, 1 + (_ShL(char2 And 15, 2)))
j = j + 1
Asc(buffer, j) = 61 ' "="
End If
End If

Base64_Encode = buffer
End Function


' Converts a base64 string to a normal string or binary data
Function Base64_Decode$ (s As String)
Dim srcSize As _Unsigned Long: srcSize = Len(s)
Dim buffer As String: buffer = Space$((srcSize \ 4) * 3) ' preallocate complete buffer
Dim j As _Unsigned Long: j = 1
Dim As _Unsigned _Byte index, char1, char2, char3, char4

Dim i As _Unsigned Long: For i = 1 To srcSize Step 4
index = Asc(s, i): GoSub find_index: char1 = index
index = Asc(s, i + 1): GoSub find_index: char2 = index
index = Asc(s, i + 2): GoSub find_index: char3 = index
index = Asc(s, i + 3): GoSub find_index: char4 = index

Asc(buffer, j) = _ShL(char1, 2) Or _ShR(char2, 4)
j = j + 1
Asc(buffer, j) = _ShL(char2 And 15, 4) Or _ShR(char3, 2)
j = j + 1
Asc(buffer, j) = _ShL(char3 And 3, 6) Or char4
j = j + 1
Next i

' Remove padding
If Right$(s, 2) = "==" Then
buffer = Left$(buffer, Len(buffer) - 2)
ElseIf Right$(s, 1) = "=" Then
buffer = Left$(buffer, Len(buffer) - 1)
End If

Base64_Decode = buffer
Exit Function

find_index:
If index >= 65 And index <= 90 Then
index = index - 65
ElseIf index >= 97 And index <= 122 Then
index = index - 97 + 26
ElseIf index >= 48 And index <= 57 Then
index = index - 48 + 52
ElseIf index = 43 Then
index = 62
ElseIf index = 47 Then
index = 63
End If
Return
End Function


' This function loads a resource directly from a string variable or constant (like the ones made by Bin2Data)
Function Base64_LoadResourceString$ (src As String, ogSize As _Unsigned Long, isComp As _Byte)
' Decode the data
Dim buffer As String: buffer = Base64_Decode(src)

' Expand the data if needed
If isComp Then buffer = _Inflate$(buffer, ogSize)

Base64_LoadResourceString = buffer
End Function


' Loads a binary file encoded with Bin2Data
' Usage:
' 1. Encode the binary file with Bin2Data
' 2. Include the file or it's contents
' 3. Load the file like so:
' Restore label_generated_by_bin2data
' Dim buffer As String
' buffer = Base64_LoadResourceData ' buffer will now hold the contents of the file
Function Base64_LoadResourceData$
Dim ogSize As _Unsigned Long, resize As _Unsigned Long, isComp As _Byte
Read ogSize, resize, isComp ' read the header

Dim buffer As String: buffer = Space$(resize) ' preallocate complete buffer

' Read the whole resource data
Dim i As _Unsigned Long: Do While i < resize
Dim chunk As String: Read chunk
Mid$(buffer, i + 1) = chunk
i = i + Len(chunk)
Loop

' Decode the data
buffer = Base64_Decode(buffer)

' Expand the data if needed
If isComp Then buffer = _Inflate$(buffer, ogSize)

Base64_LoadResourceData = buffer
End Function
Loading

0 comments on commit 6469def

Please sign in to comment.