TextWrapper.cpp revision fa5a3d9afe76005fecf8e89eebd344696400efda
/*
* testICU
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "TextWrapper.h"
#include <libnrtype/font-instance.h>
#include "libnrtype/text-boundary.h"
#include "libnrtype/one-glyph.h"
#include "libnrtype/one-para.h"
text_wrapper::text_wrapper(void)
{
// voids everything
uni32_text = NULL;
glyph_text = NULL;
utf8_length = 0;
uni32_length = 0;
glyph_length = 0;
default_font = NULL;
last_addition = -1;
// inits the pangolayout with default params
}
text_wrapper::~text_wrapper(void)
{
// frees everything
//printf("delete\n");
for (unsigned i = 0; i < nbBound; i++) {
default:
break;
}
}
default_font = NULL;
}
{
// refcounts the font for our internal uses
}
{
// appends text to what needs to be handled
if ( utf8_length <= 0 ) {
// a first check to prevent the text from containing a leading line return (which
// is probably a bug anyway)
/* fixme: Should the below be `0 <= len' ? The existing code looks wrong
* for the case that len==0.
* TODO: Document the meaning of the len parameter. */
if ( len > 0 ) {
} else {
}
}
}
// compute the length
: len );
/* effic: Use g_utf8_validate's last param to do this. */
// prepare to store the additional text
/* effic: (Not an issue for the sole caller at the time of writing.) This implementation
takes quadratic time if the text is composed of n appends. Use a proper data structure.
STL vector would suffice. */
{
}
else
{
g_warning("Failed to reallocate utf8_text");
}
int* newdata2 = static_cast<int*>(realloc(uni32_codepoint, (utf8_length + nlen + 1) * sizeof(int)));
{
}
else
{
g_warning("Failed to reallocate uni32_codepoint");
}
// copy the source text in the newly lengthened array
utf8_length += nlen;
utf8_text[utf8_length] = 0;
// remember where the text ended, before we recompute it, for the dx/dy we'll add after that (if any)
// free old uni32 structures (instead of incrementally putting the text)
uni32_text = NULL;
uni32_length = 0;
{
// recompute length of uni32 text
char *p = utf8_text;
while ( *p ) {
p = g_utf8_next_char(p); // since we validated the input text, we can use this 'fast' macro
uni32_length++;
}
}
// realloc the arrays
{
// read the utf8 string and compute codepoints positions
char *p = utf8_text;
int i = 0;
int l_o = 0;
while ( *p ) {
// get the new codepoint
uni32_text[i] = g_utf8_get_char(p);
// compute the offset in the utf8_string
// record the codepoint's start
utf8_codepoint[i] = n_o;
// record the codepoint's correspondance in the utf8 string
// and move on
p = g_utf8_next_char(p);
i++;
}
// the termination of the loop
uni32_text[uni32_length] = 0;
}
// these will be filled by a KernXForLastAddition() right after this function
// note that the SVG spec doesn't require you to give a dx for each codepoint,
// so setting the dx to 0 is mandatory
if ( uni32_length > last_addition ) {
if ( kern_x ) {
{
}
else
{
g_warning("Failed to reallocate kern_x");
}
}
if ( kern_y ) {
{
}
else
{
g_warning("Failed to reallocate kern_y");
}
}
}
}
void text_wrapper::DoLayout(void)
{
// THE function
// first some sanity checks
if ( default_font == NULL ) return;
if ( uni32_length <= 0 || utf8_length <= 0 ) return;
// prepare the pangolayout object
{
//char *tc = pango_font_description_to_string(default_font->descr);
//printf("layout with %s\n", tc);
//free(tc);
}
// reset the glyph string
glyph_text = NULL;
glyph_length = 0;
int max_g = 0;
do {
double plY = (1.0 / ((double)PANGO_SCALE)) * ((double)log_r.y); // start position of this line of the layout
while ( curR ) {
if ( pRun ) {
// a run has uniform font/directionality/etc...
for (int i = 0; i < pRun->glyphs->num_glyphs; i++) { // add glyph sequentially, reading them from the run
// realloc the structures
if ( glyph_length >= max_g ) {
{
}
else
{
g_warning("Failed to reallocate glyph_text");
}
}
// fill the glyph info
// depending on the directionality, the last uni32 codepoint for this glyph is the first of the next char
// or the first of the previous
// rtl
}
glyph_text[glyph_length + 1].uni_dir = 1; // set the directionality for the next too, so that the last glyph in
// the array has the correct direction
} else {
// ltr
if ( i > 0 ) {
}
}
// set the position
// the layout is an infinite line
glyph_text[glyph_length].x = plX + pango_to_ink * ((double)pRun->glyphs->glyphs[i].geometry.x_offset);
glyph_text[glyph_length].y = plY + pango_to_ink * ((double)pRun->glyphs->glyphs[i].geometry.y_offset);
// advance to the next glyph
// and set the next glyph's position, in case it's the terminating glyph
glyph_length++;
}
// and finish filling the info
// notably, the uni_en of the last char in ltr text and the uni_en of the first in rtl are still not set
// rtl
} else {
if ( glyph_length > 0 ) glyph_text[glyph_length - 1].uni_en = plOffset + prOffset + pRun->item->length;
}
// the terminating glyph has glyph_id=0 because it means 'no glyph'
// and is associated with no text (but you cannot set uni_st=uni_en=0, because the termination
// is expected to be the glyph for the termination of the uni32_text)
glyph_text[glyph_length].uni_st = glyph_text[glyph_length].uni_en = plOffset + prOffset + pRun->item->length;
}
}
} while ( pango_layout_iter_next_line(pIter) );
// grunt work done. now some additional info for layout: computing letters, mostly (one letter = several glyphs sometimes)
int nbAttr = 0;
// get the layout attrs, they hold the boundaries pango computed
// feed to MakeTextBoundaries which knows what to do with these
// the array of boundaries is full, but out-of-order
// boundary array is ready to be used, call chunktext to fill the *_start fields of the glyphs, and compute
// the boxed version of the text for sp-typeset
ChunkText();
// get rid of the attributes
// cleaning up
for (int i = 0; i < glyph_length; i++) {
glyph_text[i].y /= 512;
}
if ( glyph_length > 0 ) {
}
}
void text_wrapper::ChunkText(void)
{
for (int i = 0; i < glyph_length; i++) {
glyph_text[i].char_start = false;
glyph_text[i].word_start = false;
glyph_text[i].para_start = false;
// boundaries depend on the directionality
// letter boundaries correspond to the glyphs starting one letter when you read them left to right (always)
// because that's the order they are stored into in the glyph_text array
if ( glyph_text[i].uni_dir == 0 ) {
if ( IsBound(bnd_char, g_st, c_st) ) { // check if there is a charcater (=letter in pango speak) at this position
// can be a 'start' boundary or a 'end' boundary, doesn't matter, as long
// as you get from one letter to the next at this position
}
}
}
} else {
}
}
}
}
}
if ( glyph_length > 0 ) {
}
{
// doing little boxes
// check uniformity of fonts
bool first = true;
do {
do {
n_en++;
{
}
else
{
g_warning("Failed to reallocate boxes");
}
}
nbBox++;
first = false;
}
}
}
{
// doing little paras
int b_st = 0;
{
}
else
{
g_warning("Failed to reallocate paras");
}
}
nbPara++;
}
}
}
}
}
void text_wrapper::MakeVertical(void)
{
if ( glyph_length <= 0 ) return;
// explanation: when laying out text vertically, you must keep the glyphs of a single letter together
double baseY = glyph_text[0].y;
int nbLetter = 0;
do {
// move to the next letter boundary
do {
g_en++;
// got a letter
// we need to compute the letter's width (in case sometimes we implement the flushleft and flushright)
// and the total height for this letter. for example accents usually have 0 width, so this is not
// stupid
double n_adv = 0;
// so we need to update curF
if ( curPF ) {
}
}
double x = ( curF
: 0 );
}
// and put the glyphs of this letter at their new position
glyph_text[i].x -= minX;
glyph_text[i].y += lastY;
}
}
nbLetter++;
} while ( g_st < glyph_length );
}
void text_wrapper::MergeWhiteSpace(void)
{
if ( glyph_length <= 0 ) return;
// scans the glyphs and shifts them accordingly
bool inWhite = true;
int wpos = 0, rpos = 0; // wpos is the position where we read glyphs, rpos is the position where we write them back
// since we only eat whitespace, wpos <= rpos
// copy the glyph at its new position
wpos++; // move the write position
if ( inWhite ) {
// eat me: 2 steps: first add the shift in position to the cumulated shift
// then move the write position back. this way, we'll overwrite the previous whitespace with the new glyph
// since this is only done after the first whitespace, we only keep the first whitespace
wpos--;
}
inWhite = true;
} else {
inWhite = false;
}
}
// and the terminating glyph (we should probably copy the rest of the glyph's info, too)
// sets the new length
glyph_length = wpos;
}
// utility: computes the number of letters in the layout
{
if ( glyph_length <= 0 ) return 0;
g_st = 0;
g_en = glyph_length;
}
int nbLetter = 0;
}
return nbLetter;
}
{
if ( glyph_length <= 0 ) return;
g_st = 0;
g_en = glyph_length;
}
int nbLetter = 0;
// letterspacing means: add 'dx * (nbLetter - 1)' to the x position
// so we just scan the glyph string
}
}
/** @name Movement commands
* Miscellaneous functions for moving about glyphs.
* \a st and \en are start and end glyph indices.
* The three methods differ only in whether they look for .char_start, .word_start or .para_start.
* \return True iff a next character was found. (False iff we've already reached the end.)
*/
//@{
{
do {
en++;
return true;
}
{
do {
en++;
return true;
}
{
do {
en++;
return true;
}
//@}
// boundary handling
/**
* Append \a ib to our bounds array.
* \return The index of the new element.
*/
{
text_boundary *newdata = static_cast<text_boundary*>(realloc(bounds, maxBound * sizeof(text_boundary)));
{
}
else
{
g_warning("Failed to reallocate bounds");
}
}
return ix;
}
/**
* Add the start \& end boundaries \a is \& \a ie to bounds.
*/
{
}
static int CmpBound(void const *a, void const *b) {
/* TODO: I'd guess that for a given uni_pos it would be better for the end boundary to precede the start boundary. */
return 0;
}
/**
* Sort this.bounds by b.uni_pos, updating the .other index values appropriately.
*/
void text_wrapper::SortBoundaries(void)
{
/* effic: If this function (including descendents such as the qsort calll) ever takes
* non-negligible time, then we can fairly easily improve it by changing MakeBoundaries add in
* sorted order. It would just have to remember for itself the index of each start boundary
* for updating the .other fields appropriately.
*
* A simpler speedup is just to change qsort to std::sort, which can inline the comparison
* function.
*/
/* The 'other' field needs to be updated after sorting by qsort, so we build the inverse
* permutation. */
for (unsigned i = 0; i < nbBound; i++) {
}
}
for (unsigned i = 0; i < nbBound; i++) { // update 'other'
}
}
}
{
int last_c_st = -1;
int last_w_st = -1;
int last_s_st = -1;
int last_p_st = 0;
// reads the text and adds a pair of boundaries each time we encounter a stop
// last_* are used to keep track of the start of new text chunk
for (int i = 0; i <= nAttr; i++) {
// letters
if ( last_c_st >= 0 ) {
}
last_c_st = i;
}
// words
if ( last_w_st >= 0 ) {
}
last_w_st = i;
}
if ( last_w_st >= 0 ) {
}
last_w_st = i;
}
// sentences
if ( last_s_st >= 0 ) {
}
last_s_st = i;
}
// paragraphs
last_p_st = i + 1;
}
}
}
{
int scan_dir = 0;
return true;
}
if ( scan_dir < 0 ) break;
c_st++;
scan_dir = 1;
if ( scan_dir > 0 ) break;
c_st--;
scan_dir = -1;
} else {
// good pos, wrong type
c_st--;
}
return true;
}
c_st++;
}
break;
}
}
return false;
}
/* Unused. Retained only because I haven't asked cyreve (Richard Hughes) whether he intends ever
* to use it. You can probably safely remove it. */
//bool text_wrapper::Contains(BoundaryType const bnd_type, int g_st, int g_en, int &c_st, int &c_en)
//{
// if ( c_st < 0 ) c_st = 0;
// bool found = false;
// int scan_dir = 0;
// while ( unsigned(c_st) < nbBound ) {
// if ( bounds[c_st].type == bnd_type ) {
// if ( bounds[c_st].start ) {
// c_en = bounds[c_st].other;
// } else {
// }
// }
// if ( bounds[c_st].type == bnd_type && unsigned(c_en) == bounds[c_st].other ) {
// if ( g_st >= bounds[c_st].uni_pos && g_en <= bounds[c_en].uni_pos ) {
// // character found
// found = true;
// break;
// }
// }
// if ( bounds[c_st].uni_pos < g_st ) {
// if ( scan_dir < 0 ) break;
// c_st++;
// scan_dir = 1;
// } else if ( bounds[c_st].uni_pos > g_st ) {
// if ( scan_dir > 0 ) break;
// c_st--;
// scan_dir = -1;
// } else {
// // good pos, wrong type
// while ( c_st > 0 && bounds[c_st].uni_pos == g_st ) {
// c_st--;
// }
// if ( bounds[c_st].uni_pos < g_st ) c_st++;
// while ( unsigned(c_st) < nbBound && bounds[c_st].uni_pos == g_st ) {
// if ( bounds[c_st].type == bnd_type ) {
// if ( bounds[c_st].start ) {
// c_en = bounds[c_st].other;
// } else {
// }
// }
// if ( bounds[c_st].type == bnd_type && unsigned(c_en) == bounds[c_st].other ) {
// if ( g_st >= bounds[c_st].uni_pos && g_en <= bounds[c_en].uni_pos ) {
// // character found
// return true;
// }
// }
// c_st++;
// }
//
// break;
// }
// }
// return found;
//}
void text_wrapper::MeasureBoxes(void)
{
for (int i = 0; i < nbBox; i++) {
if ( curPF ) {
if ( curF ) {
}
}
}
}
{
if ( i_kern_x == NULL || i_len <= 0 || last_addition < 0 || last_addition >= uni32_length || uni32_length <= 0 ) return;
for (int i = 0; i <= uni32_length; i++) kern_x[i] = 0;
}
}
{
if ( i_kern_y == NULL || i_len <= 0 || last_addition < 0 || last_addition >= uni32_length || uni32_length <= 0 ) return;
for (int i = 0; i <= uni32_length; i++) kern_y[i] = 0;
}
}
{
if ( i_kern_x == NULL || last_addition < 0 || last_addition >= uni32_length || uni32_length <= 0 ) return;
for (int i = 0; i <= uni32_length; i++) kern_x[i] = 0;
}
}
}
{
if ( i_kern_y == NULL || last_addition < 0 || last_addition >= uni32_length || uni32_length <= 0 ) return;
for (int i = 0; i <= uni32_length; i++) kern_y[i] = 0;
}
}
}
void text_wrapper::AddDxDy(void)
{
if ( glyph_length <= 0 ) return;
if ( kern_x ) {
double sum = 0;
int l_pos = -1;
for (int i = 0; i < glyph_length; i++) {
}
glyph_text[i].x += sum;
}
{
int n_pos = uni32_length;
}
// l_pos = n_pos;
}
}
if ( kern_y ) {
double sum = 0;
int l_pos = -1;
for (int i = 0; i < glyph_length; i++) {
}
glyph_text[i].y += sum;
}
{
int n_pos = uni32_length;
}
// l_pos = n_pos;
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :