Loads the xheight font properties file into xheights_. Returns false on failure.
| 390 | // Loads the xheight font properties file into xheights_. |
| 391 | // Returns false on failure. |
| 392 | bool MasterTrainer::LoadXHeights(const char* filename) { |
| 393 | tprintf("fontinfo table is of size %d\n", fontinfo_table_.size()); |
| 394 | xheights_.init_to_size(fontinfo_table_.size(), -1); |
| 395 | if (filename == NULL) return true; |
| 396 | FILE *f = fopen(filename, "rb"); |
| 397 | if (f == NULL) { |
| 398 | fprintf(stderr, "Failed to load font xheights from %s\n", filename); |
| 399 | return false; |
| 400 | } |
| 401 | tprintf("Reading x-heights from %s ...\n", filename); |
| 402 | FontInfo fontinfo; |
| 403 | fontinfo.properties = 0; // Not used to lookup in the table. |
| 404 | fontinfo.universal_id = 0; |
| 405 | char buffer[1024]; |
| 406 | int xht; |
| 407 | int total_xheight = 0; |
| 408 | int xheight_count = 0; |
| 409 | while (!feof(f)) { |
| 410 | if (tfscanf(f, "%1023s %d\n", buffer, &xht) != 2) |
| 411 | continue; |
| 412 | buffer[1023] = '\0'; |
| 413 | fontinfo.name = buffer; |
| 414 | if (!fontinfo_table_.contains(fontinfo)) continue; |
| 415 | int fontinfo_id = fontinfo_table_.get_index(fontinfo); |
| 416 | xheights_[fontinfo_id] = xht; |
| 417 | total_xheight += xht; |
| 418 | ++xheight_count; |
| 419 | } |
| 420 | if (xheight_count == 0) { |
| 421 | fprintf(stderr, "No valid xheights in %s!\n", filename); |
| 422 | fclose(f); |
| 423 | return false; |
| 424 | } |
| 425 | int mean_xheight = DivRounded(total_xheight, xheight_count); |
| 426 | for (int i = 0; i < fontinfo_table_.size(); ++i) { |
| 427 | if (xheights_[i] < 0) |
| 428 | xheights_[i] = mean_xheight; |
| 429 | } |
| 430 | fclose(f); |
| 431 | return true; |
| 432 | } // LoadXHeights |
| 433 | |
| 434 | // Reads spacing stats from filename and adds them to fontinfo_table. |
| 435 | bool MasterTrainer::AddSpacingInfo(const char *filename) { |
nothing calls this directly
no test coverage detected