Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use font-family. #231

Closed
philippeowagner opened this issue Nov 28, 2014 · 9 comments
Closed

Can't use font-family. #231

philippeowagner opened this issue Nov 28, 2014 · 9 comments

Comments

@philippeowagner
Copy link

Hi all
I'm using Debian, WeasyPrint 0.22 and I try to use an OCR font by setting the font-family option as described here.

The OCR font is installed (same font in two directories) on the system.

# fc-list | grep ocr
/usr/local/share/fonts/ocr-b-10-pitch-bt.ttf: OCR\-B 10 BT:style=Regular
/usr/share/fonts/truetype/ocr-b-10-pitch-bt.ttf: OCR\-B 10 BT:style=Regular

I tried

font-family: 'OCR\-B 10 BT';
font-family: 'OCR-B 10 BT';
font-family: 'OCR-B';
font-family: 'ocr-b-10-pitch-bt.ttf';
font-family: '/usr/local/share/fonts/ocr-b-10-pitch-bt.ttf';

Nothing worked. Could anyone help me with this issue or has a working example?

BTW: The font in the Font Book on a Mac
screen shot 2014-11-29 at 00 07 14

@SimonSapin
Copy link
Member

WeasyPrint basically just gives the font-family value to Pango in pango_font_description_set_family(). Does your system have a pango-view program installed? Can you manage to render something in it using this font? If so, what was the command line that succeeded?

@philippeowagner
Copy link
Author

@SimonSapin I installed libpango1.0-dev (this package includes pango-view). Not sure how what and what to render using this tool. I forgot to mention that we are working on a headless system.

@SimonSapin
Copy link
Member

Try some variation of pango-view --no-display --output out.png --text 'The lazy fox something something' --font 'OCR-B 10 BT' and see if the correct font is used in the out.png image.

Also note that while pango-view’s --font parameter accepts a "font description" that can contain things like bold (to pick a font face within a font family) and even a size, the font-family CSS property only dictates the name of the family. Other font-* property determine which font face within a family is used.

In your case for example, I don’t know if "Pitch" is part of the family name or not.

@SimonSapin
Copy link
Member

Also, the value of font-family is never a file name. That would be @font-face (#28).

@philippeowagner
Copy link
Author

First of all thanks @SimonSapin for your time and your help.

I tried different variation (name, spaces, escaped, with and without style... ) as suggested but nothing worked for me. I installed another OCR-B font, (not the "Pitch 10 BT" that I have to use.. ) and the pango-view test worked.

That made me suspicious. I tried the following with the Python Image Library.

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

font = ImageFont.truetype("/usr/share/fonts/truetype/ocr-b-10-pitch-bt.ttf", 10)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"0123456789 - @ The lazy fox something something",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("/tmp/out-pil.png")

font = ImageFont.truetype("/usr/share/fonts/truetype/ocr-b", 10)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"0123456789 - @ The lazy fox something something",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("/tmp/out-pil-2.png")

And I got this output

screen shot 2014-11-29 at 13 16 16

It seems that the font is broken - .. or only broken on Linux/Debian?! It works great on my Mac 😮 Anyways. I got another font in .odf format.

 fc-list | grep OCR
 /usr/share/fonts/truetype/OCRB 10 Pitch BT.otf: OCR:style=Regular

And pango-view --no-display --output /tmp/out-1.png --text '0123456789 - @ The lazy fox something something' --font 'OCR' worked, but pango-view ... --font 'OCR:style=Regular' did not. The font worked with PIL as well.

In WeasyPrint, using

.ocr{
    font-family: 'OCR'; 
    font-size: 15px; /* same for 1.5 em, 1 em, 10 px, .. */
 }

I just got the "broken" boxed again.

screen shot 2014-11-29 at 13 53 12

The fonts are here in our Dropbox for debugging purpose.

Any further ideas?

@SimonSapin
Copy link
Member

It’s possible that the font is broken or in a format not supported by your particular version of freetype or fontconfig, and Pango ends up falling back on another one even though it could find the font. But I’m only speculating here.

Note that the --font parameter of pango-view is a "font description string" in Pango’s own format that is probably not the same as the format of fc-list’s output. So rather than --font 'OCR:style=Regular', try --font 'OCR Regular', maybe.

Also, PIL seems to bypass fontconfig entirely, since it accepts a filename directly.

I’m afraid I can’t help anymore than that. I don’t believe there’s any change I can make in WeasyPrint that would help with this, and I don’t know more about how fonts are selected and rendered than described so far.

@philippeowagner
Copy link
Author

@SimonSapin, --font 'OCR Regular' has actually worked for pango-view but not in WeasyPrint in the CSS class.
I try to get another font or find a work-around for my use case.

Thanks again for your help.

@SimonSapin
Copy link
Member

"Regular" is probably not part of the font family name, but probably identifies a font face within the family. (Others could be Bold and Italic.) So in CSS it would probably map to font-family: OCR (and font-weight: normal; font-style: normal, but these are the defaults.)

@philippeowagner
Copy link
Author

We bought today the OCR-B1 font from ocrb1. This font works great and can be addressed in CSS using the following font-family.

.ocr{
    font-family: 'OCR-B1'; 
    font-size: 16px; 
}

This is the correct setup for generating Orange inpayment slip (ISR) for the PostFinance of Switzerland.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants