MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   ePub (https://www.mobileread.com/forums/forumdisplay.php?f=179)
-   -   svg image on top of a background png image (https://www.mobileread.com/forums/showthread.php?t=257125)

roger64 03-11-2015 08:13 AM

svg image on top of a background png image
 
Hi,

I have seen an image on a children's book and I try to reproduce it on an EPUB2. Basically, I use:
- a png image (about 50% percent of its surface is transparent) as a background-image
- a svg image containing two blocks of text which is supposed to display within the transparent part of the background image.

The results so far are not that great. Both images display fine but my problem is that they coincide not perfectly. Could someone have a look at the code of the page?

Spoiler:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="fr-FR" xmlns="http://www.w3.org/1999/xhtml">
<!-- This file was converted to xhtml by Writer2xhtml ver. 1.5.1. See http://writer2latex.sourceforge.net for more info. -->
<head>
<title>Libertine</title>
<link href="styles/styles.css" rel="stylesheet" type="text/css" />
<link href="styles/styles1.css" rel="stylesheet" type="text/css" />

<style type="text/css">
body {
background-image: url(enfant.png);
background-repeat:no-repeat;
margin: 0 0 0 0;
}
</style>
</head>

<body>

<div>
<img alt="Image" src="texte-enfant.svg" style="width:100%" />
</div>
</body>
</html>

RbnJrg 03-11-2015 06:41 PM

Hi Roger;

I didn't the try but I think you must use the background not in "body" but in the div tag, something like:

Code:

<div class="enfant_back">
    <img alt="Image" src="texte-enfant.svg" style="width:100%" />
</div>

and in your .css file:

Code:

.enfant_back {
    background-image: url(enfant.png);
    background-repeat: no-repeat;
    background-size: Npx Mpx;
    margin: 0 0 0 0;
}

where N is the width and M the height of your svg image (or you can try to adjust the svg viewbox to mach the .png image to avoid distortion; in this last case, you also must adjust the position of the svg text).

As I told you, I didn't the try but I think it should work :)

Regards
Rubén

DaleDe 03-11-2015 09:04 PM

If you put the bitmap image inside the SVG itself then any text or vector graphics should be on top of the image assuming you place it within the boundaries of the png file.

Dale

roger64 03-12-2015 01:05 AM

Thank you both for your advices. I had initially a complete svg image with a png inside, but I scrapped it because it was huge and did not display properly. But there may probably have been mistakes on my side.

I am not sure if the use of a background image is such a great idea. The display is very rigid.

I am also trying another idea, putting together the two images within the same container and using a negative margin to make them fit together. It displays nicely on the Calibre editor but poorly on an EPUB. I began with the svg on top of the png, and tried also the other way (png on top), using the property:
Code:

background-color:transparent
.

Anyway, I will follow on trying.

roger64 03-12-2015 10:53 AM

This is to report success using the following solution:

- import the png file (cleaned previously with Gimp) into Inkscape and link it (not incorporate)
- fill up the text part with the Text tool of Inkscape.
- save the svg file as a simple svg
- import both images in the EPUB
- insert the svg file in the xhtml file
- declare the new position of the png file within the svg file

... and that's it.

Pretty convenient! Thanks for your help!!


but one thing: the png image does not display on the EPUB!! However if I import the EPUB in Calibre and then convert it to PDF using Jellby's plugin, I get a perfect PDF with text and image...

There is probably an easy explanation...

roger64 04-20-2015 12:08 PM

1 Attachment(s)
Hi

I resumed my trials and got some advice. I am still not here... :(

1. - Take a png image. Import it to Inkscape as a link, write some lines of text around the drawing, save the text as a plain svg. The image and the text display fine with Inkscape.

2. - Import both png image and svg text in the joint EPUB2. I made sure the svg text has the link to the png image within the EPUB2.

3. - Here are my two best trials:
- a common svg wrapper: the text is well displayed, the png image is missing
- a svg file using an object tag. Text and image are well displayed but the whole is way too big and I cannot scale it.

Using svg inline was no better. Using an image tag was worse.

Here are the files for you to look at. If somebody manages to get a fine display, I'll be very happy to learn how to do it and to understand what I missed.

Note: the used font is Linux Libertine O but it has not been embedded. Do not take into account the display of the font.

Copyright: text from Charles Perrault (died 1703), image from Arthur Rackham (died 1939).

jbacelar 04-20-2015 03:59 PM

1 Attachment(s)
An example of what I use in such cases.

roger64 04-20-2015 05:06 PM

Thank you for the demo which saves me a lot of time and hats off! I will study it carefully and try to repeat it. :thanks:

RbnJrg 04-20-2015 05:54 PM

By chance Roger, did you see these posts?:

https://www.mobileread.com/forums/sho...88&postcount=1

https://www.mobileread.com/forums/sho...87&postcount=5

Maybe you don't need to use a .svg image; just the background .png and normal text.

roger64 04-20-2015 06:15 PM

Quote:

Originally Posted by RbnJrg (Post 3087141)
By chance Roger, did you see these posts?.

Thanks Rubén for the links that I did not see previously. You are right, there are several possible solutions but I have rather use svg than background images.
My problem today was the link to the png image. If I integrated it within Inkscape, the size of the image increased too much. But as a link, it's OK and very precise.

dgatwood 04-22-2015 01:14 AM

Quote:

Originally Posted by roger64 (Post 3062771)
Thank you both for your advices. I had initially a complete svg image with a png inside, but I scrapped it because it was huge and did not display properly. But there may probably have been mistakes on my side.

Don't include the PNG image itself in the SVG. Reference the image with an svg image tag. That puts you in control over the exact dimensions of the PNG within the SVG viewport.

roger64 04-22-2015 10:42 AM

Quote:

Originally Posted by dgatwood (Post 3087935)
Don't include the PNG image itself in the SVG. Reference the image with an svg image tag. That puts you in control over the exact dimensions of the PNG within the SVG viewport.

Thanks for the tip. Is your proposal something different of what jbacelar proposed in his solution?

If you have something different in mind, would you care writing what you mean? One small example would be immensely useful. Sorry to bother you with this.

I have a question about embedded files : Inkscape produces svg text files with lots of bells and whistles. On the other hand, these plain svg files do recognize correctly the embedded font in the EPUB.

To get the same result with the basic text file I've just written, I have the feeling that I would need to add something else than the font-family element which is within the text tag of plain svg files.

jbacelar 04-22-2015 01:16 PM

2 Attachment(s)
Quote:

Originally Posted by roger64 (Post 3088186)
To get the same result with the basic text file I've just written, I have the feeling that I would need to add something else than the font-family element which is within the text tag of plain svg files.

The essai with embedded font.

roger64 04-22-2015 03:05 PM

@jbacelar

Your solution is pretty handy, easy to implement (using snippets) and very precise.

Again, thank you for a clear proof on how to do it. As I had used before a "plain svg" file produced by Inkscape, I noticed that Inkscape kept repeating the font-family name for each line of text. And I thought that THIS was necessary for svg text files. Another wrong idea discarded.

:thanks:

Also I thought wrongly that it was necessary to write the text on the transparent part of a png. I did some more tries, and I realize now that it works as well with plain jpg files.

Does the size of the viewbox concerns only the size of the image or the intended size of the svg as a whole (I mean including text space)?

jbacelar 04-22-2015 03:30 PM

You're welcome.

Quote:

Originally Posted by roger64 (Post 3088340)
Does the size of the viewbox concerns only the size of the image or the intended size of the svg as a whole (I mean including text space)?

The viewBox is applied to all the svg, inside we will have shapes, images, text, etc.

I recommend this interesting direction:
http://commons.oreilly.com/wiki/inde...ls/Coordinates

For me it has been a great help.

roger64 04-22-2015 04:01 PM

Thanks for the link and reply. :thanks:

I had some trouble producing Prince PDF from an EPUB file without embedded font, until I realized that the PDF was using a different font than the one on my Linux, a font whose metrics were slightly larger, and of course, it spoilt everything... With your last EPUB, everything is fine.

To provide users with a consistent display on all kinds of supports, I think it should be a good practice to embed a font.

Again thanks for your help and demo. :thumbsup:

RbnJrg 04-22-2015 06:45 PM

1 Attachment(s)
Hi Roger and Jbacelar;

Sorry but I'm affraid the the embedded font, as is implemented, it won't work on a .svg file. In fact, in the essai.epub that Jbacelar attached the font doesn't work. In order that an embedded font works on a svg file, you must add -in the svg statements, the following definition:

Code:

    <defs>
      <style type="text/css">
        <![CDATA[
          @font-face {
              font-family: "Charis";
              src: url("../Fonts/Charis-SIL2.ttf");
          }
        ]]>
      </style>
    </defs>

You should have in your .css file the following:

Code:

@font-face {
    font-family: "Charis";
    src: url("../Fonts/Charis-SIL2.ttf");
}

body {
    font-family: "Charis"; /* this only is neccesary if you want the embbed font for all the text in your ebook; if you only want the embedded font in your .svg, then don't include this declaration */
}

and in your .xhtml file:

Spoiler:

Code:

<body>
<div>
  <svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 578 740" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
      <style type="text/css">
        <![CDATA[
          @font-face {
              font-family: "Charis";
              src: url("../Fonts/Charis-SIL2.ttf");
          }
        ]]>
      </style>
    </defs>

    <image height="670" width="578" x="25" xlink:href="../Images/arbre2.png"/>

    <text font-family="Charis" style="font-size: 24px">
      <tspan x="20" y="250">Ainsi il avançait toujours sans</tspan>

      <tspan x="20" y="285">rencontrer le moindre être vivant jusqu’à ce</tspan>

      <tspan x="20" y="320">que dans la grande cour d’honneur, il se</tspan>

      <tspan x="20" y="355">trouva en face d’un soldat appuyé sur sa</tspan>

      <tspan x="20" y="390">lance et la tête penchée la poitrine. Au</tspan>

      <tspan x="20" y="425">premier moment, le Prince pensa qu’il</tspan>

      <tspan x="20" y="460">ne vivait plus, il fut vite détrompé car</tspan>

      <tspan x="20" y="495">ses joues étaient fraîches et rouges;</tspan>

      <tspan x="20" y="530">il n'était qu'endormi.</tspan>

      <tspan x="20" y="565">Il y avait dans cette cour</tspan>

      <tspan x="20" y="600">d’autres sentinelles aussi</tspan>

      <tspan x="20" y="640">immobiles et muettes, une rangée de hallebardiers</tspan>

      <tspan x="20" y="675">s’appuyaient contre un mur, et, devant eux, étendu</tspan>

      <tspan x="20" y="710">sur le gazon, le sergent, qui les commandait</tspan>
    </text>
  </svg>
</div>
</body>



As you can see, the text should be written by using the tags <tspan></tspan> and in the <text> tag must be defined the styles of the font (font-size; font-family; font-color; font-weight; etc., etc.). The positioning of the text must be defined with <tspan> but font-styles must be defined once, at the beginning, with the <text> tag.

I attach a new essai.epub so you can study the code better.

Regards.
Rubén

UPDATE: if you want to convert the .epub in .azw3 you should replace:

Code:

<text font-family="Charis" style="font-size: 24px">
with

Code:

<text font-family="Charis" font-size="24">
In fact, this last form works for both, .epub and .azw3 so should be preferable to use this sintax. And I think that is better to use a font-size of 22 instead of 24 (to me, it looks better).

jbacelar 04-22-2015 07:08 PM

2 Attachment(s)
Quote:

Originally Posted by RbnJrg (Post 3088461)
In fact, in the essai.epub that Jbacelar attached the font doesn't work.

My epub works perfectly in ADE (2 and 4) and Calibre viewer.

View attachments, and all the covers (V.2) of epubs I uploaded here, in MobileRead.

RbnJrg 04-22-2015 08:29 PM

Quote:

Originally Posted by jbacelar (Post 3088476)
My epub works perfectly in ADE (2 and 4) and Calibre viewer.

View attachments, and all the covers (V.2) of epubs I uploaded here, in MobileRead.

Hi Jbacelar:

I don't dispute that you can see the font on those programs. But if you open your .epub with -for example- Sigil or with Kindle Previewer; you won't be able to see the font Charis SIL.

The correct way to add a custom font (in a .svg image) is as I said in my previous post :) Also you can read this old post mine:

https://www.mobileread.com/forums/sho...51&postcount=9

But is not my invention that code :) Please, read this:

http://graphicdesign.stackexchange.c...age-on-my-site

and:

http://nimbupani.com/about-fonts-in-svg.html

Of course; you always can consult:

http://www.w3.org/TR/SVG/fonts.html#SVGFontsOverview

By using <defs> inside <svg> tags you can be sure that your custom font will be showed in any reader that supports svg.

Regards

UPDATE: Please, use another font instead of Charis (for example "Fontin") and tell me if your ADE can show it with your method; my ADE can't. Like Charis is similar to Times, maybe you are watching Times in ADE and not Charis; for this reason I ask you do the try with a font like "Fontin".

jbacelar 04-23-2015 04:32 AM

3 Attachment(s)
Quote:

Originally Posted by RbnJrg (Post 3088504)
Hi Jbacelar:

I don't dispute that you can see the font on those programs. But if you open your .epub with -for example- Sigil or with Kindle Previewer; you won't be able to see the font Charis SIL.

UPDATE: Please, use another font instead of Charis (for example "Fontin") and tell me if your ADE can show it with your method; my ADE can't. Like Charis is similar to Times, maybe you are watching Times in ADE and not Charis; for this reason I ask you do the try with a font like "Fontin".


Well, I do not dispute your solution is pure orthodoxy.
But:
1- I insist, mine works perfectly in ADE (see attached).
2- The font CharisSil retouched by JSWolf, is perfectly distinct from the Times font.
3- Please! Kindle Previewer transforms previously epubs in mobis, and we are here in the epub subforum.
4- Currently, for epubs, ¿The orthodoxy is not ADE? :)

