X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fdocument.cpp;h=ead1b8e6bda4427a7f6171469df1da7833e1e51c;hp=3f00e64e5fed549a6cad39f8cfa36b9c2fc5813b;hb=813591a7d8a7364003233939f52b0031f3a40d20;hpb=0ce8df17fae3ec986bd9570e5552aed92c080088 diff --git a/src/document.cpp b/src/document.cpp index 3f00e64..ead1b8e 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -130,8 +130,27 @@ int Document::ClipObjectToQuadChild(int object_id, QuadTreeNodeChildren type) m_objects.data_indices.push_back(m_objects.data_indices[object_id]); return 1; } - case GROUP: - break; + case BEZIER: + { + Rect child_node_bounds = TransformFromQuadChild({0,0,1,1}, type); + Rect clip_bezier_bounds; + clip_bezier_bounds.x = (child_node_bounds.x - m_objects.bounds[object_id].x) / m_objects.bounds[object_id].w; + clip_bezier_bounds.y = (child_node_bounds.y - m_objects.bounds[object_id].y) / m_objects.bounds[object_id].h; + clip_bezier_bounds.w = child_node_bounds.w / m_objects.bounds[object_id].w; + clip_bezier_bounds.h = child_node_bounds.h / m_objects.bounds[object_id].h; + std::vector new_curves = m_objects.beziers[m_objects.data_indices[object_id]].ClipToRectangle(clip_bezier_bounds); + for (size_t i = 0; i < new_curves.size(); ++i) + { + Rect new_bounds = TransformToQuadChild(m_objects.bounds[object_id], type); + //new_bounds = TransformToQuadChild(new_bounds, type); + //Bezier new_curve_data = new_curves[i].ToRelative(new_bounds); + unsigned index = AddBezierData(new_curves[i]); + m_objects.bounds.push_back(new_bounds); + m_objects.types.push_back(BEZIER); + m_objects.data_indices.push_back(index); + } + return new_curves.size(); + } default: Debug("Adding %s -> %s", m_objects.bounds[object_id].Str().c_str(), TransformToQuadChild(m_objects.bounds[object_id], type).Str().c_str()); m_objects.bounds.push_back(TransformToQuadChild(m_objects.bounds[object_id], type)); @@ -545,6 +564,24 @@ void Document::ParseSVGNode(pugi::xml_node & root, SVGMatrix & parent_transform) } } +/** + * Parse an SVG string into a rectangle + */ +void Document::ParseSVG(const string & input, const Rect & bounds) +{ + using namespace pugi; + + xml_document doc_xml; + xml_parse_result result = doc_xml.load(input.c_str()); + + if (!result) + Error("Couldn't parse SVG input - %s", result.description()); + + Debug("Loaded XML - %s", result.description()); + SVGMatrix transform = {bounds.w, 0,bounds.x, 0,bounds.h,bounds.y}; + ParseSVGNode(doc_xml, transform); +} + /** * Load an SVG into a rectangle */ @@ -557,7 +594,7 @@ void Document::LoadSVG(const string & filename, const Rect & bounds) xml_parse_result result = doc_xml.load(input); if (!result) - Fatal("Couldn't load \"%s\" - %s", filename.c_str(), result.description()); + Error("Couldn't load \"%s\" - %s", filename.c_str(), result.description()); Debug("Loaded XML - %s", result.description()); @@ -758,7 +795,7 @@ void Document::SetFont(const string & font_filename) free(m_font_data); } - FILE *font_file = fopen("DejaVuSansMono.ttf", "rb"); + FILE *font_file = fopen(font_filename.c_str(), "rb"); fseek(font_file, 0, SEEK_END); size_t font_file_size = ftell(font_file); fseek(font_file, 0, SEEK_SET); @@ -780,11 +817,11 @@ void Document::AddText(const string & text, Real scale, Real x, Real y) 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); + float font_scale = scale / (float)(ascent - descent); Real y_advance = Real(font_scale) * Real(ascent - descent + line_gap); for (unsigned i = 0; i < text.size(); ++i) { @@ -802,7 +839,6 @@ void Document::AddText(const string & text, Real scale, Real x, Real y) { kerning = stbtt_GetCodepointKernAdvance(&m_font, text[i-1], text[i]); } - Debug("%c: lsb %d, kern %d, adv_width %d", text[i], left_side_bearing, kerning, advance_width); x += Real(font_scale) * Real(kerning); AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y); x += Real(font_scale) * Real(advance_width); @@ -863,6 +899,7 @@ void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real sca { AddGroup(start_index, end_index); } + Debug("Added Glyph \"%c\" at %f %f, scale %f", (char)character, Float(x), Float(y), Float(scale)); stbtt_FreeShape(font, instructions); }