標準25碼 Barcode 25

Code25 碼(標準 25 碼)
Interleaved 2 of 5

Code25 計算 2of5i.xsl
<?xml version="1.0" encoding="UTF-8"?>
<!-- ========================================================= -->
<!-- -->
<!-- (c) 2003, RenderX -->
<!-- -->
<!-- Author: Alexander Peshkov <[email protected]> -->
<!-- -->
<!-- Permission is granted to use this document, copy and -->
<!-- modify free of charge, provided that every derived work -->
<!-- bear a reference to the present document. -->
<!-- -->
<!-- This document contains a computer program written in -->
<!-- XSL Transformations Language. It is published with no -->
<!-- warranty of any kind about its usability, as a mere -->
<!-- example of XSL technology. RenderX shall not be -->
<!-- considered liable for any damage or loss of data caused -->
<!-- by use of this program. -->
<!-- -->
<!-- ========================================================= -->

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="codabar-data">

<!-- ========================================================= -->
<!-- This stylesheet contains named template -->
<!-- (barcode-2of5i) that encodes sequence of digits using -->
<!-- 2-of-5 interleaved barcode scheme, with no check digit. -->
<!-- -->
<!-- Mandatory parameters are: -->
<!-- -->
<!-- "value" - a string to encode; can contain -->
<!-- only decimal digits -->
<!-- -->
<!-- Optional parameters are: -->
<!-- -->
<!-- "string" - a human readable string; -->
<!-- represents data encoded in -->
<!-- the barcode in a human-readable form -->
<!-- Optional parameter. -->
<!-- Default is: 'value' with checksum -->
<!-- and non-significant null added when -->
<!-- necessary. -->
<!-- "print-text" - boolean, defines if a human -->
<!-- readable label should be printed. -->
<!-- Default is: 'true'. -->
<!-- "addchecksum" - boolean, defines if checksum should -->
<!-- be added by the barcode generator; -->
<!-- Default is 'false' -->
<!-- "module" - width of the elementary unit -->
<!-- bar/space; -->
<!-- Default is 0.012in -->
<!-- "wide-to-narrow" - width ratio for bars/spaces; -->
<!-- Default is 3.0 -->
<!-- "height" - pattern height (= bar length); -->
<!-- Default is 0.25in -->
<!-- "quiet-horizontal" - quiet zone horizontal margin width; -->
<!-- Default is 0.24in -->
<!-- "quiet-vertical" - quiet zone vertical margin width; -->
<!-- Default is 0.06in -->
<!-- "font-family" - a font family used to print textual -->
<!-- representation of a barcode; -->
<!-- Default is 'Courier' -->
<!-- "font-height" - a height of the font used to print -->
<!-- textual representation of a barcode. -->
<!-- Default is 8pt -->
<!-- ========================================================= -->

<xsl:import href="2of5i-svg.xsl"/>

<xsl:output method="xml"
version="1.0"
indent="yes"/>

<xsl:param name="value"/>
<xsl:param name="string"/>
<xsl:param name="print-text" select="'true'"/>
<xsl:param name="addchecksum" select="'false'"/>
<xsl:param name="module" select="'0.012in'"/>
<xsl:param name="wide-to-narrow" select="3.0"/>
<xsl:param name="height" select="'0.25in'"/>
<xsl:param name="quiet-horizontal" select="'0.24in'"/>
<xsl:param name="quiet-vertical" select="'0.06in'"/>
<xsl:param name="font-family" select="'Courier'"/>
<xsl:param name="font-height" select="'8pt'"/>

<!--
Driver template used for command line processing, an analog of 'Main' function in C
-->
<xsl:template match="/">
<xsl:call-template name="barcode-2of5i">
<xsl:with-param name="value" select="$value"/>
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="print-text" select="$print-text"/>
<xsl:with-param name="addchecksum" select="$addchecksum"/>
<xsl:with-param name="module" select="$module"/>
<xsl:with-param name="wide-to-narrow" select="$wide-to-narrow"/>
<xsl:with-param name="height" select="$height"/>
<xsl:with-param name="quiet-horizontal" select="$quiet-horizontal"/>
<xsl:with-param name="quiet-vertical" select="$quiet-vertical"/>
<xsl:with-param name="font-family" select="$font-family"/>
<xsl:with-param name="font-height" select="$font-height"/>
</xsl:call-template>
</xsl:template>

<!-- Main template used to create a barcode -->
<xsl:template name="barcode-2of5i">
<xsl:param name="value"/>
<xsl:param name="string"/>
<xsl:param name="print-text" select="'true'"/>
<xsl:param name="addchecksum" select="'false'"/>
<xsl:param name="module" select="'0.012in'"/>
<xsl:param name="wide-to-narrow" select="3.0"/>
<xsl:param name="height" select="'0.25in'"/>
<xsl:param name="quiet-horizontal" select="'0.24in'"/>
<xsl:param name="quiet-vertical" select="'0.06in'"/>
<xsl:param name="font-family" select="'Courier'"/>
<xsl:param name="font-height" select="'8pt'"/>

<!-- Check data consistency -->
<xsl:if test="string-length($value)=0">
<xsl:message terminate="yes">Error: 'value' is not specified.</xsl:message>
</xsl:if>
<xsl:if test="string-length(translate($value,'1234567890',''))!=0">
<xsl:message terminate="no">Error: unexpected characters in 'value'.
<xsl:value-of select="$value"/> /
</xsl:message>
</xsl:if>

<!-- Add checksum if required. -->
<!--
Source string must have even number of digits, justify it with leading '0' if necessary
-->
<xsl:variable name="value-real">
<xsl:if test="((string-length($value) + ($addchecksum='true')) mod 2) != 0">0</xsl:if>
<xsl:value-of select="$value"/>
<xsl:if test="$addchecksum='true'">
<xsl:call-template name="makeChecksum">
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:if>
</xsl:variable>
<!--
Add strart/stop symbols and prepare actual barcode
-->
<xsl:variable name="value-encoded">
<xsl:value-of select="'0000'"/>
<xsl:call-template name="recursive-coder">
<xsl:with-param name="value" select="$value-real"/>
</xsl:call-template>
<xsl:value-of select="'100'"/>
</xsl:variable>

<!--
Call backend to generate SVG image of the barcode
-->
<xsl:call-template name="draw-barcode">
<xsl:with-param name="sequence" select="$value-encoded"/>
<xsl:with-param name="string">
<xsl:choose>
<xsl:when test="string-length($string)=0">
<xsl:value-of select="$value-real"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="print-text" select="$print-text"/>
<xsl:with-param name="module" select="$module"/>
<xsl:with-param name="height" select="$height"/>
<xsl:with-param name="quiet-horizontal" select="$quiet-horizontal"/>
<xsl:with-param name="quiet-vertical" select="$quiet-vertical"/>
<xsl:with-param name="font-family" select="$font-family"/>
<xsl:with-param name="font-height" select="$font-height"/>
<xsl:with-param name="value" select="$value-real"/>
</xsl:call-template>
</xsl:template>

<!--
=========================================================
-->
<!--
Calculate module 10 checksum character
-->
<xsl:template name="makeChecksum">
<xsl:param name="value"/>
<xsl:param name="position" select="2"/>
<xsl:param name="sum-odd" select="0"/>
<xsl:param name="sum-even" select="0"/>

<xsl:choose>
<xsl:when test="string-length($value) > 0">
<xsl:variable name="digit" select="substring($value, string-length($value))"/>
<xsl:call-template name="makeChecksum">
<xsl:with-param name="value" select="substring($value,1,string-length($value) - 1)"/>
<xsl:with-param name="sum-odd" select="$sum-odd + $digit*($position mod 2)"/>
<xsl:with-param name="sum-even" select="$sum-even + $digit*(($position+1) mod 2)"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="(10 - (($sum-odd + 3*$sum-even) mod 10)) mod 10"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--
=========================================================
-->
<!--
Encode string of digits using 2 of 5 interleave encoding
-->
<xsl:template name="recursive-coder">
<xsl:param name="value"/>

<!--
Convert two leading digits into binary sequence encoded with 2 of 5 code
-->
<!-- <xsl:variable name="digit1" select="document('')//mynum2bars/entry[@digit=substring($value, 1, 1)]/text()"/> -->
<!-- <xsl:variable name="digit2" select="document('')//mynum2bars/entry[@digit=substring($value, 2, 1)]/text()"/> -->
<xsl:variable name="digit1" select="document('2of5i-num2bars.xml')/num2bars/entry[@digit=substring($value, 1, 1)]/text()"/>
<xsl:variable name="digit2" select="document('2of5i-num2bars.xml')/num2bars/entry[@digit=substring($value, 2, 1)]/text()"/>

