X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fdocument.cpp;h=fe3482fbb34080bec15c3ac871636448f1616c9d;hp=8f35644fb69b97a1c7c6f1b4802faff8f60c0d90;hb=1ba642be477818ba73be34b5e96484b5e91d8c66;hpb=208cee1f6967db42329a3e665401ef51b3f6d9ff;ds=sidebyside diff --git a/src/document.cpp b/src/document.cpp index 8f35644..fe3482f 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -521,6 +521,53 @@ void Document::ParseSVGPathData(const string & d, const Rect & bounds) } } +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); + for (unsigned i = 0; i < text.size(); ++i) + { + if (text[i] == '\n') + { + y += 0.5*scale; + x = x0; + } + if (!isprint(text[i])) + continue; + + AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y); + x += 0.5*scale; + } +} + void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y) { int glyph_index = stbtt_FindGlyphIndex(font, character);