]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/paragraph.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / samples / layout / paragraph.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: Paragraph.cpp
9 *
10 * created on: 09/06/2000
11 * created by: Eric R. Mader
12 */
13
14 #include "unicode/utypes.h"
15 #include "unicode/uchar.h"
16 #include "unicode/ubidi.h"
17 #include "unicode/ustring.h"
18
19 #include "layout/ParagraphLayout.h"
20
21 #include "RenderingSurface.h"
22 #include "ScriptCompositeFontInstance.h"
23
24 #include "paragraph.h"
25 #include "UnicodeReader.h"
26 #include "FontMap.h"
27
28 #define MARGIN 10
29 #define LINE_GROW 32
30 #define PARA_GROW 8
31
32 #define CH_LF 0x000A
33 #define CH_CR 0x000D
34 #define CH_LSEP 0x2028
35 #define CH_PSEP 0x2029
36
37 static LEUnicode *skipLineEnd(LEUnicode *ptr)
38 {
39 if (ptr[0] == CH_CR && ptr[1] == CH_LF) {
40 ptr += 1;
41 }
42
43 return ptr + 1;
44 }
45
46 static le_int32 findRun(const RunArray *runArray, le_int32 offset)
47 {
48 le_int32 runCount = runArray->getCount();
49
50 for (le_int32 run = 0; run < runCount; run += 1) {
51 if (runArray->getLimit(run) > offset) {
52 return run;
53 }
54 }
55
56 return -1;
57 }
58
59 static void subsetFontRuns(const FontRuns *fontRuns, le_int32 start, le_int32 limit, FontRuns *sub)
60 {
61 le_int32 startRun = findRun(fontRuns, start);
62 le_int32 endRun = findRun(fontRuns, limit - 1);
63
64 sub->reset();
65
66 for (le_int32 run = startRun; run <= endRun; run += 1) {
67 const LEFontInstance *runFont = fontRuns->getFont(run);
68 le_int32 runLimit = fontRuns->getLimit(run) - start;
69
70 if (run == endRun) {
71 runLimit = limit - start;
72 }
73
74 sub->add(runFont, runLimit);
75 }
76 }
77
78 Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns, LEErrorCode &status)
79 : fParagraphLayout(NULL), fParagraphCount(0), fParagraphMax(PARA_GROW), fParagraphGrow(PARA_GROW),
80 fLineCount(0), fLinesMax(LINE_GROW), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL),
81 fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR)
82 {
83 static const LEUnicode separators[] = {CH_LF, CH_CR, CH_LSEP, CH_PSEP};
84
85 if (LE_FAILURE(status)) {
86 return;
87 }
88
89 le_int32 ascent = 0;
90 le_int32 descent = 0;
91 le_int32 leading = 0;
92
93 LocaleRuns *locales = NULL;
94 FontRuns fr(0);
95
96 fLines = LE_NEW_ARRAY(const ParagraphLayout::Line *, fLinesMax);
97 fParagraphLayout = LE_NEW_ARRAY(ParagraphLayout *, fParagraphMax);
98
99 fChars = LE_NEW_ARRAY(LEUnicode, charCount + 1);
100 LE_ARRAY_COPY(fChars, chars, charCount);
101 fChars[charCount] = 0;
102
103 LEUnicode *pStart = &fChars[0];
104
105 while (*pStart != 0) {
106 LEUnicode *pEnd = u_strpbrk(pStart, separators);
107 le_int32 pAscent, pDescent, pLeading;
108 ParagraphLayout *paragraphLayout = NULL;
109
110 if (pEnd == NULL) {
111 pEnd = &fChars[charCount];
112 }
113
114 if (pEnd != pStart) {
115 subsetFontRuns(fontRuns, pStart - fChars, pEnd - fChars, &fr);
116
117 paragraphLayout = new ParagraphLayout(pStart, pEnd - pStart, &fr, NULL, NULL, locales, fParagraphLevel, FALSE, status);
118
119 if (LE_FAILURE(status)) {
120 break; // return? something else?
121 }
122
123 if (fParagraphLevel == UBIDI_DEFAULT_LTR) {
124 fParagraphLevel = paragraphLayout->getParagraphLevel();
125 }
126
127 pAscent = paragraphLayout->getAscent();
128 pDescent = paragraphLayout->getDescent();
129 pLeading = paragraphLayout->getLeading();
130
131 if (pAscent > ascent) {
132 ascent = pAscent;
133 }
134
135 if (pDescent > descent) {
136 descent = pDescent;
137 }
138
139 if (pLeading > leading) {
140 leading = pLeading;
141 }
142 }
143
144 if (fParagraphCount >= fParagraphMax) {
145 fParagraphLayout = (ParagraphLayout **) LE_GROW_ARRAY(fParagraphLayout, fParagraphMax + fParagraphGrow);
146 fParagraphMax += fParagraphGrow;
147 }
148
149 fParagraphLayout[fParagraphCount++] = paragraphLayout;
150
151 if (*pEnd == 0) {
152 break;
153 }
154
155 pStart = skipLineEnd(pEnd);
156 }
157
158 fLineHeight = ascent + descent + leading;
159 fAscent = ascent;
160 }
161
162 Paragraph::~Paragraph()
163 {
164 for (le_int32 line = 0; line < fLineCount; line += 1) {
165 delete /*(LineInfo *)*/ fLines[line];
166 }
167
168 LE_DELETE_ARRAY(fLines);
169 delete fParagraphLayout;
170 LE_DELETE_ARRAY(fChars);
171 }
172
173 void Paragraph::addLine(const ParagraphLayout::Line *line)
174 {
175 if (fLineCount >= fLinesMax) {
176 fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow);
177 fLinesMax += fLinesGrow;
178 }
179
180 fLines[fLineCount++] = line;
181 }
182
183 void Paragraph::breakLines(le_int32 width, le_int32 height)
184 {
185 fHeight = height;
186
187 // don't re-break if the width hasn't changed
188 if (fWidth == width) {
189 return;
190 }
191
192 fWidth = width;
193
194 float lineWidth = (float) (width - 2 * MARGIN);
195 const ParagraphLayout::Line *line;
196
197 // Free the old LineInfo's...
198 for (le_int32 li = 0; li < fLineCount; li += 1) {
199 delete fLines[li];
200 }
201
202 fLineCount = 0;
203
204 for (le_int32 p = 0; p < fParagraphCount; p += 1) {
205 ParagraphLayout *paragraphLayout = fParagraphLayout[p];
206
207 if (paragraphLayout != NULL) {
208 paragraphLayout->reflow();
209 while ((line = paragraphLayout->nextLine(lineWidth)) != NULL) {
210 addLine(line);
211 }
212 } else {
213 addLine(NULL);
214 }
215 }
216 }
217
218 void Paragraph::draw(RenderingSurface *surface, le_int32 firstLine, le_int32 lastLine)
219 {
220 le_int32 li, x, y;
221
222 x = MARGIN;
223 y = fAscent;
224
225 for (li = firstLine; li <= lastLine; li += 1) {
226 const ParagraphLayout::Line *line = fLines[li];
227
228 if (line != NULL) {
229 le_int32 runCount = line->countRuns();
230 le_int32 run;
231
232 if (fParagraphLevel == UBIDI_RTL) {
233 le_int32 lastX = line->getWidth();
234
235 x = (fWidth - lastX - MARGIN);
236 }
237
238
239 for (run = 0; run < runCount; run += 1) {
240 const ParagraphLayout::VisualRun *visualRun = line->getVisualRun(run);
241 le_int32 glyphCount = visualRun->getGlyphCount();
242 const LEFontInstance *font = visualRun->getFont();
243 const LEGlyphID *glyphs = visualRun->getGlyphs();
244 const float *positions = visualRun->getPositions();
245
246 surface->drawGlyphs(font, glyphs, glyphCount, positions, x, y, fWidth, fHeight);
247 }
248 }
249
250 y += fLineHeight;
251 }
252 }
253
254 Paragraph *Paragraph::paragraphFactory(const char *fileName, const LEFontInstance *font, GUISupport *guiSupport)
255 {
256 LEErrorCode status = LE_NO_ERROR;
257 le_int32 charCount;
258 const UChar *text = UnicodeReader::readFile(fileName, guiSupport, charCount);
259 Paragraph *result = NULL;
260
261 if (text == NULL) {
262 return NULL;
263 }
264
265 FontRuns fontRuns(0);
266
267 fontRuns.add(font, charCount);
268
269 result = new Paragraph(text, charCount, &fontRuns, status);
270
271 if (LE_FAILURE(status)) {
272 delete result;
273 result = NULL;
274 }
275
276 LE_DELETE_ARRAY(text);
277
278 return result;
279 }
280