]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/paragraph.cpp
2 *******************************************************************************
4 * Copyright (C) 1999-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: Paragraph.cpp
10 * created on: 09/06/2000
11 * created by: Eric R. Mader
14 #include "unicode/utypes.h"
15 #include "unicode/uchar.h"
16 #include "unicode/ubidi.h"
17 #include "unicode/ustring.h"
19 #include "layout/ParagraphLayout.h"
21 #include "RenderingSurface.h"
22 #include "ScriptCompositeFontInstance.h"
24 #include "paragraph.h"
25 #include "UnicodeReader.h"
34 #define CH_LSEP 0x2028
35 #define CH_PSEP 0x2029
37 static LEUnicode
*skipLineEnd(LEUnicode
*ptr
)
39 if (ptr
[0] == CH_CR
&& ptr
[1] == CH_LF
) {
46 static le_int32
findRun(const RunArray
*runArray
, le_int32 offset
)
48 le_int32 runCount
= runArray
->getCount();
50 for (le_int32 run
= 0; run
< runCount
; run
+= 1) {
51 if (runArray
->getLimit(run
) > offset
) {
59 static void subsetFontRuns(const FontRuns
*fontRuns
, le_int32 start
, le_int32 limit
, FontRuns
*sub
)
61 le_int32 startRun
= findRun(fontRuns
, start
);
62 le_int32 endRun
= findRun(fontRuns
, limit
- 1);
66 for (le_int32 run
= startRun
; run
<= endRun
; run
+= 1) {
67 const LEFontInstance
*runFont
= fontRuns
->getFont(run
);
68 le_int32 runLimit
= fontRuns
->getLimit(run
) - start
;
71 runLimit
= limit
- start
;
74 sub
->add(runFont
, runLimit
);
78 Paragraph::Paragraph(const LEUnicode chars
[], int32_t charCount
, const FontRuns
*fontRuns
, LEErrorCode
&status
)
79 : fParagraphLayout(NULL
), fParagraphCount(0), fParagraphMax(PARA_GROW
), fParagraphGrow(PARA_GROW
),
80 fLineCount(0), fLinesMax(LINE_GROW
), fLinesGrow(LINE_GROW
), fLines(NULL
), fChars(NULL
),
81 fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR
)
83 static const LEUnicode separators
[] = {CH_LF
, CH_CR
, CH_LSEP
, CH_PSEP
};
85 if (LE_FAILURE(status
)) {
93 LocaleRuns
*locales
= NULL
;
96 fLines
= LE_NEW_ARRAY(const ParagraphLayout::Line
*, fLinesMax
);
97 fParagraphLayout
= LE_NEW_ARRAY(ParagraphLayout
*, fParagraphMax
);
99 fChars
= LE_NEW_ARRAY(LEUnicode
, charCount
+ 1);
100 LE_ARRAY_COPY(fChars
, chars
, charCount
);
101 fChars
[charCount
] = 0;
103 LEUnicode
*pStart
= &fChars
[0];
105 while (*pStart
!= 0) {
106 LEUnicode
*pEnd
= u_strpbrk(pStart
, separators
);
107 le_int32 pAscent
, pDescent
, pLeading
;
108 ParagraphLayout
*paragraphLayout
= NULL
;
111 pEnd
= &fChars
[charCount
];
114 if (pEnd
!= pStart
) {
115 subsetFontRuns(fontRuns
, pStart
- fChars
, pEnd
- fChars
, &fr
);
117 paragraphLayout
= new ParagraphLayout(pStart
, pEnd
- pStart
, &fr
, NULL
, NULL
, locales
, fParagraphLevel
, FALSE
, status
);
119 if (LE_FAILURE(status
)) {
120 break; // return? something else?
123 if (fParagraphLevel
== UBIDI_DEFAULT_LTR
) {
124 fParagraphLevel
= paragraphLayout
->getParagraphLevel();
127 pAscent
= paragraphLayout
->getAscent();
128 pDescent
= paragraphLayout
->getDescent();
129 pLeading
= paragraphLayout
->getLeading();
131 if (pAscent
> ascent
) {
135 if (pDescent
> descent
) {
139 if (pLeading
> leading
) {
144 if (fParagraphCount
>= fParagraphMax
) {
145 fParagraphLayout
= (ParagraphLayout
**) LE_GROW_ARRAY(fParagraphLayout
, fParagraphMax
+ fParagraphGrow
);
146 fParagraphMax
+= fParagraphGrow
;
149 fParagraphLayout
[fParagraphCount
++] = paragraphLayout
;
155 pStart
= skipLineEnd(pEnd
);
158 fLineHeight
= ascent
+ descent
+ leading
;
162 Paragraph::~Paragraph()
164 for (le_int32 line
= 0; line
< fLineCount
; line
+= 1) {
165 delete /*(LineInfo *)*/ fLines
[line
];
168 LE_DELETE_ARRAY(fLines
);
169 delete fParagraphLayout
;
170 LE_DELETE_ARRAY(fChars
);
173 void Paragraph::addLine(const ParagraphLayout::Line
*line
)
175 if (fLineCount
>= fLinesMax
) {
176 fLines
= (const ParagraphLayout::Line
**) LE_GROW_ARRAY(fLines
, fLinesMax
+ fLinesGrow
);
177 fLinesMax
+= fLinesGrow
;
180 fLines
[fLineCount
++] = line
;
183 void Paragraph::breakLines(le_int32 width
, le_int32 height
)
187 // don't re-break if the width hasn't changed
188 if (fWidth
== width
) {
194 float lineWidth
= (float) (width
- 2 * MARGIN
);
195 const ParagraphLayout::Line
*line
;
197 // Free the old LineInfo's...
198 for (le_int32 li
= 0; li
< fLineCount
; li
+= 1) {
204 for (le_int32 p
= 0; p
< fParagraphCount
; p
+= 1) {
205 ParagraphLayout
*paragraphLayout
= fParagraphLayout
[p
];
207 if (paragraphLayout
!= NULL
) {
208 paragraphLayout
->reflow();
209 while ((line
= paragraphLayout
->nextLine(lineWidth
)) != NULL
) {
218 void Paragraph::draw(RenderingSurface
*surface
, le_int32 firstLine
, le_int32 lastLine
)
225 for (li
= firstLine
; li
<= lastLine
; li
+= 1) {
226 const ParagraphLayout::Line
*line
= fLines
[li
];
229 le_int32 runCount
= line
->countRuns();
232 if (fParagraphLevel
== UBIDI_RTL
) {
233 le_int32 lastX
= line
->getWidth();
235 x
= (fWidth
- lastX
- MARGIN
);
239 for (run
= 0; run
< runCount
; run
+= 1) {
240 const ParagraphLayout::VisualRun
*visualRun
= line
->getVisualRun(run
);
241 le_int32 glyphCount
= visualRun
->getGlyphCount();
242 const LEFontInstance
*font
= visualRun
->getFont();
243 const LEGlyphID
*glyphs
= visualRun
->getGlyphs();
244 const float *positions
= visualRun
->getPositions();
246 surface
->drawGlyphs(font
, glyphs
, glyphCount
, positions
, x
, y
, fWidth
, fHeight
);
254 Paragraph
*Paragraph::paragraphFactory(const char *fileName
, const LEFontInstance
*font
, GUISupport
*guiSupport
)
256 LEErrorCode status
= LE_NO_ERROR
;
258 const UChar
*text
= UnicodeReader::readFile(fileName
, guiSupport
, charCount
);
259 Paragraph
*result
= NULL
;
265 FontRuns
fontRuns(0);
267 fontRuns
.add(font
, charCount
);
269 result
= new Paragraph(text
, charCount
, &fontRuns
, status
);
271 if (LE_FAILURE(status
)) {
276 LE_DELETE_ARRAY(text
);