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 "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
|
|
|
|
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"
|
|
|
|
|
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
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
#if 0
|
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:
|
2003-03-03 23:07:01 +00:00
|
|
|
le_int32 fGlyphCount;
|
|
|
|
const LEFontInstance *fFont;
|
|
|
|
UBiDiDirection fDirection;
|
|
|
|
const LEGlyphID *fGlyphs;
|
|
|
|
le_int32 *fDX;
|
|
|
|
le_int32 *fDY;
|
2002-12-05 22:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
fGlyphs = LE_NEW_ARRAY(LEGlyphID, fGlyphCount);
|
|
|
|
fDX = LE_NEW_ARRAY(le_int32, fGlyphCount);
|
|
|
|
fDY = LE_NEW_ARRAY(le_int32, fGlyphCount);
|
|
|
|
positions = LE_NEW_ARRAY(float, fGlyphCount * 2 + 2);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-06 21:45:44 +00:00
|
|
|
glyphs = (LEGlyphID *) fGlyphs;
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
paragraphLayout->getVisualRun(runIndex, glyphs, positions, NULL, &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) {
|
2003-02-05 00:05:40 +00:00
|
|
|
TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyphs[i]);
|
2002-12-05 22:50:29 +00:00
|
|
|
// filter out deleted glyphs
|
2003-02-05 00:05:40 +00:00
|
|
|
if (ttGlyph == 0xFFFE || ttGlyph == 0xFFFF) {
|
|
|
|
glyphs[i] = LE_SET_GLYPH(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
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
LE_DELETE_ARRAY(positions);
|
2002-12-05 22:50:29 +00:00
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
LineRun::~LineRun()
|
|
|
|
{
|
2003-03-03 23:07:01 +00:00
|
|
|
LE_DELETE_ARRAY(fDY);
|
|
|
|
LE_DELETE_ARRAY(fDX);
|
|
|
|
LE_DELETE_ARRAY(fGlyphs);
|
2002-12-05 22:50:29 +00:00
|
|
|
}
|
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();
|
2003-03-03 23:07:01 +00:00
|
|
|
fRuns = LE_NEW_ARRAY(LineRun *, fRunCount);
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
LE_DELETE_ARRAY(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
|
|
|
}
|
2003-03-03 23:07:01 +00:00
|
|
|
#endif
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
Paragraph::Paragraph(const LEUnicode chars[], int32_t charCount, const FontRuns *fontRuns)
|
2002-12-05 22:50:29 +00:00
|
|
|
: fParagraphLayout(NULL), fLineCount(0), fLinesMax(0), fLinesGrow(LINE_GROW), fLines(NULL),
|
|
|
|
fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1)
|
|
|
|
{
|
2003-04-23 16:16:22 +00:00
|
|
|
fParagraphLayout = new ParagraphLayout(chars, charCount, fontRuns, NULL, NULL, NULL, UBIDI_LTR, false);
|
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;
|
2002-12-05 22:50:29 +00:00
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
delete fontRuns;
|
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);
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-03-03 23:07:01 +00:00
|
|
|
LEErrorCode fontStatus = LE_NO_ERROR;
|
2002-12-05 22:50:29 +00:00
|
|
|
UErrorCode scriptStatus = U_ZERO_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);
|
|
|
|
|
|
|
|
if (text == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
ScriptCompositeFontInstance *font = new ScriptCompositeFontInstance(fontMap);
|
|
|
|
FontRuns *fontRuns = new FontRuns((const LEFontInstance **) &font, &charCount, 1);
|
2002-12-05 22:50:29 +00:00
|
|
|
|
2003-03-03 23:07:01 +00:00
|
|
|
return new Paragraph(text, charCount, fontRuns);
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|