-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
196 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package font | ||
|
||
/* | ||
#cgo LDFLAGS: -lharfbuzz -lharfbuzz-subset | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <harfbuzz/hb.h> | ||
#include <harfbuzz/hb-subset.h> | ||
int subset(const unsigned char *src, unsigned int src_len, uint32_t uni_chars[], int num_chars, unsigned char *out) | ||
{ | ||
unsigned int out_len = 0; | ||
hb_blob_t *data = hb_blob_create_or_fail(src, src_len, HB_MEMORY_MODE_READONLY, NULL, NULL); | ||
if (data == NULL) | ||
return out_len; | ||
hb_face_t *face = hb_face_create(data, 0); | ||
hb_subset_input_t *input = hb_subset_input_create_or_fail(); | ||
if (input == NULL) | ||
goto destroy_face; | ||
hb_set_t *charset = hb_subset_input_unicode_set(input); | ||
for (int i = 0; i < num_chars; i++) | ||
hb_set_add(charset, uni_chars[i]); | ||
hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_RETAIN_GIDS); | ||
hb_face_t *sub_face = hb_subset_or_fail(face, input); | ||
if (sub_face == NULL) | ||
goto destroy_input; | ||
hb_blob_t *sub_blob = hb_face_reference_blob(sub_face); | ||
const char *out_data = hb_blob_get_data(sub_blob, &out_len); | ||
memcpy(out, out_data, out_len); | ||
hb_blob_destroy(sub_blob); | ||
destroy_input: | ||
hb_subset_input_destroy(input); | ||
destroy_face: | ||
hb_face_destroy(face); | ||
return out_len; | ||
} | ||
*/ | ||
import "C" | ||
import ( | ||
"fmt" | ||
"unsafe" | ||
|
||
"golang.org/x/image/font/sfnt" | ||
) | ||
|
||
// HBSubsetC can be used as a gdf.FontSubsetFunc. It calls functions in libharfbuzz and libharfbuzz-subset via CGo. In order | ||
// for this function to work, CGo must be enabled and HarfBuzz must be installed on your system. | ||
func HBSubsetC(_ *sfnt.Font, src []byte, charset map[rune]struct{}) ([]byte, error) { | ||
// convert runes to uint32_t chars readable by hb-subset | ||
charset_u32 := make([]uint32, len(charset)) | ||
for char := range charset { | ||
charset_u32 = append(charset_u32, uint32(char)) | ||
} | ||
// allocate at least as much as the current file size | ||
b := make([]byte, 0, len(src)) | ||
|
||
srcData := unsafe.SliceData(src) | ||
charsetData := unsafe.SliceData(charset_u32) | ||
outData := unsafe.SliceData(b) | ||
|
||
written := int(C.subset( | ||
(*C.uchar)(srcData), | ||
C.uint(uint(len(src))), | ||
(*C.uint)(charsetData), | ||
C.int(len(charset_u32)), | ||
(*C.uchar)(outData))) | ||
if written < 1 { | ||
return nil, fmt.Errorf("error subsetting font") | ||
} | ||
b = unsafe.Slice(outData, written) | ||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package svg provides very limited, experimental facilities for rendering SVG images to PDFs. In addition to the obvious constraints (e.g., lack of animation), this package does not implement several important SVG features. Here's a rundown of some of them: | ||
1. There is limited support for CSS properties. | ||
2. Support for SVG text elements is unplanned. | ||
3. Elliptical Arc Curve (`A` and `a`) path commands are not supported, and the present solution for displaying ellipse elements needs substantial improvement. | ||
3. Elliptical Arc Curve (`A` and `a`) path commands may be improperly rendered. | ||
4. Mask elements and transparency/opacity-related attributes are not supported. | ||
|
||
These limitations preclude the use of this package for certain applications, but it can work with a surprising number of basic SVG images. Running an SVG through an SVG optimizer, such as SVGO(MG), and making simple manual adjustments to the SVG's source text can often fix rendering issues. | ||
These limitations preclude the use of this package for certain applications, but it can work with a surprising number of basic SVG images. Running `rsvg-convert` with the `-f svg` option on the input SVG prior to its inclusion in the PDF is **highly** recommended. Making simple manual adjustments to the SVG's source text can also often fix rendering issues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters