-
Notifications
You must be signed in to change notification settings - Fork 662
-
Hi,
I was trying to make a pdf file using braille fonts. I installed in a first step a braille font (which works in e.g. libre office) and tried
the following code
import fitz
font_path = r'C:\Windows\Fonts\Braille_deutsch.ttf'
braille_map = {
"a": "\u2801", "b": "\u2803", "c": "\u2809", "d": "\u2819", "e": "\u2811",
"f": "\u280B", "g": "\u281B", "h": "\u2813", "i": "\u280A", "j": "\u281A",
"k": "\u2805", "l": "\u2807", "m": "\u280D", "n": "\u281D", "o": "\u2815",
"p": "\u280F", "q": "\u281F", "r": "\u2817", "s": "\u280E", "t": "\u281E",
"u": "\u2825", "v": "\u2827", "w": "\u283A", "x": "\u282D", "y": "\u283D",
"z": "\u2835", " ": "\u2800"
}
def text_to_braille(text):
return "".join(braille_map.get(c.lower(), "\u28FF") for c in text)
braille_text = text_to_braille("Hello")
print(braille_text)
doc = fitz.open()
page = doc.new_page()
page.insert_text(
(50, 100),
braille_text,
fontfile=font_path,
fontsize=12,
color=(0, 0, 0)
)
doc.save("hello_braille.pdf")
doc.close()
There is no error message, however the the pdf file does not contain the expected result. There are just 5 filled rectangles.
- Is there a special font to be used or is it possible to make it work with Braille_deutsch
- If it works, is it then also possible to search for Braille letters using unicode like \u2801 or even words und does this depend on the font installed?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
This is simpler than you think. Here is how you can make it work.
braille-test.zip
Beta Was this translation helpful? Give feedback.
All reactions
-
So the visual presentation of Braille characters is taken over by the font.
This works in both directions: write normal text (with e.g. Latin characters) using a Braille font. Then the Braille symbols will appear on the page. If you extract text, a backtranslation will automatically take place to Latin text again.
Searching for text works in the same way: just use the Latin text as the needle.
You do not have to deal with the Braille Unicode values in this scenario at all. You don't even know that the extracted Text has been displayed with Braille symbols. The same is true with every font BTW.
Braille Unicodes are only required if you want to transmit the info "this should be written with a Braille font". Then you can translate (e.g. using your dictionary above) your Latin text into Braille Unicodes.
The receiver would then have to know (or determine from the Unicode range) that he has received Braille text and back-translate to Latin (and potentially again use a Braille font for writing / displaying).
Beta Was this translation helpful? Give feedback.
All reactions
-
many thanks, indeed quit simple.
Suppose I have a pdf with text in braille font and another font like e.g. arial. Additionally I do not know the latin text of the braille text, however I want to find all braille text in the document, mark it e.g. yellow and get the latin text out of it.
Beta Was this translation helpful? Give feedback.
All reactions
-
To get to know which font was used for which text particle ("span"), you must obviously extract the full "metadata" of the text, too.
For this you must use page.get_text("dict") ("rawdict"). Please do see the docs!
This returns a hierarchical dictionary structure. E.g. this script version
import pymupdf from pprint import pp doc = pymupdf.open("braille.pdf") page = doc[0] full_content = page.get_text("dict") spans = [ s for b in full_content["blocks"] if b["type"] == 0 # only look at text blocks for l in b["lines"] for s in l["spans"] ] pp(spans, width=150)
returns this:
[{'size': 20.0,
'flags': 4,
'bidi': 0,
'char_flags': 16,
'font': 'Braille3D',
'color': 0,
'alpha': 255,
'ascender': 0.800000011920929,
'descender': -0.20000000298023224,
'text': 'JUST A BRAILLE TEST',
'origin': (72.0, 72.0),
'bbox': (72.0, 52.040000915527344, 352.4600830078125, 75.87999725341797)},
{'size': 20.0,
'flags': 4,
'bidi': 0,
'char_flags': 16,
'font': 'BrailleNormal',
'color': 0,
'alpha': 255,
'ascender': 0.800000011920929,
'descender': -0.20000000298023224,
'text': 'JUST A BRAILLE TEST',
'origin': (72.0, 102.0),
'bbox': (72.0, 82.04000091552734, 352.4600830078125, 105.87999725341797)}]Beta Was this translation helpful? Give feedback.
All reactions
-
many thanks, that helps
Beta Was this translation helpful? Give feedback.