FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TextRenderer.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\View;
10 
11 
14 
15 if (! defined('ENT_SUBSTITUTE')) {
16  define('ENT_SUBSTITUTE', 0);
17 }
18 
20 {
24  private $highlightRenderer;
25 
26  public function __construct(HighlightRenderer $highlightRenderer)
27  {
28  $this->highlightRenderer = $highlightRenderer;
29  }
30 
37  public function renderText(TextFragment $fragment, $splitPositions = array(), $insertBacklink = false)
38  {
39  $state = new HighlightState($this->highlightRenderer, $insertBacklink);
40  $result = $this->render($fragment, $state, new PagedTextResult($fragment->getStartOffset()), $splitPositions);
41  return $result->getText();
42  }
43 
49  public function renderHex(TextFragment $fragment, $splitPositions = array())
50  {
51  $state = new HighlightState($this->highlightRenderer);
52  $result = $this->render($fragment, $state, new PagedHexResult($fragment->getStartOffset(), $state), $splitPositions);
53  return $result->getText();
54  }
55 
63  public function render(TextFragment $fragment, HighlightState $state, PagedResult $result, $splitPositions = array())
64  {
65  foreach ($splitPositions as $actionPosition => $entries) {
66  $isBeforeVisibleRange = $actionPosition < $fragment->getStartOffset();
67  $isAfterVisibleRange = $actionPosition >= $fragment->getEndOffset();
68  if ($isBeforeVisibleRange || $isAfterVisibleRange) {
69  $this->processEntriesOutsideVisibleRange($fragment, $state, $result, $entries, $isAfterVisibleRange);
70  } else {
71  $this->processEntriesWithinVisibleRange($fragment, $state, $result, $actionPosition, $entries);
72  }
73  }
74  $this->finalizeContentText($fragment, $state, $result);
75 
76  return $result;
77  }
78 
86  protected function processEntriesOutsideVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $entries, $isAfterVisibleRange)
87  {
88  if ($isAfterVisibleRange) {
89  $this->finalizeContentText($fragment, $state, $result);
90  }
91  $state->processSplitEntries($entries, $result);
92  }
93 
101  protected function processEntriesWithinVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $actionPosition, $entries)
102  {
103  if ($result->isEmpty()) {
104  $state->openExistingElements($result);
105  }
106  $result->appendContentText($fragment->getSlice($result->getCurrentOffset(), $actionPosition));
107  $state->insertElements($entries, $result);
108  assert($result->getCurrentOffset() == $actionPosition);
109  }
110 
116  protected function finalizeContentText(TextFragment $fragment, HighlightState $state, PagedResult $result)
117  {
118  if ($result->getCurrentOffset() < $fragment->getEndOffset()) {
119  if ($result->isEmpty()) {
120  $state->openExistingElements($result);
121  }
122  $result->appendContentText($fragment->getSlice($result->getCurrentOffset()));
123  $state->closeOpenElements($result);
124  }
125  }
126 }
openExistingElements(PagedResult $result)
closeOpenElements(PagedResult $result)
insertElements($entries, PagedResult $result)
render(TextFragment $fragment, HighlightState $state, PagedResult $result, $splitPositions=array())
finalizeContentText(TextFragment $fragment, HighlightState $state, PagedResult $result)
renderHex(TextFragment $fragment, $splitPositions=array())
renderText(TextFragment $fragment, $splitPositions=array(), $insertBacklink=false)
processEntriesWithinVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $actionPosition, $entries)
processEntriesOutsideVisibleRange(TextFragment $fragment, HighlightState $state, PagedResult $result, $entries, $isAfterVisibleRange)