Skip to content

Commit

Permalink
Merge pull request #30 from soprodecarnaval/feature/indice-de-carnaval
Browse files Browse the repository at this point in the history
Feature/indice de carnaval
  • Loading branch information
gabrielmcf authored Dec 29, 2023
2 parents d4ecc1b + 6d23cdd commit bb47dc5
Showing 1 changed file with 125 additions and 27 deletions.
152 changes: 125 additions & 27 deletions src/tsx/PdfGenerator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, ButtonGroup, Col, Dropdown, Form, Row } from "react-bootstrap";
import { Button, ButtonGroup, Col, Dropdown, Form, Row, OverlayTrigger, Tooltip, ListGroup } from "react-bootstrap";
import SVGtoPDF from "svg-to-pdfkit";
import { useState } from "react";
import { Instrument, SongArrangement } from "../types";
import { Instrument, Song, SongArrangement } from "../types";

Check failure on line 4 in src/tsx/PdfGenerator.tsx

View workflow job for this annotation

GitHub Actions / deploy

'Song' is declared but its value is never read.

// Needed for calling PDFDocument from window variable
declare const window: any;
Expand All @@ -12,6 +12,44 @@ const cm2pt = 28.3465;
const pageWidth = 18 * cm2pt;
const pageHeight = 13 * cm2pt;

const stylesOrder = [
'marcha rancho', // 7
'marchinhas', // 14
'odaras', // 7
'beagá', // 9
'fanfarras', // 8
'latinas', // 5
'pagodes', // 6
'frevos', // 4

'axés', // 15
'brazukas', // 11
'funks', // 14
'sambas', // 13
'forrós', // 4
'technohell' // 3
]

const sortStyles = (a : string,b : string) => {
if(stylesOrder.indexOf(a) < stylesOrder.indexOf(b)){
return -1
} else if (stylesOrder.indexOf(a) > stylesOrder.indexOf(b)){
return 1
} else {
return 0
}
}

const sortSongs = (a : SongArrangement,b : SongArrangement) => {
if(a.song.title < b.song.title){
return -1
} else if (a.song.title > b.song.title){
return 1
} else {
return 0
}
}

let stylesOutlines = new Map();

