2 *******************************************************************************
4 * Copyright (C) 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 1999-2015, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: Paragraph.cpp
16 * created on: 09/06/2000
17 * created by: Eric R. Mader
20 #include "unicode/utypes.h"
21 #include "unicode/uchar.h"
22 #include "unicode/ubidi.h"
23 #include "unicode/ustring.h"
25 #include "layout/ParagraphLayout.h"
27 #include "RenderingSurface.h"
29 #include "paragraph.h"
30 #include "UnicodeReader.h"
38 #define CH_LSEP 0x2028
39 #define CH_PSEP 0x2029
41 static LEUnicode *skipLineEnd(LEUnicode *ptr)
43 if (ptr[0] == CH_CR && ptr[1] == CH_LF) {
50 static le_int32 findRun(const RunArray *runArray, le_int32 offset)
52 le_int32 runCount = runArray->getCount();
54 for (le_int32 run = 0; run < runCount; run += 1) {
55 if (runArray->getLimit(run) > offset) {
63 static void subsetFontRuns(const FontRuns *fontRuns, le_int32 start, le_int32 limit, FontRuns *sub)
65 le_int32 startRun = findRun(fontRuns, start);
66 le_int32 endRun = findRun(fontRuns, limit - 1);
70 for (le_int32 run = startRun; run <= endRun; run += 1) {
71 const LEFontInstance *runFont = fontRuns->getFont(run);
72 le_int32 runLimit = fontRuns->getLimit(run) - start;
75 runLimit = limit - start;
78 sub->add(runFont, runLimit);
82 Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns, LEErrorCode &status)
83 : fParagraphLayout(NULL), fParagraphCount(0), fParagraphMax(PARA_GROW), fParagraphGrow(PARA_GROW),
84 fLineCount(0), fLinesMax(LINE_GROW), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL),
85 fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR)
87 static const LEUnicode separators[] = {CH_LF, CH_CR, CH_LSEP, CH_PSEP, 0x0000};
89 if (LE_FAILURE(status)) {
97 LocaleRuns *locales = NULL;
100 fLines = LE_NEW_ARRAY(const ParagraphLayout::Line *, fLinesMax);
101 fParagraphLayout = LE_NEW_ARRAY(ParagraphLayout *, fParagraphMax);
103 fChars = LE_NEW_ARRAY(LEUnicode, charCount + 1);
104 LE_ARRAY_COPY(fChars, chars, charCount);
105 fChars[charCount] = 0;
107 LEUnicode *pStart = &fChars[0];
109 while (*pStart != 0) {
110 LEUnicode *pEnd = u_strpbrk(pStart, separators);
111 le_int32 pAscent, pDescent, pLeading;
112 ParagraphLayout *paragraphLayout = NULL;
115 pEnd = &fChars[charCount];
118 if (pEnd != pStart) {
119 subsetFontRuns(fontRuns, pStart - fChars, pEnd - fChars, &fr);
121 paragraphLayout = new ParagraphLayout(pStart, pEnd - pStart, &fr, NULL, NULL, locales, fParagraphLevel, FALSE, status);
123 if (LE_FAILURE(status)) {
124 delete paragraphLayout;
125 break; // return? something else?
128 if (fParagraphLevel == UBIDI_DEFAULT_LTR) {
129 fParagraphLevel = paragraphLayout->getParagraphLevel();
132 pAscent = paragraphLayout->getAscent();
133 pDescent = paragraphLayout->getDescent();
134 pLeading = paragraphLayout->getLeading();
136 if (pAscent > ascent) {
140 if (pDescent > descent) {
144 if (pLeading > leading) {
149 if (fParagraphCount >= fParagraphMax) {
150 fParagraphLayout = (ParagraphLayout **) LE_GROW_ARRAY(fParagraphLayout, fParagraphMax + fParagraphGrow);
151 fParagraphMax += fParagraphGrow;
154 fParagraphLayout[fParagraphCount++] = paragraphLayout;
160 pStart = skipLineEnd(pEnd);
163 fLineHeight = ascent + descent + leading;
167 Paragraph::~Paragraph()
169 for (le_int32 line = 0; line < fLineCount; line += 1) {
170 delete /*(LineInfo *)*/ fLines[line];
173 for (le_int32 paragraph = 0; paragraph < fParagraphCount; paragraph += 1) {
174 delete fParagraphLayout[paragraph];
177 LE_DELETE_ARRAY(fLines);
178 LE_DELETE_ARRAY(fParagraphLayout);
179 LE_DELETE_ARRAY(fChars);
182 void Paragraph::addLine(const ParagraphLayout::Line *line)
184 if (fLineCount >= fLinesMax) {
185 fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow);
186 fLinesMax += fLinesGrow;
189 fLines[fLineCount++] = line;
192 void Paragraph::breakLines(le_int32 width, le_int32 height)
196 // don't re-break if the width hasn't changed
197 if (fWidth == width) {
203 float lineWidth = (float) (width - 2 * MARGIN);
204 const ParagraphLayout::Line *line;
206 // Free the old LineInfo's...
207 for (le_int32 li = 0; li < fLineCount; li += 1) {
213 for (le_int32 p = 0; p < fParagraphCount; p += 1) {
214 ParagraphLayout *paragraphLayout = fParagraphLayout[p];
216 if (paragraphLayout != NULL) {
217 paragraphLayout->reflow();
218 while ((line = paragraphLayout->nextLine(lineWidth)) != NULL) {
227 void Paragraph::draw(RenderingSurface *surface, le_int32 firstLine, le_int32 lastLine)
234 for (li = firstLine; li <= lastLine; li += 1) {
235 const ParagraphLayout::Line *line = fLines[li];
238 le_int32 runCount = line->countRuns();
241 if (fParagraphLevel == UBIDI_RTL) {
242 le_int32 lastX = line->getWidth();
244 x = (fWidth - lastX - MARGIN);
248 for (run = 0; run < runCount; run += 1) {
249 const ParagraphLayout::VisualRun *visualRun = line->getVisualRun(run);
250 le_int32 glyphCount = visualRun->getGlyphCount();
251 const LEFontInstance *font = visualRun->getFont();
252 const LEGlyphID *glyphs = visualRun->getGlyphs();
253 const float *positions = visualRun->getPositions();
255 surface->drawGlyphs(font, glyphs, glyphCount, positions, x, y, fWidth, fHeight);
263 Paragraph *Paragraph::paragraphFactory(const char *fileName, const LEFontInstance *font, GUISupport *guiSupport)
265 LEErrorCode status = LE_NO_ERROR;
267 const UChar *text = UnicodeReader::readFile(fileName, guiSupport, charCount);
268 Paragraph *result = NULL;
274 FontRuns fontRuns(0);
276 fontRuns.add(font, charCount);
278 result = new Paragraph(text, charCount, &fontRuns, status);
280 if (LE_FAILURE(status)) {
285 LE_DELETE_ARRAY(text);