Unit of size using in pdfmake library - pdfmake

what is the unit size used for pageSize in pdfmake :
var docDefinition = {
// a string or { width: number, height: number }
pageSize: {
width: 595.28,
height: 'auto'
},
};

Pdfmake uses points (pt) as a unit of measure (this goes both for page size and margin size).
In the source code of the library we can see that A4 paper is 595.35 x 841.995 pt.
Since A4 is 210 x 297 mm or 8.3 x 11.7 in, we can easily compute the conversion ratio.
1 mm = 2.835 pt
1 in = approx. 71.729 pt
See this discussion for more details.

Related

Draw rects inside Konva layer bound by a region with uniform distribution

I am using Konva.js to render some colourful boxes.
I have a Stage with some set size and regions within that Stage, bound by points (e.g. rectangles, but they can be any polygons).
I then need to add some number of smaller rects inside this area such that they look nice and are uniformly distributed.
E.g.
// these numbers are an example only, irrelevant
let width = 500 * 1.936402653140851;
let height = 500;
// setup stage, boilerplate
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});
// region layer - boundaries
var regionLayer = new Konva.Layer({
scale: {x: width/4963, y: height/2563} // these numbers are an example only, irrelevant
});
stage.add(regionLayer);
// for this example, a single region, L-shaped on it's side
let box = new Konva.Line({
name: 'region-1',
points: [1067, 681,
3337, 681,
3337, 987,
3037, 987,
3037, 787,
1067, 787],
closed: true,
fill: 'blue',
opacity: 0.5
});
regionLayer.add(box);
regionLayer.draw();
// layer to draw rects in
let rectsLayer = new Konva.Layer({
scale: {x: width/4963, y: height/2563} // these numbers are an example only, irrelevant
});
for (let i = 0; i < 90; i++) { // e.g. some number up to X
let rect = new Konva.Rect({
width: 20,
height: 20,
fill: 'red',
x: 1067 + 40 * (i + 1),
y: 681 + 20
});
rectsLayer.add(rect);
}
stage.add(rectsLayer);
rectsLayer.draw();
The result of the above is something like this
What I'd like to have, is those boxes filling free area within the boundaries of my polygon only.
I read a whole heap about Delauney triangulation, Voronoi diagrams and sequencing for normal distribution to make it happen, but sadly I came up with zilch on practical approach of doing this.
https://jsfiddle.net/x4aksz1y/2/

Corona simulator anchor points Y

I can't tell if I'm doing something silly or if there is a bug in Corona simulator. When I write the following code:
rect = display.newRect(0, 0, 100, 100)
rect.anchorX = 0
rect.anchorY = 0
rect.x = 0
rect.y = 0
This just sets the anchor point of a 100x100 square to its top left, and sets the position to 0,0. This should make the square snug in the corner, but instead it produces this. It is always a little too far down in the Y axis, but the X axis functions correctly. Does anyone have a fix for this?
With my Corona simulator (build 2016.2992) code works as expected.
main.lua
---------------------------------------------
local rect = display.newRect( 0, 0, 100, 100)
rect.anchorX = 0
rect.anchorY = 0
First of all check your config.lua file. I think it depends on display resolution. For example, in letterbox mode you may have "black bars" on devices with aspect ratios that differ from your content aspect ratio. Read more on Corona documentation.
Below is code from my config.lua file I'm using
config.lua
---------------------------------------------
--calculate the aspect ratio of the device
local aspectRatio = display.pixelHeight / display.pixelWidth
application = {
content = {
width = aspectRatio >= 1.5 and 800 or math.floor( 1200 / aspectRatio ),
height = aspectRatio <= 1.5 and 1200 or math.floor( 800 * aspectRatio ),
scale = "letterBox",
fps = 30,
imageSuffix = {
["#2x"] = 1.3,
},
},
}

Adobe AIR Stage.contentsScaleFactor always 1 on iPhone4s and iPad mini

My application is just a template AIR Mobile AS3 project from FlashDevelop: application.xml file, and a Main class.
In the Main class, I create a text field with stage.contentsScaleFactor value as a text after the first Event.RESIZE:
var textField:TextField = new TextField();
textField.appendText("Size: " + stage.stageWidth + " x " + stage.stageHeight + "\n");
textField.appendText("Scale: " + stage.contentsScaleFactor + "\n");
addChild(textField);
On my iPhone with retina support, i get
Size: 960 x 640
Scale: 1
for
<requestedDisplayResolution>high</requestedDisplayResolution>
and
Size: 480 x 320
Scale: 1
for
<requestedDisplayResolution>standard</requestedDisplayResolution>.
Almost the same for iPad,
Size: 2048 x 1536
Scale: 1
for high, and
Size: 1024 x 768
Scale: 1
for standard.
I'm compiling with the latest AIR SDK 18.0.0.142 (beta), -swf-version=29.
Same results for release AIR SDK 18.
For AIR 14 SDK and -swf-version=25, I get some garbage values for size (it looks like my swf width and height multiplied by what contentsScaleFactor should be), but still 1 for contentsScaleFactor.
Edit:
I have encountered various questions which mentioned contentsScaleFactor over the web (for example, this one). They claim that contentsScaleFactor should be 2 on Retina display
That is how this property documented:
Specifies the effective pixel scaling factor of the stage. This value
is usually 1 on standard screens and 2 on HiDPI (a.k.a Retina)
screens. When the stage is rendered on HiDPI screens the pixel
resolution is doubled; even if the stage scaling mode is set to
StageScaleMode.NO_SCALE. Stage.stageWidth and Stage.stageHeight
continue to be reported in classic pixel units. Note: this value can
change dynamically depending if the stage is on a HiDPI or standard
screen.
Also, with
<requestedDisplayResolution>standard</requestedDisplayResolution>
and configureBackBuffer with wantsBestResolution set to true, I still get 1024*768 buffer on iPad. I verified that by drawing sharp 256*256 texture on 128*128 quad - result is blurry. Then I doing the same with
<requestedDisplayResolution>high</requestedDisplayResolution>
I get sharp looking image of the same physical size.
My actual questions are:
Is contentsScaleFactor supposed to be 2 on Retina iPad/iPhone? If so, are there some compiler/packager options I'm missing?
How I can determine for requestedDisplayResolution=standard that my stage was scaled?
And if contentsScaleFactor doesn't work on mobile, for what purpose this property is? Should it work, for example, on Mac with Retina display?
Edit 2:
On Mac with Retina display, contentsScaleFactor works just fine, reporting 2.
Use Capability.screenDPI to get the DPI ratio, divide stage.stageWidth by screenDPI. On Ipad you'll get around 8 (one DPI unit = around 2 fingers of real estate), DPI gives you your density. Now compare both, non retina Ipad = 8 DPI units for 132 pixel/DPI, retina Ipad = 8 DPI units for 264 pixel/DPI (HD), etc ...
From the comments:
Yes, that is how i'm doing it now for IOS devices, but unfortunately,
Capability.screenDPI is unreliable in general. It is always 72 on
desktop, some "random" values on android, and doesn't take into
account current monitor DPI
Maybe its not so much Dots Per Inch that you need but instead you are looking for Pixels Per Inch?
DPI applies when printing as ink dots on paper. PPI means how many pixels against a real-world inch. Don't worry even the Wikipedia article admits "... It has become commonplace to refer to PPI as DPI, even though PPI refers to input resolution" (now we understand that DPI is printing resolution)
This means you should forget Capability.screenDPI and instead start including the Capabilities.screenResolutionX (display width) and Capabilities.screenResolutionY (display height) numbers in your calculations.
Here's some code for feedback. Hopefully it will be useful to you in some way: PS: Double-check the Flash traces by using a ruler against computer screen.
var scrn_W : uint = Capabilities.screenResolutionX;
var scrn_H : uint = Capabilities.screenResolutionY;
var scrn_DPI : uint = Capabilities.screenDPI;
var init_stageW : int = stage.stageWidth; var init_stageH : int = stage.stageHeight;
//Diagonal in Pixels
var scrn_Dg_Pix : Number = int( Math.sqrt( scrn_W * scrn_W + scrn_H * scrn_H ) );
//Diagonal in Inches
var scrn_Diag : Number = int( Math.sqrt( scrn_W * scrn_W + scrn_H * scrn_H ) ) / 100;
var scrn_PPI : uint = int( Math.sqrt( scrn_W * scrn_W + scrn_H * scrn_H ) ) / scrn_Diag;
//or scrn_PPI = scrn_Dg_Pix / scrn_Diag; //gives same result as above line of code
var dot_Pitch : Number = scrn_W / ( Math.sqrt( scrn_W * scrn_W + scrn_H * scrn_H ) ) / (scrn_Diag * 25.4) / scrn_W;
var scrn_Inch_W : Number = ( scrn_W / scrn_PPI ); var scrn_Inch_H : Number = ( scrn_H / scrn_PPI );
var result_Num : Number = 0; var temp_Pix_Num:Number = 0;
var testInch : Number = 0; var my_PixWidth : Number = 0;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, resizeListener); //just in case
////////////////////
//// FUNCTIONS
function inch_toPixels( in_Num : Number) : Number
{
temp_Pix_Num = in_Num;
result_Num = scrn_PPI * temp_Pix_Num;
return result_Num;
}
function pixels_toInch( in_Num : Number) : Number
{
temp_Pix_Num = in_Num;
result_Num = temp_Pix_Num / scrn_PPI;
return result_Num;
}
function cm_toInch( in_CM : Number) : Number
{
//inch = cm * 0.393700787; //from centimetre to inch
result_Num = in_CM * 0.393700787; return result_Num;
}
function inch_toCM( in_Inch : Number) : Number
{
//cm = inch * 2.54; //from inch to centimetre
result_Num = in_Inch * 2.54; return result_Num;
}
function resizeListener (e:Event):void
{
// Handle Stage resize here (ie. app window's Scale Drag / Minimize etc)
//trace("new stageWidth: " + stage.stageWidth + " new stageHeight: " + stage.stageHeight);
}
///////////////
//// TRACES
trace("Stage Init Width : " + init_stageW);
trace("Stage Init Height : " + init_stageH);
trace("Screen Width : " + scrn_W);
trace("Screen Height : " + scrn_H);
trace("Screen DPI : " + scrn_DPI);
trace("Screen PPI : " + scrn_PPI);
trace("Screen Diag : " + scrn_Diag);
trace("Screen Diag Pix : " + scrn_Dg_Pix);
trace("Dot Pitch : " + dot_Pitch);
trace("Disp Width (inches) : " + scrn_Inch_W );
trace("Disp Height (inches) : " + scrn_Inch_H );
How about make use of Application.applicationDPI & Application.runtimeDPI to calculate the actual scale factor?

