Skip to content

Library functions#

The method names are exactly the sames as the Go names, with a few changes:

  1. The first letter is always a lowercase letter: The method FormatParagraph is called formatParagraph in JavaScript.
  2. When the method name (or a variable name) starts with an upper case word such as PDF, it is now lowercase. Example: the backend document's variable PDFWriter can be accessed via pdfWriter (notice the uppercase 'W').

baseline-pdf#

The library is loaded with require("bag:baseline").

JS library function Go equivalent Info
new(filename) pdf.NewPDFWriter(w) The argument to new() is converted to a io.Writer internally.
string(string) pdf.String() The argument is converted to a PDF string.
serialize(arg) pdf.Serialize(arg) Creates a string representation of the argument which is suitable to write in the PDF file. For examples arrays are converted to [....].

backend document#

The library is loaded with require("bag:backend/document").

JS library function Go equivalent Info
document(filename) document.NewDocument(w) The argument to document() is converted to a io.Writer internally.

Example code:

const bag = require("bag:backend/bag");
const document = require("bag:backend/document");

const doc = new document.document("out.pdf");

const p1 = doc.newPage();
p1.width = bag.mustSP("21cm");
p1.height = bag.mustSP("29.7cm");
p1.shipout() ;


doc.finish();

backend bag#

The library is loaded with require("bag:backend/bag")

JS library function Go equivalent Info
mustSP(arg) MustSP(arg)
sp(arg) SP
factor Factor
scaledPointFromFloat() ScaledPointFromFloat()
max() Max()
min() Min()
maxSP() MaxSP()
multiplyFloat() MultiplyFloat()

backend color#

The library is loaded with require("bag:backend/color")

To initialize a new color use new:

const mycolor = new color.color()
mycolor.space = color.colorRGB
mycolor.r = 0.03
mycolor.g = 0.37
mycolor.b = 1
or use a constructor like

const mycolor = new color.color({ space: color.colorRGB, r: 0.03, g:0.37, b: 1 })
JS library constants Go equivalent Info
colorNone ColorNone
colorRGB ColorRGB
colorCMYK ColorCMYK
colorGray ColorGray
colorSpotcolor ColorSpotcolor

See the color section in the manual for a list of methods.

backend font#

The library is loaded with require("bag:backend/font")

JS library function Go equivalent Info
newFont(face,size) NewFont() Create an initialized Font instance
new Font() &Font{} Create a new (empty) Font instance

Load a face and convert it to a font with the document backend library:

const face = doc.loadFace("CrimsonPro-Regular.ttf", 0);
const fnt = font.newFont(face, bag.mustSP("12pt"));

backend image#

The library is loaded with require("bag:backend/image")

JS library function Go equivalent Info
new image() Image{} Return empty Image struct

Normally the image struct should be created with a call to doc.createImage():

const doc = new document.document("out.pdf");
const img = doc.pdfWriter.loadImageFile("ocean.pdf");
const i = doc.createImage(img,1,"/MediaBox");
imageNode = node.newImage();
imageNode.img = i;
imageNode.width = bag.mustSP("4cm");
imageNode.height = bag.mustSP("4cm");

backend lang#

The library is loaded with require("bag:backend/lang")

JS library function Go equivalent Info
loadPatternFile(filename) LoadPatternFile(filename) Initialize language from hyphenation pattern file.
new lang() Lang{} Return empty Lang struct

backend node#

The library is loaded with require("bag:backend/node")

JS library function Go equivalent Info
boxit() Boxit()
copyList() CopyList()
dimensions() Dimensions()
hpack() Hpack()
hpackTo() HpackTo()
hpackToWithEnd() HpackToWithEnd()
insertAfter() InsertAfter()
insertBefore() InsertBefore()
newDisc() NewDisc()
newGlue() NewGlue()
newGlyph() NewGlyph()
newHList() NewHList()
newImage() NewImage()
newKern() NewKern()
newPenalty() NewPenalty()
newRule() NewRule()
newStartStop() NewStartStop()
newVList() NewVList()
tail() Tail()
vpack() Vpack()
debug() Debug()

