File: lff2c.cpp

package info (click to toggle)
solvespace 2.3%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,752 kB
  • sloc: cpp: 111,351; ansic: 493; xml: 22; sh: 12; makefile: 3
file content (410 lines) | stat: -rw-r--r-- 13,690 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#define _USE_MATH_DEFINES
#include <zlib.h>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>

#define TOLERANCE 1e-6

double correctAngle(double a) {
    return M_PI + remainder(a - M_PI, 2 * M_PI);
}

struct Point {
    double x;
    double y;

    Point operator+(const Point &o) const { return { x + o.x, y + o.y }; }
    Point operator-(const Point &o) const { return { x - o.x, y - o.y }; }
    Point operator*(const Point &o) const { return { x * o.x, y * o.y }; }
    Point operator/(const Point &o) const { return { x / o.x, y / o.y }; }

    Point operator*(double k) const { return { x * k, y * k }; }
    Point operator/(double k) const { return { x / k, y / k }; }

    double length() const{
        return sqrt(x * x + y * y);
    }

    double angle() const {
        return correctAngle(atan2(y, x));
    }

    double distanceTo(const Point &v) const {
        return (*this - v).length();
    }

    double angleTo(const Point &v) const {
        return (v - *this).angle();
    }

    static Point polar(double radius, double angle) {
        return { radius * cos(angle), radius * sin(angle) };
    }

    bool operator==(const Point &o) const { return x == o.x && y == o.y; }
    bool operator!=(const Point &o) const { return x != o.x || y != o.y; }

};

struct Curve {
    std::vector<Point> points;
};

struct Glyph {
    char32_t character;
    char32_t baseCharacter;
    std::vector<Curve> curves;

    void getHorizontalBounds(double *rminx, double *rmaxx) const {
        double minx = 0;
        double maxx = 0;
        if(!curves.empty()) {
            minx = curves[0].points[0].x;
            maxx = minx;
            for(const Curve &c : curves) {
                for(const Point &p : c.points) {
                    maxx = std::max(maxx, p.x);
                    minx = std::min(minx, p.x);
                }
            }
        }
        if(rminx) *rminx = minx;
        if(rmaxx) *rmaxx = maxx;
    }

    void getVerticalBounds(double *rminy, double *rmaxy) const {
        double miny = 0;
        double maxy = 0;
        if(!curves.empty()) {
            miny = curves[0].points[0].y;
            maxy = miny;
            for(const Curve &c : curves) {
                for(const Point &p : c.points) {
                    maxy = std::max(maxy, p.y);
                    miny = std::min(miny, p.y);
                }
            }
        }
        if(rminy) *rminy = miny;
        if(rmaxy) *rmaxy = maxy;
    }

    void getHorizontalMetrics(double *leftSideBearing, double *boundingWidth) const {
        double minx, maxx;
        getHorizontalBounds(&minx, &maxx);
        *leftSideBearing = minx;
        *boundingWidth = maxx - minx;
    }

    bool operator<(const Glyph &o) const { return character < o.character; }
};

struct Font {
    double letterSpacing;
    double wordSpacing;
    std::vector<Glyph> glyphs;

    const Glyph &findGlyph(char32_t character) {
        return *std::find_if(glyphs.begin(), glyphs.end(),
            [&](const Glyph &g) { return g.character == character; });
    }

    void getGlyphBound(double *rminw, double *rminh, double *rmaxw, double *rmaxh) {
        if(glyphs.empty()) {
            *rminw = 0.0;
            *rmaxw = 0.0;
            *rminh = 0.0;
            *rmaxh = 0.0;
            return;
        }

        glyphs[0].getHorizontalBounds(rminw, rmaxw);
        glyphs[0].getVerticalBounds(rminh, rmaxh);
        for(const Glyph &g : glyphs) {
            double minw, minh, maxw, maxh;
            g.getHorizontalBounds(&minw, &maxw);
            g.getVerticalBounds(&minh, &maxh);
            *rminw = std::min(*rminw, minw);
            *rminh = std::min(*rminh, minh);
            *rmaxw = std::max(*rmaxw, maxw);
            *rmaxh = std::max(*rmaxh, maxh);
        }
    }

    void createArc(Curve &curve, const Point &cp, double radius,
                   double a1, double a2, bool reversed) {
        if (radius < 1e-6) return;

        double aSign = 1.0;
        if(reversed) {
            if(a1 <= a2 + TOLERANCE) a1 += 2.0 * M_PI;
            aSign = -1.0;
        } else {
            if(a2 <= a1 + TOLERANCE) a2 += 2.0 * M_PI;
        }

        // Angle Step (rad)
        double da = fabs(a2 - a1);
        int numPoints = 8;
        double aStep = aSign * da / double(numPoints);

        for(int i = 0; i <= numPoints; i++) {
            curve.points.push_back(cp + Point::polar(radius, a1 + aStep * i));
        }
    }

    void createBulge(const Point &v, double bulge, Curve &curve) {
        bool reversed = bulge < 0.0;
        double alpha = atan(bulge) * 4.0;
        Point &point = curve.points.back();

        Point middle = (point + v) / 2.0;
        double dist = point.distanceTo(v) / 2.0;
        double angle = point.angleTo(v);

        // alpha can't be 0.0 at this point
        double radius = fabs(dist / sin(alpha / 2.0));
        double wu = fabs(radius*radius - dist*dist);
        double h = sqrt(wu);

        if(bulge > 0.0) {
            angle += M_PI_2;
        } else {
            angle -= M_PI_2;
        }

        if (fabs(alpha) > M_PI) {
            h *= -1.0;
        }

        Point center = Point::polar(h, angle);
        center = center + middle;

        double a1 = center.angleTo(point);
        double a2 = center.angleTo(v);
        createArc(curve, center, radius, a1, a2, reversed);
    }

    void readLff(const std::string &path) {
        gzFile lfffont = gzopen(path.c_str(), "rb");
        if(!lfffont) {
            std::cerr << path << ": gzopen failed" << std::endl;
            std::exit(1);
        }

        // Read line by line until we find a new letter:
        Glyph *currentGlyph = nullptr;
        while(!gzeof(lfffont)) {
            std::string line;
            do {
                char buf[128] = {0};
                if(!gzgets(lfffont, buf, sizeof(buf)))
                    break;
                line += buf;
            } while(line.back() != '\n');

            if(line.empty() || line[0] == '\n') {
                continue;
            } else if(line[0] == '#') {
                // This is comment or metadata.
                std::istringstream ss(line.substr(1));

                std::vector<std::string> tokens;
                while(!ss.eof()) {
                    std::string token;
                    std::getline(ss, token, ':');
                    std::istringstream(token) >> token; // trim
                    if(!token.empty())
                        tokens.push_back(token);
                }

                // If not in form of "a:b" then it's not metadata, just a comment.
                if (tokens.size() != 2)
                    continue;

                std::string &identifier = tokens[0];
                std::string &value = tokens[1];

                std::transform(identifier.begin(), identifier.end(), identifier.begin(),
                               ::tolower);
                if (identifier == "letterspacing") {
                    std::istringstream(value) >> letterSpacing;
                } else if (identifier == "wordspacing") {
                    std::istringstream(value) >> wordSpacing;
                } else if (identifier == "linespacingfactor") {
                    /* don't care */
                } else if (identifier == "author") {
                    /* don't care */
                } else if (identifier == "name") {
                    /* don't care */
                } else if (identifier == "license") {
                    /* don't care */
                } else if (identifier == "encoding") {
                    /* don't care */
                } else if (identifier == "created") {
                    /* don't care */
                }
            } else if(line[0] == '[') {
                // This is a glyph.
                size_t closingPos;
                char32_t chr = std::stoi(line.substr(1), &closingPos, 16);;
                if(line[closingPos + 1] != ']') {
                    std::cerr << "unrecognized character number: " << line << std::endl;
                    currentGlyph = nullptr;
                    continue;
                }

                glyphs.emplace_back();
                currentGlyph = &glyphs.back();
                currentGlyph->character = chr;
                currentGlyph->baseCharacter = 0;
            } else if(currentGlyph != nullptr) {
                if (line[0] == 'C') {
                    // This is a reference to another glyph.
                    currentGlyph->baseCharacter = std::stoi(line.substr(1), nullptr, 16);
                } else {
                    // This is a series of curves.
                    currentGlyph->curves.emplace_back();
                    Curve &curve = currentGlyph->curves.back();

                    std::stringstream ss(line);
                    while (!ss.eof()) {
                        std::string vertex;
                        std::getline(ss, vertex, ';');

                        std::stringstream ssv(vertex);
                        std::string coord;
                        Point p;

                        if(!std::getline(ssv, coord, ',')) continue;
                        p.x = std::stod(coord);

                        if(!std::getline(ssv, coord, ',')) continue;
                        p.y = std::stod(coord);

                        if(!std::getline(ssv, coord, ',') || coord[0] != 'A') {
                            // This is just a point.
                            curve.points.push_back(p);
                        } else {
                            // This is a point with a bulge.
                            double bulge = std::stod(coord.substr(1));
                            createBulge(p, bulge, curve);
                        }
                    }
                }
            } else {
                std::cerr << "unrecognized line: " << line << std::endl;
            }
        }
        gzclose(lfffont);
    }