<!-- Interleave bits of two binary sequences -->
<xsl:value-of select="concat(substring($digit1,1,1), substring($digit2,1,1),
substring($digit1,2,1), substring($digit2,2,1),
substring($digit1,3,1), substring($digit2,3,1),
substring($digit1,4,1), substring($digit2,4,1),
substring($digit1,5,1), substring($digit2,5,1))"/>

<!--
If there is more digits to be drawn - activate recursion
-->
<xsl:if test="string-length($value) > 2">
<xsl:call-template name="recursive-coder">
<xsl:with-param name="value" select="substring($value,3)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

</xsl:stylesheet>



Code25 編碼 2of5i-num2bars.xml
<?xml version="1.0"  encoding="UTF-8"?>
<num2bars>
<entry digit="0">00110</entry>
<entry digit="1">10001</entry>
<entry digit="2">01001</entry>
<entry digit="3">11000</entry>
<entry digit="4">00101</entry>
<entry digit="5">10100</entry>
<entry digit="6">01100</entry>
<entry digit="7">00011</entry>
<entry digit="8">10010</entry>
<entry digit="9">01010</entry>
</num2bars>


Code25 生成圖像 2of5i-svg.xsl
<?xml version="1.0" encoding="UTF-8"?>
<!-- ========================================================= -->
<!-- -->
<!-- (c) 2003, RenderX -->
<!-- -->
<!-- Author: Alexander Peshkov <[email protected]> -->
<!-- -->
<!-- Permission is granted to use this document, copy and -->
<!-- modify free of charge, provided that every derived work -->
<!-- bear a reference to the present document. -->
<!-- -->
<!-- This document contains a computer program written in -->
<!-- XSL Transformations Language. It is published with no -->
<!-- warranty of any kind about its usability, as a mere -->
<!-- example of XSL technology. RenderX shall not be -->
<!-- considered liable for any damage or loss of data caused -->
<!-- by use of this program. -->
<!-- -->
<!-- ========================================================= -->

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:svg="http://www.w3.org/2000/svg">

<!-- ========================================================= -->
<!-- This stylesheet exports a named template used to draw -->
<!-- a 2 of 5 interleave barcode as an SVG image. -->
<!-- When used stand-alone, it creates SVG from a code -->
<!-- sequence passed in a parameter -->
<!-- -->
<!-- Mandatory parameters are: -->
<!-- -->
<!-- "sequence" - sequence of barcode states -->
<!-- to be drawn. -->
<!-- -->
<!-- Optional parameters are: -->
<!-- -->
<!-- "string" - a human readable string; -->
<!-- represents data encoded in -->
<!-- the barcode in a human-readable form -->
<!-- Default is empty string -->
<!-- "print-text" - boolean, defines if a human -->
<!-- readable label should be printed. -->
<!-- "module" - width of the elementary unit -->
<!-- bar/space; -->
<!-- Default is 0.012in -->
<!-- "wide-to-narrow" - width ratio for bars/spaces; -->
<!-- Default is 3.0 -->
<!-- "height" - pattern height (= bar length). -->
<!-- Default is 0.25in -->
<!-- "quiet-horizontal" - quiet zone horizontal margin width -->
<!-- Default is 0.24in -->
<!-- "quiet-vertical" - quiet zone vertical margin width -->
<!-- Default is 0.06in -->
<!-- "font-family" - a font family used to print textual -->
<!-- representation of a barcode. -->
<!-- Default is 'Courier' -->
<!-- "font-height" - a height of the font used to print -->
<!-- textual representation of a barcode. -->
<!-- Default is 8pt -->
<!-- ========================================================= -->

<xsl:output method="xml"
version="1.0"
indent="yes"/>

<!-- Bar states code sequence -->
<xsl:param name="sequence" select="''"/>
<!-- Information encoded by given barcode states -->
<xsl:param name="string" select="''"/>
<!-- Is human readable string printed under barcode? -->
<xsl:param name="print-text" select="'true'"/>
<!-- Optional parameters for drawing -->
<xsl:param name="module" select="'0.012in'"/>
<xsl:param name="wide-to-narrow" select="3.0"/>
<xsl:param name="height" select="'0.25in'"/>
<xsl:param name="quiet-horizontal" select="'0.24in'"/>
<xsl:param name="quiet-vertical" select="'0.06in'"/>
<xsl:param name="font-family" select="'Courier'"/>
<xsl:param name="font-height" select="'8pt'"/>
<xsl:param name="value" select="''"/>

