Skip to content

Instantly share code, notes, and snippets.

@boogie666
Last active May 23, 2018 10:44
Show Gist options
  • Save boogie666/0bbf7f92f9104ec24bfac26febc0dceb to your computer and use it in GitHub Desktop.
Save boogie666/0bbf7f92f9104ec24bfac26febc0dceb to your computer and use it in GitHub Desktop.
Example Transducer for tim.js
// 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];
// long winded example of how we make pdfs from svgs
function svgToPDFPipeline(host){
return comp(
map(tryUrlDecode),
map(str => str.replace("&nbsp;", " ")),
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