2001-11-07 23:48:17 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: Paragraph.cpp
|
|
|
|
*
|
|
|
|
* created on: 09/06/2000
|
|
|
|
* created by: Eric R. Mader
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "RenderingFontInstance.h"
|
|
|
|
|
|
|
|
#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"
|
2002-12-05 22:50:29 +00:00
|
|
|
#include "usc_impl.h" /* this is currently private! */
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
#include "paragraph.h"
|
|
|
|
#include "UnicodeReader.h"
|
|
|
|
#include "FontMap.h"
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
#include "ParagraphLayout.h"
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
#define MARGIN 10
|
2002-12-05 22:50:29 +00:00
|
|
|
#define LINE_GROW 32
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
class LineRun
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
public:
|
|
|
|
LineRun(ParagraphLayout *paragraphLayout, le_int32 runIndex);
|
|
|
|
~LineRun();
|
|
|
|
|
|
|
|
le_int32 draw(RenderingSurface *surface, le_int32 x, le_int32 y, le_int32 width, le_int32 height) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
le_int32 fGlyphCount;
|
|
|
|
const RenderingFontInstance *fFont;
|
|
|
|
UBiDiDirection fDirection;
|
|
|
|
const LEGlyphID *fGlyphs;
|
|
|
|
le_int32 *fDX;
|
|
|
|
le_int32 *fDY;
|
|
|
|
};
|
|
|
|
|
|
|
|
LineRun::LineRun(ParagraphLayout *paragraphLayout, le_int32 runIndex)
|
|
|
|
{
|
2002-12-06 21:45:44 +00:00
|
|
|
LEGlyphID *glyphs;
|
|
|
|
float *positions;
|
|
|
|
le_int32 i;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fGlyphCount = paragraphLayout->getVisualRun(runIndex, NULL, NULL, NULL, NULL, NULL);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (fGlyphCount <= 0) {
|
|
|
|
return;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fGlyphs = new LEGlyphID[fGlyphCount];
|
|
|
|
fDX = new le_int32[fGlyphCount];
|
|
|
|
fDY = new le_int32[fGlyphCount];
|
|
|
|
positions = new float[fGlyphCount * 2 + 2];
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-06 21:45:44 +00:00
|
|
|
glyphs = (LEGlyphID *) fGlyphs;
|
|
|
|
|
|
|
|
paragraphLayout->getVisualRun(runIndex, glyphs, (float *) positions, NULL, (const LEFontInstance **) &fFont, &fDirection);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (i = 0; i < fGlyphCount; i += 1) {
|
|
|
|
// filter out deleted glyphs
|
2002-12-06 21:45:44 +00:00
|
|
|
if (glyphs[i] == 0xFFFE || glyphs[i] == 0xFFFF) {
|
|
|
|
glyphs[i] = 0x0002;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fDX[i] = (le_int32) (positions[i * 2 + 2] - positions[i * 2]);
|
|
|
|
fDY[i] = (le_int32) positions[i * 2 + 1];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
delete[] positions;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
LineRun::~LineRun()
|
|
|
|
{
|
|
|
|
delete[] fDY;
|
|
|
|
delete[] fDX;
|
|
|
|
delete[] (LEGlyphID *) fGlyphs;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 LineRun::draw(RenderingSurface *surface, le_int32 x, le_int32 y, le_int32 width, le_int32 height) const
|
|
|
|
{
|
|
|
|
le_int32 dyEnd, dyStart;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
dyStart = dyEnd = 0;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
while (dyEnd < fGlyphCount) {
|
|
|
|
while (dyEnd < fGlyphCount && fDY[dyStart] == fDY[dyEnd]) {
|
|
|
|
dyEnd += 1;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
surface->drawGlyphs(fFont, &fGlyphs[dyStart], dyEnd - dyStart,
|
|
|
|
&fDX[dyStart], x, y + fDY[dyStart], width, height);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (le_int32 i = dyStart; i < dyEnd; i += 1) {
|
|
|
|
x += fDX[i];
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
dyStart = dyEnd;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
return x;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
class LineInfo
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
public:
|
|
|
|
LineInfo(ParagraphLayout *paragraphLayout);
|
|
|
|
~LineInfo();
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 getRunCount() const {return fRunCount;};
|
|
|
|
void draw(RenderingSurface *surface, le_int32 x, le_int32 y, le_int32 width, le_int32 height);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
private:
|
|
|
|
le_int32 fRunCount;
|
|
|
|
LineRun **fRuns;
|
|
|
|
};
|
2001-11-22 01:43:53 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
LineInfo::LineInfo(ParagraphLayout *paragraphLayout)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
fRunCount = paragraphLayout->countLineRuns();
|
|
|
|
fRuns = new LineRun *[fRunCount];
|
|
|
|
le_int32 run;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (run = 0; run < fRunCount; run += 1) {
|
|
|
|
fRuns[run] = new LineRun(paragraphLayout, run);
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
LineInfo::~LineInfo()
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 run;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (run = 0; run < fRunCount; run += 1) {
|
|
|
|
delete fRuns[run];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
delete[] fRuns;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
void LineInfo::draw(RenderingSurface *surface, le_int32 x, le_int32 y, le_int32 width, le_int32 height)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 run;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (run = 0; run < fRunCount; run += 1) {
|
|
|
|
x = fRuns[run]->draw(surface, x, y, width, height);
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
2002-12-05 22:50:29 +00:00
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount,
|
|
|
|
const RenderingFontInstance *runFonts[], const UScriptCode runScripts[],
|
|
|
|
const le_int32 runLimits[], le_int32 runCount)
|
|
|
|
: fParagraphLayout(NULL), fLineCount(0), fLinesMax(0), fLinesGrow(LINE_GROW), fLines(NULL),
|
|
|
|
fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1)
|
|
|
|
{
|
|
|
|
le_int32 run, maxAscent = -1, maxDescent = -1, maxLeading = -1;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
for (run = 0; run < runCount; run += 1) {
|
|
|
|
le_int32 runAscent = runFonts[run]->getAscent();
|
|
|
|
le_int32 runDescent = runFonts[run]->getDescent();
|
|
|
|
le_int32 runLeading = runFonts[run]->getLeading();
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (runAscent > maxAscent) {
|
|
|
|
maxAscent = runAscent;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (runDescent > maxDescent) {
|
|
|
|
maxDescent = runDescent;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (runLeading > maxLeading) {
|
|
|
|
maxLeading = runLeading;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fLineHeight = maxAscent + maxDescent + maxLeading;
|
|
|
|
fAscent = maxAscent;
|
|
|
|
|
|
|
|
// NOTE: We're passing the same array in for both font and script run limits...
|
|
|
|
fParagraphLayout = new ParagraphLayout(chars, charCount, (const LEFontInstance **) runFonts, runLimits, runCount, NULL, NULL, 0,
|
|
|
|
runScripts, runLimits, runCount, UBIDI_LTR, false);
|
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) {
|
|
|
|
delete (LineInfo *) fLines[line];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
delete[] fLines;
|
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);
|
|
|
|
le_int32 line;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
// Free the old LineInfo's...
|
|
|
|
for (line = 0; line < fLineCount; line += 1) {
|
|
|
|
delete fLines[line];
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
line = 0;
|
|
|
|
fParagraphLayout->reflow();
|
|
|
|
while (fParagraphLayout->nextLineBreak(lineWidth) >= 0) {
|
|
|
|
LineInfo *lineInfo = new LineInfo(fParagraphLayout);
|
|
|
|
|
|
|
|
// grow the line array, if we need to.
|
|
|
|
if (line >= fLinesMax) {
|
|
|
|
LineInfo **newLines = new LineInfo *[fLinesMax + fLinesGrow];
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (fLines != NULL) {
|
|
|
|
LE_ARRAY_COPY(newLines, fLines, fLinesMax);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
delete[] fLines;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fLinesMax += fLinesGrow;
|
|
|
|
fLines = newLines;
|
2002-03-14 22:13:52 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fLines[line++] = lineInfo;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fLineCount = line;
|
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
|
|
|
{
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 line, 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;
|
|
|
|
|
|
|
|
for (line = firstLine; line <= lastLine; line += 1) {
|
2002-12-05 22:50:29 +00:00
|
|
|
fLines[line]->draw(surface, x, y, fWidth, fHeight);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
y += fLineHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
Paragraph *Paragraph::paragraphFactory(const char *fileName, FontMap *fontMap, GUISupport *guiSupport)
|
2001-11-07 23:48:17 +00:00
|
|
|
{
|
|
|
|
RFIErrorCode fontStatus = RFI_NO_ERROR;
|
2002-12-05 22:50:29 +00:00
|
|
|
UErrorCode scriptStatus = U_ZERO_ERROR;
|
|
|
|
le_int32 charCount, runCount;
|
|
|
|
UScriptRun *sr;
|
2001-11-07 23:48:17 +00:00
|
|
|
const UChar *text = UnicodeReader::readFile(fileName, guiSupport, charCount);
|
|
|
|
|
|
|
|
if (text == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
sr = uscript_openRun(text, charCount, &scriptStatus);
|
|
|
|
|
|
|
|
runCount = 0;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
while (uscript_nextRun(sr, NULL, NULL, NULL)) {
|
|
|
|
runCount += 1;
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
uscript_resetRun(sr);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
UScriptCode *scripts = new UScriptCode[runCount];
|
|
|
|
RenderingFontInstance **fonts = new RenderingFontInstance *[runCount];
|
|
|
|
le_int32 *runLimits = new le_int32[runCount];
|
2001-11-22 01:43:53 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
le_int32 limit, run;
|
|
|
|
UScriptCode script;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
run = 0;
|
|
|
|
while (uscript_nextRun(sr, NULL, &limit, &script)) {
|
|
|
|
scripts[run] = script;
|
|
|
|
runLimits[run] = limit;
|
|
|
|
fonts[run] = (RenderingFontInstance *) fontMap->getScriptFont(script, fontStatus);
|
2001-11-22 01:43:53 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
if (fonts[run] == NULL) {
|
|
|
|
delete[] runLimits;
|
|
|
|
delete[] fonts;
|
|
|
|
delete[] scripts;
|
2001-11-22 01:43:53 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
uscript_closeRun(sr);
|
|
|
|
return NULL;
|
2001-11-22 01:43:53 +00:00
|
|
|
}
|
2002-12-05 22:50:29 +00:00
|
|
|
|
|
|
|
run += 1;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
uscript_closeRun(sr);
|
|
|
|
|
|
|
|
return new Paragraph(text, charCount, (const RenderingFontInstance **) fonts, scripts, runLimits, runCount);
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|