Skip to content

Commit

Permalink
glyph geometry in a more usable form
Browse files Browse the repository at this point in the history
  • Loading branch information
kv01 committed Dec 4, 2021
1 parent 8a95cba commit 065fcd9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 116 deletions.
11 changes: 0 additions & 11 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Kaushik Viswanathan
Copyright (c) 2021 kv01

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# ttf-parser
A bare-bones, single header file ttf font parser written in C++ for fast GPU font rendering.
A bare-bones, single header file ttf font parser for font rendering.

## How to use
* Define TTF_FONT_PARSER_IMPLEMENTATION in ONE cpp file to enable the implementation in the header file.
* Use *parse_file* or *parse_data* to get a *FontData* structure with all font metrics and glyph data needed for rendering common fonts.
* Note that *parse_file* is currently synchronous except when compiled with emscripten but will still execute the callback
* *parse_file* is currently synchronous except when compiled with emscripten but will still execute the callback

A glyph is represented as a set of triangles (p_x, p1, p2) where p_x is the center of the glyph and
p1 and p2 are sequential points on the curve. Quadratic splines will have 2 tiangles associated with them,
(p_x, p1, p2) as before and (p1, p_c, p2) where p_c is the spline control point.
Glyph geometry is a set of lines and quadratic curves
5 changes: 0 additions & 5 deletions TODO.md

This file was deleted.

30 changes: 24 additions & 6 deletions examples/parseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,42 @@
*/

#include <stdio.h>
#include <cwchar>
#include <chrono>
#include <thread>

#define TTF_FONT_PARSER_IMPLEMENTATION
#include "../src/ttfParser.h"

/*
* Change to a valid font location
*/
//Change to a valid font location
const char* font_path = "test/fonts/cm-unicode/cmunrm.ttf";

void font_parsed(void* args, void* _font_data, int error) {
if (error) {
*(uint8_t*)args = error;
printf("Unable to parse font\n");
printf("Unable to parse font %s\n", font_path);
}
else {
TTFFontParser::FontData* font_data = (TTFFontParser::FontData*)_font_data;
printf("Font: %s %s parsed\n", font_data->font_names.begin()->font_family.c_str(), font_data->font_names.begin()->font_style.c_str());
printf("Number of glyphs: %d\n", font_data->glyphs.size());
printf("Number of glyphs: %d\n", uint32_t(font_data->glyphs.size()));

//step through glyph geometry
{
for (const auto& glyph_iterator : font_data->glyphs) {
uint32_t num_curves = 0, num_lines = 0;
for (const auto& path_list : glyph_iterator.second.path_list) {
for (const auto& geometry : path_list.geometry) {
if (geometry.is_curve)
num_curves++;
else
num_lines++;
}
}
wprintf(L"glyph 0x%x %lc: %d quadratic curves and %d lines\n", glyph_iterator.first, glyph_iterator.first, num_curves, num_lines);
}
}

*(uint8_t*)args = 1;
}
}
Expand All @@ -39,4 +55,6 @@ int main() {
}

getchar();
}

return 0;
}
Loading

0 comments on commit 065fcd9

Please sign in to comment.