roger64 04-23-2015 07:20 AM

Hi

This tired soldier of mine got a lot of powerful assistance. :)

Thanks to both of you for your kind help. Before this thread I did not manage to display it properly and I learnt a lot of useful things along the way.

Doitsu 04-23-2015 07:32 AM

Quote:

Originally Posted by jbacelar (Post 3088650)
1- I insist, mine works perfectly in ADE (see attached).

RbnJrg is right, your code only works because Charis SIL is installed on your computer. If you delete it from your Windows fonts folder, the text will be rendered in a different font.

BTW, here's another working example, that uses the same font referencing method that RbnJrg suggests.

jbacelar 04-23-2015 11:51 AM

Quote:

Originally Posted by Doitsu (Post 3088722)
your code only works because Charis SIL is installed on your computer. If you delete it from your Windows fonts folder, the text will be rendered in a different font.


Doitsu sorry, but that's not true.
In my Windows is not installed Fontin font, and... (see the corresponding attached)
Also in my Pocketbook reader all these tests are fine.

To finish this matter, I now have uninstalled Charis Sil, I have restarted the computer, and I insist: everything works correctly.

Doitsu 04-23-2015 01:01 PM

Quote:

Originally Posted by jbacelar (Post 3088862)
Doitsu sorry, but that's not true.
In my Windows is not installed Fontin font, and... (see the corresponding attached)

Apparently, I missed the updated version (essai with embedded font.epub) that you must have attached sometimes yesterday to this post.

This version does indeed work with all ADE versions.

roger64 04-24-2015 10:39 AM

1 Attachment(s)
@Doitsu

I tried your example above. I added this rectangle above the image tag in cover.xhtml

Code:

<rect x="1" y="1" width="598" height="798"
      style="fill: none; stroke: black;"/>

I could see that last letter of the second word (Magnum) was partly out of the screen. I reduced the font-size in the svg image from 150px to 130px (see joint screenshoot) and it looks OK.

Once corrected, I of course suppress the rectangle which is here only for checking purpose.
Note: inserting the rectangle directly in the svg image gives the same result.

As my experience with svg is quite short, did I do something wrong?

Doitsu 04-24-2015 12:00 PM

Quote:

Originally Posted by roger64 (Post 3089502)
As my experience with svg is quite short, did I do something wrong?

No. AFAIK, everything renders as expected.


All times are GMT -4. The time now is 08:01 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.