1 /*
2 * TextWrapper.h
3 * testICU
4 *
5 */
7 #ifndef my_text_wrapper
8 #define my_text_wrapper
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include <pango/pango.h>
16 #include <libnrtype/nrtype-forward.h>
17 #include "libnrtype/boundary-type.h"
19 // miscanellous but useful data for a given text: chunking into logical pieces
20 // pieces include sentence/word, needed for example for the word-spacing property,
21 // and more important stuff like letter (ie visual letters)
23 struct text_boundary;
24 struct one_glyph;
25 struct one_box;
26 struct one_para;
28 class text_wrapper {
29 public:
30 char *utf8_text; // source text
31 gunichar *uni32_text; // ucs4 text computed from utf8_text
32 one_glyph *glyph_text; // glyph string computed for uni32_text
34 // maps between the 2
35 // These should most definitely be size_t, not int.
36 // I am quite sure (but not bored enough to actually test it
37 // on a 500MHz machine with 256MB RAM ) that this will crash
38 // for text longer than 2GB on architectures where
39 // sizeof(size_t) != sizeof(int)
40 int utf8_length; // utf8_text length
41 int uni32_length; // uni32_text length
42 int glyph_length; /**< Number of glyph in the glyph_text array.
43 * The size of the array is (glyph_length+1) in fact; the last glyph is kind of a '0' char. */
44 int *uni32_codepoint; // uni32_codepoint[i] is the index in uni32_text corresponding to utf8_text[i]
45 int *utf8_codepoint; // utf8_codepoint[i] is the index in utf8_text of the beginning of uni32_text[i]
47 // layout
48 font_instance *default_font; // font set as the default font (would need at least one alternate per language)
49 PangoLayout *pLayout; // private structure
51 // kerning additions
52 int last_addition; // index in uni32_text of the beginning of the text added by the last AppendUTF8 call
53 double *kern_x; // dx[i] is the dx for the ith unicode char
54 double *kern_y;
56 // boundaries, in an array
57 unsigned nbBound, maxBound;
58 text_boundary *bounds;
60 // text organization
61 int nbBox, maxBox;
62 one_box *boxes;
63 int nbPara, maxPara;
64 one_para *paras;
66 text_wrapper(void);
67 virtual ~text_wrapper(void);
69 // filling the structure with input data
70 void SetDefaultFont(font_instance *iFont);
72 /**
73 * Append the specified text to utf8_text and uni32_codepoint.
74 *
75 * Note: Despite the name, the current implementation is primarily suited for a single
76 * call to set the text, rather than repeated calls to AppendUTF8: the implementation is
77 * Omega(n) in the new total length of the string, rather than just in the length of the
78 * text being appended. This can probably be addressed fairly easily (see comments in
79 * code) if this is an issue for new callers.
80 *
81 * \pre text is valid UTF-8, or null.
82 * Formally: text==NULL || g_utf8_validate(text, len, NULL).
83 *
84 * \param len Our sole existing caller (widgets/font_selector.cpp) uses len=-1. N.B. The current
85 * implementation may be buggy for non-negative len, especially for len==0.
86 */
87 void AppendUTF8(char const *text, int len);
89 // adds dx or dy for the text added by the last AppendUTF8() call
90 void KernXForLastAddition(double *i_kern_x, int i_len, double scale = 1.0);
91 void KernYForLastAddition(double *i_kern_y, int i_len, double scale = 1.0);
92 void KernXForLastAddition(GList *i_kern_x, double scale = 1.0);
93 void KernYForLastAddition(GList *i_kern_y, double scale = 1.0);
94 // compute the layout and stuff
95 void DoLayout(void);
96 // semi-private: computes boundaries in the input text
97 void ChunkText(void);
98 // utility function to move to the next element
99 bool NextChar(int &st, int &en) const;
100 bool NextWord(int &st, int &en) const;
101 bool NextPara(int &st, int &en) const;
103 // post-processing after the initial layout
104 // for the xml-space property: merges consecutive whitespace, and eats leading whitespace in the text
105 void MergeWhiteSpace(void);
106 // makes vertical 'x' and 'y' fields in the glyph_text based on the computed positions
107 void MakeVertical(void);
108 // as the names says...
109 void AddLetterSpacing(double dx, double dy, int g_st = -1, int g_en = -1);
110 // adds the kerning specified by the KernXForLastAddition call to the layout
111 void AddDxDy(void);
113 // boundary handling
114 private:
115 unsigned AddBoundary(text_boundary const &ib);
116 public:
117 void AddTwinBoundaries(text_boundary const &is, text_boundary const &ie);
118 void SortBoundaries(void);
119 void MakeTextBoundaries(PangoLogAttr *pAttrs, int nAttr);
120 //bool Contains(BoundaryType type, int g_st, int g_en, int &c_st, int &c_en);
121 bool IsBound(BoundaryType type, int g_st, int &c_st);
123 void MeasureBoxes(void);
124 int NbLetter(int g_st, int g_en);
125 };
127 #endif
130 /*
131 Local Variables:
132 mode:c++
133 c-file-style:"stroustrup"
134 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135 indent-tabs-mode:nil
136 fill-column:99
137 End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :