def get_single_character_width(self, character_code: int) -> Optional[Decimal]: # self if "Widths" in self and "FirstChar" in self and "LastChar" in self: if self["FirstChar"] <= character_code <= self["LastChar"] and len( self["Widths"] ) >= (self["LastChar"] - self["FirstChar"]): return self["Widths"][int(character_code - self["FirstChar"])] return None # standard 14 standard_14_font = AdobeFontMetrics.get(self.get_font_name()) if ( standard_14_font is not None and "Widths" in standard_14_font and "FirstChar" in standard_14_font and "LastChar" in standard_14_font ): if standard_14_font["FirstChar"] <= character_code <= standard_14_font[ "LastChar" ] and len(standard_14_font["Widths"]) >= ( standard_14_font["LastChar"] - standard_14_font["FirstChar"] ): return standard_14_font["Widths"][ int(character_code - standard_14_font["FirstChar"]) ] # default return None
def get_single_character_width(self, character_code: int) -> Optional[Decimal]: """ Get the width (in text space) of a given character code. Returns None if the character code can not be represented in this Font. """ # self if "Widths" in self and "FirstChar" in self and "LastChar" in self: if self["FirstChar"] <= character_code <= self["LastChar"] and len( self["Widths"]) >= (self["LastChar"] - self["FirstChar"]): return self["Widths"][int(character_code - self["FirstChar"])] return None # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "Widths" in standard_14_font and "FirstChar" in standard_14_font and "LastChar" in standard_14_font): if standard_14_font[ "FirstChar"] <= character_code <= standard_14_font[ "LastChar"] and len(standard_14_font["Widths"]) >= ( standard_14_font["LastChar"] - standard_14_font["FirstChar"]): return standard_14_font["Widths"][int( character_code - standard_14_font["FirstChar"])] # default return None
def __init__( self, text: str, font: Union[Font, str] = "Helvetica", font_size: Decimal = Decimal(12), font_color: Color = X11Color("Black"), border_top: bool = False, border_right: bool = False, border_bottom: bool = False, border_left: bool = False, border_color: Color = X11Color("Black"), border_width: Decimal = Decimal(1), padding_top: Decimal = Decimal(0), padding_right: Decimal = Decimal(0), padding_bottom: Decimal = Decimal(0), padding_left: Decimal = Decimal(0), vertical_alignment: Alignment = Alignment.TOP, horizontal_alignment: Alignment = Alignment.LEFT, background_color: typing.Optional[Color] = None, parent: typing.Optional["LayoutElement"] = None, ): super().__init__( border_top=border_top, border_right=border_right, border_bottom=border_bottom, border_left=border_left, border_color=border_color, border_width=border_width, padding_top=padding_top, padding_right=padding_right, padding_bottom=padding_bottom, padding_left=padding_left, vertical_alignment=vertical_alignment, horizontal_alignment=horizontal_alignment, background_color=background_color, parent=parent, ) self.text = text if isinstance(font, str): self.font: Font = FontType1() font_to_copy: typing.Optional[Font] = AdobeFontMetrics.get(font) self.font[Name("Encoding")] = Name("WinAnsiEncoding") assert font_to_copy for k, v in font_to_copy.items(): self.font[k] = v assert self.font else: self.font = font self.font_color = font_color self.font_size = font_size
def get_descent(self) -> Optional[Decimal]: # self if "FontDescriptor" in self and "Descent" in self["FontDescriptor"]: return self["FontDescriptor"]["Descent"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "Descent" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["Descent"] # default return None
def get_average_character_width(self) -> Optional[Decimal]: # self if "FontDescriptor" in self and "AvgWidth" in self["FontDescriptor"]: return self["FontDescriptor"]["AvgWidth"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "AvgWidth" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["AvgWidth"] # default return None
def get_missing_character_width(self) -> Optional[Decimal]: # self if "FontDescriptor" in self and "MissingWidth" in self["FontDescriptor"]: return self["FontDescriptor"]["MissingWidth"] # standard 14 standard_14_font = AdobeFontMetrics.get(self.get_font_name()) if ( standard_14_font is not None and "FontDescriptor" in standard_14_font and "MissingWidth" in standard_14_font["FontDescriptor"] ): return standard_14_font["FontDescriptor"]["MissingWidth"] # default return None
def get_ascent(self) -> Optional[Decimal]: # self if "FontDescriptor" in self and "Ascent" in self["FontDescriptor"]: return self["FontDescriptor"]["Ascent"] # standard 14 standard_14_font = AdobeFontMetrics.get(self.get_font_name()) if ( standard_14_font is not None and "FontDescriptor" in standard_14_font and "Ascent" in standard_14_font["FontDescriptor"] ): return standard_14_font["FontDescriptor"]["Ascent"] # default return None
def get_missing_character_width(self) -> Decimal: # self if "FontDescriptor" in self and "MissingWidth" in self[ "FontDescriptor"]: return self["FontDescriptor"]["MissingWidth"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "MissingWidth" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["MissingWidth"] # default return Decimal(250)
def get_descent(self) -> Optional[Decimal]: """ (Required, except for Type 3 fonts) The maximum depth below the baseline reached by glyphs in this font. The value shall be a negative number. """ # self if "FontDescriptor" in self and "Descent" in self["FontDescriptor"]: return self["FontDescriptor"]["Descent"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "Descent" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["Descent"] # default return None
def get_ascent(self) -> Optional[Decimal]: """ (Required, except for Type 3 fonts) The maximum height above the baseline reached by glyphs in this font. The height of glyphs for accented characters shall be excluded. """ # self if "FontDescriptor" in self and "Ascent" in self["FontDescriptor"]: return self["FontDescriptor"]["Ascent"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "Ascent" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["Ascent"] # default return None
def get_missing_character_width(self) -> Decimal: """ (Optional) The width to use for character codes whose widths are not specified in a font dictionary’s Widths array. This shall have a predictable effect only if all such codes map to glyphs whose actual widths are the same as the value of the MissingWidth entry. Default value: 0. """ # self if "FontDescriptor" in self and "MissingWidth" in self[ "FontDescriptor"]: return self["FontDescriptor"]["MissingWidth"] # standard 14 font_name: Optional[str] = self.get_font_name() assert font_name is not None standard_14_font = AdobeFontMetrics.get(font_name) if (standard_14_font is not None and "FontDescriptor" in standard_14_font and "MissingWidth" in standard_14_font["FontDescriptor"]): return standard_14_font["FontDescriptor"]["MissingWidth"] # default return Decimal(250)