<!--
Driver template used for command line processing, an analog of 'Main' function in C
-->
<xsl:template match="/">
<xsl:call-template name="draw-barcode">
<xsl:with-param name="sequence" select="$sequence"/>
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="print-text" select="$print-text"/>
<xsl:with-param name="module" select="$module"/>
<xsl:with-param name="wide-to-narrow" select="$wide-to-narrow"/>
<xsl:with-param name="height" select="$height"/>
<xsl:with-param name="quiet-horizontal" select="$quiet-horizontal"/>
<xsl:with-param name="quiet-vertical" select="$quiet-vertical"/>
<xsl:with-param name="font-family" select="$font-family"/>
<xsl:with-param name="font-height" select="$font-height"/>
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:template>

<!-- Main template used to create a barcode -->
<xsl:template name="draw-barcode">
<!-- Bar states code sequence -->
<xsl:param name="sequence" select="''"/>
<!-- Information encoded by given barcode states -->
<xsl:param name="string" select="''"/>
<!-- Is human readable string printed under barcode? -->
<xsl:param name="print-text" select="'true'"/>
<!-- Optional parameters for drawing -->
<xsl:param name="module" select="'0.012in'"/>
<xsl:param name="wide-to-narrow" select="3.0"/>
<xsl:param name="height" select="'0.25in'"/>
<xsl:param name="quiet-horizontal" select="'0.24in'"/>
<xsl:param name="quiet-vertical" select="'0.06in'"/>
<xsl:param name="font-family" select="'Courier'"/>
<xsl:param name="font-height" select="'8pt'"/>
<xsl:param name="value"/>

<!-- Parse narrow bar/space width specifier -->
<xsl:variable name="narrow-real">
<xsl:call-template name="convert-to-basic-units">
<xsl:with-param name="length" select="$module"/>
</xsl:call-template>
</xsl:variable>
<!-- Calculate wide bar/space width -->
<xsl:variable name="wide-real" select="round($narrow-real * $wide-to-narrow)"/>
<!-- Parse bar height specifier -->
<xsl:variable name="height-real">
<xsl:call-template name="convert-to-basic-units">
<xsl:with-param name="length" select="$height"/>
</xsl:call-template>
</xsl:variable>
<!--
Parse quiet zone vertical/horizontal margins specifiers
-->
<xsl:variable name="quiet-horizontal-real">
<xsl:call-template name="convert-to-basic-units">
<xsl:with-param name="length" select="$quiet-horizontal"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="quiet-vertical-real">
<xsl:call-template name="convert-to-basic-units">
<xsl:with-param name="length" select="$quiet-vertical"/>
</xsl:call-template>
</xsl:variable>
<!-- Calculate font height and line-height-->
<xsl:variable name="font-height-real">
<xsl:call-template name="convert-to-basic-units">
<xsl:with-param name="length" select="$font-height"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="line-height-real" select="round($font-height-real*1.2)*number($print-text='true')"/>
<!-- Useful variable - length of string -->
<xsl:variable name="length">
<xsl:value-of select="string-length($sequence)"/>
</xsl:variable>
<!-- Calculate codesequence length -->
<xsl:variable name="code-width-real" select="$wide-real*(($length - 7)*2 div 5+1) + $narrow-real*(($length - 7)*3 div 5+6)"/>
<!-- Calculate drawing area dimensions -->
<xsl:variable name="area-width-real" select="$code-width-real + 2*$quiet-horizontal-real"/>
<xsl:variable name="area-height-real" select="$height-real + $line-height-real + 2*$quiet-vertical-real"/>

<!-- Establish SVG drawing area -->
<!-- Drawing unit is equal to 1/360mm -->
<svg:svg width="{concat($area-width-real div 360, 'mm')}"
height="{concat($area-height-real div 360, 'mm')}"
viewBox="{concat('0 0 ', $area-width-real, ' ', $area-height-real)}">
<desc>
<barcode value="{$value}" type="i2of5"/>
</desc>
<!-- Prepare actual path -->
<xsl:variable name="path">
<!-- Call template to reqursively draw all bars -->
<xsl:call-template name="recursive-draw">
<xsl:with-param name="sequence" select="$sequence"/>
<xsl:with-param name="narrow-real" select="$narrow-real"/>
<xsl:with-param name="wide-real" select="$wide-real"/>
<xsl:with-param name="height-real" select="$height-real"/>
</xsl:call-template>
</xsl:variable>
<!--
Position a pen at the beggining of the barcode and make the actual drawing
-->
<xsl:variable name="full-path" select="concat('M ', $quiet-horizontal-real, ' ' , $quiet-vertical-real, $path)"/>
<svg:path d="{$full-path}" fill="black"/>
<xsl:if test="$print-text='true'">
<svg:text x="{$area-width-real div 2}" y="{$quiet-vertical-real*2 + $height-real + $font-height-real}" text-anchor="middle" font-family="{$font-family}" font-size="{$font-height-real}" fill="black">
<xsl:value-of select="$string"/>
</svg:text>
</xsl:if>
</svg:svg>
</xsl:template>

