SVG text and line elements
[ipdf/code.git] / src / document.cpp
index 8f35644..f23b406 100644 (file)
@@ -291,6 +291,15 @@ void Document::ParseSVGNode(pugi::xml_node & root, const Rect & bounds, Real & w
                        Debug("Path data attribute is \"%s\"", d.c_str());
                        ParseSVGPathData(d, Rect(bounds.x,bounds.y,width,height));
                }
+               else if (strcmp(child.name(), "line") == 0)
+               {
+                       Real x0(child.attribute("x1").as_float()/width + bounds.x);
+                       Real y0(child.attribute("y1").as_float()/height + bounds.y);
+                       Real x1(child.attribute("x2").as_float()/width + bounds.x);
+                       Real y1(child.attribute("y2").as_float()/height + bounds.y);
+                       unsigned index = AddBezierData(Bezier(x0,y0,x1,y1,x1,y1,x1,y1));
+                       Add(BEZIER, Rect(0,0,1,1), index);
+               }
                else if (strcmp(child.name(), "rect") == 0)
                {
                        Real coords[4];
@@ -316,6 +325,13 @@ void Document::ParseSVGNode(pugi::xml_node & root, const Rect & bounds, Real & w
                        Add(CIRCLE_FILLED, rect,0);
                        Debug("Added Circle %s", rect.Str().c_str());                   
                }
+               else if (strcmp(child.name(), "text") == 0)
+               {
+                       Real x = child.attribute("x").as_float()/width + bounds.x;
+                       Real y = child.attribute("y").as_float()/height + bounds.y;
+                       Debug("Add text \"%s\"", child.child_value());
+                       AddText(child.child_value(), 0.05, x, y);
+               }
        }
 }
 
@@ -521,6 +537,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);

UCC git Repository :: git.ucc.asn.au