synopse pdf changing paper size using Delphi

I am trying to create a PDF document using Synopse SynPDF library using Delphi. I need to be able to change the paper size on the fly to accommodate the document I am creating. The paper size's height needs to be changed anywhere from 11 inches to over 100 inches. I would also like to set the resolution of the image to be anywhere from 300 pixels per inch to 600 pixels per inch. This is what I have as a test.
lPdf := TPdfDocumentGDI.Create;
try
lPdf.ScreenLogPixels:=600;
lPdf.DefaultPageHeight := lPdf.ScreenLogPixels * 50; // Since ScreenLogPixels holds the number of pixels per inch this should give me a 50 inch long page.
lPdf.DefaultPageWidth := lPdf.ScreenLogPixels * 8; // Same here with Page being 8 inches wide.
// When viewing the document in Adobe Reader the page height and width 66.67 x 200.00 with nothing displayed
// If I comment out the ScreenLogPixels line the page size becomes 10.67 x 66.67 with a pixel count of 768 x 4800 with the proper text on the document.
lPage := lPDF.AddPage;
lPdf.VCLCanvas.Brush.Style:=bsClear;
MyY:=300;
lPDF.VCLCanvas.TextOut(100, 100, 'Width = ' + IntToStr(lPage.PageWidth) +
' Height = ' + IntToStr(lPage.PageHeight));
for MyX := 1 to 400 do begin
MyXLoc:=(MyX*120) mod (lPage.PageWidth);
MyString:=IntToStr(MyX);
lPDF.VCLCanvas.TextOut(MyXLoc, MyY, Mystring);
lPDF.VCLCanvas.Font.Size:= lPDF.VCLCanvas.Font.Size+4;
lPDF.VCLCanvas.Rectangle(MyXLoc, MyY, MyXLoc+lPDF.VCLCanvas.TextWidth(MyString), MyY+lPDF.VCLCanvas.TextHeight(MyString));
MyY := MyY + lPDF.VCLCanvas.TextHeight(MyString);
end;
lPdf.SaveToFile('c:\Syntest.pdf');
finally
lPdf.Free;
end;
In PDF, all locations and sizes are stored in a logical value called a PDF unit. 1 PDF unit is equivalent to 1/72 of an inch.
DefaultPageHeight and DefaultPageWidth are values in PDF units, so 1/72th of a inch.
So for a 50' * 8' page, you can write:
lPdf.DefaultPageHeight := 72 * 50;
lPdf.DefaultPageWidth := 72 * 8;
Then the VCL canvas available in lPdf.VCLCanvas will have a diverse coordinate system, depending in fact of lPdf.ScreenLogPixels.
So when you draw something in the lPdf.VCLCanvas, ensure you use the right size for coordinates, i.e. via lPdf.VCLCanvasSize values.

