|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include "internal.h" |
| 5 | +#include <c2d/font.h> |
| 6 | + |
| 7 | +C2D_Font C2D_FontLoad(const char* filename) |
| 8 | +{ |
| 9 | + FILE* f = fopen(filename, "rb"); |
| 10 | + if (!f) return NULL; |
| 11 | + C2D_Font ret = C2D_FontLoadFromHandle(f); |
| 12 | + fclose(f); |
| 13 | + return ret; |
| 14 | +} |
| 15 | + |
| 16 | +static inline C2D_Font C2Di_FontAlloc(void) |
| 17 | +{ |
| 18 | + return (C2D_Font)malloc(sizeof(struct C2D_Font_s)); |
| 19 | +} |
| 20 | + |
| 21 | +static C2D_Font C2Di_PostLoadFont(C2D_Font font) |
| 22 | +{ |
| 23 | + if (!font->cfnt) |
| 24 | + { |
| 25 | + free(font); |
| 26 | + font = NULL; |
| 27 | + } else |
| 28 | + { |
| 29 | + fontFixPointers(font->cfnt); |
| 30 | + |
| 31 | + TGLP_s* glyphInfo = font->cfnt->finf.tglp; |
| 32 | + font->glyphSheets = malloc(sizeof(C3D_Tex)*glyphInfo->nSheets); |
| 33 | + font->textScale = 30.0f / glyphInfo->cellHeight; |
| 34 | + if (!font->glyphSheets) |
| 35 | + { |
| 36 | + C2D_FontFree(font); |
| 37 | + return NULL; |
| 38 | + } |
| 39 | + |
| 40 | + int i; |
| 41 | + for (i = 0; i < glyphInfo->nSheets; i++) |
| 42 | + { |
| 43 | + C3D_Tex* tex = &font->glyphSheets[i]; |
| 44 | + tex->data = &glyphInfo->sheetData[glyphInfo->sheetSize*i]; |
| 45 | + tex->fmt = glyphInfo->sheetFmt; |
| 46 | + tex->size = glyphInfo->sheetSize; |
| 47 | + tex->width = glyphInfo->sheetWidth; |
| 48 | + tex->height = glyphInfo->sheetHeight; |
| 49 | + tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) |
| 50 | + | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_BORDER) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_BORDER); |
| 51 | + tex->border = 0xFFFFFFFF; |
| 52 | + tex->lodParam = 0; |
| 53 | + } |
| 54 | + } |
| 55 | + return font; |
| 56 | +} |
| 57 | + |
| 58 | +C2D_Font C2D_FontLoadFromMem(const void* data, size_t size) |
| 59 | +{ |
| 60 | + C2D_Font font = C2Di_FontAlloc(); |
| 61 | + if (font) |
| 62 | + { |
| 63 | + font->cfnt = linearAlloc(size); |
| 64 | + if (font->cfnt) |
| 65 | + memcpy(font->cfnt, data, size); |
| 66 | + font = C2Di_PostLoadFont(font); |
| 67 | + } |
| 68 | + return font; |
| 69 | +} |
| 70 | + |
| 71 | +C2D_Font C2D_FontLoadFromFD(int fd) |
| 72 | +{ |
| 73 | + C2D_Font font = C2Di_FontAlloc(); |
| 74 | + if (font) |
| 75 | + { |
| 76 | + CFNT_s cfnt; |
| 77 | + read(fd, &cfnt, sizeof(CFNT_s)); |
| 78 | + font->cfnt = linearAlloc(cfnt.fileSize); |
| 79 | + if (font->cfnt) |
| 80 | + { |
| 81 | + memcpy(font->cfnt, &cfnt, sizeof(CFNT_s)); |
| 82 | + read(fd, (u8*)(font->cfnt) + sizeof(CFNT_s), cfnt.fileSize - sizeof(CFNT_s)); |
| 83 | + } |
| 84 | + font = C2Di_PostLoadFont(font); |
| 85 | + } |
| 86 | + return font; |
| 87 | +} |
| 88 | + |
| 89 | +C2D_Font C2D_FontLoadFromHandle(FILE* handle) |
| 90 | +{ |
| 91 | + C2D_Font font = C2Di_FontAlloc(); |
| 92 | + if (font) |
| 93 | + { |
| 94 | + CFNT_s cfnt; |
| 95 | + fread(&cfnt, 1, sizeof(CFNT_s), handle); |
| 96 | + font->cfnt = linearAlloc(cfnt.fileSize); |
| 97 | + if (font->cfnt) |
| 98 | + { |
| 99 | + memcpy(font->cfnt, &cfnt, sizeof(CFNT_s)); |
| 100 | + fread((u8*)(font->cfnt) + sizeof(CFNT_s), 1, cfnt.fileSize - sizeof(CFNT_s), handle); |
| 101 | + } |
| 102 | + font = C2Di_PostLoadFont(font); |
| 103 | + } |
| 104 | + return font; |
| 105 | +} |
| 106 | + |
| 107 | +static C2D_Font C2Di_FontLoadFromArchive(u64 binary_lowpath) |
| 108 | +{ |
| 109 | + C2D_Font font = C2Di_FontAlloc(); |
| 110 | + if (font) |
| 111 | + { |
| 112 | + int fontNum = (binary_lowpath & 0x300) >> 8; |
| 113 | + const u8 sizeMod = fontNum == 0 ? 0xA0 : 0xB0; |
| 114 | + u64 lowPath[] = { binary_lowpath, 0x00000001FFFFFE00 }; |
| 115 | + Handle romfs_handle; |
| 116 | + u64 romfs_size = 0; |
| 117 | + u32 romfs_bytes_read = 0; |
| 118 | + |
| 119 | + FS_Path savedatacheck_path = { PATH_BINARY, 16, (u8*)lowPath }; |
| 120 | + u8 file_binary_lowpath[20] = {}; |
| 121 | + FS_Path romfs_path = { PATH_BINARY, 20, file_binary_lowpath }; |
| 122 | + |
| 123 | + if (R_FAILED(FSUSER_OpenFileDirectly(&romfs_handle, (FS_ArchiveID)0x2345678a, savedatacheck_path, romfs_path, FS_OPEN_READ, 0))) |
| 124 | + { |
| 125 | + free(font); |
| 126 | + return NULL; |
| 127 | + } |
| 128 | + if (R_FAILED(FSFILE_GetSize(romfs_handle, &romfs_size))) |
| 129 | + { |
| 130 | + free(font); |
| 131 | + FSFILE_Close(romfs_handle); |
| 132 | + return NULL; |
| 133 | + } |
| 134 | + |
| 135 | + u8* romfs_data_buffer = malloc(romfs_size); |
| 136 | + if (!romfs_data_buffer) |
| 137 | + { |
| 138 | + free(font); |
| 139 | + FSFILE_Close(romfs_handle); |
| 140 | + return NULL; |
| 141 | + } |
| 142 | + if (R_FAILED(FSFILE_Read(romfs_handle, &romfs_bytes_read, 0, romfs_data_buffer, romfs_size))) |
| 143 | + { |
| 144 | + free(romfs_data_buffer); |
| 145 | + free(font); |
| 146 | + FSFILE_Close(romfs_handle); |
| 147 | + return NULL; |
| 148 | + } |
| 149 | + FSFILE_Close(romfs_handle); |
| 150 | + |
| 151 | + u8* compFontData = romfs_data_buffer + sizeMod; |
| 152 | + |
| 153 | + u32 fontSize = *(u32*)(compFontData) >> 8; |
| 154 | + font->cfnt = linearAlloc(fontSize); |
| 155 | + if (font->cfnt) |
| 156 | + { |
| 157 | + if (!decompress_LZ11(font->cfnt, fontSize, NULL, compFontData + 4, romfs_size - sizeMod - 4)) |
| 158 | + { |
| 159 | + C2D_FontFree(font); |
| 160 | + return NULL; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + free(romfs_data_buffer); |
| 165 | + |
| 166 | + font = C2Di_PostLoadFont(font); |
| 167 | + } |
| 168 | + |
| 169 | + return font; |
| 170 | +} |
| 171 | + |
| 172 | +C2D_Font C2D_FontLoadSystem(CFG_Region region) |
| 173 | +{ |
| 174 | + u8 systemRegion; |
| 175 | + if (R_FAILED(CFGU_SecureInfoGetRegion(&systemRegion))) |
| 176 | + return NULL; |
| 177 | + |
| 178 | + switch (region) |
| 179 | + { |
| 180 | + case 0: |
| 181 | + case 1: |
| 182 | + case 2: |
| 183 | + case 3: |
| 184 | + if (systemRegion > 3) |
| 185 | + return C2Di_FontLoadFromArchive(0x0004009b00014002); |
| 186 | + break; |
| 187 | + case 4: |
| 188 | + if (systemRegion == 4) |
| 189 | + break; |
| 190 | + return C2Di_FontLoadFromArchive(0x0004009b00014102); |
| 191 | + case 5: |
| 192 | + if (systemRegion == 5) |
| 193 | + break; |
| 194 | + return C2Di_FontLoadFromArchive(0x0004009b00014202); |
| 195 | + case 6: |
| 196 | + if (systemRegion == 6) |
| 197 | + break; |
| 198 | + return C2Di_FontLoadFromArchive(0x0004009b00014302); |
| 199 | + } |
| 200 | + return NULL; |
| 201 | +} |
| 202 | + |
| 203 | +void C2D_FontFree(C2D_Font font) |
| 204 | +{ |
| 205 | + if (font) |
| 206 | + { |
| 207 | + if (font->cfnt) |
| 208 | + linearFree(font->cfnt); |
| 209 | + free(font->glyphSheets); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +int C2D_FontGlyphIndexFromCodePoint(C2D_Font font, u32 codepoint) |
| 214 | +{ |
| 215 | + if (!font) |
| 216 | + return fontGlyphIndexFromCodePoint(fontGetSystemFont(), codepoint); |
| 217 | + else |
| 218 | + return fontGlyphIndexFromCodePoint(font->cfnt, codepoint); |
| 219 | +} |
| 220 | + |
| 221 | +charWidthInfo_s* C2D_FontGetCharWidthInfo(C2D_Font font, int glyphIndex) |
| 222 | +{ |
| 223 | + if (!font) |
| 224 | + return fontGetCharWidthInfo(fontGetSystemFont(), glyphIndex); |
| 225 | + else |
| 226 | + return fontGetCharWidthInfo(font->cfnt, glyphIndex); |
| 227 | +} |
| 228 | + |
| 229 | +void C2D_FontCalcGlyphPos(C2D_Font font, fontGlyphPos_s* out, int glyphIndex, u32 flags, float scaleX, float scaleY) |
| 230 | +{ |
| 231 | + if (!font) |
| 232 | + fontCalcGlyphPos(out, fontGetSystemFont(), glyphIndex, flags, scaleX, scaleY); |
| 233 | + else |
| 234 | + fontCalcGlyphPos(out, font->cfnt, glyphIndex, flags, scaleX, scaleY); |
| 235 | +} |
| 236 | + |
| 237 | +FINF_s* C2D_FontGetInfo(C2D_Font font) |
| 238 | +{ |
| 239 | + if (!font) |
| 240 | + return fontGetInfo(NULL); |
| 241 | + else |
| 242 | + return fontGetInfo(font->cfnt); |
| 243 | +} |
0 commit comments