#1 2010-07-02 03:15:39

profh
Member
Registered: 2010-07-02
Posts: 161

emf2pdf

i need to create a pdf file from the emf files with simple chinese fonts, but the error in "setfont" function occured.

i use SynPdf 1.8.1, and here is my simple code:

var
  PDF: TPDFDocument;
  m:TMetafile;
  i:integer;
begin
  PDF:= TPDFDocument.Create;
  pdf.DefaultPaperSize := psA4;
  m := TMetafile.Create;
  m.LoadFromFile('c:\1\1.emf');
  with pdf do
  try
//    pdf.EmbeddedTTF := true;
//    PDF.EmbeddedWholeTTF := true;
    PDF.UseUniscribe := true;
   
    StandardFontsReplace := true;
    AddPage;
//    Canvas.SetFont('宋体',12,[fsItalic,fsBold]);
    Canvas.RenderMetaFile(m,1);
    SaveToFile('c:\1.pdf');
  finally
    Free;
  end;
  m.free;
end.

i wonder how to upload the emf file.

thanks and best regards!

Offline

#2 2010-07-02 07:10:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

Note that calling SetFont() before RenderMetaFile won't do nothing: the RenderMetaFile will override the SetFont parameters.

Font names are expected to be win-ansi encoded, i.e. in enligsh characters... no equivalency of this font does exists?

Offline

#3 2010-07-02 09:05:45

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

thanks for your reply!

i print the ms word file with simple chinese font into emf files, so the font should be  "simple chinese font".
Could you give me your email address, i will email you the emf file?

thanks again!

Offline

#4 2010-07-02 09:48:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I sent an email to you...

Offline

#5 2010-07-02 10:13:50

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

thank you.

i sent you an email with the emf file attached.

Offline

#6 2010-07-15 01:51:54

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

hi,ab:

i am still not able to transform my emf file to pdf file.
did you receive my email with the emf file which i used?

thanks!

Offline

#7 2010-07-15 02:21:29

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

with the latest html2pdf,i also got an error message.
i sent you an email with the screenshot.

Best Regards!

Offline

#8 2010-07-15 05:26:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

They must be an error with chinese code page and such... I'll check.

Offline

#9 2010-07-15 05:33:45

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

thanks.

it was the same message when i convert my emf file to the pdf file.

Offline

#10 2010-07-15 08:40:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I've made some upload in SynCommons.pas and SynPdf.pas, after having found a bug in font name comparison.

Please check in from http://synopse.info/fossil and try to run it.

Otherwise, as I said in my mail:

Can you make a little debugging on your computer?

Such a message indicates that the 'Arial' font is not found.

Is suspect the name is invalid.

Can you put a breakpoint in TPdfCanvas.SetFont() then check what's wrong?                                           
The breakpoint must be made in line:

      raise EPdfInvalidValue.CreateFmt('SetFont %s',[AName]); // error 

Can you give me also the fDoc.FTrueTypeFonts.Text content in this content?

Offline

#11 2010-07-15 09:26:51

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i sent u an email with attached files.

thanks.

Offline

#12 2010-07-15 09:30:19

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i used the latest synpdf from http://synopse.info/fossil in the debug mediafile.

Offline

#13 2010-07-16 06:12:29

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

setFont times New Roman.

Offline

#14 2010-07-16 06:26:31

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

can't SameText()
so  FontIndex always equal -1, then Error occurs.

Offline

#15 2010-07-16 06:31:50

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

it seems that the content of fDoc.FTrueTypeFonts list is different from which in my windows fonts directory.

my computer system is windows XP with service pack 2

Offline

#16 2010-07-16 07:20:48

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

profh wrote:

it seems that the content of fDoc.FTrueTypeFonts list is different from which in my windows fonts directory.

my computer system is windows XP with service pack 2


PDF uses Embeded font,mostly don't support Chinese Font. In order to show Chinese Fonts ,it's necessary to translate it.

Offline

#17 2010-07-16 07:22:12

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

ab wrote:

They must be an error with chinese code page and such... I'll check.


i have sent you an Email ,please Check it.

Offline

#18 2010-07-16 07:31:29

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

longge007  wrote:

In order to show Chinese Fonts ,it's necessary to translate it.

how to translate it,any idea?

thanks.

Offline

#19 2010-07-16 07:43:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

It was definitively a "charset" problem...

