diff --git a/icu4c/source/samples/layout/paragraph.cpp b/icu4c/source/samples/layout/paragraph.cpp index 2ac4504ed6d..246e3f8a164 100644 --- a/icu4c/source/samples/layout/paragraph.cpp +++ b/icu4c/source/samples/layout/paragraph.cpp @@ -1,7 +1,7 @@ /* ******************************************************************************* * - * Copyright (C) 1999-2003, International Business Machines + * Copyright (C) 1999-2005, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -14,6 +14,7 @@ #include "unicode/utypes.h" #include "unicode/uchar.h" #include "unicode/ubidi.h" +#include "unicode/ustring.h" #include "layout/ParagraphLayout.h" @@ -26,29 +27,136 @@ #define MARGIN 10 #define LINE_GROW 32 +#define PARA_GROW 8 + +#define CH_LF 0x000A +#define CH_CR 0x000D +#define CH_LSEP 0x2028 +#define CH_PSEP 0x2029 + +static LEUnicode *skipLineEnd(LEUnicode *ptr) +{ + if (ptr[0] == CH_CR && ptr[1] == CH_LF) { + ptr += 1; + } + + return ptr + 1; +} + +static le_int32 findRun(const RunArray *runArray, le_int32 offset) +{ + le_int32 runCount = runArray->getCount(); + + for (le_int32 run = 0; run < runCount; run += 1) { + if (runArray->getLimit(run) > offset) { + return run; + } + } + + return -1; +} + +static void subsetFontRuns(const FontRuns *fontRuns, le_int32 start, le_int32 limit, FontRuns *sub) +{ + le_int32 startRun = findRun(fontRuns, start); + le_int32 endRun = findRun(fontRuns, limit - 1); + + sub->reset(); + + for (le_int32 run = startRun; run <= endRun; run += 1) { + const LEFontInstance *runFont = fontRuns->getFont(run); + le_int32 runLimit = fontRuns->getLimit(run) - start; + + if (run == endRun) { + runLimit = limit - start; + } + + sub->add(runFont, runLimit); + } +} Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns, LEErrorCode &status) - : fParagraphLayout(NULL), fLineCount(0), fLinesMax(0), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL), + : fParagraphLayout(NULL), fParagraphCount(0), fParagraphMax(PARA_GROW), fParagraphGrow(PARA_GROW), + fLineCount(0), fLinesMax(LINE_GROW), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL), fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1) { + static const LEUnicode separators[] = {CH_LF, CH_CR, CH_LSEP, CH_PSEP}; + if (LE_FAILURE(status)) { return; } + le_int32 ascent = 0; + le_int32 descent = 0; + le_int32 leading = 0; + + UBiDiLevel paragraphLevel = UBIDI_DEFAULT_LTR; + LocaleRuns *locales = NULL; + FontRuns fr(0); - fChars = LE_NEW_ARRAY(LEUnicode, charCount); + fLines = LE_NEW_ARRAY(const ParagraphLayout::Line *, fLinesMax); + fParagraphLayout = LE_NEW_ARRAY(ParagraphLayout *, fParagraphMax); + + fChars = LE_NEW_ARRAY(LEUnicode, charCount + 1); LE_ARRAY_COPY(fChars, chars, charCount); + fChars[charCount] = 0; - fParagraphLayout = new ParagraphLayout(fChars, charCount, fontRuns, NULL, NULL, locales, UBIDI_DEFAULT_LTR, FALSE, status); + LEUnicode *pStart = &fChars[0]; - if (LE_FAILURE(status)) { - return; - } + while (*pStart != 0) { + LEUnicode *pEnd = u_strpbrk(pStart, separators); + le_int32 pAscent, pDescent, pLeading; - le_int32 ascent = fParagraphLayout->getAscent(); - le_int32 descent = fParagraphLayout->getDescent(); - le_int32 leading = fParagraphLayout->getLeading(); + if (pEnd == NULL) { + pEnd = &fChars[charCount]; + } + + if (pEnd == pStart) { + pEnd = skipLineEnd(pEnd); + } + + subsetFontRuns(fontRuns, pStart - fChars, pEnd - fChars, &fr); + + ParagraphLayout *paragraphLayout = new ParagraphLayout(pStart, pEnd - pStart, &fr, NULL, NULL, locales, paragraphLevel, FALSE, status); + + if (fParagraphCount >= fParagraphMax) { + fParagraphLayout = (ParagraphLayout **) LE_GROW_ARRAY(fParagraphLayout, fParagraphMax + fParagraphGrow); + fParagraphMax += fParagraphGrow; + } + + if (LE_FAILURE(status)) { + break; // return? something else? + } + + fParagraphLayout[fParagraphCount++] = paragraphLayout; + + if (paragraphLevel == UBIDI_DEFAULT_LTR) { + paragraphLevel = paragraphLayout->getParagraphLevel(); + } + + pAscent = paragraphLayout->getAscent(); + pDescent = paragraphLayout->getDescent(); + pLeading = paragraphLayout->getLeading(); + + if (pAscent > ascent) { + ascent = pAscent; + } + + if (pDescent > descent) { + descent = pDescent; + } + + if (pLeading > leading) { + leading = pLeading; + } + + if (*pEnd == 0) { + break; + } + + pStart = skipLineEnd(pEnd); + } fLineHeight = ascent + descent + leading; fAscent = ascent; @@ -86,15 +194,20 @@ void Paragraph::breakLines(le_int32 width, le_int32 height) } li = 0; - fParagraphLayout->reflow(); - while ((line = fParagraphLayout->nextLine(lineWidth)) != NULL) { - // grow the line array, if we need to. - if (li >= fLinesMax) { - fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow); - fLinesMax += fLinesGrow; - } - fLines[li++] = line; + for (le_int32 p = 0; p < fParagraphCount; p += 1) { + ParagraphLayout *paragraphLayout = fParagraphLayout[p]; + + paragraphLayout->reflow(); + while ((line = paragraphLayout->nextLine(lineWidth)) != NULL) { + // grow the line array, if we need to. + if (li >= fLinesMax) { + fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow); + fLinesMax += fLinesGrow; + } + + fLines[li++] = line; + } } fLineCount = li; @@ -112,7 +225,7 @@ void Paragraph::draw(RenderingSurface *surface, le_int32 firstLine, le_int32 las le_int32 runCount = line->countRuns(); le_int32 run; - if (fParagraphLayout->getParagraphLevel() == UBIDI_RTL) { + if (fParagraphLayout[0]->getParagraphLevel() == UBIDI_RTL) { le_int32 lastX = line->getWidth(); x = (fWidth - lastX - MARGIN); diff --git a/icu4c/source/samples/layout/paragraph.h b/icu4c/source/samples/layout/paragraph.h index 67db5eb37f6..63ee0f5bd15 100644 --- a/icu4c/source/samples/layout/paragraph.h +++ b/icu4c/source/samples/layout/paragraph.h @@ -1,7 +1,7 @@ /* ******************************************************************************* * - * Copyright (C) 1999-2003, International Business Machines + * Copyright (C) 1999-2005, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -46,11 +46,15 @@ public: static Paragraph *paragraphFactory(const char *fileName, const LEFontInstance *font, GUISupport *guiSupport); private: - ParagraphLayout *fParagraphLayout; + ParagraphLayout **fParagraphLayout; - le_int32 fLineCount; - le_int32 fLinesMax; - le_int32 fLinesGrow; + le_int32 fParagraphCount; + le_int32 fParagraphMax; + le_int32 fParagraphGrow; + + le_int32 fLineCount; + le_int32 fLinesMax; + le_int32 fLinesGrow; const ParagraphLayout::Line **fLines; LEUnicode *fChars;