SVG 在特定位置的 SVG 内缩放

发布于 2024-12-04 15:57:48 字数 726 浏览 2 评论 0原文

我是复杂 SVG 的新手,正在从事一些工作,需要帮​​助。我有几个 SVG 文件,它们已经正确格式化了内容……线条、矩形、文本等。它们是用简单的 X=、Y=、X1=、Y1= 绘制的,并且仅基于整数。最初的 SVG 是为打印而设计的,x/y 位置是根据 300dpi 的打印设置的。

因此,这与来自其他来源的几个 SVG 一起存在,我正在尝试合并到一个新的单个 SVG 文档中。因此,我需要将其中一个元素放置在基于英寸或厘米的位置 (x,y)(根据我到目前为止所读到的内容),但我还需要它们尊重特定的尺寸......比如说2 英寸高,3.4 英寸宽。

由于原始的 SVG 仅基于整数并且没有“英寸”方向,我能做什么......或者,它如何自我缩放。

如果没有正确的 SVG 语法,这里基本上是一些细节。

SVG1 的整体 x/y 矩形面积为 0,0 到 476,100 SVG2 的整体 x/y 矩形区域为 0,0 到 273,24

新的 SVG 需要为 4" x 6"

例如:在向下 1/4"、距顶部 1" 的位置,我需要插入 SVG1 ,即使它是 476x100,也需要缩放到大约 1/2" 高 x 3" 宽的区域。

相似地, 在下2.8英寸、宽1.75英寸的位置,我需要插入SVG2,其尺寸需要大约2英寸高,2.5英寸宽作为最大区域。

可以缩放,但不要倾斜,它们需要保持原始比例而不是被剪裁。如果我能得到基本的理解,我就可以调整最终的尺寸,只是不知道如何获得这项工作的基础设施。

谢谢。

I'm new to complex SVG and working on something and need help. I have a couple of SVG files that are already properly formatted with content.. lines, rectangles, text, etc. They are drawn with simple X=, Y=, X1=, Y1= and based on just whole numbers. The original SVG was designed for printing and the x/y positions were set based on printing at 300dpi.

So this exists with a couple SVG coming from other origins and I'm trying to merge into a new single SVG document. So, one of these elements, I need to put at position (x,y) based on either inches or centimeters (from what I've read so far), but I also need them to respect a specific size of... say 2 in tall, 3.4in wide.

Since the original SVG was based on just whole numbers and no orientation to "inches", what can I do.. or, how can it self-scale.

Without proper SVG syntax, here's basically some of the details.

SVG1 has an overall x/y rectangle area of 0,0 to 476,100
SVG2 has an overall x/y rectangle area of 0,0 to 273,24

The new SVG needs to be 4" by 6"

Ex: at position 1/4" down, 1" across from the top, I need to insert SVG1, and even though it is 476x100, it needs to be scaled into an area about 1/2" tall x 3" wide.

Similarly,
at position 2.8" down, 1.75" across, I need to insert SVG2, and its size needs to be about 2" tall, and 2.5" wide as a maximum area.

Scaled yes, but not to be skewed, they need to keep their original proportions and not clipped. If I can get the basic understanding, I can tweak the final dimensions, just don't know how to get the infrastructure of this working.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-12-11 15:57:49

经过多次尝试后,我终于得到了它,以防万一有人对 SVG 感兴趣并且像我一样对 SVG 比较陌生。正如问题中一样,我有一些预先生成的 SVG 输出文件,它们的 X、Y 高度、宽度设置均基于数值,没有英寸、厘米等上下文,但我的要求是适合给定的 X英寸到 Y 英寸范围。

所以,我发现了“defs”标签,这就像声明一个变量,稍后可以在稍后的 SVG 主体中用作“把那个东西放在这里”。在 SVG 的顶部,我能够给出我需要的尺寸。然后,通过使用“g”标签进行分组,我能够以数字方式将某些内容定位到给定的 x,y 位置。然后,在其中,我做了另一个“g”来应用“defs”部分中声明的“变量”的缩放(因为“g”元素中不能有两个“transform”标签)。

