Pinstripe/Printer: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Raku}}: Fix comment: Perl 6 --> Raku)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(9 intermediate revisions by 5 users not shown)
Line 7:
 
Optionally, on systems where the printer resolution cannot be determined, it is permissible to prompt the user for printer resolution, and to calculate point size based on user input, enabling fractional point sizes to be used.
 
=={{header|Ada}}==
{{libheader|APDF}}
This program will create the PDF file 'pinstripe.pdf'.
<syntaxhighlight lang="ada">with Ada.Text_IO;
 
with PDF_Out;
 
procedure Pinstripe_Printer
is
use PDF_Out;
 
package Point_IO
is new Ada.Text_Io.Float_IO (Real);
 
procedure Pinstripe (Doc : in out Pdf_Out_File;
Line_Width : Real;
Line_Height : Real;
Screen_Width : Real;
Y : Real)
is
Count : constant Natural
:= Natural (Real'Floor (Screen_Width / (2.0 * Line_Width)));
Corner : constant Point := (Doc.Left_Margin, Doc.Bottom_Margin);
Corner_Box : constant Point := Corner + (10.0, 10.0);
Corner_Text : constant Point := Corner_Box + (10.0, 10.0);
Light_Gray : constant Color_Type := (0.9, 0.9, 0.9);
Image : String (1 .. 4);
begin
-- Pinstripes
Doc.Color (Black);
for A in 0 .. Count loop
Doc.Draw (What => Corner +
Rectangle'(X_Min => 2.0 * Real (A) * Line_Width,
Y_Min => Y,
Width => Line_Width,
Height => Line_Height),
Rendering => Fill);
end loop;
 
-- Box
Doc.Stroking_Color (Black);
Doc.Color (Light_Gray);
Doc.Line_Width (3.0);
Doc.Draw (What => Corner_Box + (0.0, Y, 120.0, 26.0),
Rendering => Fill_Then_Stroke);
-- Text
Doc.Color (Black);
Doc.Text_Rendering_Mode (Fill);
Point_Io.Put (Image, Line_Width, Aft => 1, Exp => 0);
Doc.Put_XY (Corner_Text.X, Corner_Text.Y + Y,
Image & " point pinstripe");
end Pinstripe;
 
Doc : PDF_Out_File;
begin
Doc.Create ("pinstripe.pdf");
Doc.Page_Setup (A4_Portrait);
Doc.Margins (Margins_Type'(Left => Cm_2_5,
others => One_cm));
declare
Width : constant Real
:= A4_Portrait.Width - Doc.Left_Margin - Doc.Right_Margin;
Height : constant Real
:= A4_Portrait.Height - Doc.Top_Margin - Doc.Bottom_Margin;
begin
Pinstripe (Doc, 1.0, One_Inch, Width, Height - 1.0 * One_Inch);
Pinstripe (Doc, 2.0, One_Inch, Width, Height - 2.0 * One_Inch);
Pinstripe (Doc, 3.0, One_Inch, Width, Height - 3.0 * One_Inch);
Pinstripe (Doc, 4.0, One_inch, Width, Height - 4.0 * One_Inch);
Pinstripe (Doc, 5.0, One_Inch, Width, Height - 5.0 * One_Inch);
Pinstripe (Doc, 6.0, One_Inch, Width, Height - 6.0 * One_Inch);
Pinstripe (Doc, 7.0, One_Inch, Width, Height - 7.0 * One_Inch);
Pinstripe (Doc, 8.0, One_Inch, Width, Height - 8.0 * One_Inch);
Pinstripe (Doc, 9.0, One_Inch, Width, Height - 9.0 * One_Inch);
Pinstripe (Doc, 10.0, One_Inch, Width, Height - 10.0 * One_Inch);
Pinstripe (Doc, 11.0, One_Inch, Width, Height - 11.0 * One_Inch);
end;
Doc.Close;
end Pinstripe_Printer;</syntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PD_RETURNDC = 256
_LOGPIXELSY = 90
Line 46 ⟶ 126:
pitch% += 2
NEXT y%
VDU 2,1,12,3</langsyntaxhighlight>
 
=={{header|Go}}==
Line 52 ⟶ 132:
<br>
The code for this task is basically the same as for [[Pinstripe/Display#Go]] except that the drawing parameters have been tweaked to produce 1 inch bands when printing on A4 paper and some code has been added to dump the image to the default printer.
<langsyntaxhighlight lang="go">package main
import (
Line 93 ⟶ 173:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
 
=={{header|Julia}}==
Creates a png file of dimensions per printer DPI and width and sends it to the default printer using the system's print function.
<syntaxhighlight lang="julia">using FileIO, ImageIO
 
function getnumberwithprompt(prompt, t::Type)
s = ""
while (x = tryparse(t, s)) == nothing
print("\n", prompt, ": -> ")
s = strip(readline())
end
return x
end
 
dpi = getnumberwithprompt("Printer DPI (dots per inch)", Int)
pwidth = getnumberwithprompt("Printer width (inches)", Float64)
plength = 10.0
 
imgwidth, imgheight = Int(round(pwidth * dpi)), Int(round(plength * dpi))
 
img = zeros(UInt8, Int(round(imgheight)), Int(round(imgwidth)))
 
for row in 1:imgheight, col in 1:imgwidth
stripewidth = div(row, dpi) + 1
img[row, col] = rem(col, stripewidth * 2) < stripewidth ? 0 : 255
end
 
save("temp.png", img)
run(`print temp.png`) # the run statement may need to be set up for the installed device
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Draws the pattern in a window onto a large graphic box, then dumps to the printer.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 147 ⟶ 258:
close #main
end
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{libheader|gintro}}
 
The code is almost the same as for [[Pinstripe/Display#Nim]] task.
<syntaxhighlight lang="nim">import gintro/[glib, gobject, gtk, gio, cairo]
 
const Colors = [[255.0, 255.0, 255.0], [0.0, 0.0, 0.0]]
 
#---------------------------------------------------------------------------------------------------
 
proc beginPrint(op: PrintOperation; printContext: PrintContext; data: pointer) =
## Process signal "begin_print", that is set the number of pages to print.
op.setNPages(1)
 
#---------------------------------------------------------------------------------------------------
 
proc drawPage(op: PrintOperation; printContext: PrintContext; pageNum: int; data: pointer) =
## Draw a page.
 
let context = printContext.getCairoContext()
let lineHeight = printContext.height / 4
 
var y = 0.0
for lineWidth in [1.0, 2.0, 3.0, 4.0]:
context.setLineWidth(lineWidth)
var x = 0.0
var colorIndex = 0
while x < printContext.width:
context.setSource(Colors[colorIndex])
context.moveTo(x, y)
context.lineTo(x, y + lineHeight)
context.stroke()
colorIndex = 1 - colorIndex
x += lineWidth
y += lineHeight
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
# Launch a print operation.
let op = newPrintOperation()
op.connect("begin_print", beginPrint, pointer(nil))
op.connect("draw_page", drawPage, pointer(nil))
 
# Run the print dialog.
discard op.run(printDialog)
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.Pinstripe")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
=={{header|Phix}}==
Line 154 ⟶ 320:
=={{header|PicoLisp}}==
 
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/ps.l")
 
(call 'lpr
Line 162 ⟶ 328:
(gray (if (bit? 1 X) 0 100)
(vline X 0 842) ) )
(page) ) )</langsyntaxhighlight>
 
=={{header|Racket}}==
The drawing code is exactly the same code as [[Pinstripe/Display#Racket]], only
drawing onto a printer device context now.
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket/gui
 
Line 185 ⟶ 351:
 
(send* dc (end-page) (end-doc))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 192 ⟶ 358:
Note that Raku does not attempt to be a printer driver. This example allows users to specify the dpi and paper size, then generates an image and passes it to the default printer. Defaults to 300 dpi and US letter paper.
 
<syntaxhighlight lang="raku" perl6line>unit sub MAIN ($dpi = 300, $size = 'letter');
 
my $filename = './Pinstripe-printer-perl6.png';
Line 246 ⟶ 412:
 
# Uncomment next line if you actually want to print it
#run('lp', $filename)</langsyntaxhighlight>
 
See [https://github.com/thundergnat/rc/blob/master/img/Pinstripe-printer-perl6.png Pinstripe-printer-perl6.png] (offsite png image)
Line 254 ⟶ 420:
This code assumes that the page's printable area is 8.5"&times;11".
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
# Allocate a temporary drawing surface
canvas .c
Line 267 ⟶ 433:
exec lp - << [.c postscript -height $y -width $x -pageheight $y -pagewidth $x]
# Explicit exit; no GUI desired
exit</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<br>
As DOME doesn't have a method to print an image to a printer, we first need to create a small plug-in in C to add this functionality.
<syntaxhighlight lang="c">/* gcc -O3 -std=c11 -shared -o printer.so -fPIC -I./include printer.c */
 
#include <stdlib.h>
#include <string.h>
#include "dome.h"
 
static DOME_API_v0* core;
static WREN_API_v0* wren;
 
static const char* source = ""
"class Printer {\n"
"foreign static printFile(name) \n"
"} \n";
 
void C_printFile(WrenVM* vm) {
const char *arg = wren->getSlotString(vm, 1);
char command[strlen(arg) + 4];
strcpy(command, "lp ");
strcat(command, arg);
int res = system(command);
}
 
DOME_EXPORT DOME_Result PLUGIN_onInit(DOME_getAPIFunction DOME_getAPI, DOME_Context ctx) {
core = DOME_getAPI(API_DOME, DOME_API_VERSION);
wren = DOME_getAPI(API_WREN, WREN_API_VERSION);
core->registerModule(ctx, "printer", source);
core->registerClass(ctx, "printer", "Printer", NULL, NULL);
core->registerFn(ctx, "printer", "static Printer.printFile(_)", C_printFile);
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_preUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_postUpdate(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_preDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_postDraw(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}
 
DOME_EXPORT DOME_Result PLUGIN_onShutdown(DOME_Context ctx) {
return DOME_RESULT_SUCCESS;
}</syntaxhighlight>
This assumes that the ''dome.h'' header file is copied to an ''include'' sub-directory of the current one and that the resulting ''printer.so'' shared library file is created in the latter.
 
We can now use this plug-in in the following script which draws the image to the canvas, copies it to an ''ImageData'' object, saves it as a ''.png'' file and prints it to the default printer.
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color, ImageData
import "dome" for Window
import "plugin" for Plugin
 
Plugin.load("printer")
 
import "printer" for Printer
 
class Main {
construct new() {
Window.title = "Pinstripe - printer"
_width = 842
_height = 595
Canvas.resize(_width, _height)
Window.resize(_width, _height)
var colors = [
Color.hex("FFFFFF"), // white
Color.hex("000000") // black
]
pinstripe(colors)
}
 
pinstripe(colors) {
var w = _width
var h = (_height/7).floor
for (b in 1..11) {
var x = 0
var ci = 0
while (x < w) {
var y = h * (b - 1)
Canvas.rectfill(x, y, b, h, colors[ci%2])
x = x + b
ci = ci + 1
}
}
}
 
init() {
var img = ImageData.create("pinstripe", _width, _height)
for (x in 0..._width) {
for (y in 0..._height) img.pset(x, y, Canvas.pget(x, y))
}
img.saveToFile("pinstripe.png")
Printer.printFile("pinstripe.png")
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = Main.new()</syntaxhighlight>
 
{{omit from|DWScript}}
9,482

edits