interface PdfGeneratorProps {
Expand Down Expand Up @@ -58,7 +96,7 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
return fetch(url)
.then((r) => r.text())
.then((svg) => {
let pdfPage = backNumber ? 2 * page + 2 : page + 1;
let pdfPage = carnivalMode ? 2 * page + 5 : page + 1;
doc.switchToPage(pdfPage);
const width = 17.17 * cm2pt;
const height = 9.82 * cm2pt;
Expand Down Expand Up @@ -186,8 +224,8 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
const styles = new Set(songArrangements.map(({ song }) => song.style));
const stylesCount = styles.size;
const containerWidth = 17.17 * cm2pt;
const containerHeight = 9.82 * cm2pt;
const totalLineCount = songsCount + stylesCount * 2;
const containerHeight = 11 * cm2pt;
let totalLineCount = carnivalMode ? 76 : songsCount + stylesCount * 2;

let columnCount = 1;
if (totalLineCount > 80) {
Expand All @@ -205,11 +243,18 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
);
const columnWidth = Math.floor(containerWidth / columnCount);

let cursorStartPosition = [0.44 * cm2pt, 2.55 * cm2pt];
let cursorStartPosition = [0.44 * cm2pt, 1.55 * cm2pt];
let currentColumn = 0;
let currentLine = 0;
let itemCount = 0;
let songCount = 0;
let firstPage = true;
const resetCursorPosition = () => {
cursorStartPosition = [0.44 * cm2pt, 1.55 * cm2pt];
currentColumn = 0;
currentLine = 0;
itemCount = 0;
}
const nextCursorPosition = () => {
if (itemCount == 0) {
itemCount++;
Expand All @@ -226,30 +271,46 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
let [currentX, currentY] = nextCursorPosition();
let reorderedSongs: SongArrangement[] = [];
doc
.addPage()
.fontSize(25)
.font("Helvetica-Bold")
.text("ÍNDICE", currentX + 0.3 * cm2pt, 1.2 * cm2pt);
[...styles].sort().forEach((style) => {
songArrangements
.addPage();
// .fontSize(25)
// .font("Helvetica-Bold")
// .text("ÍNDICE", currentX + 0.3 * cm2pt, 1.2 * cm2pt);
let sortStyleFunc = carnivalMode ? sortStyles : undefined;
[...styles].sort(sortStyleFunc).forEach((style, styleIdx) => {
let filteredSongs = songArrangements
.filter(({ song }) => song.style == style)
.forEach((songArrangement, i) => {

if(carnivalMode){
if(firstPage && songCount + (styleIdx+1)*2 + filteredSongs.length + 2 > totalLineCount){
firstPage = false
resetCursorPosition();
[currentX, currentY] = nextCursorPosition();
doc.addPage()
}
}

filteredSongs
.sort(sortSongs)
.forEach((songArrangement, songIdx) => {
reorderedSongs.push(songArrangement);
if (i == 0) {
if (songIdx == 0) {
if (currentLine == maxLinesPerColumn)
[currentX, currentY] = nextCursorPosition();
doc
.font("Helvetica-Bold")
.fontSize(fontSize - 2)
.text(`${style.toUpperCase()}`, currentX + 0.3 * cm2pt, currentY);
.text(`${style.toUpperCase()}`, currentX + 0.3 * cm2pt, currentY); // Título do estílo
[currentX, currentY] = nextCursorPosition();
}
doc
.font("Helvetica-Bold")
.fontSize(fontSize - 2)
.text(1 + songCount++, currentX - 0.3 * cm2pt, currentY, {
.text(1 + songCount++,
currentX - 0.5 * cm2pt,
currentY,
{
align: "right",
width: 0.5 * cm2pt,
width: 0.6 * cm2pt,
goTo: songCount,
}) // Número da página
.font("Helvetica")
Expand All @@ -259,6 +320,9 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
currentY,
{
goTo: songCount,
width: columnWidth - 0.3*cm2pt,
height: fontSize,
lineBreak: false
}
);
[currentX, currentY] = nextCursorPosition();
Expand All @@ -273,6 +337,21 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
doc.fontSize(25).text(songbookTitle.toUpperCase(), 120, 100);
doc.fontSize(22).text(instrument.toUpperCase(), 120, 125);
if (backNumber) doc.addPage();
if (carnivalMode) {
doc.addPage()
.fontSize(16)
.text(`Mussum Ipsum, cacilds vidis litro abertis. Todo mundo vê os porris que eu tomo, mas ninguém vê os tombis que eu levo! Pra lá, depois divoltis porris, paradis. Viva Forevis aptent taciti sociosqu ad litora torquent. Pellentesque nec nulla ligula. Donec gravida turpis a vulputate ultricies.
Detraxit consequat et quo num tendi nada. Posuere libero varius. Nullam a nisl ut ante blandit hendrerit. Aenean sit amet nisi. Per aumento de cachacis, eu reclamis. Interagi no mé, cursus quis, vehicula ac nisi.
Mauris nec dolor in eros commodo tempor. Aenean aliquam molestie leo, vitae iaculis nisl. Vehicula non. Ut sed ex eros. Vivamus sit amet nibh non tellus tristique interdum. Nulla id gravida magna, ut semper sapien. A ordem dos tratores não altera o pão duris.`,
1 * cm2pt,
3 * cm2pt,
{
width: 16 * cm2pt,
align: 'center'
})
.addPage();

}
let reorderedSongs = addIndexPage(doc);
let styles = new Set(songArrangements.map(({ song }) => song.style));
const { outline } = doc;
Expand Down Expand Up @@ -316,9 +395,25 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
const [songbookTitle, setTitle] = useState("");
const onInput = ({ target: { value } }: any) => setTitle(value);

const [carnivalMode, setCarnivalMode] = useState(false)
const onCheckCarnivalMode = ({ target: { checked } }: any) => {
setBackNumber(checked)
setCarnivalMode(checked)
}

const carnivalModeTooltip = (
<Tooltip id="tooltip">
<ListGroup>
<ListGroup.Item>Númeração no verso de cada música</ListGroup.Item>
<ListGroup.Item>Índice com duas páginas</ListGroup.Item>
<ListGroup.Item>Mensagem anti assédio no início</ListGroup.Item>
</ListGroup>
</Tooltip>
);

const [backNumber, setBackNumber] = useState(false);
const onCheckBackNumber = ({ target: { checked } }: any) =>
setBackNumber(checked);
// const onCheckBackNumber = ({ target: { checked } }: any) =>
// setBackNumber(checked);

return (
<Row className="mt-4">
Expand Down Expand Up @@ -349,14 +444,17 @@ const PDFGenerator = ({ songArrangements }: PdfGeneratorProps) => {
</Dropdown.Menu>
</Dropdown>
</Col>
<Col sm={4}>
<Form.Check
type="switch"
id="back-number"
label="Número no verso"
onChange={onCheckBackNumber}
/>
</Col>
<OverlayTrigger placement="left" overlay={carnivalModeTooltip}>
<Col sm={4}>
<Form.Check
type="switch"
id="back-number"
label="Modo carnaval"
onChange={onCheckCarnivalMode}
/>

</Col>
</OverlayTrigger>
</Form>
</Row>
);
Expand Down

0 comments on commit bb47dc5

Please sign in to comment.