2001-11-07 23:48:17 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2005-08-02 18:22:23 +00:00
|
|
|
* Copyright (C) 1999-2005, International Business Machines
|
2001-11-07 23:48:17 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: Paragraph.cpp
|
|
|
|
*
|
|
|
|
* created on: 09/06/2000
|
|
|
|
* created by: Eric R. Mader
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-03-19 03:01:28 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-11-22 01:43:53 +00:00
|
|
|
#include "unicode/ubidi.h"
|
2005-08-02 18:22:23 +00:00
|
|
|
#include "unicode/ustring.h"
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2003-04-23 23:05:00 +00:00
|
|
|
#include "layout/ParagraphLayout.h"
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
#include "RenderingSurface.h"
|
|
|
|
#include "ScriptCompositeFontInstance.h"
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
#include "paragraph.h"
|
|
|
|
#include "UnicodeReader.h"
|
|
|
|
#include "FontMap.h"
|
|
|
|
|
|
|
|
#define MARGIN 10
|
2002-12-05 22:50:29 +00:00
|
|
|
#define LINE_GROW 32
|
2005-08-02 18:22:23 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2003-11-04 21:30:00 +00:00
|
|
|
Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns, LEErrorCode &status)
|
2005-08-02 18:22:23 +00:00
|
|
|
: fParagraphLayout(NULL), fParagraphCount(0), fParagraphMax(PARA_GROW), fParagraphGrow(PARA_GROW),
|
|
|
|
fLineCount(0), fLinesMax(LINE_GROW), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL),
|
2005-08-09 00:54:54 +00:00
|
|
|
fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR)
|
2002-12-05 22:50:29 +00:00
|
|
|
{
|
2005-08-02 18:22:23 +00:00
|
|
|
static const LEUnicode separators[] = {CH_LF, CH_CR, CH_LSEP, CH_PSEP};
|
|
|
|
|
2003-11-04 21:30:00 +00:00
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-02 18:22:23 +00:00
|
|
|
le_int32 ascent = 0;
|
|
|
|
le_int32 descent = 0;
|
|
|
|
le_int32 leading = 0;
|
|
|
|
|
2003-11-04 21:30:00 +00:00
|
|
|
LocaleRuns *locales = NULL;
|
2005-08-02 18:22:23 +00:00
|
|
|
FontRuns fr(0);
|
2003-11-04 21:30:00 +00:00
|
|
|
|
2005-08-02 18:22:23 +00:00
|
|
|
fLines = LE_NEW_ARRAY(const ParagraphLayout::Line *, fLinesMax);
|
|
|
|
fParagraphLayout = LE_NEW_ARRAY(ParagraphLayout *, fParagraphMax);
|
|
|
|
|
|
|
|
fChars = LE_NEW_ARRAY(LEUnicode, charCount + 1);
|
2003-05-07 21:42:38 +00:00
|
|
|
LE_ARRAY_COPY(fChars, chars, charCount);
|
2005-08-02 18:22:23 +00:00
|
|
|
fChars[charCount] = 0;
|
2003-05-07 21:42:38 +00:00
|
|
|
|
2005-08-02 18:22:23 +00:00
|
|
|
LEUnicode *pStart = &fChars[0];
|
2003-11-04 21:30:00 +00:00
|
|
|
|
2005-08-02 18:22:23 +00:00
|
|
|
while (*pStart != 0) {
|
|
|
|
LEUnicode *pEnd = u_strpbrk(pStart, separators);
|
|
|
|
le_int32 pAscent, pDescent, pLeading;
|
2005-08-09 00:54:54 +00:00
|
|
|
ParagraphLayout *paragraphLayout = NULL;
|
2005-08-02 18:22:23 +00:00
|
|
|
|
|
|
|
if (pEnd == NULL) {
|
|
|
|
pEnd = &fChars[charCount];
|
|
|
|
}
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (pEnd != pStart) {
|
|
|
|
subsetFontRuns(fontRuns, pStart - fChars, pEnd - fChars, &fr);
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
paragraphLayout = new ParagraphLayout(pStart, pEnd - pStart, &fr, NULL, NULL, locales, fParagraphLevel, FALSE, status);
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
break; // return? something else?
|
|
|
|
}
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (fParagraphLevel == UBIDI_DEFAULT_LTR) {
|
|
|
|
fParagraphLevel = paragraphLayout->getParagraphLevel();
|
|
|
|
}
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
pAscent = paragraphLayout->getAscent();
|
|
|
|
pDescent = paragraphLayout->getDescent();
|
|
|
|
pLeading = paragraphLayout->getLeading();
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (pAscent > ascent) {
|
|
|
|
ascent = pAscent;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (pDescent > descent) {
|
|
|
|
descent = pDescent;
|
|
|
|
}
|
2005-08-02 18:22:23 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (pLeading > leading) {
|
|
|
|
leading = pLeading;
|
|
|
|
}
|
2005-08-02 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (fParagraphCount >= fParagraphMax) {
|
|
|
|
fParagraphLayout = (ParagraphLayout **) LE_GROW_ARRAY(fParagraphLayout, fParagraphMax + fParagraphGrow);
|
|
|
|
fParagraphMax += fParagraphGrow;
|
2005-08-02 18:22:23 +00:00
|
|
|
}
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
fParagraphLayout[fParagraphCount++] = paragraphLayout;
|
2005-08-02 18:22:23 +00:00
|
|
|
|
|
|
|
if (*pEnd == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pStart = skipLineEnd(pEnd);
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
fLineHeight = ascent + descent + leading;
|
|
|
|
fAscent = ascent;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
Paragraph::~Paragraph()
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
for (le_int32 line = 0; line < fLineCount; line += 1) {
|
2003-03-03 23:07:01 +00:00
|
|
|
delete /*(LineInfo *)*/ fLines[line];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
LE_DELETE_ARRAY(fLines);
|
2003-05-07 21:42:38 +00:00
|
|
|
delete fParagraphLayout;
|
|
|
|
LE_DELETE_ARRAY(fChars);
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
void Paragraph::addLine(const ParagraphLayout::Line *line)
|
|
|
|
{
|
|
|
|
if (fLineCount >= fLinesMax) {
|
|
|
|
fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow);
|
|
|
|
fLinesMax += fLinesGrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
fLines[fLineCount++] = line;
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
void Paragraph::breakLines(le_int32 width, le_int32 height)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
fHeight = height;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
// don't re-break if the width hasn't changed
|
|
|
|
if (fWidth == width) {
|
|
|
|
return;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fWidth = width;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
float lineWidth = (float) (width - 2 * MARGIN);
|
2003-03-03 23:07:01 +00:00
|
|
|
const ParagraphLayout::Line *line;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
// Free the old LineInfo's...
|
2005-08-09 00:54:54 +00:00
|
|
|
for (le_int32 li = 0; li < fLineCount; li += 1) {
|
2003-03-03 23:07:01 +00:00
|
|
|
delete fLines[li];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
fLineCount = 0;
|
2002-03-14 22:13:52 +00:00
|
|
|
|
2005-08-02 18:22:23 +00:00
|
|
|
for (le_int32 p = 0; p < fParagraphCount; p += 1) {
|
|
|
|
ParagraphLayout *paragraphLayout = fParagraphLayout[p];
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (paragraphLayout != NULL) {
|
|
|
|
paragraphLayout->reflow();
|
|
|
|
while ((line = paragraphLayout->nextLine(lineWidth)) != NULL) {
|
|
|
|
addLine(line);
|
2005-08-02 18:22:23 +00:00
|
|
|
}
|
2005-08-09 00:54:54 +00:00
|
|
|
} else {
|
|
|
|
addLine(NULL);
|
2005-08-02 18:22:23 +00:00
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
void Paragraph::draw(RenderingSurface *surface, le_int32 firstLine, le_int32 lastLine)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2003-03-03 23:07:01 +00:00
|
|
|
le_int32 li, x, y;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
x = MARGIN;
|
2001-11-07 23:48:17 +00:00
|
|
|
y = fAscent;
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
for (li = firstLine; li <= lastLine; li += 1) {
|
|
|
|
const ParagraphLayout::Line *line = fLines[li];
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
if (line != NULL) {
|
|
|
|
le_int32 runCount = line->countRuns();
|
|
|
|
le_int32 run;
|
|
|
|
|
|
|
|
if (fParagraphLevel == UBIDI_RTL) {
|
|
|
|
le_int32 lastX = line->getWidth();
|
2003-08-20 23:04:49 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
x = (fWidth - lastX - MARGIN);
|
|
|
|
}
|
2003-08-20 23:04:49 +00:00
|
|
|
|
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
for (run = 0; run < runCount; run += 1) {
|
|
|
|
const ParagraphLayout::VisualRun *visualRun = line->getVisualRun(run);
|
|
|
|
le_int32 glyphCount = visualRun->getGlyphCount();
|
|
|
|
const LEFontInstance *font = visualRun->getFont();
|
|
|
|
const LEGlyphID *glyphs = visualRun->getGlyphs();
|
|
|
|
const float *positions = visualRun->getPositions();
|
2003-03-03 23:07:01 +00:00
|
|
|
|
2005-08-09 00:54:54 +00:00
|
|
|
surface->drawGlyphs(font, glyphs, glyphCount, positions, x, y, fWidth, fHeight);
|
|
|
|
}
|
2003-03-03 23:07:01 +00:00
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
y += fLineHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-07 21:42:38 +00:00
|
|
|
Paragraph *Paragraph::paragraphFactory(const char *fileName, const LEFontInstance *font, GUISupport *guiSupport)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2003-11-04 21:30:00 +00:00
|
|
|
LEErrorCode status = LE_NO_ERROR;
|
2003-03-03 23:07:01 +00:00
|
|
|
le_int32 charCount;
|
2001-11-07 23:48:17 +00:00
|
|
|
const UChar *text = UnicodeReader::readFile(fileName, guiSupport, charCount);
|
2003-05-07 21:42:38 +00:00
|
|
|
Paragraph *result = NULL;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
if (text == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-07 21:42:38 +00:00
|
|
|
FontRuns fontRuns(0);
|
|
|
|
|
|
|
|
fontRuns.add(font, charCount);
|
|
|
|
|
2003-11-04 21:30:00 +00:00
|
|
|
result = new Paragraph(text, charCount, &fontRuns, status);
|
|
|
|
|
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
delete result;
|
|
|
|
result = NULL;
|
|
|
|
}
|
2003-05-07 21:42:38 +00:00
|
|
|
|
|
|
|
LE_DELETE_ARRAY(text);
|
2002-12-05 22:50:29 +00:00
|
|
|
|
2003-05-07 21:42:38 +00:00
|
|
|
return result;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|