BEGIN {
use
vars
qw( @ISA $fonts $cmap $alias $VERSION )
;
@ISA
=
qw( PDF::API2::Resource::CIDFont )
;
(
$VERSION
) =
'$Revision: 1.3 $'
=~ /Revision: (\S+)\s/;
$fonts
= { };
$cmap
= { };
}
sub
_look_for_font ($) {
my
$fname
=
lc
(
shift
);
$fname
=~s/[^a-z0-9]+//gi;
$fname
=
$alias
->{
$fname
}
if
(
defined
$alias
->{
$fname
});
return
(%{
$fonts
->{
$fname
}})
if
(
defined
$fonts
->{
$fname
});
eval
"require PDF::API2::Resource::CIDFont::CJKFont::$fname; "
;
unless
($@){
return
({%{
$fonts
->{
$fname
}}});
}
else
{
die
"requested font '$fname' not installed "
;
}
}
sub
_look_for_cmap ($) {
my
$fname
=
lc
(
shift
);
$fname
=~s/[^a-z0-9]+//gi;
return
(%{
$cmap
->{
$fname
}})
if
(
defined
$cmap
->{
$fname
});
eval
"require PDF::API2::Resource::CIDFont::CMap::$fname; "
;
unless
($@){
return
({%{
$cmap
->{
$fname
}}});
}
else
{
die
"requested cmap '$fname' not installed "
;
}
}
sub
new {
my
(
$class
,
$pdf
,
$name
,
@opts
) =
@_
;
my
%opts
=();
%opts
=
@opts
if
((
scalar
@opts
)%2 == 0);
my
$data
= _look_for_font(
$name
);
my
$cmap
= _look_for_cmap(
$data
->{cmap});
$data
->{u2g} = { %{
$cmap
->{u2g}} };
$data
->{g2u} = [ @{
$cmap
->{g2u}} ];
$class
=
ref
$class
if
ref
$class
;
my
$self
=
$class
->SUPER::new(
$pdf
,
$data
->{apiname}.pdfkey());
$pdf
->new_obj(
$self
)
if
(
defined
(
$pdf
) && !
$self
->is_obj(
$pdf
));
$self
->{
' data'
}=
$data
;
my
$des
=
$self
->descrByData;
$self
->{
'BaseFont'
} = PDFName(
$self
->fontname.
'-Identity-H'
);
my
$de
=
$self
->{
' de'
};
$de
->{
'FontDescriptor'
} =
$des
;
$de
->{
'Subtype'
} = PDFName(
'CIDFontType0'
);
$de
->{
'BaseFont'
} = PDFName(
$self
->fontname);
$de
->{
'DW'
} = PDFNum(
$self
->missingwidth);
if
(
defined
$opts
{-encode}) {
$self
->data->{encode}=
$opts
{-encode};
}
return
(
$self
);
}
sub
new_api {
my
(
$class
,
$api
,
@opts
)=
@_
;
my
$obj
=
$class
->new(
$api
->{pdf},
@opts
);
$self
->{
' api'
}=
$api
;
$api
->{pdf}->out_obj(
$api
->{pages});
return
(
$obj
);
}
sub
text_cid {
my
(
$self
,
$text
)=
@_
;
my
$newtext
=
''
;
foreach
my
$g
(
unpack
(
'n*'
,
$text
)) {
$newtext
.=
sprintf
(
'%04X'
,
$g
);
}
return
(
"<$newtext>"
);
}
sub
cidsByStr {
my
(
$self
,
$s
)=
@_
;
$s
=
pack
(
'n*'
,
map
{
$self
->cidByUni(
$_
) }
unpack
(
'U*'
,decode(
$self
->data->{encode},
$s
)));
return
(
$s
);
}
sub
width {
my
(
$self
,
$text
)=
@_
;
my
$width
=0;
if
(is_utf8(
$text
)) {
foreach
my
$n
(
unpack
(
'U*'
,
$text
)) {
$width
+=
$self
->wxByUni(
$n
);
}
}
else
{
return
$self
->width_cid(
$self
->cidsByStr(
$text
));
}
$width
/=1000;
return
(
$width
);
}
sub
outobjdeep {
my
(
$self
,
$fh
,
$pdf
,
%opts
) =
@_
;
return
$self
->SUPER::outobjdeep(
$fh
,
$pdf
)
if
defined
$opts
{
'passthru'
};
my
$notdefbefore
=1;
my
$wx
=PDFArray();
$self
->{
' de'
}->{
'W'
} =
$wx
;
my
$ml
;
foreach
my
$w
(0..(
scalar
@{
$self
->data->{g2u}} - 1 )) {
if
((
defined
$self
->data->{wx}->[
$w
]) &&
$notdefbefore
==1) {
$notdefbefore
=0;
$ml
=PDFArray();
$wx
->add_elements(PDFNum(
$w
),
$ml
);
$ml
->add_elements(PDFNum(
$self
->data->{wx}->[
$w
]));
}
elsif
((
defined
$self
->data->{wx}->[
$w
]) &&
$notdefbefore
==0) {
$notdefbefore
=0;
$ml
->add_elements(PDFNum(
$self
->data->{wx}->[
$w
]));
}
else
{
$notdefbefore
=1;
}
}
$self
->SUPER::outobjdeep(
$fh
,
$pdf
,
%opts
);
}
BEGIN {
$alias
={
'traditional'
=>
'adobemingstdlightacro'
,
'simplified'
=>
'adobesongstdlightacro'
,
'korean'
=>
'adobemyungjostdmediumacro'
,
'japanese'
=>
'kozgopromediumacro'
,
'japanese2'
=>
'kozminproregularacro'
,
};
}
1;