2001-11-07 23:48:17 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1999-2003, 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"
|
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
|
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)
|
2003-05-07 21:42:38 +00:00
|
|
|
: fParagraphLayout(NULL), fLineCount(0), fLinesMax(0), fLinesGrow(LINE_GROW), fLines(NULL), fChars(NULL),
|
2002-12-05 22:50:29 +00:00
|
|
|
fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1)
|
|
|
|
{
|
2003-11-04 21:30:00 +00:00
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocaleRuns *locales = NULL;
|
|
|
|
|
2003-05-07 21:42:38 +00:00
|
|
|
fChars = LE_NEW_ARRAY(LEUnicode, charCount);
|
|
|
|
LE_ARRAY_COPY(fChars, chars, charCount);
|
|
|
|
|
2003-12-08 22:43:41 +00:00
|
|
|
fParagraphLayout = new ParagraphLayout(fChars, charCount, fontRuns, NULL, NULL, locales, UBIDI_DEFAULT_LTR, FALSE, status);
|
2003-11-04 21:30:00 +00:00
|
|
|
|
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
le_int32 ascent = fParagraphLayout->getAscent();
|
|
|
|
le_int32 descent = fParagraphLayout->getDescent();
|
|
|
|
le_int32 leading = fParagraphLayout->getLeading();
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
le_int32 li;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
// Free the old LineInfo's...
|
2003-03-03 23:07:01 +00:00
|
|
|
for (li = 0; li < fLineCount; li += 1) {
|
|
|
|
delete fLines[li];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
li = 0;
|
2002-12-05 22:50:29 +00:00
|
|
|
fParagraphLayout->reflow();
|
2003-03-03 23:07:01 +00:00
|
|
|
while ((line = fParagraphLayout->nextLine(lineWidth)) != NULL) {
|
2002-12-05 22:50:29 +00:00
|
|
|
// grow the line array, if we need to.
|
2003-03-03 23:07:01 +00:00
|
|
|
if (li >= fLinesMax) {
|
|
|
|
fLines = (const ParagraphLayout::Line **) LE_GROW_ARRAY(fLines, fLinesMax + fLinesGrow);
|
2002-12-05 22:50:29 +00:00
|
|
|
fLinesMax += fLinesGrow;
|
2002-03-14 22:13:52 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
fLines[li++] = line;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
fLineCount = li;
|
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];
|
|
|
|
le_int32 runCount = line->countRuns();
|
|
|
|
le_int32 run;
|
|
|
|
|
2003-08-20 23:04:49 +00:00
|
|
|
if (fParagraphLayout->getParagraphLevel() == UBIDI_RTL) {
|
2003-11-04 19:39:41 +00:00
|
|
|
le_int32 lastX = line->getWidth();
|
2003-08-20 23:04:49 +00:00
|
|
|
|
|
|
|
x = (fWidth - lastX - MARGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-03 23:07:01 +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();
|
|
|
|
|
|
|
|
surface->drawGlyphs(font, glyphs, glyphCount, positions, x, y, fWidth, fHeight);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|