    void writeCppHeader(const std::string &hName) {
        std::sort(glyphs.begin(), glyphs.end());

        std::ofstream ts(hName, std::ios::out);

        double minX, minY, maxX, maxY;
        getGlyphBound(&minX, &minY, &maxX, &maxY);

        double size  = 32766.0;
        double scale = size / std::max({ fabs(maxX), fabs(minX), fabs(maxY), fabs(minY) });

        double capHeight, ascender, descender;
        findGlyph('A').getVerticalBounds(nullptr, &capHeight);
        findGlyph('h').getVerticalBounds(nullptr, &ascender);
        findGlyph('p').getVerticalBounds(&descender, nullptr);

        // We use tabs for indentation here to make compilation slightly faster
        ts <<
        "/**** This is a generated file - do not edit ****/\n\n"
        "#ifndef __VECTORFONT_TABLE_H\n"
        "#define __VECTORFONT_TABLE_H\n"
        "\n"
        "#define PEN_UP 32767\n"
        "#define UP PEN_UP\n"
        "\n"
        "#define FONT_CAP_HEIGHT ((int16_t)" << (int)floor(capHeight * scale) << ")\n" <<
        "#define FONT_ASCENDER   ((int16_t)" << (int)floor(ascender * scale) << ")\n" <<
        "#define FONT_DESCENDER  ((int16_t)" << (int)floor(descender * scale) << ")\n" <<
        "#define FONT_SIZE       (FONT_ASCENDER-FONT_DESCENDER)\n"
        "\n"
        "struct VectorGlyph {\n"
        "\tchar32_t       character;\n"
        "\tchar32_t       baseCharacter;\n"
        "\tint            leftSideBearing;\n"
        "\tint            boundingWidth;\n"
        "\tint            advanceWidth;\n"
        "\tconst int16_t *data;\n"
        "};\n"
        "\n"
        "const int16_t VectorFontData[] = {\n"
        "\tUP, UP,\n";

        std::map<char32_t, size_t> glyphIndexes;
        size_t index = 2;
        for(const Glyph &g : glyphs) {
            ts << "\t// U+" << std::hex << g.character << std::dec << "\n";
            glyphIndexes[g.character] = index;
            for(const Curve &c : g.curves) {
                for(const Point &p : c.points) {
                    ts << "\t" << (int)floor(p.x * scale) << ", " <<
                                  (int)floor(p.y * scale) << ",\n";
                    index += 2;
                }
                ts << "\tUP, UP,\n";
                index += 2;
            }
            ts << "\tUP, UP,\n"; // end-of-glyph marker
            index += 2;
        }

        ts <<
        "};\n"
        "\n"
        "const VectorGlyph VectorFont[] = {\n"
        "\t// U+20\n"
        "\t{ 32, 0, 0, 0, " << (int)floor(wordSpacing * scale) << ", &VectorFontData[0] },\n";

        for(const Glyph &g : glyphs) {
            double leftSideBearing, boundingWidth;
            g.getHorizontalMetrics(&leftSideBearing, &boundingWidth);

            ts << "\t// U+" << std::hex << g.character << std::dec << "\n";
            ts << "\t{ " << g.character << ", "
                         << g.baseCharacter << ", "
                         << (int)floor(leftSideBearing * scale) << ", "
                         << (int)floor(boundingWidth * scale) << ", "
                         << (int)floor((leftSideBearing + boundingWidth +
                                        letterSpacing) * scale) << ", ";
            ts << "&VectorFontData[" << glyphIndexes[g.character] << "] },\n";
        }

        ts <<
        "};\n"
        "\n"
        "#undef UP\n"
        "\n"
        "#endif\n";
    }
};

int main(int argc, char** argv) {
    if(argc != 3) {
        std::cerr << "Usage: " << argv[0] << " <header out> <lff in>\n" << std::endl;
        return 1;
    }

    Font font;
    font.readLff(argv[2]);
    font.writeCppHeader(argv[1]);

    return 0;
}