In TPdfDocument.Create, it enumerates fonts only for the current CharSet: therefore, it enumerated only fonts with chinese characters if the current CharSet was Chinese.

I just changed the corresponding line to:

  LFont.lfCharset := DEFAULT_CHARSET; // FCharSet will avoid Arial or Times New Roman

Here is the new callback code:

function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  FontType: Integer; Doc: TPdfDocument): Integer; stdcall;
// we enumerate all available fonts, whatever the charset is, because
// we may won't enumerate Arial if current FCharSet is chinese e.g.
var Temp: string;
begin
  with LogFont do
    if (FontType=TRUETYPE_FONTTYPE) and (lfFaceName[0]<>'@') then begin
      Temp := lfFaceName;
      with Doc.FTrueTypeFonts do
        if (Count=0) or not SysUtils.SameText(Strings[Count-1],Temp) then
          Add(Temp);
    end;
  Result := 1;
end;

This new 1.8.5 version uploaded to our source code repository should correct your problems.

Offline

#20 2010-07-16 08:11:06

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

many thanks,but two problems are remained:
1. the text position is not exactly right.
2. part of content did not also appeared.


i sent you an email,please check.

Offline

#21 2010-07-16 08:22:14

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

now ,it can create PDF file ,but can't show chinese fonts, all are Error code.

Offline

#22 2010-07-16 08:23:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

longge007 wrote:

Exception class EAccessViolation with message 'Access violation at address 004C021A

Which line source code is it? Hexa address are pointless, because it depends on your compiler and libraries...

I suspect you didn't get all modifications from our source code repository, but modified the code by hand, according to my comment in my previous post, which is not enough. smile

Offline

#23 2010-07-16 08:28:09

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

ab wrote:
longge007 wrote:

Exception class EAccessViolation with message 'Access violation at address 004C021A

Which line source code is it? Hexa address are pointless, because it depends on your compiler and libraries...

I suspect you didn't get all modifications from our source code repository, but modified the code by hand, according to my comment in my previous post, which is not enough. smile

yeah, it's right, i only change some.

Offline

#24 2010-07-16 08:40:47

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

test html codes

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
</head>

<body>
<strong><font size="+5">20100716测试中文字体,中国北京</font></strong><br>
Test chinese Font
</body>
</html>

Offline

#25 2010-07-16 09:07:55

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

you can install asian fonts in you pc, and then test it

Offline

#26 2010-07-16 09:36:54

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

my computer system is windows XP with service pack2 and simple chinese fonts.

Offline

#27 2010-07-16 11:15:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

Problem about html comes not from SynPdf here...

THtmlView itself doesn't handle UTF-8 encoding for HTML files sad

I'll have to hack the THtmlView  code, and add this feature....

Offline

#28 2010-07-16 14:43:31

longge007
Member
Registered: 2010-06-22
Posts: 107

Re: emf2pdf

ab wrote:

Problem about html comes not from SynPdf here...

i think the problem is from synpdf , or Thtmlview code.

for i test the Example5 ,i write some chinese words in the Text, it also shows wrong fonts.

Offline

#29 2010-07-16 15:58:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I've updated the SynPdf unit in the Source Code Repository:

  Version 1.8.6
  - system font enumeration is now stored using UTF-8, and any non ASCII font name will used in the PDF content the official Postscript name extracted from its TrueType font content;
  - optional charset parameter is now available in TPdfCanvas.SetFont: this was needed in case of TMetaFile rendering to fix some encoding problems.

It should fix some problems about chinese fonts. But THtmlView is not able to read UTF-8 content by now.

Offline

#30 2010-07-16 16:00:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

profh wrote:

many thanks,but two problems are remained:
1. the text position is not exactly right.
2. part of content did not also appeared.

I've updated the SynPdf unit in the Source Code Repository:

  Version 1.8.6
  - system font enumeration is now stored using UTF-8, and any non ASCII font name will used in the PDF content the official Postscript name extracted from its TrueType font content;
  - optional charset parameter is now available in TPdfCanvas.SetFont: this was needed in case of TMetaFile rendering to fix some encoding problems.

It should fix some problems about fonts with name encoded in Chinese (I hope).

Offline

#31 2010-07-17 02:46:20

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

many many thanks,ab

update the source code,two problems are still remained:
1. the text position is not exactly right, all arranged on the upper left corner.
2. many more text does appear, but a few part of content is still not appeared.

Offline

#32 2012-07-06 08:47:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

By the way, I'm working on font fall-back for SynPDF.

I hope that it will be able to easily handle Chinese characters soon, without the need to set the explicit font.

Offline

#33 2012-07-10 14:21:38

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i just try it,the chinese words of emf file are all generated into pdf file,but they can not display properly in pdf file.
during generate pdf file,some exceptions are fired near the following code of synpdf.pas:

constructor TPdfTTF.Create(aUnicodeTTF: TPdfFontTrueType);
......
    for i := 0 to aUnicodeTTF.fUsedWideChar.Count-1 do
    with aUnicodeTTF.fUsedWide[i] do
      if Glyph<>0 then
        Width := (cardinal(fhmtx[Glyph*2])*1000) shr fUnitsPerEmShr;

Offline

#34 2012-07-10 15:05:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

"Some" and "Near" are a big vague to investigate further...
wink

Are you sure the TTF file is a true outline font?

Offline

#35 2012-07-10 15:50:16

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

yes, a big vague smile ,because i can not past picture or file here.

the exception line is:
     Width := (cardinal(fhmtx[Glyph*2])*1000) shr fUnitsPerEmShr;

exception msg:
'Access violation at address 004626A7 in module 'Project1.exe'. Read of address 004B4000'

Are you sure the TTF file is a true outline font?

i m not sure,indeed.

Offline

#36 2012-07-11 05:52:17

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I've modified the TTF reading, in order to had a check for the size of the fhmtx[] array.
See http://synopse.info/fossil/info/87a1242747

Offline

#37 2012-07-11 10:46:11

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i really appreciate.

with my own rtf and emf files, no exception raised any more,but:
1. in pdf file,no chinese word appears,if i switch to view this pdf document as text file,then all chinese words appear.
2. position of numeric word in the pdf is not exactally correct.

i send pdf files to you,pls check it: abouchez@magic.fr

Offline

#38 2012-07-11 11:01:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

This email address is down.

Please use webcontact01 at synopse dot info.

Offline

#39 2012-07-12 08:39:20

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i sent an email including emf and pdf files to you last night,please check it.

Offline

#40 2013-04-08 08:19:26

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

procedure TForm1.Button3Click(Sender: TObject);
var
  Gdip:TGDIPlusFull;
  emf:TMetaFile;
begin
  Gdip := TGDIPlusFull.Create;
  emf := TMetaFile.Create;
  try
    emf.LoadFromFile('c:\emf\1.emf');
    gdip.DrawAntiAliased(emf,PaintBox1.Canvas.Handle,Rect(0,0,300,500));
  finally
    emf.Free;
    Gdip.Free;
  end;
end;

PaintBox1 can display all of the emf file including chinese words and bitmap.

if i change above code as the following, the produced pdf file just contains bitmap,but no chinese words at all.

procedure TForm1.Button2Click(Sender: TObject);
const
  OutputFileName = 'c:\emf\1.pdf';
  InputFileName = 'c:\emf\1.emf';
var
  PdfDoc: TPDFDocumentGDI;
  PdfPage: TPdfpage;
  Emf: TMetafile;
begin
  Emf := TMetafile.Create();
  Emf.LoadFromFile(InputFileName);

  PdfDoc := TPdfDocumentGDI.Create;
  PdfDoc.UseFontFallBack := true;
  PdfDoc.CompressionMethod := cmFlateDecode;
  PdfDoc.DefaultPaperSize := psUserDefined;
  PdfPage := PdfDoc.AddPage();
  PdfPage.PageWidth := Round(72 * Emf.MMWidth / 2540);
  PdfPage.PageHeight := Round(72 * Emf.MMHeight / 2540);
  gdip.DrawAntiAliased(emf,PdfDoc.VCLCanvas.Handle,Rect(0, 0,
//gdip.DrawAntiAliased(emf,PaintBox1.Canvas.Handle,Rect(0, 0,
      Round(Screen.PixelsPerInch * Emf.MMWidth / 2540),
      Round(Screen.PixelsPerInch * Emf.MMHeight / 2540)));

  PdfDoc.SaveToFile(OutputFileName);
  PdfDoc.Free();

  FreeAndNil(Emf);
end;

Offline