Webgl gl.viewport change

I have a problem with canvas resizing and gl.viewport sync.
Let's say that I start with both the canvas 300x300 canvas, and the initialization of gl.viewport at the same sizes (gl.vieport(0, 0, 300, 300)).
After that, in browser's console I make my tests:
I'm changing size of my canvas, using jquery, calling something like $("#scene").width(200).height(200)
After this, i'm calling my resizeWindow function:
function resizeWindow(width, height){
var ww = width === undefined? w.gl.viewportWidth : width;
var h = height === undefined? w.gl.viewportHeight : height;
h = h <= 0? 1 : h;
w.gl.viewport(0, 0, ww, h);
mat4.identity(projectionMatrix);
mat4.perspective(45, ww / h, 1, 1000.0, projectionMatrix);
mat4.identity(modelViewMatrix);
}
function that's synchronizing viewport with required dimensions.
Unfortunatly, my gl.viewport after this call takes only a part of my canvas.
Could anyone tell me what is going wrong?
There is no such thing is gl.viewportWidth or gl.viewportHeight
If you want to set your perspective matrix you should use canvas.clientWidth and canvas.clientHeight as your inputs to perspective. Those will give you the correct results regardless of what size the browser scales the canvas. As in if you set the canvas auto scale with css
<canvas style="width: 100%; height:100%;"></canvas>
...
var width = canvas.clientHeight;
var height = Math.max(1, canvas.clientHeight); // prevent divide by 0
mat4.perspective(45, width / height, 1, 1000, projectionMatrix);
As for the viewport. Use gl.drawingBufferWidth and gl.drawingBufferHeight. That's the correct way to find the size of your drawingBuffer
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
Just to be clear there are several things conflated here
canvas.width, canvas.height = size you requested the canvas's drawingBuffer to be
gl.drawingBufferWidth, gl.drawingBufferHeight = size you actually got. In 99.99% of cases this will be the same as canvas.width, canvas.height.
canvas.clientWidth, canvas.clientHeight = size the browser is displaying your canvas.
To see the difference
<canvas width="10" height="20" style="width: 30px; height: 40px"></canvas>
or
canvas.width = 10;
canvas.height = 20;
canvas.style.width = "30px";
canvas.style.height = "40px";
In these cases canvas.width will be 10, canvas.height will be 20, canvas.clientWidth will be 30, canvas.clientHeight will be 40. It's common to set canvas.style.width and canvas.style.height to a percentage so that the browser scales it to fit whatever element it is contained in.
On top of that there are the 2 things you brought up
viewport = generally you want this to be the size of your drawingBuffer
aspect ratio = generally you want this to be the size your canvas is scaled to
Given those definitions the width and height used for viewport is often not the same as the width and height used for aspect ratio.

Resources