2 *******************************************************************************
4 * Copyright (C) 1999-2007, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: Paragraph.cpp
10 * created on: 09/06/2000
11 * created by: Eric R. Mader
14 #include "unicode/utypes.h"
15 #include "unicode/uchar.h"
16 #include "unicode/ubidi.h"
17 #include "unicode/ustring.h"
19 #include "layout/ParagraphLayout.h"
21 #include "RenderingSurface.h"
23 #include "paragraph.h"
24 #include "UnicodeReader.h"
32 #define CH_LSEP 0x2028
33 #define CH_PSEP 0x2029
35 static LEUnicode *skipLineEnd(LEUnicode *ptr)
37 if (ptr[0] == CH_CR && ptr[1] == CH_LF) {
44 static le_int32 findRun(const RunArray *runArray, le_int32 offset)
46 le_int32 runCount = runArray->getCount();
48 for (le_int32 run = 0; run < runCount; run += 1) {
49 if (runArray->getLimit(run) > offset) {
57 static void subsetFontRuns(const FontRuns *fontRuns, le_int32 start, le_int32 limit, FontRuns *sub)
59 le_int32 startRun = findRun(fontRuns, start);
60 le_int32 endRun = findRun(fontRuns, limit - 1);
64 for (le_int32 run = startRun; run <= endRun; run += 1) {
65 const LEFontInstance *runFont = fontRuns->getFont(run);
66 le_int32 runLimit = fontRuns->getLimit(run) - start;
69 runLimit = limit - start;
72 sub->add(runFont, runLimit);
76 Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns, LEErrorCode &status)
77 : fParagraphLayout(NULL), fParagraphCount(0), fParagraphMax(PARA_GROW), fParagraphGrow(PARA_GROW),
78 fLineCount(0), fLinesMax(LINE_GROW), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL),
79 fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR)
81 static const LEUnicode separators[] = {CH_LF, CH_CR, CH_LSEP, CH_PSEP, 0x0000};
83 if (LE_FAILURE(status)) {
91 LocaleRuns *locales = NULL;
94 fLines = LE_NEW_ARRAY(const ParagraphLayout::Line *, fLinesMax);
95 fParagraphLayout = LE_NEW_ARRAY(ParagraphLayout *, fParagraphMax);
97 fChars = LE_NEW_ARRAY(LEUnicode, charCount + 1);
98 LE_ARRAY_COPY(fChars, chars, charCount);
99 fChars[charCount] = 0;
101 LEUnicode *pStart = &fChars[0];
103 while (*pStart != 0) {
104 LEUnicode *pEnd = u_strpbrk(pStart, separators);
105 le_int32 pAscent, pDescent, pLeading;
106 ParagraphLayout *paragraphLayout = NULL;
109 pEnd = &fChars[charCount];
112 if (pEnd != pStart) {
113 subsetFontRuns(fontRuns, pStart - fChars, pEnd - fChars, &fr);
115 paragraphLayout = new ParagraphLayout(pStart, pEnd - pStart, &fr, NULL, NULL, locales, fParagraphLevel, FALSE, status);
117 if (LE_FAILURE(status)) {
118 break; // return? something else?
121 if (fParagraphLevel == UBIDI_DEFAULT_LTR) {
122 fParagraphLevel = paragraphLayout->getParagraphLevel();
125 pAscent = paragraphLayout->getAscent();
126 pDescent = paragraphLayout->getDescent();
127 pLeading = paragraphLayout->getLeading();
129 if (pAscent > ascent) {
133 if (pDescent > descent) {
137 if (pLeading > leading) {
142 if (fParagraphCount >= fParagraphMax) {
143 fParagraphLayout = (ParagraphLayout **) LE_GROW_ARRAY(fParagraphLayout, fParagraphMax + fParagraphGrow);
144 fParagraphMax += fParagraphGrow;
147 fParagraphLayout[fParagraphCount++] = paragraphLayout;
153 pStart = skipLineEnd(pEnd);
156 fLineHeight = ascent + descent + leading;
160 Paragraph::~Paragraph()
162 for (le_int32 line = 0; line < fLineCount; line += 1) {
163 delete /*(LineInfo *)*/ fLines[line];
166 LE_DELETE_ARRAY(fLines);
167 delete fParagraphLayout;
168 LE_DELETE_ARRAY(fChars);
171 void Paragraph::addLine(const ParagraphLayout::Line *line)
173 if (fLineCount >= fLinesMax) {
174 fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow);
175 fLinesMax += fLinesGrow;
178 fLines[fLineCount++] = line;
181 void Paragraph::breakLines(le_int32 width, le_int32 height)
185 // don't re-break if the width hasn't changed
186 if (fWidth == width) {
192 float lineWidth = (float) (width - 2 * MARGIN);
193 const ParagraphLayout::Line *line;
195 // Free the old LineInfo's...
196 for (le_int32 li = 0; li < fLineCount; li += 1) {
202 for (le_int32 p = 0; p < fParagraphCount; p += 1) {
203 ParagraphLayout *paragraphLayout = fParagraphLayout[p];
205 if (paragraphLayout != NULL) {
206 paragraphLayout->reflow();
207 while ((line = paragraphLayout->nextLine(lineWidth)) != NULL) {
216 void Paragraph::draw(RenderingSurface *surface, le_int32 firstLine, le_int32 lastLine)
223 for (li = firstLine; li <= lastLine; li += 1) {
224 const ParagraphLayout::Line *line = fLines[li];
227 le_int32 runCount = line->countRuns();
230 if (fParagraphLevel == UBIDI_RTL) {
231 le_int32 lastX = line->getWidth();
233 x = (fWidth - lastX - MARGIN);
237 for (run = 0; run < runCount; run += 1) {
238 const ParagraphLayout::VisualRun *visualRun = line->getVisualRun(run);
239 le_int32 glyphCount = visualRun->getGlyphCount();
240 const LEFontInstance *font = visualRun->getFont();
241 const LEGlyphID *glyphs = visualRun->getGlyphs();
242 const float *positions = visualRun->getPositions();
244 surface->drawGlyphs(font, glyphs, glyphCount, positions, x, y, fWidth, fHeight);
252 Paragraph *Paragraph::paragraphFactory(const char *fileName, const LEFontInstance *font, GUISupport *guiSupport)
254 LEErrorCode status = LE_NO_ERROR;
256 const UChar *text = UnicodeReader::readFile(fileName, guiSupport, charCount);
257 Paragraph *result = NULL;
263 FontRuns fontRuns(0);
265 fontRuns.add(font, charCount);
267 result = new Paragraph(text, charCount, &fontRuns, status);
269 if (LE_FAILURE(status)) {
274 LE_DELETE_ARRAY(text);