How to use PDF_get_option() method to get font name? - php

function nvpdf_WrapString($my_pdf,$string,$width,$width_in_chars = "") {
// known problem: can only be called after the pdf document has been
// started, font size set, etc as it relies on the pdf being in the
// page scope to use pdf_stringwidth (see PDFLib manual p.83).
// returns an array: 1st level column
// 2nd level data string broken up into lines that fit the column
$string = str_replace('\n', "\n", $string);
$inlines = explode("\n",$string);
$outlines = array();
$font_size = PDF_get_option($my_pdf, "fontsize", "");
$font_name = pdf_get_parameter($my_pdf, "fontname", 0);
$font_id = PDF_load_font($my_pdf, $font_name, "host","");
I got a deprecation warning from php saying that I should use pdf_get_option() instead of pdf_get_parameter(). But how can I convert this pdf_get_parameter() to pdf_get_option()?
I need a method that will return me the font name of $mypdf.

Found the solve. pdf_get_option() wont give me the font name. All I have to do is use pdf_info_font() and then use pdf_get_string() to get the fontname.

nice that you could find the solution yourself.
Another quick note: If you have found a deprecated warning for a PDFlib function, it is recommended to replace it even if the functionality still works in the version you are using. This is because the deprecated function may no longer be available when updating.
The current PDFlib 9 and 10 packages include a PDFlib Migration Guide which describes all necessary changes in detail.
You can also find the latest PDFlib 10 Migration Guide on the PDFlib Web site pdflib.com.
for this particular question you would find the explanation in chapter 2.1 table 2.3:
deprecated parameter:
ascender, ascenderfaked, capheight, capheightfaked, descende,
descenderfaked, fontencoding, fontname,fontmaxcode, xheight, xheightfaked
deprecated since:
PDFlib 7
replacement method and option:
PDF_info_font( ) with same-named keywords; for the numerical
values fontsize=1 must be supplied;
parameter fontencoding: use keyword encoding
A sample usage is also available in the PDFlib cookbook "font/font_info".
/* Get the font name: use "api" to get the font name used by PDFlib.
* (use "acrobat" for the Acrobat font name or use "full" for
* retrieving the full font name)
*/
$p->fit_textline("fontname (api):", $x, $y-=$yoffset, "");
$info = $p->info_font($font, "fontname", "api");
if ($info > -1) {
$fontname = $p->get_string($info, "");
$p->fit_textline($fontname, $xindent, $y, "");
}

Related

SURF/SIFT type image pattern matching library in PHP

i wanna know if there is some SURF library that is written in PHP available? From little findings that i did till now surf libraries are either in C++/C#, if some link to similar technologies that are built in PHP if provided it will be appreciated. I googled my way for some builtin features, the only thing that was close enough was Image Magick. But from comments it looks like pattern matching cannot be done in it.
Let me re define my self i just dont want to compare 2 images , lets say there is a Google logo with in an image, and there is an image of Google Logo as a separate image what i want to search if there is some image where Google's logo/image is reproduced.
OpenCV is a great open source library for image processing and computer vision. I found a PHP wrapper (a detailed tutorial here), BUT it I am afraid that it doesn't wrap the SIFT/SURF code...
Reading your comment I saw you need to match two images. If your objective is to match a pattern (one image) against another image, you can use this example:
<?php
use OpenCV\Image as Image;
use OpenCV\Histogram as Histogram;
echo "Loading sample\n";
$im = Image::load("elephpant_sample.jpg", Image::LOAD_IMAGE_COLOR);
$im2 = Image::load("dragonbe_elephpants.jpg", Image::LOAD_IMAGE_COLOR);
for ($i = 0; $i < 6; $i++) {
$result = $im2->matchTemplate($im, $i);
$result->save("mt_output_$i.jpg");
}
In the case you are searching for objects, you can use the Haar Cascade part. Here is an example that detects faces in images.
You can try the phash (http://phash.org) algorithm in php. Or read this question about image fingerprint and similarities:Image fingerprint to compare similarity of many images.

Is there a way to tell whether a font supports a given character in Imagick?

I'm using Imagick to generate simple logos, which are just text on a background.
I'm usually looping through all available fonts, to present the user with a choice of different renderings for every font (one image per font).
The problem is, some fonts don't support the ASCII characters (I think they've been designed for a given language only). And I guess that some of the fonts which support ASCII characters, will fail with non-ASCII characters as well.
Anyway, I end up with images such as these:
Is there a programmatic way in Imagick to tell whether a given font supports all the characters in a given string?
That would help me filter out those fonts which do not support the text the user typed in, and avoid displaying any garbage images such as the ones above.
I don't know a way using imagemagik, but you could use the php-font-parser library from here:
https://github.com/Pomax/PHP-Font-Parser
Specifically, you can parse a font for each letter in your required string and check the return value:
$fonts = array("myfont.ttf");
/**
* For this test, we'll print the header information for the
* loaded font, and try to find the letter "g".
*/
$letter = "g";
$json = false;
while($json === false && count($fonts)>0) {
$font = new OTTTFont(array_pop($fonts));
echo "font header data:\n" . $font->toString() . "\n";
$data = $font->get_glyph($letter);
if($data!==false) {
$json = $data->toJSON(); }}
if($json===false) { die("the letter '$letter' could not be found!"); }
echo "glyph information for '$letter':\n" . $json;
Above code comes from the font parser projects fonttest.php class:
https://github.com/Pomax/PHP-Font-Parser/blob/master/fonttest.php

Is possible to embed fontawesome font in fpdf?

I'd like to use fontawesome in pdf. I generate my pdf using php library fpdf and font embedding. However I cannot make it works.
I use this tool to generate afm file: http://fpdf.fruit-lab.de/
But when I try to use fontawesome I always get white square instead of icons.
I use this syntax to add icon:
MultiCell(0,8,"\uF000 ",0,'C')
I cannot answer for fpdf since I have never used it. However, I do use mPDF and there I use fontawesome regularly - no issues at all. The only thing I have to ensure is that the content I output to the PDF document (mPDF takes this in the form of HTML markup) hast to be UTF8 encoded.
mPDF is very good so if you are at an early stage of your project you might just consider switching to it. Otherwise, it is worth exploring whether you too are not running into a UTF8 encoding issue.
I figured this out even though this thread is over a year old. It is possible to use font-awesome although you can only use up to 256 characters per subset. I created several character maps to encompass all of the glyphs as of font awesome version 4.3.0. You just use the map that contains the characters you're going to use or you can make three subsets of it. It's not necessarily as performant as other solutions, but fPDF is still much faster than some of the alternatives because it is lacking a lot of the more modern features like unicode support.
http://code.deweyartdesign.com/fpdf/makefont/fa1.map
http://code.deweyartdesign.com/fpdf/makefont/fa2.map
http://code.deweyartdesign.com/fpdf/makefont/fa3.map
First, you need to use ttf2pt1 to create the afm file for the font.
ttf2pt1 -a fontawesome-webfont.ttf fontawesome-webfont
I made three copies of the webfont to run through makefont.php and used each encoding on the corresponding font.
require "makefont.php";
makefont('fontawesome-webfont1.ttf','fa1.map');
makefont('fontawesome-webfont2.ttf','fa2.map');
makefont('fontawesome-webfont3.ttf','fa3.map');
To use them in fPDF, put the files generated in the font folder and add them like this:
$pdf->AddFont('FA1','',fontawesome-webfont1.php);
$pdf->AddFont('FA2','',fontawesome-webfont2.php);
$pdf->AddFont('FA3','',fontawesome-webfont3.php);
Then you use the character number to render the glyph for the corresponding subset of font-awesome. The three font maps above contain the character numbers to use :
$pdf->SetFont('FA1','',14);
$WineGlass = chr(32);
$pdf->Cell(40,10,$WineGlass);
I also made a character map generator that will show the character numbers below the glyphs.
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/pdf/fpdf.php');
// Establish / Get variables
function GETVAR($key, $default = null, $prefix = null, $suffix = null) {
return isset($_GET[$key]) ? $prefix . $_GET[$key] . $suffix : $prefix . $default . $suffix;
}
$font = GETVAR('font','fontawesome-webfont1','','.php');
$pdf = new FPDF('L','mm',array(268.33,415.3));
$pdf->AddPage();
$pdf->SetMargins(0,0,0);
$pdf->SetAutoPageBreak(0,0);
// add custom fonts
$pdf->AddFont('H','','helvetica.php');
$pdf->AddFont('FA','',$font);
$pdf->SetFillColor(200,200,200);
$pdf->SetXY(9,9);
for ($i = 32; $i <= 256; $i++) {
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetX($x);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('FA','',14);
$pdf->Cell(12,12,chr($i),1,0,'C');
$pdf->SetXY($x,$y+12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('H','',14);
$pdf->Cell(12,12,$i,1,0,'C',1);
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetXY($x,$y-12);
if ($x > 400) {
$pdf->SetXY(9,$y+14);
}
if ($i == 328){
$pdf->AddPage();
}
}
$pdf->Output("charmap.pdf",'I');
You can re-create the font-awesome and re-map it to A-Z characters,
and that way, use it easily in fpdf.
// Import and prepare font for usage by engine:
$pdf->AddFont('fontawesome-123-full','','fontawesome-123-full.php');
...
// print FontAwesome's asterisk
$pdf->Text($x, $y, chr(0x21));
You can download the ready font and ready-to-use FPDF font files here:
http://mdb-blog.blogspot.com/2021/12/using-fontawesome-in-php-fpdf.html

PHP Excel Error when entering SUM function to a cell

I'm having this issue, when generating an EXCEL file with sum formulas.
PHP code:
$objPHPExcel->getActiveSheet()->setCellValue("I".$fila,"=SUMA(I17:I".($fila-1).")");
Note: SUMA is "sum" in spanish.. Works like that for spanish config
When I open Excel file, it shows values as : ######
But the formula is correct, when I just type enter after the formula it gets re-calculated and correct.
PHPExcel uses English language internally: if you're using Spanish language for your formula functions, you have to set the local to Spanish; and can then translate them to English to inject into the cell. See section 4.6.5 of the developer documentation (entitled "Locale Settings for Formulae").
$locale = 'es';
$validLocale = PHPExcel_Settings::setLocale($locale);
if (!$validLocale) {
echo 'Unable to set locale to '.$locale." - reverting to en_us<br />\n";
}
$formula = "=SUMA(I17:I".($fila-1).")";
$internalFormula =
PHPExcel_Calculation::getInstance()->translateFormulaToEnglish($formula);
$objPHPExcel->getActiveSheet()->setCellValue("I".$fila, $internalFormula);
You can use the formula functions in English.
$objPHPExcel->getActiveSheet()->setCellValue("I".$fila,"=SUM(I17:I".($fila-1).")");
open the document in excel, you can see the total calculated and in function formula "=SUMA()"
Only a corretion:
$internalFormula =
PHPExcel_Calculation::getInstance()->_translateFormulaToEnglish($formula);

Sorting Katakana names

If I have a list of Katakana names what is the best way to sort them?
Also is it more common to sort names based on their {first name}{last name} or {last name}{first name}.
Another question is how do we get the first character Hiragana representation of a Katakana name like how it is done for the iPhone's contact list is sorted.?
Thanks.
In Japan it is common (if not expected) that a person's first name appear after their surname when written: {last} {first}. But this would also depend on the context. In a less formal context it would be acceptable for a name to appear {first} {last}.
http://en.wikipedia.org/wiki/Japanese_name
Not that it matters, but why would the names of individuals be written in Katakana and not in the traditional Kanji?
I think it's
sort($array,SORT_LOCALE_STRING);
Provide more information if it's not your case
This answer talks about using the system locale to sort Unicode strings in PHP. Besides threading issues, it is also dependent on your vendor having supplied you with a correct locale for what you want to use. I’ve had so much trouble with that particular issue that I’ve given up using vendor locales altogether.
If you’re worried about different pronunciations of Unihan ideographs, then you probably need access to the Unihan database — or its moral equivalent. A smaller subset may suffice.
For example, I know that in Perl, the JIS X 0208 standard is used when the Japanese "ja" locale for is selected in the constructor for Unicode::Collate::Locale. This doesn’t depend on the system locale, so you can rely on it.
I’ve also had good luck in Perl with Lingua::JA::Romanize::Japanese, as that’s somewhat friendlier to use than accessing Unicode::Unihan directly.
Back to PHP. This article observes that you can’t get PHP to sort Japanese correctly.
I’ve taken his set of strings and run it through Perl’s sort, and I indeed get a different answer than he gets. If I use the default or English locale, I get in Perl what he gets in PHP. But if I use the Japanese locale for the collation module — which has nothing to do with the system locale and is completely thread-safe — then I get a rather different result. Watch:
JA Sort                          = EN Sort
------------------------------------------------------------
Java                               Java
NVIDIA                             NVIDIA
Windows ファイウォール             Windows ファイウォール
インターネット オプション          インターネット オプション
キーボード                         キーボード
システム                           システム
タスク                             タスク
フォント                           フォント
プログラムの追加と削除             プログラムの追加と削除
マウス                             マウス
メール                             メール
音声認識                         ! 地域と言語オプション
画面                             ! 日付と時刻
管理ツール                       ! 画面
自動更新                         ! 管理ツール
地域と言語オプション             ! 自動更新
電源オプション                     電源オプション
電話とモデムのオプション           電話とモデムのオプション
日付と時刻                       ! 音声認識
I don’t know whether this will help you at all, because I don’t know how to get at the Perl bits from PHP (can you?), but here is the program that generates that. It uses a couple of non-standard modules installed from CPAN to do its business.
#!/usr/bin/env perl
#
# jsort - demo showing how Perl sorts Japanese in a
# different way than PHP does.
#
# Data taken from http://www.localizingjapan.com/blog/2011/02/13/sorting-in-japanese-—-an-unsolved-problem/
#
# Program by Tom Christiansen <tchrist#perl.com>
# Saturday, April 9th, 2011
use utf8;
use 5.10.1;
use strict;
use autodie;
use warnings;
use open qw[ :std :utf8 ];
use Unicode::Collate::Locale;
use Unicode::GCString;
binmode(DATA, ":utf8");
my #data = <DATA>;
chomp #data;
my $ja_sorter = new Unicode::Collate::Locale locale => "ja";
my $en_sorter = new Unicode::Collate::Locale locale => "en";
my #en_data = $en_sorter->sort(#data);
my #ja_data = $ja_sorter->sort(#data);
my $gap = 8;
my $width = 0;
for my $datum (#data) {
my $columns = width($datum);
$width = $columns if $columns > $width;
}
my $bar = "-" x ( 2 + 2 * $width + $gap );
$width = -($width + $gap);
say justify($width => "JA Sort"), "= ", "EN Sort";
say $bar;
for my $i ( 0 .. $#data ) {
my $same = $ja_data[$i] eq $en_data[$i] ? " " : "!";
say justify($width => $ja_data[$i]), $same, " ", $en_data[$i];
}
sub justify {
my($len, $str) = #_;
my $alen = abs($len);
my $cols = width($str);
my $spacing = ($alen > $cols) && " " x ($alen - $cols);
return ($len < 0)
? $str . $spacing
: $spacing . $str
}
sub width {
return 0 unless #_;
my $str = shift();
return 0 unless length $str;
return Unicode::GCString->new($str)->columns;
}
__END__
システム
画面
Windows ファイウォール
インターネット オプション
キーボード
メール
音声認識
管理ツール
自動更新
日付と時刻
タスク
プログラムの追加と削除
フォント
電源オプション
マウス
地域と言語オプション
電話とモデムのオプション
Java
NVIDIA
Hope this helps. It shows that it is, at least theoretically, possible.
EDIT
This answer from How can I use Perl libraries from PHP? references this PHP package to do that for you. So if you don’t find a PHP library with the needed Japanese sorting stuff, you should be able to use the Perl module. The only one you need is Unicode::Collate::Locale. It comes standard as of release 5.14 (really 5.13.4, but that’s a devel version), but you can always install it from CPAN if you have an earlier version of Perl.

Categories