<commit_before>define(function (require) {

    var textWidthCache = {};
    var textWidthCacheCounter = 0;
    var TEXT_CACHE_MAX = 5000;

    var util = require('../core/util');
    var BoundingRect = require('../core/BoundingRect');

    function getTextWidth(text, textFont) {
        var key = text + ':' + textFont;
        if (textWidthCache[key]) {
            return textWidthCache[key];
        }

        var textLines = (text + '').split('\n');
        var width = 0;

        for (var i = 0, l = textLines.length; i < l; i++) {
            // measureText 可以被覆盖以兼容不支持 Canvas 的环境
            width =  Math.max(textContain.measureText(textLines[i], textFont).width, width);
        }

        if (textWidthCacheCounter > TEXT_CACHE_MAX) {
            textWidthCacheCounter = 0;
            textWidthCache = {};
        }
        textWidthCacheCounter++;
        textWidthCache[key] = width;

        return width;
    };

    function getTextRect(text, textFont, textAlign, textBaseline) {
        var textLineLen = text.split('\n').length;

        var width = getTextWidth(text, textFont);
        // FIXME 高度计算比较粗暴
        var lineHeight = getTextWidth('国', textFont);
        var height = textLineLen * lineHeight;

        var rect = new BoundingRect(0, 0, width, height)
        // Text has a special line height property
        rect.lineHeight = lineHeight;

        switch (textBaseline) {
            case 'bottom':
            case 'alphabetic':
                rect.y -= lineHeight;
                break;
            case 'middle':
                rect.y -= lineHeight / 2;
                break;
            // case 'hanging':
            // case 'top':
        };

        // FIXME Right to left language
        switch (textAlign) {
            case 'end':
            case 'right':
                rect.x -= rect.width;
                break;
            case 'center':
                rect.x -= rect.width / 2;
                break;
            // case 'start':
            // case 'left':
        }

        return rect;
    };

    function adjustTextPositionOnRect(textPosition, rect, textRect, distance) {

        var x = rect.x;
        var y = rect.y;

        var height = rect.height;
        var width = rect.width;

        var textWidth = textRect.width;
        var textHeight = textRect.height;

        var halfWidth = width / 2 - textWidth / 2;
        var halfHeight = height / 2 - textHeight / 2;

        switch (textPosition) {
            case 'left':
                x -= distance + textWidth;
                y += halfHeight;
                break;
            case 'right':
                x += width + distance;
                y += halfHeight;
                break;
            case 'top':
                x += halfWidth;
                y -= distance + textHeight;
                break;
            case 'bottom':
                x += halfWidth;
                y += height + distance;
                break;
            case 'inside':
                x += halfWidth;
                y += halfHeight;
                break;
            case 'insideLeft':
                x += distance;
                y += halfHeight;
                break;
            case 'insideRight':
                x += width - textWidth - distance;
                y += halfHeight;
                break;
            case 'insideTop':
                x += halfWidth;
                y += distance;
                break;
            case 'insideBottom':
                x += halfWidth;
                y += height - textHeight - distance;
                break;
        }<commit_msg>Rect text position add insideLeftTop, insideLeftBottom, insideRightTop, insideRightBottom<commit_after>define(function (require) {

    var textWidthCache = {};
    var textWidthCacheCounter = 0;
    var TEXT_CACHE_MAX = 5000;

    var util = require('../core/util');
    var BoundingRect = require('../core/BoundingRect');

    function getTextWidth(text, textFont) {
        var key = text + ':' + textFont;
        if (textWidthCache[key]) {
            return textWidthCache[key];
        }

        var textLines = (text + '').split('\n');
        var width = 0;

        for (var i = 0, l = textLines.length; i < l; i++) {
            // measureText 可以被覆盖以兼容不支持 Canvas 的环境
            width =  Math.max(textContain.measureText(textLines[i], textFont).width, width);
        }

        if (textWidthCacheCounter > TEXT_CACHE_MAX) {
            textWidthCacheCounter = 0;
            textWidthCache = {};
        }
        textWidthCacheCounter++;
        textWidthCache[key] = width;

        return width;
    };

    function getTextRect(text, textFont, textAlign, textBaseline) {
        var textLineLen = text.split('\n').length;

        var width = getTextWidth(text, textFont);
        // FIXME 高度计算比较粗暴
        var lineHeight = getTextWidth('国', textFont);
        var height = textLineLen * lineHeight;

        var rect = new BoundingRect(0, 0, width, height)
        // Text has a special line height property
        rect.lineHeight = lineHeight;

        switch (textBaseline) {
            case 'bottom':
            case 'alphabetic':
                rect.y -= lineHeight;
                break;
            case 'middle':
                rect.y -= lineHeight / 2;
                break;
            // case 'hanging':
            // case 'top':
        };

        // FIXME Right to left language
        switch (textAlign) {
            case 'end':
            case 'right':
                rect.x -= rect.width;
                break;
            case 'center':
                rect.x -= rect.width / 2;
                break;
            // case 'start':
            // case 'left':
        }

        return rect;
    };

    function adjustTextPositionOnRect(textPosition, rect, textRect, distance) {

        var x = rect.x;
        var y = rect.y;

        var height = rect.height;
        var width = rect.width;

        var textWidth = textRect.width;
        var textHeight = textRect.height;

        var halfWidth = width / 2 - textWidth / 2;
        var halfHeight = height / 2 - textHeight / 2;

        switch (textPosition) {
            case 'left':
                x -= distance + textWidth;
                y += halfHeight;
                break;
            case 'right':
                x += width + distance;
                y += halfHeight;
                break;
            case 'top':
                x += halfWidth;
                y -= distance + textHeight;
                break;
            case 'bottom':
                x += halfWidth;
                y += height + distance;
                break;
            case 'inside':
                x += halfWidth;
                y += halfHeight;
                break;
            case 'insideLeft':
                x += distance;
                y += halfHeight;
                break;
            case 'insideRight':
                x += width - textWidth - distance;
                y += halfHeight;
                break;
            case 'insideTop':
                x += halfWidth;
                y += distance;
                break;
            case 'insideBottom':
                x += halfWidth;
                y += height - textHeight - distance;
                break;
            case 'insideTopLeft':
                x += distance;
                y += distance;
                break;
            case 'insideTopRight':
                x += width - textWidth - distance;
                y += distance;
                break;
            case 'insideBottomLeft':
                x += distance;
                y += height - textHeight - distance;
                break;
            case 'insideBottomRight':
                x += width - textWidth - distance;
                y += height - textHeight - distance;
                break;

        }
{'fraction_non_alphanumeric': 0.10140880822439396, 'fraction_numerical': 0.004061429115369971, 'mean_word_length': 1.229137199434229, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1958608', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2001, 'n_words': 549}