+
+void Document::SetFont(const string & font_filename)
+{
+ if (m_font_data != NULL)
+ {
+ free(m_font_data);
+ }
+
+ FILE *font_file = fopen("DejaVuSansMono.ttf", "rb");
+ fseek(font_file, 0, SEEK_END);
+ size_t font_file_size = ftell(font_file);
+ fseek(font_file, 0, SEEK_SET);
+ m_font_data = (unsigned char*)malloc(font_file_size);
+ size_t read = fread(m_font_data, 1, font_file_size, font_file);
+ if (read != font_file_size)
+ {
+ Fatal("Failed to read font data from \"%s\" - Read %u bytes expected %u - %s", font_filename.c_str(), read, font_file_size, strerror(errno));
+ }
+ fclose(font_file);
+ stbtt_InitFont(&m_font, m_font_data, 0);
+}
+
+void Document::AddText(const string & text, Real scale, Real x, Real y)
+{
+ if (m_font_data == NULL)
+ {
+ Warn("No font loaded");
+ return;
+ }
+
+ float font_scale = stbtt_ScaleForPixelHeight(&m_font, scale);
+ Real x0(x);
+ //Real y0(y);
+ int ascent = 0, descent = 0, line_gap = 0;
+ stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &line_gap);
+ Real y_advance = Real(font_scale) * Real(ascent - descent + line_gap);
+ for (unsigned i = 0; i < text.size(); ++i)
+ {
+ if (text[i] == '\n')
+ {
+ y += y_advance;
+ x = x0;
+ }
+ if (!isprint(text[i]))
+ continue;
+
+ int advance_width = 0, left_side_bearing = 0, kerning = 0;
+ stbtt_GetCodepointHMetrics(&m_font, text[i], &advance_width, &left_side_bearing);
+ if (i > 1)
+ {
+ kerning = stbtt_GetCodepointKernAdvance(&m_font, text[i-1], text[i]);
+ }
+ x += Real(font_scale) * Real(left_side_bearing + kerning);
+ AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y);
+ x += Real(font_scale) * Real(advance_width);
+ }
+}
+
+void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y)
+{
+ int glyph_index = stbtt_FindGlyphIndex(font, character);
+
+ // Check if there is actully a glyph to render.
+ if (stbtt_IsGlyphEmpty(font, glyph_index))
+ {
+ return;
+ }
+
+ stbtt_vertex *instructions;
+ int num_instructions = stbtt_GetGlyphShape(font, glyph_index, &instructions);
+
+ Real current_x(0), current_y(0);
+
+ for (int i = 0; i < num_instructions; ++i)
+ {
+ // TTF uses 16-bit signed ints for coordinates:
+ // with the y-axis inverted compared to us.
+ // Convert and scale any data.
+ Real inst_x = Real(instructions[i].x)*scale;
+ Real inst_y = Real(instructions[i].y)*-scale;
+ Real inst_cx = Real(instructions[i].cx)*scale;
+ Real inst_cy = Real(instructions[i].cy)*-scale;
+ Real old_x(current_x), old_y(current_y);
+ current_x = inst_x;
+ current_y = inst_y;
+ unsigned bezier_index;
+ switch(instructions[i].type)
+ {
+ // Move To
+ case STBTT_vmove:
+ break;
+ // Line To
+ case STBTT_vline:
+ bezier_index = AddBezierData(Bezier(old_x + x, old_y + y, old_x + x, old_y + y, current_x + x, current_y + y, current_x + x, current_y + y));
+ Add(BEZIER,Rect(0,0,1,1),bezier_index);
+ break;
+ // Quadratic Bezier To:
+ case STBTT_vcurve:
+ // Quadratic -> Cubic:
+ // - Endpoints are the same.
+ // - cubic1 = quad0+(2/3)*(quad1-quad0)
+ // - cubic2 = quad2+(2/3)*(quad1-quad2)
+ bezier_index = AddBezierData(Bezier(old_x + x, old_y + y, old_x + Real(2)*(inst_cx-old_x)/Real(3) + x, old_y + Real(2)*(inst_cy-old_y)/Real(3) + y,
+ current_x + Real(2)*(inst_cx-current_x)/Real(3) + x, current_y + Real(2)*(inst_cy-current_y)/Real(3) + y, current_x + x, current_y + y));
+ Add(BEZIER,Rect(0,0,1,1),bezier_index);
+ break;
+ }
+ }
+
+ stbtt_FreeShape(font, instructions);
+}