Formats
Write a PDF, encode a QR code, turn Markdown into HTML, read XML, encode and decode Protocol Buffers.
Overview
Five packages to produce and read common formats. They run inside your program, with no online service.
The packages
pdf v0.3.1
Writes PDF 1.4 files: several pages, automatic line wrapping, embedded Liberation Sans font, lines, rectangles and JPEG images. The document comes out as bytes, ready to serve over HTTP or attach to an email.
Limits: one page size per document, RGB colors only.
Note: with amc 0.8.92, amc package add pdf prints a facade compile error. amc build then recompiles it: the example below builds.
- Install:
amc package add pdf - Source code: amalgame-lang/amalgame-pdf
qrcode v0.1.2
Encodes text as a QR Code, with no online service: byte mode, error correction level M, versions 1 to 10 (up to 213 bytes). It returns the module grid; you draw it, as SVG or in a PDF.
- Install:
amc package add qrcode - Source code: amalgame-lang/amalgame-qrcode
markdown v0.1.0
Turns Markdown into HTML: headings, code blocks, tables, lists, blockquotes, bold, italic and links. All text is escaped, and javascript:, data: and vbscript: links are replaced with #.
Limits: no images, no raw HTML, no footnotes.
- Install:
amc package add markdown - Source code: amalgame-lang/amalgame-markdown
formats-xml v0.1.0
Reads an XML document into a tree. It resolves namespaces: you look up an element by namespace and local name, whatever prefix the sender chose. Useful for WebDAV, CalDAV or SOAP.
Limits: read-only, it does not write XML.
- Install:
amc package add formats-xml - Source code: amalgame-lang/amalgame-formats-xml
formats-protobuf v0.2.4
Encodes and decodes the Protocol Buffers (proto3) binary format on a list of bytes, null bytes included. A generator (Node.js script) creates message classes from a .proto file.
- Install:
amc package add formats-protobuf - Source code: amalgame-lang/amalgame-formats-protobuf
Example: a PDF ticket with a QR code
This program uses pdf and qrcode. It compiles with amc 0.8.92 after amc package add pdf and amc package add qrcode.
namespace Demo
import Amalgame.Formats.Pdf
import Amalgame.QrCode
public class Program {
public static int Main(List<string> args) {
let p: Pdf = Pdf.A4Portrait()
p.TextBold(40, 60, 18, "Ticket #42")
p.Text(40, 90, 11, "Show this code at the door.")
// One black 4-point square per dark module
let qr: QrResult = QrCode.Encode("https://example.com/ticket/42")
if (qr.Ok) {
for y in 0..qr.Size {
for x in 0..qr.Size {
if (qr.Modules.Get(y * qr.Size + x)) {
p.FillRect(40 + x * 4, 120 + y * 4, 4, 4, 0x000000)
}
}
}
}
let bytes: List<int> = p.Build() // the PDF, as bytes
Console.WriteLine(String_FromInt(bytes.Count()) + " bytes")
return 0
}
}
See also
Two more formats have their own page: yaml, and msgpack, which is not in the registry yet.
At a glance
- License: Apache-2.0 for all five packages; the font embedded by pdf is under the SIL Open Font License 1.1
- Versions and installation checked with amc 0.8.92
- Official index: amalgame-lang/packages-index