#41 2013-04-08 08:51:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

Did you set the FontFallBackName property to an existing font including all glyphs?
Did you use the latest unstable 1.18 version from our repository?

Offline

#42 2013-04-08 09:34:16

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i download ARIALUNI.TTF from http://anonym.to/?http://sourceforge.ne … nts/files/, and then set FontFallBackName like this:

PdfDoc.FontFallBackName := 'Arial Unicode MS';

the chinese words are still missing.

i also download synpdfunicode_test.rar  from https://cdn.anonfiles.com/1363978013590.rar , and it works as expected.

BTW,i am using the latest code.

Last edited by profh (2013-04-08 09:35:48)

Offline

#43 2013-04-08 09:39:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

Could you step in the SynPDF code and check if the font is found, and how the glyphs are retrieved and appended to the pdf?

Offline

#44 2013-04-08 09:58:20

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

DrawAntiAliased in SynGdiPlus.pas is like this:

procedure TGDIPlus.DrawAntiAliased(Source: TMetafile; Dest: HDC; R: TRect;
  aSmoothing: TSmoothingMode; aTextRendering: TTextRenderingHint);
begin
  dec(R.Right);
  dec(R.Bottom); // Metafile rect includes right and bottom coords
  PlayEnhMetaFile(Dest,Source.Handle,R); // use GDI drawing by default
end;

but PlayEnhMetaFile is a windows function, i can not step in it.

Offline

#45 2013-04-08 11:17:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I just checked your code.

It does not make sense at all to call DrawAntiAliaised() over a PDF canvas.
GDI+ targets bitmap content, not vectorial content.

Just draw the emf content to the PDF canvas, as stated by all sample code around.

Offline

#46 2013-04-09 03:18:54

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i use htm2pdf in the latest package from http://synopse.info/fossil/wiki/Synopse+OpenSource, and open a html file with chinese words, then save html file to pdf file, the chinese words are still missing in the output pdf file, but the metafile produced by htmlview contains all things in the html file.

could you give me a sample, many thanks.

Offline

#47 2013-05-19 15:42:05

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i change line 5827 of synpdf.pas

from:

RawUnicodeToUtf8(ALogFont.lfFaceName,StrLenW(ALogFont.lfFaceName));

to:

AName := ALogFont.lfFaceName;


then the following code can render emf file including all chinese words into pdf file:

procedure TForm1.Button6Click(Sender: TObject);
var
  emf : TMetafile;
  Pdf: TPdfDocument;
begin
  emf := TMetafile.Create;
  Pdf := TPdfDocument.Create;

  try
    emf.LoadFromFile('c:\emf\21.emf');
    Pdf.EmbeddedTTF := True;
    pdf.StandardFontsReplace := true;
    pdf.PDFA1 := true;
    Pdf.UseFontFallBack := true;
    Pdf.FontFallBackName := 'Arial Unicode MS';
    Pdf.DefaultPageWidth := MulDiv(emf.Width,72,Pdf.ScreenLogPixels);
    Pdf.DefaultPageHeight := MulDiv(emf.Height,72,Pdf.ScreenLogPixels);
    Pdf.AddPage;
    Pdf.Canvas.RenderMetaFile(emf);
    pdf.SaveToFile('c:\emf\test.pdf');
  finally
    FreeAndNil(emf);
    FreeAndNil(pdf);
  end;
end;

but the position and size of some words are unusual.

i sent an email with "pdf from emf.jpg" and "emf21.emf" to webcontact01 at synopse.info, please check it.

Offline

#48 2013-05-20 09:54:56

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

Thanks.
I'll check it.

Which version of Delphi are you using?

Offline

#49 2013-05-20 11:52:34

profh
Member
Registered: 2010-07-02
Posts: 161

Re: emf2pdf

i use delphi7 and the latest great mORMot.

thanks.

Offline

#50 2013-05-20 12:05:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: emf2pdf

I just checked the code.

With Delphi 7, it indicates that the internal true type font list is not encoded in UTF-8, but in Ansi.
Weird... I'm not able to reproduce it here.

We use TLogFontW in each API call, i.e. the Unicode version of it, not the Ansi version..

What is the font name?
What is the exact ALogFont.lfFaceName buffer content?
What is the value stored by EnumFontsProcW() into FTrueTypeFonts[] ?

Offline

Board footer

Powered by FluxBB