/**
@file text_reassemble.c
\verbatim
Method:
1. For all ordered text objects which are sequential and share the same esc.
2. For the first only pull x,y,esc and save, these define origin and rotation.
3. Save the text object.
4. Phase I: For all saved text objects construct lines.
5. Check for allowed overlaps on sequential saved text object bounding rectangles.
6 If found merge second with first, check next one.
7. If not found, start a new complex (line).
8. Phase II; for all lines construct paragraphs.
9. Check alignment and line spacing of preceding line with current line.
10. if alignment is the same, and line spacing is compatible merge current line into
current paragraph. Reaverage line spacing over all lines in paragraph. Check next one.
11. If alignment does not match start a new paragraph.
(Test program)
12. Over all phase II paragraphs
13. Over all phase I lines in each paragraph.
14. Over all text objects in each line.
Emit SVG corresponding to this construct to a file dump.svg.
(Conversion to other file types would be modeled on this example.)
15. Clean up.
(General program)
Like for the Test program, but final representation may not be SVG.
Text object and bounding rectangle memory would all be released. If another set of
text will be processed then hang onto both Freetype and Fontconfig structures. If no
other text will be processed here, then also release Freetype structures. If the caller uses
Fontconfig elsewhere then do not release it, otherwise, do so.
NOTE ON COORDINATES: x is positive to the right, y is positive down. So (0,0) is the upper left corner, and the
lower left corner of a rectangle has a LARGER Y coordinate than the upper left. Ie, LL=(10,10) UR=(30,5) is typical.
\endverbatim
*/
/*
Compilation of test program (with all debugging output, but not loop testing):
On Windows use:
gcc -Wall -DWIN32 -DTEST -DDBG_TR_PARA -DDBG_TR_INPUT \
-o text_reassemble text_reassemble.c uemf_utf.c \
On Linux use:
gcc -Wall -DTEST -DDBG_TR_PARA -DDBG_TR_INPUT -I. -I/usr/include/freetype2 -o text_reassemble text_reassemble.c uemf_utf.c -lfreetype -lfontconfig -lm
Compilation of object file only (Windows):
gcc -Wall -DWIN32 -c \
Compilation of object file only (Linux):
gcc -Wall -c -I. -I/usr/include/freetype2 text_reassemble.c
Optional compiler switches for development:
-DDBG_TR_PARA draw bounding rectangles for paragraphs in SVG output
-DDBG_TR_INPUT draw input text and their bounding rectangles in SVG output
-DTEST build the test program
-DDBG_LOOP force the test program to cycle 5 times. Useful for finding
memory leaks. Ouput file is overwritten each time.
File: text_reassemble.c
Version: 0.0.18
Date: 11-MAR-2016
Author: David Mathog, Biology Division, Caltech
email: mathog@caltech.edu
Copyright: 2016 David Mathog and California Institute of Technology (Caltech)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "text_reassemble.h"
#include <locale.h>
#include <float.h>
/* Code generated by make_ucd_mn_table.c using:
cat mnlist.txt | ./make_ucd_mn_table >generated.c
*/
#include <stdint.h>
0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x06,
0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0B};
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
0x0E, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x00, 0x00,
0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x19, 0x00, 0x00,
0x1A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x1C, 0x1D, 0x1E, 0x1F, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x21, 0x00,
0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00,
0x24, 0x25, 0x00, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27,
0x00, 0x28, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000FFFF, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000F8, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFE0000, 0xBFFFFFFF, 0x000000B6, 0x00000000,
0x07FF0000, 0x00000000, 0xFFFFF800, 0x00010000, 0x00000000, 0x00000000, 0x9FC00000, 0x00003D9F,
0x00020000, 0xFFFF0000, 0x000007FF, 0x00000000, 0x00000000, 0x0001FFC0, 0x00000000, 0x000FF800,
0xFBC00000, 0x00003EEF, 0x0E000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x7FFFFFF0,
0x00000007, 0x14000000, 0x00FE21FE, 0x0000000C, 0x00000002, 0x10000000, 0x0000201E, 0x0000000C,
0x00000006, 0x10000000, 0x00023986, 0x00230000, 0x00000006, 0x10000000, 0x000021BE, 0x0000000C,
0x00000002, 0x90000000, 0x0040201E, 0x0000000C, 0x00000004, 0x00000000, 0x00002001, 0x00000000,
0x00000000, 0xC0000000, 0x00603DC1, 0x0000000C, 0x00000000, 0x90000000, 0x00003040, 0x0000000C,
0x00000000, 0x00000000, 0x0000201E, 0x0000000C, 0x00000000, 0x00000000, 0x005C0400, 0x00000000,
0x00000000, 0x07F20000, 0x00007F80, 0x00000000, 0x00000000, 0x1BF20000, 0x00003F00, 0x00000000,
0x03000000, 0x02A00000, 0x00000000, 0x7FFE0000, 0xFEFFE0DF, 0x1FFFFFFF, 0x00000040, 0x00000000,
0x00000000, 0x66FDE000, 0xC3000000, 0x001E0001, 0x20002064, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0xE0000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x001C0000, 0x001C0000, 0x000C0000, 0x000C0000, 0x00000000, 0x3FB00000, 0x200FFE40, 0x00000000,
0x00003800, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000200, 0x00000000, 0x00000000,
0x00000000, 0x0E040187, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x01800000, 0x00000000, 0x7F400000, 0x9FF81FE5, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x0000000F, 0x17D00000, 0x00000004, 0x000FF800, 0x00000003, 0x00000B3C, 0x00000000, 0x0003A340,
0x00000000, 0x00CFF000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFF70000, 0x001021FD,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xF000007F,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x1FFF0000, 0x0001FFE2,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00038000,
0x00000000, 0x00000000, 0x00000000, 0x80000000, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFF,
0x00000000, 0x00003C00, 0x00000000, 0x00000000, 0x06000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x3FF08000, 0x80000000, 0x00000000, 0x00000000, 0x00030000,
0x00000844, 0x00000060, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000010, 0x0003FFFF,
0x00000000, 0x00003FC0, 0x0003FF80, 0x00000000, 0x00000007, 0x13C80000, 0x00000000, 0x00000000,
0x00000000, 0x00667E00, 0x00001008, 0x00000000, 0x00000000, 0xC19D0000, 0x00000002, 0x00403000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00002120,
0x40000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x0000FFFF, 0x0000007F, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000,
0x0000F06E, 0x87000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000002, 0xFF000000, 0x0000007F, 0x00000000, 0x00000003, 0x06780000, 0x00000000, 0x00000000,
0x00000007, 0x001FEF80, 0x00000000, 0x00000000, 0x00000003, 0x7FC00000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00BF2800, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00078000, 0x00000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0xF8000380, 0x00000FE7, 0x00003C00, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x0000001C, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000FFFF};
int result=0;
int spage_idx;
if(test<MN_TEST_LIMIT){
}
return(result);
}
/**
\brief Find a (sub)string in a caseinvariant manner, used for locating "Narrow" in font name
\return Returns -1 if no match, else returns the position (numbered from 0) of the first character of the match.
\param string Text to search
\param sub Text to find
*/
int i,j;
int match=0;
for(i=0; string[i]; i++){
match=0;
break;
}
}
}
return((match ? i : -1));
}
/**
\brief Constrouct a fontspec from a TCHUNK_SPECS and a fontname
\return Returns NULL on error, new fontspec on success
\param tsp pointer to TCHUNK_SPECS to use for information
\param fontname Fontname to use in the new fontspec
*/
/* construct a font name */
sprintf(newfs,"%s:slant=%d:weight=%d:size=%f:width=%d",fontname,tsp->italics,tsp->weight,tsp->fs,(tsp->co ? 75 : tsp->condensed));
return(newfs);
}
/**
\brief Reconstrouct a fontspec by substituting a font name into an existing spec
\return Returns NULL on error, new fontspec on success
\param fontspec Original fontspec, only the name will be changed
\param fontname Fontname to substitute into the new fontspec
*/
/* construct a font name */
int colon;
return(newfs);
}
/**
\brief Find a font in the list that has a glyph for this character, change alternate to match
\return Returns 0 if no match or an error, else returns the glyph index in the new alternate font
\param fti pointer to the FT_INFO structure, may be modified if alternate font is added
\param efsp Pointer to a Pointer to the original FNT_SPECS struct. On return contains the FNT_SPECS corresponding to the glyph_index..
\param wc Current character (32 bit int)
*/
uint32_t i;
char *filename;
char *fontname;
char *newfontspec;
int fi_idx;
glyph_index = FT_Get_Char_Index( fsp2->face, wc); /* we have the face, might as well check that directly */
if (glyph_index){ /* found a glyph for the character in this font */
(void) fsp_alts_weight(fsp, i);
return(glyph_index);
}
}
/* it was not in alts, now go through fontset and see if it is in there */
for(i=1; i< (unsigned int) fsp->fontset->nfont;i++){ /* already know the primary does not have this character */
glyph_index = i;
/* Do a lot of work to find the filename corresponding to the fontset entry.
None of these should ever fail, but if one does, return 0
*/
if(
)return(0);
if(
(FcPatternGetString( fsp->fontset->fonts[i], FC_FULLNAME, 0, (FcChar8 **)&fontname) != FcResultMatch)
)return(0);
/* find the font (added from an unrelated fontset, for instance) or insert it as new */
if(fi_idx < 0){
if(fi_idx < 0)return(0); /* This could happen if we run out of memory*/
}
/* add the new font index to the alts list on the (current) fsp. */
/* release FC's own memory related to this call that does not need to be kept around so that face will work */
return(glyph_index);
}
}
return(0);
}
/**
\brief Get the advance for the 32 bit character
\return Returns -1 on error, or advance in units of 1/64th of a Point.
\param fti pointer to the FT_INFO structure, may be modified if alternate font is required
\param fsp Pointer to FNT_SPECS struct.
\param wc Current character (32 bit int)
\param pc Previous character
\param load_flags Controls internal advance:
FT_LOAD_NO_SCALE, internal advance is in 1/64th of a point. (kerning values are still scaled)
FT_LOAD_TARGET_NORMAL internal advance is in 1/64th of a point. The scale
factor seems to be (Font Size in points)*(DPI)/(32.0 pnts)*(72 dpi).
\param kern_mode FT_KERNING_DEFAULT, FT_KERNING_UNFITTED, or FT_KERNING_UNSCALED. Set to match calling application.
\param ymin If the pointer is defined, the value is adjusted if ymin of wc character is less than the current value.
\param ymax If the pointer is defined, the value is adjusted if ymin of wc character is more than the current value.
*/
int TR_getadvance(FT_INFO *fti, FNT_SPECS *fsp, uint32_t wc, uint32_t pc, int load_flags, int kern_mode, int *ymin, int *ymax){
int glyph_index;
if(!glyph_index){ /* not in primary font, check alternates */
}
if(glyph_index){
}
}
}
/* If there was no way to determine the width, this returns the error value */
return(advance);
}
/**
\brief Get the kerning for a pair of 32 bit characters
\return Returns 0 on error, or kerning value (which may be 0) for the pair in units of 1/64th of a point.
\param fsp Pointer to FNT_SPECS struct.
\param wc Current character (32 bit int)
\param pc Previous character
\param kern_mode FT_KERNING_DEFAULT, FT_KERNING_UNFITTED, or FT_KERNING_UNSCALED. Set to match calling application.
*/
int this_glyph_index;
int prev_glyph_index;
int kern=0;
&akerning )){
}
return(kern);
}
/**
\brief Get the kerning for a pair of 32 bit characters, where one is the last character in the previous text block, and the other is the first in the current text block.
\return Returns 0 on error, or kerning value (which may be 0) for the pair in units of 1/64th of a point.
\param fsp Pointer to FNT_SPECS struct.
\param tsp current text object
\param ptsp previous text object
\param kern_mode FT_KERNING_DEFAULT, FT_KERNING_UNFITTED, or FT_KERNING_UNSCALED. Set to match calling application.
*/
int kern=0;
if(!text32){ // LATIN1 encoded >128 are generally not valid UTF, so the first will fail
if(!text32)break;
}
if(!ptxt32){ // LATIN1 encoded >128 are generally not valid UTF, so the first will fail
if(!ptxt32)break;
}
break;
}
return(kern);
}
/**
\brief Find baseline on Y axis of a complex.
If the complex is a TR_TEXT or TR_LINE find its baseline.
If the complex is TR_PARA_[UCLR]J find the baseline of the last line.
If there are multiple text elements in a TR_LINE, the baseline is that of the
element that uses the largest font. This will definitely give the wrong
result if that line starts with a super or subscript that is full font size, but
they are usually smaller.
\return Returns 0 if it cannot determine a baseline, else returns the baseline Y coordinate.
\param tri pointer to the TR_INFO structure holding all TR data
\param src index of the current complex
\param ymax If the pointer is defined, the value is adjusted if ymax of current complex is more than the current value.
\param ymin If the pointer is defined, the value is adjusted if ymin of current complex is less than the current value.
*/
double baseline=0;
double yheight;
int last;
int i;
int trec;
case TR_TEXT:
if(ymax){
}
else if(ymin){
tmp = tpi->chunks[trec].fs * ((double)-fsp->face->bbox.yMin/yheight); /* yMin in face is negative */
}
break;
case TR_LINE:
for(i=last;i>=0;i--){ /* here last is the count of text objects in the complex */
if(ymax){
/* gcc 4.6.3 had a bizarre optimization error for -O2 and -O3 where *ymax <= tmp was
not true when *ymax == tmp, as verified by examining the binary representations.
This was apparently due to retained excess precision. Making tmp volatile
forces it to be stored into a 64 bit location, dropping the extra 12 bits from
the 80 bit register. */
}
}
else if(ymin){
tmp = tpi->chunks[trec].fs * (((double)-fsp->face->bbox.yMin)/yheight); /* yMin in face is negative */
}
}
}
break;
case TR_PARA_UJ:
case TR_PARA_LJ:
case TR_PARA_CJ:
case TR_PARA_RJ:
break;
}
return(baseline);
}
/**
\brief Check or set vertical advance on the growing complex relative to the current complex.
Vadvance is a multiplicative factor like 1.25.
The distance between successive baselines is vadvance * max(font_size), where the maximum
is over all text elements in src.
The growing complex is always the last one in the CX_INFO section of the TR_INFO structure.
If an existing vadvance does not match the one which would be required to fit the next complex
to add to the growing one, it terminates a growing complex. (Ie, starts a new paragraph.)
Find baseline on Y axis of a complex.
If the complex is a TR_TEXT or TR_LINE find its baseline.
If the complex is TR_PARA+* find the baseline of the last line.
If there are multiple text elements in a TR_LINE, the baseline is that of the
element that uses the largest font. This will definitely give the wrong
result if that line starts with a super or subscript that is full font size, but
they are usually smaller.
\return Returns 0 on success, !0 on failure.
\param tri pointer to the TR_INFO structure holding all TR data
\param src index of the current complex, to be added to the growing complex.
This lets the value of "src - lines" determine the weight to give to each new vadvance value
as it is merged into the running weighted average. This improves the accuracy of the vertical advance,
since there can be some noise introduced when lines have different maximum font sizes.
\param lines index of the first text block that was added to the growing complex.
*/
int status = 0;
double prevbase;
double thisbase;
double weight;
int trec;
double newV;
int dst;
trec = cxi->cx[dst].kids.members[0]; /* complex whose first text record holds vadvance for this complex */
/* already set on the first text (only place it is stored.)
See if the line to be added is compatible.
vadvance must be within 1% or do not add a new line */
status = 1;
}
else { /* recalculate the weighted vadvance */
}
}
else { /* only happens when src = lines + 1*/
}
return(status);
}
/**
\brief Initialize an FT_INFO structure. Sets up a freetype library to use in this context.
\returns a pointer to the FT_INFO structure created, or NULL on error.
*/
if(FcInit()){
if(fti){
if(ftinfo_make_insertable(fti)){
}
}
else {
}
}
}
return(fti);
}
/**
\brief Make an FT_INFO structure insertable. Adds storage as needed.
\param fti pointer to the FT_INFO structure
\returns 0 on success, !0 on error.
*/
int status=0;
if(!fti)return(2);
if(tmp){
}
else {
status=1;
}
}
return(status);
}
/**
\brief Insert a copy of a FNT_SPECS structure into the FT_INFO structure.
\param fti pointer to the FT_INFO structure.
\param fsp pointer to the FNT_SPECS structure.
\returns 0 on success, !0 on error.
*/
if(!fti)return(2);
if(!fsp)return(3);
}
return(status);
}
/**
\brief Release an FT_INFO structure. Release all associated memory.
Use like: fi_ptr = ftinfo_release(fi_ptr)
\param fti pointer to the FT_INFO structure.
\returns NULL.
*/
(void) ftinfo_clear(fti);
FcFini(); /* shut down FontConfig, release memory, patterns must have already been released or boom! */
return NULL;
}
/**
\brief Clear an FT_INFO structure. Release all Freetype memory but does not release Fontconfig.
This would be called in preference to ftinfo_release() if some other part of the program needed
to continue using Fontconfig.
Use like: fi_ptr = ftinfo_clear(fi_ptr)
\param fti pointer to the FT_INFO structure.
\returns NULL.
*/
uint32_t i;
if(fti){
}
}
return NULL;
}
/**
\brief Find the loaded font matching fontspec
\returns index of font on success, -1 if not found
\param tri pointer to the TR_INFO structure.
\param fontspec UTF-8 description of the font, as constructed in trinfo_load_fontname
*/
uint32_t i;
/* If it is already loaded, do not load it again */
status=i;
break;
}
}
return(status);
}
/**
\brief Find the loaded font matching the source file
\returns index of font on success, -1 if not found
\param tri pointer to the TR_INFO structure.
\param filename UTF-8 file name for the font
*/
uint32_t i;
/* If it is already loaded, do not load it again */
status=i;
break;
}
}
return(status);
}
/**
\brief Load a (new) font by name into a TR_INFO structure or find it if it is already loaded
\returns fi_idx of inserted (or found) font on success, <0 on error.
\param fti pointer to the FT_INFO structure.
\param fontname UTF-8 font name
\param fontspec UTF-8 font specification used for query string.
*/
char *filename;
double fd;
int status;
int fi_idx;
if(!fti)return(-1);
/* If it is already loaded, do not load it again */
status = 0; /* was -1, reset to 0 */
while(1) { /* this is NOT a loop, it uses breaks to avoid gotos and deep nesting */
/* get a fontset, trimmed to only those with new glyphs as needed, so that missing glyph's may be handled */
if(!(fontset = FcFontSort (NULL,pattern, FcTrue, NULL, &result)) || (result != FcResultMatch)){ status = -4; break; }
if(FcPatternGetString( fpat, FC_FILE, 0, (FcChar8 **)&filename) != FcResultMatch){ status = -5; break; }
/* copy these into memory for external use */
break;
}
/* release FC's own memory related to this call that does not need to be kept around so that face will work */
if(status<0){
return(status);
}
/* get the current face */
if(FT_Set_Char_Size(
0, /* char_width in 1/64th of points */
72, /* horizontal device resolution, DPI */
72) /* vebrical device resolution, DPI */
){ return(-9); }
/* The space advance is needed in various places. Get it now, and get it in the font units,
so that it can be scaled later with the text size */
status = TR_getadvance(fti, fsp,' ',0,FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP, FT_KERNING_UNSCALED, NULL, NULL);
if(status < 0)return(-7);
/*
char *fs;
int fb;
if(FcPatternGetBool( fpat, FC_OUTLINE, 0, &fb)== FcResultMatch){ printf("outline: %d\n",fb);fflush(stdout); }
if(FcPatternGetBool( fpat, FC_SCALABLE, 0, &fb)== FcResultMatch){ printf("scalable: %d\n",fb);fflush(stdout); }
if(FcPatternGetDouble( fpat, FC_DPI, 0, &fd)== FcResultMatch){ printf("DPI: %f\n",fd);fflush(stdout); }
if(FcPatternGetInteger( fpat, FC_FONTVERSION, 0, &fb)== FcResultMatch){ printf("fontversion: %d\n",fb);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FULLNAME , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FULLNAME : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FAMILY , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FAMILY : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_STYLE , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("STYLE : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FOUNDRY , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FOUNDRY : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FAMILYLANG , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FAMILYLANG : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_STYLELANG , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("STYLELANG : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FULLNAMELANG, 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FULLNAMELANG: %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_CAPABILITY , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("CAPABILITY : %s\n",fs);fflush(stdout); }
if(FcPatternGetString( fpat, FC_FONTFORMAT , 0, (FcChar8 **)&fs)== FcResultMatch){ printf("FONTFORMAT : %s\n",fs);fflush(stdout); }
*/
return(fi_idx);
}
/**
\brief Dump the contents of the TR_INFO structure to stdout. For debugging purposes,not used in production code.
\param tri pointer to the TR_INFO structure.
*/
uint32_t i,j;
printf("fti font: %6d space: %6d used: %6d spcadv %8f fsize %8f \n",i,fsp->space,fsp->used,fsp->spcadv,fsp->fsize);
}
}
}
/**
\brief Make the FNT_SPECS alts structure insertable. Adds storage as needed.
\param fti pointer to the FT_INFO structure
\returns 0 on success, !0 on error.
*/
int status=0;
if(!fsp)return(2);
if(tmp){
}
else {
status=1;
}
}
return(status);
}
/**
\brief Insert a new ALT_SPECS into the FNT_SPECS alts list.
\param fsp pointer to the FNT_SPECS structure.
\param fi_idx font index to add to the alts list
\returns 0 on success, !0 on error.
*/
if(!fsp)return(3);
}
return(status);
}
/**
\brief Increment the weight of an alts entry by 1, readjust order if necessary
\param fsp pointer to the FNT_SPECS structure.
\param idx index of the alts entry to increment
\returns 0 on success, !0 on error.
*/
uint32_t i;
if(!fsp)return(1);
/* If a counter hits the limit divide all counts in half. */
}
for(i=a_idx; i>0; i--){
}
return(0);
}
/**
\brief Make a CHILD_SPECS structure insertable. Adds storage as needed.
\param csp pointer to the CHILD_SPECS structure
\returns 0 on success, !0 on error.
*/
int status=0;
int *tmp;
if(!csp)return(2);
if(tmp){
}
else {
status=1;
}
}
return(status);
}
/**
\brief Add a member to a CHILD_SPECS structure. (Member is an index for either a text object or a complex.)
\param dst pointer to the CHILD_SPECS structure.
\param src index of the member.
\returns 0 on success, !0 on error.
*/
if(!dst)return(2);
}
return(status);
}
/**
\brief Append all the members of one CHILD_SPECS structure to another CHILD_SPECS structure.
Member is an index for either a text object or a complex.
The donor is not modified.
\param dst pointer to the recipient CHILD_SPECS structure.
\param src pointer to the donor CHILD_SPECS structure.
\returns 0 on success, !0 on error.
*/
uint32_t i;
if(!dst)return(2);
if(!src)return(3);
if(status)break;
}
return(status);
}
/**
\brief Release a CHILD_SPECS structure. Release all associated memory.
\param csp pointer to the CHILD_SPECS structure.
\returns NULL.
*/
if(csp){
}
}
/**
\brief Clear a CHILD_SPECS structure, making all allocated slots usable. Does not release associated memory.
\param csp pointer to the CHILD_SPECS structure.
\returns NULL.
*/
}
/**
\brief Initialize an CX_INFO structure. Holds complexes (multiple text objects in known positions and order.)
\returns a pointer to the CX_INFO structure created, or NULL on error.
*/
if(cxi){
if(cxinfo_make_insertable(cxi)){
}
}
return(cxi);
}
/**
\brief Make a CX_INFO structure insertable. Adds storage as needed.
\returns 0 on success, !0 on error.
\param cxi pointer to the CX_INFO structure
*/
int status=0;
if(tmp){
}
else {
status=1;
}
}
return(status);
}
/**
\brief Insert a complex into the CX_INFO structure. (Insert may be either TR_TEXT or TR_LINE.)
\returns 0 on success, !0 on error.
\param cxi pointer to the CX_INFO structure (complexes).
\param src index of the complex to insert.
\param src_rt_tidx index of the bounding rectangle
\param type TR_TEXT (index is for tpi->chunks[]) or TR_LINE (index is for cxi->kids[])
*/
if(!cxi)return(2);
}
return(status);
}
/**
\brief Append a complex to the CX_INFO structure and give it a type.
\param cxi pointer to the CX_INFO structure (complexes).
\param src index of the complex to append.
\param type TR_LINE (src is an index for tpi->chunks[]) or TR_PARA (src is an index for cxi->kids[]).
\returns 0 on success, !0 on error.
*/
if(!cxi)return(2);
}
return(status);
}
/**
\brief Merge a complex dst with N members (N>=1) by adding a second complex src, and change the type.
\param cxi pointer to the CX_INFO structure (complexes).
\param dst index of the complex to expand.
\param src index of the donor complex (which is not modified).
\param type TR_LINE (src is an index for tpi->chunks[]) or TR_PARA (src is an index for cxi->kids[]).
\returns 0 on success, !0 on error.
*/
if(!cxi)return(2);
if(src < 0)return(5);
return(status);
}
/**
\brief Trim the last complex from thelist of complexes.
\param cxi pointer to the CX_INFO structure (complexes).
\returns 0 on success, !0 on error.
*/
int status = 0;
int last ;
if(!cxi)return(1);
return(status);
}
/**
\brief Dump the contents of the TR_INFO structure to stdout. For debugging purposes,not used in production code.
\param tri pointer to the TR_INFO structure.
*/
uint32_t i,j,k;
if(cxi){
printf("cxi cx[%d] type:%d rt_tidx:%d kids_used:%d kids_space:%d\n",i, csp->type, csp->rt_cidx, csp->kids.used, csp->kids.space);
printf("cxi cx[%d] member:%3d tp_idx:%3d ldir:%d rt_tidx:%3d br (LL,UR) (%8.3f,%8.3f),(%8.3f,%8.3f) xy (%8.3f,%8.3f) kern (%8.3f,%8.3f) text:<%s> decor:%5.5x\n",
}
else { /* TR_PARA_* */
printf("cxi cx[%d] member:%d cx_idx:%d\n",i, j, k);
}
}
}
}
return;
}
/**
\brief Release a CX_INFO structure. Release all associated memory.
use like: cxi = cxiinfo_release(cxi);
\param cxi pointer to the CX_INFO structure.
\returns NULL.
*/
uint32_t i;
if(cxi){
}
return NULL;
}
/**
\brief Initialize an TP_INFO structure. Holds text objects from which complexes are built.
\returns a pointer to the TP_INFO structure created, or NULL on error.
*/
if(tpi){
if(tpinfo_make_insertable(tpi)){
}
}
return(tpi);
}
/**
\brief Make a TP_INFO structure insertable. Adds storage as needed.
\returns 0 on success, !0 on error.
\param tpi pointer to the TP_INFO structure
*/
int status=0;
if(tmp){
}
else {
status=1;
}
}
return(status);
}
/**
\brief Insert a copy of a TCHUNK_SPECS structure into a TP_INFO structure. (Insert a text object.)
\returns 0 on success, !0 on error.
\param tpi pointer to the TP_INFO structure
\param tsp pointer to the TCHUNK_SPECS structure
*/
if(!tpi)return(2);
if(!tsp)return(3);
}
return(status);
}
/**
\brief Release a TP_INFO structure. Release all associated memory.
use like: tpi = tpinfo_release(tpi);
\returns NULL.
\param tpi pointer to the TP_INFO structure.
*/
uint32_t i;
if(tpi){
}
return NULL;
}
/**
\brief Initialize an BR_INFO structure. Holds bounding rectangles, for both text objects and complexes.
\returns a pointer to the BR_INFO structure created, or NULL on error.
*/
if(bri){
if(brinfo_make_insertable(bri)){
}
}
return(bri);
}
/**
\brief Make a BR_INFO structure insertable. Adds storage as needed.
\returns 0 on success, !0 on error.
\param bri pointer to the BR_INFO structure
*/
int status=0;
if(!bri)return(2);
else { status = 1;}
}
return(status);
}
/**
\brief Insert a copy of a BRECT_SPEC structure into a BR_INFO structure. (Insert a bounding rectangle.)
\returns 0 on success, !0 on error.
\param bri pointer to the BR_INFO structure
\param element pointer to the BRECT_SPECS structure
*/
if(!bri)return(2);
}
return(status);
}
/**
\brief Merge BRECT_SPEC element src into/with BRECT_SPEC element dst. src is unchanged. (Merge two bounding rectangles.)
\returns 0 on success, !0 on error.
\param bri pointer to the BR_INFO structure
\param dst index of the destination bounding rectangle.
\param src index of the source bounding rectangle.
*/
if(!bri)return(1);
bri->rects[dst].yll = TEREMAX(bri->rects[dst].yll, bri->rects[src].yll); /* MAX because Y is positive DOWN */
bri->rects[dst].yur = TEREMIN(bri->rects[dst].yur, bri->rects[src].yur); /* MIN because Y is positive DOWN */
/*
printf("bri_Merge into rect:%d (LL,UR) dst:(%f,%f),(%f,%f) src:(%f,%f),(%f,%f)\n",dst,
(bri->rects[dst].xll),
(bri->rects[dst].yll),
(bri->rects[dst].xur),
(bri->rects[dst].yur),
(bri->rects[src].xll),
(bri->rects[src].yll),
(bri->rects[src].xur),
(bri->rects[src].yur));
*/
return(0);
}
/**
\brief Check for an allowable overlap of two bounding rectangles.
Allowable overlap is any area overlap of src and dst bounding rectangles, after
they have been expanded (padded) by allowed edge expansions. (For instance, if
missing spaces must be accounted for.)
The method works backwards: look for all reasons they might not overlap,
if none are found, then the rectangles do overlap.
An overlap here does not count just a line or a point - area must be involved.
\returns 0 on success (overlap detected), 1 on no overlap, anything else is an error.
\param bri pointer to the BR_INFO structure
\param dst index of the destination bounding rectangle.
\param src index of the source bounding rectangle.
\param rp_dst Pointer to edge padding values for dst.
\param rp_src Pointer to edge padding values for src.
*/
int status;
if( /* Test all conditions that exclude overlap, if any are true, then no overlap */
((br_dst->yur - rp_dst->up) > (br_src->yll + rp_src->down) ) || /* dst fully below (Y is positive DOWN) */
((br_dst->yll + rp_dst->down) < (br_src->yur - rp_src->up) ) /* dst fully above (Y is positive DOWN) */
){
status = 1;
}
else {
/* overlap not excluded, so it must occur.
Only accept overlaps that are mostly at one end or the other, not mostly top or bottom.
If the following condition is true then there is no more than a tiny bit of horizontal overlap of src
within dist, which suggests that the two pieces of text may be considered part of one line.
if(
(br_src->xll >= br_dst->xur - rp_dst->right) || /* src overlaps just a little on the right (L->R language) */
(br_src->xur <= br_dst->xll + rp_dst->left) /* src overlaps just a little on the left (R->L language) */
){
status = 0;
}
else { /* Too much overlap, reject the overlap */
status = 1;
}
}
/*
printf("Overlap status:%d\nOverlap trects (LL,UR) dst:(%f,%f),(%f,%f) src:(%f,%f),(%f,%f)\n",
status,
(br_dst->xll - rp_dst->left ),
(br_dst->yll - rp_dst->down ),
(br_dst->xur + rp_dst->right),
(br_dst->yur + rp_dst->up ),
(br_src->xll - rp_src->left ),
(br_src->yll - rp_src->down ),
(br_src->xur + rp_src->right),
(br_src->yur + rp_src->up ));
printf("Overlap brects (LL,UR) dst:(%f,%f),(%f,%f) src:(%f,%f),(%f,%f)\n",
(br_dst->xll),
(br_dst->yll),
(br_dst->xur),
(br_dst->yur),
(br_src->xll),
(br_src->yll),
(br_src->xur),
(br_src->yur));
printf("Overlap rprect (LL,UR) dst:(%f,%f),(%f,%f) src:(%f,%f),(%f,%f)\n",
(rp_dst->left),
(rp_dst->down),
(rp_dst->right),
(rp_dst->up),
(rp_src->left),
(rp_src->down),
(rp_src->right),
(rp_src->up));
*/
return(status);
}
/**
\brief Check for various sorts of invalid text elements upstream (language dir changes, draw order backwards from language direction)
\returns 0 on success (not upstream), 1 if upstream, anything else is an error.
\param bri pointer to the BR_INFO structure
\param dst index of the destination bounding rectangle.
\param src index of the source bounding rectangle.
\param ddir direction of dst
\param sdir direction of src
*/
int status=0;
if(!bri)return(2);
}
}
}
}
return(status);
}
/**
\brief Try to deduce justification of a paragraph from the bounding rectangles for two successive lines.
\returns one of TR_PARA_ UJ (unknown justified), LJ, CJ, or RJ (left, center, or right justified).
\param bri pointer to the BR_INFO structure
\param dst index of the destination bounding rectangle.
\param src index of the source bounding rectangle.
\param slop allowed error in edge alignment.
\param type Preexisting justification for dst, if any. Justification of dst and src must match this or
TR_PARA_UJ is returned even if dst and src have some (other) alignment.
*/
enum tr_classes brinfo_pp_alignment(const BR_INFO *bri, int dst, int src, double slop, enum tr_classes type){
/* lines in the wrong vertical order, no paragraph possible (Y is positive down) */
}
/* LJ (might also be CJ but LJ takes precedence) */
}
/* RJ */
}
/* CJ */
}
else {
/* not aligned */
}
/* within a paragraph type can change from unknown to known, but not from one known type to another*/
}
/*
printf("pp_align newtype:%d brects (LL,UR) dst:(%f,%f),(%f,%f) src:(%f,%f),(%f,%f)\n",
newtype,
(br_dst->xll),
(br_dst->yll),
(br_dst->xur),
(br_dst->yur),
(br_src->xll),
(br_src->yll),
(br_src->xur),
(br_src->yur));
*/
return(newtype);
}
/**
\brief Release a BR_INFO structure. Release all associated memory.
use like: bri = brinfo_release(bri);
\param bri pointer to the BR_INFO structure.
\returns NULL.
*/
if(bri){
}
return NULL;
}
/**
\brief Initialize an TR_INFO structure. Holds all data for text reassembly.
\returns a pointer to the TR_INFO structure created, or NULL on error.
*/
return(tri);
}
/**
\brief Release a TR_INFO structure completely.
Release all associated memory, including FontConfig.
See also trinfo_clear() and trinfo_release_except_FC().
use like: tri = trinfo_release(tri);
\param tri pointer to the TR_INFO structure.
\returns NULL.
*/
if(tri){
}
return(NULL);
}
/**
\brief Release a TR_INFO structure mostly.
Release all associated memory EXCEPT Fontconfig.
Fontconfig may still be needed elsewhere in a program and there is no way to figure that out here.
See also trinfo_clear() and trinfo_release().
use like: tri = trinfo_release_except_FC(tri);
\param tri pointer to the TR_INFO structure.
\returns NULL.
*/
if(tri){
}
return(NULL);
}
/**
\brief Clear a TR_INFO structure.
Releases text and rectangle information, but retains font information, both
Freetype information and Fontconfig information.
See also trinfo_release() and trinfo_release_except_FC().
Use like: tri = trinfo_clear(tri);
\param tri pointer to the TR_INFO structure.
\returns NULL.
*/
if(tri){
};
/* Do NOT modify: qe, use_kern, usebk, load_flags, kern_mode, or bkcolor. Set the rest back to their defaults */
){
}
}
return(tri);
}
/**
\brief Set the quantization error value for a TR_INFO structure.
If coordinates have passed through an integer form limits
in accuracy may have been imposed. For instance, if the X coordinate of a point in such a file
is 1000, and the conversion factor from those coordinates to points is .04, then eq is .04. This
just says that single coordinates are only good to within .04, and two coordinates may differ by as much
as .08, just due to quantization error. So if some calculation shows a difference of
.02 it may be interpreted as this sort of error and set to 0.0.
\returns 0 on success, !0 on error.
\param tri pointer to TR_INFO structure
\param qe quantization error.
*/
if(!tri)return(1);
return(0);
}
/**
\brief Set the background color and whether or not to use it.
When background color is turned on each line of text is underwritten with a rectangle
of the specified color. The rectangle is the merged bounding rectangle for that line.
\returns 0 on success but nothing changed, >0 on error, <0 on success and a value changed.
\param tri pointer to TR_INFO structure
\param usebk 0 for no background, anything else uses background color
\param bkcolor background color to use
*/
int status=0;
else {
else {
}
}
return(status);
}
/**
\brief Are the proposed new background and background color a change?
\returns 0 if they are the same, -1 if either is different
\param tri pointer to TR_INFO structure
\param usebk 0 for no background, anything else uses background color
\param bkcolor background color to use
*/
int status = 0;
return(status);
}
/**
\brief Set Freetype parameters and kerning mode (if any) in a TRI_INFO structure.
\returns 0 on success, !0 on error.
\param tri pointer to a TR_INFO structure
\param use_kern 0 if kerning is to be employed, !0 otherwise.
\param load_flags Controls internal advance:
FT_LOAD_NO_SCALE, internal advance is in 1/64th of a point. (kerning values are still scaled)
FT_LOAD_TARGET_NORMAL internal advance is in 1/64th of a point. The scale
factor seems to be (Font Size in points)*(DPI)/(32.0 pnts)*(72 dpi).
\param kern_mode FT_KERNING_DEFAULT, FT_KERNING_UNFITTED, or FT_KERNING_UNSCALED. Set to match calling application.
*/
if(!tri)return(1);
return(0);
}
/**
\brief Append text to a TR_INFO struct's output buffer, expanding it if necessary.
\returns 0 on success, !0 on error.
\param tri pointer to a TR_INFO structure
\param src Pointer to a text string.
*/
if(!src)return(-1);
else { return(-1); }
}
return(0);
}
/**
\brief Load a text object into a TR_INFO struct.
\returns 0 on success, !0 on error. -1 means that the escapement is different from the objects already loaded.
\param tri pointer to a TR_INFO structure
\param tsp pointer to a TCHUNK_SPECS structure (text object to load)
\param escapement angle in degrees of the text object.
\param flags special processing flags:
TR_EMFBOT calculate Y coordinates of ALIBOT object compatible with EMF files TA_BOTTOM alignment.
*/
int status;
double x,y,xe;
/* check incoming parameters */
if(!tri)return(1);
if(!tsp)return(2);
}
else {
}
ymin = 64000;
ymax = -64000;
/* The geometry model has origin Y at the top of screen, positive Y is down, maximum positive
Y is at the bottom of the screen. That makes "top" (by positive Y) actually the bottom
(as viewed on the screen.) */
/* Careful! face bbox does NOT scale with FT_Set_Char_Size
printf("Face idx:%d bbox: xMax/Min:%ld,%ld yMax/Min:%ld,%ld UpEM:%d asc/des:%d,%d height:%d size:%f\n",
idx,
fsp->face->bbox.xMax,fsp->face->bbox.xMin,
fsp->face->bbox.yMax,fsp->face->bbox.yMin,
fsp->face->units_per_EM,fsp->face->ascender,fsp->face->descender,fsp->face->height,fsp->fsize);
*/
if(!text32){ // LATIN1 encoded >128 are generally not valid UTF, so the first will fail
if(!text32)return(5);
}
/* baseline advance is independent of character orientation */
status = TR_getadvance(fti, fsp, *tptr, (tri->use_kern ? prev: 0), tri->load_flags, tri->kern_mode, &ymin, &ymax);
if(status>=0){
}
else { return(6); }
}
/* Some glyphs in fonts have no vertical extent, for instance, Hebrew glyphs in Century Schoolbook L.
Use the 3/4 of the font size as a (very bad) approximation for the actual values. */
}
/* This did not work very well because the ascender/descender went well beyond the actual characters, causing
overlaps on lines that did not actually overlap (vertically).
asc = ((double) (fsp->face->ascender) )/64.0;
dsc = ((double) (fsp->face->descender))/64.0;
*/
/* find the font ascender descender (general one, not specific for current text) */
/* originally the denominator was just 32.0, but it broke when units_per_EM wasn't 2048 */
/* now place the rectangle using ALN information */
}
}
else{ /* taln & ALIHORI & ALIRIGHT */
}
}
/* From this point forward y is on the baseline, so need to correct it in chunks. The asc/dsc are the general
ones for the font, else the text content will muck around with the baseline in BAD ways. */
else{ /* taln & ALIVERT & ALIBOT */
if(flags & TR_EMFBOT){ tpi->chunks[current].y -= 0.35 * tsp->fs; } /* compatible with EMF implementations */
}
/* since y is always on the baseline, the lower left and upper right are easy. These use asc/dsc for the particular text,
so that the bounding box will fit it tightly. */
return(0);
}
/**
\brief Fontweight conversion. Fontconfig units to SVG units.
Anything not recognized becomes "normal" == 400.
There is no interpolation because a value that mapped to 775, for instance, most
likely would not display properly because it is intermediate between 700 and 800, and
only those need be supported in SVG viewers.
\returns SVG font weight
\param weight Fontconfig font weight.
*/
else { ret = 400; }
return(ret);
}
/**
\brief Set the padding that will be added to bounding rectangles before checking for overlaps in brinfo_overlap().
\returns void
\param rt_pad pointer to an RT_PAD structure.
\param up padding for the top of a bounding rectangle.
\param down padding for the bottom of a bounding rectangle.
\param left padding for the left of a bounding rectangle.
\param right padding for the right of a bounding rectangle.
*/
}
/**
\brief Convert from analyzed complexes to SVG format.
\returns void
\param tri pointer to a TR_INFO struct which will be analyzed. Result is stored in its "out" buffer.
*/
double x = tri->x;
double y = tri->y;
double esc;
int cutat;
int ldir;
/* copy the current numeric locale, make a copy because setlocale may stomp on
the memory it points to. Then change it because SVG needs decimal points,
not commas, in floats. Restore on exit from this routine.
*/
/*
#define DBG_TR_PARA 0
#define DBG_TR_INPUT 1
*/
/* The debug section below is difficult to see if usebk is anything other than BKCLR_NONE */
/* put rectangles down for each text string - debugging!!! This will not work properly for any Narrow fonts */
sprintf(stransform,"transform=\"matrix(%f,%f,%f,%f,%f,%f)\"\n",cos(esc),-sin(esc),sin(esc),cos(esc), 1.25*x,1.25*y);
for(i=cxi->phase1; i<cxi->used;i++){ /* over all complex members from phase2 == TR_PARA_* complexes */
for(j=0; j<csp->kids.used; j++){ /* over all members of these complexes, which are phase1 complexes */
if(!j && !k){
#if DBG_TR_PARA
TRPRINT(tri, "style=\"color:#0000FF;color-interpolation:sRGB;color-interpolation-filters:linearRGB;fill:none;stroke:#000000;stroke-width:0.8;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;clip-rule:nonzero\"\n");
sprintf(obuf,"x=\"%f\" y=\"%f\"\n",1.25*(bri->rects[csp->rt_cidx].xll),1.25*(bri->rects[csp->rt_cidx].yur));
#endif /* DBG_TR_PARA */
}
#if DBG_TR_INPUT /* debugging code, this section writes the original text objects */
TRPRINT(tri, "style=\"color:#000000;color-interpolation:sRGB;color-interpolation-filters:linearRGB;fill:none;stroke:#00FF00;stroke-width:0.3;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;clip-rule:nonzero\"\n");
sprintf(obuf,"font-size:%fpx;",tsp->fs*1.25); /*IMPORTANT, if the FS is given in pt it looks like crap in browsers. As if px != 1.25 pt, maybe 96 dpi not 90?*/
#endif /* DBG_TR_INPUT debugging code, original text objects */
}
}
}
sprintf(stransform,"transform=\"matrix(%f,%f,%f,%f,%f,%f)\"\n",cos(esc),-sin(esc),sin(esc),cos(esc), 1.25*x,1.25*y);
for(i=cxi->phase1; i<cxi->used;i++){ /* over all complex members from phase2 == TR_PARA_* complexes */
for(j=0; j<csp->kids.used; j++){ /* over all members of these complexes, which are phase1 complexes */
sprintf(obuf,"style=\"color-interpolation:sRGB;color-interpolation-filters:linearRGB;fill:#%2.2X%2.2X%2.2X;;stroke:none;;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;clip-rule:nonzero\"\n",tri->bkcolor.Red,tri->bkcolor.Green,tri->bkcolor.Blue);
sprintf(obuf,"width=\"%f\"\n", 1.25*(bri->rects[cline_sp->rt_cidx].xur - bri->rects[cline_sp->rt_cidx].xll));
sprintf(obuf,"height=\"%f\"\n",1.25*(bri->rects[cline_sp->rt_cidx].yll - bri->rects[cline_sp->rt_cidx].yur));
sprintf(obuf,"x=\"%f\" y=\"%f\"\n",1.25*(bri->rects[cline_sp->rt_cidx].xll),1.25*(bri->rects[cline_sp->rt_cidx].yur));
}
if(!j && !k){
sprintf(obuf,"style=\"color-interpolation:sRGB;color-interpolation-filters:linearRGB;fill:#%2.2X%2.2X%2.2X;;stroke:none;;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;clip-rule:nonzero\"\n",tri->bkcolor.Red,tri->bkcolor.Green,tri->bkcolor.Blue);
sprintf(obuf,"x=\"%f\" y=\"%f\"\n",1.25*(bri->rects[csp->rt_cidx].xll),1.25*(bri->rects[csp->rt_cidx].yur));
}
}
sprintf(obuf,"style=\"color-interpolation:sRGB;color-interpolation-filters:linearRGB;fill:#%2.2X%2.2X%2.2X;;stroke:none;;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;clip-rule:nonzero\"\n",tri->bkcolor.Red,tri->bkcolor.Green,tri->bkcolor.Blue);
}
}
}
}
}
/* over all complex members from phase2. Paragraphs == TR_PARA_* */
/* over all members of the present Paragraph. Each of these is a line and a phase 1 complex.
It may be either TR_TEXT or TR_LINE */
if(j){
}
recenter = 0; /* mostly to quiet a compiler warning, should always be set below */
/* over all members of the present Line. These are the original text objects which were reassembled.
There will be one for TR_TEXT, more than one for TR_LINE */
if(!k){ /* first iteration */
case TR_TEXT:
case TR_LINE:
/* these should never occur, this section quiets a compiler warning */
break;
case TR_PARA_UJ:
case TR_PARA_LJ:
if(ldir == LDIR_RL){ recenter = -(bri->rects[cxi->cx[jdx].rt_cidx].xur - bri->rects[cxi->cx[jdx].rt_cidx].xll); }
else { recenter = 0.0; }
break;
case TR_PARA_CJ:
if(ldir == LDIR_RL){ recenter = -(bri->rects[cxi->cx[jdx].rt_cidx].xur - bri->rects[cxi->cx[jdx].rt_cidx].xll)/2.0; }
else { recenter = +(bri->rects[cxi->cx[jdx].rt_cidx].xur - bri->rects[cxi->cx[jdx].rt_cidx].xll)/2.0; }
break;
case TR_PARA_RJ:
break;
}
if(!j){
sprintf(obuf,"font-size:%fpx;",tsp->fs*1.25); /*IMPORTANT, if the FS is given in pt it looks like crap in browsers. As if px != 1.25 pt, maybe 96 dpi not 90?*/
else { lineheight = 125.0; }
case TR_TEXT:
case TR_LINE:
/* these should never occur, this section quiets a compiler warning */
break;
case TR_PARA_UJ:
case TR_PARA_LJ:
break;
case TR_PARA_CJ:
break;
case TR_PARA_RJ:
break;
}
sprintf(obuf,"transform=\"matrix(%f,%f,%f,%f,%f,%f)\"\n",cos(esc),-sin(esc),sin(esc),cos(esc),1.25*x,1.25*y);
}
sprintf(obuf,"<tspan sodipodi:role=\"line\"\nx=\"%f\" y=\"%f\"\n>",tmpx,1.25*(bri->rects[kdx].yll - tsp->boff));
}
/* Scale kerning and make any other necessary adjustments
*/
sprintf(obuf,"font-size:%fpx;",tsp->fs*1.25); /*IMPORTANT, if the FS is given in pt it looks like crap in browsers. As if px != 1.25 pt, maybe 96 dpi not 90?*/
/* multiple text decoration styles may be set */
if(*obuf){
/* only a single text decoration line type may be set */
case TXTDECOR_SOLID: break; // "solid" is the CSS 3 default, omitting it remains CSS 2 compatible
default: break;
}
if((tsp->decoration & TXTDECOR_CLRSET) && memcmp(&(tsp->decColor),&(tsp->color),sizeof(TRCOLORREF))){
/* CSS 3, CSS 2 implementations may choke on it. If the specified color matches text color omit, for better CSS 2 compatitiblity. */
}
}
}
} /* end of k loop */
} /* end of j loop */
} /* end of i loop */
/* restore locale and free memory. */
}
/**
\brief Attempt to figure out the original organization, in lines and paragraphs, of the text objects.
The method is:
1. Generate complexes from the text objects (strings) by overlaps (optionally allowing up to two spaces to be
added) to produce larger rectangles. Complexes that are more or less sequential and have 2 or more text objects
are TR_LINEs, therwise they are TR_TEXT.
2. Group sequential complexes (TR_LINE or TR_TEXT) into TR_PARA_UJ (paragraphs,by smooth progression in vertical
position down page).
3. Analyze the paragraphs to classify them as Left/Center/Right justified (possibly with indentation.) If
4. Return the number of complex text objects.
\returns Number of complexes. (>=1, <= number of text objects.) <0 is an error.
\param tri pointer to the TR_INFO structure holding the data, which will also hold the results.
*/
unsigned int i,j,k;
int ok;
int cxidx;
int src_rt;
int dst_rt;
double ratio;
double spcadv;
if(!tri)return(-1);
/* When debugging
ftinfo_dump(fti);
*/
/* Phase 1. Working sequentially, insert text. Initially as TR_TEXT and then try to extend to TR_LINE by checking
overlaps. When done the complexes will contain a mix of TR_LINE and TR_TEXT. */
memcpy(&bsp,&(bri->rects[tspi->rt_tidx]),sizeof(BRECT_SPECS)); /* Must make a copy as next call may reallocate rects! */
/* for the leading text: pad with no leading and two trailing spaces, leading and trailing depend on direction */
if(tspi->ldir == LDIR_RL){ TR_rt_pad_set(&rt_pad_i,tri->qe, tri->qe, tri->qe + 2.0 * spcadv, 0.0); }
/* Reject font size changes of greater than 50%, these are almost certainly not continuous text. These happen
in math formulas, for instance, where a sum or integral is much larger than the other symbols. */
/* for the trailing text: pad with one leading and trailing spaces (so it should work L->R and R->L) */
/* Reject direction changes like [1 <- Hebrew][2 -> English], that is where the direction changes AND the
next logical piece of text is "upstream" positionally of its logical predecessor. The meaning of such
a construct is at best ambiguous. The test is only applied with respect to the first text chunk. This sort
of construct may appear when a valid initial construct like [1->English][2<-Hebrew][3->English] is edited
and the leading chunk of text removed.
Also reject reversed order text as in (English) <A><B><C> (draw order) arranged as <C><B><A>. This happens
if the language direction field is incorrect, perhaps due to a corrupt or malformed input file.
*/
if(brinfo_upstream(bri,
dst_rt, /* index into bri for dst */
src_rt, /* index into bri for src */
if(!brinfo_overlap(bri,
dst_rt, /* index into bri for dst */
src_rt, /* index into bri for src */
/* for the leading text: pad with two leading and trailing spaces (so it should work L->R and R->L */
}
else { /* either alignment ge*/
break;
}
}
/* Bidirectional text will cause complexes to not assemble in one pass.
This happens whenever a change of direction occurs with 2 or more sequential elements in
the opposite direction,
Let + = LR and - = RL.
Reading left to right, this happens with +-- or -++.
For instance, the sequence ++-+ ---+ would break into the two complexes shown.
Not until the last element in the second complex is added will the bounding rectangles for the complexes overlap.
Check for this effect now if there is a preceding complex and the first element of the current complex is
reversed from the last in the preceding. */
if(cxidx >= 1){
tspi = &(tpi->chunks[ kidp->members[kidp->used - 1] ]); /* here, the last text element in preceding complex */
if(tspi->ldir == LDIR_RL){ TR_rt_pad_set(&rt_pad_i,tri->qe, tri->qe, tri->qe + 2.0 * spcadv, 0.0); }
if(!brinfo_overlap(bri,
/* Merge the current complex into the preceding one*/
(void) brinfo_merge(bri,cxi->cx[cxidx-1].rt_cidx,cxi->cx[cxidx].rt_cidx); /* merge the bounding boxes*/
(void) cxinfo_trim(cxi);
/* remove the current complex */
}
}
}
i=j-1; /* start up after the last merged entry (there may not be any) */
}
/* phase 1.5, calculate kerning. This is as good a place to do it as any. At this point all kern values
are zero. Each of these pieces is strictly unidirectional, but each piece can have a different direction.
The direction of the line is set by the first text element. The ends of runs of elements which are
reversed with respect to the line direction are special, everything else is simple:
Let: + == L->R, - == R->L, $ == end of text, the rules for kerning on B are:
A B others xkern
[+|$] + + [+|$] Bll - Aur
[-|$] - - [-|$] All - Bur (chs)
+ - + [-|$] Bll - Aur (chs)
- + - [+|$] All - Bur
+ - -...[-=C] [+|$] All - Cur (chs)
- + +...[+=C] [-|$] Cll - Aur
chs = change sign, because dx is an absolute direction, and direction of text on RTL is in -x.
Kerning calculations currently seems unstable for R->L if the kerning extends to the end of the line. If
the first and last characters are back in sync there are no issues. When things go south R->L left justified
text is not justified when read in.
*/
tspi = &tpi->chunks[csp->kids.members[0]]; /* used here as last tsp. no kerning is applied to the first element */
rev = 0; /* the first ldir defines forward and reverse */
if(!rev){ /* back in original orientation */
if(ldir == LDIR_RL){ tspj->xkern = bri->rects[tspj->rt_tidx].xur - bri->rects[tspRevStart->rt_tidx].xll; }
}
else { /* now in reversed orientation */
/* scan forward for the last text object in this orientation, include the first */
if(tpi->chunks[csp->kids.members[k]].ldir == ldir){ tspRevEnd = &tpi->chunks[csp->kids.members[k]]; }
else { break; }
}
if(lastldir == LDIR_RL){ tspj->xkern = bri->rects[tspRevEnd->rt_tidx].xur - bri->rects[tspi->rt_tidx].xll; }
}
}
else {
}
/*
Sometimes a font substitution was absolutely terrible, for instance, for Arial Narrow on (most) Linux systems,
The resulting advance (xkern) may be much too large so that it overruns the next text chunk. Since
overlapping text on the same line is almost never encountered, this may be used to detect the bad
substitution so that a more appropriate offset can be used.
Detect this situation as a negative dx < 1/2 a space character's width while |dy| < an entire space width.
*/
}
else {
}
}
/* if x or y kern is less than twice the quantization error it is probably noise, set it to zero */
/* reintroduce spaces on the leading edge of text "j" if the kerning can be in part or in whole replaced
with 1 or 2 spaces */
sprintf((char *)newstring," "); /* start with two spaces, possibly overwrite one in the next line */
}
}
}
}
/* Phase 2, try to group sequential lines. There may be "lines" that are still TR_TEXT, as in:
... this is a sentence that wraps by one
word.
And some paragrahs might be single word lines (+ = bullet in the following)
+verbs
+nouns
+adjectives
Everything starts out as TR_PARA_UJ and if the next one can be lined up, the type changes to
an aligned paragraph and complexes are appended to the existing one.
*/
/* Must make a copy as next call may reallocate rects, so if we just passed a pointer to something in the structure
it would vaporize part way through the call. */
ok = 1;
switch (type){
case TR_PARA_UJ: /* paragraph type was set and j line does not fit, or no paragraph alignment matched */
ok = 0; /* force exit from j loop */
j--; /* this will increment at loop bottom */
break;
case TR_PARA_LJ:
case TR_PARA_CJ:
case TR_PARA_RJ:
/* two successive lines have been identified (possible following others already in the paragraph */
if(TR_check_set_vadvance(tri,j,i)){ /* check for compatibility with vadvance if set, set it if it isn't. */
ok = 0; /* force exit from j loop */
j--; /* this will increment at loop bottom */
}
else {
}
break;
default:
return(-6); /* programming error */
}
}
i=j-1;
}
/* When debugging
cxinfo_dump(tri);
*/
}
/* no doxygen documentation below this point, these pieces are for the text program, not the library. */
#if TEST
enum OP_TYPES {OPCOM,OPOOPS,OPFONT,OPESC,OPORI,OPXY,OPFS,OPTEXT,OPALN,OPLDIR,OPMUL,OPITA,OPWGT,OPDEC,OPCND,OPBKG,OPCLR,OPDCLR,OPBCLR,OPFLAGS,OPEMIT,OPDONE};
int pre;
return(OPOOPS);
}
}
}
}
return(NULL);
}
char *data;
int op;
int lineno = 0;
int status;
int flags=0;
char *infile;
int bkmode;
char *fontspec;
printf("Usage: text_reassemble input_file\n");
printf(" Test program reads an input file containing lines like:\n");
printf(" FONT:(font for next text)\n");
printf(" ESC:(escapement angle degrees of text line, up from X axis)\n");
printf(" ORI:(angle degrees of character orientation, up from X axis)\n");
printf(" FS:(font size, units)\n");
printf(" XY:(x,y) X 0 is at left, N is at right, Y 0 is at top, N is at bottom, as page is viewed.\n");
printf(" TEXT:(UTF8 text)\n");
printf(" ALN:combination of {LCR}{BLT} = Text is placed on {X,Y} at Left/Center/Right of text, at Bottom,baseLine,Top of text.\n");
printf(" LDIR:{LR|RL|TB) Left to Right, Right to Left, and Top to Bottom \n");
printf(" MUL:(float, multiplicative factor to convert FS,XY units to points).\n");
printf(" ITA:(Italics, 0=normal, 100=italics, 110=oblique).\n");
printf(" WGT:(Weight, 0-215: 80=normal, 200=bold, 215=ultrablack, 0=thin)).\n");
printf(" DEC:(this is a bit field. For color see DCLR\n");
printf(" style: 000 none, 001 underline,002 overline, 004 blink, 008 strike-through\n");
printf(" line: 000 solid, 010 double, 020 dotted, 040 dashed, 080 wavy)\n");
printf(" CND:(Condensed 50-200: 100=normal, 50=ultracondensed, 75=condensed, 200=expanded).\n");
printf(" BKG:(Background color: 0 none, 1 by input fragment, 2 by assembled line, 3 by entire assembly. Use BCLR, THEN BKG) \n");
printf(" CLR:(Text RGB color, as 6 HEX digits, like: FF0000 (red) or 0000FF (blue)) \n");
printf(" DCLR:(Decoration color, specify like CLR, except 1000000 or higher disables.)\n");
printf(" BCLR:(Background RGB color, specify like CLR.) \n");
printf(" FLAG: Special processing options. 1 EMF compatible text alignment.\n");
printf(" EMIT:(Process everything up to this point, then start clean for remaining input).\n");
printf(" DONE:(no more input, process it).\n");
printf(" # comment\n");
printf("\n");
printf(" The output is a summary of how the pieces are to be assembled into complex text.\n");
printf("\n");
printf(" egrep pattern: '^LOAD:|^FONT:|^ESC:|^ORI:|^FS:|^XY:|^TEXT:|^ALN:|^LDIR:|^MUL:|^ITA:|^WGT:|^DEC:|^CND:|^BKG:|^CLR:|^BCLR:|^DCLR:|^FLAG:|^EMIT:^DONE:'\n");
}
tri = trinfo_init(tri); /* If it loops the trinfo_clear at the end will reset tri to the proper state, do NOT call trinfo_init twice! */
#ifdef DBG_LOOP
int ldx;
#endif
tsp.x = 0.0;
tsp.y = 0.0;
tsp.boff = 0.0; /* offset to baseline from LL corner of bounding rectangle, changes with fs and taln*/
/* no need to set rt_tidx */
if(!tri){
}
while(ok){
lineno++;
switch(op){
case OPCOM: /* ignore comments*/
break;
case OPFONT:
/* If the font name includes "Narrow" condensed may not have been set */
}
else {
}
break;
case OPESC:
break;
case OPORI:
break;
case OPFS:
break;
case OPXY:
break;
case OPTEXT:
/* FreeType parameters match inkscape*/
}
break;
case OPALN:
switch (*data++){
}
switch (*data++){
}
break;
case OPLDIR:
break;
case OPMUL:
break;
case OPITA:
if(1 != sscanf(data,"%d",&tsp.italics) || tsp.italics < 0 || tsp.italics>110)boom("Invalid ITA:",lineno);
break;
case OPWGT:
if(1 != sscanf(data,"%d",&tsp.weight) || tsp.weight < 0 || tsp.weight > 215)boom("Invalid WGT:",lineno);
break;
case OPDEC:
break;
case OPCND:
if(1 != sscanf(data,"%d",&tsp.condensed) || tsp.condensed < 50 || tsp.condensed > 200)boom("Invalid CND:",lineno);
break;
case OPBKG:
break;
case OPCLR:
break;
case OPDCLR:
if(utmp32 >= 0x1000000){
}
else {
}
break;
case OPBCLR:
break;
case OPFLAGS:
break;
case OPEMIT:
break;
case OPDONE:
ok = 0;
break;
case OPOOPS:
default:
break;
}
}
if(fpo){
}
#ifdef DBG_LOOP
ok = 1;
}
#endif /* DBG_LOOP */
}
#endif /* TEST */
#ifdef __cplusplus
}
#endif