<commit_before>package visualizer;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import de.freiburg.iif.model.HasRectangle;
import de.freiburg.iif.model.Rectangle;
import drawer.PdfDrawer;
import drawer.pdfbox.PdfBoxDrawer;
import model.PdfArea;
import model.PdfDocument;
import model.PdfElement;
import model.PdfFeature;
import model.PdfPage;

/**
 * The default implmentation of a PdfVisualizer.
 *
 * @author <NAME>
 *
 */
public class PlainPdfVisualizer implements PdfVisualizer {

  @Override
  public void visualize(PdfDocument document, OutputStream stream)
    throws IOException {
    visualize(document, null, stream);
  }

  @Override
  public void visualize(PdfDocument document, List<PdfFeature> features,
      OutputStream stream) throws IOException {
    PdfDrawer drawer = new PdfBoxDrawer(document.getPdfFile());

    // If the list of features is empty, take all features.
    if (features == null || features.isEmpty()) {
      features = Arrays.asList(PdfFeature.values());
    }

    for (PdfPage page : document.getPages()) {
      visualizePage(page, features, drawer);
    }

    drawer.writeTo(stream);
  }

  // ___________________________________________________________________________

  /**
   * Visualizes the given features of the given document using the given drawer.
   */
  protected void visualizePage(PdfPage page, List<PdfFeature> features,
      PdfDrawer drawer) throws IOException {

    if (features == null) {
      return;
    }

    for (PdfFeature feature : features) {
      visualizeFeature(page, feature, drawer);
    }

//    if (page.getRects() != null) {
//      for (Rectangle rect : page.getRects()) {
//        drawer.drawRectangle(rect, page.getPageNumber(), Color.BLACK);
//      }
//    }
    
    for (PdfArea block : page.getBlocks()) {
//      drawer.drawRectangle(block.getRectangle(), page.getPageNumber(),
//          Color.BLACK);
      if (block.getRects() != null) {
        for (Rectangle rect : block.getRects()) {
          drawer.drawRectangle(rect, page.getPageNumber(), Color.BLUE);
        }
      }      
    }    
  }

  // ___________________________________________________________________________

  /**
   * Visualizes the paragraphs of the given document using the given drawer.
   */
  protected void visualizeFeature(PdfPage page, PdfFeature feature,
      PdfDrawer drawer) throws IOException {
    if (feature == null) {
      return;<commit_msg>The feature 'paragraphs_with_lines' can now be visualized<commit_after>package visualizer;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import de.freiburg.iif.model.HasRectangle;
import de.freiburg.iif.model.Rectangle;
import drawer.PdfDrawer;
import drawer.pdfbox.PdfBoxDrawer;
import model.PdfArea;
import model.PdfDocument;
import model.PdfElement;
import model.PdfFeature;
import model.PdfPage;
import model.PdfTextLine;
import model.PdfTextParagraph;

/**
 * The default implmentation of a PdfVisualizer.
 *
 * @author <NAME>
 *
 */
public class PlainPdfVisualizer implements PdfVisualizer {

  @Override
  public void visualize(PdfDocument document, OutputStream stream)
    throws IOException {
    visualize(document, null, stream);
  }

  @Override
  public void visualize(PdfDocument document, List<PdfFeature> features,
      OutputStream stream) throws IOException {
    PdfDrawer drawer = new PdfBoxDrawer(document.getPdfFile());

    // If the list of features is empty, take all features.
    if (features == null || features.isEmpty()) {
      features = Arrays.asList(PdfFeature.values());
    }

    for (PdfPage page : document.getPages()) {
      visualizePage(page, features, drawer);
    }

    drawer.writeTo(stream);
  }

  // ___________________________________________________________________________

  /**
   * Visualizes the given features of the given document using the given drawer.
   */
  protected void visualizePage(PdfPage page, List<PdfFeature> features,
      PdfDrawer drawer) throws IOException {

    if (features == null) {
      return;
    }

    for (PdfFeature feature : features) {
      /**
       * This feature was added for David. He needs the paragraphs with the 
       * associated lines. Remove it if it not needed anymore.
       */
      if (feature == PdfFeature.paragraphs_with_lines) {
        visualizeFeature(page, PdfFeature.lines, drawer);
        visualizeFeature(page, PdfFeature.paragraphs, drawer);
        continue;
      }
      
      visualizeFeature(page, feature, drawer);
    }

//    if (page.getRects() != null) {
//      for (Rectangle rect : page.getRects()) {
//        drawer.drawRectangle(rect, page.getPageNumber(), Color.BLACK);
//      }
//    }
    
    for (PdfArea block : page.getBlocks()) {
//      drawer.drawRectangle(block.getRectangle(), page.getPageNumber(),
//          Color.BLACK);
      if (block.getRects() != null) {
        for (Rectangle rect : block.getRects()) {
          drawer.drawRectangle(rect, page.getPageNumber(), Color.BLUE);
        }
      }      
    }    
  }

  // ___________________________________________________________________________

  /**
   * Visualizes the paragraphs of the given document using the given drawer.
   */
  protected void visualizeFeature(PdfPage page, PdfFeature feature,
      PdfDrawer drawer) throws IOException {
    if (feature == null) {
      return;
{'fraction_non_alphanumeric': 0.10388666908826734, 'fraction_numerical': 0.0, 'mean_word_length': 3.275621118012422, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227003', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1561, 'n_words': 398}