]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/paragraph.cpp
2 *******************************************************************************
4 * Copyright (C) 1999-2015, 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"
23 #include "paragraph.h"
24 #include "UnicodeReader.h"
32 #define CH_LSEP 0x2028
33 #define CH_PSEP 0x2029
35 static LEUnicode
*skipLineEnd(LEUnicode
*ptr
)
37 if (ptr
[0] == CH_CR
&& ptr
[1] == CH_LF
) {
44 static le_int32
findRun(const RunArray
*runArray
, le_int32 offset
)
46 le_int32 runCount
= runArray
->getCount();
48 for (le_int32 run
= 0; run
< runCount
; run
+= 1) {
49 if (runArray
->getLimit(run
) > offset
) {
57 static void subsetFontRuns(const FontRuns
*fontRuns
, le_int32 start
, le_int32 limit
, FontRuns
*sub
)
59 le_int32 startRun
= findRun(fontRuns
, start
);
60 le_int32 endRun
= findRun(fontRuns
, limit
- 1);
64 for (le_int32 run
= startRun
; run
<= endRun
; run
+= 1) {
65 const LEFontInstance
*runFont
= fontRuns
->getFont(run
);
66 le_int32 runLimit
= fontRuns
->getLimit(run
) - start
;
69 runLimit
= limit
- start
;
72 sub
->add(runFont
, runLimit
);
76 Paragraph::Paragraph(const LEUnicode chars
[], int32_t charCount
, const FontRuns
*fontRuns
, LEErrorCode
&status
)
77 : fParagraphLayout(NULL
), fParagraphCount(0), fParagraphMax(PARA_GROW
), fParagraphGrow(PARA_GROW
),
78 fLineCount(0), fLinesMax(LINE_GROW
), fLinesGrow(LINE_GROW
), fLines(NULL
), fChars(NULL
),
79 fLineHeight(-1), fAscent(-1), fWidth(-1), fHeight(-1), fParagraphLevel(UBIDI_DEFAULT_LTR
)
81 static const LEUnicode separators
[] = {CH_LF
, CH_CR
, CH_LSEP
, CH_PSEP
, 0x0000};
83 if (LE_FAILURE(status
)) {
91 LocaleRuns
*locales
= NULL
;
94 fLines
= LE_NEW_ARRAY(const ParagraphLayout::Line
*, fLinesMax
);
95 fParagraphLayout
= LE_NEW_ARRAY(ParagraphLayout
*, fParagraphMax
);
97 fChars
= LE_NEW_ARRAY(LEUnicode
, charCount
+ 1);
98 LE_ARRAY_COPY(fChars
, chars
, charCount
);
99 fChars
[charCount
] = 0;
101 LEUnicode
*pStart
= &fChars
[0];
103 while (*pStart
!= 0) {
104 LEUnicode
*pEnd
= u_strpbrk(pStart
, separators
);
105 le_int32 pAscent
, pDescent
, pLeading
;
106 ParagraphLayout
*paragraphLayout
= NULL
;
109 pEnd
= &fChars
[charCount
];
112 if (pEnd
!= pStart
) {
113 subsetFontRuns(fontRuns
, pStart
- fChars
, pEnd
- fChars
, &fr
);
115 paragraphLayout
= new ParagraphLayout(pStart
, pEnd
- pStart
, &fr
, NULL
, NULL
, locales
, fParagraphLevel
, FALSE
, status
);
117 if (LE_FAILURE(status
)) {
118 delete paragraphLayout
;
119 break; // return? something else?
122 if (fParagraphLevel
== UBIDI_DEFAULT_LTR
) {
123 fParagraphLevel
= paragraphLayout
->getParagraphLevel();
126 pAscent
= paragraphLayout
->getAscent();
127 pDescent
= paragraphLayout
->getDescent();
128 pLeading
= paragraphLayout
->getLeading();
130 if (pAscent
> ascent
) {
134 if (pDescent
> descent
) {
138 if (pLeading
> leading
) {
143 if (fParagraphCount
>= fParagraphMax
) {
144 fParagraphLayout
= (ParagraphLayout
**) LE_GROW_ARRAY(fParagraphLayout
, fParagraphMax
+ fParagraphGrow
);
145 fParagraphMax
+= fParagraphGrow
;
148 fParagraphLayout
[fParagraphCount
++] = paragraphLayout
;
154 pStart
= skipLineEnd(pEnd
);
157 fLineHeight
= ascent
+ descent
+ leading
;
161 Paragraph::~Paragraph()
163 for (le_int32 line
= 0; line
< fLineCount
; line
+= 1) {
164 delete /*(LineInfo *)*/ fLines
[line
];
167 for (le_int32 paragraph
= 0; paragraph
< fParagraphCount
; paragraph
+= 1) {
168 delete fParagraphLayout
[paragraph
];
171 LE_DELETE_ARRAY(fLines
);
172 LE_DELETE_ARRAY(fParagraphLayout
);
173 LE_DELETE_ARRAY(fChars
);
176 void Paragraph::addLine(const ParagraphLayout::Line
*line
)
178 if (fLineCount
>= fLinesMax
) {
179 fLines
= (const ParagraphLayout::Line
**) LE_GROW_ARRAY(fLines
, fLinesMax
+ fLinesGrow
);
180 fLinesMax
+= fLinesGrow
;
183 fLines
[fLineCount
++] = line
;
186 void Paragraph::breakLines(le_int32 width
, le_int32 height
)
190 // don't re-break if the width hasn't changed
191 if (fWidth
== width
) {
197 float lineWidth
= (float) (width
- 2 * MARGIN
);
198 const ParagraphLayout::Line
*line
;
200 // Free the old LineInfo's...
201 for (le_int32 li
= 0; li
< fLineCount
; li
+= 1) {
207 for (le_int32 p
= 0; p
< fParagraphCount
; p
+= 1) {
208 ParagraphLayout
*paragraphLayout
= fParagraphLayout
[p
];
210 if (paragraphLayout
!= NULL
) {
211 paragraphLayout
->reflow();
212 while ((line
= paragraphLayout
->nextLine(lineWidth
)) != NULL
) {
221 void Paragraph::draw(RenderingSurface
*surface
, le_int32 firstLine
, le_int32 lastLine
)
228 for (li
= firstLine
; li
<= lastLine
; li
+= 1) {
229 const ParagraphLayout::Line
*line
= fLines
[li
];
232 le_int32 runCount
= line
->countRuns();
235 if (fParagraphLevel
== UBIDI_RTL
) {
236 le_int32 lastX
= line
->getWidth();
238 x
= (fWidth
- lastX
- MARGIN
);
242 for (run
= 0; run
< runCount
; run
+= 1) {
243 const ParagraphLayout::VisualRun
*visualRun
= line
->getVisualRun(run
);
244 le_int32 glyphCount
= visualRun
->getGlyphCount();
245 const LEFontInstance
*font
= visualRun
->getFont();
246 const LEGlyphID
*glyphs
= visualRun
->getGlyphs();
247 const float *positions
= visualRun
->getPositions();
249 surface
->drawGlyphs(font
, glyphs
, glyphCount
, positions
, x
, y
, fWidth
, fHeight
);
257 Paragraph
*Paragraph::paragraphFactory(const char *fileName
, const LEFontInstance
*font
, GUISupport
*guiSupport
)
259 LEErrorCode status
= LE_NO_ERROR
;
261 const UChar
*text
= UnicodeReader::readFile(fileName
, guiSupport
, charCount
);
262 Paragraph
*result
= NULL
;
268 FontRuns
fontRuns(0);
270 fontRuns
.add(font
, charCount
);
272 result
= new Paragraph(text
, charCount
, &fontRuns
, status
);
274 if (LE_FAILURE(status
)) {
279 LE_DELETE_ARRAY(text
);