- 
  Notifications
 
You must be signed in to change notification settings  - Fork 661
 
How to : create a ink annotation from a string #1983
-
Hello,
I am trying to convert a string to an ink annotation using a a font.
Why :
- Because it is not possible to use any font with free text
 - Because stamps annotation does not support image (otherwise I would create an image from text
 
I created a SVG from text using Cairo :
with cairo.SVGSurface(svg_file_path, 200, 50) as surface:
 # creating a cairo context object for SVG surface
 # using Context method
 Context = cairo.Context(surface)
 # setting color of the context
 Context.set_source_rgb(1, 0, 0)
 # approximate text height
 Context.set_font_size(30)
 # Font Style
 Context.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
 # position for the text
 Context.move_to(35, 45)
 # displays the text
 Context.text_path("Lorem ipsum")
 # Width of outline
 Context.set_line_width(1)
 # stroke out the color and width property
 Context.fill()
Then I get the SVG path to create a list of list of points :D (as described here https://pymupdf.readthedocs.io/en/latest/recipes-annotations.html#how-to-use-ink-annotations) and I used annot = page.add_ink_annot(all_pixels_svg).
But at the end I have a stroke SVG.
pdf_signed_for_proto_annotations_annot_pdf 
Do you have any suggestion, others solutions ?
My goal is to write like an ink signature from a given string.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 4 comments 2 replies
-
Interesting idea!
But let me convert this to a "Discussion" first - this is where this content belongs to.
Beta Was this translation helpful? Give feedback.
All reactions
- 
 
👍 1 
-
I am not sure where to look at the moment, but all fonts in the end compose their characters (glyphs actually) from Bézier curves.
These are mapping from real numbers in [0, 1] to points in the plane - something like f(t) = fitz.Point(xt, yt). These mappings in fact / in practice are polynomials of degree 2 or at most 3.
So if you were able to get hold of the (set of) Bézier curve(s) defining some e.g. letter "G" in some given font, you should be able to represent any text in that font as an ink annotation.
Another idea:
- write some text to a temp PDF page with a font you like
 - make an SVG from that page: 
svg_string = page.get_svg_image(). By default, that text will be represented as "paths" - nothing else than a bunch of Bézier curves. So you avoid the extra effort to interface with other stuff. 
doc=fitz.open() page=doc.new_page() page.insert_text((0,20),"Hello") 1 svg_string=page.get_svg_image() print(svg_string) <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="595pt" height="842pt" viewBox="0 0 595 842"> <defs> <path id="font_1_41" d="M.5509949 .33198548V0H.6439972V.72898867H.5509949V.4139862H.17599488V.72898867H.08299255V0H.17599488V.33198548H.5509949Z"/> <path id="font_1_70" d="M.5130005 .23399353C.5130005 .31399537 .5069885 .3619995 .49198915 .40098573 .45799256 .4869995 .37799073 .5389862 .27999879 .5389862 .13398743 .5389862 .039993287 .42799378 .039993287 .25498963 .039993287 .08198547 .12998963-.023010254 .27799989-.023010254 .397995-.023010254 .48098756 .04499817 .5019989 .15899658H.41799928C.394989 .08999634 .34799195 .053985597 .2809906 .053985597 .22799683 .053985597 .18299866 .07798767 .15499878 .12199402 .1349945 .1519928 .12799073 .18199158 .1269989 .23399353H.5130005M.1289978 .3019867C.13598633 .39898683 .19499207 .46199037 .2789917 .46199037 .3639984 .46199037 .4229889 .3959961 .4229889 .3019867H.1289978Z"/> <path id="font_1_77" d="M.1519928 .72898867H.067993167V0H.1519928V.72898867Z"/> <path id="font_1_80" d="M.27198792 .5389862C.12399292 .5389862 .035995485 .43399049 .035995485 .2579956 .035995485 .08099365 .12399292-.023010254 .272995-.023010254 .42099-.023010254 .5099945 .08198547 .5099945 .2539978 .5099945 .43598939 .42399598 .5389862 .27198792 .5389862M.272995 .46199037C.36698915 .46199037 .4229889 .3849945 .4229889 .25498963 .4229889 .1309967 .36499024 .053985597 .272995 .053985597 .17999268 .053985597 .12298584 .1309967 .12298584 .2579956 .12298584 .3849945 .17999268 .46199037 .272995 .46199037Z"/> </defs> <use data-text="H" xlink:href="#font_1_41" transform="matrix(11,0,0,-11,0,20)"/> <use data-text="e" xlink:href="#font_1_70" transform="matrix(11,0,0,-11,7.942001,20)"/> <use data-text="l" xlink:href="#font_1_77" transform="matrix(11,0,0,-11,14.0580019,20)"/> <use data-text="l" xlink:href="#font_1_77" transform="matrix(11,0,0,-11,16.500002,20)"/> <use data-text="o" xlink:href="#font_1_80" transform="matrix(11,0,0,-11,18.942002,20)"/> </svg>
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi @JorjMcKie
Thank you for your reply and your detailed messages.
Also thank you for doing the role of maintainer of this great tool.
I tried the second idea. I get the same results than with my Cairo solution, except that I am not using one more dependencies.
I did not tried the first one because I did not understand :D completely how to do it :/.
Do you know if it will be possible to add stamp annotation by providing a picture or any kind of annotation where I can have as input : string + font or image ? Because I can create an image with a text + font.
Beta Was this translation helpful? Give feedback.
All reactions
-
Do you know if it will be possible to add stamp annotation by providing a picture or any kind of annotation where I can have as input : string + font or image ? Because I can create an image with a text + font.
While this is possible by the PDF spec, (Py-) MuPDF (like most, if not all free other tools) does not yet support it.
I did not tried the first one because I did not understand :D completely how to do it :/.
This a variation of the SVG alternative: Instead of back-translating the SVG source, it would mean to directly use the equivalent instructions contained in every font.
SVG doesn't do any different than defining a set of Bézier functions to represent the lines and curves that represent a particular character.
MuPDF's internal C code is able to work its way into the font's binary, where those functions can be found.
But anyway, I understood you would prefer a way to directly display an image via a Stamp annot.
Beta Was this translation helpful? Give feedback.
All reactions
-
it is possible to have a stamp type annotation where to put an image for us and not one of the predefined values for the stamp
Beta Was this translation helpful? Give feedback.
All reactions
-
No, sorry. As mentioned before, no can do.
Beta Was this translation helpful? Give feedback.