<!--
Draws single bar and calls itself if there are more bars to be drawn
-->
<xsl:template name="recursive-draw">
<xsl:param name="sequence"/>
<xsl:param name="narrow-real"/>
<xsl:param name="wide-real"/>
<xsl:param name="height-real"/>
<xsl:param name="position" select="1"/>

<xsl:variable name="barstate" select="substring($sequence, 1, 1)"/>
<!-- Select bar width -->
<xsl:variable name="width">
<xsl:choose>
<xsl:when test="$barstate=1">
<xsl:value-of select="$wide-real"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$narrow-real"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- Draw black or white bar -->
<xsl:choose>
<xsl:when test="($position mod 2) = 1">
<!-- Create appropriate path segment -->
<xsl:value-of select="concat(' l 0 ', $height-real, ' ', $width, ' 0 0 -', $height-real, ' z m ', $width, ' 0')"/>
</xsl:when>
<xsl:otherwise>
<!-- Create appropriate path segment -->
<xsl:value-of select="concat(' m ', $width, ' 0')"/>
</xsl:otherwise>
</xsl:choose>

<xsl:if test="string-length($sequence) > 1">
<xsl:call-template name="recursive-draw">
<xsl:with-param name="sequence" select="substring($sequence,2)"/>
<xsl:with-param name="narrow-real" select="$narrow-real"/>
<xsl:with-param name="wide-real" select="$wide-real"/>
<xsl:with-param name="height-real" select="$height-real"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<!-- Utility templates -->
<!--
=========================================================
-->
<!--
Convert any lengths to the basic units.
-->
<xsl:template name="convert-to-basic-units">
<xsl:param name="length"/>

<xsl:variable name="length-numeric-value" select="translate ($length, 'ptxcinme ', '')"/>
<xsl:variable name="length-unit" select="translate ($length, '-0123456789. ', '')"/>
<xsl:variable name="length-scale-factor">

<xsl:call-template name="get-unit-scaling-factor">
<xsl:with-param name="unit" select="$length-unit"/>
</xsl:call-template>
</xsl:variable>

<xsl:value-of select="round($length-numeric-value * $length-scale-factor)"/>
</xsl:template>

<!--
=========================================================
-->
<!--
This template expresses all length units in 1/360s of mm.
-->
<!--
This is the largest unit in which both 1pt and 1 mm get
-->
<!--
integer values. Also spellchecks length units.
-->
<xsl:template name="get-unit-scaling-factor">
<xsl:param name="unit"/>

<xsl:choose>
<xsl:when test="$unit = 'cm'">3600</xsl:when>
<xsl:when test="$unit = 'mm'">360</xsl:when>
<xsl:when test="$unit = 'in'">9144</xsl:when>
<xsl:when test="$unit = 'pt'">127</xsl:when>
<xsl:when test="$unit = 'pc'">1524</xsl:when>
<xsl:when test="$unit = 'em'">
<xsl:text>1524</xsl:text> <!-- defaulting to 12pt -->
<xsl:message>
[BARCODE GENERATOR] Units of 'em' should not be mixed with other units;
assuming 1 em = 1 pica.
</xsl:message>
</xsl:when>
<xsl:when test="$unit = 'ex'">
<xsl:text>700</xsl:text> <!-- defaulting to 12pt x 0.46 -->
<xsl:message>
[BARCODE GENERATOR] Units of 'ex' should not be mixed with other units;
assuming 1 ex = 0.46 pica.
</xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:text>360</xsl:text> <!-- defaulting to 1mm -->
<xsl:message>
[BARCODE GENERATOR] Unknown unit '
<xsl:value-of select="$unit"/>
' should not be mixed with other units;
assuming 1
<xsl:value-of select="$unit"/>
= 1 mm.
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章