Changes in uspace/lib/draw/font/pcf.c [5a6cc679:a35b458] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/draw/font/pcf.c
r5a6cc679 ra35b458 42 42 #include <offset.h> 43 43 #include <stdlib.h> 44 #include <str.h> 44 45 45 46 #include "pcf.h" … … 150 151 { 151 152 pcf_data_t *data = (pcf_data_t *) opaque_data; 152 153 153 154 /* TODO is this correct? */ 154 155 uint8_t byte1 = (chr >> 8) & 0xff; … … 159 160 (byte1 - e->min_byte1) * (e->max_byte2 - e->min_byte2 + 1) + 160 161 (byte2 - e->min_byte2); 161 162 162 163 aoff64_t entry_offset = data->encodings_table.offset + 163 164 (sizeof(uint32_t) + 5 * sizeof(uint16_t)) + 164 165 entry_index * sizeof(uint16_t); 165 166 166 167 int rc = fseek(data->file, entry_offset, SEEK_SET); 167 168 if (rc != 0) 168 169 return errno; 169 170 170 171 uint16_t glyph = 0; 171 172 size_t records_read = fread(&glyph, sizeof(uint16_t), 1, data->file); 172 173 if (records_read != 1) 173 174 return EINVAL; 174 175 175 176 glyph = uint16_t_pcf2host(glyph, data->encodings_table.format); 176 177 177 178 if (glyph == 0xffff) 178 179 return ENOENT; 179 180 180 181 *glyph_id = glyph; 181 182 182 183 return EOK; 183 184 } … … 189 190 int rc; 190 191 size_t records_read; 191 192 192 193 if (table->format & PCF_FORMAT_COMPRESSED_METRICS) { 193 194 offset = table->offset + sizeof(uint32_t) + sizeof(uint16_t) + 194 195 glyph_id * sizeof(pcf_compressed_metrics_t); 195 196 196 197 rc = fseek(data->file, offset, SEEK_SET); 197 198 if (rc != 0) 198 199 return errno; 199 200 200 201 pcf_compressed_metrics_t compressed_metrics; 201 202 records_read = fread(&compressed_metrics, … … 203 204 if (records_read != 1) 204 205 return EINVAL; 205 206 206 207 metrics->left_side_bearing = 207 208 compressed2int(compressed_metrics.left_side_bearing); … … 219 220 offset = table->offset + 2 * sizeof(uint32_t) + 220 221 glyph_id * sizeof(pcf_default_metrics_t); 221 222 222 223 rc = fseek(data->file, offset, SEEK_SET); 223 224 if (rc != 0) 224 225 return errno; 225 226 226 227 pcf_default_metrics_t uncompressed_metrics; 227 228 records_read = fread(&uncompressed_metrics, … … 229 230 if (records_read != 1) 230 231 return EINVAL; 231 232 232 233 metrics->left_side_bearing = 233 234 int16_t_pcf2host(uncompressed_metrics.left_side_bearing, … … 249 250 table->format); 250 251 } 251 252 252 253 return EOK; 253 254 } … … 257 258 { 258 259 pcf_data_t *data = (pcf_data_t *) opaque_data; 259 260 260 261 pcf_default_metrics_t pcf_metrics; 261 262 memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t)); … … 264 265 if (rc != EOK) 265 266 return rc; 266 267 267 268 aoff64_t offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) + 268 269 (glyph_id * sizeof(uint32_t)); 269 270 270 271 if (fseek(data->file, offset, SEEK_SET) < 0) 271 272 return errno; 272 273 273 274 uint32_t bitmap_offset = 0; 274 275 size_t records_read = fread(&bitmap_offset, sizeof(uint32_t), 1, … … 278 279 bitmap_offset = uint32_t_pcf2host(bitmap_offset, 279 280 data->bitmap_table.format); 280 281 281 282 offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) + 282 283 (data->glyph_count * sizeof(uint32_t)) + (4 * sizeof(uint32_t)) 283 284 + bitmap_offset; 284 285 285 286 if (fseek(data->file, offset, SEEK_SET) < 0) 286 287 return errno; 287 288 288 289 surface_coord_t width = pcf_metrics.character_width; 289 290 surface_coord_t height = pcf_metrics.character_ascent + … … 293 294 size_t row_bytes = ALIGN_UP(ALIGN_UP(width, 8) / 8, row_padding_bytes); 294 295 size_t bitmap_bytes = height * row_bytes; 295 296 296 297 uint8_t *bitmap = malloc(bitmap_bytes); 297 298 if (bitmap == NULL) 298 299 return ENOMEM; 299 300 300 301 records_read = fread(bitmap, sizeof(uint8_t), bitmap_bytes, 301 302 data->file); 302 303 303 304 surface_t *surface = surface_create(width, height, NULL, 0); 304 305 if (!surface) { … … 306 307 return ENOMEM; 307 308 } 308 309 309 310 for (unsigned int y = 0; y < height; ++y) { 310 311 size_t row_offset = row_bytes * y; … … 333 334 } 334 335 } 335 336 336 337 *out_surface = surface; 337 338 free(bitmap); … … 343 344 { 344 345 pcf_data_t *data = (pcf_data_t *) opaque_data; 345 346 346 347 pcf_default_metrics_t pcf_metrics; 347 348 memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t)); … … 350 351 if (rc != EOK) 351 352 return rc; 352 353 353 354 gm->left_side_bearing = pcf_metrics.left_side_bearing; 354 355 gm->width = pcf_metrics.character_width; … … 358 359 pcf_metrics.character_ascent; 359 360 gm->ascender = pcf_metrics.character_ascent; 360 361 361 362 return EOK; 362 363 } … … 365 366 { 366 367 pcf_data_t *data = (pcf_data_t *) opaque_data; 367 368 368 369 fclose(data->file); 369 370 free(data); … … 382 383 if (rc != 0) 383 384 return errno; 384 385 385 386 aoff64_t file_size = ftell(data->file); 386 387 387 388 rc = fseek(data->file, 0, SEEK_SET); 388 389 if (rc != 0) 389 390 return errno; 390 391 391 392 char header[4]; 392 393 size_t records_read = fread(header, sizeof(char), 4, data->file); 393 394 if (records_read != 4) 394 395 return EINVAL; 395 396 396 397 if (header[0] != 1 || header[1] != 'f' || header[2] != 'c' || 397 398 header[3] != 'p') 398 399 return EINVAL; 399 400 400 401 uint32_t table_count; 401 402 records_read = fread(&table_count, sizeof(uint32_t), 1, … … 403 404 if (records_read != 1) 404 405 return EINVAL; 405 406 406 407 table_count = uint32_t_le2host(table_count); 407 408 408 409 bool found_bitmap_table = false; 409 410 bool found_metrics_table = false; 410 411 bool found_encodings_table = false; 411 412 bool found_accelerators_table = false; 412 413 413 414 for (uint32_t index = 0; index < table_count; index++) { 414 415 pcf_toc_entry_t toc_entry; … … 419 420 toc_entry.size = uint32_t_le2host(toc_entry.size); 420 421 toc_entry.offset = uint32_t_le2host(toc_entry.offset); 421 422 422 423 if (toc_entry.offset >= file_size) 423 424 continue; 424 425 425 426 aoff64_t end = ((aoff64_t) toc_entry.offset) + ((aoff64_t) toc_entry.size); 426 427 if (end > file_size) 427 428 continue; 428 429 429 430 if (toc_entry.type == PCF_TABLE_BITMAPS) { 430 431 if (found_bitmap_table) … … 452 453 } 453 454 } 454 455 455 456 if (!found_bitmap_table || !found_metrics_table || 456 457 !found_encodings_table || !found_accelerators_table) 457 458 return EINVAL; 458 459 459 460 return EOK; 460 461 } … … 466 467 if (rc != 0) 467 468 return errno; 468 469 469 470 size_t records_read = fread(&format, sizeof(uint32_t), 1, data->file); 470 471 if (records_read != 1) 471 472 return EINVAL; 472 473 473 474 format = uint32_t_le2host(format); 474 475 if (format != table->format) 475 476 return EINVAL; 476 477 477 478 return EOK; 478 479 } … … 483 484 if (rc != EOK) 484 485 return rc; 485 486 486 487 if ((data->bitmap_table.format & PCF_FORMAT_MASK) != PCF_FORMAT_DEFAULT) 487 488 return EINVAL; 488 489 489 490 uint32_t glyph_count = 0; 490 491 size_t records_read = fread(&glyph_count, sizeof(uint32_t), 1, … … 503 504 if (rc != EOK) 504 505 return rc; 505 506 506 507 size_t records_read; 507 508 uint32_t metrics_count; … … 524 525 data->metrics_table.format); 525 526 } 526 527 527 528 if (metrics_count != data->glyph_count) 528 529 return EINVAL; 529 530 530 531 return EOK; 531 532 } … … 536 537 if (rc != EOK) 537 538 return rc; 538 539 539 540 pcf_encoding_t encoding; 540 541 size_t records_read = fread(&encoding, sizeof(pcf_encoding_t), 1, … … 542 543 if (records_read != 1) 543 544 return EINVAL; 544 545 545 546 encoding.min_byte1 = uint16_t_pcf2host(encoding.min_byte1, 546 547 data->encodings_table.format); … … 553 554 encoding.default_char = uint16_t_pcf2host(encoding.default_char, 554 555 data->encodings_table.format); 555 556 556 557 data->encoding = encoding; 557 558 return EOK; … … 563 564 if (rc != EOK) 564 565 return rc; 565 566 566 567 pcf_accelerators_t accelerators; 567 568 size_t records_read = fread(&accelerators, sizeof(pcf_accelerators_t), … … 569 570 if (records_read != 1) 570 571 return EINVAL; 571 572 572 573 data->font_metrics.ascender = int32_t_pcf2host(accelerators.font_ascent, 573 574 data->accelerators_table.format); … … 575 576 data->accelerators_table.format); 576 577 data->font_metrics.leading = 0; 577 578 578 579 return EOK; 579 580 } … … 585 586 if (data == NULL) 586 587 return ENOMEM; 587 588 588 589 data->file = fopen(filename, "rb"); 589 590 if (data->file == NULL) 590 591 goto read_error; 591 592 592 593 rc = pcf_read_toc(data); 593 594 if (rc != EOK) 594 595 goto error; 595 596 596 597 rc = pcf_read_bitmap_table_header(data); 597 598 if (rc != EOK) 598 599 goto error; 599 600 600 601 rc = pcf_read_metrics_table_header(data); 601 602 if (rc != EOK) 602 603 goto error; 603 604 604 605 rc = pcf_read_encodings_table_header(data); 605 606 if (rc != EOK) 606 607 goto error; 607 608 608 609 rc = pcf_read_accelerators_table(data); 609 610 if (rc != EOK) 610 611 goto error; 611 612 612 613 rc = bitmap_font_create(&fd_pcf, data, data->glyph_count, 613 614 data->font_metrics, points, font); 614 615 if (rc != EOK) 615 616 goto error; 616 617 617 618 return EOK; 618 619 read_error:
Note:
See TracChangeset
for help on using the changeset viewer.