import sys
import math
import arabic_reshaper
from fontTools.ttLib import TTFont
from fontTools.pens.basePen import BasePen

class LengthPen(BasePen):
    def __init__(self, glyphSet):
        BasePen.__init__(self, glyphSet)
        self.total_length = 0
        self.curr_p = (0, 0)

    def _moveTo(self, p):
        self.curr_p = p

    def _lineTo(self, p):
        self.total_length += math.hypot(
            p[0] - self.curr_p[0],
            p[1] - self.curr_p[1]
        )
        self.curr_p = p

    def _curveToOne(self, p1, p2, p3):
        steps = 20
        last_p = self.curr_p

        for i in range(1, steps + 1):
            t = i / steps

            px = (
                (1 - t) ** 3 * self.curr_p[0]
                + 3 * (1 - t) ** 2 * t * p1[0]
                + 3 * (1 - t) * t ** 2 * p2[0]
                + t ** 3 * p3[0]
            )

            py = (
                (1 - t) ** 3 * self.curr_p[1]
                + 3 * (1 - t) ** 2 * t * p1[1]
                + 3 * (1 - t) * t ** 2 * p2[1]
                + t ** 3 * p3[1]
            )

            self.total_length += math.hypot(
                px - last_p[0],
                py - last_p[1]
            )

            last_p = (px, py)

        self.curr_p = p3

    def _qCurveToOne(self, p1, p2):
        steps = 15
        last_p = self.curr_p

        for i in range(1, steps + 1):
            t = i / steps

            px = (
                (1 - t) ** 2 * self.curr_p[0]
                + 2 * (1 - t) * t * p1[0]
                + t ** 2 * p2[0]
            )

            py = (
                (1 - t) ** 2 * self.curr_p[1]
                + 2 * (1 - t) * t * p1[1]
                + t ** 2 * p2[1]
            )

            self.total_length += math.hypot(
                px - last_p[0],
                py - last_p[1]
            )

            last_p = (px, py)

        self.curr_p = p2

    def _closePath(self):
        pass


def calculate_length(text, font_path, target_width_cm):
    font = TTFont(font_path)
    glyph_set = font.getGlyphSet()
    cmap = font.getBestCmap()

    lines = text.split("\n")

    line_data = []

    for line in lines:

        if line.strip() == "":
            continue

        reshaped_text = arabic_reshaper.reshape(line)

        line_length = 0
        line_width = 0

        for char in reshaped_text:

            code = ord(char)

            if code in cmap:

                glyph_name = cmap[code]

                pen = LengthPen(glyph_set)
                glyph_set[glyph_name].draw(pen)

                line_length += pen.total_length
                line_width += font['hmtx'][glyph_name][0]


        if line_width > 0:
            line_data.append(
                {
                    "length": line_length,
                    "width": line_width
                }
            )


    if not line_data:
        return 0


    # پیدا کردن عریض ترین خط مثل Corel
    max_width = max(
        item["width"] for item in line_data
    )


    # ضریب بزرگنمایی
    scale = target_width_cm / max_width


    total_length = 0


    # همه خطوط با همان ضریب بزرگ می شوند
    for item in line_data:

        total_length += item["length"] * scale


    return total_length * 0.96764


if __name__ == "__main__":
    text = sys.argv[1]
    font_path = sys.argv[2]
    width_cm = float(sys.argv[3])

    result = calculate_length(text, font_path, width_cm)

    print(round(result, 2))