Last active
May 23, 2018 10:44
-
-
Save boogie666/0bbf7f92f9104ec24bfac26febc0dceb to your computer and use it in GitHub Desktop.
Example Transducer for tim.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example of get the position of the cursor in a line of a text buffer from a click event | |
function clickPositionToCharIndex(clickX){ | |
return comp( | |
map(measureCharWidth), | |
reductions((a,b) => a + b, 0), | |
takeWhile(x => x < clickX), | |
mapIndexed((idx, item) => idx) | |
); | |
} | |
//index of all the characters up to the click coord (i.e. x = 100) | |
const positions = transduce(clickPositionToCharIndex(100), array_push, [], textBuffer); | |
// the last one is the click position | |
const cursorPosition = positions[position.length - 1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// long winded example of how we make pdfs from svgs | |
function svgToPDFPipeline(host){ | |
return comp( | |
map(tryUrlDecode), | |
map(str => str.replace(" ", " ")), | |
map(toByteStream), | |
map(toXMLResource), | |
map(removeNoScriptTag), | |
map(processTextAnchor), | |
map(processFillProperty), | |
map(processStyle), | |
map(processedFilteredText), | |
map(partial(processPlaceHolderImage, host)), | |
map(addXMLNS), | |
map(svg => svgToString(svg)) | |
); | |
} | |
const pdfGenerationProcess = comp( | |
svgToPDFPipeline("www.somehost.com"), | |
map(toByteStream), | |
map(inputStream => new TranscoderInput(inputStream)), | |
timing // this is just a benchmark thing | |
); | |
const pdfGen = pdfGenerator(fontConfig, outputStream); | |
transduce(pdfGenerationProcess, pdfGen, arrayOfSvgs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment