How to render chinese characters and other languages like arabic with cairo in python? - character

it seems that cairo can't correctly render text like characters in chinese and arabic. Could anyone tell me how to render these texts in cairo? Can pango with cairo in python render these characters? Thanks!

If you select an appropriate font face, you should be able to draw the texts straightforwardly.
cr.select_font_face('Noto Sans CJK SC')
cr.move_to(100, 100)
cr.set_source_rgb(0, 0, 0)
cr.show_text('好吧')
To list available fonts, type in console:
$ fc-list : family

According to cairo's FAQ, the answer is "use PangoCairo":
[...] you learn that cairo_show_text is part of cairo's "toy" text API. It's fine for quick demos, and for learning how to use cairo, but it's not intended for use in actual applications.
It's not hard to run into some of the limitations of cairo_show_text.
[...]
Fortunately, the pango library exists and does do sophisticated text layout, shaping, etc. and integrates very nicely with cairo. We heartily recommend that "real" applications wanting to display text with cairo use pango to do it.

Related

What is this syntax formatting tool?

I know this has been out for a while, but I missed the wave, and now I don't know what it's called.
What plugin is being used here to format the italics, single arrow icon, and combined equal signs? I primarily use VSCode.
Actually not a plug-in -- You're looking for a font that has both ligatures and cursive italics, like operator mono. Fair warning, it's not cheap!

Unicode characters for «email», «save», «print»

I want to make a fallback for my icon font. For example, for my beautiful icon font check mark I use Unicode check mark equivalent:
.icon-checkmark {
&:before {
content: "\2713"; /* Unicode Character 'CHECK MARK' (U+2713) */
}
}
My icon font has character with code "\2713" also. If my icon font fails to load, user will see Unicode check mark; if icon font loads successfully, user will see icon font's beautiful check mark.
I'm searching for Unicode character equivalents for «email», «save» and «print» entities. Are there any or similar in Unicode tables? I have searched on http://www.fileformat.info/ but with no luck.
(I have found only an «email» character — http://www.fileformat.info/info/unicode/char/1f4e7/browsertest.htm, but it does not work in Chrome 28 (it works in all other browsers however :).
Here's some ideas. I have not tested them on any browsers except Firefox on Linux.
Email: ✉, Unlikely: 
Print: ⎙
Save: ↓, ▼
Edit: 💾 \U0001f4be could be used for saving since Unicode 6.x
I was also looking for save (floppy disk) symbol.
💾 symbol (mentioned in #Dark Falcon answer) is colored and not adjustable with its adjacent text colors.
I finally got 🖫 from graphemica.com
We can adjust it in any color by CSS color property.
🖫 white hard shell floppy disk for save (U+1F5AB)
✉ print screen symbol (U+2399)
⎙ envelope for email (U+2709)
Your question is actually two-fold: which Unicode code-points are useful for your purpose, and which Unicode code-points are covered with common font installations.
And it raises a new question: why do some programs (Chrome on Windows?) not show correct glyphs where other programs can?
Regarding the first two questions: as you can see, these days some really useful symbols just don't work on many systems out of the box.
Regarding the last question: I have no idea, but some insights on Linux:
Many programs (including Chrome) end up using fontconfig via one way or another. That library is responsible to find the fonts useful to display certain "text". At a higher level, the rendering is done with a mix of fonts, because for more challenging (web page) text there will always be a situation where one font won't cover everything there to display. Might the reason be that another style is requested or a code point is not covered.
So if Chrome on Linux does not show one thing or another, install fonts which have those glyphs (in a way that integrates well with fontconfig-configuration).
I have no idea what drives font-mixing on Windows.

Why does Firefox squeeze Windows' heart, but not Ubuntu's?

I'm trying to display the heart ♥ Unicode character (U+2665 BLACK HEART SUIT) in this jsfiddle.
Even though I've specified the Droid Sans font, the different browsers are displaying the same character differently. So, I'm assuming that the Droid Sans font doesn't include the ♥ character and the browser must fallback to some other font to display this character. But how does the browser determine which font to use for Unicode characters; as it turns out (from screenshots) that it's not operating system specific as Firefox and Chromium both on Ubuntu display it differently; and also it is not browser specific as Firefox displays it differently on Ubuntu and Windows 7.
So my questions are - How does a browser determine which font to use to display Unicode characters; how can I find out which font is being used by the browser to display Unicode characters; and how can I ensure a consistent look cross-browser?
PS: (Firefox specific) Even though Droid Sans doesn't include the ♥ character, Firefox displays it as in screenshot only when the selected font is Droid Sans. For any other font, Firefox picks up the DejaVu Sans font to display the ♥ character (on Ubuntu, confirmed by hit and trial).
The Droid Sans font does not contain U+2665 BLACK HEART SUIT, so declaring the font is rather irrelevant here. I cannot reproduce the observation in your “PS”, so I’m not trying to explain it.
(A quick way to check character coverage in a font is to download and install the LastResort font. It contains a generic, easily recognizable rendering for all characters, so by using font-family: foo, LastResort on your test text you will quickly find out whether a particular character exists in font “foo”.)
The use of fallback fonts is browser-dependent. Browsers may have settings for this. But the point is that you, as an author, cannot know what happens on other people’s browsers, when your characters cannot be found in the list of fonts you specify (as installed, if installed, in the user’s computer).

What fonts to browse scala source code and render <- and =>

I'm using Eclipse on Windows 7 to browse Scala source code. Scala allows ⇒ to be used instead of =>, and ← instead of <-.
However, I can't seem to find a good clear monospace font that renders the unicode ⇒ and ← well. Often I just get a box instead of ⇒.
I've tried:
Consolas - is the clearest general font, but won't render ⇒
Anonymous Pro - doesn't render ⇒
Deja Vu Sans Mono - does render ⇒, but it is so small it is hard to see!
For my own code, I can just avoid ⇒, but this doesn't help viewing third party library source. How have other folks solved this problem? If other folks are using ⇒ in their code then presumably they also know of a monospace font that also renders it clearly :-)
I think Inconsolata works--but I use Linux, so I'm not sure it will work for you on Windows.
Inconsolata doesn't have the glyph. Your best bet is to take Deja Vu Sans Mono and edit the right-double-arrow glyph to be bigger. I just did it using FontForge on Linux; it took me about 5 minutes.
Here's an example before and after (after is on the bottom):
I unfortunately don't know if they also support Unicode characters and if one of them is to your liking, but give Source Code Pro and Droid Sans Mono a try. I guess you googled already, but this arbitrary list of 42 monospaced fonts might drop some new names nevertheless.
I am personally quite happy with DejaVu Sans Mono, but I agree that the arrow (amongst other characters) is not the nicest.
On a related note: I am always impressed by how well browsers (Firefox in my case) display all kinds of wired Unicode characters. If I remember correctly, then they apply a kind of fallback strategy. That is, if the main font cannot handle a character, a (or multiple) fallback fonts are tried. I don't assume that you can use such a method directly, but searching along those lines might yield other potential solutions to your problem.

Why do Firefox and Chrome render "ಠ_ಠ" (U+0CA0) differently, even if I set both on UTF-8?

The character in question is ಠ (U+0CA0; ಠ). Here are three screenshots:
Chrome 17 for Mac
Firefox 7 for Mac
Firefox > 4 for Windows
All browsers I tried had UTF-8 as encoding. Here it is copy-pasted : ಠ_ಠ, but I have no idea how you are seeing it.
This is probably due to the different platforms and browsers having different default fonts and font implementations.
The font-family on SO is:
Arial,Liberation Sans,DejaVu Sans,sans-serif
So different fonts will apply on different platforms.
In your Mac examples, the different browsers display a "missing" glyph differently - Chrome with a simple square, FF with the hex Unicode of the missing glyph rendered within the box (in this case 0CA0).
The glyph is not covered by the font used by the webbrowser to display the page which is either the browser default font or the font specified by CSS on the page. You need to make sure that you specify a font by CSS which has most likely guaranteed this glyph in all environments. Arial, for example.
This problem is not related to the character encoding used. A problem in the character encoding used would rather have resulted in Mojibake, not in empty boxes or boxes with hexcode representing the Unicode codepoint which basically identify an unavailable glyph in the font used.
What you posted does not contain U+3232 PARENTHESIZED IDEOGRAPH HAVE “㈲” but a three-character sequence U+0CA0 U+005F U+0CA0, i.e. LOW LINE between two KANNADA LETTER TTHA characters. I don’t know what happened and where. Posting a URL might help.
There are differences in rendering across computers due to different font repertoires. The first two renderings in your screenshot indicate lack of glyphs, i.e. no font in the system contains the character U+0CA0.
Firefox and Chrome (unlike IE) tend to scan thru all available fonts to find the character. But rare special characters often have unsatisfactory implementations in fonts, so for best results, check out the list of fonts supporting the character and specify them in your font-family declaration in order of preference. This also helps poor IE to find a suitable font when available.
If the character you want is really U+3232, then check out
http://www.fileformat.info/info/unicode/char/3232/fontsupport.htm
It most probably does not cover all fonts, since new fonts emerge. But for this character, Arial Unicode MS is probably the font that will be used in the great majority of browsing situations – and if it is not available, an indicator of missing glyph is seen.