version 2.8
exporttools.hh
1 // Utility functions for exporting to C source code files.
2 
3 #pragma once
4 #include <string>
5 #include <vector>
6 #include <iostream>
7 #include <functional>
8 #include "datafile.hh"
9 
10 namespace mcufont {
11 
12 // Convert a file name to a valid C identifier
13 std::string filename_to_identifier(std::string name);
14 
15 // Write a vector of integers as line-wrapped hex/integer data for initializing const array.
16 void wordwrap_vector(std::ostream &out, const std::vector<unsigned> &data,
17  const std::string &prefix, size_t width = 2);
18 
19 // Write a vector of integers as a C constant array of given datatype.
20 void write_const_table(std::ostream &out, const std::vector<unsigned> &data,
21  const std::string &datatype, const std::string &tablename,
22  size_t width = 2);
23 
24 // Get minimum tracking width of font
25 int get_min_x_advance(const DataFile &datafile);
26 
27 // Get maximum tracking width of font
28 int get_max_x_advance(const DataFile &datafile);
29 
30 // Select the character to use as a fallback.
31 int select_fallback_char(const DataFile &datafile);
32 
33 // Structure to represent one consecutive range of characters.
34 struct char_range_t
35 {
36  uint16_t first_char;
37  uint16_t char_count;
38  std::vector<int> glyph_indices;
39 
40  char_range_t(): first_char(0), char_count(0) {}
41 };
42 
43 // Decide how to best divide the characters in the font into ranges.
44 // Limitations are:
45 // - Gaps longer than minimum_gap should result in separate ranges.
46 // - Each range can have encoded data size of at most maximum_size.
47 std::vector<char_range_t> compute_char_ranges(const DataFile &datafile,
48  std::function<size_t(size_t)> get_encoded_glyph_size,
49  size_t maximum_size,
50  size_t minimum_gap);
51 
52 }