我的想法如下,希望详细的评论能够帮助其他人进行 SVG 的研究。

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
        "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

    <!-- Explicitly define the SVG window area as 4 inches by 6 inches -->
    <svg xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         width="4in" height="6in" >

    <!-- Add some styles, fonts etc for line drawing, labels and data -->
    <style type="text/css" >
        <![CDATA[

            line.Black
            {   stroke:RGB(0,0,0);
                stroke-width:2;
            }

            text.Hdr
            { 
                font-family: Arial;
                font-size:x-small;
                stroke: #000000;
                stroke-width:.4;
            }

            text.Data
            {
                font-family: Courier New;
                font-size:normal;
                stroke: #000000;
                stroke-width:.5;
            }
        ]]>
    </style>


    <!-- all these "defs" are zero-based position for their own content
        and will be speicifcally placed where needed via "g" tags.
        The simple "id" name will be used as an "insert <something> here" -->
    <defs>
        <!-- Below will come from actual data from system
            The "ID" is what becomes the "variable" that is later
            used later in the SVG as the "put me here" -->
        <g id="DataOneElement" >
            <text class="Data">SOME TEXT DATA</text>
        </g>

        <!-- This partial linear barcode generated somewhere else 
            Notice these are just integer positions, and nothing
            to do with specific "inches" measurements.  Also, they
            start at position 0,0 and go however large they need.
            When applied with the "g" positioning, thats where it
            starts, then gets scaled from there if needed bigger/smaller -->
        <g id="DataPartialBarCode" >
            <rect x="0" y="0" width="1" height="50" />
            <rect x="4" y="0" width="1" height="50" />
            <rect x="6" y="0" width="3" height="50" />
            <rect x="10" y="0" width="3" height="50" />
            <rect x="14" y="0" width="1" height="50" />
            <rect x="16" y="0" width="3" height="50" />
            <rect x="20" y="0" width="3" height="50" />
            <rect x="24" y="0" width="1" height="50" />
            <rect x="26" y="0" width="1" height="50" />
            <rect x="30" y="0" width="1" height="50" />
            <rect x="32" y="0" width="1" height="50" />
            <rect x="34" y="0" width="1" height="50" />
            <rect x="38" y="0" width="3" height="50" />
        </g>

        <!-- Actual data generated from AMS to populate these too.
            Notice here too, the entire address starts as position 0,0 -->
        <g id="SampleAddress" >
            <text class="Data" x="0" y="0">Some Person's Name</text>
            <text class="Data" x="0" y="17">First Address Line</text>
            <text class="Data" x="0" y="30">Another Address</text>
            <text class="Data" x="0" y="43">3rd Address line</text>
            <text class="Data" x="0" y="56">And Testing for longer address content</text>
        </g>

        <!-- another bar code that will generated -->
        <g id="AnotherBarCode" >
            <rect x="0" y="0" width="1" height="70" />
            <rect x="4" y="0" width="1" height="70" />
            <rect x="6" y="0" width="3" height="70" />
            <rect x="10" y="0" width="3" height="70" />
            <rect x="14" y="0" width="1" height="70" />
            <rect x="16" y="0" width="3" height="70" />
            <rect x="20" y="0" width="1" height="70" />
            <rect x="24" y="0" width="1" height="70" />
            <rect x="26" y="0" width="1" height="70" />
            <rect x="28" y="0" width="3" height="70" />
            <rect x="32" y="0" width="1" height="70" />
            <rect x="36" y="0" width="1" height="70" />
            <rect x="38" y="0" width="3" height="70" />
            <rect x="42" y="0" width="3" height="70" />
            <rect x="46" y="0" width="1" height="70" />
        </g>
    </defs>

    <!-- Now, starting the drawing of the SVG...
        Border around entire box drawing area 
        Notice these are in specific INCH dimensions... -->
    <line class="Black" x1="0in" y1="0in" x2="4in" y2="0in" />
    <line class="Black" x1="0in" y1="0in" x2="0in" y2="6in" />
    <line class="Black" x1="4in" y1="0in" x2="4in" y2="6in" />
    <line class="Black" x1="0in" y1="6in" x2="4in" y2="6in" />


    <!-- Translate is Across then Down from the top/left corner of SVG -->
    <!-- Translate is NOT based on inch, cm, or other measurements
         so you may have to tweak these numbers -->
    <g transform="translate( 100 20 ) ">
        <!-- Now, take whatever we are providing and scale it within the area.
            In this case, scale the ENTIRE "g" object to 1.5 its original size -->
        <g transform="scale(1.75)">
            <!-- This is where the "defs" variable declaration comes 
                in, as looking at the "g" tag by the ID name -->
            <use xlink:href="#DataOneElement" />
        </g>
    </g>

    <!-- and now the partial barcode "defs" variable -->
    <g transform="translate( 20 23 ) ">
        <!-- In this case, scale the width by 115% and the height by 95% -->
        <g transform="scale( 1.15 .95 )">
            <use xlink:href="#DataPartialBarCode" />
        </g>
    </g>


    <!-- Any other specific lines within the area -->
    <line class="Black" x1="0in" y1=".8in" x2="4in" y2=".8in" />

    <!-- Now, just insert the "defs" from above at a scale that will still be readable.
        Cool thing, the entire address is considered a single element here. -->
    <g transform="translate(20 97)">
        <g transform="scale(.7)">
            <use xlink:href="#SampleAddress" />
        </g>
    </g>


    <!-- We can even show the address AGAIN, scaled differently elsewhere. -->
    <g transform="translate(2 250)">
        <g transform="scale(1.3)">
            <use xlink:href="#SampleAddress" />
        </g>
    </g>

    <!-- Another line and then barcode-->
    <line class="Black" x1="0in" y1="1.55in" x2="4in" y2="1.55in" />

    <g transform="translate( 175 175 ) ">
        <!-- Scale this barcode 100% wide, but only 70% height -->
        <g transform="scale(1 .7)">
            <use xlink:href="#AnotherBarCode" />
        </g>
    </g>
