Hi Chris,

I'm surely going to show my ignorance with this answer, but never
mind ;)

On Sat, Sep 21, 2013 at 05:45:10AM -0700, [email protected] wrote:
> Internally, for the fonts that Tesseract "knows" about, does it store any sort
> of graphical representation of the glyphs in the font (vector or raster), or
> are fonts stored in more of a machine-learning-graph-tree sort of thing?

I looked into this a while ago. I don't have the time to re-look
now, so I may be misremembering things slightly. The character
shapes are stored in the .tr files, I believe there are multiple
samples per character, but each one is somewhat 'normalised'.

> In other words, is there a way to ask Tesseract to tell me (for example) what 
> a
> capital Q looks like in the URW Bookman font?

I tried to do exactly this, but ran out of spare time to get it
working. I'll attach it here, but do note that it's an
embarassing mess and it doesn't work. The saving grace is that as
it's written in C I know most people on the list won't dare to try
to read it anyway :) Oh, also the program for now only works on a
selection of a .tr file - see the comments around line 58.

Hope this helps. Do share anything you find out or make in this
direction, if you can.

Nick

-- 
-- 
You received this message because you are subscribed to the Google
Groups "tesseract-ocr" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/tesseract-ocr?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
/*
 * Copyright 2012 Nick White <[email protected]>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * Version 0.0
 *
 * This will read a .tr file and draw the shape of it.
 *
 * Build it with something like this:
 * cc `pkg-config --cflags --libs MagickWand` drawtrshape.c -o drawtrshape
 */

#define usage "drawtrshape - overlays an image with box information\n" \
              "usage: drawtrshape in.tr out.png\n"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <wand/MagickWand.h>

#define scaleup 200.0
#define startpoint 0.5

typedef struct {
	double x;
	double y;
	double length;
	double dir;	
} Feature;

int main(int argc, char *argv[]) {
	char buf[BUFSIZ];
	unsigned int i;
	double x1, y1, x2, y2, radians;
	FILE *tr;
	Feature *features = NULL;
	Feature *f = NULL;
	unsigned int featlen;

	MagickWand *wand;
	DrawingWand *dwand;
	PixelWand *pwand;

	if(argc != 3) {
		fputs(usage, stdout);
		return 1;
	}

	if((tr = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "Can't open tr file: %s\n", argv[1]);
		return 1;
	}

	/* parse tr file into Feature struct */
	/* NOTE: for now this isn't a real .tr file, just a set of the mf features lines.
	 *       it will need to handle all characters, choosing the one I want, or outputting
	 *       them all in a row or something.
	 *       not segfaulting on non feature lines would also be a bonus. */
	int tmp1, tmp2;
	for(i=0; !feof(tr); i++) {
		features = realloc(features, sizeof(*features) * (i + 1));
                f = &(features[i]);
                fgets(buf, sizeof(buf) - 1, tr);
                sscanf(buf, " %lf %lf %lf %lf %d %d",
                       &(f->x), &(f->y), &(f->length), &(f->dir), &tmp1, &tmp2);
        }
	featlen = i;

	fclose(tr);

	MagickWandGenesis();
	wand = NewMagickWand();
	pwand = NewPixelWand();
	dwand = NewDrawingWand();
	
	PixelSetColor(pwand,"white");
	MagickNewImage(wand,scaleup*2,scaleup*2,pwand);

	PixelSetColor(pwand,"blue");
	/* scaling up is needed as imagemagick expects pixel coordinates,
	 * whereas tesseract uses -1 to +1. */
	DrawScale(dwand, scaleup, scaleup);
	DrawSetStrokeColor(dwand,pwand);
	DrawSetStrokeOpacity(dwand,0.5);
	DrawSetStrokeWidth(dwand,0.1);
	DrawSetFillOpacity(dwand,0);
	PushDrawingWand(dwand);

	for(i=0; i<featlen; i++) {
		/* dir probably works like this: 0 is straight down, 1 is straight up, 0.5 is horizontal.
                 * and it's always going right */

		radians = (features[i].dir - 0.5) * 2; /* convert angle to radians */
		x2 = features[i].x+startpoint + (features[i].length * cos(radians));
		y2 = features[i].y+startpoint + (features[i].length * sin(radians));

		x1 = features[i].x+startpoint;
		y1 = features[i].y+startpoint;

		DrawLine(dwand, x1, y1, x2, y2);
	}

	PopDrawingWand(dwand);
	MagickDrawImage(wand,dwand);

	if(MagickWriteImages(wand, argv[2], MagickTrue) == MagickFalse)
		fprintf(stderr, "Error writing image file\n");

	wand = DestroyMagickWand(wand);
	dwand = DestroyDrawingWand(dwand);
	pwand = DestroyPixelWand(pwand);
	MagickWandTerminus();

	return 0;
}

Reply via email to