/* Stroke SVG from M+ SVG */
/* note: mplus_outline_fonts/svg.d/_works/*.ai to SVG convert with Scribus */

/* build: gcc `pkg-config libxml-2.0 --cflags` mplusstroketosvg.c mplusstroke.c `pkg-config libxml-2.0 --libs` -lm -o mplusstroketosvg */

#include "mplusstroke.h"

#include <stdio.h>

MPLUS_GlyphsData_t g_MPLUS_GlyphsData = {0};

void PrintSVG(const MPLUS_GlyphsData_t* i_ptGlyphsData) {
	unsigned int uiIndex;
	unsigned int uiItem;
	MPLUS_Glyph_t* ptGlyph;
	MPLUS_Item_t* ptItem;

	printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	printf("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"5000pt\" height=\"10900pt\" viewBox=\"0 0 5000 10900\">\n");

	for(uiIndex = 0; uiIndex < i_ptGlyphsData->glyphscount; uiIndex++) {
		ptGlyph = &(i_ptGlyphsData->glyphs[uiIndex]);

		printf("<!-- UTF16:%04x UTF8:%06x item:%d -->\n", ptGlyph->code, ptGlyph->code8, ptGlyph->itemscount);
		printf("<path style=\"fill:none;stroke:#000000; stroke-width:1; stroke-linecap:butt; stroke-linejoin:miter; stroke-dasharray:none;\" d=\"");

		for(uiItem = 0; uiItem < ptGlyph->itemscount; uiItem++) {
			ptItem = &(ptGlyph->items[uiItem]);
			switch(ptItem->top.ItemType) {
				case MPLUS_ITEMTYPE_LINE:
					printf("M%.4f %.4f L%.4f %.4f ", ptItem->line.pos1.x, ptItem->line.pos1.y, ptItem->line.pos2.x, ptItem->line.pos2.y);
					break;
				case MPLUS_ITEMTYPE_CUBIC:
					printf("M%.4f %.4f C%.4f %.4f %.4f %.4f %.4f %.4f ", ptItem->cubic.pos1.x, ptItem->cubic.pos1.y, ptItem->cubic.pos2.x, ptItem->cubic.pos2.y, ptItem->cubic.pos3.x, ptItem->cubic.pos3.y, ptItem->cubic.pos4.x, ptItem->cubic.pos4.y);
					break;
			}
		}

		printf("\" transform=\"translate(%d, %d)\"/>\n", (uiIndex % 50) * 100, (uiIndex / 50) * 100);
	}

	printf("</svg>\n");
}

void main(void) {
	LoadMPLUSStroke(&g_MPLUS_GlyphsData);
	PrintSVG(&g_MPLUS_GlyphsData);
}
