@@ -569,46 +569,6 @@ void FT2Font::select_charmap(unsigned long i)
569
569
}
570
570
}
571
571
572
- FT_BBox FT2Font::compute_string_bbox ()
573
- {
574
- FT_BBox bbox;
575
- /* initialize string bbox to "empty" values */
576
- bbox.xMin = bbox.yMin = 32000 ;
577
- bbox.xMax = bbox.yMax = -32000 ;
578
-
579
- int right_side = 0 ;
580
- for (size_t n = 0 ; n < glyphs.size (); n++) {
581
- FT_BBox glyph_bbox;
582
- FT_Glyph_Get_CBox (glyphs[n], ft_glyph_bbox_subpixels, &glyph_bbox);
583
- if (glyph_bbox.xMin < bbox.xMin ) {
584
- bbox.xMin = glyph_bbox.xMin ;
585
- }
586
- if (glyph_bbox.yMin < bbox.yMin ) {
587
- bbox.yMin = glyph_bbox.yMin ;
588
- }
589
- if (glyph_bbox.xMin == glyph_bbox.xMax ) {
590
- right_side += glyphs[n]->advance .x >> 10 ;
591
- if (right_side > bbox.xMax ) {
592
- bbox.xMax = right_side;
593
- }
594
- } else {
595
- if (glyph_bbox.xMax > bbox.xMax ) {
596
- bbox.xMax = glyph_bbox.xMax ;
597
- }
598
- }
599
- if (glyph_bbox.yMax > bbox.yMax )
600
- bbox.yMax = glyph_bbox.yMax ;
601
- }
602
- /* check that we really grew the string bbox */
603
- if (bbox.xMin > bbox.xMax ) {
604
- bbox.xMin = 0 ;
605
- bbox.yMin = 0 ;
606
- bbox.xMax = 0 ;
607
- bbox.yMax = 0 ;
608
- }
609
- return bbox;
610
- }
611
-
612
572
int FT2Font::get_kerning (int left, int right, int mode)
613
573
{
614
574
if (!FT_HAS_KERNING (face)) {
@@ -617,7 +577,7 @@ int FT2Font::get_kerning(int left, int right, int mode)
617
577
FT_Vector delta;
618
578
619
579
if (!FT_Get_Kerning (face, left, right, mode, &delta)) {
620
- return (int )(delta.x / hinting_factor);
580
+ return (int )(delta.x ) / ( hinting_factor << 6 );
621
581
} else {
622
582
return 0 ;
623
583
}
@@ -641,17 +601,22 @@ void FT2Font::set_text(
641
601
pen.x = 0 ;
642
602
pen.y = 0 ;
643
603
604
+ bbox.xMin = bbox.yMin = 32000 ;
605
+ bbox.xMax = bbox.yMax = -32000 ;
606
+
644
607
for (unsigned int n = 0 ; n < N; n++) {
645
608
std::string thischar (" ?" );
646
609
FT_UInt glyph_index;
610
+ FT_BBox glyph_bbox;
611
+ FT_Pos last_advance;
647
612
648
613
glyph_index = FT_Get_Char_Index (face, codepoints[n]);
649
614
650
615
// retrieve kerning distance and move pen position
651
616
if (use_kerning && previous && glyph_index) {
652
617
FT_Vector delta;
653
618
FT_Get_Kerning (face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
654
- pen.x += delta.x / hinting_factor;
619
+ pen.x += ( delta.x << 10 ) / ( hinting_factor << 16 ) ;
655
620
}
656
621
error = FT_Load_Glyph (face, glyph_index, flags);
657
622
if (error) {
@@ -669,18 +634,30 @@ void FT2Font::set_text(
669
634
}
670
635
// ignore errors, jump to next glyph
671
636
637
+ last_advance = face->glyph ->advance .x ;
672
638
FT_Glyph_Transform (thisGlyph, 0 , &pen);
639
+ FT_Glyph_Transform (thisGlyph, &matrix, 0 );
673
640
xys.push_back (pen.x );
674
641
xys.push_back (pen.y );
675
- pen.x += face->glyph ->advance .x ;
642
+
643
+ FT_Glyph_Get_CBox (thisGlyph, ft_glyph_bbox_subpixels, &glyph_bbox);
644
+
645
+ bbox.xMin = std::min (bbox.xMin , glyph_bbox.xMin );
646
+ bbox.xMax = std::max (bbox.xMax , glyph_bbox.xMax );
647
+ bbox.yMin = std::min (bbox.yMin , glyph_bbox.yMin );
648
+ bbox.yMax = std::max (bbox.yMax , glyph_bbox.yMax );
649
+
650
+ pen.x += last_advance;
676
651
677
652
previous = glyph_index;
678
653
glyphs.push_back (thisGlyph);
679
654
}
680
655
681
- // now apply the rotation
682
- for (unsigned int n = 0 ; n < glyphs.size (); n++) {
683
- FT_Glyph_Transform (glyphs[n], &matrix, 0 );
656
+ FT_Vector_Transform (&pen, &matrix);
657
+ advance = pen.x ;
658
+
659
+ if (bbox.xMin > bbox.xMax ) {
660
+ bbox.xMin = bbox.yMin = bbox.xMax = bbox.yMax = 0 ;
684
661
}
685
662
}
686
663
@@ -722,30 +699,29 @@ void FT2Font::load_glyph(FT_UInt glyph_index, FT_UInt32 flags)
722
699
723
700
void FT2Font::get_width_height (long *width, long *height)
724
701
{
725
- FT_BBox bbox = compute_string_bbox ();
726
-
727
- *width = bbox.xMax - bbox.xMin ;
702
+ *width = advance;
728
703
*height = bbox.yMax - bbox.yMin ;
729
704
}
730
705
731
706
long FT2Font::get_descent ()
732
707
{
733
- FT_BBox bbox = compute_string_bbox ();
734
708
return -bbox.yMin ;
735
709
}
736
710
711
+ void FT2Font::get_bitmap_offset (long *x, long *y)
712
+ {
713
+ *x = bbox.xMin ;
714
+ *y = 0 ;
715
+ }
716
+
737
717
void FT2Font::draw_glyphs_to_bitmap (bool antialiased)
738
718
{
739
- FT_BBox string_bbox = compute_string_bbox ();
740
- size_t width = (string_bbox.xMax - string_bbox.xMin ) / 64 + 2 ;
741
- size_t height = (string_bbox.yMax - string_bbox.yMin ) / 64 + 2 ;
719
+ size_t width = (bbox.xMax - bbox.xMin ) / 64 + 2 ;
720
+ size_t height = (bbox.yMax - bbox.yMin ) / 64 + 2 ;
742
721
743
722
image.resize (width, height);
744
723
745
724
for (size_t n = 0 ; n < glyphs.size (); n++) {
746
- FT_BBox bbox;
747
- FT_Glyph_Get_CBox (glyphs[n], ft_glyph_bbox_pixels, &bbox);
748
-
749
725
error = FT_Glyph_To_Bitmap (
750
726
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0 , 1 );
751
727
if (error) {
@@ -756,22 +732,17 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
756
732
// now, draw to our target surface (convert position)
757
733
758
734
// bitmap left and top in pixel, string bbox in subpixel
759
- FT_Int x = (FT_Int)(bitmap->left - (string_bbox .xMin / 64 .));
760
- FT_Int y = (FT_Int)((string_bbox .yMax / 64 .) - bitmap->top + 1 );
735
+ FT_Int x = (FT_Int)(bitmap->left - (bbox .xMin / 64 .));
736
+ FT_Int y = (FT_Int)((bbox .yMax / 64 .) - bitmap->top + 1 );
761
737
762
738
image.draw_bitmap (&bitmap->bitmap , x, y);
763
739
}
764
740
}
765
741
766
742
void FT2Font::get_xys (bool antialiased, std::vector<double > &xys)
767
743
{
768
- FT_BBox string_bbox = compute_string_bbox ();
769
-
770
744
for (size_t n = 0 ; n < glyphs.size (); n++) {
771
745
772
- FT_BBox bbox;
773
- FT_Glyph_Get_CBox (glyphs[n], ft_glyph_bbox_pixels, &bbox);
774
-
775
746
error = FT_Glyph_To_Bitmap (
776
747
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0 , 1 );
777
748
if (error) {
@@ -781,8 +752,8 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
781
752
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
782
753
783
754
// bitmap left and top in pixel, string bbox in subpixel
784
- FT_Int x = (FT_Int)(bitmap->left - string_bbox .xMin / 64 .);
785
- FT_Int y = (FT_Int)(string_bbox .yMax / 64 . - bitmap->top + 1 );
755
+ FT_Int x = (FT_Int)(bitmap->left - bbox .xMin / 64 .);
756
+ FT_Int y = (FT_Int)(bbox .yMax / 64 . - bitmap->top + 1 );
786
757
// make sure the index is non-neg
787
758
x = x < 0 ? 0 : x;
788
759
y = y < 0 ? 0 : y;
0 commit comments