</svg>

I finally got it after much playing around, just in case anyone is interested and relatively new with SVG as I am. As in the question, I had some pre-generated SVG output files that have their X,Y Height,Width settings all based on numeric value, with no context to inch, centimeter, etc. but my requirement was to fit into a given X inch by Y inch range.

So, I found out about "defs" tag, which is like declaring a variable that can later be used as a "put that thing HERE" within the later SVG body. At the TOP of the SVG, I was able to give the dimensions I needed. Then, by using the "g" tag for grouping, I am able to numerically position something to a given x,y position. Then, within that, I did another "g" to apply a scaling of the "variable" as declared in the "defs" section (as a "g" element can not have two "transform" tags within it).

What I came up with was something like below, and hope the detailed comments can help others in their research dealing with SVG.

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
        "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

    <!-- Explicitly define the SVG window area as 4 inches by 6 inches -->
    <svg xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         width="4in" height="6in" >

    <!-- Add some styles, fonts etc for line drawing, labels and data -->
    <style type="text/css" >
        <![CDATA[

            line.Black
            {   stroke:RGB(0,0,0);
                stroke-width:2;
            }

            text.Hdr
            { 
                font-family: Arial;
                font-size:x-small;
                stroke: #000000;
                stroke-width:.4;
            }

            text.Data
            {
                font-family: Courier New;
                font-size:normal;
                stroke: #000000;
                stroke-width:.5;
            }
        ]]>
    </style>


    <!-- all these "defs" are zero-based position for their own content
        and will be speicifcally placed where needed via "g" tags.
        The simple "id" name will be used as an "insert <something> here" -->
    <defs>
        <!-- Below will come from actual data from system
            The "ID" is what becomes the "variable" that is later
            used later in the SVG as the "put me here" -->
        <g id="DataOneElement" >
            <text class="Data">SOME TEXT DATA</text>
        </g>

        <!-- This partial linear barcode generated somewhere else 
            Notice these are just integer positions, and nothing
            to do with specific "inches" measurements.  Also, they
            start at position 0,0 and go however large they need.
            When applied with the "g" positioning, thats where it
            starts, then gets scaled from there if needed bigger/smaller -->
        <g id="DataPartialBarCode" >
            <rect x="0" y="0" width="1" height="50" />
            <rect x="4" y="0" width="1" height="50" />
            <rect x="6" y="0" width="3" height="50" />
            <rect x="10" y="0" width="3" height="50" />
            <rect x="14" y="0" width="1" height="50" />
            <rect x="16" y="0" width="3" height="50" />
            <rect x="20" y="0" width="3" height="50" />
            <rect x="24" y="0" width="1" height="50" />
            <rect x="26" y="0" width="1" height="50" />
            <rect x="30" y="0" width="1" height="50" />
            <rect x="32" y="0" width="1" height="50" />
            <rect x="34" y="0" width="1" height="50" />
            <rect x="38" y="0" width="3" height="50" />
        </g>

        <!-- Actual data generated from AMS to populate these too.
            Notice here too, the entire address starts as position 0,0 -->
        <g id="SampleAddress" >
            <text class="Data" x="0" y="0">Some Person's Name</text>
            <text class="Data" x="0" y="17">First Address Line</text>
            <text class="Data" x="0" y="30">Another Address</text>
            <text class="Data" x="0" y="43">3rd Address line</text>
            <text class="Data" x="0" y="56">And Testing for longer address content</text>
        </g>

        <!-- another bar code that will generated -->
        <g id="AnotherBarCode" >
            <rect x="0" y="0" width="1" height="70" />
            <rect x="4" y="0" width="1" height="70" />
            <rect x="6" y="0" width="3" height="70" />
            <rect x="10" y="0" width="3" height="70" />
            <rect x="14" y="0" width="1" height="70" />
            <rect x="16" y="0" width="3" height="70" />
            <rect x="20" y="0" width="1" height="70" />
            <rect x="24" y="0" width="1" height="70" />
            <rect x="26" y="0" width="1" height="70" />
            <rect x="28" y="0" width="3" height="70" />
            <rect x="32" y="0" width="1" height="70" />
            <rect x="36" y="0" width="1" height="70" />
            <rect x="38" y="0" width="3" height="70" />
            <rect x="42" y="0" width="3" height="70" />
            <rect x="46" y="0" width="1" height="70" />
        </g>
    </defs>

    <!-- Now, starting the drawing of the SVG...
        Border around entire box drawing area 
        Notice these are in specific INCH dimensions... -->
    <line class="Black" x1="0in" y1="0in" x2="4in" y2="0in" />
    <line class="Black" x1="0in" y1="0in" x2="0in" y2="6in" />
    <line class="Black" x1="4in" y1="0in" x2="4in" y2="6in" />
    <line class="Black" x1="0in" y1="6in" x2="4in" y2="6in" />


    <!-- Translate is Across then Down from the top/left corner of SVG -->
    <!-- Translate is NOT based on inch, cm, or other measurements
         so you may have to tweak these numbers -->
    <g transform="translate( 100 20 ) ">
        <!-- Now, take whatever we are providing and scale it within the area.
            In this case, scale the ENTIRE "g" object to 1.5 its original size -->
        <g transform="scale(1.75)">
            <!-- This is where the "defs" variable declaration comes 
                in, as looking at the "g" tag by the ID name -->
            <use xlink:href="#DataOneElement" />
        </g>
    </g>

    <!-- and now the partial barcode "defs" variable -->
    <g transform="translate( 20 23 ) ">
        <!-- In this case, scale the width by 115% and the height by 95% -->
        <g transform="scale( 1.15 .95 )">
            <use xlink:href="#DataPartialBarCode" />
        </g>
    </g>


    <!-- Any other specific lines within the area -->
    <line class="Black" x1="0in" y1=".8in" x2="4in" y2=".8in" />

    <!-- Now, just insert the "defs" from above at a scale that will still be readable.
        Cool thing, the entire address is considered a single element here. -->
    <g transform="translate(20 97)">
        <g transform="scale(.7)">
            <use xlink:href="#SampleAddress" />
        </g>
    </g>


    <!-- We can even show the address AGAIN, scaled differently elsewhere. -->
    <g transform="translate(2 250)">
        <g transform="scale(1.3)">
            <use xlink:href="#SampleAddress" />
        </g>
    </g>

    <!-- Another line and then barcode-->
    <line class="Black" x1="0in" y1="1.55in" x2="4in" y2="1.55in" />

    <g transform="translate( 175 175 ) ">
        <!-- Scale this barcode 100% wide, but only 70% height -->
        <g transform="scale(1 .7)">
            <use xlink:href="#AnotherBarCode" />
        </g>
    </g>
</svg>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文