1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/reflow/reflowengine.h"
8 #include "reflowedpage.h"
9 #include "layoutprovider_taggedpdf.h"
10 IPDF_LayoutProcessor* IPDF_LayoutProcessor::Create_LayoutProcessor_Reflow(FX_FLOAT TopIndent, FX_FLOAT fWidth, FX_FLOAT fHeight, void* pReflowedPage, int flags, FX_FLOAT lineSpace )
12 if(pReflowedPage == NULL || fWidth <= 20) {
15 CPDF_LayoutProcessor_Reflow* pReflowEngine = FX_NEW CPDF_LayoutProcessor_Reflow();
16 if (NULL == pReflowEngine) {
19 pReflowEngine->Init(TopIndent, fWidth, fHeight, (CPDF_ReflowedPage*)pReflowedPage, flags, lineSpace);
22 CPDF_LayoutProcessor_Reflow::CPDF_LayoutProcessor_Reflow()
25 m_pLayoutElement = NULL;
29 m_fCurrLineHeight = 0;
30 m_bIllustration = FALSE;
32 m_pCurrLine = FX_NEW CRF_DataPtrArray(50);
33 m_pTempLine = FX_NEW CRF_DataPtrArray(50);
37 CPDF_LayoutProcessor_Reflow::~CPDF_LayoutProcessor_Reflow()
40 m_pCurrLine->RemoveAll();
45 m_pTempLine->RemoveAll();
50 void CPDF_LayoutProcessor_Reflow::Init(FX_FLOAT TopIndent, FX_FLOAT fWidth, FX_FLOAT fHeight, CPDF_ReflowedPage* pReflowedPage, int flags, FX_FLOAT lineSpace)
52 m_pLayoutElement = NULL;
53 m_TopIndent = TopIndent;
54 m_Status = LayoutReady;
56 m_pReflowedPage = pReflowedPage;
57 m_fScreenHeight = fHeight;
59 m_fCurrLineHeight = 0;
61 m_fLineSpace = lineSpace;
62 pReflowedPage->m_PageWidth = fWidth;
63 pReflowedPage->m_PageHeight = TopIndent;
65 void CPDF_LayoutProcessor_Reflow::FitPageMode()
67 if(m_flags & RF_PARSER_PAGEMODE && m_fScreenHeight > 20) {
68 float fitPageHeight = m_fScreenHeight;
69 CPDF_ReflowedPage* pRFPage = m_pReflowedPage;
70 int count = pRFPage->m_pReflowed->GetSize();
75 FX_FLOAT h = pRFPage->GetPageHeight();
76 while (h > screenCount * fitPageHeight) {
77 FX_FLOAT tempPageHeight = screenCount * fitPageHeight;
80 for(int i = 0; i < count; i++) {
81 CRF_Data* pData = (*pRFPage->m_pReflowed)[i];
84 if(FXSYS_fabs(posY) > tempPageHeight &&
85 FXSYS_fabs(posY + pData->m_Height) < tempPageHeight) {
89 if(pData->m_Height > fitPageHeight) {
91 FX_FLOAT spaceh = screenCount * fitPageHeight + posY + pData->m_Height;
92 if(spaceh < fitPageHeight / 3 * 2) {
93 spaceh = fitPageHeight;
95 zoom = spaceh / pData->m_Height;
96 tempDy = spaceh - pData->m_Height;
97 pData->m_Height = spaceh;
98 pData->m_Width *= zoom;
101 FX_FLOAT dy = pData->m_PosY + pData->m_Height + tempPageHeight;
105 } else if(FXSYS_fabs(posY + pData->m_Height) > tempPageHeight) {
109 for(; j < count; j++) {
110 CRF_Data* pData = (*pRFPage->m_pReflowed)[j];
112 posY = pData->m_PosY;
113 if(FXSYS_fabs(posY) > tempPageHeight ) {
114 pData->m_PosY -= tempDy;
116 if(pData->m_Height >= fitPageHeight) {
117 pData->m_Height = fitPageHeight - 1;
118 if(pData->GetType() == CRF_Data::Text) {
119 CRF_CharData* pCharData = (CRF_CharData*)pData;
120 pCharData->m_pCharState->m_fFontSize = pData->m_Height;
124 pRFPage->m_PageHeight += tempDy;
130 LayoutStatus CPDF_LayoutProcessor_Reflow::StartProcess(IPDF_LayoutElement* pElement, IFX_Pause* pPause, const CFX_AffineMatrix* pPDFMatrix)
136 m_PDFMatrix = *pPDFMatrix;
137 m_pRootElement = pElement;
138 ProcessElement(m_pRootElement, m_fRefWidth);
139 if(m_Status == LayoutToBeContinued) {
140 return LayoutToBeContinued;
142 m_Status = LayoutFinished;
144 return LayoutFinished;
146 LayoutStatus CPDF_LayoutProcessor_Reflow::Continue()
148 int size = m_pReflowedPage->m_pReflowed->GetSize();
149 ProcessElement(m_pRootElement, m_CurrRefWidth);
150 size = m_pReflowedPage->m_pReflowed->GetSize();
151 if(m_Status == LayoutReady) {
152 m_Status = LayoutFinished;
157 int CPDF_LayoutProcessor_Reflow::GetPosition()
159 return m_PausePosition;
161 FX_BOOL CPDF_LayoutProcessor_Reflow::IsCanBreakAfter(FX_DWORD unicode)
174 } else if(unicode >= 48 && unicode <= 57) {
176 } else if(unicode >= 64 && unicode <= 90) {
178 } else if(unicode >= 97 && unicode <= 122) {
183 FX_BOOL CPDF_LayoutProcessor_Reflow::IsCanBreakBefore(FX_DWORD unicode)
201 } else if(unicode >= 48 && unicode <= 57) {
203 } else if(unicode >= 64 && unicode <= 90) {
205 } else if(unicode >= 97 && unicode <= 122) {
210 void CPDF_LayoutProcessor_Reflow::ProcessTable(FX_FLOAT dx)
212 if(m_pReflowedPage->m_pReflowed->GetSize() == 0) {
215 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
216 int rowCount = pTable->m_nCell.GetSize();
218 FX_FLOAT* dyRow = FX_Alloc(FX_FLOAT, rowCount + 1);
219 FXSYS_memset32(dyRow, 0, sizeof(FX_FLOAT) * (rowCount + 1));
221 dyRow[0] = - pTable->m_ReflowPageHeight;
222 int tableColCount = 0;
224 for(i = 0; i < rowCount; i++) {
225 int colCount = pTable->m_nCell.GetAt(i);
226 if(colCount > tableColCount) {
227 tableColCount = colCount;
230 int cellCount = tableColCount * rowCount;
231 RF_TableCell** pVirtualTable = FX_Alloc(RF_TableCell*, cellCount);
232 FXSYS_memset32(pVirtualTable, 0, sizeof(RF_TableCell*) * cellCount);
233 for(i = 0; i < rowCount; i++) {
234 int colCount = pTable->m_nCell.GetAt(i);
235 FX_FLOAT rowWidth = 0;
237 int s = pTable->m_pCellArray.GetSize();
238 for(j = 0; j < colCount; j++) {
239 RF_TableCell* pCell = (RF_TableCell*)pTable->m_pCellArray.GetAt(n++);
240 if(pCell->m_EndPos < pCell->m_BeginPos) {
243 int pos = i * tableColCount;
244 while(pos < cellCount && pVirtualTable[pos] != NULL) {
247 if(pos >= (i + 1) * tableColCount) {
248 pos = i * tableColCount + j;
250 int RowSpan = pCell->m_RowSpan;
251 int ColSpan = pCell->m_ColSpan;
252 if(RowSpan + i > rowCount) {
253 RowSpan = rowCount - i;
255 if(ColSpan + j > colCount) {
256 ColSpan = colCount - j;
258 for(int m = 0; m < RowSpan; m++) {
259 for(int nn = 0; nn < ColSpan; nn++) {
260 if(pos + nn >= cellCount) {
263 pVirtualTable[pos + nn] = pCell;
265 pos += tableColCount;
267 FX_FLOAT dxCell = dx;
268 for(pos = i * tableColCount; pVirtualTable[pos] != pCell && pos < cellCount; pos++) {
269 dxCell += (pVirtualTable[pos])->m_MaxWidth;
271 CRF_Data* pData = (*m_pReflowedPage->m_pReflowed)[pCell->m_BeginPos];
272 FX_FLOAT dy = dyRow[i] - pData->m_Height - pData->m_PosY;
273 CFX_AffineMatrix matrix(1, 0, 0, 1, dxCell, dy);
274 Transform(&matrix, m_pReflowedPage->m_pReflowed, pCell->m_BeginPos, pCell->m_EndPos - pCell->m_BeginPos + 1);
275 if(pCell->m_RowSpan + i <= rowCount) {
276 if(FXSYS_fabs(dyRow[pCell->m_RowSpan + i]) < FXSYS_fabs(dyRow[i] - pCell->m_CellHeight)) {
277 dyRow[pCell->m_RowSpan + i] = dyRow[i] - pCell->m_CellHeight;
283 for(i = 0; i < rowCount; i++) {
284 int colCount = pTable->m_nCell.GetAt(i);
285 for(int j = 0; j < colCount; j++) {
286 RF_TableCell* pCell = (RF_TableCell*)pTable->m_pCellArray.GetAt(n++);
287 switch(pCell->m_BlockAlign) {
289 FX_FLOAT dy = dyRow[i + pCell->m_RowSpan] - pCell->m_CellHeight - dyRow[i];
290 CFX_AffineMatrix matrix(1, 0, 0, 1, 0, dy);
291 Transform(&matrix, m_pReflowedPage->m_pReflowed, pCell->m_BeginPos, pCell->m_EndPos - pCell->m_BeginPos + 1);
295 case LayoutJustify: {
296 FX_FLOAT dy = (dyRow[i + pCell->m_RowSpan] + pCell->m_CellHeight - dyRow[i]) / 2;
297 CFX_AffineMatrix matrix(1, 0, 0, 1, 0, dy);
298 Transform(&matrix, m_pReflowedPage->m_pReflowed, pCell->m_BeginPos, pCell->m_EndPos - pCell->m_BeginPos + 1);
306 CRF_Data* pData = (*m_pReflowedPage->m_pReflowed)[m_pReflowedPage->m_pReflowed->GetSize() - 1];
307 m_pReflowedPage->m_PageHeight = - dyRow[rowCount] + pData->m_Height;
308 FX_Free(pVirtualTable);
310 int size = pTable->m_pCellArray.GetSize();
311 for(i = 0; i < size; i++) {
312 RF_TableCell* pCell = pTable->m_pCellArray.GetAt(i);
315 pTable->m_pCellArray.RemoveAll();
316 pTable->m_nCell.RemoveAll();
317 int s = sizeof(CRF_Table);
319 m_TableArray.RemoveAt(m_TableArray.GetSize() - 1);
321 CFX_FloatRect CPDF_LayoutProcessor_Reflow::GetElmBBox(IPDF_LayoutElement* pElement)
324 int objCount = pElement->CountObjects();
325 int count = pElement->CountChildren();
326 if(objCount == 0 && count == 0) {
329 CFX_AffineMatrix matrix;
331 for(i = 0; i < objCount; i++) {
332 CPDF_PageObject* pObj = pElement->GetObject(0);
336 if( rect.Height() == 0 ) {
337 rect = pObj->GetBBox(&matrix);
339 rect.Union(pObj->GetBBox(&matrix));
342 for(i = 0; i < count; i++) {
343 IPDF_LayoutElement* pChildElement = pElement->GetChild(i);
344 if( rect.Height() == 0 ) {
345 rect = GetElmBBox(pChildElement);
347 rect.Union(GetElmBBox(pChildElement));
352 FX_FLOAT CPDF_LayoutProcessor_Reflow::GetElmWidth(IPDF_LayoutElement* pElement)
357 LayoutType layoutType = pElement->GetType();
359 if(layoutType == LayoutTable || layoutType == LayoutTableDataCell || layoutType == LayoutTableHeaderCell) {
360 width = pElement->GetNumberAttr(LayoutWidth);
364 } else if( layoutType == LayoutTableRow) {
365 int count = pElement->CountChildren();
366 for(int i = 0; i < count; i++) {
367 IPDF_LayoutElement* pElm = pElement->GetChild(i);
368 width += pElm->GetNumberAttr(LayoutWidth);
374 CFX_FloatRect rect = GetElmBBox(pElement);
377 FX_BOOL GetIntersection(FX_FLOAT low1, FX_FLOAT high1, FX_FLOAT low2, FX_FLOAT high2,
378 FX_FLOAT& interlow, FX_FLOAT& interhigh);
379 FX_BOOL IsSameLine(FX_BOOL bHorizontal, CFX_FloatRect Rect1, CFX_FloatRect Rect2)
382 FX_FLOAT inter_top, inter_bottom;
383 if (!GetIntersection(Rect1.bottom, Rect1.top, Rect2.bottom, Rect2.top,
384 inter_bottom, inter_top)) {
387 FX_FLOAT lineHeight = Rect1.top - Rect1.bottom;
388 if(lineHeight > 20 && lineHeight > Rect2.Height() * 2) {
391 if(lineHeight > 5 && Rect2.Height() / 2 > lineHeight) {
394 FX_FLOAT inter_h = inter_top - inter_bottom;
395 if (inter_h < (lineHeight) / 2 && inter_h < Rect2.Height() / 2) {
399 FX_FLOAT inter_left, inter_right;
400 if(!GetIntersection(Rect1.left, Rect1.right, Rect2.left, Rect2.right, inter_left, inter_right)) {
403 FX_FLOAT inter_w = inter_right - inter_left;
404 if (inter_w < (Rect1.right - Rect1.left) / 2 && inter_w < (Rect2.right - Rect2.left) / 2) {
410 FX_INT32 IsCanMergeParagraph(IPDF_LayoutElement* pPrevElement, IPDF_LayoutElement* pNextElement)
412 FX_INT32 analogial = 100;
413 FX_INT32 nPrevObj = pPrevElement->CountObjects(), i;
414 CPDF_PageObject* pPrevObj = NULL;
415 CFX_FloatRect prevRect, rect;
416 CFX_PtrArray prevLine, line;
417 FX_BOOL bParagraphStart = FALSE;
418 for(i = 0; i < nPrevObj; i++) {
419 CPDF_PageObject* pObj = pPrevElement->GetObject(i);
422 rect = CFX_FloatRect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
426 CFX_FloatRect objRect = CFX_FloatRect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
427 if(IsSameLine(TRUE, rect, objRect)) {
431 prevLine.RemoveAll();
432 prevLine.Append(line);
437 if(!bParagraphStart) {
438 if (prevRect.left > rect.left + rect.Height() * 1.5) {
439 bParagraphStart = TRUE;
444 if(prevLine.GetSize()) {
445 if(FXSYS_fabs(rect.right - prevRect.right) > rect.Height()) {
449 CPDF_PageObject* pObj = pPrevElement->GetObject(nPrevObj - 1);
450 if(pObj->m_Type == PDFPAGE_TEXT) {
451 CPDF_TextObject* pText = (CPDF_TextObject*)pObj;
452 FX_INT32 nItem = pText->CountItems();
453 CPDF_TextObjectItem item;
454 pText->GetItemInfo(nItem - 1, &item);
455 CFX_WideString wStr = pText->GetFont()->UnicodeFromCharCode(item.m_CharCode);
457 wStr = (FX_WCHAR)item.m_CharCode;
459 FX_WCHAR wch = wStr.GetAt(wStr.GetLength() - 1);
471 prevLine.RemoveAll();
472 prevLine.Append(line);
474 FX_INT32 nNextObj = pNextElement->CountObjects();
476 FX_BOOL bFirst = TRUE;
477 for(i = 0; i < nNextObj; i++) {
478 CPDF_PageObject* pObj = pNextElement->GetObject(i);
481 rect = CFX_FloatRect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
485 CFX_FloatRect objRect = CFX_FloatRect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
486 if(IsSameLine(TRUE, rect, objRect)) {
490 if(FXSYS_fabs(rect.right - prevRect.right) < rect.Height() && FXSYS_fabs(rect.left - prevRect.left) < rect.Height()) {
493 prevLine.RemoveAll();
494 prevLine.Append(line);
505 if(prevLine.GetSize()) {
506 if(bParagraphStart) {
507 if(prevRect.left - rect.left > rect.Height() && prevRect.left - rect.left < rect.Height() * 3) {
511 if(FXSYS_fabs(prevRect.left - rect.left) < rect.Height()) {
518 void CPDF_LayoutProcessor_Reflow::ProcessElement(IPDF_LayoutElement* pElement, FX_FLOAT reflowWidth)
520 if(pElement == NULL) {
523 if(m_Status == LayoutReady) {
524 LayoutType layoutType = pElement->GetType();
525 FX_INT32 ElementType = GetElementTypes(layoutType);
526 switch(ElementType) {
528 m_bIllustration = TRUE;
532 FX_FLOAT StartIndent = 0;
533 if(IPDF_LayoutElement* pParent = pElement->GetParent()) {
534 StartIndent = pParent->GetNumberAttr(LayoutStartIndent);
536 FX_FLOAT currStartIndent = pElement->GetNumberAttr(LayoutStartIndent);
537 m_StartIndent = ConverWidth(currStartIndent);
538 FX_FLOAT width = reflowWidth;
539 if(StartIndent != currStartIndent) {
540 reflowWidth -= m_StartIndent;
542 FX_FLOAT spaceBefore = pElement->GetNumberAttr(LayoutSpaceBefore);
543 m_pReflowedPage->m_PageHeight += spaceBefore;
544 m_TextAlign = pElement->GetEnumAttr(LayoutTextAlign);
545 if(IPDF_LayoutElement* pParent = pElement->GetParent()) {
546 StartIndent = pParent->GetNumberAttr(LayoutEndIndent);
547 FX_FLOAT currEndIndent = pElement->GetNumberAttr(LayoutEndIndent);
548 if(StartIndent != currStartIndent) {
549 reflowWidth -= ConverWidth(currEndIndent);
552 if(reflowWidth * 2 < width) {
560 CRF_Table* pTable = FX_NEW CRF_Table;
561 if (NULL == pTable) {
564 m_TableArray.Add(pTable);
565 pTable->m_ReflowPageHeight = m_pReflowedPage->m_PageHeight;
566 pTable->m_TableWidth = GetElmWidth(pElement);
569 case LayoutTableRow: {
570 if(!m_TableArray.GetSize()) {
573 int count = pElement->CountChildren();
574 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
576 for(int i = 0; i < count; i++) {
577 IPDF_LayoutElement* pChildElement = pElement->GetChild(i);
578 LayoutType type = pChildElement->GetType();
579 if(type == LayoutTableDataCell || type == LayoutTableHeaderCell) {
583 pTable->m_nCell.Add(f);
586 case LayoutTableDataCell:
587 case LayoutTableHeaderCell: {
588 if(!m_TableArray.GetSize()) {
591 RF_TableCell* pCell = FX_Alloc(RF_TableCell, 1);
592 FXSYS_memset32(pCell, 0 , sizeof(RF_TableCell));
593 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
594 int pos = pTable->m_nCell.GetSize() - 1;
595 pCell->m_BeginPos = m_pReflowedPage->m_pReflowed->GetSize();
596 FX_FLOAT cellWidth = pElement->GetNumberAttr(LayoutWidth);
597 if(cellWidth == 0 || pCell->m_MaxWidth > pTable->m_TableWidth) {
598 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
599 pCell->m_MaxWidth = reflowWidth / pTable->m_nCell.GetAt(pTable->m_nCell.GetSize() - 1);
601 pCell->m_MaxWidth = pElement->GetNumberAttr(LayoutWidth) * reflowWidth / pTable->m_TableWidth;
603 pCell->m_ColSpan = (int)(pElement->GetNumberAttr(LayoutColSpan));
604 pCell->m_RowSpan = (int)(pElement->GetNumberAttr(LayoutRowSpan));
605 if(!pCell->m_ColSpan) {
606 pCell->m_ColSpan = 1;
608 if(!pCell->m_RowSpan ) {
609 pCell->m_RowSpan = 1;
611 pCell->m_BlockAlign = pElement->GetEnumAttr(LayoutBlockAlign);
612 m_TextAlign = pElement->GetEnumAttr(LayoutInlineAlign);
615 reflowWidth = pCell->m_MaxWidth;
616 pTable->m_pCellArray.Add(pCell);
622 m_fLineHeight = pElement->GetNumberAttr(LayoutLineHeight);
623 int ReflowedSize = m_pReflowedPage->m_pReflowed->GetSize();
624 if(pElement->CountObjects()) {
625 ProcessObjs(pElement, reflowWidth);
628 int count = pElement->CountChildren();
629 for(int i = 0; i < count; i++) {
630 IPDF_LayoutElement* pChildElement = pElement->GetChild(i);
631 ProcessElement(pChildElement, reflowWidth);
632 if(m_pPause && m_pRootElement == pElement && m_Status != LayoutToBeContinued ) {
633 if(m_pPause->NeedToPauseNow()) {
634 m_pLayoutElement = pChildElement;
635 m_Status = LayoutToBeContinued;
636 m_CurrRefWidth = reflowWidth;
637 m_PausePosition = (i + 1) * 100 / (count + 1);
641 if(m_Status == LayoutToBeContinued && m_pLayoutElement == pChildElement) {
642 m_Status = LayoutReady;
645 if(m_Status == LayoutReady) {
647 LayoutType layoutType = pElement->GetType();
648 FX_INT32 ElementType = GetElementTypes(layoutType);
649 switch(ElementType) {
651 m_bIllustration = FALSE;
656 FX_FLOAT StartIndent = 0;
657 if(IPDF_LayoutElement* pParent = pElement->GetParent()) {
658 StartIndent = pParent->GetNumberAttr(LayoutStartIndent);
660 FX_FLOAT currStartIndent = pElement->GetNumberAttr(LayoutStartIndent);
661 if(StartIndent != currStartIndent) {
662 reflowWidth += ConverWidth(currStartIndent);
663 dx += ConverWidth(currStartIndent);
665 FX_FLOAT spaceAfter = pElement->GetNumberAttr(LayoutSpaceAfter);
666 m_pReflowedPage->m_PageHeight += spaceAfter;
670 case LayoutTableDataCell:
671 case LayoutTableHeaderCell: {
672 if(!m_TableArray.GetSize()) {
675 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
676 RF_TableCell* pCell = pTable->m_pCellArray.GetAt(pTable->m_pCellArray.GetSize() - 1);
677 pCell->m_EndPos = m_pReflowedPage->m_pReflowed->GetSize() - 1;
678 if(pCell->m_EndPos < pCell->m_BeginPos) {
679 pCell->m_CellHeight = 0;
681 CRF_Data* pBeginData = (*m_pReflowedPage->m_pReflowed)[pCell->m_BeginPos];
682 CRF_Data* pEndData = (*m_pReflowedPage->m_pReflowed)[pCell->m_EndPos];
683 pCell->m_CellHeight = pBeginData->m_Height > pEndData->m_Height ? pBeginData->m_Height : pEndData->m_Height;
684 pCell->m_CellHeight -= pEndData->m_PosY - pBeginData->m_PosY;
688 case LayoutTableRow: {
689 if(!m_TableArray.GetSize()) {
692 CRF_Table* pTable = m_TableArray.GetAt(m_TableArray.GetSize() - 1);
693 if(pTable->m_nCol == 0) {
694 pTable->m_nCol = pTable->m_pCellArray.GetSize();
704 CFX_AffineMatrix matrix(1, 0, 0, 1, dx, 0);
705 int ReflowedSize = m_pReflowedPage->m_pReflowed->GetSize();
706 Transform(&matrix, m_pReflowedPage->m_pReflowed, ReflowedSize, m_pReflowedPage->m_pReflowed->GetSize() - ReflowedSize);
710 if(m_pRootElement == pElement) {
711 m_PausePosition = 100;
714 FX_INT32 CPDF_LayoutProcessor_Reflow::GetElementTypes(LayoutType layoutType)
717 case LayoutParagraph:
727 case LayoutListLabel:
730 case LayoutTableHeaderCell:
731 case LayoutTableDataCell:
733 case LayoutTableHeaderGroup:
734 case LayoutTableBodyGroup:
735 case LayoutTableFootGroup:
746 case LayoutReference:
759 FX_FLOAT CPDF_LayoutProcessor_Reflow::ConverWidth(FX_FLOAT width)
763 void CPDF_LayoutProcessor_Reflow::ProcessObject(CPDF_PageObject* pObj, FX_FLOAT reflowWidth, CFX_AffineMatrix objMatrix)
768 if(pObj->m_Type == PDFPAGE_TEXT) {
769 ProcessTextObject( (CPDF_TextObject *)pObj, reflowWidth, objMatrix);
770 } else if(pObj->m_Type == PDFPAGE_IMAGE) {
771 if(!(m_flags & RF_PARSER_IMAGE)) {
774 CPDF_PageObjects* pObjs = FX_NEW CPDF_PageObjects(FALSE);
778 FX_POSITION pos = pObjs->GetLastObjectPosition();
779 pos = pObjs->InsertObject(pos, pObj);
780 CFX_AffineMatrix matrix;
781 FX_RECT rect = pObj->GetBBox(&matrix);
782 CPDF_ImageObject* ImageObj = (CPDF_ImageObject*)pObj;
783 ProcessUnitaryObjs(pObjs, reflowWidth, objMatrix);
785 } else if(pObj->m_Type == PDFPAGE_PATH) {
786 } else if(pObj->m_Type == PDFPAGE_FORM) {
787 CPDF_FormObject* pForm = (CPDF_FormObject*)pObj;
788 FX_POSITION pos = pForm->m_pForm->GetFirstObjectPosition();
789 objMatrix.Concat(pForm->m_FormMatrix);
791 CPDF_PageObject* pObj1 = pForm->m_pForm->GetNextObject(pos);
792 ProcessObject(pObj1, reflowWidth, objMatrix);
796 void CPDF_LayoutProcessor_Reflow::ProcessObjs(IPDF_LayoutElement* pElement, FX_FLOAT reflowWidth)
798 m_fCurrMaxWidth = reflowWidth;
799 int ObjCount = pElement->CountObjects();
800 for(int i = 0; i < ObjCount; i++) {
801 CPDF_PageObject* pObj = pElement->GetObject(i);
802 ProcessObject(pObj, reflowWidth, m_PDFMatrix);
806 void CPDF_LayoutProcessor_Reflow::AddTemp2CurrLine(int begin, int count)
808 if(begin < 0 || count <= 0 || !m_pReflowedPage || !m_pReflowedPage->m_pReflowed || !m_pTempLine) {
813 int size = m_pReflowedPage->m_pReflowed->GetSize();
814 int temps = m_pTempLine->GetSize();
815 for(int i = begin; i < count; i++) {
816 CRF_Data* pData = (*m_pTempLine)[i];
817 AddData2CurrLine(pData);
820 void CPDF_LayoutProcessor_Reflow::AddData2CurrLine(CRF_Data* pData)
822 if(pData == NULL || m_pCurrLine == NULL) {
825 m_pCurrLine->Add(pData);
826 m_fCurrLineWidth = pData->m_PosX + pData->m_Width;
827 if(pData->m_Height > m_fCurrLineHeight) {
828 m_fCurrLineHeight = pData->m_Height;
831 void CPDF_LayoutProcessor_Reflow::UpdateCurrLine()
834 void CPDF_LayoutProcessor_Reflow::Transform(const CFX_AffineMatrix* pMatrix, CRF_DataPtrArray* pDataArray, int beginPos, int count)
840 count = pDataArray->GetSize();
844 for(int i = beginPos; i < count; i++) {
845 CRF_Data* pData = (*pDataArray)[i];
846 Transform(pMatrix, pData);
849 void CPDF_LayoutProcessor_Reflow::Transform(const CFX_AffineMatrix* pMatrix, CRF_Data* pData)
851 if(pData->GetType() == CRF_Data::Path) {
852 CRF_PathData* pPathData = (CRF_PathData*)pData;
853 pPathData->m_pPath2Device.Concat(*pMatrix);
855 pMatrix->Transform(pData->m_PosX, pData->m_PosY, pData->m_PosX, pData->m_PosY);
857 FX_BOOL CPDF_LayoutProcessor_Reflow::FinishedCurrLine()
859 if (NULL == m_pCurrLine) {
862 int count = m_pCurrLine->GetSize();
866 if(m_fLineHeight > m_fCurrLineHeight) {
867 m_fCurrLineHeight = m_fLineHeight;
869 m_fCurrLineHeight += 2;
871 if(m_pReflowedPage->m_pReflowed->GetSize() > 0) {
872 m_fCurrLineHeight += m_fLineSpace;
874 FX_FLOAT height = m_pReflowedPage->m_PageHeight + m_fCurrLineHeight;
875 FX_FLOAT lineHeight = m_fLineHeight;
876 if(lineHeight == 0) {
877 lineHeight = m_fCurrLineHeight;
880 switch(m_TextAlign) {
882 dx = (m_fCurrMaxWidth - m_fCurrLineWidth) / 2;
885 dx = m_fCurrMaxWidth - m_fCurrLineWidth;
892 FX_FLOAT dy = - height;
893 int refedSize = m_pReflowedPage->m_pReflowed->GetSize();
897 for(int i = 0; i < count; i++) {
898 CRF_Data* pData = (*m_pCurrLine)[i];
899 m_pReflowedPage->m_pReflowed->Add(pData);
900 FX_FLOAT x = m_StartIndent + dx * (m_TextAlign == LayoutJustify ? i + 1 : 1);
901 CFX_AffineMatrix matrix(1, 0, 0, 1, x, dy);
902 Transform(&matrix, pData);
904 m_pCurrLine->RemoveAll();
905 m_fCurrLineWidth = 0;
906 m_pReflowedPage->m_PageHeight += m_fCurrLineHeight;
907 m_fCurrLineHeight = 0;
910 CRF_CharState* CPDF_LayoutProcessor_Reflow::GetCharState(CPDF_TextObject* pObj, CPDF_Font* pFont, FX_FLOAT fHeight, FX_ARGB color)
912 if (NULL == m_pReflowedPage->m_pCharState) {
915 int count = m_pReflowedPage->m_pCharState->GetSize();
916 for(int i = count - 1; i >= 0; i--) {
917 CRF_CharState* pState = (CRF_CharState*)m_pReflowedPage->m_pCharState->GetAt(i);
918 if(pState->m_Color == color && pState->m_fFontSize == fHeight && pState->m_pFont == pFont && pState->m_pTextObj == pObj) {
922 CRF_CharState pState;
923 pState.m_pTextObj = pObj;
924 pState.m_Color = color;
925 pState.m_pFont = pFont;
926 pState.m_fFontSize = fHeight;
927 int ascent = pFont->GetTypeAscent();
928 int descent = pFont->GetTypeDescent();
929 pState.m_fAscent = ascent * fHeight / (ascent - descent);
931 pState.m_fDescent = 0;
933 pState.m_fDescent = descent * fHeight / (ascent - descent);
935 pState.m_bVert = FALSE;
936 CPDF_CIDFont *pCIDFont = pFont->GetCIDFont();
938 pState.m_bVert = pCIDFont->IsVertWriting();
940 m_pReflowedPage->m_pCharState->Add(pState);
941 return (CRF_CharState*)m_pReflowedPage->m_pCharState->GetAt(count);
943 int CPDF_LayoutProcessor_Reflow::GetCharWidth(FX_DWORD charCode, CPDF_Font* pFont) const
948 int w = pFont->GetCharWidthF(charCode);
951 pFont->AppendChar(str, charCode);
952 w = pFont->GetStringWidth(str, 1);
955 pFont->GetCharBBox(charCode, BBox);
956 w = BBox.right - BBox.left;
961 void CPDF_LayoutProcessor_Reflow::CreateRFData(CPDF_PageObject* pObj, CFX_AffineMatrix* pObjMatrix)
963 if (NULL == m_pReflowedPage->m_pMemoryPool) {
966 if(pObj->m_Type == PDFPAGE_TEXT) {
967 CPDF_TextObject* pTextObj = (CPDF_TextObject* )pObj;
968 int count = pTextObj->CountItems();
973 CPDF_TextObjectItem Item;
974 pTextObj->GetItemInfo(0, &Item);
975 if(Item.m_CharCode == 49) {
979 CPDF_Font * pFont = pTextObj->GetFont();
980 FX_FLOAT fs = pTextObj->GetFontSize();
981 FX_FLOAT* pmatrix = pTextObj->m_TextState.GetMatrix();
982 FX_FLOAT matrix1 = pmatrix[1];
983 if(pmatrix[2] == 0) {
986 CFX_AffineMatrix textMatrix(pmatrix[0], matrix1, pmatrix[2], pmatrix[3], 0, 0);
987 FX_FLOAT height = FXSYS_fabs(textMatrix.TransformDistance(fs));
989 height = FXSYS_fabs(pObjMatrix->TransformDistance(height));
991 int r = 0, g = 0, b = 0;
992 pTextObj->m_ColorState.GetFillColor()->GetRGB(r, g, b);
993 FX_ARGB col = r * 0x10000;
996 CRF_CharState* pState = GetCharState(pTextObj, pFont, height, col);
997 FX_FLOAT dx = 0, dy = 0;
1000 ObjBBox = pTextObj->GetBBox(pObjMatrix);
1001 dx = (float)ObjBBox.left;
1002 dy = (float)ObjBBox.bottom;
1004 CFX_AffineMatrix matrix;
1005 ObjBBox = pTextObj->GetBBox(&matrix);
1007 FX_FLOAT objWidth = 0;
1009 FX_BOOL bOrder = TRUE;
1010 CFX_PtrArray tempArray;
1012 CPDF_TextObjectItem Item;
1013 pTextObj->GetItemInfo(i, &Item);
1014 dx = Item.m_OriginX;
1015 dy = Item.m_OriginY;
1016 textMatrix.Transform(Item.m_OriginX, Item.m_OriginY, dx, dy);
1017 CRF_CharData* pLastData = NULL;
1018 FX_FLOAT horzScale = pTextObj->m_TextState.GetFontSizeV() / pTextObj->m_TextState.GetFontSizeH();
1020 pTextObj->GetItemInfo(i, &Item);
1021 if(Item.m_CharCode == -1) {
1025 FX_FLOAT OriginX, OriginY;
1026 textMatrix.Transform(Item.m_OriginX, Item.m_OriginY, OriginX, OriginY);
1027 CRF_CharData* pData = (CRF_CharData*)m_pReflowedPage->m_pMemoryPool->Alloc(sizeof(CRF_CharData));
1028 if (NULL == pData) {
1031 pData->m_Type = CRF_Data::Text;
1032 if(FXSYS_fabs(OriginY - dy) > FXSYS_fabs(OriginX - dx)) {
1034 pData->m_PosX = pLastData->m_PosX + pLastData->m_Width + textMatrix.TransformDistance(pTextObj->m_TextState.GetObject()->m_CharSpace);
1036 pData->m_PosY = OriginY;
1037 pData->m_PosX = OriginX;
1039 int size = tempArray.GetSize();
1040 if(size && pData->m_PosX < pLastData->m_PosX ) {
1041 for (int j = 0; j < size; j++) {
1042 CRF_CharData* pData1 = (CRF_CharData*)tempArray.GetAt(j);
1043 if(pData1->m_PosX > pData->m_PosX) {
1044 tempArray.InsertAt(j, pData);
1049 tempArray.Add(pData);
1052 pData->m_CharCode = Item.m_CharCode;
1053 pData->m_Height = FXSYS_fabs(height);
1054 int w = GetCharWidth(Item.m_CharCode, pFont);
1055 pData->m_Width = FXSYS_fabs(fs * textMatrix.TransformDistance((FX_FLOAT)w) / 1000);
1057 pData->m_Width /= horzScale;
1059 pData->m_pCharState = pState;
1062 count = tempArray.GetSize();
1063 for (int j = 0; j < count; j++) {
1064 CRF_CharData* pData = (CRF_CharData*)tempArray.GetAt(j);
1066 m_pTempLine->Add(pData);
1069 tempArray.RemoveAll();
1070 } else if(pObj->m_Type == PDFPAGE_IMAGE) {
1071 CPDF_ImageObject* pImageObj = (CPDF_ImageObject* )pObj;
1072 CRF_ImageData* pRFImage = (CRF_ImageData*)m_pReflowedPage->m_pMemoryPool->Alloc(sizeof(CRF_ImageData));
1073 if (NULL == pRFImage) {
1076 pRFImage->m_pBitmap = NULL;
1077 pRFImage->m_Type = CRF_Data::Image;
1079 m_pTempLine->Add(pRFImage);
1081 CPDF_Image *pImage = pImageObj->m_pImage;
1082 if (!pImage->m_pDIBSource || !pImage->m_pMask) {
1083 if(pImage->StartLoadDIBSource(m_pReflowedPage->GetFormResDict(pImageObj), m_pReflowedPage->m_pPDFPage->m_pResources, 0, 0, TRUE)) {
1084 pImage->Continue(NULL);
1087 CFX_DIBSource* pDibSource = pImage->DetachBitmap();
1089 pRFImage->m_pBitmap = pDibSource->Clone();
1092 CFX_DIBSource* pMask = pImage->DetachMask();
1094 if (!pMask->IsAlphaMask()) {
1095 CFX_DIBitmap* pMaskBmp = pMask->Clone();
1096 pMaskBmp->ConvertFormat(FXDIB_8bppMask);
1097 pRFImage->m_pBitmap->MultiplyAlpha(pMaskBmp);
1100 pRFImage->m_pBitmap->MultiplyAlpha(pMask);
1104 CFX_FloatRect ObjBBox;
1106 ObjBBox = pImageObj->GetBBox(pObjMatrix);
1108 CFX_AffineMatrix matrix;
1109 ObjBBox = pImageObj->GetBBox(&matrix);
1111 pRFImage->m_Width = ObjBBox.Width();
1112 pRFImage->m_Height = ObjBBox.Height();
1113 pRFImage->m_PosX = 0;
1114 pRFImage->m_PosY = 0;
1115 CFX_AffineMatrix matrix(1, 0, 0, -1, 0, 0);
1116 matrix.Concat(pImageObj->m_Matrix);
1117 matrix.Concat(*pObjMatrix);
1118 pRFImage->m_Matrix.Set(matrix.a == 0 ? 0 : matrix.a / FXSYS_fabs(matrix.a),
1119 matrix.b == 0 ? 0 : matrix.b / FXSYS_fabs(matrix.b),
1120 matrix.c == 0 ? 0 : matrix.c / FXSYS_fabs(matrix.c),
1121 matrix.d == 0 ? 0 : matrix.d / FXSYS_fabs(matrix.d), 0, 0);
1122 } else if(pObj->m_Type == PDFPAGE_PATH) {
1125 FX_FLOAT CPDF_LayoutProcessor_Reflow:: GetDatasWidth(int beginPos, int endpos)
1127 if(endpos < beginPos || !m_pTempLine) {
1130 if(endpos > m_pTempLine->GetSize() - 1) {
1131 endpos = m_pTempLine->GetSize() - 1;
1133 CRF_Data* pBeginData = (*m_pTempLine)[beginPos];
1134 CRF_Data* pEndData = (*m_pTempLine)[endpos];
1135 return pEndData->m_PosX - pBeginData->m_PosX + pEndData->m_Width;
1137 FX_WCHAR CPDF_LayoutProcessor_Reflow::GetPreChar()
1139 if (NULL == m_pCurrLine) {
1142 int index = m_pCurrLine->GetSize() - 1;
1143 CRF_CharData* pCharData = NULL;
1144 while (index >= 0 && !pCharData) {
1145 CRF_Data* pData = (*m_pCurrLine)[index];
1146 if(pData->GetType() == CRF_Data::Text) {
1147 pCharData = (CRF_CharData*)pData;
1153 if(m_pReflowedPage) {
1154 index = m_pReflowedPage->m_pReflowed->GetSize() - 1;
1156 while(!pCharData && index >= 0) {
1157 CRF_Data* pData = (*m_pReflowedPage->m_pReflowed)[index];
1158 if(pData->GetType() == CRF_Data::Text) {
1159 pCharData = (CRF_CharData*)pData;
1166 CFX_WideString str = pCharData->m_pCharState->m_pFont->UnicodeFromCharCode(pCharData->m_CharCode);
1167 return str.GetAt(0);
1171 int CPDF_LayoutProcessor_Reflow::ProcessInsertObject(CPDF_TextObject* pObj, CFX_AffineMatrix formMatrix)
1173 if(!pObj || !m_pPreObj || !m_pCurrLine) {
1176 if(m_pCurrLine->GetSize() == 0) {
1179 CPDF_TextObjectItem item;
1180 int nItem = m_pPreObj->CountItems();
1181 m_pPreObj->GetItemInfo(nItem - 1, &item);
1182 FX_FLOAT last_pos = item.m_OriginX;
1183 FX_FLOAT last_width = GetCharWidth(item.m_CharCode, m_pPreObj->GetFont()) * m_pPreObj->GetFontSize() / 1000;
1184 last_width = FXSYS_fabs(last_width);
1185 pObj->GetItemInfo(0, &item);
1186 FX_FLOAT this_width = GetCharWidth(item.m_CharCode, pObj->GetFont()) * pObj->GetFontSize() / 1000;
1187 this_width = FXSYS_fabs(this_width);
1188 FX_FLOAT threshold = last_width > this_width ? last_width / 4 : this_width / 4;
1189 CFX_AffineMatrix prev_matrix, prev_reverse;
1190 m_pPreObj->GetTextMatrix(&prev_matrix);
1191 prev_matrix.Concat(m_perMatrix);
1192 prev_reverse.SetReverse(prev_matrix);
1193 FX_FLOAT x = pObj->GetPosX(), y = pObj->GetPosY();
1194 formMatrix.Transform(x, y);
1195 prev_reverse.Transform(x, y);
1196 FX_WCHAR preChar = GetPreChar();
1197 CFX_WideString wstrItem = pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
1198 FX_WCHAR curChar = wstrItem.GetAt(0);
1199 if (FXSYS_fabs(y) > threshold * 2) {
1200 if (preChar == L'-') {
1203 if (preChar != L' ') {
1208 if ((x - last_pos - last_width) > threshold && curChar != L' ' && preChar != L' ') {
1213 FX_INT32 CPDF_LayoutProcessor_Reflow::LogicPreObj(CPDF_TextObject* pObj)
1215 CPDF_TextObject* pPreObj = m_pPreObj;
1217 if(!pObj || !pPreObj) {
1220 CPDF_TextObjectItem item;
1221 pPreObj->GetItemInfo(pPreObj->CountItems() - 1, &item);
1222 FX_FLOAT last_pos = item.m_OriginX;
1223 FX_FLOAT last_width = pPreObj->GetFont()->GetCharWidthF(item.m_CharCode) * pPreObj->GetFontSize() / 1000;
1224 last_width = FXSYS_fabs(last_width);
1225 pObj->GetItemInfo(0, &item);
1226 FX_FLOAT this_width = pObj->GetFont()->GetCharWidthF(item.m_CharCode) * pObj->GetFontSize() / 1000;
1227 this_width = FXSYS_fabs(this_width);
1228 FX_FLOAT threshold = last_width > this_width ? last_width / 4 : this_width / 4;
1229 CFX_AffineMatrix prev_matrix, prev_reverse;
1230 pPreObj->GetTextMatrix(&prev_matrix);
1231 prev_reverse.SetReverse(prev_matrix);
1232 FX_FLOAT x = pObj->GetPosX(), y = pObj->GetPosY();
1233 prev_reverse.Transform(x, y);
1234 CFX_WideString wstrItem = pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
1235 FX_WCHAR curChar = wstrItem.GetAt(0);
1236 if (FXSYS_fabs(y) > threshold * 2) {
1239 FX_WCHAR preChar = 0;
1240 if (FXSYS_fabs(last_pos + last_width - x) > threshold && curChar != L' ') {
1248 if(pPreObj->m_Type != pObj->m_Type) {
1251 CFX_FloatRect rcCurObj(pObj->m_Left, pObj->m_Bottom, pObj->m_Right, pObj->m_Top);
1252 CFX_FloatRect rcPreObj(pPreObj->m_Left, pPreObj->m_Bottom, pPreObj->m_Right, pPreObj->m_Top);
1253 if(pObj->m_Type == PDFPAGE_IMAGE) {
1254 if(rcPreObj.Contains(rcCurObj)) {
1257 if(rcCurObj.Contains(rcPreObj)) {
1262 if(pObj->m_Type == PDFPAGE_TEXT) {
1263 if(!((rcPreObj.bottom > rcCurObj.top) || (rcPreObj.top < rcCurObj.bottom))) {
1264 FX_FLOAT height = FX_MIN(rcPreObj.Height(), rcCurObj.Height());
1265 if((rcCurObj.left - rcPreObj.right) > height / 3) {
1269 if(FXSYS_fabs(rcPreObj.Width() - rcCurObj.Width()) >= 2 || FXSYS_fabs(rcPreObj.Height() - rcCurObj.Height()) >= 2 ) {
1272 CPDF_TextObject* pPreTextObj = (CPDF_TextObject*)pPreObj;
1273 CPDF_TextObject* pCurTextObj = (CPDF_TextObject*)pObj;
1274 int nPreCount = pPreTextObj->CountItems();
1275 int nCurCount = pCurTextObj->CountItems();
1276 if (nPreCount != nCurCount) {
1279 FX_BOOL bSame = TRUE;
1280 for (int i = 0; i < nPreCount; i++) {
1281 CPDF_TextObjectItem itemPer, itemCur;
1282 pPreTextObj->GetItemInfo(i, &itemPer);
1283 pCurTextObj->GetItemInfo(i, &itemCur);
1284 if (itemCur.m_CharCode != itemPer.m_CharCode) {
1287 if (itemCur.m_OriginX != itemPer.m_OriginX) {
1290 if (itemCur.m_OriginY != itemPer.m_OriginY) {
1294 if(rcPreObj.left == rcCurObj.left && rcPreObj.top == rcCurObj.top) {
1297 if(FXSYS_fabs(rcPreObj.left - rcCurObj.left) < rcPreObj.Width() / 3
1298 && FXSYS_fabs(rcPreObj.top - rcCurObj.top) < rcPreObj.Height() / 3) {
1304 FX_BOOL CPDF_LayoutProcessor_Reflow::IsSameTextObject(CPDF_TextObject* pTextObj1, CPDF_TextObject* pTextObj2)
1306 if (!pTextObj1 || !pTextObj2) {
1309 CFX_FloatRect rcPreObj(pTextObj2->m_Left, pTextObj2->m_Bottom, pTextObj2->m_Right, pTextObj2->m_Top);
1310 CFX_FloatRect rcCurObj(pTextObj1->m_Left, pTextObj1->m_Bottom, pTextObj1->m_Right, pTextObj1->m_Top);
1311 if (rcPreObj.IsEmpty() && rcCurObj.IsEmpty()) {
1314 if (!rcPreObj.IsEmpty() || !rcCurObj.IsEmpty()) {
1315 rcPreObj.Intersect(rcCurObj);
1316 if (rcPreObj.IsEmpty()) {
1319 if (FXSYS_fabs(rcPreObj.Width() - rcCurObj.Width()) > rcCurObj.Width() / 2) {
1322 if (pTextObj2->GetFontSize() != pTextObj1->GetFontSize()) {
1326 int nPreCount = pTextObj2->CountItems();
1327 int nCurCount = pTextObj1->CountItems();
1328 if (nPreCount != nCurCount) {
1331 for (int i = 0; i < nPreCount; i++) {
1332 CPDF_TextObjectItem itemPer, itemCur;
1333 pTextObj2->GetItemInfo(i, &itemPer);
1334 pTextObj1->GetItemInfo(i, &itemCur);
1335 if (itemCur.m_CharCode != itemPer.m_CharCode) {
1341 void CPDF_LayoutProcessor_Reflow::ProcessTextObject(CPDF_TextObject *pTextObj, FX_FLOAT reflowWidth, CFX_AffineMatrix objMatrix)
1343 if(reflowWidth < 0 || !m_pCurrLine || !m_pTempLine) {
1346 if(IsSameTextObject(pTextObj, m_pPreObj)) {
1349 CPDF_PageObject* pPreObj = m_pPreObj;
1350 FX_INT32 logic = ProcessInsertObject(pTextObj, objMatrix);
1351 m_pPreObj = pTextObj;
1352 m_perMatrix.Copy(objMatrix);
1353 int size = m_pTempLine->GetSize();
1354 int curs = m_pCurrLine->GetSize();
1355 CreateRFData(pTextObj);
1356 size = m_pTempLine->GetSize();
1357 int reds = m_pReflowedPage->m_pReflowed->GetSize();
1362 m_fCurrLineWidth += pTextObj->GetBBox(&objMatrix).Height() / 3;
1363 } else if(logic == 3 && curs) {
1364 m_fCurrLineWidth -= (*m_pCurrLine)[curs - 1]->m_Width;
1365 m_pCurrLine->Delete(curs - 1);
1367 int beginPos = 0, endPos = m_pTempLine->GetSize() - 1;
1368 while(beginPos <= endPos) {
1369 int tempBeginPos = beginPos;
1370 int tempEndPos = endPos;
1371 FX_FLOAT all_width = GetDatasWidth( beginPos, endPos);
1372 if(all_width < reflowWidth - m_fCurrLineWidth) {
1373 CRF_CharData* pBeginData = (CRF_CharData*)(*m_pTempLine)[beginPos];
1374 CFX_AffineMatrix matrix(1, 0, 0, 1, -pBeginData->m_PosX + m_fCurrLineWidth, -pBeginData->m_PosY);
1375 Transform(&matrix, m_pTempLine, beginPos, endPos - beginPos + 1);
1376 AddTemp2CurrLine(beginPos, endPos - beginPos + 1);
1377 m_pTempLine->RemoveAll();
1381 if(tempBeginPos >= tempEndPos && tempEndPos != 0) {
1382 midPos = tempEndPos;
1384 while (tempBeginPos < tempEndPos ) {
1385 midPos = (tempEndPos - tempBeginPos) / 2 + tempBeginPos;
1386 if(midPos == tempBeginPos || midPos == tempEndPos) {
1389 FX_FLOAT w = GetDatasWidth( beginPos, midPos);
1390 if(w < reflowWidth - m_fCurrLineWidth) {
1391 tempBeginPos = midPos;
1393 tempEndPos = midPos;
1396 midPos = tempBeginPos;
1398 FX_FLOAT w = GetDatasWidth( beginPos, 1);
1399 if(w > reflowWidth - m_fCurrLineWidth) {
1405 int count = m_pCurrLine->GetSize();
1412 for(i = midPos; i >= beginPos; i--) {
1413 CRF_CharData* pData = (CRF_CharData*)(*m_pTempLine)[i];
1414 CFX_WideString Wstr = pData->m_pCharState->m_pFont->UnicodeFromCharCode(pData->m_CharCode);
1415 FX_WCHAR cha = Wstr.GetAt(0);
1416 if(i < m_pTempLine->GetSize() - 1) {
1417 CRF_CharData* pNextData = (CRF_CharData*)(*m_pTempLine)[i + 1];
1418 if(pNextData->m_PosX - (pData->m_PosX + pData->m_Width) >= pData->m_Height / 4) {
1424 if(IsCanBreakAfter((FX_DWORD)cha)) {
1427 } else if(IsCanBreakBefore((FX_DWORD)cha)) {
1435 CRF_CharData* pBeginData = (CRF_CharData*)(*m_pTempLine)[beginPos];
1436 CFX_AffineMatrix matrix(1, 0, 0, 1, -pBeginData->m_PosX + m_fCurrLineWidth, -pBeginData->m_PosY);
1437 Transform(&matrix, m_pTempLine, beginPos, f - beginPos + 1);
1438 CRF_Data* pData = (*m_pTempLine)[0];
1439 AddTemp2CurrLine(beginPos, f - beginPos + 1);
1446 if(f == -1 && i < beginPos) {
1447 if( m_pCurrLine->GetSize()) {
1448 int count = m_pCurrLine->GetSize();
1450 for(int i = count - 1; i >= 0; i--) {
1451 CRF_Data* pData = (*m_pCurrLine)[i];
1452 if(pData->GetType() != CRF_Data::Text) {
1455 CRF_CharData* pCharData = (CRF_CharData*)pData;
1456 CFX_WideString Wstr = pCharData->m_pCharState->m_pFont->UnicodeFromCharCode(pCharData->m_CharCode);
1457 FX_WCHAR cha = Wstr.GetAt(0);
1458 if(IsCanBreakAfter(cha)) {
1461 } else if(IsCanBreakBefore(cha)) {
1471 int reflowdCount = m_pReflowedPage->m_pReflowed->GetSize();
1472 int pos = reflowdCount + f - count;
1473 CRF_CharData* pData = (CRF_CharData*)(*m_pReflowedPage->m_pReflowed)[pos];
1474 CFX_AffineMatrix matrix(1, 0, 0, 1, -pData->m_PosX + m_fCurrLineWidth, -pData->m_PosY);
1475 Transform(&matrix, m_pReflowedPage->m_pReflowed, pos, reflowdCount - pos);
1476 for(int j = pos; j < reflowdCount; j++) {
1477 AddData2CurrLine((*m_pReflowedPage->m_pReflowed)[j]);
1479 m_pReflowedPage->m_pReflowed->Delete(pos, count - f);
1481 m_fCurrLineWidth += pTextObj->GetBBox(&objMatrix).Height() / 3;
1489 CRF_CharData* pData = (CRF_CharData*)(*m_pTempLine)[beginPos];
1490 CFX_AffineMatrix matrix(1, 0, 0, 1, -pData->m_PosX + m_fCurrLineWidth, -pData->m_PosY);
1491 if(beginPos == midPos) {
1492 Transform(&matrix, pData);
1494 pData->m_pCharState->m_pFont->GetFontBBox(rect);
1495 FX_FLOAT* pmatrix = pTextObj->m_TextState.GetMatrix();
1496 CFX_AffineMatrix textMatrix(pmatrix[0], pmatrix[1], pmatrix[2], pmatrix[3], 0, 0);
1497 FX_FLOAT width = pData->m_Height * (rect.right - rect.left) / 1000;
1498 FX_FLOAT f = (reflowWidth - m_fCurrLineWidth) / width;
1500 pData->m_Width *= f;
1501 pData->m_Height *= f;
1502 pData->m_pCharState = GetCharState(pData->m_pCharState->m_pTextObj, pData->m_pCharState->m_pFont, pData->m_Height, pData->m_pCharState->m_Color);
1503 AddData2CurrLine(pData);
1505 for(int m = beginPos; m <= midPos; m++) {
1506 CRF_CharData* pData = (CRF_CharData*)(*m_pTempLine)[m];
1507 Transform(&matrix, pData);
1508 AddData2CurrLine(pData);
1512 beginPos = midPos + 1;
1516 m_pTempLine->RemoveAll();
1519 void CPDF_LayoutProcessor_Reflow::ProcessUnitaryObjs(CPDF_PageObjects *pObjs, FX_FLOAT reflowWidth, CFX_AffineMatrix objMatrix)
1524 CFX_FloatRect ObjBBox = pObjs->CalcBoundingBox();
1525 objMatrix.TransformRect(ObjBBox);
1526 FX_FLOAT ObjWidth = ObjBBox.Width();
1527 FX_FLOAT ObjHeight = ObjBBox.Height();
1528 CFX_AffineMatrix matrix;
1529 if(ObjWidth <= reflowWidth - m_fCurrLineWidth) {
1530 matrix.Set(1, 0, 0, 1, m_fCurrLineWidth , 0);
1531 } else if(ObjWidth <= reflowWidth) {
1533 matrix.Set(1, 0, 0, 1, 0, 0);
1536 FX_FLOAT f = reflowWidth / ObjWidth ;
1537 matrix.Set(f, 0, 0, f, 0, 0);
1539 CFX_AffineMatrix tempMatrix = matrix;
1540 matrix.Concat(objMatrix);
1541 FX_POSITION pos = pObjs->GetFirstObjectPosition();
1543 CPDF_PageObject* pObj = pObjs->GetNextObject(pos);
1544 if(pObj->m_Type == PDFPAGE_TEXT) {
1545 FX_INT32 ret = LogicPreObj((CPDF_TextObject*)pObj);
1546 if(ret == 1 || ret == 2) {
1550 CreateRFData(pObj, &matrix);
1553 Transform(&tempMatrix, m_pTempLine, 0, m_pTempLine->GetSize());
1554 AddTemp2CurrLine(0, m_pTempLine->GetSize());
1555 m_pTempLine->RemoveAll();
1558 void CPDF_LayoutProcessor_Reflow::ProcessPathObject(CPDF_PathObject *pObj, FX_FLOAT reflowWidth)