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