Index: Plug.tmpasm =================================================================== --- Plug.tmpasm (revision 20255) +++ Plug.tmpasm (revision 20256) @@ -1,5 +1,8 @@ put /local/pcb/mod {import_ttf} -put /local/pcb/mod/OBJS [@ $(PLUGDIR)/import_ttf/ttf.o @] +put /local/pcb/mod/OBJS [@ + $(PLUGDIR)/import_ttf/ttf.o + $(PLUGDIR)/import_ttf/ttf_load.o +@] switch /local/pcb/export_png/controls case {disable} end; Index: ttf.c =================================================================== --- ttf.c (revision 20255) +++ ttf.c (revision 20256) @@ -30,12 +30,6 @@ #include #include -#include -#include -#include -#include -#include -#include #include "board.h" #include "data.h" @@ -44,6 +38,8 @@ #include "plugins.h" #include "hid.h" +#include "ttf_load.h" + static const char *ttf_cookie = "ttf importer"; static const char pcb_acts_LoadTtfGlyphs[] = "LoadTtfGlyphs(filename, srcglyps, [dstchars])"; Index: ttf_load.c =================================================================== --- ttf_load.c (nonexistent) +++ ttf_load.c (revision 20256) @@ -0,0 +1,107 @@ +/* + * COPYRIGHT + * + * pcb-rnd, interactive printed circuit board design + * + * ttf low level loader + * pcb-rnd Copyright (C) 2018 Tibor 'Igor2' Palinkas + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Contact: + * Project page: http://repo.hu/projects/pcb-rnd + * lead developer: email to pcb-rnd (at) igor2.repo.hu + * mailing list: pcb-rnd (at) list.repo.hu (send "subscribe") + */ + +#include +#include +#include +#include +#include +#include "ttf_load.h" + +# undef __FTERRORS_H__ +# define FT_ERRORDEF( e, v, s ) { e, s }, +# define FT_ERROR_START_LIST { +# define FT_ERROR_END_LIST { 0, 0 } }; + static const struct { + FT_Error errnum; + const char *errstr; + } ft_errtab[] = +# include FT_ERRORS_H + +static const char *int_errtab[] = { + "success", + "Need an outline font" +}; + +int pcb_ttf_load(pcb_ttf_t *ctx, const char *fn) +{ + FT_Error errnum; + + errnum = FT_Init_FreeType(&ctx->library); + if (errnum == 0) + errnum = FT_New_Face(ctx->library, fn, 0, &ctx->face); + return errnum; +} + +int pcb_ttf_unload(pcb_ttf_t *ctx) +{ + FT_Done_Face(ctx->face); + FT_Done_Library(ctx->library); + memset(&ctx, 0, sizeof(pcb_ttf_t)); +} + + +FT_Error pcb_ttf_trace(pcb_ttf_t *ctx, FT_ULong ttf_chr, FT_ULong out_chr, pcb_ttf_stroke_t *str) +{ + FT_Error err; + FT_Glyph gly; + FT_OutlineGlyph ol; + + err = FT_Load_Glyph(ctx->face, FT_Get_Char_Index(ctx->face, ttf_chr), FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE); + if (err != 0) + return err; + + FT_Get_Glyph(ctx->face->glyph, &gly); + ol = (FT_OutlineGlyph)gly; + if (ctx->face->glyph->format != ft_glyph_format_outline) + return -1; /* this method supports only outline font */ + + str->start(str, out_chr); + err = FT_Outline_Decompose(&(ol->outline), &str->funcs, str); + str->finish(str); + + if (err != 0) + return err; + + return 0; +} + +const char *pcb_ttf_errmsg(FT_Error errnum) +{ + if (errnum > 0) { + if (errnum < sizeof(ft_errtab) / sizeof(ft_errtab[0])) + return ft_errtab[errnum].errstr; + return "Invalid freetype2 error code."; + } + + errnum = -errnum; + if (errnum < sizeof(int_errtab) / sizeof(int_errtab[0])) + return int_errtab[errnum]; + + return "Invalid internal error code."; +} Index: ttf_load.h =================================================================== --- ttf_load.h (nonexistent) +++ ttf_load.h (revision 20256) @@ -0,0 +1,66 @@ +/* + * COPYRIGHT + * + * pcb-rnd, interactive printed circuit board design + * + * ttf low level loader + * pcb-rnd Copyright (C) 2018 Tibor 'Igor2' Palinkas + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Contact: + * Project page: http://repo.hu/projects/pcb-rnd + * lead developer: email to pcb-rnd (at) igor2.repo.hu + * mailing list: pcb-rnd (at) list.repo.hu (send "subscribe") + */ + +#ifndef PCB_TTF_LOAD_H +#define PCB_TTF_LOAD_H + +#include +#include + +typedef struct pcb_ttf_s { + FT_Library library; + FT_Face face; +} pcb_ttf_t; + +/* Stroker backend; this low level lib does not know how to draw anything but + calls back the stroker for outline objects */ +typedef struct pcb_ttf_stroke_s pcb_ttf_stroke_t; +struct pcb_ttf_stroke_s { + FT_Outline_Funcs funcs; + void (*init)(pcb_ttf_stroke_t *s); + void (*start)(pcb_ttf_stroke_t *s, int chr); + void (*finish)(pcb_ttf_stroke_t *s); + void (*uninit)(pcb_ttf_stroke_t *s); + + double x, y; + double dx, dy, scale_x, scale_y; +}; + +/* Load the ttf font from fn; return 0 on success */ +FT_Error pcb_ttf_load(pcb_ttf_t *ttf, const char *fn); + +int pcb_ttf_unload(pcb_ttf_t *ctx); + +/* Use str to trace the outline of a glyph; returns 0 on success */ +FT_Error pcb_ttf_trace(pcb_ttf_t *ctx, FT_ULong ttf_chr, FT_ULong out_chr, pcb_ttf_stroke_t *str); + +/* Convert an error code into a human readable error message */ +const char *pcb_ttf_errmsg(FT_Error errnum); + + +#endif