frontend#

The library is loaded with require("bag:frontend")

JS library function Go equivalent Info
new(filename) New(filename)
getLanguage(name) GetLanguage(name)
new fontSource(args) FontSource{} Return a FontSource struct
new fe.fontSource( {location: "FiraSans-Regular.otf", fontFeatures: ["smcp"] })
JS library constants Go equivalent Info
fontStyleNormal FontStyleNormal
fontStyleItalic FontStyleItalic
fontStyleOblique FontStyleOblique
fontWeight100 FontWeight100
fontWeight200 FontWeight200
fontWeight300 FontWeight300
fontWeight400 FontWeight400
fontWeight500 FontWeight500
fontWeight600 FontWeight600
fontWeight700 FontWeight700
fontWeight800 FontWeight800
fontWeight900 FontWeight900
settingTextDecorationLine SettingTextDecorationLine
settingBox SettingBox
settingBackgroundColor SettingBackgroundColor
settingBorderBottomWidth SettingBorderBottomWidth
settingBorderLeftWidth SettingBorderLeftWidth
settingBorderRightWidth SettingBorderRightWidth
settingBorderTopWidth SettingBorderTopWidth
settingBorderBottomColor SettingBorderBottomColor
settingBorderLeftColor SettingBorderLeftColor
settingBorderRightColor SettingBorderRightColor
settingBorderTopColor SettingBorderTopColor
settingBorderBottomStyle SettingBorderBottomStyle
settingBorderLeftStyle SettingBorderLeftStyle
settingBorderRightStyle SettingBorderRightStyle
settingBorderTopStyle SettingBorderTopStyle
settingBorderTopLeftRadius SettingBorderTopLeftRadius
settingBorderTopRightRadius SettingBorderTopRightRadius
settingBorderBottomLeftRadius SettingBorderBottomLeftRadius
settingBorderBottomRightRadius SettingBorderBottomRightRadius
settingColor SettingColor
settingDebug SettingDebug
settingFontExpansion SettingFontExpansion
settingFontFamily SettingFontFamily
settingFontWeight SettingFontWeight
settingHAlign SettingHAlign
settingHangingPunctuation SettingHangingPunctuation
settingHeight SettingHeight
settingHyperlink SettingHyperlink
settingIndentLeft SettingIndentLeft
settingIndentLeftRows SettingIndentLeftRows
settingLeading SettingLeading
settingMarginBottom SettingMarginBottom
settingMarginLeft SettingMarginLeft
settingMarginRight SettingMarginRight
settingMarginTop SettingMarginTop
settingOpenTypeFeature SettingOpenTypeFeature
settingPaddingBottom SettingPaddingBottom
settingPaddingLeft SettingPaddingLeft
settingPaddingRight SettingPaddingRight
settingPaddingTop SettingPaddingTop
settingPrepend SettingPrepend
settingPreserveWhitespace SettingPreserveWhitespace
settingSize SettingSize
settingStyle SettingStyle
settingTabSizeSpaces SettingTabSizeSpaces
settingTabSize SettingTabSize
settingTextDecorationLine SettingTextDecorationLine
settingWidth SettingWidth
settingVAlign SettingVAlign
settingYOffset SettingYOffset

harfbuzz#

The library is loaded with require("bag:harfbuzz")

JS library function Go equivalent Info
newBuffer() NewBuffer()
features(array) Convert the array of strings to harfbuzz features

XML/XSLT#

The library is loaded with require("xml")

JS library function Go equivalent Info
newParser() NewParser()
xpath = require("xpath")
const mf = xpath.newParser("myfile.xml");
const seq = mf.evaluate("/log/*");

for (const itm of seq) {
    console.log(itm);
}

See pkg.go.dev/github.com/speedata/goxpath and pkg.go.dev/github.com/speedata/goxml for documentation hints.