September 2007


Development and PHP25 Sep 2007 12:00 pm

**I HAVE UPDATED THIS, PLEASE READ THE UPDATE**
I have recently been working with Zend_Pdf and it appears that their ability to put text on a screen is very lacking compared to FPDF. I have recently been diving into expanding the functionality and have provided the following class: Zend_Pdf_Cell. This will allow a developer to create a “cell” within a page, center the cell and align the text within the cell.

The following is a small sample code of how to use the cell:


	$pdf=new Zend_Pdf();
	$pdf->pages[] =new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC);
        $pdf->pages[0]->setFont($font,12);  

	//create and attach the cell to the first page, and center in the X and Y direction
        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X | Zend_Pdf_Cell::POSITION_CENTER_Y);
	//align the text in the center
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
        $cell->newLine();
	//change the font
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD),28);
	//align this to the right
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
        $cell->newLine();
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES),10);
	//align this to the left
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_LEFT);
        $cell->newLine();
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC),28);
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
	//finally write the cell to the page.  Text will not show up unless you write to the page.
        $cell->write();
        //create a new cell and center on just the X coordinate
        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X);
        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $cell->setPosition(Zend_Pdf_Cell::POSITION_TOP);
        $cell->addText("The quick brown fox jumped over the lazy dog.");
        $cell->write();

        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_RIGHT,$pdf->pages[0]->getWidth());
        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
        $cell->setPosition(Zend_Pdf_Cell::POSITION_BOTTOM);
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
        $cell->write();

	$pdf->save($this->uploadDir.'/TEST-CELL2.pdf');

I have attached not only the Cell patch, but also the diff of font resource files separately.

In the origional source for the font’s, we can see that they didn’t use the correct widths:

/* The glyph numbers assigned here are synthetic; they do not match the
*  actual glyph numbers used by the font. This is not a big deal though
*  since this data never makes it to the PDF file. It is only used
* internally for layout calculations.
*/

So when attempting to calculate things like a string width, you would get the wrong numbers. I looked up the numbers on Adobe’s Font and Type Technology Center particularly the Unix fonts. From there I generated a small PHP script that would output the ASCII value and the corresponding glyph width. Using these numbers, I updated the Zend font resource files which gives you the diff below. Because these fonts only contain width values for the ASCII numbers 32 – 251ish, those are all that were updated.

Zend Font Width Patch

Cell Patch

Development19 Sep 2007 02:09 pm

Over the first 4 weeks of my senior semester, I have been working on a Rubix Cube application. It started out difficult, such as how we were going to represent the data structure. Once that was decided (using a lot of LINQ for helper functions), I started to focus on parts of the UI. During these parts of the UI, I ran into a couple of pitfalls while using DirectX that held me back.

Culling

So if you make the standard hand with your left hand, look at what plane you are defining your points. If you are defining your points on the XY plane, then notice where your index finger is pointing. If you are not looking at your cube from that direction, your item will not show up because of culling. You can turn this off, but remember you will get a performance hit. I preferred to keep it off because if my graphics card cannot handle ~100 triangles w/o culling, I might as well get another computer or do the computations by hand!

Z Buffering

Contrary to what I believed, DirectX does not enable Z buffering by default. From reading my book I was able to read how to turn it on, but essentially this caused whatever I was drawing last to draw over my other points – even if they were hidden due to their Z layer.

Those two problems really caused me to get a cube that looked crazy and with both combined debugging was impossible because there was no intuitive reason why I would see my faces sometimes and wouldn’t on other times.

This next week or so, we are going to be working on an installer for the application that will allow somebody to install and run the program. I’m not sure how many people will be happy to have to install DirectX 9 and .Net 3.5 to run our little 500kb or so Rubix Cube software, but hey, it was a fun project!