Skip to content

Commit

Permalink
Write the font bbox as well
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Jun 14, 2024
1 parent 5d99129 commit b2288af
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/cff/dict/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ impl<'a> DictionaryParser<'a> {
}
}

pub fn parse_font_bbox(&mut self) -> Option<[Number; 4]> {
self.parse_operands()?;
let operands = self.operands();
if operands.len() == 4 {
Some([operands[0], operands[1], operands[2], operands[3]])
} else {
None
}
}

pub fn parse_font_matrix(&mut self) -> Option<[Number; 6]> {
self.parse_operands()?;
let operands = self.operands();
Expand Down Expand Up @@ -145,6 +155,7 @@ pub(crate) mod operators {

// TOP DICT OPERATORS
pub const NOTICE: Operator = Operator(OperatorType::OneByteOperator([1]));
pub const FONT_BBOX: Operator = Operator(OperatorType::OneByteOperator([5]));
pub const COPYRIGHT: Operator =
Operator(OperatorType::TwoByteOperator([TWO_BYTE_OPERATOR_MARK, 0]));
pub const FONT_MATRIX: Operator =
Expand Down
11 changes: 11 additions & 0 deletions src/cff/dict/top_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct TopDictData {
pub font_name: Option<StringId>,
pub has_ros: bool,
pub font_matrix: Option<[Number; 6]>,
pub font_bbox: Option<[Number; 4]>,
}

pub fn parse_top_dict_index(r: &mut Reader) -> Option<TopDictData> {
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn parse_top_dict_index(r: &mut Reader) -> Option<TopDictData> {
FD_ARRAY => top_dict.fd_array = Some(dict_parser.parse_offset()?),
FD_SELECT => top_dict.fd_select = Some(dict_parser.parse_offset()?),
FONT_MATRIX => top_dict.font_matrix = Some(dict_parser.parse_font_matrix()?),
FONT_BBOX => top_dict.font_bbox = Some(dict_parser.parse_font_bbox()?),
_ => {}
}
}
Expand Down Expand Up @@ -115,6 +117,15 @@ pub fn rewrite_top_dict_index(
]));
sub_w.write(FONT_MATRIX);

// Write a default font bbox, if it does not exist.
sub_w.write(top_dict_data.font_bbox.as_ref().unwrap_or(&[
Number::zero(),
Number::zero(),
Number::zero(),
Number::zero(),
]));
sub_w.write(FONT_BBOX);

// Note: When writing the offsets, we need to add the current length of w AND sub_w.
// Charset
offsets.charset_offset.update_location(sub_w.len() + w.len());
Expand Down
4 changes: 2 additions & 2 deletions tests/ttx/LatinModernRoman-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.004"/>
<checkSumAdjustment value="0xbb4babf0"/>
<checkSumAdjustment value="0x45f2f96c"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -105,7 +105,7 @@
<PaintType value="0"/>
<CharstringType value="2"/>
<FontMatrix value="0.001 0 0 0.001 0 0"/>
<FontBBox value="0 0 0 0"/>
<FontBBox value="-430 -290 1417 1127"/>
<StrokeWidth value="0"/>
<CIDFontVersion value="0"/>
<CIDFontRevision value="0"/>
Expand Down
4 changes: 2 additions & 2 deletions tests/ttx/NewCMMath-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.0"/>
<checkSumAdjustment value="0xc9a635b6"/>
<checkSumAdjustment value="0x29342cfe"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00001011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -113,7 +113,7 @@ This work is released under the GUST Font License -- see http://tug.org/fonts/li
<PaintType value="0"/>
<CharstringType value="2"/>
<FontMatrix value="0.001 0 0 0.001 0 0"/>
<FontBBox value="0 0 0 0"/>
<FontBBox value="-1042 -3060 4082 3560"/>
<StrokeWidth value="0"/>
<CIDFontVersion value="0"/>
<CIDFontRevision value="0"/>
Expand Down
4 changes: 2 additions & 2 deletions tests/ttx/NotoSansCJKsc-Bold-subset1_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.004"/>
<checkSumAdjustment value="0x3a658607"/>
<checkSumAdjustment value="0xa7acfdf8"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -102,7 +102,7 @@
<PaintType value="0"/>
<CharstringType value="2"/>
<FontMatrix value="0.001 0 0 0.001 0 0"/>
<FontBBox value="0 0 0 0"/>
<FontBBox value="-1013 -1046 2926 1806"/>
<StrokeWidth value="0"/>
<CIDFontVersion value="0"/>
<CIDFontRevision value="0"/>
Expand Down
4 changes: 2 additions & 2 deletions tests/ttx/NotoSansCJKsc-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.004"/>
<checkSumAdjustment value="0xf75f3f09"/>
<checkSumAdjustment value="0x4456ad8"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -123,7 +123,7 @@
<PaintType value="0"/>
<CharstringType value="2"/>
<FontMatrix value="0.001 0 0 0.001 0 0"/>
<FontBBox value="0 0 0 0"/>
<FontBBox value="-1002 -1048 2928 1808"/>
<StrokeWidth value="0"/>
<CIDFontVersion value="0"/>
<CIDFontRevision value="0"/>
Expand Down

0 comments on commit b2288af

Please sign in to comment.